src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
34 / 36
94.4%
Branches:
22 / 34
64.7%
Functions:
7 / 8
87.5%

storage/index/dram/pet_hash_index.h
Line Branch Exec Source
1 #pragma once
2
3 #include <memory>
4 #include <new>
5
6 #include "base/factory.h"
7 #include "storage/index/index.h"
8 #include "storage/nvm/pet_kv/pet_hash.h"
9
10 class DramPetHashIndex : public Index {
11 public:
12 384 explicit DramPetHashIndex(const BaseKVConfig& config) : Index(config) {
13 const uint64_t capacity =
14
2/4
✓ Branch 1 taken 384 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 384 times.
✗ Branch 5 not taken.
384 config.json_config_.at("capacity").get<uint64_t>();
15 const size_t bytes =
16
1/2
✓ Branch 1 taken 384 times.
✗ Branch 2 not taken.
384 base::PetHash<Key_t, Value_t, false>::MemorySize(capacity);
17 384 void* mem = nullptr;
18
3/6
✓ Branch 0 taken 384 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 384 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 384 times.
384 if (posix_memalign(&mem, 64, bytes) != 0 || mem == nullptr) {
19 ✗ throw std::bad_alloc();
20 }
21 384 impl_ = new (mem) base::PetHash<Key_t, Value_t, false>();
22
1/2
✓ Branch 1 taken 384 times.
✗ Branch 2 not taken.
384 impl_->Initialize(capacity);
23 384 }
24
25 768 ~DramPetHashIndex() override {
26
1/2
✓ Branch 0 taken 384 times.
✗ Branch 1 not taken.
384 if (impl_ != nullptr) {
27 384 impl_->~PetHash<Key_t, Value_t, false>();
28 384 std::free(impl_);
29 384 impl_ = nullptr;
30 }
31 768 }
32
33 82484 void Get(Key_t key, Value_t& pointer, unsigned tid) override {
34 (void)tid;
35
1/2
✓ Branch 1 taken 82484 times.
✗ Branch 2 not taken.
82484 auto [value, exists] = impl_->Get(key);
36
2/2
✓ Branch 0 taken 79736 times.
✓ Branch 1 taken 2748 times.
82484 pointer = exists ? value : NONE;
37 82484 }
38
39 190330 Value_t Put(Key_t key, Value_t pointer, unsigned tid) override {
40 (void)tid;
41 190330 Value_t old_handle = kValueHandleNone;
42
1/2
✓ Branch 2 taken 190330 times.
✗ Branch 3 not taken.
190330 impl_->Set(key, pointer, nullptr, false, &old_handle);
43 190330 return old_handle;
44 }
45
46 634 void BatchGet(base::ConstArray<Key_t> keys,
47 Value_t* pointers,
48 unsigned tid) override {
49 (void)tid;
50
2/2
✓ Branch 1 taken 23364 times.
✓ Branch 2 taken 634 times.
23998 for (int i = 0; i < keys.Size(); ++i) {
51
2/2
✓ Branch 1 taken 22730 times.
✓ Branch 2 taken 634 times.
23364 if (i + 1 < keys.Size()) {
52
1/2
✓ Branch 2 taken 22730 times.
✗ Branch 3 not taken.
22730 impl_->HintPrefetch(keys[i + 1]);
53 }
54
1/2
✓ Branch 2 taken 23364 times.
✗ Branch 3 not taken.
23364 auto [value, exists] = impl_->Get(keys[i]);
55
2/2
✓ Branch 0 taken 23228 times.
✓ Branch 1 taken 136 times.
23364 pointers[i] = exists ? value : NONE;
56 }
57 634 }
58
59 18 void BatchPut(base::ConstArray<Key_t> keys,
60 Value_t* pointers,
61 unsigned tid) override {
62
2/2
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 18 times.
162 for (int i = 0; i < keys.Size(); ++i) {
63 144 Put(keys[i], pointers[i], tid);
64 }
65 18 }
66
67 4 bool Delete(Key_t& key) override { return impl_->Delete(key); }
68 ✗ size_t Capacity() override { return impl_->Capacity(); }
69
70 private:
71 base::PetHash<Key_t, Value_t, false>* impl_ = nullptr;
72 };
73
74 FACTORY_REGISTER(Index, DRAM_PET_HASH, DramPetHashIndex, const BaseKVConfig&);
75