forked from goossaert/hashmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadow_hashmap.cc
263 lines (212 loc) · 7.87 KB
/
shadow_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
#include "shadow_hashmap.h"
namespace hashmap {
int ShadowHashMap::Open() {
buckets_ = new Bucket[num_buckets_];
memset(buckets_, 0, sizeof(Bucket) * (num_buckets_));
monitoring_ = new hashmap::Monitoring(num_buckets_, size_neighborhood_max_, static_cast<HashMap*>(this));
return 0;
}
int ShadowHashMap::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 ShadowHashMap::Get(const std::string& key, std::string* value) {
uint64_t hash = hash_function(key);
uint64_t index_init = hash % num_buckets_;
bool found = false;
uint32_t i;
for (i = 0; i < size_neighborhood_; i++) {
uint64_t index_current = (index_init + i) % num_buckets_;
if ( buckets_[index_current].entry != NULL
&& buckets_[index_current].hash == hash
&& 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;
}
}
if (found) return 0;
monitoring_->AddDMB(size_neighborhood_);
monitoring_->AddAlignedDMB(index_init, (index_init + i) % num_buckets_);
return 1;
}
uint64_t ShadowHashMap::FindEmptyBucketAndDoSwaps(uint64_t index_init) {
// In this function, the modulos function is being applied on indexes at the last moment,
// when they are being used or returned. This allows to handle cases where the
// indexes are cycling back to the beginning of the bucket array.
bool found = false;
uint64_t index_current = index_init;
for (uint32_t i = 0; i < size_probing_; i++) {
index_current = index_init + i;
if (buckets_[index_current % num_buckets_].entry == NULL) {
found = true;
monitoring_->AddDFB(i);
monitoring_->AddAlignedDFB(index_init, index_current);
break;
}
}
if (!found) {
return num_buckets_;
}
int num_swaps = 0;
uint64_t index_empty = index_current;
while (index_empty - index_init >= size_neighborhood_) {
uint64_t index_base_min = index_empty - (size_neighborhood_ - 1);
bool found_swap = false;
for (uint32_t i = size_neighborhood_ - 1; i > 0; i--) {
uint64_t index_candidate = index_empty - i;
if (index_candidate < index_init) continue;
if (buckets_[index_candidate % num_buckets_].hash % num_buckets_ >= index_base_min) {
// the candidate has its base bucket within the right scope, so we swap!
buckets_[index_empty % num_buckets_].entry = buckets_[index_candidate % num_buckets_].entry;
buckets_[index_empty % num_buckets_].hash = buckets_[index_candidate % num_buckets_].hash;
buckets_[index_candidate % num_buckets_].entry = NULL;
buckets_[index_candidate % num_buckets_].hash = 0;
uint64_t dib = monitoring_->GetDIB(index_candidate % num_buckets_);
monitoring_->RemoveDIB(index_candidate % num_buckets_);
monitoring_->SetDIB(index_empty % num_buckets_, dib);
index_empty = index_candidate;
found_swap = true;
num_swaps += 1;
break;
}
}
if (!found_swap) {
if (size_neighborhood_ < size_neighborhood_max_) {
size_neighborhood_ *= 2;
//std::cerr << "Increasing neighborhood, now " << size_neighborhood_ << std::endl;
} else {
// For debugging only, dump of the area around the neighborhood
if (false) {
//fprintf(stderr, "index [%" PRIu64 "] empty [%" PRIu64 "]\n", index_init, index_empty);
uint32_t index_temp = index_empty - size_neighborhood_ + 1;
if (index_temp > index_init) index_temp = index_init;
if (index_temp < 20) {
index_temp = 0;
} else {
index_temp -= 20;
}
for (; index_temp <= index_empty + 20; index_temp++) {
if (index_temp == index_empty - size_neighborhood_ + 1) {
fprintf(stderr, "neigh ");
} else if (index_temp == index_init) {
fprintf(stderr, "index ");
} else if (index_temp == index_empty) {
fprintf(stderr, "empty ");
} else {
fprintf(stderr, " ");
}
fprintf(stderr, " %7du ", index_temp);
if (buckets_[index_temp % num_buckets_].entry == NULL) {
fprintf(stderr, " EMP");
} else {
fprintf(stderr, "%7" PRIu64 " ", buckets_[index_temp % num_buckets_].hash % num_buckets_);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
}
return num_buckets_;
}
}
}
monitoring_->SetDIB(index_empty % num_buckets_,
index_empty - index_init);
monitoring_->AddNumberOfSwaps(num_swaps);
return index_empty % num_buckets_;
}
int ShadowHashMap::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);
// TODO: Put() should use Exists() and perform a replacement if needed.
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());
ShadowHashMap::Entry *entry = new ShadowHashMap::Entry;
entry->size_key = key.size();
entry->size_value = value.size();
entry->data = data;
buckets_[index_empty].entry = entry;
buckets_[index_empty].hash = hash;
return 0;
}
int ShadowHashMap::Exists(const std::string& key) {
// TODO: implement
return 0;
}
int ShadowHashMap::Remove(const std::string& key) {
uint64_t hash = hash_function(key);
uint64_t index_init = hash % num_buckets_;
bool found = false;
uint64_t index_current;
for (uint32_t i = 0; i < size_neighborhood_; i++) {
index_current = (index_init + i) % num_buckets_;
if ( buckets_[index_current].entry != NULL
&& buckets_[index_current].hash == hash
&& key.size() == buckets_[index_current].entry->size_key
&& memcmp(buckets_[index_current].entry->data, key.c_str(), key.size()) == 0) {
found = true;
break;
}
}
if (found) {
delete[] buckets_[index_current].entry->data;
delete buckets_[index_current].entry;
buckets_[index_current].entry = NULL;
monitoring_->RemoveDIB(index_current);
return 0;
}
return 0;
}
int ShadowHashMap::Resize() {
// TODO: implement
return 0;
}
// For debugging
int ShadowHashMap::CheckDensity() {
return 0;
}
int ShadowHashMap::BucketCounts() {
std::cout << "current neighborhood: " << size_neighborhood_ << std::endl;
return 0;
}
int ShadowHashMap::Dump() {
return 0;
}
int ShadowHashMap::GetBucketState(int index) {
if (buckets_[index].entry == NULL) {
return 0;
}
return 1;
}
int ShadowHashMap::FillInitIndex(uint64_t index_stored, uint64_t *index_init) {
if(buckets_[index_stored].entry == NULL) return -1;
*index_init = buckets_[index_stored].hash % num_buckets_;
return 0;
}
void ShadowHashMap::GetMetadata(std::map< std::string, std::string >& metadata) {
metadata["name"] = "shadow";
char buffer[1024];
sprintf(buffer, "{\"num_buckets\": %" PRIu64 ", \"size_probing\": %u, \"size_neighborhood_start\": %u, \"size_neighborhood_end\": %u}", num_buckets_, size_probing_, size_neighborhood_start_, size_neighborhood_max_);
metadata["parameters_hashmap"] = buffer;
sprintf(buffer, "nb%" PRIu64 "-sp%u-sns%u-sne%u", num_buckets_, size_probing_, size_neighborhood_start_, size_neighborhood_max_);
metadata["parameters_hashmap_string"] = buffer;
}
};