Skip to content

Commit 7664ade

Browse files
author
Mason Chang
committed
fixed btree tests. delete seems to work
1 parent 5b64662 commit 7664ade

File tree

4 files changed

+81
-17
lines changed

4 files changed

+81
-17
lines changed

src/coffeedb/CoffeeDB.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private void btreeTests() {
170170

171171
System.out.println(btree.toString());
172172

173-
for (int i = 0; i < 1; i++) {
173+
for (int i = 0; i < 5; i++) {
174174
Tuple tuple = Tuple.createTupleAndSchema(i, "test");
175175
System.out.println("Deleting tuple: " + tuple);
176176
btree.deleteKey(tuple.getValue(0));

src/coffeedb/core/Btree.java

+38-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ public Btree(int branchFactor) {
1717

1818
public BtreeNode findBucket(BtreeNode node, Value key) {
1919
if (node.isLeaf()) {
20-
return (BtreeLeafNode) node;
20+
return node;
2121
}
2222

2323
BtreeInternalNode internalNode = (BtreeInternalNode) node;
2424
return internalNode.getChild(key);
2525
}
2626

27+
public BtreeLeafNode findLeaf(Value key) {
28+
return (BtreeLeafNode) findBucket(_root, key);
29+
}
30+
2731
private boolean isRoot(BtreeNode node) {
2832
return _root == node;
2933
}
@@ -55,11 +59,15 @@ public void addKey(Value key, Tuple tuple) {
5559
}
5660
}
5761

62+
public boolean isEmpty() {
63+
return _root.isEmpty();
64+
}
65+
5866
public void deleteKey(Value key) {
5967
BtreeLeafNode leaf = (BtreeLeafNode) findBucket(_root, key);
6068
leaf.deleteKey(key);
6169

62-
if (leaf.needsMerge()) {
70+
if (!isRoot(leaf) && leaf.needsMerge()) {
6371
mergeNode(leaf);
6472
}
6573
}
@@ -332,6 +340,34 @@ public String toString() {
332340
}
333341

334342
return buffer.toString();
343+
}
344+
345+
public int getBranchFactor() {
346+
return _branchFactor;
347+
}
348+
349+
public BtreeNode getRoot() {
350+
assert (_root != null);
351+
return _root;
352+
}
353+
354+
public int getNumberOfNodes() {
355+
LinkedList<BtreeNode> queue = new LinkedList<BtreeNode>();
356+
queue.add(_root);
357+
int count = 0;
358+
359+
while (!queue.isEmpty()) {
360+
BtreeNode node = queue.removeFirst();
361+
count++;
362+
363+
if (node.isLeaf()) continue;
364+
365+
BtreeInternalNode internalNode = (BtreeInternalNode) node;
366+
for (BtreeNode child : internalNode.getChildren()) {
367+
queue.add(child);
368+
}
369+
}
335370

371+
return count;
336372
}
337373
}

src/coffeedb/core/BtreeLeafNode.java

+12
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ public boolean isEmpty() {
115115

116116
return false;
117117
}
118+
119+
public boolean hasNext() {
120+
return _next != null;
121+
}
118122

119123
public BtreeLeafNode getNext() {
120124
return _next;
@@ -154,4 +158,12 @@ public void clean() {
154158
public Value getTupleKey(Tuple tuple) {
155159
return tuple.getValue(KEY_INDEX);
156160
}
161+
162+
public boolean containsTuple(Tuple tuple) {
163+
for (Tuple t : _tuples) {
164+
if (tuple.equals(t)) return true;
165+
}
166+
167+
return false;
168+
}
157169
}

test/coffeedb/test/core/BtreeTests.java

+30-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import coffeedb.Tuple;
1616
import coffeedb.Value;
1717
import coffeedb.core.Btree;
18+
import coffeedb.core.BtreeInternalNode;
1819
import coffeedb.core.BtreeLeafNode;
1920
import coffeedb.core.BtreeNode;
2021

@@ -37,27 +38,29 @@ public void setUp() throws Exception {
3738
@After
3839
public void tearDown() throws Exception {
3940
}
40-
/*
41+
4142
private void leavesHaveTuplesInOrder(List<Tuple> tuples) {
4243
assert (!tuples.isEmpty());
4344
Tuple first = tuples.get(0);
4445
Value firstKey = first.getValue(0);
4546

4647
BtreeLeafNode bucket = _instance.findLeaf(firstKey);
47-
for (int i = 0; i < tuples.size(); i++) {
48-
if (((i % Btree.BRANCH_FACTOR) == 0) && (i != 0)) {
49-
if (bucket.hasNextLeaf()) {
50-
bucket = bucket.getNextLeaf();
51-
assert (bucket != null);
52-
}
48+
for (int i = 0; i < tuples.size(); ) {
49+
List<Tuple> bucketTuples = bucket.getTuples();
50+
51+
for (int j = 0; j < bucketTuples.size(); j++) {
52+
Tuple bucketTuple = bucketTuples.get(j);
53+
Tuple listTuple = tuples.get(i);
54+
assertTrue(bucketTuple.equals(listTuple));
55+
i++;
5356
}
5457

55-
Tuple tuple = tuples.get(i);
56-
assertTrue(bucket.containsTuple(tuple));
58+
if (bucket.hasNext()) {
59+
bucket = bucket.getNext();
60+
}
5761
}
5862
}
5963

60-
/*
6164
private boolean tuplesExist(List<Tuple> tuples) {
6265
for (Tuple t : tuples) {
6366
Value key = t.getValue(0);
@@ -84,15 +87,16 @@ public void insertTest() {
8487
public void splitLeafTest() {
8588
HashMap<Value, Tuple> insertions = new HashMap<Value, Tuple>();
8689
// Force one split
87-
for (int i = 0; i <= Btree.BRANCH_FACTOR; i++) {
90+
for (int i = 0; i <= _instance.getBranchFactor(); i++) {
8891
Tuple tuple = Tuple.createTupleAndSchema(i, "key");
8992
Value key = tuple.getValue(0);
9093
_instance.addKey(key, tuple);
9194
insertions.put(key, tuple);
9295
}
9396

94-
BtreeNode root = _instance.getRoot();
95-
assertTrue(_instance.getTreeSize() == 3);
97+
BtreeInternalNode root = (BtreeInternalNode) _instance.getRoot();
98+
assertTrue(_instance.getNumberOfNodes() == 3);
99+
96100
assertTrue(root.getChildren().size() == 2);
97101

98102
for (BtreeNode child : root.getChildren()) {
@@ -166,5 +170,17 @@ public void testSingleDelete() {
166170
deleteTuples(insertedTuples);
167171
assertFalse(tuplesExist(insertedTuples));
168172
}
169-
*/
173+
174+
@Test
175+
public void deleteAllTuples() {
176+
int records[] = { 1, 4, 47, 49 };
177+
List<Tuple> insertedTuples = insertTuples(records);
178+
assertTrue(tuplesExist(insertedTuples));
179+
180+
deleteTuples(insertedTuples);
181+
assertFalse(tuplesExist(insertedTuples));
182+
183+
int numberOfNodes = _instance.getNumberOfNodes();
184+
assertTrue(numberOfNodes == 1);
185+
}
170186
}

0 commit comments

Comments
 (0)