-
Notifications
You must be signed in to change notification settings - Fork 83
/
bitmap_hashmap.cc
347 lines (282 loc) · 9.66 KB
/
bitmap_hashmap.cc
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
#include "bitmap_hashmap.h"
namespace hashmap {
int BitmapHashMap::Open() {
buckets_ = new Bucket[num_buckets_ + size_neighborhood_];
memset(buckets_, 0, sizeof(Bucket) * (num_buckets_ + size_neighborhood_));
monitoring_ = new hashmap::Monitoring(num_buckets_, size_neighborhood_, static_cast<HashMap*>(this));
return 0;
}
int BitmapHashMap::Close() {
if (buckets_ != NULL) {
for (uint32_t i = 0; i < num_buckets_; i++) {
if (buckets_[i].entry != NULL) {
delete[] buckets_[i].entry->data;
delete buckets_[i].entry;
}
}
delete[] buckets_;
}
if (monitoring_ != NULL) {
delete monitoring_;
}
return 0;
}
int BitmapHashMap::Get(const std::string& key, std::string* value) {
uint64_t hash = hash_function(key);
uint64_t index_init = hash % num_buckets_;
uint32_t mask = 1 << (size_neighborhood_-1);
bool found = false;
uint32_t i;
uint32_t dmb = 0;
for (i = 0; i < size_neighborhood_; i++) {
if (buckets_[index_init].bitmap & mask) {
dmb = i;
uint64_t index_current = (index_init + i) % num_buckets_;
if ( buckets_[index_current].entry != NULL
&& key.size() == buckets_[index_current].entry->size_key
&& memcmp(buckets_[index_current].entry->data, key.c_str(), key.size()) == 0) {
*value = std::string(buckets_[index_current].entry->data + key.size(),
buckets_[index_current].entry->size_value);
found = true;
break;
}
}
mask = mask >> 1;
}
if (found) return 0;
monitoring_->AddDMB(dmb);
monitoring_->AddAlignedDMB(index_init, (index_init + i) % num_buckets_);
return 1;
}
uint64_t BitmapHashMap::FindEmptyBucketAndDoSwaps(uint64_t index_init) {
bool found = false;
uint64_t index_current = index_init;
for (uint32_t i = 0; i < size_probing_; i++) {
index_current = (index_init + i) % num_buckets_;
if (buckets_[index_current].entry == NULL) {
found = true;
monitoring_->AddDFB(i);
monitoring_->AddAlignedDFB(index_init, index_current);
break;
}
}
if (!found) {
return num_buckets_;
}
int num_swaps = 0;
uint32_t index_base = 0;
uint64_t index_empty = index_current;
while ( (index_empty >= index_init && (index_empty - index_init) >= size_neighborhood_)
|| (index_empty < index_init && (index_empty + num_buckets_ - index_init) >= size_neighborhood_)) {
uint64_t index_base_init = (num_buckets_ + index_empty - (size_neighborhood_ - 1)) % num_buckets_;
// For each candidate base bucket
bool found_swap = false;
for (uint32_t i = 0; i < size_neighborhood_ - 1; i++) {
// -1 because no need to test the bucket at index_empty
// For each mask position
index_base = (index_base_init + i) % num_buckets_;
uint32_t mask = 1 << (size_neighborhood_-1);
for (uint32_t j = 0; j < size_neighborhood_ - i - 1; j++) {
if (buckets_[index_base].bitmap & mask) {
// Found, so now we swap buckets and update the bitmap
uint32_t index_candidate = (index_base + j) % num_buckets_;
buckets_[index_empty].entry = buckets_[index_candidate].entry;
buckets_[index_base].bitmap &= ~mask;
uint32_t mask_new = 1 << i;
buckets_[index_base].bitmap |= mask_new;
// Move PSL monitoring
uint64_t dib = monitoring_->GetDIB(index_candidate);
monitoring_->RemoveDIB(index_candidate);
monitoring_->SetDIB(index_empty, dib);
// Prepare for next iteration
index_empty = index_candidate;
found_swap = true;
num_swaps += 1;
break;
}
mask = mask >> 1;
}
if (found_swap) break;
}
if (!found_swap) {
// This is a dirty hack in case no reordering worked but we already had a
// few swaps, we want to avoid having the same entry pointer in two
// different buckets, which would make the program crash when freeing
// the memory in Close().
// This should be changed whenever the Resize() method is implemented.
buckets_[index_empty].entry = NULL;
return num_buckets_;
}
}
// Monitoring
uint64_t dib;
if (index_empty >= index_init) {
dib = index_empty - index_init;
} else {
dib = index_empty + num_buckets_ - index_init;
}
monitoring_->SetDIB(index_empty, dib);
monitoring_->AddNumberOfSwaps(num_swaps);
return index_empty;
}
int BitmapHashMap::Put(const std::string& key, const std::string& value) {
uint64_t hash = hash_function(key);
uint64_t index_init = hash % num_buckets_;
uint64_t index_empty = FindEmptyBucketAndDoSwaps(index_init);
if (index_empty == num_buckets_) {
return 1;
}
char *data = new char[key.size() + value.size()];
memcpy(data, key.c_str(), key.size());
memcpy(data + key.size(), value.c_str(), value.size());
BitmapHashMap::Entry *entry = new BitmapHashMap::Entry;
entry->size_key = key.size();
entry->size_value = value.size();
entry->data = data;
buckets_[index_empty].entry = entry;
uint32_t mask;
if (index_empty >= index_init) {
mask = 1 << (size_neighborhood_ - ((index_empty - index_init) + 1));
} else {
mask = 1 << (size_neighborhood_ - ((index_empty + num_buckets_ - index_init) + 1));
}
buckets_[index_init].bitmap |= mask;
return 0;
}
int BitmapHashMap::Exists(const std::string& key) {
// TODO: implement
return 0;
}
int BitmapHashMap::Remove(const std::string& key) {
uint64_t hash = hash_function(key);
uint64_t index_init = hash % num_buckets_;
uint32_t mask = 1 << (size_neighborhood_-1);
bool found = false;
uint64_t index_current;
for (uint32_t i = 0; i < size_neighborhood_; i++) {
if (buckets_[index_init].bitmap & mask) {
index_current = (index_init + i) % num_buckets_;
if ( key.size() == buckets_[index_current].entry->size_key
&& memcmp(buckets_[index_current].entry->data, key.c_str(), key.size()) == 0) {
found = true;
break;
}
}
mask = mask >> 1;
}
if (found) {
//fprintf(stderr, "Remove() [%s] %" PRIu64 " %" PRIu64 "\n", key.c_str(), index_init, index_current);
delete[] buckets_[index_current].entry->data;
delete buckets_[index_current].entry;
buckets_[index_current].entry = NULL;
buckets_[index_init].bitmap = buckets_[index_init].bitmap & (~mask);
monitoring_->RemoveDIB(index_current);
return 0;
}
return 1;
}
int BitmapHashMap::Resize() {
// TODO: implement
// If the resize is called when FindEmptyBucketAndDoSwaps() cannot perform
// the necessary swaps, then make sure that the item being inserted
// or swapped is not nullified and that it is correctly inserted
// after the resize.
return 0;
}
// For debugging
int BitmapHashMap::CheckDensity() {
int num_pages = 0;
int count_empty = 0;
int count_probe = 0;
int level = 32;
for (uint32_t i = 0; i < num_buckets_; i++) {
if (buckets_[i].entry == NULL) {
count_empty += 1;
} else {
count_probe += 1;
}
if (i > 0 && i % level == 0) {
if (count_probe < 0.25 * level) {
std::cout << ".";
} else if (count_probe < 0.5 * level) {
std::cout << ":";
} else if (count_probe < 0.75 * level) {
std::cout << "|";
} else if (count_probe < 0.85 * level) {
std::cout << "o";
} else if (count_probe < 0.95 * level) {
std::cout << "U";
} else if (count_probe < level) {
std::cout << "O";
} else {
std::cout << "0";
}
count_probe = 0;
num_pages += 1;
}
}
std::cout << std::endl;
std::cout << "Count empty: " << count_empty << "/" << num_buckets_ << std::endl;
std::cout << "Pages: " << num_pages << " | " << num_pages * level << std::endl;
return 0;
}
int BitmapHashMap::BucketCounts() {
int counts[33];
for (int i = 0; i <= 32; i++) {
counts[i] = 0;
}
int total = 0;
for (uint32_t i = 0; i < num_buckets_; i++) {
counts[hamming2(buckets_[i].bitmap)] += 1;
}
for (int i = 0; i <= 32; i++) {
std::cout << "size " << i << ": " << counts[i] << std::endl;
total += counts[i];
}
std::cout << "total: " << total << std::endl;
return 0;
}
int BitmapHashMap::Dump() {
for (uint32_t i = 0; i < num_buckets_ + size_neighborhood_; i++) {
std::cout << "bitmap: ";
for (uint32_t j = 0; j < size_neighborhood_; j++) {
uint32_t mask = 1 << (size_neighborhood_-1-j);
if (buckets_[i].bitmap & mask) {
std::cout << "1";
} else {
std::cout << "0";
}
}
if (buckets_[i].entry != NULL) {
std::string key(buckets_[i].entry->data,
buckets_[i].entry->size_key);
std::string value(buckets_[i].entry->data + buckets_[i].entry->size_key,
buckets_[i].entry->size_value);
std::cout << " | index: " << i << " - " << key << " " << value;
}
std::cout << std::endl;
}
return 0;
}
int BitmapHashMap::GetBucketState(int index) {
if (buckets_[index].entry == NULL) {
return 0;
}
return 1;
}
int BitmapHashMap::FillInitIndex(uint64_t index_stored, uint64_t *index_init) {
if(buckets_[index_stored].entry == NULL) return -1;
std::string key(buckets_[index_stored].entry->data,
buckets_[index_stored].entry->size_key);
*index_init = hash_function(key) % num_buckets_;
return 0;
}
void BitmapHashMap::GetMetadata(std::map< std::string, std::string >& metadata) {
metadata["name"] = "bitmap";
char buffer[1024];
sprintf(buffer, "{\"num_buckets\": %" PRIu64 ", \"size_probing\": %u}", num_buckets_, size_probing_);
metadata["parameters_hashmap"] = buffer;
sprintf(buffer, "nb%" PRIu64 "-sp%u", num_buckets_, size_probing_);
metadata["parameters_hashmap_string"] = buffer;
}
};