Skip to content

Commit

Permalink
add prioritized cache wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: haoxiang47 <[email protected]>
  • Loading branch information
haoxiang47 committed Feb 29, 2020
1 parent 4dc4ba8 commit f420771
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
35 changes: 35 additions & 0 deletions include/titan/prioritized_cache.h
Original file line number Diff line number Diff line change
@@ -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, Cache::Priority priority);
~PrioritizedCache();

// Get the Cache ptr
std::shared_ptr<Cache> 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_;
Cache::Priority priority_;
};

} // namespace titandb
} // namespace rocksdb
27 changes: 27 additions & 0 deletions src/prioritized_cache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "titan/prioritized_cache.h"

namespace rocksdb {
namespace titandb {

PrioritizedCache::PrioritizedCache(std::shared_ptr<Cache> cache,
Cache::Priority priority)
: cache_(cache), priority_(priority) {}

std::shared_ptr<Cache> 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

0 comments on commit f420771

Please sign in to comment.