-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_level.h
469 lines (419 loc) · 16.9 KB
/
basic_level.h
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include"generate_idx.h"
level_hash *level_init(uint64_t level_size,char* name)
//初始化一个level_hash
{
level_hash *level =(level_hash *)alignedmalloc(sizeof(level_hash));
level->name = (char *)malloc(sizeof(name));
if (!level)
{
printf("The level hash table initialization fails:1\n");
exit(1);
}
strcpy(level->name,name);
level->level_size = level_size;
level->addr_capacity = pow(2, level_size);
level->total_capacity = pow(2, level_size) + pow(2, level_size - 1);
generate_seeds(level);
level->buckets[0] = (level_bucket *)alignedmalloc(pow(2, level_size)*sizeof(level_bucket));
level->buckets[1] = (level_bucket *)alignedmalloc(pow(2, level_size - 1)*sizeof(level_bucket));
level->level_item_num[0] = 0;
level->level_item_num[1] = 0;
level->level_expand_time = 0;
level->resize_state = 0;
if (!level->buckets[0] || !level->buckets[1])
{
printf("The level hash table initialization fails:2\n");
exit(1);
}
printf("Level hashing: ASSOC_NUM %d, KEY_LEN %d, VALUE_LEN %d \n", ASSOC_NUM, KEY_LEN, VALUE_LEN);
printf("The number of top-level buckets: %ld\n", level->addr_capacity);
printf("The number of all buckets: %ld\n", level->total_capacity);
printf("The number of all entries: %ld\n", level->total_capacity*ASSOC_NUM);
printf("The level hash table initialization succeeds!\n");
return level;
}
void level_expand(level_hash *level)
//扩展level_hash
{
if (!level)
{
printf("The expanding fails: 1\n");
exit(1);
}
level->resize_state = 1;
level->addr_capacity = pow(2, level->level_size + 1);
level_bucket *newBuckets = (level_bucket *)alignedmalloc(level->addr_capacity*sizeof(level_bucket));
if (!newBuckets) {
printf("The expanding fails: 2\n");
exit(1);
}
uint64_t new_level_item_num = 0;
uint64_t old_idx;
for (old_idx = 0; old_idx < pow(2, level->level_size - 1); old_idx ++) {
uint64_t i, j;
for(i = 0; i < ASSOC_NUM; i ++){
if (level->buckets[1][old_idx].token[i] == 1)
{
uint8_t *key = level->buckets[1][old_idx].slot[i].key;
uint8_t *value = level->buckets[1][old_idx].slot[i].value;
uint64_t f_idx = F_IDX(F_HASH(level, key), level->addr_capacity);
uint64_t s_idx = S_IDX(S_HASH(level, key), level->addr_capacity);
uint8_t insertSuccess = 0;
for(j = 0; j < ASSOC_NUM; j ++){
/* The rehashed item is inserted into the less-loaded bucket between
the two hash locations in the new level
*/
if (newBuckets[f_idx].token[j] == 0)
{
memcpy(newBuckets[f_idx].slot[j].key, key, KEY_LEN);
memcpy(newBuckets[f_idx].slot[j].value, value, VALUE_LEN);
newBuckets[f_idx].token[j] = 1;
insertSuccess = 1;
new_level_item_num ++;
break;
}
if (newBuckets[s_idx].token[j] == 0)
{
memcpy(newBuckets[s_idx].slot[j].key, key, KEY_LEN);
memcpy(newBuckets[s_idx].slot[j].value, value, VALUE_LEN);
newBuckets[s_idx].token[j] = 1;
insertSuccess = 1;
new_level_item_num ++;
break;
}
}
if(!insertSuccess){
printf("The expanding fails: 3\n");
exit(1);
}
level->buckets[1][old_idx].token[i] = 0;
}
}
}
level->level_size ++;
level->total_capacity = pow(2, level->level_size) + pow(2, level->level_size - 1);
free(level->buckets[1]);
level->buckets[1] = level->buckets[0];
level->buckets[0] = newBuckets;
newBuckets = NULL;
level->level_item_num[1] = level->level_item_num[0];
level->level_item_num[0] = new_level_item_num;
level->level_expand_time ++;
level->resize_state = 0;
}
void level_shrink(level_hash *level)
//缩小level_hash
{
if (!level)
{
printf("The shrinking fails: 1\n");
exit(1);
}
// The shrinking is performed only when the hash table has very few items.
if(level->level_item_num[0] + level->level_item_num[1] > level->total_capacity*ASSOC_NUM*0.4){
printf("The shrinking fails: 2\n");
exit(1);
}
level->resize_state = 2;
level->level_size --;
level_bucket *newBuckets = (level_bucket *)alignedmalloc(pow(2, level->level_size - 1)*sizeof(level_bucket));
level_bucket *interimBuckets = level->buckets[0];
level->buckets[0] = level->buckets[1];
level->buckets[1] = newBuckets;
newBuckets = NULL;
level->level_item_num[0] = level->level_item_num[1];
level->level_item_num[1] = 0;
level->addr_capacity = pow(2, level->level_size);
level->total_capacity = pow(2, level->level_size) + pow(2, level->level_size - 1);
uint64_t old_idx, i;
for (old_idx = 0; old_idx < pow(2, level->level_size+1); old_idx ++) {
for(i = 0; i < ASSOC_NUM; i ++){
if (interimBuckets[old_idx].token[i] == 1)
{
if(level_insert(level, interimBuckets[old_idx].slot[i].key, interimBuckets[old_idx].slot[i].value)){
printf("The shrinking fails: 3\n");
exit(1);
}
interimBuckets[old_idx].token[i] = 0;
}
}
}
free(interimBuckets);
level->level_expand_time = 0;
level->resize_state = 0;
}
uint8_t* level_dynamic_query(level_hash *level, uint8_t *key)
{
uint64_t f_hash = F_HASH(level, key);
uint64_t s_hash = S_HASH(level, key);
uint64_t i, j, f_idx, s_idx;
if(level->level_item_num[0] > level->level_item_num[1]){
f_idx = F_IDX(f_hash, level->addr_capacity);
s_idx = S_IDX(s_hash, level->addr_capacity);
for(i = 0; i < 2; i ++){
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i][f_idx].token[j] == 1&&strcmp((const char *)level->buckets[i][f_idx].slot[j].key,(const char *)key) == 0)
{
return level->buckets[i][f_idx].slot[j].value;
}
}
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i][s_idx].token[j] == 1&&strcmp((const char *)level->buckets[i][s_idx].slot[j].key, (const char *)key) == 0)
{
return level->buckets[i][s_idx].slot[j].value;
}
}
f_idx = F_IDX(f_hash, level->addr_capacity / 2);
s_idx = S_IDX(s_hash, level->addr_capacity / 2);
}
}
else{
f_idx = F_IDX(f_hash, level->addr_capacity/2);
s_idx = S_IDX(s_hash, level->addr_capacity/2);
for(i = 2; i > 0; i --){
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i-1][f_idx].token[j] == 1&&strcmp((const char *)level->buckets[i-1][f_idx].slot[j].key, (const char *)key) == 0)
{
return level->buckets[i-1][f_idx].slot[j].value;
}
}
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i-1][s_idx].token[j] == 1&&strcmp((const char *)level->buckets[i-1][s_idx].slot[j].key, (const char *)key) == 0)
{
return level->buckets[i-1][s_idx].slot[j].value;
}
}
f_idx = F_IDX(f_hash, level->addr_capacity);
s_idx = S_IDX(s_hash, level->addr_capacity);
}
}
return NULL;
}
uint8_t* level_static_query(level_hash *level, uint8_t *key)
{
uint64_t f_hash = F_HASH(level, key);
uint64_t s_hash = S_HASH(level, key);
uint64_t f_idx = F_IDX(f_hash, level->addr_capacity);
uint64_t s_idx = S_IDX(s_hash, level->addr_capacity);
uint64_t i, j;
for(i = 0; i < 2; i ++){
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i][f_idx].token[j] == 1&&strcmp((const char *)level->buckets[i][f_idx].slot[j].key,(const char *)key) == 0)
{
return level->buckets[i][f_idx].slot[j].value;
}
}
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i][s_idx].token[j] == 1&&strcmp((const char *)level->buckets[i][s_idx].slot[j].key, (const char *)key) == 0)
{
return level->buckets[i][s_idx].slot[j].value;
}
}
f_idx = F_IDX(f_hash, level->addr_capacity / 2);
s_idx = S_IDX(s_hash, level->addr_capacity / 2);
}
return NULL;
}
uint8_t level_delete(level_hash *level, uint8_t *key)
//删除指定键值
{
uint64_t f_hash = F_HASH(level, key);
uint64_t s_hash = S_HASH(level, key);
uint64_t f_idx = F_IDX(f_hash, level->addr_capacity);
uint64_t s_idx = S_IDX(s_hash, level->addr_capacity);
uint64_t i, j;
for(i = 0; i < 2; i ++){
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i][f_idx].token[j] == 1&&strcmp((const char *)level->buckets[i][f_idx].slot[j].key, (const char *)key) == 0)
{
level->buckets[i][f_idx].token[j] = 0;
level->level_item_num[i] --;
return 0;
}
}
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i][s_idx].token[j] == 1&&strcmp((const char *)level->buckets[i][s_idx].slot[j].key, (const char *)key) == 0)
{
level->buckets[i][s_idx].token[j] = 0;
level->level_item_num[i] --;
return 0;
}
}
f_idx = F_IDX(f_hash, level->addr_capacity / 2);
s_idx = S_IDX(s_hash, level->addr_capacity / 2);
}
return 1;
}
uint8_t level_update(level_hash *level, uint8_t *key, uint8_t *new_value)
//修改指定键值
{
uint64_t f_hash = F_HASH(level, key);
uint64_t s_hash = S_HASH(level, key);
uint64_t f_idx = F_IDX(f_hash, level->addr_capacity);
uint64_t s_idx = S_IDX(s_hash, level->addr_capacity);
uint64_t i, j;
for(i = 0; i < 2; i ++){
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i][f_idx].token[j] == 1&&strcmp((const char *)level->buckets[i][f_idx].slot[j].key, (const char *)key) == 0)
{
memcpy(level->buckets[i][f_idx].slot[j].value, new_value, VALUE_LEN);
return 0;
}
}
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[i][s_idx].token[j] == 1&&strcmp((const char *)level->buckets[i][s_idx].slot[j].key, (const char *)key) == 0)
{
memcpy(level->buckets[i][s_idx].slot[j].value, new_value, VALUE_LEN);
return 0;
}
}
f_idx = F_IDX(f_hash, level->addr_capacity / 2);
s_idx = S_IDX(s_hash, level->addr_capacity / 2);
}
return 1;
}
uint8_t level_insert(level_hash *level, uint8_t *key, uint8_t *value)
//插入指定键值
{
uint64_t f_hash = F_HASH(level, key);
uint64_t s_hash = S_HASH(level, key);
uint64_t f_idx = F_IDX(f_hash, level->addr_capacity);
uint64_t s_idx = S_IDX(s_hash, level->addr_capacity);
uint64_t i, j;
int empty_location;
for(i = 0; i < 2; i ++){
for(j = 0; j < ASSOC_NUM; j ++){
/* The new item is inserted into the less-loaded bucket between
the two hash locations in each level
*/
if (level->buckets[i][f_idx].token[j] == 0)
{
memcpy(level->buckets[i][f_idx].slot[j].key, key, KEY_LEN);
memcpy(level->buckets[i][f_idx].slot[j].value, value, VALUE_LEN);
level->buckets[i][f_idx].token[j] = 1;
level->level_item_num[i] ++;
return 0;
}
if (level->buckets[i][s_idx].token[j] == 0)
{
memcpy(level->buckets[i][s_idx].slot[j].key, key, KEY_LEN);
memcpy(level->buckets[i][s_idx].slot[j].value, value, VALUE_LEN);
level->buckets[i][s_idx].token[j] = 1;
level->level_item_num[i] ++;
return 0;
}
}
f_idx = F_IDX(f_hash, level->addr_capacity / 2);
s_idx = S_IDX(s_hash, level->addr_capacity / 2);
}
f_idx = F_IDX(f_hash, level->addr_capacity);
s_idx = S_IDX(s_hash, level->addr_capacity);
for(i = 0; i < 2; i++){
if(!try_movement(level, f_idx, i, key, value)){
return 0;
}
if(!try_movement(level, s_idx, i, key, value)){
return 0;
}
f_idx = F_IDX(f_hash, level->addr_capacity/2);
s_idx = S_IDX(s_hash, level->addr_capacity/2);
}
if(level->level_expand_time > 0){
empty_location = b2t_movement(level, f_idx);
if(empty_location != -1){
memcpy(level->buckets[1][f_idx].slot[empty_location].key, key, KEY_LEN);
memcpy(level->buckets[1][f_idx].slot[empty_location].value, value, VALUE_LEN);
level->buckets[1][f_idx].token[empty_location] = 1;
level->level_item_num[1] ++;
return 0;
}
empty_location = b2t_movement(level, s_idx);
if(empty_location != -1){
memcpy(level->buckets[1][s_idx].slot[empty_location].key, key, KEY_LEN);
memcpy(level->buckets[1][s_idx].slot[empty_location].value, value, VALUE_LEN);
level->buckets[1][s_idx].token[empty_location] = 1;
level->level_item_num[1] ++;
return 0;
}
}
return 1;
}
uint8_t try_movement(level_hash *level, uint64_t idx, uint64_t level_num, uint8_t *key, uint8_t *value)
//将指定的键值对转移到其另一个地方
{
uint64_t i, j, jdx;
for(i = 0; i < ASSOC_NUM; i ++){
uint8_t *m_key = level->buckets[level_num][idx].slot[i].key;
uint8_t *m_value = level->buckets[level_num][idx].slot[i].value;
uint64_t f_hash = F_HASH(level, m_key);
uint64_t s_hash = S_HASH(level, m_key);
uint64_t f_idx = F_IDX(f_hash, level->addr_capacity/(1+level_num));
uint64_t s_idx = S_IDX(s_hash, level->addr_capacity/(1+level_num));
if(f_idx == idx)
jdx = s_idx;
else
jdx = f_idx;
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[level_num][jdx].token[j] == 0)
{
memcpy(level->buckets[level_num][jdx].slot[j].key, m_key, KEY_LEN);
memcpy(level->buckets[level_num][jdx].slot[j].value, m_value, VALUE_LEN);
level->buckets[level_num][jdx].token[j] = 1;
level->buckets[level_num][idx].token[i] = 0;
// The movement is finished and then the new item is inserted
memcpy(level->buckets[level_num][idx].slot[i].key, key, KEY_LEN);
memcpy(level->buckets[level_num][idx].slot[i].value, value, VALUE_LEN);
level->buckets[level_num][idx].token[i] = 1;
level->level_item_num[level_num] ++;
return 0;
}
}
}
return 1;
}
int b2t_movement(level_hash *level, uint64_t idx)
//尝试将底层的键值对移到顶层
{
uint8_t *key, *value;
uint64_t s_hash, f_hash;
uint64_t s_idx, f_idx;
uint64_t i, j;
for(i = 0; i < ASSOC_NUM; i ++){
key = level->buckets[1][idx].slot[i].key;
value = level->buckets[1][idx].slot[i].value;
f_hash = F_HASH(level, key);
s_hash = S_HASH(level, key);
f_idx = F_IDX(f_hash, level->addr_capacity);
s_idx = S_IDX(s_hash, level->addr_capacity);
for(j = 0; j < ASSOC_NUM; j ++){
if (level->buckets[0][f_idx].token[j] == 0)
{
memcpy(level->buckets[0][f_idx].slot[j].key, key, KEY_LEN);
memcpy(level->buckets[0][f_idx].slot[j].value, value, VALUE_LEN);
level->buckets[0][f_idx].token[j] = 1;
level->buckets[1][idx].token[i] = 0;
level->level_item_num[0] ++;
level->level_item_num[1] --;
return i;
}
else if (level->buckets[0][s_idx].token[j] == 0)
{
memcpy(level->buckets[0][s_idx].slot[j].key, key, KEY_LEN);
memcpy(level->buckets[0][s_idx].slot[j].value, value, VALUE_LEN);
level->buckets[0][s_idx].token[j] = 1;
level->buckets[1][idx].token[i] = 0;
level->level_item_num[0] ++;
level->level_item_num[1] --;
return i;
}
}
}
return -1;
}
void level_destroy(level_hash *level)
//销毁一个level_hash
{
free(level->buckets[0]);
free(level->buckets[1]);
level = NULL;
}