From 9e0acde5cfa5eaca84839165e453ae3108f0ef81 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 14 May 2018 16:53:28 -0700 Subject: [PATCH] ircd::db: Add cache entry iteration (experimental). --- include/ircd/db/cache.h | 5 +++++ ircd/db.cc | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/ircd/db/cache.h b/include/ircd/db/cache.h index e2a91bae6..1d92a504c 100644 --- a/include/ircd/db/cache.h +++ b/include/ircd/db/cache.h @@ -36,4 +36,9 @@ namespace ircd::db // Test if key exists bool exists(rocksdb::Cache &, const string_view &key); bool exists(rocksdb::Cache *const &, const string_view &key); + + // Iterate the cache entries. + using cache_closure = std::function; + void for_each(rocksdb::Cache &, const cache_closure &); + void for_each(rocksdb::Cache *const &, const cache_closure &); } diff --git a/ircd/db.cc b/ircd/db.cc index bbf0f1f41..8e7da9b71 100644 --- a/ircd/db.cc +++ b/ircd/db.cc @@ -5074,6 +5074,50 @@ ircd::db::_seek_(rocksdb::Iterator &it, // cache.h // +void +ircd::db::for_each(rocksdb::Cache *const &cache, + const cache_closure &closure) +{ + if(cache) + for_each(*cache, closure); +} + +void +ircd::db::for_each(rocksdb::Cache &cache, + const cache_closure &closure) +{ + thread_local rocksdb::Cache *_cache; + _cache = &cache; + + thread_local const cache_closure *_closure; + _closure = &closure; + + cache.ApplyToAllCacheEntries([] + (void *const data, const size_t charge) + { + assert(_cache); + assert(_closure); + auto *const &handle + { + reinterpret_cast(data) + }; + + const void *const &value + { + _cache->Value(handle) + }; + + assert(value); + const auto &s + { + *reinterpret_cast(value) + }; + + (*_closure)(slice(s), charge); + }, + false); +} + bool ircd::db::exists(rocksdb::Cache *const &cache, const string_view &key)