From f420771f7a8e3cb825e1af0a1859db21e81b0f33 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 | 35 +++++++++++++++++++++++++++++++ src/prioritized_cache.cc | 27 ++++++++++++++++++++++++ 2 files changed, 62 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..7909e5ed0 --- /dev/null +++ b/include/titan/prioritized_cache.h @@ -0,0 +1,35 @@ +#pragma once +#include "rocksdb/cache.h" + +namespace rocksdb { +namespace titandb { + +class PrioritizedCache : 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: + // real insert function, it will check the cache ptr first + Status CheckCacheAndInsert(const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), + Cache::Handle** handle); + + 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..98bf2a1c3 --- /dev/null +++ b/src/prioritized_cache.cc @@ -0,0 +1,27 @@ +#include "titan/prioritized_cache.h" + +namespace rocksdb { +namespace titandb { + +PrioritizedCache::PrioritizedCache(std::shared_ptr cache, + Cache::Priority priority) + : cache_(cache), priority_(priority) {} + +std::shared_ptr PrioritizedCache::GetCache() { return cache_; } + +Status PrioritizedCache::CheckCacheAndInsert( + const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), Cache::Handle** handle) { + if (!cache_) return Status::NotFound(); + return cache_->Insert(key, value, charge, deleter, handle, priority_); +} + +Status PrioritizedCache::Insert(const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), + Cache::Handle** handle, + Cache::Priority /*priority*/) { + return CheckCacheAndInsert(key, value, charge, deleter, handle); +} + +} // namespace titandb +} // namespace rocksdb \ No newline at end of file