Skip to content

Commit 5dd96cd

Browse files
committed
chore: minor changes in dash code
Does not change functionality. bucket_count is currently constant for a segment so the additional arithmetics do not affect bucket_count_. Signed-off-by: Roman Gershman <roman@dragonflydb.io>
1 parent 0762cab commit 5dd96cd

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/core/dash.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ auto DashTable<_Key, _Value, Policy>::InsertInternal(U&& key, V&& value, Evictio
785785

786786
typename SegmentType::Iterator it;
787787
bool res = true;
788+
unsigned num_buckets = target->num_buckets();
788789
if (mode == InsertMode::kForceInsert) {
789790
it = target->InsertUniq(std::forward<U>(key), std::forward<V>(value), key_hash, true);
790791
res = it.found();
@@ -794,6 +795,8 @@ auto DashTable<_Key, _Value, Policy>::InsertInternal(U&& key, V&& value, Evictio
794795
}
795796

796797
if (res) { // success
798+
// in case segment bucket count changed, we need to update total bucket count.
799+
bucket_count_ + (target->num_buckets() - num_buckets);
797800
++size_;
798801
return std::make_pair(iterator{this, target_seg_id, it.index, it.slot}, true);
799802
}
@@ -905,7 +908,12 @@ void DashTable<_Key, _Value, Policy>::Split(uint32_t seg_id) {
905908

906909
auto hash_fn = [this](const auto& k) { return policy_.HashFn(k); };
907910

911+
// remove current segment bucket count.
912+
bucket_count_ -= (source->num_buckets() + target->num_buckets());
908913
source->Split(std::move(hash_fn), target); // increases the depth.
914+
915+
// add back the updated bucket count.
916+
bucket_count_ += (target->num_buckets() + source->num_buckets());
909917
++unique_segments_;
910918

911919
for (size_t i = start_idx + chunk_size / 2; i < start_idx + chunk_size; ++i) {

src/core/dash_internal.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,11 @@ class Segment {
434434

435435
template <bool UV = kUseVersion>
436436
std::enable_if_t<UV, uint64_t> GetVersion(PhysicalBid bid) const {
437-
return bucket_[bid].GetVersion();
437+
return GetBucket(bid).GetVersion();
438438
}
439439

440440
template <bool UV = kUseVersion> std::enable_if_t<UV> SetVersion(PhysicalBid bid, uint64_t v) {
441-
return bucket_[bid].SetVersion(v);
441+
return GetBucket(bid).SetVersion(v);
442442
}
443443

444444
// Traverses over Segment's bucket bid and calls cb(const Iterator& it) 0 or more times
@@ -1304,6 +1304,7 @@ void Segment<Key, Value, Policy>::Split(HFunc&& hfn, Segment* dest_right) {
13041304

13051305
template <typename Key, typename Value, typename Policy>
13061306
int Segment<Key, Value, Policy>::MoveToOther(bool own_items, unsigned from_bid, unsigned to_bid) {
1307+
assert(from_bid < kBucketNum && to_bid < kBucketNum);
13071308
auto& src = bucket_[from_bid];
13081309
uint32_t mask = src.GetProbe(!own_items);
13091310
if (mask == 0) {
@@ -1519,7 +1520,8 @@ void Segment<Key, Value, Policy>::TraverseBucket(PhysicalBid bid, Cb&& cb) {
15191520

15201521
template <typename Key, typename Value, typename Policy>
15211522
template <typename Cb, typename HashFn>
1522-
bool Segment<Key, Value, Policy>::TraverseLogicalBucket(uint8_t bid, HashFn&& hfun, Cb&& cb) const {
1523+
bool Segment<Key, Value, Policy>::TraverseLogicalBucket(LogicalBid bid, HashFn&& hfun,
1524+
Cb&& cb) const {
15231525
assert(bid < kBucketNum);
15241526

15251527
const Bucket& b = bucket_[bid];
@@ -1673,12 +1675,12 @@ auto Segment<Key, Value, Policy>::BumpUp(uint8_t bid, SlotId slot, Hash_t key_ha
16731675

16741676
// update ptr for swapped items
16751677
if (is_probing) {
1676-
unsigned prev_bid = PrevBid(swap_bid);
1678+
LogicalBid prev_bid = PrevBid(swap_bid);
16771679
auto& prevb = bucket_[prev_bid];
16781680
prevb.SetStashPtr(stash_pos, swap_fp, &swapb);
16791681
} else {
16801682
// stash_ptr resides in the current or the next bucket.
1681-
unsigned next_bid = NextBid(swap_bid);
1683+
LogicalBid next_bid = NextBid(swap_bid);
16821684
swapb.SetStashPtr(stash_pos, swap_fp, bucket_ + next_bid);
16831685
}
16841686

src/core/dash_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ TEST_F(DashTest, Eviction) {
700700

701701
ASSERT_THROW(loop(), bad_alloc);
702702
ASSERT_LT(num, 5000);
703+
ASSERT_EQ(2, dt_.unique_segments());
703704
EXPECT_LT(dt_.size(), ev.max_capacity);
704705
LOG(INFO) << "size is " << dt_.size();
705706

0 commit comments

Comments
 (0)