Skip to content

Commit 359b6bc

Browse files
tzikcmumford
authored andcommitted
Add leveldb::Cache::Prune
Prune() drops on-memory read cache of the database, so that the client can relief its memory shortage. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=101335710
1 parent 50e77a8 commit 359b6bc

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

include/leveldb/cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ class Cache {
8181
// its cache keys.
8282
virtual uint64_t NewId() = 0;
8383

84+
// Remove all cache entries that are not actively in use. Memory-constrained
85+
// applications may wish to call this method to reduce memory usage.
86+
// Default implementation of Prune() does nothing. Subclasses are strongly
87+
// encouraged to override the default implementation. A future release of
88+
// leveldb may change Prune() to a pure abstract method.
89+
virtual void Prune() {}
90+
8491
private:
8592
void LRU_Remove(Handle* e);
8693
void LRU_Append(Handle* e);

util/cache.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class LRUCache {
147147
Cache::Handle* Lookup(const Slice& key, uint32_t hash);
148148
void Release(Cache::Handle* handle);
149149
void Erase(const Slice& key, uint32_t hash);
150+
void Prune();
150151

151152
private:
152153
void LRU_Remove(LRUHandle* e);
@@ -264,6 +265,19 @@ void LRUCache::Erase(const Slice& key, uint32_t hash) {
264265
}
265266
}
266267

268+
void LRUCache::Prune() {
269+
MutexLock l(&mutex_);
270+
for (LRUHandle* e = lru_.next; e != &lru_; ) {
271+
LRUHandle* next = e->next;
272+
if (e->refs == 1) {
273+
table_.Remove(e->key(), e->hash);
274+
LRU_Remove(e);
275+
Unref(e);
276+
}
277+
e = next;
278+
}
279+
}
280+
267281
static const int kNumShardBits = 4;
268282
static const int kNumShards = 1 << kNumShardBits;
269283

@@ -314,6 +328,11 @@ class ShardedLRUCache : public Cache {
314328
MutexLock l(&id_mutex_);
315329
return ++(last_id_);
316330
}
331+
virtual void Prune() {
332+
for (int s = 0; s < kNumShards; s++) {
333+
shard_[s].Prune();
334+
}
335+
}
317336
};
318337

319338
} // end anonymous namespace

util/cache_test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ TEST(CacheTest, NewId) {
179179
ASSERT_NE(a, b);
180180
}
181181

182+
TEST(CacheTest, Prune) {
183+
Insert(1, 100);
184+
Insert(2, 200);
185+
186+
Cache::Handle* handle = cache_->Lookup(EncodeKey(1));
187+
ASSERT_TRUE(handle);
188+
cache_->Prune();
189+
cache_->Release(handle);
190+
191+
ASSERT_EQ(100, Lookup(1));
192+
ASSERT_EQ(-1, Lookup(2));
193+
}
194+
182195
} // namespace leveldb
183196

184197
int main(int argc, char** argv) {

0 commit comments

Comments
 (0)