From 88e2653032057e71570525f240e26f7a2e7e3143 Mon Sep 17 00:00:00 2001 From: Luan Santos Date: Sat, 9 Dec 2023 19:17:42 -0800 Subject: [PATCH] fix tests --- src/kv/kv.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/kv/kv.cpp b/src/kv/kv.cpp index 0e2f4b2f836..f3133dec9ca 100644 --- a/src/kv/kv.cpp +++ b/src/kv/kv.cpp @@ -32,7 +32,7 @@ void KVStore::set(const std::string &key, const ValueWrapper &value) { } void KVStore::setLocked(const std::string &key, const ValueWrapper &value) { - logger.debug("KVStore::set({})", key); + logger.trace("KVStore::set({})", key); auto it = store_.find(key); if (it != store_.end()) { it->second.first = value; @@ -53,7 +53,7 @@ void KVStore::setLocked(const std::string &key, const ValueWrapper &value) { } std::optional KVStore::get(const std::string &key, bool forceLoad /*= false */) { - logger.debug("KVStore::get({})", key); + logger.trace("KVStore::get({})", key); std::scoped_lock lock(mutex_); if (forceLoad || !store_.contains(key)) { auto value = load(key); @@ -72,11 +72,26 @@ std::optional KVStore::get(const std::string &key, bool forceLoad return value; } +std::unordered_set KVStore::keys(const std::string &prefix /*= ""*/) { + std::scoped_lock lock(mutex_); + std::unordered_set keys; + for (const auto &[key, value] : store_) { + if (key.find(prefix) == 0) { + std::string suffix = key.substr(prefix.size()); + keys.insert(suffix); + } + } + for (const auto &key : loadPrefix(prefix)) { + keys.insert(key); + } + return keys; +} + void KV::remove(const std::string &key) { set(key, ValueWrapper::deleted()); } std::shared_ptr KVStore::scoped(const std::string &scope) { - logger.debug("KVStore::scoped({})", scope); + logger.trace("KVStore::scoped({})", scope); return std::make_shared(logger, *this, scope); }