-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtendibleHashing.cpp
More file actions
402 lines (316 loc) · 11.4 KB
/
Copy pathExtendibleHashing.cpp
File metadata and controls
402 lines (316 loc) · 11.4 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
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
#include "ExtendibleHashing.h"
#include <bitset>
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//Display not found
void displayNotFound(int key) {
cout << "\t\t\t key: " << key << " \t value : \t not found" << endl;
}
//Display one record entry
void displayItem(DataItem* dataItem) {
if (dataItem != 0 && dataItem->valid) {
cout << "\t\t\t key: " << bitset<8>(dataItem->key) << "\t value:\t" << dataItem->data << endl;
} else {
cout << "\t\t\t key: ============ \t value:\t empty data" << endl;
}
}
//Display one Bucket
void displayBucket(Bucket& currentBucket, string& depths, string& values, int verbose) {
depths.append(to_string(currentBucket.localDepth));
depths.append(",");
values.append("[");
if (verbose)
cout << "\t\tBucket:\t local depth:" << currentBucket.localDepth << endl;
if (currentBucket.currentEntries == 0) {
if (verbose)
cout << "\t\t\tNo Data yet\n";
for (size_t i = 0; i < RECORDSPERBUCKET; i++) {
values.append("null");
values.append(",");
}
} else {
for (size_t i = 0; i < RECORDSPERBUCKET; i++)
{
if (currentBucket.dataItem[i].valid) {
values.append(to_string(currentBucket.dataItem[i].data));
values.append(",");
if (verbose)
cout << "\t\t\t key: " << bitset<8>(currentBucket.dataItem[i].key) << "\t value:\t" << currentBucket.dataItem[i].data << endl;
} else {
values.append("null");
values.append(",");
if (verbose)
cout << "\t\t\t key: ============ \t value:\t empty data" << endl;
}
}
}
values.pop_back();
values.append("]");
}
//Display Directory and it's contents
void displayDirectory(GlobalDirectory& globaldirectory, Bucket& currentBucket, int verbose) {
cout << "Directory:\t global depth:" << globaldirectory.globalDepth << endl;
string values = "(";
string depths = "(";
int count = 0;
if (globaldirectory.length == 0) {
count++;
cout << "\tNo Directory yet\n";
displayBucket(currentBucket, depths, values, verbose);
} else {
for (size_t i = 0; i < globaldirectory.length; i++) {
if (i == 0) {
count++;
} else if (globaldirectory.entry[i - 1] != globaldirectory.entry[i]) {
count++;
}
if (verbose)
cout << "\t key: " << bitset<8>(i) << "\t value:\t" << globaldirectory.entry[i] << endl;
displayBucket(*globaldirectory.entry[i], depths, values, verbose);
if (verbose)
cout << "-----------------------------------------------\n\n";
}
depths.pop_back();
}
values.append(")");
depths.append(")");
cout << " buckets:\t" << count << "/" << globaldirectory.length << endl;
cout << "values:\t" << values << endl;
cout << "depths:\t" << depths << endl;
cout << "=========================\n";
char t[100];
}
//Get Hash of the bucket related to the given key
int getCurrentHash(int key, int depth) {
return (key & MAXKEYVALUE) >> (MAXKEYLENGTH - depth);
}
//Get the peer hash of a given bucket hash
int getPeerBucketKey(int hashedKey, int bitLength) {
string bitString = bitset<32>(hashedKey).to_string();
bitString = bitString.substr(32 - bitLength, bitLength);
bitString[bitLength - 1] = (bitString[bitLength - 1] == '1') ? '0' : '1';
int modifiedKey = stoi(bitString, nullptr, 2);
return modifiedKey;
}
//Try to insert item into a bucket
int insertItemIntoBucket(Bucket& currentBucket, DataItem data) {
if ( data.valid == 0 ) {
return 0;
}
if (currentBucket.currentEntries >= RECORDSPERBUCKET) {
return 0;
}
for (size_t i = 0; i < RECORDSPERBUCKET; i++) {
if (currentBucket.dataItem[i].valid == 0) {
currentBucket.currentEntries = currentBucket.currentEntries + 1;
currentBucket.dataItem[i] = data;
return 1;
}
}
return 0;
}
//Look for an item in a bucket using key
void findItemInBucket(Bucket& currentBucket, int key) {
for (size_t i = 0; i < currentBucket.currentEntries; i++) {
if ( currentBucket.dataItem[i].key == key ) {
if ( currentBucket.dataItem[i].valid == 0 ) {
break;
}
displayItem(¤tBucket.dataItem[i]);
return;
}
}
displayNotFound(key);
return;
}
//Try to Delete item based on a key value from a bucket
int deleteItemFromBucket(Bucket& currentBucket, int key) {
for (size_t i = 0; i < RECORDSPERBUCKET; i++) {
if ( currentBucket.dataItem[i].key == key ) {
if (currentBucket.dataItem[i].valid == 0) {
return 0;
}
currentBucket.currentEntries = currentBucket.currentEntries - 1;
currentBucket.dataItem[i].valid = 0;
return 1;
}
}
return 0;
}
//Try to insert item in the file
int insertItem(DataItem data, Bucket& currentBucket, GlobalDirectory& globaldirectory) {
if ( data.valid == 0 ) {
return 0;
}
if (globaldirectory.globalDepth == 0) {
if (insertItemIntoBucket(currentBucket, data) == 0) {
createFirstTimeDirectory(globaldirectory, currentBucket);
} else {
return 1;
}
}
int hashedKey = getCurrentHash(data.key, globaldirectory.globalDepth);
for (size_t i = 0; i < 5; i++) {
int n = insertItemIntoBucket(*globaldirectory.entry[hashedKey], data);
if (n == 0) {
if (splitBucket(globaldirectory, hashedKey) == 0) {
return 0;
}
} else {
return 1;
}
hashedKey = getCurrentHash(data.key, globaldirectory.globalDepth);
}
return 0;
}
//Search the directory for an item using the key
void searchItem(int key, Bucket& currentBucket, GlobalDirectory& globaldirectory) {
if (globaldirectory.length == 0) {
findItemInBucket(currentBucket, key);
return;
}
int hashedKey = getCurrentHash(key, globaldirectory.globalDepth);
currentBucket = *globaldirectory.entry[hashedKey];
findItemInBucket(currentBucket, key);
}
//Search on an item based on the key and delete it
int deleteItem(int key, Bucket& currentBucket, GlobalDirectory& globaldirectory) {
if (globaldirectory.length == 0) {
return deleteItemFromBucket(currentBucket, key);
}
int hashedKey = getCurrentHash(key, globaldirectory.globalDepth);
if (deleteItemFromBucket(*globaldirectory.entry[hashedKey], key) == 1) {
if (globaldirectory.entry[hashedKey]->currentEntries == 0 && globaldirectory.length > 2) {
int peerHashedKey = getPeerBucketKey(hashedKey, globaldirectory.entry[hashedKey]->localDepth);
if (globaldirectory.entry[peerHashedKey]->localDepth == globaldirectory.entry[hashedKey]->localDepth) {
globaldirectory.entry[hashedKey] = globaldirectory.entry[peerHashedKey];
}
}
int minimizationResult = checkDirectoryMinimization(globaldirectory);
while (minimizationResult != -1) {
minimizationResult = checkDirectoryMinimization(globaldirectory);
}
return 1;
}
return 0;
}
//Create the first directory
int createFirstTimeDirectory(GlobalDirectory& globaldirectory, Bucket& currentBucket) {
globaldirectory.globalDepth = 1;
globaldirectory.length = 2;
globaldirectory.entry = new Bucket * [globaldirectory.length];
globaldirectory.entry[0] = new Bucket(globaldirectory.globalDepth);
globaldirectory.entry[1] = new Bucket(globaldirectory.globalDepth);
for (size_t i = 0; i < RECORDSPERBUCKET; i++) {
int newKey = getCurrentHash(currentBucket.dataItem[i].key, globaldirectory.globalDepth);
if (newKey == -1) {
return -1;
}
insertItemIntoBucket(*globaldirectory.entry[newKey], currentBucket.dataItem[i]);
}
return 1;
}
//Split a given bucket because a bucket is full
int splitBucket(GlobalDirectory &globaldirectory,int splitIndex) {
int hashKey;
int peerKey;
int end;
Bucket bucketToSplit = * globaldirectory.entry[splitIndex];
if (globaldirectory.globalDepth == bucketToSplit.localDepth) {
return extendDirectory(globaldirectory, splitIndex);
}
int bucketRepetitions = 0;
vector<int> matchingIndices;
for(int i = 0; i < globaldirectory.length; i++) {
if (globaldirectory.entry[i] == globaldirectory.entry[splitIndex]) {
matchingIndices.push_back(i);
bucketRepetitions++;
}
}
sort(matchingIndices.begin(), matchingIndices.end());
hashKey = matchingIndices[0];
peerKey = matchingIndices[bucketRepetitions/2];
end = matchingIndices[bucketRepetitions-1];
globaldirectory.entry[hashKey] = new Bucket(bucketToSplit.localDepth+1);
globaldirectory.entry[peerKey] = new Bucket(bucketToSplit.localDepth+1);
for (size_t i = 0; i < globaldirectory.length; i++) {
if (hashKey <= i && peerKey > i) {
globaldirectory.entry[i] = globaldirectory.entry[hashKey];
} else if (peerKey <= i && end >= i) {
globaldirectory.entry[i] = globaldirectory.entry[peerKey];
}
}
for (size_t i = 0; i < bucketToSplit.currentEntries; i++) {
int newKey = getCurrentHash(bucketToSplit.dataItem[i].key, globaldirectory.globalDepth);
if (hashKey <= newKey && peerKey > newKey) {
insertItemIntoBucket(*globaldirectory.entry[splitIndex], bucketToSplit.dataItem[i]);
} else if (peerKey <= newKey && end >= newKey) {
insertItemIntoBucket(*globaldirectory.entry[peerKey], bucketToSplit.dataItem[i]);
}
else{
matchingIndices.clear();
return 0;
}
}
matchingIndices.clear();
return 1;
}
//Extend global directory because a given bucket can not be split
int extendDirectory(GlobalDirectory &globaldirectory,int splitIndex) {
if (globaldirectory.globalDepth == 8) {
return 0;
}
Bucket bucketToSplit = * globaldirectory.entry[splitIndex];
Bucket** oldEntry = globaldirectory.entry;
globaldirectory.length= globaldirectory.length *2;
globaldirectory.globalDepth ++;
globaldirectory.entry = new Bucket * [globaldirectory.length];
int count = 0;
for (size_t i = 0; i < globaldirectory.length/2; i++) {
globaldirectory.entry[i+count] = oldEntry[i];
globaldirectory.entry[i+1+count] = oldEntry[i];
count++;
}
delete [] oldEntry;
int peerHashedKey = getPeerBucketKey(splitIndex, globaldirectory.globalDepth);
globaldirectory.entry[splitIndex] = new Bucket(bucketToSplit.localDepth+1);
globaldirectory.entry[peerHashedKey] = new Bucket(bucketToSplit.localDepth+1);
for (size_t i = 0; i < bucketToSplit.currentEntries; i++) {
int newKey = getCurrentHash(bucketToSplit.dataItem[i].key, globaldirectory.globalDepth);
cout << "newkey" << newKey << endl;
if (newKey == splitIndex) {
insertItemIntoBucket(*globaldirectory.entry[splitIndex], bucketToSplit.dataItem[i]);
} else if (newKey == peerHashedKey) {
insertItemIntoBucket(*globaldirectory.entry[peerHashedKey], bucketToSplit.dataItem[i]);
} else{
return 0;
}
}
return 1;
}
//Minimize directory if all buckets have depth less than global depth
int checkDirectoryMinimization(GlobalDirectory& globaldirectory) {
for (size_t i = 0; i < globaldirectory.length; i++) {
if (globaldirectory.entry[i]->localDepth == globaldirectory.globalDepth)
return -1;
}
if (globaldirectory.length == 1)
return -1;
int oldGlobalDepth, oldLength;
Bucket** oldEntry;
oldGlobalDepth = globaldirectory.globalDepth;
oldLength = globaldirectory.length;
globaldirectory.globalDepth -= 1;
globaldirectory.length /= 2;
oldEntry = globaldirectory.entry;
globaldirectory.entry = new Bucket * [globaldirectory.length];
for (size_t i = 0; i < globaldirectory.length; i++) {
globaldirectory.entry[i] = oldEntry[2*i];
}
delete [] oldEntry;
return 1;
}