-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.java
More file actions
909 lines (841 loc) · 38 KB
/
Copy pathTable.java
File metadata and controls
909 lines (841 loc) · 38 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
import java.io.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;
import com.opencsv.CSVWriter;
public class Table implements Serializable {
private String TableName = "";
private String ClusteringKeyColumn;
private int pageCount = 0;
private Vector<PageInfo> Pages = new Vector<PageInfo>();
private Vector<String> colOrder = new Vector<String>();
public Table() {
}
public Table(String strTableName,
String strClusteringKeyColumn,
Hashtable<String, String> htblColNameType) throws DBAppException {
if (htblColNameType.size() == 0) {
throw new DBAppException("Please insert the columns and their types");
}
this.TableName = strTableName;
this.ClusteringKeyColumn = strClusteringKeyColumn;
String hash = htblColNameType.toString();
String hash2 = hash.substring(1, hash.length() - 1);
String[] hash3 = hash2.split(",");
for (int i = hash3.length - 1; i >= 0; i--) {
String[] x = hash3[i].split("=");
String xtrimmed = x[0].trim();
if (colOrder.contains(xtrimmed)) {
throw new DBAppException("Can not have 2 columns with the same name");
} else {
colOrder.add(xtrimmed);
if (x[1].equalsIgnoreCase("java.lang.Integer") || x[1].equalsIgnoreCase("java.lang.Double")
|| x[1].equalsIgnoreCase("java.lang.String")) {
if (xtrimmed.equals(strClusteringKeyColumn)) {
String[] y = {strTableName, xtrimmed, x[1], "True", "null", "null"};
writeDataLineByLine("resources/metaFile.csv", y);
} else {
String[] y = {strTableName, xtrimmed, x[1], "False", "null", "null"};
writeDataLineByLine("resources/metaFile.csv", y);
}
} else {
throw new DBAppException("cannot create a column with type " + x[1]);
}
}
}
}
private static void writeDataLineByLine(String filePath, String[] hash) {
try {
FileWriter outputfile = new FileWriter(filePath, true);
CSVWriter writer = new CSVWriter(outputfile);
writer.writeNext(hash);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getTableName() {
return this.TableName;
}
public Vector<String> getColOrder() {
return this.colOrder;
}
public int getPageCount() {
return this.pageCount;
}
public Vector<PageInfo> getPages() {
return this.Pages;
}
public String getClusteringKeyColumn() {
return this.ClusteringKeyColumn;
}
private int getMax() {
int x;
try {
FileReader fr = new FileReader("resources/DBApp.config");
BufferedReader br = new BufferedReader(fr);
String z = br.readLine();
String[] y = z.split("=");
z = y[1].trim();
x = Integer.parseInt(z);
br.close();
fr.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return x;
}
public void createIndexHelper(String tableName, String columnName) {
int index = colOrder.indexOf(columnName);
BPTree tree = new BPTree(2);
tree.setFileName(tableName, columnName);
tree.createFile(tree.getFileName());
for (int i = 0; i < Pages.size(); i++) {
Page p = deserializePage(Pages.get(i).getPageName());
Vector<Tuple> tuples = p.getTuples();
for (int j = 0; j < p.tupleSize(); j++) {
Tuple tuple = tuples.get(j);
Ref reference = new Ref(p.getPageName(), j);
tree.insert((Comparable) tuple.getData().get(index), reference);
}
serializePage(p);
}
serializeTree(tree);
}
private void serializeTree(BPTree t) {
String treeName = t.getFileName();
File file = new File(treeName);
ObjectOutputStream os = null;
try {
FileOutputStream fileOS = new FileOutputStream(file);
os = new ObjectOutputStream(fileOS);
os.writeObject(t);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private BPTree deserializeTree(String treeName) {
BPTree t = new BPTree(2);
String fileName = treeName;
ObjectInputStream in = null;
try {
FileInputStream fileIn = new FileInputStream(fileName);
in = new ObjectInputStream(fileIn);
t = (BPTree) in.readObject();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
c.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return t;
}
private String binarySearch(Vector<PageInfo> pageInfos, int start, int end, Object value) {
if (end >= start) {
int mid = (end + start) / 2;
// If the element is present at the middle itself
PageInfo w = pageInfos.get(mid);
if (((Comparable) value).compareTo((Comparable) w.getMin()) >= 0
&& ((Comparable) value).compareTo((Comparable) w.getMax()) <= 0) {
return w.getPageName();
}
// If element is smaller than mid, then it can only be present in right subarray
if (((Comparable) value).compareTo((Comparable) w.getMax()) > 0)
return binarySearch(pageInfos, mid + 1, end, value);
// Else the element can only be present in left subarray
return binarySearch(pageInfos, start, mid - 1, value);
} else if (((Comparable) pageInfos.firstElement().getMin()).compareTo((Comparable) value) > 0) {
return pageInfos.firstElement().getPageName();
} else if (((Comparable) pageInfos.lastElement().getMax()).compareTo((Comparable) value) < 0) {
return pageInfos.lastElement().getPageName();
} else {
return pageInfos.get(start).getPageName();
}
}
private Tuple correctTuple(Tuple data) {
Tuple data2 = new Tuple();
for (int i = 0; i < data.getData().size(); i++) {
String type = Validators.dataType(colOrder.get(i), TableName);
String value = data.getData().get(i) + "";
if (type.equals("int")) {
int x = Integer.parseInt(value);
data2.getData().add(x);
} else if (type.equals("double")) {
double x = Double.parseDouble(value);
data2.getData().add(x);
} else {
data2.getData().add(value);
}
}
return data2;
}
public boolean addData(Tuple data2) {
Tuple data = correctTuple(data2);
int max = getMax();
// int max = 100;
String fileName = TableName + "," + ClusteringKeyColumn + ".bin";
File check = new File(fileName);
if (Pages.size() == 0) {
pageCount++;
String pageName = TableName + (pageCount);
Page p = new Page(pageName, data);
addInBTreeHelper(p, 0, data, 1);
PageInfo pi = new PageInfo(pageName, data.getData().get(colOrder.indexOf(ClusteringKeyColumn)),
data.getData().get(colOrder.indexOf(ClusteringKeyColumn)));
Pages.add(pi);
// System.out.print(p.toString());
serializePage(p);
return true;
} else if (check.exists()) {
BPTree tree = deserializeTree(fileName);
int index = colOrder.indexOf(ClusteringKeyColumn);
BPTreeLeafNode r = tree.searchGreaterthan((Comparable) data.getData().get(index));
if (tree.search((Comparable) data.getData().get(index)) != null) {
serializeTree(tree);
return false;
} else {
serializeTree(tree);
addWithIndex(data, r);
return true;
}
} else {
int index = colOrder.indexOf(ClusteringKeyColumn);
Object value = data.getData().get(index);
String type = Validators.dataType(ClusteringKeyColumn, TableName);
String name = binarySearch(Pages, 0, Pages.size() - 1, value);
Page p = deserializePage(name);
// System.out.println("page: " + name);
// System.out.println("value" + value);
Vector<Tuple> tuples = p.getTuples();
for (int j = 0; j < tuples.size(); j++) {
Tuple a = tuples.get(j);
String b = data.getData().get(index) + ""; // value to be inserted
String c = a.getData().get(index) + ""; // value in page
if (checkEqual(b, c, type)) {
serializePage(p);
return false;
}
if (checkLessThan(b, c, type)) {
if (tuples.size() < max) {
p.addData(data, j);
addInBTreeHelper(p, j, data, 2);
// System.out.print(p.toString());
for (int i = 0; i < Pages.size(); i++) {
if (name.equals(Pages.get(i).getPageName())) {
Pages.get(i).setMin(tuples.firstElement().getData().get(index));
Pages.get(i).setMax(tuples.lastElement().getData().get(index));
// System.out.println("awel if");
// System.out.println("min" + tuples.firstElement().getData().get(index));
// System.out.println("max" + tuples.lastElement().getData().get(index));
}
}
serializePage(p);
return true;
} else if (tuples.size() == max) {
Tuple shift = tuples.lastElement();
p.addData(data, j);
addInBTreeHelper(p, j, data, 3);
p.removeData(shift);
// System.out.print(p.toString());
for (int i = 0; i < Pages.size(); i++) {
if (name.equals(Pages.get(i).getPageName())) {
Pages.get(i).setMin(tuples.firstElement().getData().get(index));
Pages.get(i).setMax(tuples.lastElement().getData().get(index));
// System.out.println("tany if");
// System.out.println("min" + tuples.firstElement().getData().get(index));
// System.out.println("max" + tuples.lastElement().getData().get(index));
}
}
serializePage(p);
addData(shift);
return true;
}
} else if (Pages.lastElement().getPageName().equals(name)) {
if (j == tuples.size() - 1) {
if (tuples.size() == max) {
pageCount++;
String pageName = TableName + (pageCount);
Page x = new Page(pageName, data);
PageInfo pi = new PageInfo(pageName,
data.getData().get(colOrder.indexOf(ClusteringKeyColumn)),
data.getData().get(colOrder.indexOf(ClusteringKeyColumn)));
Pages.add(pi);
addInBTreeHelper(x, 0, data, 1);
// System.out.println(p.toString());
// System.out.println(x.toString());
serializePage(x);
serializePage(p);
return true;
} else {
p.addData(data, j + 1);
addInBTreeHelper(p, j + 1, data, 1);
// System.out.print(p.toString());
Pages.lastElement().setMin(tuples.firstElement().getData().get(index));
Pages.lastElement().setMax(tuples.lastElement().getData().get(index));
// System.out.println("else");
// System.out.println("min" + tuples.firstElement().getData().get(index));
// System.out.println("max" + tuples.lastElement().getData().get(index));
serializePage(p);
return true;
}
}
}
}
}
return false;
}
private boolean checkEqual(String b, String c, String type) {
if (type.equals("int")) {
int x = Integer.parseInt(b);
int y = Integer.parseInt(c);
if (x == y) {
return true;
} else {
return false;
}
} else if (type.equals("double")) {
double x = Double.parseDouble(b);
double y = Double.parseDouble(c);
if (x == y) {
return true;
} else {
return false;
}
} else {
if (b.equals(c)) {
return true;
} else {
return false;
}
}
}
private boolean checkLessThan(String b, String c, String type) {
if (type.equals("int")) {
int x = Integer.parseInt(b);
int y = Integer.parseInt(c);
if (x < y) {
return true;
} else {
return false;
}
} else if (type.equals("double")) {
double x = Double.parseDouble(b);
double y = Double.parseDouble(c);
if (x < y) {
return true;
} else {
return false;
}
} else {
if (b.compareTo(c) < 0) {
return true;
} else {
return false;
}
}
}
public void addWithIndex(Tuple data, BPTreeLeafNode node) {
int max = getMax();
// int max = 100;
if (node == null) {
Page p = deserializePage(Pages.lastElement().getPageName());
int index = colOrder.indexOf(ClusteringKeyColumn);
Vector<Tuple> tuples = p.getTuples();
if (tuples.size() == max) {
pageCount++;
String pageName = TableName + (pageCount);
Page x = new Page(pageName, data);
PageInfo pi = new PageInfo(pageName, data.getData().get(index), data.getData().get(index));
Pages.add(pi);
addInBTreeHelper(x, 0, data, 1);
// System.out.println(p.toString());
// System.out.println(x.toString());
serializePage(x);
serializePage(p);
return;
} else {
p.addData(data, tuples.size());
addInBTreeHelper(p, tuples.size() - 1, data, 1);
// System.out.print(p.toString());
Pages.lastElement().setMin(tuples.firstElement().getData().get(index));
Pages.lastElement().setMax(tuples.lastElement().getData().get(index));
serializePage(p);
return;
}
} else {
// System.out.println(node);
int colIndex = colOrder.indexOf(ClusteringKeyColumn);
Ref yy = null;
// System.out.println(node.getRecords().length);
for (int i = node.getRecords().length - 1; i >= 0; i--) {
if (node.getKey(i) != null) {
if (((Comparable) node.getKey(i)).compareTo(data.getData().get(colIndex)) > 0) {
yy = node.getRecords()[i];
}
}
}
Ref r = yy;
Page p = deserializePage(r.getFileName());
int index = r.getIndexInPage();
// System.out.println("index: " + index);
Vector<Tuple> tuples = p.getTuples();
if (tuples.size() < max) {
p.addData(data, index);
addInBTreeHelper(p, index, data, 2);
// System.out.print(p.toString());
for (int i = 0; i < Pages.size(); i++) {
if (r.getFileName().equals(Pages.get(i).getPageName())) {
Pages.get(i).setMin(tuples.firstElement().getData().get(colIndex));
Pages.get(i).setMax(tuples.lastElement().getData().get(colIndex));
}
}
serializePage(p);
return;
} else if (tuples.size() == max) {
Tuple shift = tuples.lastElement();
p.addData(data, index);
addInBTreeHelper(p, index, data, 3);
p.removeData(shift);
// System.out.print(p.toString());
for (int i = 0; i < Pages.size(); i++) {
if (r.getFileName().equals(Pages.get(i).getPageName())) {
Pages.get(i).setMin(tuples.firstElement().getData().get(colIndex));
Pages.get(i).setMax(tuples.lastElement().getData().get(colIndex));
}
}
serializePage(p);
addData(shift);
return;
} else if (Pages.lastElement().getPageName().equalsIgnoreCase(p.getPageName())) {
if (index == tuples.size() - 1) {
if (tuples.size() == max) {
pageCount++;
String pageName = TableName + (pageCount);
Page x = new Page(pageName, data);
PageInfo pi = new PageInfo(pageName, data.getData().get(colOrder.indexOf(ClusteringKeyColumn)),
data.getData().get(colOrder.indexOf(ClusteringKeyColumn)));
Pages.add(pi);
addInBTreeHelper(x, 0, data, 1);
// System.out.println(p.toString());
// System.out.println(x.toString());
serializePage(x);
serializePage(p);
return;
} else {
p.addData(data, index + 1);
addInBTreeHelper(p, index + 1, data, 1);
// System.out.print(p.toString());
Pages.lastElement().setMin(tuples.firstElement().getData().get(colIndex));
Pages.lastElement().setMax(tuples.lastElement().getData().get(colIndex));
serializePage(p);
return;
}
}
}
}
}
private void addInBTreeHelper(Page p, int startIndex, Tuple data, int flag) {
Vector<Tuple> tuples = p.getTuples();
for (int i = 0; i < colOrder.size(); i++) {
String fileName = TableName + "," + colOrder.get(i) + ".bin";
File check = new File(fileName);
if (check.exists()) {
BPTree tree = deserializeTree(fileName);
if (flag == 1) {
Ref reference = new Ref(p.getPageName(), startIndex);
tree.insert((Comparable) data.getData().get(i), reference);
serializeTree(tree);
} else {
if ((flag == 3) || flag == 2) {
Ref refOld = new Ref(p.getPageName(), tuples.indexOf(tuples.lastElement()) - 1);
tree.delete((Comparable) tuples.lastElement().getData().get(i), refOld);
}
for (int j = startIndex; j < p.getTuples().size() - 1; j++) {
if (!(flag == 3 && j == p.getTuples().size() - 2)) {
Ref refOld = new Ref(p.getPageName(), j);
Ref refNew = new Ref(p.getPageName(), j + 1);
tree.update((Comparable) tuples.get(j + 1).getData().get(i),
(Comparable) tuples.get(j + 1).getData().get(i), refNew, refOld);
}
}
Ref reference = new Ref(p.getPageName(), startIndex);
tree.insert((Comparable) data.getData().get(i), reference);
serializeTree(tree);
}
}
}
}
public void deleteData(Tuple data) throws DBAppException {
// get the bptree
int index = colOrder.indexOf(ClusteringKeyColumn);
String fileName = TableName + "," + ClusteringKeyColumn + ".bin";
File check = new File(fileName);
if (check.exists()) {
BPTree tree = deserializeTree(fileName);
Ref r = tree.search((Comparable) data.getData().get(index));
serializeTree(tree);
if (r != null) {
Page p = deserializePage(r.getFileName());
boolean deleted = p.removeDataIndex(r.getIndexInPage());
if (deleted) {
Vector<Tuple> tuples = p.getTuples();
// serialise the page after deleting from it
//serializePage(p);
// delete the reference of that page from the tree
//tree.delete((Comparable) data.getData().get(index), r);
//deleting the data from all trees if they exist
for (int i = 0; i < colOrder.size(); i++) {
String treeCheck = colOrder.get(i);
String fName = TableName + "," + treeCheck + ".bin";
File tCheck = new File(fName);
if (tCheck.exists()) {
BPTree tt = deserializeTree(fName);
tt.delete((Comparable) data.getData().get(i), r);
for (int j = r.getIndexInPage(); j < p.getTuples().size(); j++) {
Ref refOld = new Ref(r.getFileName(), j + 1);
Ref refNew = new Ref(p.getPageName(), j);
tt.update((Comparable) tuples.get(j).getData().get(i),
(Comparable) tuples.get(j).getData().get(i), refNew, refOld);
}
serializeTree(tt);
}
}
// loop over all the pages
for (int i = 0; i < Pages.size(); i++) {
// check if the page is the one we just deleted from
if (r.getFileName().equals(Pages.get(i).getPageName())) {
// load the page info
PageInfo pi = Pages.get(i);
// check if the page is empty
if (p.tupleSize() == 0) {
// remove the page from the pages list
Pages.remove(i);
// delete the page from the disk
serializePage(p);
File f = new File(r.getFileName() + ".bin");
f.delete();
// break the loop because we deleted the page
break;
}
// deserialize the page again to update the min and max
//Page page = deserializePage(r.getFileName());
// set the min and max of the page info
pi.setMin(p.getTuples().get(0).getData().get(index));
pi.setMax(p.getTuples().get(p.tupleSize() - 1).getData().get(index));
// finally, serialize the page
serializePage(p);
}
}
// serialise the tree after deleting from it
//serializeTree(tree);
}
}
} else {
// if tree not found then search linearly
Object value = data.getData().get(index);
String name = binarySearch(Pages, 0, Pages.size() - 1, value);
Page p = deserializePage(name);
Vector<Tuple> tuples = p.getTuples();
for (int i = 0; i < tuples.size(); i++) {
value = value + "";
String value2 = tuples.get(i).getData().get(index) + "";
if (value2.equals(value)) {
boolean deleted = p.removeDataIndex(i);
if (deleted) {
// serialise the page after deleting from it
//serializePage(p);
for (int k = 0; k < colOrder.size(); k++) {
String treeCheck = colOrder.get(k);
String fName = TableName + "," + treeCheck + ".bin";
File tCheck = new File(fName);
if (tCheck.exists()) {
BPTree tt = deserializeTree(fName);
Ref r = new Ref(p.getPageName(), i);
tt.delete((Comparable) data.getData().get(k), r);
for (int j = r.getIndexInPage(); j < p.getTuples().size(); j++) {
Ref refOld = new Ref(r.getFileName(), j + 1);
Ref refNew = new Ref(p.getPageName(), j);
tt.update((Comparable) tuples.get(j).getData().get(k),
(Comparable) tuples.get(j).getData().get(k), refNew, refOld);
}
serializeTree(tt);
}
}
// loop over all the pages
for (int j = 0; j < Pages.size(); j++) {
// check if the page is the one we just deleted from
if (p.getPageName().equals(Pages.get(j).getPageName())) {
// load the page info
PageInfo pi = Pages.get(j);
// check if the page is empty
if (p.tupleSize() == 0) {
// remove the page from the pages list
Pages.remove(j);
// delete the page from the disk
serializePage(p);
File f = new File(p.getPageName() + ".bin");
f.delete();
// break the loop because we deleted the page
break;
}
// deserialize the page again to update the min and max
//Page page = deserializePage(p.getPageName());
// set the min and max of the page info
pi.setMin(p.getTuples().get(0).getData().get(index));
pi.setMax(p.getTuples().get(p.tupleSize() - 1).getData().get(index));
// finally, serialize the page
serializePage(p);
}
}
}
}
}
}
}
public Tuple binarySearchTuples(Vector<Tuple> tuples, Object value) {
int start = 0;
int end = tuples.size() - 1;
while (start <= end) {
int mid = (start + end) / 2;
Tuple midTuple = tuples.get(mid);
Object midValue = midTuple.getData().get(colOrder.indexOf(ClusteringKeyColumn));
if (midValue.equals(value)) {
return midTuple;
} else if (((Comparable) midValue).compareTo((Comparable) value) < 0) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return null;
}
public Tuple getTuple(Object clusteringKeyValue) {
Tuple resultTuple = new Tuple();
String fileName = TableName + "," + ClusteringKeyColumn + ".bin";
File check = new File(fileName);
if (check.exists()) {
BPTree tree = deserializeTree(fileName);
Ref r = tree.search((Comparable) clusteringKeyValue);
Page p = deserializePage(r.getFileName());
resultTuple = p.getTuples().get(r.getIndexInPage());
serializePage(p);
serializeTree(tree);
} else {
// Search for the tuple using binary search
// Get the page where the tuple is located
String columnType = Validators.dataType(ClusteringKeyColumn, TableName);
// binary search based on the clustering key type
String pageName = "";
switch (columnType) {
case "int":
pageName = binarySearch(Pages, 0, Pages.size() - 1,
Integer.parseInt(clusteringKeyValue + ""));
break;
case "double":
pageName = binarySearch(Pages, 0, Pages.size() - 1,
Double.parseDouble(clusteringKeyValue + ""));
break;
case "string":
pageName = binarySearch(Pages, 0, Pages.size() - 1, clusteringKeyValue + "");
break;
}
Page p = deserializePage(pageName);
// Get the tuples in the page (O(1)) operation
Vector<Tuple> tuples = p.getTuples();
serializePage(p);
// binary search within the tuples to find the tuple with the clustering key value
switch (columnType) {
case "int":
resultTuple = binarySearchTuples(tuples, Integer.parseInt(clusteringKeyValue + ""));
break;
case "double":
resultTuple = binarySearchTuples(tuples, Double.parseDouble(clusteringKeyValue + ""));
break;
case "string":
resultTuple = binarySearchTuples(tuples, clusteringKeyValue);
break;
}
}
return resultTuple;
}
public void serializePage(Page p) {
String pageName = p.getPageName() + ".bin";
File file = new File(pageName);
ObjectOutputStream os = null;
try {
FileOutputStream fileOS = new FileOutputStream(file);
os = new ObjectOutputStream(fileOS);
// p.addData(data);
os.writeObject(p);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Page deserializePage(String pageName) {
Page p = new Page();
String fileName = pageName + ".bin";
ObjectInputStream in = null;
try {
FileInputStream fileIn = new FileInputStream(fileName);
in = new ObjectInputStream(fileIn);
p = (Page) in.readObject();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
c.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return p;
}
public Vector<Tuple> getAllData() {
// Get all data from all pages. This is done linearly.
Vector<Tuple> data = new Vector<Tuple>();
for (PageInfo page : Pages) {
Page p = deserializePage(page.getPageName());
data.addAll(p.getTuples());
serializePage(p);
}
return data;
}
public Vector<Tuple> getSelectDataIndex(String columnName, String operator, Object value) throws DBAppException {
// Get only the relevant data using the binary plus tree index.
Vector<Tuple> data = new Vector<Tuple>();
BPTree tree = deserializeTree(TableName + "," + columnName + ".bin");
//value = value + "";
switch (operator) {
case "=":
Ref b = tree.search((Comparable) value);
if (b != null) {
ArrayList<Ref> dups = tree.searchDuplicates((Comparable) value);
for (int i = 0; i < dups.size(); i++) {
Ref a = dups.get(i);
if (a != null) {
Page p = deserializePage(a.getFileName());
data.add(p.getTuples().get(a.getIndexInPage()));
serializePage(p);
}
}
serializeTree(tree);
}
break;
case ">":
BPTreeLeafNode nn = tree.searchGreaterthan((Comparable) value);
Object max = value;
if (nn != null) {
Object temp = value;
for (int i = nn.getRecords().length - 1; i >= 0; i--) {
if (nn.getKey(i) != null) {
if (((Comparable) nn.getKey(i)).compareTo(temp) > 0) {
max = nn.getKey(i);
}
}
}
} else {
serializeTree(tree);
break;
}
while (nn != null) {
ArrayList<Ref> dups = tree.searchDuplicates((Comparable) max);
for (int i = 0; i < dups.size(); i++) {
Ref rr = dups.get(i);
if (rr != null) {
Page p = deserializePage(rr.getFileName());
data.add(p.getTuples().get(rr.getIndexInPage()));
serializePage(p);
}
}
nn = tree.searchGreaterthan((Comparable) max);
if (nn != null) {
Object temp = max;
for (int i = nn.getRecords().length - 1; i >= 0; i--) {
if (nn.getKey(i) != null) {
if (((Comparable) nn.getKey(i)).compareTo(temp) > 0) {
max = nn.getKey(i);
}
}
}
} else {
serializeTree(tree);
break;
}
}
serializeTree(tree);
break;
case "<":
BPTreeLeafNode n = tree.searchMinNode();
if (n != null) {
Object min = n.getFirstKey();
while (((Comparable) min).compareTo(value) < 0) {
ArrayList<Ref> dups = tree.searchDuplicates((Comparable) min);
for (int i = 0; i < dups.size(); i++) {
Ref rr = dups.get(i);
if (rr != null) {
Page p = deserializePage(rr.getFileName());
data.add(p.getTuples().get(rr.getIndexInPage()));
serializePage(p);
}
}
n = tree.searchGreaterthan((Comparable) min);
if (n != null) {
Object temp = min;
for (int i = n.getRecords().length - 1; i >= 0; i--) {
if (n.getKey(i) != null) {
if (((Comparable) n.getKey(i)).compareTo(temp) > 0) {
min = n.getKey(i);
}
}
}
} else {
serializeTree(tree);
break;
}
}
serializeTree(tree);
}
break;
case ">=":
Vector<Tuple> data1 = getSelectDataIndex(columnName, "=", value);
Vector<Tuple> data2 = getSelectDataIndex(columnName, ">", value);
data = RecordOperators.orRecords(data1, data2);
break;
case "<=":
Vector<Tuple> data11 = getSelectDataIndex(columnName, "=", value);
Vector<Tuple> data22 = getSelectDataIndex(columnName, "<", value);
data = RecordOperators.orRecords(data11, data22);
break;
}
return data;
}
}