ApplyToAllCacheEntries

Summary: Added a method that executes a callback on every cache entry.

Test Plan: added a unit test

Reviewers: haobo

Reviewed By: haobo

CC: leveldb, dhruba

Differential Revision: https://reviews.facebook.net/D18441
This commit is contained in:
Igor Canadi
2014-05-02 16:24:04 -04:00
parent 31d38a6732
commit 4ecfbcf865
3 changed files with 52 additions and 0 deletions

View File

@@ -164,6 +164,9 @@ class LRUCache {
return usage_;
}
void ApplyToAllCacheEntries(void (*callback)(void*, size_t),
bool thread_safe);
private:
void LRU_Remove(LRUHandle* e);
void LRU_Append(LRUHandle* e);
@@ -220,6 +223,19 @@ void LRUCache::FreeEntry(LRUHandle* e) {
free(e);
}
void LRUCache::ApplyToAllCacheEntries(void (*callback)(void*, size_t),
bool thread_safe) {
if (thread_safe) {
mutex_.Lock();
}
for (auto e = lru_.next; e != &lru_; e = e->next) {
callback(e->value, e->charge);
}
if (thread_safe) {
mutex_.Unlock();
}
}
void LRUCache::LRU_Remove(LRUHandle* e) {
e->next->prev = e->prev;
e->prev->next = e->next;
@@ -432,6 +448,14 @@ class ShardedLRUCache : public Cache {
virtual void DisownData() {
shards_ = nullptr;
}
virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t),
bool thread_safe) override {
int num_shards = 1 << num_shard_bits_;
for (int s = 0; s < num_shards; s++) {
shards_[s].ApplyToAllCacheEntries(callback, thread_safe);
}
}
};
} // end anonymous namespace