Skip to content

Commit 19e67a7

Browse files
committed
Refactor Cache::Erase return value
1 parent feefe31 commit 19e67a7

11 files changed

Lines changed: 24 additions & 19 deletions

File tree

cache/cache_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ class CacheTest : public testing::TestWithParam<std::string> {
115115
&CacheTest::Deleter);
116116
}
117117

118-
void Erase(std::shared_ptr<Cache> cache, int key) {
119-
cache->Erase(EncodeKey(key));
118+
bool Erase(std::shared_ptr<Cache> cache, int key) {
119+
return cache->Erase(EncodeKey(key));
120120
}
121121

122122
int Lookup(int key) { return Lookup(cache_, key); }
@@ -125,15 +125,15 @@ class CacheTest : public testing::TestWithParam<std::string> {
125125
Insert(cache_, key, value, charge);
126126
}
127127

128-
void Erase(int key) { Erase(cache_, key); }
128+
bool Erase(int key) { return Erase(cache_, key); }
129129

130130
int Lookup2(int key) { return Lookup(cache2_, key); }
131131

132132
void Insert2(int key, int value, int charge = 1) {
133133
Insert(cache2_, key, value, charge);
134134
}
135135

136-
void Erase2(int key) { Erase(cache2_, key); }
136+
bool Erase2(int key) { return Erase(cache2_, key); }
137137
};
138138
CacheTest* CacheTest::current_;
139139

cache/clock_cache.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class ClockCacheShard : public CacheShard {
261261
virtual bool Ref(Cache::Handle* handle) override;
262262
virtual bool Release(Cache::Handle* handle,
263263
bool force_erase = false) override;
264-
virtual void Erase(const Slice& key, uint32_t hash) override;
264+
virtual bool Erase(const Slice& key, uint32_t hash) override;
265265
bool EraseAndConfirm(const Slice& key, uint32_t hash,
266266
CleanupContext* context);
267267
virtual size_t GetUsage() const override;
@@ -647,10 +647,11 @@ bool ClockCacheShard::Release(Cache::Handle* h, bool force_erase) {
647647
return erased;
648648
}
649649

650-
void ClockCacheShard::Erase(const Slice& key, uint32_t hash) {
650+
bool ClockCacheShard::Erase(const Slice& key, uint32_t hash) {
651651
CleanupContext context;
652-
EraseAndConfirm(key, hash, &context);
652+
bool ret EraseAndConfirm(key, hash, &context);
653653
Cleanup(context);
654+
return ret;
654655
}
655656

656657
bool ClockCacheShard::EraseAndConfirm(const Slice& key, uint32_t hash,

cache/lirs_cache.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ Status LIRSCacheShard::Insert(const Slice& key, uint32_t hash, void* value,
407407
return s;
408408
}
409409

410-
void LIRSCacheShard::Erase(const Slice& key, uint32_t hash) {
410+
bool LIRSCacheShard::Erase(const Slice& key, uint32_t hash) {
411411
LIRSHandle* e;
412412
bool last_reference = false;
413413
{
@@ -430,6 +430,7 @@ void LIRSCacheShard::Erase(const Slice& key, uint32_t hash) {
430430
if (last_reference) {
431431
e->Free();
432432
}
433+
return e != nullptr;
433434
}
434435

435436
size_t LIRSCacheShard::GetUsage() const {

cache/lirs_cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class ALIGN_AS(CACHE_LINE_SIZE) LIRSCacheShard : public CacheShard {
106106
virtual bool Ref(Cache::Handle* handle) override;
107107
virtual bool Release(Cache::Handle* handle,
108108
bool force_erase = false) override;
109-
virtual void Erase(const Slice& key, uint32_t hash) override;
109+
virtual bool Erase(const Slice& key, uint32_t hash) override;
110110

111111
virtual size_t GetUsage() const override;
112112
virtual size_t GetPinnedUsage() const override;

cache/lru_cache.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ Status LRUCacheShardTemplate<CacheMonitor>::Insert(
438438
}
439439

440440
template <class CacheMonitor>
441-
void LRUCacheShardTemplate<CacheMonitor>::Erase(const Slice& key,
441+
bool LRUCacheShardTemplate<CacheMonitor>::Erase(const Slice& key,
442442
uint32_t hash) {
443443
LRUHandle* e;
444444
bool last_reference = false;
@@ -462,6 +462,7 @@ void LRUCacheShardTemplate<CacheMonitor>::Erase(const Slice& key,
462462
if (last_reference) {
463463
e->Free();
464464
}
465+
return e != nullptr;
465466
}
466467

467468
template <class CacheMonitor>

cache/lru_cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class ALIGN_AS(CACHE_LINE_SIZE) LRUCacheShardTemplate : public CacheMonitor,
209209
virtual bool Ref(Cache::Handle* handle) override;
210210
virtual bool Release(Cache::Handle* handle,
211211
bool force_erase = false) override;
212-
virtual void Erase(const Slice& key, uint32_t hash) override;
212+
virtual bool Erase(const Slice& key, uint32_t hash) override;
213213

214214
// Although in some platforms the update of size_t is atomic, to make sure
215215
// GetUsage() and GetPinnedUsage() work correctly under any platform, we'll

cache/lru_cache_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class LRUCacheTest : public testing::Test,
8989
return cache_->Lookup(key, 0 /*hash*/);
9090
}
9191

92-
void Erase(const std::string& key) { cache_->Erase(key, 0 /*hash*/); }
92+
bool Erase(const std::string& key) { return cache_->Erase(key, 0 /*hash*/); }
9393

9494
void ValidateLRUList(std::vector<std::string> keys,
9595
size_t num_high_pri_pool_keys = 0) {

cache/sharded_cache.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ bool ShardedCache::Release(Handle* handle, bool force_erase) {
7171
return GetShard(Shard(hash))->Release(handle, force_erase);
7272
}
7373

74-
void ShardedCache::Erase(const Slice& key) {
74+
bool ShardedCache::Erase(const Slice& key) {
7575
uint32_t hash = HashSlice(key);
76-
GetShard(Shard(hash))->Erase(key, hash);
76+
return GetShard(Shard(hash))->Erase(key, hash);
7777
}
7878

7979
uint64_t ShardedCache::NewId() {

cache/sharded_cache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CacheShard {
3232
virtual Cache::Handle* Lookup(const Slice& key, uint32_t hash) = 0;
3333
virtual bool Ref(Cache::Handle* handle) = 0;
3434
virtual bool Release(Cache::Handle* handle, bool force_erase = false) = 0;
35-
virtual void Erase(const Slice& key, uint32_t hash) = 0;
35+
virtual bool Erase(const Slice& key, uint32_t hash) = 0;
3636
virtual void SetCapacity(size_t capacity) = 0;
3737
virtual void SetStrictCapacityLimit(bool strict_capacity_limit) = 0;
3838
virtual size_t GetUsage() const = 0;
@@ -68,7 +68,7 @@ class ShardedCache : public Cache {
6868
virtual Handle* Lookup(const Slice& key, Statistics* stats) override;
6969
virtual bool Ref(Handle* handle) override;
7070
virtual bool Release(Handle* handle, bool force_erase = false) override;
71-
virtual void Erase(const Slice& key) override;
71+
virtual bool Erase(const Slice& key) override;
7272
virtual uint64_t NewId() override;
7373
virtual size_t GetCapacity() const override;
7474
virtual bool HasStrictCapacityLimit() const override;

include/rocksdb/cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ class Cache {
221221
// If the cache contains entry for key, erase it. Note that the
222222
// underlying entry will be kept around until all existing handles
223223
// to it have been released.
224-
virtual void Erase(const Slice& key) = 0;
224+
// Return false if erase a non-exists key
225+
virtual bool Erase(const Slice& key) = 0;
225226
// Return a new numeric id. May be used by multiple clients who are
226227
// sharding the same cache to partition the key space. Typically the
227228
// client will allocate a new id at startup and prepend the id to

0 commit comments

Comments
 (0)