leveldb
Public Member Functions | Private Member Functions | Private Attributes | List of all members
leveldb::anonymous_namespace{cache.cc}::LRUCache Class Reference
Collaboration diagram for leveldb::anonymous_namespace{cache.cc}::LRUCache:
Collaboration graph
[legend]

Public Member Functions

 LRUCache ()
 
 ~LRUCache ()
 
void SetCapacity (size_t capacity)
 
Cache::HandleInsert (const Slice &key, uint32_t hash, void *value, size_t charge, void(*deleter)(const Slice &key, void *value))
 
Cache::HandleLookup (const Slice &key, uint32_t hash)
 
void Release (Cache::Handle *handle)
 
void Erase (const Slice &key, uint32_t hash)
 
void Prune ()
 
size_t TotalCharge () const
 

Private Member Functions

void LRU_Remove (LRUHandle *e)
 
void LRU_Append (LRUHandle *list, LRUHandle *e)
 
void Ref (LRUHandle *e)
 
void Unref (LRUHandle *e)
 
bool FinishErase (LRUHandle *e)
 

Private Attributes

size_t capacity_
 
port::Mutex mutex_
 
size_t usage_
 
LRUHandle lru_
 
LRUHandle in_use_
 
HandleTable table_
 

Detailed Description

Definition at line 153 of file cache.cc.

Constructor & Destructor Documentation

§ LRUCache()

leveldb::anonymous_namespace{cache.cc}::LRUCache::LRUCache ( )

Definition at line 200 of file cache.cc.

§ ~LRUCache()

leveldb::anonymous_namespace{cache.cc}::LRUCache::~LRUCache ( )

Definition at line 209 of file cache.cc.

209  {
210  assert(in_use_.next == &in_use_); // Error if caller has an unreleased handle
211  for (LRUHandle* e = lru_.next; e != &lru_; ) {
212  LRUHandle* next = e->next;
213  assert(e->in_cache);
214  e->in_cache = false;
215  assert(e->refs == 1); // Invariant of lru_ list.
216  Unref(e);
217  e = next;
218  }
219 }
Here is the call graph for this function:

Member Function Documentation

§ Erase()

void leveldb::anonymous_namespace{cache.cc}::LRUCache::Erase ( const Slice key,
uint32_t  hash 
)

Definition at line 318 of file cache.cc.

318  {
319  MutexLock l(&mutex_);
320  FinishErase(table_.Remove(key, hash));
321 }
LRUHandle * Remove(const Slice &key, uint32_t hash)
Definition: cache.cc:96
Here is the call graph for this function:
Here is the caller graph for this function:

§ FinishErase()

bool leveldb::anonymous_namespace{cache.cc}::LRUCache::FinishErase ( LRUHandle e)
private

Definition at line 307 of file cache.cc.

307  {
308  if (e != NULL) {
309  assert(e->in_cache);
310  LRU_Remove(e);
311  e->in_cache = false;
312  usage_ -= e->charge;
313  Unref(e);
314  }
315  return e != NULL;
316 }
Here is the call graph for this function:
Here is the caller graph for this function:

§ Insert()

Cache::Handle * leveldb::anonymous_namespace{cache.cc}::LRUCache::Insert ( const Slice key,
uint32_t  hash,
void *  value,
size_t  charge,
void(*)(const Slice &key, void *value)  deleter 
)

Definition at line 269 of file cache.cc.

271  {
272  MutexLock l(&mutex_);
273 
274  LRUHandle* e = reinterpret_cast<LRUHandle*>(
275  malloc(sizeof(LRUHandle)-1 + key.size()));
276  e->value = value;
277  e->deleter = deleter;
278  e->charge = charge;
279  e->key_length = key.size();
280  e->hash = hash;
281  e->in_cache = false;
282  e->refs = 1; // for the returned handle.
283  memcpy(e->key_data, key.data(), key.size());
284 
285  if (capacity_ > 0) {
286  e->refs++; // for the cache's reference.
287  e->in_cache = true;
288  LRU_Append(&in_use_, e);
289  usage_ += charge;
291  } // else don't cache. (Tests use capacity_==0 to turn off caching.)
292 
293  while (usage_ > capacity_ && lru_.next != &lru_) {
294  LRUHandle* old = lru_.next;
295  assert(old->refs == 1);
296  bool erased = FinishErase(table_.Remove(old->key(), old->hash));
297  if (!erased) { // to avoid unused variable when compiled NDEBUG
298  assert(erased);
299  }
300  }
301 
302  return reinterpret_cast<Cache::Handle*>(e);
303 }
void LRU_Append(LRUHandle *list, LRUHandle *e)
Definition: cache.cc:247
LRUHandle * Remove(const Slice &key, uint32_t hash)
Definition: cache.cc:96
Here is the call graph for this function:
Here is the caller graph for this function:

§ Lookup()

Cache::Handle * leveldb::anonymous_namespace{cache.cc}::LRUCache::Lookup ( const Slice key,
uint32_t  hash 
)

Definition at line 255 of file cache.cc.

255  {
256  MutexLock l(&mutex_);
257  LRUHandle* e = table_.Lookup(key, hash);
258  if (e != NULL) {
259  Ref(e);
260  }
261  return reinterpret_cast<Cache::Handle*>(e);
262 }
LRUHandle * Lookup(const Slice &key, uint32_t hash)
Definition: cache.cc:76
Here is the call graph for this function:
Here is the caller graph for this function:

§ LRU_Append()

void leveldb::anonymous_namespace{cache.cc}::LRUCache::LRU_Append ( LRUHandle list,
LRUHandle e 
)
private

Definition at line 247 of file cache.cc.

247  {
248  // Make "e" newest entry by inserting just before *list
249  e->next = list;
250  e->prev = list->prev;
251  e->prev->next = e;
252  e->next->prev = e;
253 }
Here is the caller graph for this function:

§ LRU_Remove()

void leveldb::anonymous_namespace{cache.cc}::LRUCache::LRU_Remove ( LRUHandle e)
private

Definition at line 242 of file cache.cc.

242  {
243  e->next->prev = e->prev;
244  e->prev->next = e->next;
245 }
Here is the caller graph for this function:

§ Prune()

void leveldb::anonymous_namespace{cache.cc}::LRUCache::Prune ( )

Definition at line 323 of file cache.cc.

323  {
324  MutexLock l(&mutex_);
325  while (lru_.next != &lru_) {
326  LRUHandle* e = lru_.next;
327  assert(e->refs == 1);
328  bool erased = FinishErase(table_.Remove(e->key(), e->hash));
329  if (!erased) { // to avoid unused variable when compiled NDEBUG
330  assert(erased);
331  }
332  }
333 }
LRUHandle * Remove(const Slice &key, uint32_t hash)
Definition: cache.cc:96
Here is the call graph for this function:
Here is the caller graph for this function:

§ Ref()

void leveldb::anonymous_namespace{cache.cc}::LRUCache::Ref ( LRUHandle e)
private

Definition at line 221 of file cache.cc.

221  {
222  if (e->refs == 1 && e->in_cache) { // If on lru_ list, move to in_use_ list.
223  LRU_Remove(e);
224  LRU_Append(&in_use_, e);
225  }
226  e->refs++;
227 }
void LRU_Append(LRUHandle *list, LRUHandle *e)
Definition: cache.cc:247
Here is the call graph for this function:
Here is the caller graph for this function:

§ Release()

void leveldb::anonymous_namespace{cache.cc}::LRUCache::Release ( Cache::Handle handle)

Definition at line 264 of file cache.cc.

264  {
265  MutexLock l(&mutex_);
266  Unref(reinterpret_cast<LRUHandle*>(handle));
267 }
Here is the call graph for this function:
Here is the caller graph for this function:

§ SetCapacity()

void leveldb::anonymous_namespace{cache.cc}::LRUCache::SetCapacity ( size_t  capacity)
inline

Definition at line 159 of file cache.cc.

Here is the call graph for this function:
Here is the caller graph for this function:

§ TotalCharge()

size_t leveldb::anonymous_namespace{cache.cc}::LRUCache::TotalCharge ( ) const
inline

Definition at line 169 of file cache.cc.

169  {
170  MutexLock l(&mutex_);
171  return usage_;
172  }
Here is the call graph for this function:
Here is the caller graph for this function:

§ Unref()

void leveldb::anonymous_namespace{cache.cc}::LRUCache::Unref ( LRUHandle e)
private

Definition at line 229 of file cache.cc.

229  {
230  assert(e->refs > 0);
231  e->refs--;
232  if (e->refs == 0) { // Deallocate.
233  assert(!e->in_cache);
234  (*e->deleter)(e->key(), e->value);
235  free(e);
236  } else if (e->in_cache && e->refs == 1) { // No longer in use; move to lru_ list.
237  LRU_Remove(e);
238  LRU_Append(&lru_, e);
239  }
240 }
void LRU_Append(LRUHandle *list, LRUHandle *e)
Definition: cache.cc:247
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

§ capacity_

size_t leveldb::anonymous_namespace{cache.cc}::LRUCache::capacity_
private

Definition at line 182 of file cache.cc.

§ in_use_

LRUHandle leveldb::anonymous_namespace{cache.cc}::LRUCache::in_use_
private

Definition at line 195 of file cache.cc.

§ lru_

LRUHandle leveldb::anonymous_namespace{cache.cc}::LRUCache::lru_
private

Definition at line 191 of file cache.cc.

§ mutex_

port::Mutex leveldb::anonymous_namespace{cache.cc}::LRUCache::mutex_
mutableprivate

Definition at line 185 of file cache.cc.

§ table_

HandleTable leveldb::anonymous_namespace{cache.cc}::LRUCache::table_
private

Definition at line 197 of file cache.cc.

§ usage_

size_t leveldb::anonymous_namespace{cache.cc}::LRUCache::usage_
private

Definition at line 186 of file cache.cc.


The documentation for this class was generated from the following file: