-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
314 lines (284 loc) · 7.9 KB
/
HashTable.cpp
File metadata and controls
314 lines (284 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
// HashTable.cpp
// devl
//
// Created by 吴海源 on 2018/12/20.
// Copyright © 2018 why. All rights reserved.
//
#include "HashTable.h"
namespace hybridKV {
//void Dict::debugPrint() {
// for (int i=0; i<size_; ++i) {
// if (table[i] == nullptr)
// std::cout << "index " << i << ": " << (long int)table[i] << " has no element!" <<std::endl;
// else {
// auto itor = table[i];
// while (itor != nullptr) {
// const char *tt = (reinterpret_cast<kvObj*>(itor->val))->data();
// if (tt == nullptr)
// std::cout << "wrong when add, no data" <<std::endl;
// std::cout << "index "<< i << " has element(s): " << tt << " ";
// itor = itor->next;
// }
// std::cout << std::endl;
// }
// }
//}
int Dict::Set(kvObj *key, kvObj *value, dictEntry **entryPtr) {
int ret = 0;
// if ((*entryPtr = Add(key, value, &ret)) == nullptr) {
// if (ret == -1)
// ++dupKeyCnt;
// return ret;
// }
if (LookUp(key) == nullptr) {
if ((*entryPtr = Add(key, value, &ret)) == nullptr) {
LOG("ERROR WHEN ADD KEY\r\n");
return ret;
}
} else {
// ++dupKeyCnt;
if ((*entryPtr = OverWrite(key, value)) == nullptr) {
LOG("ERROR WHEN OVER WRITE\r\n");
return -1;
} else {
return 1;
}
}
return 0;
}
int Dict::Get(const kvObj* key, std::string* value) {
kvObj* vo = nullptr;
if ((vo = LookUp(key)) == nullptr) {
// value->assign(100, 'a');
// LOG("No such key");
return -1;
}
// kvObj *val = static_cast<kvObj *>(vo->data());
value->assign(vo->data(), vo->size());
return 0;
}
//return kvobj val if key exists, otherwise return null
Dict::dictEntry* Dict::getEntry(const kvObj* key) {
// LOG("Function: getEntry()");
Dict::dictEntry* entry;
uint32_t hash_key = hasher(key->data(), key->size());
int idx = hash_key & szMask();
entry = table[idx];
#ifdef HiKV_TEST
#ifdef PM_READ_LATENCY_TEST
pload((uint64_t*)(&table[idx]), sizeof(void*));
#endif
#endif
int conflict = 0;
while (entry != nullptr) {
if (equal(*key, *(kvObj*)(entry->key)))
return entry;
#ifdef HiKV_TEST
#ifdef PM_READ_LATENCY_TEST
pload((uint64_t*)(&entry->next), sizeof(void*));
#endif
#endif
++conflict;
conflictCntRd += conflict;
if (maxConflictRd < conflict)
maxConflictRd = conflict;
entry = entry->next;
}
return nullptr;
}
kvObj* Dict::LookUp(const kvObj* key) {
// LOG("Function: LookUP()");
Dict::dictEntry *entry = getEntry(key);
if (entry != nullptr) {
return (kvObj*)(entry->val);
}
return nullptr;
}
int Dict::getIndex(const kvObj* key) {
// LOG("Function: getIndex()");
uint32_t hash_key = hasher(key->data(), key->size());
uint32_t idx = hash_key & szMask();
dictEntry* entry = table[idx];
#ifdef HiKV_TEST
#ifdef PM_READ_LATENCY_TEST
pload((uint64_t*)(&table[idx]), sizeof(void*));
#endif
#endif
int conflict = 0;
while (entry != nullptr) {
++conflict;
if (equal(*key, *(kvObj*)(entry->key)))
return -1;
#ifdef HiKV_TEST
#ifdef PM_READ_LATENCY_TEST
pload((uint64_t*)(&entry->next), sizeof(void*));
#endif
#endif
entry = entry->next;
}
conflictCnt += conflict;
if (maxConflict < conflict)
maxConflict = conflict;
return idx;
}
Dict::dictEntry* Dict::Add(kvObj* key, kvObj* value, int* ret) {
// LOG("Function: DictAdd()");
if ((used() + 1) == size()) {
*ret = -2;
return nullptr;
}
uint32_t idx;
if ((idx = getIndex(key)) == -1) {
*ret = -1;
return nullptr;
}
// std::cout << "idx = " << idx <<std::endl;
Dict::dictEntry* newEntry = new Dict::dictEntry();
// std:cout << ""
newEntry->key = key;
newEntry->val = value;
newEntry->next = table[idx];
table[idx] = newEntry;
#ifdef HiKV_TEST
#ifdef PM_WRITE_LATENCY_TEST
pflush((uint64_t*)(newEntry), sizeof(dictEntry));
pflush((uint64_t*)(&table[idx]), sizeof(void*));
#endif
#endif
incrNode();
return newEntry;
}
Dict::dictEntry* Dict::OverWrite(const kvObj* key, kvObj* value) {
// LOG("Function: OverWrite() ");
dictEntry* entry = nullptr;
// get the entry contains the key value
entry = getEntry(key);
// key not exists
if (entry == nullptr)
return nullptr;
// mark the val
kvObj *tmp = (kvObj*)(entry->val);
// set new value
entry->val = value;
#ifdef HiKV_TEST
#ifdef PM_WRITE_LATENCY_TEST
pflush((uint64_t*)(&entry->val), sizeof(void*));
#endif
#endif
// free old value
if (tmp != nullptr)
delete tmp;
// tmp->~kvObj();
// if (tmp)
// delete tmp;
return entry;
}
int Dict::Delete(const kvObj* key, int& bt) {
// LOG("Function: Delete() ");
dictEntry *tarEntry, *preEntry = nullptr;
uint32_t hash_key = hasher(key->data(), key->size());
uint32_t idx = hash_key & szMask();
tarEntry = table[idx];
#ifdef HiKV_TEST
#ifdef PM_READ_LATENCY_TEST
pload((uint64_t*)(&table[idx]), sizeof(void *));
#endif
#endif
// preNode = nullptr;
while (tarEntry) {
if (*key == *(kvObj*)(tarEntry->key)) {
if (preEntry != nullptr) {
preEntry->next = tarEntry->next;
} else {
#ifdef HiKV_TEST
#ifdef PM_READ_LATENCY_TEST
pload((uint64_t*)(&tarEntry->next), sizeof(void *));
#endif
#endif
table[idx] = tarEntry->next;
#ifdef HiKV_TEST
#ifdef PM_WRITE_LATENCY_TEST
pflush((uint64_t*)(&table[idx]), sizeof(void*));
#endif
#endif
}
bt = tarEntry->idx;
delete tarEntry;
decrNode();
return 0;
}
preEntry = tarEntry;
tarEntry = tarEntry->next;
#ifdef HiKV_TEST
#ifdef PM_READ_LATENCY_TEST
pload((uint64_t*)(&table[idx]), sizeof(void *));
#endif
#endif
}
LOG(" No key to delete");
return -1;
}
int HashTable::Get(const kvObj* key, std::string* value) {
// LOG("HashTable::Get()");
// kvObj* k = new kvObj(key, false);
int res = dict_->Get(key, value);
// LOG("Value: " << *value);
// LOG("");
return res;
}
int HashTable::Delete(const kvObj* key, int& bt) {
// LOG("HashTable::Delete()");
// kvObj* k = new kvObj(key, false);
int res = dict_->Delete(key, bt);
// LOG("");
return res;
}
int HashTable::Get(const std::string& key, std::string* value) {
// LOG("HashTable::Get()");
kvObj* k = new kvObj(key, false);
int res = dict_->Get(k, value);
// LOG("Value: " << *value);
// LOG("");
return res;
}
int HashTable::Set(kvObj* key, kvObj* value, Dict::dictEntry** entryPointer) {
// LOG("HashTable::Set()");
int ret = dict_->Set(key, value, entryPointer);
if (ret == -2) {
Rehash();
ret = dict_->Set(key, value, entryPointer);
if (ret == -2)
LOG("No more key");
}
// LOG("");
return ret;
}
int HashTable::Delete(const std::string& key) {
// LOG("HashTable::Delete()");
kvObj* k = new kvObj(key, false);
int x;
int res = dict_->Delete(k, x);
// LOG("");
return res;
}
void HashTable::Rehash() {
LOG("Rehashing...");
uint32_t newSize = std::min(dict_->size() << 1 , limits_);
Dict* newDict = new Dict(newSize, hasher);
Dict::Iterator iter(dict_);
iter.SeekToFirst();
Dict::dictEntry* ret = 0;
while (iter.Valid()) {
if (newDict->Set((kvObj*)(iter.Key()->key), (kvObj*)(iter.Key()->val), &ret) != -1) {
dict_->decrNode();
}
uint32_t index = iter.index();
iter.Next();
dict_->remove(index);
}
assert(dict_->used() == 0);
delete dict_;
dict_ = newDict;
}
}