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 | 348 | explicit DramPetHashIndex(const BaseKVConfig& config) : Index(config) { | |
| 13 | const uint64_t capacity = | ||
| 14 |
2/4✓ Branch 1 taken 348 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 348 times.
✗ Branch 5 not taken.
|
348 | config.json_config_.at("capacity").get<uint64_t>(); |
| 15 | const size_t bytes = | ||
| 16 |
1/2✓ Branch 1 taken 348 times.
✗ Branch 2 not taken.
|
348 | base::PetHash<Key_t, Value_t, false>::MemorySize(capacity); |
| 17 | 348 | void* mem = nullptr; | |
| 18 |
3/6✓ Branch 0 taken 348 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 348 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 348 times.
|
348 | if (posix_memalign(&mem, 64, bytes) != 0 || mem == nullptr) { |
| 19 | ✗ | throw std::bad_alloc(); | |
| 20 | } | ||
| 21 | 348 | impl_ = new (mem) base::PetHash<Key_t, Value_t, false>(); | |
| 22 |
1/2✓ Branch 1 taken 348 times.
✗ Branch 2 not taken.
|
348 | impl_->Initialize(capacity); |
| 23 | 348 | } | |
| 24 | |||
| 25 | 696 | ~DramPetHashIndex() override { | |
| 26 |
1/2✓ Branch 0 taken 348 times.
✗ Branch 1 not taken.
|
348 | if (impl_ != nullptr) { |
| 27 | 348 | impl_->~PetHash<Key_t, Value_t, false>(); | |
| 28 | 348 | std::free(impl_); | |
| 29 | 348 | impl_ = nullptr; | |
| 30 | } | ||
| 31 | 696 | } | |
| 32 | |||
| 33 | 80362 | void Get(Key_t key, Value_t& pointer, unsigned tid) override { | |
| 34 | (void)tid; | ||
| 35 |
1/2✓ Branch 1 taken 80362 times.
✗ Branch 2 not taken.
|
80362 | auto [value, exists] = impl_->Get(key); |
| 36 |
2/2✓ Branch 0 taken 77874 times.
✓ Branch 1 taken 2488 times.
|
80362 | pointer = exists ? value : NONE; |
| 37 | 80362 | } | |
| 38 | |||
| 39 | 138108 | Value_t Put(Key_t key, Value_t pointer, unsigned tid) override { | |
| 40 | (void)tid; | ||
| 41 | 138108 | Value_t old_handle = kValueHandleNone; | |
| 42 |
1/2✓ Branch 2 taken 138108 times.
✗ Branch 3 not taken.
|
138108 | impl_->Set(key, pointer, nullptr, false, &old_handle); |
| 43 | 138108 | return old_handle; | |
| 44 | } | ||
| 45 | |||
| 46 | 624 | void BatchGet(base::ConstArray<Key_t> keys, | |
| 47 | Value_t* pointers, | ||
| 48 | unsigned tid) override { | ||
| 49 | (void)tid; | ||
| 50 |
2/2✓ Branch 1 taken 23340 times.
✓ Branch 2 taken 624 times.
|
23964 | for (int i = 0; i < keys.Size(); ++i) { |
| 51 |
2/2✓ Branch 1 taken 22716 times.
✓ Branch 2 taken 624 times.
|
23340 | if (i + 1 < keys.Size()) { |
| 52 |
1/2✓ Branch 2 taken 22716 times.
✗ Branch 3 not taken.
|
22716 | impl_->HintPrefetch(keys[i + 1]); |
| 53 | } | ||
| 54 |
1/2✓ Branch 2 taken 23340 times.
✗ Branch 3 not taken.
|
23340 | auto [value, exists] = impl_->Get(keys[i]); |
| 55 |
2/2✓ Branch 0 taken 23214 times.
✓ Branch 1 taken 126 times.
|
23340 | pointers[i] = exists ? value : NONE; |
| 56 | } | ||
| 57 | 624 | } | |
| 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 | ✗ | 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 |