diff --git a/src/prioritized_cache.cc b/src/prioritized_cache.cc new file mode 100644 index 000000000..edeec2beb --- /dev/null +++ b/src/prioritized_cache.cc @@ -0,0 +1,26 @@ +#include "prioritized_cache.h" + +namespace rocksdb { +namespace titandb { + +PrioritizedCache::PrioritizedCache(std::shared_ptr cache) : + cache_(cache) {} + +std::shared_ptr PrioritizedCache::GetPrioritizedCache() { + return cache_; +} + +Status PrioritizedCache::HighPriCacheInsert(const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), + Cache::Handle** handle) { + return cache_->Insert(key, value, charge, deleter, handle, Cache::Priority::HIGH); +} + +Status PrioritizedCache::LowPriCacheInsert(const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), + Cache::Handle** handle) { + return cache_->Insert(key, value, charge, deleter, handle, Cache::Priority::LOW); +} + +} // namespace titandb +} // namespace rocksdb \ No newline at end of file diff --git a/src/prioritized_cache.h b/src/prioritized_cache.h new file mode 100644 index 000000000..89d91221b --- /dev/null +++ b/src/prioritized_cache.h @@ -0,0 +1,37 @@ +#pragma once +#include "rocksdb/cache.h" +#include "rocksdb/env.h" + +namespace rocksdb { +namespace titandb { + +class PrioritizedCache { +public: + + // Constructs a PrioritizedCache, which is a wrapper + // of rocksdb Cache + PrioritizedCache(std::shared_ptr cache); + ~PrioritizedCache(); + + // Get the Cache ptr + std::shared_ptr GetPrioritizedCache(); + + // always insert into the cache with high-priority, + // regardless of user provided option + Status HighPriCacheInsert(const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), + Cache::Handle** handle = nullptr); + + // always insert into the cache with low-priority, + // regardless of user provided option + Status LowPriCacheInsert(const Slice& key, void* value, size_t charge, + void (*deleter)(const Slice& key, void* value), + Cache::Handle** handle = nullptr); + +private: + std::shared_ptr cache_; +}; + + +} // namespace titandb +} // namespace rocksdb \ No newline at end of file