feat(server): Extend string_map api, fix AddOrSet method.

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
Roman Gershman 2022-12-22 21:22:42 +02:00 committed by Roman Gershman
parent 9375a48f71
commit 5d34f271e9
7 changed files with 118 additions and 22 deletions

View file

@ -313,7 +313,7 @@ void DenseSet::Grow() {
}
}
bool DenseSet::AddInternal(void* ptr, bool has_ttl) {
void* DenseSet::AddOrFind(void* ptr, bool has_ttl) {
uint64_t hc = Hash(ptr, 0);
if (entries_.empty()) {
@ -325,13 +325,14 @@ bool DenseSet::AddInternal(void* ptr, bool has_ttl) {
++size_;
++num_used_buckets_;
return true;
return nullptr;
}
// if the value is already in the set exit early
uint32_t bucket_id = BucketId(hc);
if (Find(ptr, bucket_id, 0).second != nullptr) {
return false;
DensePtr* dptr = Find(ptr, bucket_id, 0).second;
if (dptr != nullptr) {
return dptr->GetObject();
}
DCHECK_LT(bucket_id, entries_.size());
@ -347,7 +348,7 @@ bool DenseSet::AddInternal(void* ptr, bool has_ttl) {
}
++num_used_buckets_;
++size_;
return true;
return nullptr;
}
if (size_ < entries_.size()) {
@ -392,7 +393,7 @@ bool DenseSet::AddInternal(void* ptr, bool has_ttl) {
DCHECK(!entries_[bucket_id].IsDisplaced());
++size_;
return true;
return nullptr;
}
auto DenseSet::Find(const void* ptr, uint32_t bid, uint32_t cookie) -> pair<DensePtr*, DensePtr*> {