-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.cpp
348 lines (273 loc) · 6.88 KB
/
Table.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
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
#include "pch.h"
#include "Table.h"
Table::Table()
{
}
Table::~Table()
{
}
Element::Element() {
}
Element::Element(std::string n, std::string pass, std::string phone,
std::string dname, std::string dfather, std::string tdate,
int _bg, int _age, int _sex) {
name = n;
pswd = pass;
phoneNo = phone;
donorName = dname;
donorFather = dfather;
transferDate = tdate;
blood_group = _bg;
age = _age;
sex = _sex;
}
Field::Field(){
noOfChains = 0;
}
Element Field::getElement(int pos = 0) {
return vec[pos];
}
int primaryHashFunction(std::string s) {
int hashVal = 0, asc;
for (int i = 0; i < s.size(); i++)
{
asc = s[i] > 96 ? s[i] - 96 : s[i] - 64;
hashVal = (hashVal * 32 + asc) % MAX_ROW_SIZE;
}
return (int)abs(hashVal);
}
int secondaryHashFunction(std::string s) {
return 0;
}
json Table::readFromDisk() {
json j;
std::string block;
std::ifstream info(".\\data\\info.txt");
int rownum, count;
while (info>>rownum>>count) {
block.clear();
block = "block_";
block += std::to_string(rownum);
block += ".dat";
std::cout << block << std::endl;
std::cout << "count is :" << count << std::endl;
std::cout << "element is :" << sizeof(Element)<< std::endl;
std::ifstream fin(block.c_str(), std::ios::binary | std::ios::in);
fin.read((char*)&row[rownum].vec[0], sizeof(Element)*count);
std::cout << "num is " << row[rownum].vec[0].phoneNo << std::endl;
fin.close();
}
info.close();
j["status"] = "OK";
return j;
}
json Table::ReadProtocol(json&& j) {
std::string p = j["protocol"].dump();
json ret;
std::cout << "\nregistered with: " << j["val"]<< std::endl;
if (strcmp(p.c_str(), "99") == 0) {
registerSchema(j["val"]);
}
else if (strcmp(p.c_str(), "102") == 0) {
std::cout << "add entry checkup" << std::endl;
ret = addEntryCheckup(j["val"]);
}
else if (strcmp(p.c_str(),"100")==0) {
std::cout << "add entry" << std::endl;
ret = addEntry(j["val"]);
}
else if (strcmp(p.c_str(), "200") == 0) {
std::cout << "find all" << std::endl;
ret = findAll(j["val"]);
}
else if (strcmp(p.c_str(), "201") == 0) {
std::cout << "find one" << std::endl;
ret = findOne(j["val"]);
}
else if (strcmp(p.c_str(), "402") == 0) {
std::cout << "delete entry" << std::endl;
ret = deleteEntry(j["val"]);
}
else if (strcmp(p.c_str(), "301") == 0) {
std::cout << "find one and update" << std::endl;
ret = findOneAndUpdate(j["val"]);
}
else if (strcmp(p.c_str(), "0") == 0) {
std::cout << "commiting to disk" << std::endl;
ret = commitToDisk();
}
else if (strcmp(p.c_str(), "-1") == 0) {
//std::cout << "reading from disk" << std::endl;
ret = readFromDisk();
}
else if (strcmp(p.c_str(), "300") == 0) {
std::cout << "find all and update" << std::endl;
ret = findAllAndUpdate(j["val"]);
}
return ret;
}
void Table::registerSchema(json& j) {
}
void Table::writeToInfo(size_t rownum, size_t rec) {
std::ofstream fout(".\\data\\info.txt");
fout << rownum << " " << rec << std::endl;
fout.close();
}
json Table::commitToDisk() {
std::string blockName;
json j;
for (size_t i = 0; i < MAX_ROW_SIZE; i++) {
blockName.clear();
blockName = ".\\data\\block_";
blockName += std::to_string(i);
blockName += ".dat";
if (row[i].vec.size() > 0) {
std::ofstream fout(blockName.c_str(), std::ios::binary |std::ios::out | std::ios::app);
std::cout << "\nROW is " << i << std::endl;
writeToInfo(i, row[i].vec.size());
fout.write((char*)&row[i].vec[0], sizeof(Element)*row[i].vec.size());
fout.close();
}
}
j["status"] = "OK";
return j;
}
json Table::addEntry(json& j) {
std::cout << "adding entry" <<std::endl;
Element e;
json resp;
fillObj(j, &e);
int pos = primaryHashFunction(e.phoneNo);
row[pos].vec.push_back(e);
row[pos].noOfChains++;
resp["status"] = "OK";
return resp;
}
json Table::addEntryCheckup(json& j) {
std::cout << "\nadding entry with check\n" << std::endl;
Element e;
fillObj(j, &e);
json resp;
std::string phno = j["phoneNo"].dump();
//first check if the stuff exists
int pos = primaryHashFunction(e.phoneNo);
bool found = false;
auto scanVec = row[pos].vec;
for (int i = 0; i < scanVec.size(); i++) {
if (scanVec[i].phoneNo == phno) {
found = true;
}
}
if (found) {
std::cout << "duplicate found in check" << std::endl;
resp["status"] = "DUPLICATE_FOUND";
}
else {
std::cout << "duplicate not found in check\nadding entry" << std::endl;
row[pos].vec.push_back(e);
row[pos].noOfChains++;
resp["status"] = "OK";
}
return resp;
}
void Table::fillObj(json& j, Element *e) {
e->name = j["name"].dump();
e->pswd = j["pswd"].dump();
e->phoneNo = j["phoneNo"].dump();
e->donorFather = j["donorFather"].dump();
e->transferDate = j["transferDate"].dump();
e->donorName = j["donorName"].dump();
e->blood_group = atoi(j["bloodGroup"].dump().c_str());
e->age = atoi(j["age"].dump().c_str());
e->sex = atoi(j["sex"].dump().c_str());
}
bool Table::copyExists(std::vector<Element> vec, Element e) {
bool exists = false;
for (std::vector<Element>::iterator i = vec.begin(); i != vec.end(); i++) {
if (i->phoneNo == e.phoneNo) {
exists = true;
}
}
return exists;
}
json Table::findAll(json& j) {
std::string phno = j["phoneNo"].dump();
Element e;
json resp;
json fill;
int pos = primaryHashFunction(phno);
std::cout << "pos is " << pos << std::endl;
auto scanVec = row[pos].vec;
if (row[pos].noOfChains == 0) {
resp["status"] = "NULL";
}
else {
for (int i = 0; i < scanVec.size(); i++) {
filljson(fill , scanVec[i]);
resp[i] = fill;
}
}
return resp;
}
void Table::filljson(json &j, Element e) {
j["name"] = e.name;
j["pswd"] = e.pswd;
j["blood_group"] = e.blood_group;
j["phoneNo"] = e.phoneNo;
j["donorFather"] = e.donorFather;
j["donorName"] = e.donorName;
j["transferDate"] = e.transferDate;
j["age"] = e.age;
j["sex"] = e.sex;
}
json Table::findOne(json& j) {
std::string phno = j["phoneNo"].dump();
Element e;
json resp;
int pos = primaryHashFunction(phno);
std::cout << "filling json" << std::endl;
if (row[pos].noOfChains == 0) {
resp["status"] = "NULL";
}
else {
filljson(resp, row[pos].vec[0]);
}
return resp;
}
json Table::deleteEntry(json& j) {
std::string phno = j["phoneNo"].dump();
Element e;
json resp;
int pos = primaryHashFunction(phno);
auto scanVec = row[pos].vec;
for (int i = 0; i < scanVec.size(); i++) {
if (scanVec[i].phoneNo == phno) {
}
}
return resp;
}
json Table::findOneAndUpdate(json& j) {
std::string phno = j["phoneNo"].dump();
Element e;
json resp;
fillObj(j, &e);
int pos = primaryHashFunction(phno);
row[pos].vec[0]=e;
resp["status"] = "OK";
return resp;
}
json Table::findAllAndUpdate(json& j) {
std::string phno = j["phoneNo"].dump();
Element e;
json resp;
fillObj(j, &e);
int pos = primaryHashFunction(phno);
auto scanVec = row[pos].vec;
for (int i = 0; i < scanVec.size(); i++) {
if (scanVec[i].phoneNo == phno) {
scanVec[i] = e;
}
}
resp["status"] = "OK";
return resp;
}