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 21, 2020
1 parent 4dc4ba8 commit 25fc0ff
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/prioritized_cache.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "prioritized_cache.h"

namespace rocksdb {
namespace titandb {

PrioritizedCache::PrioritizedCache(std::shared_ptr<Cache> cache) :
cache_(cache) {}

std::shared_ptr<Cache> 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
37 changes: 37 additions & 0 deletions src/prioritized_cache.h
Original file line number Diff line number Diff line change
@@ -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> cache);
~PrioritizedCache();

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


} // namespace titandb
} // namespace rocksdb

0 comments on commit 25fc0ff

Please sign in to comment.