-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
170 lines (138 loc) · 3.93 KB
/
main.cpp
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
#include <iostream>
#include <vector>
#include <filesystem>
#include <queue>
#include <omp.h>
#include "sha256.h"
#include "hashtree.h"
#define NUM_THREADS 8
namespace fs = std::__fs::filesystem;
using namespace std;
using namespace fs;
const string backupPath = "/Users/akshay_raman/Documents/backup/";
const string basePath = "/Users/akshay_raman/Documents/";
const string dataPath = "/Users/akshay_raman/Documents/test/Fall 21-22/STS";
class query {
string pathName;
string operation;
public:
query(string operation, string pathName) {
this->operation = operation;
this->pathName = pathName;
}
bool execute() {
string source = pathName;
string destination = backupPath + source.substr(basePath.length(), source.length() - basePath.length());
fs::path fileLoc = destination;
fileLoc.remove_filename();
try {
if (operation == "add") {
fs::create_directories(fileLoc);
fs::copy(source,destination, fs::copy_options::overwrite_existing | fs::copy_options::recursive);
} else if (operation == "delete") {
fs::remove_all(destination);
} else if (operation == "modify") {
fs::remove_all(destination);
fs::create_directories(fileLoc);
fs::copy(source,destination, fs::copy_options::overwrite_existing | fs::copy_options::recursive);
} else {
cout << "Invalid query" << endl;
return false;
}
return true;
} catch (exception& e){
cout << "Unable to execute query";
return false;
}
}
void print() {
cout << operation << " " << pathName << endl;
}
};
queue<string> current; // stores current nodes in BFS
vector<query> queries;
void compareHelper(vector<string> lold, vector<string> lnew) {
int i = 0, j = 0;
while (i < lold.size() && j < lnew.size()) {
if (lold[i] < lnew[j]) {
cout << "Deleted " << lold[i] << endl;
queries.push_back(query("delete",lold[i]));
i++;
} else if (lnew[j] < lold[i]) {
cout << "Added " << lnew[j] << endl;
queries.push_back(query("add",lnew[j]));
j++;
} else {
current.push(lold[i]);
i++;
j++;
}
}
while(i < lold.size()) {
cout << "Deleted " << lold[i] << endl;
queries.push_back(query("delete",lold[i]));
i++;
}
while(j < lnew.size()) {
cout << "Added " << lnew[j] << endl;
queries.push_back(query("add",lnew[j]));
j++;
}
}
void compareTrees(HashTree *htold, HashTree *htnew) {
cout << "\nChanges: " << endl;
current.push(htold -> get_root());
while(!current.empty()) {
string node = current.front();
current.pop();
if (htold -> get_hash(node) != htnew -> get_hash(node)) {
if (htnew -> get_children(node).empty()) {
cout << "Modified " << node << endl;
queries.push_back(query("modify",node));
} else
compareHelper(htold-> get_children(node), htnew -> get_children(node));
}
}
}
int main() {
cout << "(Parallel) Data Backup Using Hash Trees" << endl;
cout << endl;
cout << "Current Data Hash Tree:" << endl;
HashTree *ht_data;
omp_set_num_threads(NUM_THREADS);
double start = omp_get_wtime();
#pragma omp parallel default(none) firstprivate(dataPath) shared(ht_data)
{
#pragma omp single
{
ht_data = new HashTree(dataPath, false); // current state
}
}
double end = omp_get_wtime();
// ht_data.print();
ht_data->print_metadata();
cout << endl;
cout << "Time Taken: " << end - start << "s" << endl;
cout << endl;
if (!fs::exists(backupPath + "hashtree.txt")) {
cout << "No backup found. Creating backup..." << endl;
queries.push_back(query("add",dataPath));
} else {
cout << "Backup Hash Tree:" << endl;
HashTree *ht_backup = new HashTree(backupPath+"hashtree.txt", true);
// ht_data.print();
ht_data->print_metadata();
compareTrees(ht_backup,ht_data); // find changes
}
cout << endl;
cout << "Executed Queries:" << endl;
#pragma omp parallel default(none) shared(queries)
#pragma omp for schedule(dynamic,1)
for (query q : queries) { //update backup
q.print();
q.execute();
}
cout << endl;
ht_data->save(backupPath + "hashtree.txt");
return 0;
}