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 | 384 | config.json_config_.at("capacity").get<uint64_t>(); | |
| 15 | const size_t bytes = | ||
| 16 | 384 | base::PetHash<Key_t, Value_t, false>::MemorySize(capacity); | |
| 17 | 384 | void* mem = nullptr; | |
| 18 | 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 | 384 | impl_->Initialize(capacity); | |
| 23 | 384 | } | |
| 24 | |||
| 25 | 768 | ~DramPetHashIndex() override { | |
| 26 | 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 | 82484 | auto [value, exists] = impl_->Get(key); | |
| 36 | 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 | 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 | 23998 | for (int i = 0; i < keys.Size(); ++i) { | |
| 51 | 23364 | if (i + 1 < keys.Size()) { | |
| 52 | 22730 | impl_->HintPrefetch(keys[i + 1]); | |
| 53 | } | ||
| 54 | 23364 | auto [value, exists] = impl_->Get(keys[i]); | |
| 55 | 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 | 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 |