-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPTree.java
More file actions
384 lines (330 loc) · 9.91 KB
/
Copy pathBPTree.java
File metadata and controls
384 lines (330 loc) · 9.91 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
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;
public class BPTree<T extends Comparable<T>> implements Serializable {
private static final long serialVersionUID = 1L;
private int order;
private BPTreeNode<T> root;
private String filename;
private ArrayList<BPTreeNode<T>> nodes = new ArrayList<BPTreeNode<T>>();
/**
* Creates an empty B+ tree
*
* @param order the maximum number of keys in the nodes of the tree
*/
public BPTree(int order) {
this.order = order;
root = new BPTreeLeafNode<T>(this.order);
nodes.add(root);
root.setRoot(true);
// nodes=this.getNodes();
// this.createFile(filename);
}
public void setFileName(String tableName, String columnName) {
filename = tableName + "," + columnName + ".bin";
}
public String getFileName() {
return filename;
}
public ArrayList getNodes() {
ArrayList output = new ArrayList();
for (int i = 0; i < nodes.size(); i++) {
if (nodes.get(i).isRoot()) {
} else {
output.add(nodes.get(i));
}
}
return output;
}
public void createFile(String fileName) {
try {
FileOutputStream fileOut = new FileOutputStream(filename, false);
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
/**
* Inserts the specified key associated with the given record in the B+ tree
*
* @param key the key to be inserted
* @param recordReference the reference of the record associated with the key
*/
public void insert(T key, Ref recordReference) {
BPTreeLeafNode leaf = this.searchNode(key);
if (leaf != null) {
leaf.insertOverflow(key, recordReference);
} else {
PushUp<T> pushUp = root.insert(key, recordReference, null, -1);
if (pushUp != null) {
BPTreeInnerNode<T> newRoot = new BPTreeInnerNode<T>(order);
nodes.add(newRoot);
newRoot.insertLeftAt(0, pushUp.key, root);
newRoot.setChild(1, pushUp.newNode);
root.setRoot(false);
root = newRoot;
root.setRoot(true);
}
}
}
/**
* updates a value in the tree
*/
public void update(T Oldkey, T newKey, Ref refNew, Ref refOld) {
delete(Oldkey, refOld);
insert(newKey, refNew);
}
/*
* updates the references of shifted keys
*/
public void updateRefNonKey(ArrayList<Ref> oldRefs, ArrayList<Ref> newRefs) {
BPTreeLeafNode min = this.searchMinNode();
while (min != null) {
for (int j = 0; j < min.numberOfKeys; j++) {
for (int i = 0; i < oldRefs.size(); i++) {
// for each leaf we will check if it is equal to one of the old ref,update if it
// does and remove
// old,new corresponding ref
Ref curRef = oldRefs.get(i);
Ref newRef = newRefs.get(i);
if (min.getRecord(j).isEqual(curRef)) {
min.setRecord(j, newRef);
oldRefs.remove(i);
newRefs.remove(i);
break;
} else if (min.getOverflow(j) != null && min.overflow.size() > 0) {
for (int k = 0; k < min.getOverflow(j).size(); k++) {
if (((Ref) min.getOverflow(j).get(k)).isEqual(curRef)) {
min.getOverflow(j).remove(k);
min.getOverflow(j).add(k, newRef);
oldRefs.remove(i);
newRefs.remove(i);
break;
}
}
}
}
}
min = min.getNext();
}
}
public void updateRef(ArrayList<Ref> refs, int index, BPTreeLeafNode leaf) {
System.err.println(refs);
System.err.println("Inex is" + 1);
// leaf.setRecord(index, refs.get(0));
int j = 0;
for (int i = index; i < leaf.numberOfKeys && j < refs.size(); i++) {
leaf.setRecord(i, refs.get(j));
j++;
if (leaf.getOverflow(i) != null && leaf.getOverflow(i).size() > 0) {
System.err.println("I enter when duplicates of" + leaf.getKey(i));
int size = leaf.getOverflow(i).size();
leaf.initializeVectors(i);
for (int m = 0; m < size && j < refs.size(); m++) {
leaf.getOverflow(i).add(m, refs.get(j));
j++;
}
}
}
leaf = leaf.getNext();
while (leaf != null && j < refs.size()) {
for (int i = 0; i < leaf.numberOfKeys && j < refs.size(); i++) {
leaf.setRecord(i, refs.get(j));
j++;
if (leaf.getOverflow(i) != null && leaf.getOverflow(i).size() > 0) {
System.err.println("I enter when duplicates of" + leaf.getKey(i));
int size = leaf.getOverflow(i).size();
leaf.initializeVectors(i);
for (int m = 0; m < size && j < refs.size(); m++) {
leaf.getOverflow(i).add(m, refs.get(j));
j++;
}
}
}
leaf = leaf.getNext();
}
}
/**
* Looks up for the record that is associated with th e specified key
*
* @param key the key to find its record
* @return the reference of the record associated with this key
*/
public Ref search(T key) {
return root.search(key);
}
public ArrayList<Ref> searchDuplicates(T Key) {
return root.searchFromNodesDuplicates(Key);
}
public BPTreeLeafNode searchNode(T key) {
return root.searchForNode2(key);
}
public BPTreeLeafNode searchNodeRef(T key, Ref ref) {
return root.searchForNodeRef(key, ref, this);
}
public BPTreeLeafNode searchMinNode() {
return root.searchForMin();
}
public BPTreeLeafNode searchGreaterthan(T key) {
return root.searchGreaterthan(key);
}
public BPTreeLeafNode searchGreaterEqual(T key) {
return root.searchGreatereEqual(key);
}
public BPTreeLeafNode searchGreaterEqualPoly(T key) {
return root.searchGreatereEqualPoly(key);
}
public BPTreeLeafNode getLastLeaf() {
BPTreeLeafNode curNode = this.searchMinNode();
while (curNode.getNext() != null) {
curNode = curNode.getNext();
}
return curNode;
}
/**
* Delete a key and its associated record from the tree.
*
* @param key the key to be deleted
* @return a boolean to indicate whether the key is successfully deleted or it
* was not in the tree
*/
public Ref insertRef(T key, int maxRows, String tableName, boolean empty) {
if (empty) {
return new Ref(tableName + "0" + ".class", 0);
} else {
BPTreeLeafNode b = this.searchGreaterthan(key);
if (b == null) {
Ref BeforeLast;
if (this.getLastLeaf().getOverflow(this.getLastLeaf().numberOfKeys - 1) != null) {
if (this.getLastLeaf().getOverflow(this.getLastLeaf().numberOfKeys - 1).size() == 0) {
BeforeLast = this.getLastLeaf().getLastRecord();
} else {
Vector<Ref> refs = this.getLastLeaf().getOverflow(this.getLastLeaf().numberOfKeys - 1);
BeforeLast = refs.get(refs.size() - 1);
}
} else {
BeforeLast = this.getLastLeaf().getLastRecord();
}
System.err.println("Last ref is" + BeforeLast);
String pageNumber = BeforeLast.getPage();
String number = pageNumber.substring(tableName.length(), pageNumber.length() - 6);
int n = Integer.parseInt(number);
int rowsCurrent = BeforeLast.getIndexInPage() + 1;
System.err.println("will I enter?");
if (rowsCurrent >= maxRows) {
System.err.println("I entered");
int newRow = 0;
n++;
String nn = Integer.toString(n);
String pageNew = tableName + nn + ".class";
return new Ref(pageNew, newRow);
} else {
return new Ref(pageNumber, rowsCurrent);
}
} else {
int indexInser = -1;
for (int i = 0; i < b.numberOfKeys; i++)
if (b.getKey(i).compareTo(key) > 0) {
indexInser = i;
break;
}
return b.getRecord(indexInser);
}
}
}
public boolean deleteHelper(T key, Ref ref) {
if (root.numberOfKeys == 0) {
if (root instanceof BPTreeInnerNode)
root = ((BPTreeInnerNode) root).getFirstChild();
}
boolean done = root.delete(key, null, -1, ref);
// go down and find the new root in case the old root is deleted
while (root instanceof BPTreeInnerNode && !root.isRoot())
root = ((BPTreeInnerNode<T>) root).getFirstChild();
return done;
}
//delete using references
public boolean delete(T key, Ref ref) {
boolean done = false;
BPTreeLeafNode leaf = this.searchNode(key);
if (leaf != null && leaf.hasDuplicates(key)) {
//System.out.println("I have duplicates so I entered here");
done = leaf.deleteOverflow(key, ref);
//System.out.println("No matching tuple so I will look at main ref");
}
if (ref == null) {
while (this.searchNode(key) != null) {
boolean done2 = false;
done = this.deleteL(key, ref);
done = done2 || done;
}
}
if (ref != null) {
// boolean done2 = false;
// done = this.deleteL(key, ref);
// done = done2 || done;
done = this.deleteHelper(key, ref);
//System.out.println("I will delete here now and it should succeed");
}
return done;
}
public void printingRef(BPTreeLeafNode leaf) {
if (leaf != null) {
leaf.print();
} else {
System.out.println("null");
}
}
public boolean deleteL(T key, Ref ref) {
if (root.numberOfKeys == 0) {
if (root instanceof BPTreeInnerNode)
root = ((BPTreeInnerNode) root).getFirstChild();
}
return root.deleteLeft(key, null, -1, ref);
}
public boolean deleteAllRefs(ArrayList<Ref> refs, T key) {
if (refs == null || refs.size() == 0)
return false;
for (int i = 0; i < refs.size(); i++) {
this.delete(key, refs.get(i));
}
return false;
}
/**
* Returns a string representation of the B+ tree.
*/
public String toString() {
// <For Testing>
// node : (id)[k1|k2|k3|k4]{P1,P2,P3,}
String s = "";
Queue<BPTreeNode<T>> cur = new LinkedList<BPTreeNode<T>>(), next;
cur.add(root);
while (!cur.isEmpty()) {
next = new LinkedList<BPTreeNode<T>>();
while (!cur.isEmpty()) {
BPTreeNode<T> curNode = cur.remove();
System.out.print(curNode);
if (curNode instanceof BPTreeLeafNode) {
this.printingRef((BPTreeLeafNode) curNode);
System.out.print("->");
} else {
System.out.print("{");
BPTreeInnerNode<T> parent = (BPTreeInnerNode<T>) curNode;
for (int i = 0; i <= parent.numberOfKeys; ++i) {
System.out.print(parent.getChild(i).index + ",");
next.add(parent.getChild(i));
}
System.out.print("} ");
}
}
System.out.println();
cur = next;
}
// </For Testing>
return s;
}
}