From accc8b8b1842ed6d522b71a056777bcac8f39e81 Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Thu, 30 Aug 2018 08:30:23 -0700 Subject: index: Access functions for global block filter indexes. --- src/index/blockfilterindex.cpp | 33 +++++++++++++++++++++++++++++++++ src/index/blockfilterindex.h | 26 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) (limited to 'src/index') diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp index a55aeca704..20f33baf2c 100644 --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -94,6 +94,8 @@ struct DBHashKey { }; // namespace +static std::map g_filter_indexes; + BlockFilterIndex::BlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size, bool f_memory, bool f_wipe) : m_filter_type(filter_type) @@ -432,3 +434,34 @@ bool BlockFilterIndex::LookupFilterHashRange(int start_height, const CBlockIndex } return true; } + +BlockFilterIndex* GetBlockFilterIndex(BlockFilterType filter_type) +{ + auto it = g_filter_indexes.find(filter_type); + return it != g_filter_indexes.end() ? &it->second : nullptr; +} + +void ForEachBlockFilterIndex(std::function fn) +{ + for (auto& entry : g_filter_indexes) fn(entry.second); +} + +bool InitBlockFilterIndex(BlockFilterType filter_type, + size_t n_cache_size, bool f_memory, bool f_wipe) +{ + auto result = g_filter_indexes.emplace(std::piecewise_construct, + std::forward_as_tuple(filter_type), + std::forward_as_tuple(filter_type, + n_cache_size, f_memory, f_wipe)); + return result.second; +} + +bool DestroyBlockFilterIndex(BlockFilterType filter_type) +{ + return g_filter_indexes.erase(filter_type); +} + +void DestroyAllBlockFilterIndexes() +{ + g_filter_indexes.clear(); +} diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h index a689461800..436d52515f 100644 --- a/src/index/blockfilterindex.h +++ b/src/index/blockfilterindex.h @@ -65,4 +65,30 @@ public: std::vector& hashes_out) const; }; +/** + * Get a block filter index by type. Returns nullptr if index has not been initialized or was + * already destroyed. + */ +BlockFilterIndex* GetBlockFilterIndex(BlockFilterType filter_type); + +/** Iterate over all running block filter indexes, invoking fn on each. */ +void ForEachBlockFilterIndex(std::function fn); + +/** + * Initialize a block filter index for the given type if one does not already exist. Returns true if + * a new index is created and false if one has already been initialized. + */ +bool InitBlockFilterIndex(BlockFilterType filter_type, + size_t n_cache_size, bool f_memory = false, bool f_wipe = false); + +/** + * Destroy the block filter index with the given type. Returns false if no such index exists. This + * just releases the allocated memory and closes the database connection, it does not delete the + * index data. + */ +bool DestroyBlockFilterIndex(BlockFilterType filter_type); + +/** Destroy all open block filter indexes. */ +void DestroyAllBlockFilterIndexes(); + #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H -- cgit v1.2.3