-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauxiliary_dht_functions.h
266 lines (230 loc) · 7.32 KB
/
auxiliary_dht_functions.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
/* Global dhtrunner */
dht::DhtRunner node;
/* Prepares a file object to be put into the DHT. Converts
values to string with format id##isExists##content */
std::string parseOutgoingDHT(file data) {
std::stringstream ss;
ss << std::to_string(data.id) << "##" << std::to_string(data.isExists) << "##" << std::to_string(data.isDirectory) << "##" << data.content;
return ss.str();
}
/* Converts a (DHT) string with format id##isExists##content
to a file object */
file parseIncomingDHT(std::string data) {
file fileObject;
std::string id = data.substr(0, data.find("##"));
data.erase(0, data.find("##") + 2);
std::string isExists = data.substr(0, data.find("##"));
data.erase(0, data.find("##") + 2);
std::string isDirectory = data.substr(0, data.find("##"));
data.erase(0, data.find("##") + 2);
std::string content = data;
fileObject.id = std::stoull(id);
fileObject.isExists = (isExists == "1");
fileObject.isDirectory = (isDirectory == "1");
fileObject.content = content;
return fileObject;
}
/* finds non-deleted uuid for <path, uuid> pairs
see wiki on remove for more details */
std::string existing_uuid_key(std::string path_name) {
/* vector containing all uuid strings in path */
std::vector<std::string> uuidVec;
/* Contains all uuids that have been labeled as deleted */
std::vector<std::string> deletedUuidsVec;
/* Contains all uuids do not have deleted label, but may have been */
std::vector<std::string> existingUuidsVec;
/* existingKey should be a non-deleted uuid if one exists */
std::string existingKey = "NONE";
/* populate uuidVec */
node.get(path_name, [&uuidVec](const std::vector<std::shared_ptr<dht::Value>>& values) {
for (const auto& value : values) {
std::string uuidString = asciify((*value).data);
uuidVec.push_back(uuidString);
}
return true;
});
sleep(2);
/* sort uuids
- if marked with 'D' at the end, it has been deleted
- otherwise, it can potentially exist */
for (int i = 0; i < uuidVec.size(); i++) {
if (uuidVec[i].back() == 'D') {
deletedUuidsVec.push_back(uuidVec[i]);
}
else {
existingUuidsVec.push_back(uuidVec[i]);
}
}
for (int i = 0; i < deletedUuidsVec.size(); i++) {
deletedUuidsVec[i].pop_back();
}
/* check existing uuids against deleted uuids */
for (int i = 0; i < existingUuidsVec.size(); i++) {
//there should at most one
bool isExists = true;
for (int j = 0; j < deletedUuidsVec.size(); j++) {
if (existingUuidsVec[i] == deletedUuidsVec[j]) {
isExists = false;
}
}
if (isExists) {
existingKey = existingUuidsVec[i];
}
}
return existingKey;
}
/* checks if path_name exists
checks at <path, uuid> level */
bool file_exists(std::string path_name) {
/* vector containing all uuid strings in path */
std::vector<std::string> uuidVec;
/* Contains all uuids that have been labeled as deleted */
std::vector<std::string> deletedUuidsVec;
/* Contains all uuids do not have deleted label, but may have been */
std::vector<std::string> existingUuidsVec;
/* existingKey should be a non-deleted uuid if one exists */
std::string existingKey = "NONE";
/* populate uuidVec */
node.get(path_name, [&uuidVec](const std::vector<std::shared_ptr<dht::Value>>& values) {
for (const auto& value : values) {
std::string uuidString = asciify((*value).data);
uuidVec.push_back(uuidString);
}
return true;
});
sleep(2);
/* sort uuids
- if marked with 'D' at the end, it has been deleted
- otherwise, it can potentially exist */
for (int i = 0; i < uuidVec.size(); i++) {
if (uuidVec[i].back() == 'D') {
deletedUuidsVec.push_back(uuidVec[i]);
}
else {
existingUuidsVec.push_back(uuidVec[i]);
}
}
for (int i = 0; i < deletedUuidsVec.size(); i++) {
deletedUuidsVec[i].pop_back();
}
/* check existing uuids against deleted uuids */
for (int i = 0; i < existingUuidsVec.size(); i++) {
//there should at most one
bool isExists = true;
for (int j = 0; j < deletedUuidsVec.size(); j++) {
if (existingUuidsVec[i] == deletedUuidsVec[j]) {
isExists = false;
}
}
if (isExists) {
existingKey = existingUuidsVec[i];
}
}
if (existingKey == "NONE") {
return false;
}
return true;
}
/* checks if path_name is a directory
if it is, return true, otherwise false */
bool is_directory(std::string path_name) {
/* Check if the file exists */
if (file_exists(path_name)) {
/* string for converting name to uuid */
std::string path_uuid = existing_uuid_key(path_name);
/* vector for storing all file strings in uuid */
std::vector<std::string> fileStringsVec;
/* vector that contains the conversion of file strings to data type file */
std::vector<file> fileVec;
/* get current file based on id */
file current_file;
/* populate fileStringsVec based on uuid */
node.get(path_uuid, [&fileStringsVec](const std::vector<std::shared_ptr<dht::Value>>& values) {
for (const auto& value : values) {
std::string uuidString = asciify((*value).data);
fileStringsVec.push_back(uuidString);
}
return true;
});
sleep(1);
for (int i = 0; i < fileStringsVec.size(); i++) {
fileVec.push_back(parseIncomingDHT(fileStringsVec[i]));
}
for (int i = 0; i < fileVec.size(); i++) {
if (std::stoull(path_uuid) == fileVec[i].id) {
current_file = fileVec[i];
}
}
if (current_file.isDirectory) {
return true;
}
return false;
}
else {
return false;
}
}
/* exclusive put - checks if value already exists in key.
- If it exists, do not put
- If it does not exist, put
- verbatim parameter tell to directly put the value without parsing
(generally used for putting at a different key than self)
TODO: never need to use xcp with <path, uuid> pairs
intended use is for <uuid, <uuid, content, E, D>> pairs
*/
bool dht_exclusive_put(std::string key, std::string value, bool isDirectory, bool verbatim) {
bool keyIsUuid = true;
try {
std::stoull(key);
}
catch (std::invalid_argument) {
/* key is a path instead => <path, uuid> pair */
keyIsUuid = false;
}
if (keyIsUuid) {
std::vector<std::string> existingValuesVec;
std::vector<file> fileVec;
bool toPut = true;
/* get all existing values at key */
node.get(key, [&existingValuesVec](const std::vector<std::shared_ptr<dht::Value>>& values) {
for (const auto& value : values) {
std::string value_string = asciify((*value).data);
existingValuesVec.push_back(value_string);
}
return true;
});
sleep(1);
/* extract into file objects */
for (int i = 0; i < existingValuesVec.size(); i++) {
fileVec.push_back(parseIncomingDHT(existingValuesVec[i]));
}
/* check if content matches */
for(int i = 0; i < fileVec.size(); i++) {
if (value == fileVec[i].content) {
toPut = false;
}
}
if (toPut) {
if (verbatim) {
node.put(key, value);
sleep(1);
return true;
}
file new_file(stoull(key), value, 1, isDirectory);
node.put(key, parseOutgoingDHT(new_file));
sleep(1);
return true;
}
return false;
}
else {
// key is a path, check if uuid exists in path
// if one exists, don't put
// if does not exist, put <key, value>
if (file_exists(key)) {
return false;
}
node.put(key, value);
return true;
}
}