-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSDB.cpp
361 lines (351 loc) · 9.32 KB
/
DSDB.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
349
350
351
352
353
354
355
356
357
358
359
360
361
#include"DSDB.h"
int FieldToInt(string field)
{
if (toLower(field) == "id") { return 1; }
if (toLower(field) == "year") { return 2; }
if (toLower(field) == "deaths") { return 3; }
if (toLower(field) == "death rate") { return 4; }
if (toLower(field) == "cause name") { return 5; }
if (toLower(field) == "state") { return 6; }
return -1;
}
template <typename t>
void menu2(BTree<t>& bt)
{
int opt2, typeno;
t key1, key2;
string field, type;
int oldvalueInt; float oldvalueFloat; string oldvalueString;
int newvalueInt; float newvalueFloat; string newvalueString;
do
{
system("cls");
cout << " DSDB \n";
cout << "Sub - Menu \n";
cout << "Enter 1 for Point Search\n";
cout << "Enter 2 for Range Search\n";
cout << "Enter 3 to Update Key\n";
cout << "Enter 4 to Perform point search with Field Clause\n";
cout << "Enter 5 to Perform range search with Field Clause\n";
cout << "Enter -1 to go back to previous menu.\n";
cout << "Enter 0 to exit\n";
cin >> opt2;
system("cls");
switch (opt2)
{
case 1:
cout << "POINT SEARCH\n";
cout << "Enter key to point search:\n";
cin >> key1;
cout << "Processing..." << endl;
bt.IOcount = 0;
bt.pointSearch(key1);
cout << "\nNo. of I/O operations done: " << bt.IOcount << endl;
system("pause");
break;
case 2:
cout << "RANGE SEARCH\n";
cout << "Enter key to range search\n" << "Enter starting key: \n";
cin >> key1;
cout << "Enter ending key: \n";
cin >> key2;
cout << "Processing..." << endl;
bt.IOcount = 0;
bt.RangeQuery(key1, key2);
cout << "\nNo. of I/O operations done: " << bt.IOcount << endl;
system("pause");
break;
case 3:
cout << "UPDATE VALUE\n";
cout << "Enter key: \n";
cin >> key1;
cout << "Enter field: (id,year,cause,state,deaths,death rate)\n";
cin >> field;
typeno = FieldToInt(field);
cout << "Enter field value: ";
if (typeno >= 1 && typeno <= 3)
cin >> oldvalueInt;
else if (typeno == 4)
cin >> oldvalueFloat;
else if (typeno == 5 || typeno == 6)
cin >> oldvalueString;
cout << "Enter new value(to be changed with supposedly): ";
if (typeno >= 1 && typeno <= 3)
cin >> newvalueInt;
else if (typeno == 4)
cin >> newvalueFloat;
else if (typeno == 5 || typeno == 6)
cin >> newvalueString;
cout << "\nProcessing...\n";
bt.IOcount = 0;
if (typeno >= 1 && typeno <= 3)
bt.UpdateValue(key1, field, oldvalueInt, newvalueInt);
else if (typeno == 4)
bt.UpdateValue(key1, field, oldvalueFloat, newvalueFloat);
else if (typeno == 5 || typeno == 6)
bt.UpdateValue(key1, field, oldvalueString, newvalueString);
cout << "\nNo. of I/O operations done: " << bt.IOcount << endl;
system("pause");
break;
case 4:
cout << "POINT SEARCH WITH FIELD CLAUSE\n";
cout << "Enter key to be searched: \n";
cin >> key1;
cout << "Enter field: (id,year,cause,state,deaths,death rate)\n";
cin >> field;
typeno = FieldToInt(field);
cout << "Enter field value: ";
if (typeno >= 1 && typeno <= 3)
cin >> oldvalueInt;
else if (typeno == 4)
cin >> oldvalueFloat;
else if (typeno == 5 || typeno == 6)
cin >> oldvalueString;
cout << "\nProcessing...\n";
bt.IOcount = 0;
if (typeno >= 1 && typeno <= 3)
bt.pointSearch(key1, field, oldvalueInt);
else if (typeno == 4)
bt.pointSearch(key1, field, oldvalueFloat);
else if (typeno == 5 || typeno == 6)
bt.pointSearch(key1, field, oldvalueString);
cout << "\nNo. of I/O operations done: " << bt.IOcount << endl;
system("pause");
break;
case 5:
cout << "RANGE SEARCH WITH FIELD CLAUSE\n";
cout << "Enter key from: \n";
cin >> key1;
cout << "Enter key to: \n";
cin >> key2;
cout << "Enter field: (id,year,cause,state,deaths,death rate)\n";
cin >> field;
typeno = FieldToInt(field);
cout << "Enter Field value: ";
if (typeno >= 1 && typeno <= 3)
cin >> oldvalueInt;
else if (typeno == 4)
cin >> oldvalueFloat;
else if (typeno == 5 || typeno == 6)
cin >> oldvalueString;
cout << "\nProcessing...\n";
bt.IOcount = 0;
if (typeno >= 1 && typeno <= 3)
bt.RangeQuery(key1, key2, field, oldvalueInt);
else if (typeno == 4)
bt.RangeQuery(key1, key2, field, oldvalueFloat);
else if (typeno == 5 || typeno == 6)
bt.RangeQuery(key1, key2, field, oldvalueString);
cout << "\nNo. of I/O operations done: " << bt.IOcount << endl;
system("pause");
break;
case -1: return; break;
case 0: cout << "Thank You\n"; exit(0); break;
default: cout << "\nInvalid Option\n"; break;
}
} while (opt2 != 0);
}
void menu1()
{
int opt1, opt, x, fieldno;
int c = 0;
const string Filename = "DeathRate_";
BTree<int> BIn1(0);
BTree<int> BIn2(0);
BTree<int> BIn3(0);
BTree<float> BFl(0);
BTree<string> BStr1(0);
BTree<string> BStr2(0);
string field, datafield;
do
{
system("cls");
cout << " WELCOME TO DSDB \n";
cout << "Enter 1 to Create Index \n";
cout << "Enter 2 to Operate on existing Index\n";
cout << "Enter 0 to Exit program.\n";
cin >> opt1;
//system("cls");
switch (opt1)
{
case 1:
cout << "Enter '1' for BTree or '2' for B+Tree.\n";
cin >> opt;
if (opt == 2)
{
cout << "sorry,B+ Tree coding is not done\n";
break;
}
cout << "Enter order of tree: ";
cin >> x;
cout << "choose datafield to index on\t{id,year,deaths,death rate,cause name,state}\n";
cin >> field;
fieldno = FieldToInt(field);
cout << "\nProcessing...\n";
if (fieldno == 1) {
BIn1.t = x;
readI<int>(Filename, fieldno, BIn1);
BIn1.CreateIndex();
menu2<int>(BIn1);
}
else if (fieldno == 2)
{
BIn2.t = x;
readI<int>(Filename, fieldno, BIn2);
BIn2.CreateIndex();
menu2<int>(BIn2);
}
else if (fieldno == 3)
{
BIn3.t = x;
readI<int>(Filename, fieldno, BIn3);
BIn3.CreateIndex();
menu2<int>(BIn3);
}
else if (fieldno == 4)
{
BFl.t = x;
readI<float>(Filename, fieldno, BFl);
BFl.CreateIndex();
menu2<float>(BFl);
}
else if (fieldno == 5)
{
BStr1.t = x;
readS(Filename, 1, BStr1);
BStr1.CreateIndex();
menu2<string>(BStr1);
}
else if (fieldno == 6)
{
BStr2.t = x;
readS(Filename, 2, BStr2);
BStr2.CreateIndex();
menu2<string>(BStr2);
}
break;
case 2:
//if(BIn1.directory == "")
cout << "Enter 1 for B and 2 for B+ Tree.\n";
cin >> opt;
if (opt == 2)
break;
cout << "Enter datafield: ";
cin >> datafield;
fieldno = FieldToInt(datafield);
if (fieldno == 1) {
if (BIn1.directory == "") {
cout << "Indexing not done already, Press 1 to Index\n";
cin >> c;
if (c == 1) {
cout << "Enter order of tree: ";
cin >> x;
BIn1.t = x;
cout << "\nProcessing...\n";
readI<int>(Filename, fieldno, BIn1);
BIn1.CreateIndex();
}
else
break;
}
menu2<int>(BIn1);
}
else if (fieldno == 2) {
if (BIn2.directory == "") {
cout << "Indexing not done already, Press 1 to Index\n";
cin >> c;
if (c == 1) {
cout << "Enter order of tree: ";
cin >> x;
BIn2.t = x;
cout << "\nProcessing...\n";
readI<int>(Filename, fieldno, BIn2);
BIn2.CreateIndex();
}
else
break;
}
menu2<int>(BIn2);
}
else if (fieldno == 3) {
if (BIn3.directory == "") {
cout << "Indexing not done already, Press 1 to Index\n";
cin >> c;
if (c == 1) {
cout << "Enter order of tree: ";
cin >> x;
BIn3.t = x;
cout << "\nProcessing...\n";
readI<int>(Filename, fieldno, BIn3);
BIn3.CreateIndex();
}
else
break;
}
menu2<int>(BIn3);
}
else if (fieldno == 4) {
if (BFl.directory == "") {
cout << "Indexing not done already, Press 1 to Index\n";
cin >> c;
if (c == 1) {
cout << "Enter order of tree: ";
cin >> x;
BFl.t = x;
cout << "\nProcessing...\n";
readI<float>(Filename, fieldno, BFl);
BFl.CreateIndex();
}
else
break;
}
menu2<float>(BFl);
}
else if (fieldno == 5) {
if (BStr1.directory == "") {
cout << "Indexing not done already, Press 1 to Index\n";
cin >> c;
if (c == 1) {
cout << "Enter order of tree: ";
cin >> x;
BStr1.t = x;
cout << "\nProcessing...\n";
readS(Filename, fieldno, BStr1);
BStr1.CreateIndex();
}
else
break;
}
menu2<string>(BStr1);
}
else if (fieldno == 6) {
if (BStr2.directory == "") {
cout << "Indexing not done already, Press 1 to Index\n";
cin >> c;
if (c == 1) {
cout << "Enter order of tree: ";
cin >> x;
BStr2.t = x;
cout << "\nProcessing...\n";
readS(Filename, fieldno, BStr2);
BStr2.CreateIndex();
}
else
break;
}
menu2<string>(BStr2);
}
break;
case 0:
cout << "Thank You\n"; exit(0);
break;
default: cout << "\nInvalid Option\n"; break;
}
} while (opt1 != 0);
}
int main()
{
menu1();
system("pause");
return 0;
}