10 #include "port/port.h" 44 void (*deleter)(
const Slice&,
void* value);
59 return *(
reinterpret_cast<Slice*
>(value));
61 return Slice(key_data, key_length);
73 HandleTable() : length_(0), elems_(0), list_(NULL) { Resize(); }
77 return *FindPointer(key, hash);
83 h->
next_hash = (old == NULL ? NULL : old->next_hash);
87 if (elems_ > length_) {
117 LRUHandle** ptr = &list_[hash & (length_ - 1)];
118 while (*ptr != NULL &&
119 ((*ptr)->hash != hash || key != (*ptr)->key())) {
126 uint32_t new_length = 4;
127 while (new_length < elems_) {
131 memset(new_list, 0,
sizeof(new_list[0]) * new_length);
133 for (uint32_t i = 0; i < length_; i++) {
137 uint32_t hash = h->
hash;
138 LRUHandle** ptr = &new_list[hash & (new_length - 1)];
145 assert(elems_ == count);
148 length_ = new_length;
163 void* value,
size_t charge,
164 void (*deleter)(
const Slice& key,
void* value));
215 assert(e->refs == 1);
266 Unref(reinterpret_cast<LRUHandle*>(handle));
270 const Slice& key, uint32_t hash,
void* value,
size_t charge,
271 void (*deleter)(
const Slice& key,
void* value)) {
295 assert(old->
refs == 1);
327 assert(e->
refs == 1);
348 static uint32_t
Shard(uint32_t hash) {
355 const size_t per_shard = (capacity + (kNumShards - 1)) /
kNumShards;
362 void (*deleter)(
const Slice& key,
void* value)) {
363 const uint32_t hash = HashSlice(key);
364 return shard_[Shard(hash)].
Insert(key, hash, value, charge, deleter);
367 const uint32_t hash = HashSlice(key);
368 return shard_[Shard(hash)].
Lookup(key, hash);
375 const uint32_t hash = HashSlice(key);
376 shard_[Shard(hash)].
Erase(key, hash);
379 return reinterpret_cast<LRUHandle*
>(handle)->value;
402 return new ShardedLRUCache(capacity);
void LRU_Append(LRUHandle *list, LRUHandle *e)
static uint32_t HashSlice(const Slice &s)
void LRU_Append(Handle *e)
virtual Handle * Insert(const Slice &key, void *value, size_t charge, void(*deleter)(const Slice &key, void *value))=0
LRUHandle * Insert(LRUHandle *h)
virtual void Release(Handle *handle)
uint32_t Hash(const char *data, size_t n, uint32_t seed)
LRUHandle ** FindPointer(const Slice &key, uint32_t hash)
size_t TotalCharge() const
virtual Handle * Lookup(const Slice &key)
virtual void Release(Handle *handle)=0
void(* deleter)(const Slice &, void *value)
virtual void Erase(const Slice &key)
void LRU_Remove(Handle *e)
Cache::Handle * Lookup(const Slice &key, uint32_t hash)
virtual Handle * Lookup(const Slice &key)=0
virtual size_t TotalCharge() const
ShardedLRUCache(size_t capacity)
bool FinishErase(LRUHandle *e)
virtual void * Value(Handle *handle)
void Erase(const Slice &key, uint32_t hash)
static const int kNumShardBits
void LRU_Remove(LRUHandle *e)
LRUHandle * Remove(const Slice &key, uint32_t hash)
void SetCapacity(size_t capacity)
void Release(Cache::Handle *handle)
Cache * NewLRUCache(size_t capacity)
LRUHandle * Lookup(const Slice &key, uint32_t hash)
static const int kNumShards
virtual ~ShardedLRUCache()
static uint32_t Shard(uint32_t hash)
virtual Handle * Insert(const Slice &key, void *value, size_t charge, void(*deleter)(const Slice &key, void *value))
virtual void Erase(const Slice &key)=0
const char * data() const
Cache::Handle * Insert(const Slice &key, uint32_t hash, void *value, size_t charge, void(*deleter)(const Slice &key, void *value))