From 52ad7c30b72d9a55752cfb837b2e6fb961c29f03 Mon Sep 17 00:00:00 2001 From: haoxiang47 Date: Fri, 21 Feb 2020 22:52:24 +0800 Subject: [PATCH] add prioritized cache wrapper Signed-off-by: haoxiang47 --- include/titan/prioritized_cache.h | 29 +++++++++++++++++++++++++++++ src/prioritized_cache.cc | 23 +++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 include/titan/prioritized_cache.h create mode 100644 src/prioritized_cache.cc diff --git a/include/titan/prioritized_cache.h b/include/titan/prioritized_cache.h new file mode 100644 index 000000000..10edff1fb --- /dev/null +++ b/include/titan/prioritized_cache.h @@ -0,0 +1,29 @@ +#pragma once +#include "rocksdb/cache.h" + +namespace rocksdb { +namespace titandb { + +class PrioritizedCache : public Cache { + public: + // Constructs a PrioritizedCache, which is a wrapper + // of rocksdb Cache + PrioritizedCache(std::shared_ptr cache, Cache::Priority priority); + ~PrioritizedCache(); + + // Get the Cache ptr + std::shared_ptr GetCache(); + + // always insert into the cache with the priority when init, + // regardless of user provided option + Status Insert(const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), + Cache::Handle** handle, Cache::Priority /*priority*/) override; + + private: + std::shared_ptr cache_; + Cache::Priority priority_; +}; + +} // namespace titandb +} // namespace rocksdb \ No newline at end of file diff --git a/src/prioritized_cache.cc b/src/prioritized_cache.cc new file mode 100644 index 000000000..385226ef3 --- /dev/null +++ b/src/prioritized_cache.cc @@ -0,0 +1,23 @@ +#include "titan/prioritized_cache.h" + +namespace rocksdb { +namespace titandb { + +PrioritizedCache::PrioritizedCache(std::shared_ptr cache, + Cache::Priority priority) + : cache_(cache), priority_(priority) {} + +PrioritizedCache::~PrioritizedCache() {} + +std::shared_ptr PrioritizedCache::GetCache() { return cache_; } + +Status PrioritizedCache::Insert(const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), + Cache::Handle** handle, + Cache::Priority /*priority*/) { + assert(cache_); + return cache_->Insert(key, value, charge, deleter, handle, priority_); +} + +} // namespace titandb +} // namespace rocksdb \ No newline at end of file