|
4 | 4 |
|
5 | 5 | #pragma once
|
6 | 6 |
|
| 7 | +#include <cassert> |
7 | 8 | #include <cstring>
|
8 | 9 | #include <memory>
|
9 | 10 | #include <string_view>
|
10 | 11 | #include <vector>
|
11 | 12 |
|
| 13 | +#include "base/hash.h" |
| 14 | + |
12 | 15 | namespace dfly {
|
13 | 16 |
|
14 |
| -class ISSEntry { |
| 17 | +class ISLEntry { |
| 18 | + friend class IntrusiveStringList; |
| 19 | + |
15 | 20 | public:
|
16 |
| - ISSEntry(std::string_view key) { |
17 |
| - ISSEntry* next = nullptr; |
| 21 | + ISLEntry() = default; |
| 22 | + |
| 23 | + ISLEntry(char* data) { |
| 24 | + data_ = data; |
| 25 | + } |
| 26 | + |
| 27 | + operator bool() const { |
| 28 | + return data_; |
| 29 | + } |
| 30 | + |
| 31 | + std::string_view Key() const { |
| 32 | + return {GetKeyData(), GetKeySize()}; |
| 33 | + } |
| 34 | + |
| 35 | + private: |
| 36 | + static ISLEntry Create(std::string_view key) { |
| 37 | + char* next = nullptr; |
18 | 38 | uint32_t key_size = key.size();
|
19 | 39 |
|
20 | 40 | auto size = sizeof(next) + sizeof(key_size) + key_size;
|
21 | 41 |
|
22 |
| - data_ = (char*)malloc(size); |
| 42 | + char* data = (char*)malloc(size); |
23 | 43 |
|
24 |
| - std::memcpy(data_, &next, sizeof(next)); |
| 44 | + std::memcpy(data, &next, sizeof(next)); |
25 | 45 |
|
26 |
| - auto* key_size_pos = data_ + sizeof(next); |
| 46 | + auto* key_size_pos = data + sizeof(next); |
27 | 47 | std::memcpy(key_size_pos, &key_size, sizeof(key_size));
|
28 | 48 |
|
29 | 49 | auto* key_pos = key_size_pos + sizeof(key_size);
|
30 | 50 | std::memcpy(key_pos, key.data(), key_size);
|
| 51 | + |
| 52 | + return ISLEntry(data); |
31 | 53 | }
|
32 | 54 |
|
33 |
| - std::string_view Key() const { |
34 |
| - return {GetKeyData(), GetKeySize()}; |
| 55 | + static void Destroy(ISLEntry entry) { |
| 56 | + free(entry.data_); |
35 | 57 | }
|
36 | 58 |
|
37 |
| - ISSEntry* Next() const { |
38 |
| - ISSEntry* next = nullptr; |
39 |
| - std::memcpy(&next, data_, sizeof(next)); |
| 59 | + ISLEntry Next() const { |
| 60 | + ISLEntry next; |
| 61 | + std::memcpy(&next.data_, data_, sizeof(next)); |
40 | 62 | return next;
|
41 | 63 | }
|
42 | 64 |
|
43 |
| - void SetNext(ISSEntry* next) { |
| 65 | + void SetNext(ISLEntry next) { |
44 | 66 | std::memcpy(data_, &next, sizeof(next));
|
45 | 67 | }
|
46 | 68 |
|
47 |
| - private: |
48 | 69 | const char* GetKeyData() const {
|
49 |
| - return data_ + sizeof(ISSEntry*) + sizeof(uint32_t); |
| 70 | + return data_ + sizeof(ISLEntry*) + sizeof(uint32_t); |
50 | 71 | }
|
51 | 72 |
|
52 | 73 | uint32_t GetKeySize() const {
|
53 | 74 | uint32_t size = 0;
|
54 |
| - std::memcpy(&size, data_ + sizeof(ISSEntry*), sizeof(size)); |
| 75 | + std::memcpy(&size, data_ + sizeof(ISLEntry*), sizeof(size)); |
55 | 76 | return size;
|
56 | 77 | }
|
57 | 78 |
|
58 | 79 | // TODO consider use SDS strings or other approach
|
59 | 80 | // TODO add optimization for big keys
|
60 |
| - // memory daya layout [ISSEntry*, key_size, key] |
61 |
| - char* data_; |
| 81 | + // memory daya layout [ISLEntry*, key_size, key] |
| 82 | + char* data_ = nullptr; |
62 | 83 | };
|
63 | 84 |
|
64 |
| -class ISMEntry { |
65 |
| - public: |
66 |
| - ISMEntry(std::string_view key, std::string_view val) { |
67 |
| - ISMEntry* next = nullptr; |
68 |
| - uint32_t key_size = key.size(); |
69 |
| - uint32_t val_size = val.size(); |
| 85 | +class FakePrevISLEntry : public ISLEntry { |
| 86 | + FakePrevISLEntry(ISLEntry) { |
| 87 | + fake_allocated_mem_ = ; |
| 88 | + } |
70 | 89 |
|
71 |
| - auto size = sizeof(next) + sizeof(key_size) + sizeof(val_size) + key_size + val_size; |
| 90 | + private: |
| 91 | + void* fake_allocated_mem_; |
| 92 | +} |
72 | 93 |
|
73 |
| - data_ = (char*)malloc(size); |
| 94 | +class IntrusiveStringList { |
| 95 | + public: |
| 96 | + ~IntrusiveStringList() { |
| 97 | + while (start_) { |
| 98 | + auto next = start_.Next(); |
| 99 | + ISLEntry::Destroy(start_); |
| 100 | + start_ = next; |
| 101 | + } |
| 102 | + } |
74 | 103 |
|
75 |
| - std::memcpy(data_, &next, sizeof(next)); |
| 104 | + ISLEntry Insert(ISLEntry e) { |
| 105 | + e.SetNext(start_); |
| 106 | + start_ = e; |
| 107 | + return start_; |
| 108 | + } |
76 | 109 |
|
77 |
| - auto* key_size_pos = data_ + sizeof(next); |
78 |
| - std::memcpy(key_size_pos, &key_size, sizeof(key_size)); |
| 110 | + ISLEntry Emplace(std::string_view key) { |
| 111 | + return Insert(ISLEntry::Create(key)); |
| 112 | + } |
79 | 113 |
|
80 |
| - auto* val_size_pos = key_size_pos + sizeof(key_size); |
81 |
| - std::memcpy(val_size_pos, &val_size, sizeof(val_size)); |
| 114 | + ISLEntry Find(std::string_view str) { |
| 115 | + auto it = start_; |
| 116 | + for (; it && it.Key() != str; it = it.Next()) |
| 117 | + ; |
| 118 | + return it; |
| 119 | + } |
82 | 120 |
|
83 |
| - auto* key_pos = val_size_pos + sizeof(val_size); |
84 |
| - std::memcpy(key_pos, key.data(), key_size); |
| 121 | + bool Erase(std::string_view str) { |
| 122 | + if (!start_) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + auto it = start_; |
| 126 | + if (it.Key() == str) { |
| 127 | + start_ = it.Next(); |
| 128 | + ISLEntry::Destroy(it); |
| 129 | + return true; |
| 130 | + } |
85 | 131 |
|
86 |
| - auto* val_pos = key_pos + key_size; |
87 |
| - std::memcpy(val_pos, val.data(), val_size); |
| 132 | + auto prev = it; |
| 133 | + for (it = it.Next(); it; prev = it, it = it.Next()) { |
| 134 | + if (it.Key() == str) { |
| 135 | + prev.SetNext(it.Next()); |
| 136 | + ISLEntry::Destroy(it); |
| 137 | + return true; |
| 138 | + } |
| 139 | + } |
| 140 | + return false; |
88 | 141 | }
|
89 | 142 |
|
90 |
| - std::string_view Key() const { |
91 |
| - return {GetKeyData(), GetKeySize()}; |
| 143 | + void MoveNext(ISLEntry& prev) { |
| 144 | + auto next = prev.Next(); |
| 145 | + prev.SetNext(next.Next()); |
| 146 | + Insert(next); |
92 | 147 | }
|
93 | 148 |
|
94 |
| - std::string_view Val() const { |
95 |
| - return {GetValData(), GetValSize()}; |
96 |
| - } |
| 149 | + private: |
| 150 | + ISLEntry start_; |
| 151 | +}; |
97 | 152 |
|
98 |
| - ISMEntry* Next() const { |
99 |
| - ISMEntry* next = nullptr; |
100 |
| - std::memcpy(&next, data_, sizeof(next)); |
101 |
| - return next; |
102 |
| - } |
| 153 | +class IntrusiveStringSet { |
| 154 | + public: |
| 155 | + // TODO add TTL processing |
| 156 | + ISLEntry Add(std::string_view str, uint32_t ttl_sec = UINT32_MAX) { |
| 157 | + if (size_ >= entries_.size()) { |
| 158 | + Grow(); |
| 159 | + } |
| 160 | + auto bucket_id = BucketId(Hash(str)); |
| 161 | + auto& bucket = entries_[bucket_id]; |
103 | 162 |
|
104 |
| - void SetVal(std::string_view val) { |
105 |
| - // TODO add optimization for the same size key |
106 |
| - uint32_t val_size = val.size(); |
107 |
| - auto new_size = |
108 |
| - sizeof(ISMEntry*) + sizeof(uint32_t) + sizeof(uint32_t) + GetKeySize() + val_size; |
| 163 | + if (auto existed_item = bucket.Find(str); existed_item) { |
| 164 | + // TODO consider common implementation for key value pair |
| 165 | + return ISLEntry(); |
| 166 | + } |
109 | 167 |
|
110 |
| - data_ = (char*)realloc(data_, new_size); |
| 168 | + ++size_; |
| 169 | + return bucket.Emplace(str); |
| 170 | + } |
111 | 171 |
|
112 |
| - auto* val_size_pos = data_ + sizeof(ISMEntry*) + sizeof(uint32_t); |
113 |
| - std::memcpy(val_size_pos, &val_size, sizeof(val_size)); |
| 172 | + bool Erase(std::string_view str) { |
| 173 | + auto bucket_id = BucketId(Hash(str)); |
| 174 | + return entries_[bucket_id].Erase(str); |
| 175 | + } |
114 | 176 |
|
115 |
| - auto* val_pos = val_size_pos + sizeof(val_size) + GetKeySize(); |
116 |
| - std::memcpy(val_pos, val.data(), val_size); |
| 177 | + ISLEntry Find(std::string_view member) { |
| 178 | + auto bucket_id = BucketId(Hash(member)); |
| 179 | + return entries_[bucket_id].Find(member); |
117 | 180 | }
|
118 | 181 |
|
119 |
| - void SetNext(ISMEntry* next) { |
120 |
| - std::memcpy(data_, &next, sizeof(next)); |
| 182 | + // Returns the number of elements in the map. Note that it might be that some of these elements |
| 183 | + // have expired and can't be accessed. |
| 184 | + size_t UpperBoundSize() const { |
| 185 | + return size_; |
121 | 186 | }
|
122 | 187 |
|
123 |
| - private: |
124 |
| - const char* GetKeyData() const { |
125 |
| - return data_ + sizeof(ISMEntry*) + sizeof(uint32_t) + sizeof(uint32_t); |
| 188 | + bool Empty() const { |
| 189 | + return size_ == 0; |
126 | 190 | }
|
127 | 191 |
|
128 |
| - uint32_t GetKeySize() const { |
129 |
| - uint32_t size = 0; |
130 |
| - std::memcpy(&size, data_ + sizeof(ISMEntry*), sizeof(size)); |
131 |
| - return size; |
| 192 | + private: |
| 193 | + std::uint32_t Capacity() const { |
| 194 | + return 1 << capacity_log_; |
132 | 195 | }
|
133 | 196 |
|
134 |
| - const char* GetValData() const { |
135 |
| - return GetKeyData() + GetKeySize(); |
| 197 | + void Grow() { |
| 198 | + ++capacity_log_; |
| 199 | + entries_.resize(Capacity()); |
| 200 | + |
| 201 | + // TODO rehashing |
136 | 202 | }
|
137 | 203 |
|
138 |
| - uint32_t GetValSize() const { |
139 |
| - uint32_t size = 0; |
140 |
| - std::memcpy(&size, data_ + sizeof(ISMEntry*) + sizeof(uint32_t), sizeof(size)); |
141 |
| - return size; |
| 204 | + uint32_t BucketId(uint64_t hash) const { |
| 205 | + assert(capacity_log_ > 0); |
| 206 | + return hash >> (64 - capacity_log_); |
142 | 207 | }
|
143 | 208 |
|
144 |
| - // TODO consider use SDS strings or other approach |
145 |
| - // TODO add optimization for big keys |
146 |
| - // memory daya layout [ISMEntry*, key_size, val_size, key, val] |
147 |
| - char* data_; |
148 |
| -}; |
| 209 | + uint64_t Hash(std::string_view str) const { |
| 210 | + constexpr XXH64_hash_t kHashSeed = 24061983; |
| 211 | + return XXH3_64bits_withSeed(str.data(), str.size(), kHashSeed); |
| 212 | + } |
149 | 213 |
|
150 |
| -template <class EntryT> class IntrusiveStringSet { |
151 |
| - public: |
152 | 214 | private:
|
153 |
| - std::vector<EntryT*> entries_; |
| 215 | + static constexpr size_t kMinSizeShift = 2; |
| 216 | + std::uint32_t capacity_log_ = 1; |
| 217 | + std::uint32_t size_ = 0; // number of elements in the set. |
| 218 | + |
| 219 | + static_assert(sizeof(IntrusiveStringList) == sizeof(void*), |
| 220 | + "IntrusiveStringList should be just a pointer"); |
| 221 | + std::vector<IntrusiveStringList> entries_; |
154 | 222 | };
|
155 | 223 |
|
156 | 224 | } // namespace dfly
|
0 commit comments