-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhashbucket.h
381 lines (315 loc) · 10.1 KB
/
hashbucket.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
//
// (c) 2016 by University of Delaware, Argonne National Laboratory, San Diego
// Supercomputer Center, National University of Defense Technology,
// National Supercomputer Center in Guangzhou, and Sun Yat-sen University.
//
// See COPYRIGHT in top-level directory.
//
#ifndef HASH_BUCKET_H
#define HASH_BUCKET_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include "globals.h"
#include "hash.h"
#include "memory.h"
#include "log.h"
#include "config.h"
#include "stat.h"
#include "hash.h"
#include "memory.h"
#include "hashbucket.h"
#include "serializer.h"
namespace MIMIR_NS {
struct CombinerVal
{
char *kv;
};
struct ReducerVal
{
int64_t valbytes;
char *values_start;
char *values_end;
};
struct EmptyVal
{
};
template <typename ValType = EmptyVal>
class HashBucket
{
public:
struct HashEntry
{
char *key; // key bytes
int keysize; // key size
ValType val; // val
HashEntry *next;
};
HashBucket(int scale = 1, bool iscopykey = false, bool isremove = false)
{
this->scale = scale;
this->iscopykey = iscopykey;
this->isremove = isremove;
nbucket = BUCKET_COUNT;
buckets = (HashEntry **) mem_aligned_malloc(
MEMPAGE_SIZE, sizeof(HashEntry *) * nbucket);
for (int i = 0; i < nbucket; i++) buckets[i] = NULL;
mem_bytes += sizeof(HashEntry *) * nbucket;
PROFILER_RECORD_COUNT(COUNTER_HASH_BUCKET, this->mem_bytes, OPMAX);
buf_size = (int) DATA_PAGE_SIZE;
buf_idx = 0;
buf_off = 0;
nunique = 0;
LOG_PRINT(DBG_GEN, "HashBucket: create nbucket=%d\n", nbucket);
}
virtual ~HashBucket()
{
mem_bytes -= sizeof(HashEntry *) * nbucket;
mem_aligned_free(buckets);
for (size_t i = 0; i < buffers.size(); i++) {
mem_bytes -= buf_size;
if (buffers[i] != NULL) mem_aligned_free(buffers[i]);
}
LOG_PRINT(DBG_GEN, "HashBucket: destroy.\n");
}
void print()
{
for (int i = 0; i < nbucket; i++) {
int64_t nitem = 0;
HashEntry *ptr = this->buckets[i];
while (ptr != NULL) {
nitem++;
ptr = ptr->next;
}
LOG_PRINT(DBG_GEN, "Hash entry %d: %ld\n", i, nitem);
}
}
ValType *findEntry(char *key, int keysize)
{
// Compute bucket index
uint32_t ibucket = (hashlittle(key, keysize, 0) / scale) % nbucket;
// Search the key
HashEntry *ptr = this->buckets[ibucket];
while (ptr != NULL) {
if (ptr->keysize == keysize
&& memcmp(ptr->key, key, keysize) == 0) {
break;
}
ptr = ptr->next;
}
if (ptr) return &(ptr->val);
return NULL;
}
ValType *updateEntry(char *key, int keysize, char *newkey)
{
// Compute bucket index
uint32_t ibucket = (hashlittle(key, keysize, 0) / scale) % nbucket;
// Search the key
HashEntry *ptr = this->buckets[ibucket];
while (ptr != NULL) {
if (ptr->keysize == keysize
&& memcmp(ptr->key, key, keysize) == 0) {
break;
}
ptr = ptr->next;
}
if (ptr) {
ptr->key = newkey;
return &(ptr->val);
}
LOG_ERROR("Cannot find the entry key=%s, ibucket=%d!\n", key, ibucket);
return NULL;
}
virtual void removeEntry(char *key, int keysize)
{
if (!isremove) {
LOG_ERROR("This hash bucket does not support remove function!\n");
}
// Compute bucket index
uint32_t ibucket = (hashlittle(key, keysize, 0) / scale) % nbucket;
// Search the key
HashEntry *ptr = this->buckets[ibucket];
HashEntry *pre_ptr = ptr;
while (ptr != NULL) {
if (ptr->keysize == keysize
&& memcmp(ptr->key, key, keysize) == 0) {
break;
}
pre_ptr = ptr;
ptr = ptr->next;
}
if (ptr) {
int ssize = (int) sizeof(HashEntry);
if (iscopykey) ssize += ptr->keysize;
slices.insert(std::make_pair((char *) ptr, ssize));
if (ptr == buckets[ibucket])
buckets[ibucket] = ptr->next;
else
pre_ptr->next = ptr->next;
nunique--;
}
}
virtual void insertEntry(char *key, int keysize, ValType *val)
{
HashEntry *entry = NULL;
int entry_size = sizeof(HashEntry);
if (iscopykey) entry_size += keysize;
if (isremove) {
// Find a slice to store the entry
std::unordered_map<char *, int>::iterator iter;
for (iter = this->slices.begin(); iter != this->slices.end();
iter++) {
char *sbuf = iter->first;
int ssize = iter->second;
if (ssize >= entry_size) {
entry = (HashEntry *) (sbuf + (ssize - entry_size));
entry->keysize = keysize;
entry->val = *val;
entry->next = NULL;
if (iscopykey) {
memcpy((char *) entry + sizeof(HashEntry), key,
keysize);
entry->key = (char *) entry + sizeof(HashEntry);
}
else {
entry->key = key;
}
if (iter->second == entry_size)
this->slices.erase(iter);
else
this->slices[iter->first] -= entry_size;
// Add to the list
uint32_t ibucket
= (hashlittle(key, keysize, 0) / scale) % nbucket;
if (buckets[ibucket] == NULL) {
buckets[ibucket] = entry;
}
else {
HashEntry *tmp = buckets[ibucket];
buckets[ibucket] = entry;
entry->next = tmp;
}
nunique++;
return;
}
}
}
// Add a new buffer
if (buf_idx == (int) buffers.size()) {
char *buffer = (char *) mem_aligned_malloc(MEMPAGE_SIZE, buf_size);
mem_bytes += buf_size;
PROFILER_RECORD_COUNT(COUNTER_HASH_BUCKET, this->mem_bytes, OPMAX);
buffers.push_back(buffer);
buf_off = 0;
}
// Add a new buffer
if (entry_size > buf_size) LOG_ERROR("Entry is too long!\n");
if (buf_size - buf_off < entry_size) {
memset(buffers[buf_idx] + buf_off, 0, buf_size - buf_off);
buf_idx += 1;
buf_off = 0;
if (buf_idx == (int) buffers.size()) {
char *buffer
= (char *) mem_aligned_malloc(MEMPAGE_SIZE, buf_size);
mem_bytes += buf_size;
PROFILER_RECORD_COUNT(COUNTER_HASH_BUCKET, this->mem_bytes,
OPMAX);
buffers.push_back(buffer);
}
}
// Add entry
entry = (HashEntry *) (buffers[buf_idx] + buf_off);
entry->keysize = keysize;
entry->val = *val;
entry->next = NULL;
buf_off += (int) sizeof(HashEntry);
if (iscopykey) {
memcpy(buffers[buf_idx] + buf_off, key, keysize);
entry->key = buffers[buf_idx] + buf_off;
buf_off += keysize;
}
else {
entry->key = key;
}
// Add to the list
uint32_t ibucket = (hashlittle(key, keysize, 0) / scale) % nbucket;
if (buckets[ibucket] == NULL) {
buckets[ibucket] = entry;
}
else {
HashEntry *tmp = buckets[ibucket];
buckets[ibucket] = entry;
entry->next = tmp;
}
nunique++;
}
void open()
{
iter_buf_idx = 0;
iter_buf_off = 0;
iunique = 0;
}
void close() {}
HashEntry *next()
{
if (iunique >= nunique) return NULL;
if (iter_buf_idx == buf_idx && iter_buf_off >= buf_off) return NULL;
while (1) {
auto iter = this->slices.find(buffers[iter_buf_idx] + iter_buf_off);
if (iter != this->slices.end()) {
iter_buf_off += iter->second;
continue;
}
if ((buf_size - iter_buf_off) < (int) sizeof(HashEntry)) {
iter_buf_idx += 1;
iter_buf_off = 0;
continue;
}
else {
HashEntry *entry
= (HashEntry *) (buffers[iter_buf_idx] + iter_buf_off);
if (entry->key == NULL) {
iter_buf_idx += 1;
iter_buf_off = 0;
continue;
}
}
if (iter_buf_idx == buf_idx && iter_buf_off >= buf_off) return NULL;
break;
}
HashEntry *entry = (HashEntry *) (buffers[iter_buf_idx] + iter_buf_off);
if (iscopykey)
iter_buf_off += ((int) sizeof(HashEntry) + entry->keysize);
else
iter_buf_off += (int) sizeof(HashEntry);
iunique += 1;
return entry;
}
int64_t get_nunique() { return nunique; }
virtual void clear()
{
for (int i = 0; i < nbucket; i++) buckets[i] = NULL;
nunique = 0;
buf_idx = 0;
buf_off = 0;
slices.clear();
}
protected:
int scale;
bool iscopykey;
bool isremove;
int nbucket;
HashEntry **buckets;
int buf_size, buf_idx, buf_off;
std::vector<char *> buffers;
int iter_buf_idx, iter_buf_off;
uint64_t iunique, nunique;
std::unordered_map<char *, int> slices;
public:
static uint64_t mem_bytes;
};
template <typename ValType>
uint64_t HashBucket<ValType>::mem_bytes = 0;
} // namespace MIMIR_NS
#endif