-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskMultiMap.cpp
More file actions
executable file
·331 lines (282 loc) · 7.9 KB
/
DiskMultiMap.cpp
File metadata and controls
executable file
·331 lines (282 loc) · 7.9 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
#include "DiskMultiMap.h"
#include "BinaryFile.h"
#include <iostream>
#include <cstring>
#include <functional>
using namespace std;
DiskMultiMap::DiskMultiMap() // DONE
{
sizenode = sizeof(DiskNode); // initialize int variables for size of node, bucket, and offset
sizebucket = sizeof(Bucket);
sizeoffset = sizeof(BinaryFile::Offset);
head = 0;
r_head = 0;
}
DiskMultiMap::~DiskMultiMap() // DONE
{
close();
}
bool DiskMultiMap::createNew(const string& filename, unsigned int numBuckets) // DONE
{
if (bf.isOpen())
bf.close();
bool success = bf.createNew(filename);
if (!success)
return false;
head = numBuckets; // head holds number of buckets
r_head = sizeoffset; // r_head is head of "removed" linked list, initially points to itself
bf.write(head, 0);
bf.write(r_head, sizeoffset);
for (int i = 0; i < head; i++)
{
Bucket b(0); // create specified number of buckets
bf.write(b, (2 * sizeoffset) + (i * sizebucket));
}
return true;
}
bool DiskMultiMap::openExisting(const string& filename) // DONE
{
if (bf.isOpen())
bf.close();
bool success = bf.openExisting(filename);
if (!success)
return false;
bf.read(head, 0);
bf.read(r_head, sizeoffset);
return true;
}
void DiskMultiMap::close() // DONE
{
if (bf.isOpen())
bf.close();
}
bool DiskMultiMap::insert(const string& key, const string& value, const string& context) // DONE
{
const char* ckey = key.c_str(); // cnnvert to cstrings
const char* cval = value.c_str();
const char* ccont = context.c_str();
if (strlen(ckey) > 120 || strlen(cval) > 120 || strlen(ccont) > 120) // check for valid size
return false;
DiskNode n; // create node and copy passed in strings
strcpy(n.key, ckey);
strcpy(n.value, cval);
strcpy(n.context, ccont);
hash<string> str_hash; // hash function assigns int value to a string
unsigned int hashValue = str_hash(key);
unsigned int buck = hashValue%head;
int buckaddr = (2 * sizeoffset) + (sizebucket * buck); // find address of specified bucket
Bucket b(0);
bf.read(b, buckaddr);
if (r_head == sizeoffset) // if there are no reusable nodes
{
BinaryFile::Offset length = bf.fileLength(); // measure length of file
if (b.nodeaddr == 0) // if bucket is empty
{
b.nodeaddr = length;
n.next = buckaddr;
}
else
{ // if bucket already contains node(s)
n.next = b.nodeaddr;
b.nodeaddr = length;
}
bf.write(n, length);
bf.write(b, buckaddr);
}
else
{ // if there are reusable nodes
DiskNode test;
bf.read(test, r_head);
if (b.nodeaddr == 0) // if bucket is empty
{
b.nodeaddr = r_head;
r_head = test.next;
n.next = buckaddr;
}
else
{ // if bucket already contains node(s)
BinaryFile::Offset temp = test.next;
n.next = b.nodeaddr;
b.nodeaddr = r_head;
r_head = temp;
}
bf.write(n, b.nodeaddr);
bf.write(b, buckaddr);
bf.write(r_head, sizeoffset);
}
return true;
}
DiskMultiMap::Iterator DiskMultiMap::search(const std::string& key) // DONE
{
hash<string> str_hash; // hash function assigns int to string
unsigned int hashValue = str_hash(key);
unsigned int buck = hashValue%head;
int buckaddr = (2 * sizeoffset) + (sizebucket * buck); // find address of specified bucket
Bucket b(0);
bf.read(b, buckaddr);
if (b.nodeaddr == 0) // if bucket is empty
{
DiskMultiMap::Iterator it; // return invalid iterator
return it;
}
else
{
DiskNode n;
bf.read(n, b.nodeaddr);
BinaryFile::Offset curraddr = b.nodeaddr;
bool flag = false;
int setoff = 0;
while (flag == false) // search for desired key at specified bucket
{
if (key == n.key) // for first matching key that is found
{
DiskMultiMap::Iterator it(key, curraddr, n.next, buckaddr, &bf);
return it; // return iterator pointing to that node
}
if (setoff == 0)
{
curraddr = n.next; // move to next node
bf.read(n, curraddr);
}
if (setoff != 0) // condition to exit loop
flag = true;
if (n.next == buckaddr) // if reached end of linked list
setoff++;
}
DiskMultiMap::Iterator it; // return invalid iterator if key not found
return it;
}
}
DiskMultiMap::Iterator::Iterator() // DONE
{
valid = false; // invalid iterator constructor
}
DiskMultiMap::Iterator::Iterator(const string& key, BinaryFile::Offset c, BinaryFile::Offset n, BinaryFile::Offset a, BinaryFile* bfptr) // DONE
: cur(c), next(n), buckaddress(a), ptr(bfptr)
{
valid = true; // valid iterator constructor
strcpy(hold, key.c_str());
}
bool DiskMultiMap::Iterator::isValid() const // DONE
{
return valid;
}
DiskMultiMap::Iterator& DiskMultiMap::Iterator::operator++() // DONE
{
if (!isValid()) // return itself if invalid
return *this;
DiskNode n;
ptr->read(n, cur);
while (1)
{
cur = n.next;
if (n.next == buckaddress)
{
valid = false; // if at end of linked list
return *this;
}
ptr->read(n, cur);
next = n.next; // increment iterator to point to next node in list
if (strcmp(n.key, hold) == 0)
return *this;
}
}
MultiMapTuple DiskMultiMap::Iterator::operator*() // DONE
{
if (!isValid())
{
MultiMapTuple empty; // return empty multimaptuple if invalid
empty.key = "";
empty.value = "";
empty.context = "";
return empty;
}
DiskNode n;
ptr->read(n, cur);
MultiMapTuple m; // retrun multimaptuple with data at that node
m.key = n.key;
m.value = n.value;
m.context = n.context;
return m;
}
int DiskMultiMap::erase(const string& key, const string& value, const string& context) // DONE
{
const char* akey = key.c_str();
const char* aval = value.c_str();
const char* acont = context.c_str(); // convert to cstrings
int removed = 0;
hash<string> str_hash;
unsigned int hashValue = str_hash(key); // same hash function as in insert
unsigned int buck = hashValue%head;
int buckaddr = (2 * sizeoffset) + (sizebucket * buck);
Bucket b(0);
bf.read(b, buckaddr);
if (b.nodeaddr == 0) // if bucket is empty, return
return 0;
DiskNode temp;
bf.read(temp, b.nodeaddr);
BinaryFile::Offset prev = 0;
BinaryFile::Offset tempaddr = temp.next;
while (tempaddr != b.nodeaddr)
{
if (strcmp(akey, temp.key) == 0 && strcmp(aval, temp.value) == 0 && strcmp(acont, temp.context) == 0)
{ // if data matches
BinaryFile::Offset rem;
if (prev == 0) // if first node in list
{
rem = b.nodeaddr; // remember node that is deleted
if (tempaddr == buckaddr)
b.nodeaddr = 0;
else
b.nodeaddr = tempaddr;
bf.write(b, buckaddr);
}
else
{ // if middle node in list
DiskNode holder;
bf.read(holder, prev);
rem = holder.next;
holder.next = tempaddr;
bf.write(holder, prev);
}
if (r_head == sizeoffset) // if "removed" list is empty
{
r_head = rem; // add deletd node to "removed" list
bf.write(r_head, sizeoffset);
DiskNode r_temp;
bf.read(r_temp, r_head);
r_temp.next = sizeoffset;
bf.write(r_temp, r_head);
}
else
{ // if "removed" already contains nodes
DiskNode r_temp;
bf.read(r_temp, rem); // add newly removed node to front of list
r_temp.next = r_head;
bf.write(r_temp, rem);
r_head = rem;
bf.write(r_head, sizeoffset);
}
removed++; // count number of nodes deleted
}
else
{ // if data does not match
if (prev == 0)
prev = b.nodeaddr;
else
{
DiskNode holder;
bf.read(holder, prev);
prev = holder.next;
}
}
if (tempaddr == buckaddr) // if at end of list
tempaddr = b.nodeaddr;
else
{
bf.read(temp, tempaddr); // otherwise check next node and go to top of loop
tempaddr = temp.next;
}
}
return removed; // return number of nodes removed
}