storage/kv_engine/base_kv.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | #include <boost/coroutine2/all.hpp> | ||
| 3 | #include <cstdint> | ||
| 4 | #include <string> | ||
| 5 | #include <tuple> | ||
| 6 | |||
| 7 | #include "base/array.h" | ||
| 8 | #include "base/json.h" | ||
| 9 | #include "base/log.h" | ||
| 10 | |||
| 11 | using boost::coroutines2::coroutine; | ||
| 12 | |||
| 13 | // #define XMH_SIMPLE_MALLOC | ||
| 14 | |||
| 15 | struct BaseKVConfig { | ||
| 16 | int num_threads_ = 0; | ||
| 17 | json json_config_; // add your custom config in this field | ||
| 18 | }; | ||
| 19 | /* | ||
| 20 | KVEngineComposite uses the nested configuration format: | ||
| 21 | { | ||
| 22 | "engine_type": "KVEngineComposite", | ||
| 23 | "capacity": 1000000, | ||
| 24 | "index": {"type": "DRAM_EXTENDIBLE_HASH"}, | ||
| 25 | "value": { | ||
| 26 | "type": "DRAM_VALUE_STORE", | ||
| 27 | "path": "/tmp/recstore/value", | ||
| 28 | "default_value_size_hint": 128, | ||
| 29 | "dram_allocator": { | ||
| 30 | "type": "PERSIST_LOOP_SLAB", | ||
| 31 | "capacity_bytes": 128000000 | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | ResolveEngine returns the matching factory name; engine_type defaults to | ||
| 37 | KVEngineComposite when omitted. | ||
| 38 | Field validation happens in each engine constructor. KVEngineComposite rejects | ||
| 39 | legacy top-level fields such as path/index_type/value_type and nested file_path. | ||
| 40 | */ | ||
| 41 | |||
| 42 | class BaseKV { | ||
| 43 | public: | ||
| 44 | 1054 | virtual ~BaseKV() {} | |
| 45 | |||
| 46 | 1054 | explicit BaseKV(const BaseKVConfig& config){}; | |
| 47 | |||
| 48 | ✗ | virtual void Util() { | |
| 49 | ✗ | std::cout << "BaseKV Util: no impl" << std::endl; | |
| 50 | ✗ | return; | |
| 51 | } | ||
| 52 | ✗ | virtual std::string ExtraResultFields() const { return ""; } | |
| 53 | virtual void Get(const uint64_t key, std::string& value, unsigned tid) = 0; | ||
| 54 | ✗ | virtual bool Exists(const uint64_t key, unsigned tid) { | |
| 55 | ✗ | std::string value; | |
| 56 | ✗ | Get(key, value, tid); | |
| 57 | ✗ | return !value.empty(); | |
| 58 | ✗ | } | |
| 59 | |||
| 60 | virtual void | ||
| 61 | Put(const uint64_t key, const std::string_view& value, unsigned tid) = 0; | ||
| 62 | |||
| 63 | ✗ | virtual void BatchPut(base::ConstArray<uint64_t> keys, | |
| 64 | std::vector<base::ConstArray<float>>* values, | ||
| 65 | unsigned tid) { | ||
| 66 | ✗ | LOG(FATAL) << "not implemented"; | |
| 67 | } | ||
| 68 | |||
| 69 | ✗ | virtual void BatchPut(coroutine<void>::push_type& sink, | |
| 70 | base::ConstArray<uint64_t> keys, | ||
| 71 | std::vector<base::ConstArray<float>>* values, | ||
| 72 | unsigned tid) { | ||
| 73 | ✗ | LOG(FATAL) << "not implemented"; | |
| 74 | }; | ||
| 75 | |||
| 76 | virtual void BatchGet(base::ConstArray<uint64_t> keys, | ||
| 77 | std::vector<base::ConstArray<float>>* values, | ||
| 78 | unsigned tid) = 0; | ||
| 79 | |||
| 80 | struct BatchGetFlatStats { | ||
| 81 | std::uint64_t index_lookup_ns = 0; | ||
| 82 | std::uint64_t zero_fill_ns = 0; | ||
| 83 | std::uint64_t row_copy_ns = 0; | ||
| 84 | std::uint64_t missing_rows = 0; | ||
| 85 | }; | ||
| 86 | |||
| 87 | struct DirectFixedRow { | ||
| 88 | const char* data = nullptr; | ||
| 89 | size_t size = 0; | ||
| 90 | bool missing = false; | ||
| 91 | }; | ||
| 92 | |||
| 93 | struct RDMABackingRegion { | ||
| 94 | char* data = nullptr; | ||
| 95 | size_t size = 0; | ||
| 96 | }; | ||
| 97 | |||
| 98 | 2 | virtual bool BatchGetFlat( | |
| 99 | base::ConstArray<uint64_t> keys, | ||
| 100 | float* values, | ||
| 101 | int64_t num_rows, | ||
| 102 | int64_t embedding_dim, | ||
| 103 | unsigned tid, | ||
| 104 | BatchGetFlatStats* stats = nullptr) { | ||
| 105 | 2 | return false; | |
| 106 | } | ||
| 107 | |||
| 108 | ✗ | virtual bool BatchGetIndexOnly(base::ConstArray<uint64_t> keys, | |
| 109 | unsigned tid, | ||
| 110 | BatchGetFlatStats* stats = nullptr) { | ||
| 111 | ✗ | return false; | |
| 112 | } | ||
| 113 | |||
| 114 | ✗ | virtual bool BatchGetDirectFixedRows( | |
| 115 | base::ConstArray<uint64_t> keys, | ||
| 116 | int64_t num_rows, | ||
| 117 | int64_t embedding_dim, | ||
| 118 | unsigned tid, | ||
| 119 | std::vector<DirectFixedRow>* rows, | ||
| 120 | BatchGetFlatStats* stats = nullptr) { | ||
| 121 | ✗ | return false; | |
| 122 | } | ||
| 123 | |||
| 124 | ✗ | virtual RDMABackingRegion GetRDMABackingRegion() const { return {}; } | |
| 125 | |||
| 126 | ✗ | virtual void BatchGet(coroutine<void>::push_type& sink, | |
| 127 | base::ConstArray<uint64_t> keys, | ||
| 128 | std::vector<base::ConstArray<float>>* values, | ||
| 129 | unsigned tid) { | ||
| 130 | ✗ | LOG(FATAL) << "not implemented"; | |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | virtual bool ApplySgdUpdateFlat( | |
| 134 | base::ConstArray<uint64_t> keys, | ||
| 135 | const float* grads, | ||
| 136 | int64_t num_rows, | ||
| 137 | int64_t embedding_dim, | ||
| 138 | float learning_rate, | ||
| 139 | uint8_t tag, | ||
| 140 | unsigned tid) { | ||
| 141 | ✗ | return false; | |
| 142 | } | ||
| 143 | |||
| 144 | ✗ | virtual void DebugInfo() const {} | |
| 145 | |||
| 146 | ✗ | virtual void BulkLoad(base::ConstArray<uint64_t> keys, const void* value) { | |
| 147 | ✗ | LOG(FATAL) << "not implemented"; | |
| 148 | }; | ||
| 149 | |||
| 150 | ✗ | virtual void LoadFakeData(int64_t key_capacity, int value_size) { | |
| 151 | ✗ | std::vector<uint64_t> keys; | |
| 152 | ✗ | float* values = new float[value_size / sizeof(float) * key_capacity]; | |
| 153 | ✗ | keys.reserve(key_capacity); | |
| 154 | ✗ | for (int64_t i = 0; i < key_capacity; i++) { | |
| 155 | ✗ | keys.push_back(i); | |
| 156 | } | ||
| 157 | ✗ | this->BulkLoad(base::ConstArray<uint64_t>(keys), values); | |
| 158 | ✗ | delete[] values; | |
| 159 | ✗ | }; | |
| 160 | |||
| 161 | ✗ | virtual void clear() { | |
| 162 | ✗ | LOG(WARNING) << "clear() not fully implemented for this KV engine"; | |
| 163 | ✗ | }; | |
| 164 | |||
| 165 | protected: | ||
| 166 | }; | ||
| 167 |