mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user