From 3585e69ac2c21003786d56792667966860132f12 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Thu, 27 Jun 2019 15:05:08 +0430 Subject: [PATCH 01/26] fix version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1576139..0ed8a8a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ jar io.uten BPlusTree - 1.0-SNAPSHOT + 1.0.0 1.21 1.12 From b958c01a566571c6b5c2d6587e01028498a8d8b5 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Thu, 27 Jun 2019 15:05:31 +0430 Subject: [PATCH 02/26] rename pop to poll --- src/main/java/bplustree/BplusTree.java | 2 +- src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java | 4 ++-- src/test/java/bplustree/BplusTreeTest.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index 6a55d2c..e25d853 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -98,7 +98,7 @@ public Value peekValue() { public Key peekKey() { return _root.peekKey(); } - public Value pop() throws BTreeException { + public Value poll() throws BTreeException { Value poppedVal = _root.pop(); if (_root.isEmpty()) _root = new BplusTreeLeafNode(null, null, null, this); diff --git a/src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java b/src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java index 7225edd..4ca9790 100644 --- a/src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java +++ b/src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java @@ -58,12 +58,12 @@ public void setup() throws BTreeException { @Benchmark public void popNodeInBtree(Blackhole blackhole) throws BTreeException { - blackhole.consume(bplusTree.pop()); + blackhole.consume(bplusTree.poll()); } @Benchmark public void peekAndPopNodeInBtree(Blackhole blackhole) throws BTreeException { blackhole.consume(bplusTree.peekKey()); - blackhole.consume(bplusTree.pop()); + blackhole.consume(bplusTree.poll()); } } diff --git a/src/test/java/bplustree/BplusTreeTest.java b/src/test/java/bplustree/BplusTreeTest.java index 94a1bb3..1139c84 100644 --- a/src/test/java/bplustree/BplusTreeTest.java +++ b/src/test/java/bplustree/BplusTreeTest.java @@ -213,7 +213,7 @@ void shouldPop() throws BTreeException { for (int i = 0; i < MAXN; i++) { Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.peekValue()); Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekKey()); - bplusTree.pop(); + bplusTree.poll(); Assertions.assertNotEquals(2 * i, bplusTree.peekValue()); Assertions.assertNotEquals(i, bplusTree.peekKey()); } From 043b37f3097dbe934a21319c4bad34e7c2700b99 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Thu, 27 Jun 2019 16:02:55 +0430 Subject: [PATCH 03/26] fix prev and next updating --- src/main/java/bplustree/BplusTreeLeafNode.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/bplustree/BplusTreeLeafNode.java b/src/main/java/bplustree/BplusTreeLeafNode.java index 5519009..3d720f2 100644 --- a/src/main/java/bplustree/BplusTreeLeafNode.java +++ b/src/main/java/bplustree/BplusTreeLeafNode.java @@ -9,10 +9,22 @@ class BplusTreeLeafNode, Value> extends BplusTreeNod private BplusTreeLeafNode next, prev; private BplusTree tree; + public BplusTreeLeafNode getNext() { + return next; + } + public BplusTreeLeafNode getPrev() { return prev; } + public void setNext(BplusTreeLeafNode next) { + this.next = next; + } + + public void setPrev(BplusTreeLeafNode prev) { + this.prev = prev; + } + public int getDepth() { BplusTreeNode node = this; int depth = 1; @@ -76,6 +88,11 @@ protected void split() throws BTreeException { protected void rebalance() throws BTreeException { tree.setRecentlyUsed(null); + if (getPrev() != null) + getPrev().setNext(getNext()); + if (getNext() != null) + getNext().setPrev(getPrev()); + if (parent != null) parent.removeNode(LeftRangeKey); } From 15870ed0e1ce37df39eedd8bf9215ecadf3b60b0 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Thu, 27 Jun 2019 16:34:57 +0430 Subject: [PATCH 04/26] revert: rename pop to poll --- src/main/java/bplustree/BplusTree.java | 2 +- src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java | 4 ++-- src/test/java/bplustree/BplusTreeTest.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index e25d853..6a55d2c 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -98,7 +98,7 @@ public Value peekValue() { public Key peekKey() { return _root.peekKey(); } - public Value poll() throws BTreeException { + public Value pop() throws BTreeException { Value poppedVal = _root.pop(); if (_root.isEmpty()) _root = new BplusTreeLeafNode(null, null, null, this); diff --git a/src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java b/src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java index 4ca9790..7225edd 100644 --- a/src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java +++ b/src/test/java/benchmark/PeekAndPopNodeInBtreeBenchmark.java @@ -58,12 +58,12 @@ public void setup() throws BTreeException { @Benchmark public void popNodeInBtree(Blackhole blackhole) throws BTreeException { - blackhole.consume(bplusTree.poll()); + blackhole.consume(bplusTree.pop()); } @Benchmark public void peekAndPopNodeInBtree(Blackhole blackhole) throws BTreeException { blackhole.consume(bplusTree.peekKey()); - blackhole.consume(bplusTree.poll()); + blackhole.consume(bplusTree.pop()); } } diff --git a/src/test/java/bplustree/BplusTreeTest.java b/src/test/java/bplustree/BplusTreeTest.java index 1139c84..94a1bb3 100644 --- a/src/test/java/bplustree/BplusTreeTest.java +++ b/src/test/java/bplustree/BplusTreeTest.java @@ -213,7 +213,7 @@ void shouldPop() throws BTreeException { for (int i = 0; i < MAXN; i++) { Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.peekValue()); Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekKey()); - bplusTree.poll(); + bplusTree.pop(); Assertions.assertNotEquals(2 * i, bplusTree.peekValue()); Assertions.assertNotEquals(i, bplusTree.peekKey()); } From 2b968041ce501d035550b704a556a03e8639ff86 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Thu, 27 Jun 2019 16:42:15 +0430 Subject: [PATCH 05/26] add peekLastNode and popBack --- .../java/bplustree/BplusTreeBranchNode.java | 12 +++++++++++- src/main/java/bplustree/BplusTreeLeafNode.java | 17 +++++++++++++++++ src/main/java/bplustree/BplusTreeNode.java | 2 ++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/bplustree/BplusTreeBranchNode.java b/src/main/java/bplustree/BplusTreeBranchNode.java index 2ec72c2..73f6424 100644 --- a/src/main/java/bplustree/BplusTreeBranchNode.java +++ b/src/main/java/bplustree/BplusTreeBranchNode.java @@ -164,6 +164,11 @@ public BplusTreeLeafNode.BplusTreeIterator peekLast() { return children.peekBack().peekLast(); } + @Override + public BplusTreeLeafNode peekLastNode() { + return children.peekBack().peekLastNode(); + } + @Override public Key peekKey() { return children.get(0).peekKey(); @@ -176,6 +181,11 @@ public Value peekValue() { @Override public Value pop() throws BTreeException { - return children.get(0).pop(); + return children.peekFront().pop(); + } + + @Override + public Value popBack() throws BTreeException { + return children.peekBack().popBack(); } } diff --git a/src/main/java/bplustree/BplusTreeLeafNode.java b/src/main/java/bplustree/BplusTreeLeafNode.java index 3d720f2..ee72613 100644 --- a/src/main/java/bplustree/BplusTreeLeafNode.java +++ b/src/main/java/bplustree/BplusTreeLeafNode.java @@ -178,6 +178,11 @@ public BplusTreeIterator peekLast() { return new BplusTreeIterator(this, keys.size() - 1); } + @Override + public BplusTreeLeafNode peekLastNode() { + return this; + } + @Override public Key peekKey() { @@ -201,6 +206,18 @@ public Value pop() throws BTreeException { return result; } + @Override + public Value popBack() throws BTreeException { + Value result = leaves.peekBack(); + + leaves.popBack(); + keys.popBack(); + + if(underOccupied()) + rebalance(); + return result; + } + public class BplusTreeIterator implements Iterator{ private BplusTreeLeafNode node; private int index; diff --git a/src/main/java/bplustree/BplusTreeNode.java b/src/main/java/bplustree/BplusTreeNode.java index 4757141..ffd3b93 100644 --- a/src/main/java/bplustree/BplusTreeNode.java +++ b/src/main/java/bplustree/BplusTreeNode.java @@ -31,8 +31,10 @@ public boolean isInRange(Key key) throws BTreeException { public abstract void removeFrom(Key searchKey) throws BTreeException; public abstract Value find(Key searchKey) throws BTreeException; public abstract BplusTreeLeafNode.BplusTreeIterator peekLast(); + public abstract BplusTreeLeafNode peekLastNode(); public abstract Key peekKey(); public abstract Value peekValue(); public abstract Value pop() throws BTreeException; + public abstract Value popBack() throws BTreeException; } From c750fc0af7bebc9b2a7d1816f24e89da35c96ec6 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Thu, 27 Jun 2019 16:45:04 +0430 Subject: [PATCH 06/26] use lastNode for caching and add peek and pop from back --- src/main/java/bplustree/BplusTree.java | 44 +++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index 6a55d2c..efa8eee 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -4,9 +4,10 @@ public class BplusTree, Value> { private BplusTreeNode _root = new BplusTreeLeafNode(null, null, null, this); - private BplusTreeLeafNode recentlyUsed; + private BplusTreeLeafNode recentlyUsed; + private BplusTreeLeafNode lastNode = (BplusTreeLeafNode) _root; private int hit = 0, miss = 0; - private boolean cacheDisabled = false; + private boolean cacheDisabled; public BplusTree() { this(false); @@ -59,6 +60,8 @@ public void add(Key key, Value value) throws BTreeException { if (_root.getParent() != null) _root = _root.getParent(); + if (lastNode.getNext() != null) + lastNode = lastNode.getNext(); } public void remove(Key key) throws BTreeException { if (getRecentNode() != null && getRecentNode().isInRange(key)) { @@ -69,16 +72,23 @@ public void remove(Key key) throws BTreeException { ++miss; } - if (_root.isEmpty()) + if (_root.isEmpty()) { _root = new BplusTreeLeafNode(null, null, null, this); + lastNode = (BplusTreeLeafNode) _root; + } + else if (lastNode.isEmpty()) + lastNode = lastNode.getPrev(); } public void removeFrom(Key key) throws BTreeException { _root.removeFrom(key); if (_root.isEmpty()) { _root = new BplusTreeLeafNode(null, null, null, this); + lastNode = (BplusTreeLeafNode) _root; recentlyUsed = null; } + else if (lastNode.isEmpty()) + lastNode = _root.peekLastNode(); } public Value find(Key key) throws BTreeException { if (getRecentNode() != null && getRecentNode().isInRange(key)) { @@ -98,12 +108,38 @@ public Value peekValue() { public Key peekKey() { return _root.peekKey(); } + public Value peekLastValue() { + if (lastNode == null) + throw new RuntimeException("lastNode can not be null"); + return lastNode.peekValue(); + } + public Key peekLastKey() { + if (lastNode == null) + throw new RuntimeException("lastNode can not be null"); + return lastNode.peekKey(); + } public Value pop() throws BTreeException { Value poppedVal = _root.pop(); - if (_root.isEmpty()) + if (_root.isEmpty()) { _root = new BplusTreeLeafNode(null, null, null, this); + lastNode = (BplusTreeLeafNode) _root; + } return poppedVal; } + public Value popBack() throws BTreeException { + if (lastNode == null) + throw new RuntimeException("lastNode can not be null"); + + Value poppedVal = lastNode.popBack(); + if (_root.isEmpty()) { + _root = new BplusTreeLeafNode(null, null, null, this); + lastNode = (BplusTreeLeafNode) _root; + } + else if (lastNode.isEmpty()) + lastNode = lastNode.getPrev(); + + return poppedVal; + } } From b0ef65c20ce0f1dbc3e19dfc95c282c8abd16dbc Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Thu, 27 Jun 2019 16:47:23 +0430 Subject: [PATCH 07/26] add poll and peek for general use --- src/main/java/bplustree/BplusTree.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index efa8eee..be7ac08 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -142,4 +142,11 @@ else if (lastNode.isEmpty()) return poppedVal; } + public Value poll() throws BTreeException { + return popBack(); + } + @Beta + public Value peek() { + return peekLastValue(); + } } From 04ef414ded8a6c0f477c62b0498f3ca89b5dc593 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Thu, 27 Jun 2019 18:12:53 +0430 Subject: [PATCH 08/26] fix peekLastValue --- src/main/java/bplustree/BplusTree.java | 2 +- src/main/java/bplustree/BplusTreeLeafNode.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index be7ac08..a38f70d 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -111,7 +111,7 @@ public Key peekKey() { public Value peekLastValue() { if (lastNode == null) throw new RuntimeException("lastNode can not be null"); - return lastNode.peekValue(); + return lastNode.peekBackValue(); } public Key peekLastKey() { if (lastNode == null) diff --git a/src/main/java/bplustree/BplusTreeLeafNode.java b/src/main/java/bplustree/BplusTreeLeafNode.java index ee72613..af1a475 100644 --- a/src/main/java/bplustree/BplusTreeLeafNode.java +++ b/src/main/java/bplustree/BplusTreeLeafNode.java @@ -193,6 +193,9 @@ public Key peekKey() { public Value peekValue() { return leaves.peekFront(); } + public Value peekBackValue() { + return leaves.peekBack(); + } @Override public Value pop() throws BTreeException { From 88943899176f5935577ba6e9523b3761a18eb3b0 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 29 Jun 2019 11:53:56 +0430 Subject: [PATCH 09/26] add clone and equals to CircularFifoQueue --- src/main/java/utility/CircularFifoQueue.java | 45 ++++++++++++++++-- .../java/utility/CircularFifoQueueTest.java | 47 +++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/main/java/utility/CircularFifoQueue.java b/src/main/java/utility/CircularFifoQueue.java index dac17a7..93ea376 100644 --- a/src/main/java/utility/CircularFifoQueue.java +++ b/src/main/java/utility/CircularFifoQueue.java @@ -24,13 +24,13 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; import java.util.AbstractCollection; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; -import static java.lang.StrictMath.max; -import static java.lang.StrictMath.min; +import static java.lang.StrictMath.*; /** * CircularFifoQueue is a first-in first-out queue with a fixed size @@ -40,7 +40,7 @@ * @since 4.0 */ public class CircularFifoQueue extends AbstractCollection - implements Serializable { + implements Serializable, Cloneable { /** Serialization version. */ private static final long serialVersionUID = -8423413834657610406L; @@ -112,6 +112,45 @@ public CircularFifoQueue(final E[] elements, final int size) { this.full = end == maxElements; } + @Override + public boolean equals(Object obj) { + if (!(obj instanceof CircularFifoQueue)) + return false; + + CircularFifoQueue queue = (CircularFifoQueue) obj; + if (queue.start != this.start || queue.end != this.end || queue.full != this.full || queue.maxElements != this.maxElements) + return false; + for (int i = 0; i < this.maxElements; i++) + if (this.elements[i] != null) { + if (!queue.elements[i].equals(this.elements[i])) + return false; + } else if (queue.elements[i] != null) + return false; + + return true; + } + + public CircularFifoQueue clone() throws CloneNotSupportedException { + CircularFifoQueue cloned = (CircularFifoQueue) super.clone(); + cloned.start = this.start; + cloned.end = this.end; + cloned.full = this.full; + + for (int i = 0; i < this.maxElements; i++) + if (this.elements[i] != null) { + try { + try { + cloned.elements[i] = this.elements[i].getClass().getMethod("clone").invoke(this.elements[i]); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } catch (NoSuchMethodException e) { + e.printStackTrace(); + throw new CloneNotSupportedException("E is not cloneable"); + } + } + return cloned; + } //----------------------------------------------------------------------- /** * Write the queue out using a custom routine. diff --git a/src/test/java/utility/CircularFifoQueueTest.java b/src/test/java/utility/CircularFifoQueueTest.java index d3336e7..2448133 100644 --- a/src/test/java/utility/CircularFifoQueueTest.java +++ b/src/test/java/utility/CircularFifoQueueTest.java @@ -403,4 +403,51 @@ void peekFrontForcedFromEnd() { queue.removeFrom(0); Assertions.assertEquals(Integer.valueOf(0), queue.peekFrontForced()); } + + class SimpleCloneable implements Cloneable { + public SimpleCloneable clone() throws CloneNotSupportedException { + return (SimpleCloneable) super.clone(); + } + } + + @Test + void equals() { + Assertions.assertEquals(queue, queue); + CircularFifoQueue shifted = new CircularFifoQueue(4); + + shifted.pushBack(0); + shifted.pushBack(queue.peekFront()); + shifted.popFront(); + + for (int i = 1; i < 4; i++) + shifted.pushBack(queue.get(i)); + + for (int i = 0; i < 4; i++) + Assertions.assertEquals(shifted.get(i), queue.get(i)); + Assertions.assertNotEquals(shifted, queue); + } + + @Test + void cloneFullArray() throws CloneNotSupportedException { + CircularFifoQueue base = new CircularFifoQueue<>(4); + for (int i = 0; i < 2; i++) { + base.pushBack(new SimpleCloneable()); + base.pushFront(new SimpleCloneable()); + } + + CircularFifoQueue cloned = base.clone(); + Assertions.assertEquals(base, cloned); + Assertions.assertNotSame(base, cloned); + } + + @Test + void cloneHalfFullArray() throws CloneNotSupportedException { + CircularFifoQueue base = new CircularFifoQueue<>(4); + base.pushFront(new SimpleCloneable()); + base.pushBack(new SimpleCloneable()); + + CircularFifoQueue cloned = base.clone(); + Assertions.assertEquals(base, cloned); + Assertions.assertNotSame(base, cloned); + } } \ No newline at end of file From 6e581ab5a009e5a7b99013cd58c730c0dffd9d68 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 29 Jun 2019 12:50:02 +0430 Subject: [PATCH 10/26] Revert "add poll and peek for general use" This reverts commit b0ef65c20ce0f1dbc3e19dfc95c282c8abd16dbc. --- src/main/java/bplustree/BplusTree.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index a38f70d..ae4a2c6 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -142,11 +142,4 @@ else if (lastNode.isEmpty()) return poppedVal; } - public Value poll() throws BTreeException { - return popBack(); - } - @Beta - public Value peek() { - return peekLastValue(); - } } From 40b152b286dd7aec245950bde45e363960484930 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 29 Jun 2019 19:50:38 +0430 Subject: [PATCH 11/26] add SimpleCloneable and SimpleComparableCloneable for testing --- src/test/java/utility/SimpleCloneable.java | 12 ++++++++++++ .../utility/SimpleComparableCloneable.java | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/test/java/utility/SimpleCloneable.java create mode 100644 src/test/java/utility/SimpleComparableCloneable.java diff --git a/src/test/java/utility/SimpleCloneable.java b/src/test/java/utility/SimpleCloneable.java new file mode 100644 index 0000000..6f22a5f --- /dev/null +++ b/src/test/java/utility/SimpleCloneable.java @@ -0,0 +1,12 @@ +package utility; + +public class SimpleCloneable implements Cloneable { + public SimpleCloneable clone() throws CloneNotSupportedException { + return (SimpleCloneable) super.clone(); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof SimpleCloneable; + } +} \ No newline at end of file diff --git a/src/test/java/utility/SimpleComparableCloneable.java b/src/test/java/utility/SimpleComparableCloneable.java new file mode 100644 index 0000000..5ac7a3d --- /dev/null +++ b/src/test/java/utility/SimpleComparableCloneable.java @@ -0,0 +1,18 @@ +package utility; + +public class SimpleComparableCloneable implements Comparable, Cloneable { + @Override + public SimpleComparableCloneable clone() throws CloneNotSupportedException { + return (SimpleComparableCloneable) super.clone(); + } + + @Override + public int compareTo(SimpleComparableCloneable o) { + return 0; + } + + @Override + public boolean equals(Object obj) { + return obj instanceof SimpleComparableCloneable; + } +} From fb0a2b0d33f85e7554d7e93d87349120a8652804 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 29 Jun 2019 19:52:44 +0430 Subject: [PATCH 12/26] return null Iterator if tree is empty --- src/main/java/bplustree/BplusTreeLeafNode.java | 2 +- src/test/java/bplustree/BplusTreeTest.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/bplustree/BplusTreeLeafNode.java b/src/main/java/bplustree/BplusTreeLeafNode.java index af1a475..438984e 100644 --- a/src/main/java/bplustree/BplusTreeLeafNode.java +++ b/src/main/java/bplustree/BplusTreeLeafNode.java @@ -175,7 +175,7 @@ public Value find(Key searchKey) throws BTreeException { @Override public BplusTreeIterator peekLast() { - return new BplusTreeIterator(this, keys.size() - 1); + return isEmpty() ? null : new BplusTreeIterator(this, keys.size() - 1); } @Override diff --git a/src/test/java/bplustree/BplusTreeTest.java b/src/test/java/bplustree/BplusTreeTest.java index 94a1bb3..c93ff25 100644 --- a/src/test/java/bplustree/BplusTreeTest.java +++ b/src/test/java/bplustree/BplusTreeTest.java @@ -318,7 +318,9 @@ void shouldCheckSampleDepth() { } @Test - void peekLast() { + void shouldIterateBackward() { + Assertions.assertNull(new BplusTree().peekLast()); + BplusTreeLeafNode.BplusTreeIterator iterator = bplusTree.peekLast(); for (int i = MAXN - 1; i > 0; i--) { From e9edd6e8c41f74815c96215c9727edb57ad94386 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 29 Jun 2019 19:53:53 +0430 Subject: [PATCH 13/26] add equals() and clone() --- src/main/java/bplustree/BplusTree.java | 46 +++++++- .../java/bplustree/BplusTreeBranchNode.java | 2 +- src/main/java/bplustree/BplusTreeCloner.java | 107 ++++++++++++++++++ .../java/bplustree/BplusTreeLeafNode.java | 7 +- src/main/java/bplustree/BplusTreeNode.java | 2 +- src/main/java/utility/CircularFifoQueue.java | 34 ++++-- src/test/java/bplustree/BplusTreeTest.java | 34 ++++++ .../java/utility/CircularFifoQueueTest.java | 6 - 8 files changed, 219 insertions(+), 19 deletions(-) create mode 100644 src/main/java/bplustree/BplusTreeCloner.java diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index ae4a2c6..260603d 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -3,12 +3,56 @@ import com.google.common.annotations.Beta; public class BplusTree, Value> { - private BplusTreeNode _root = new BplusTreeLeafNode(null, null, null, this); + private BplusTreeNode _root = new BplusTreeLeafNode<>(null, null, null, this); private BplusTreeLeafNode recentlyUsed; private BplusTreeLeafNode lastNode = (BplusTreeLeafNode) _root; private int hit = 0, miss = 0; private boolean cacheDisabled; + @Override + public boolean equals(Object obj) { + if (!(obj instanceof BplusTree)) + return false; + + BplusTreeLeafNode.BplusTreeIterator here = peekLast(); + BplusTreeLeafNode.BplusTreeIterator there = ((BplusTree) obj).peekLast(); + + if (here == null || there == null) + return here == there; + + while (true) { + if (!here.getKey().equals(there.getKey())) + return false; + if (!here.getValue().equals(there.getValue())) + return false; + if (here.hasNext() != there.hasNext()) + return false; + + if (here.hasNext()) { + here.goToNext(); + there.goToNext(); + } + else + break; + } + + return true; + } + + @Override + public BplusTree clone() throws CloneNotSupportedException{ + BplusTree cloned = new BplusTree(); + BplusTreeCloner treeCloner = new BplusTreeCloner(cloned); + + cloned._root = treeCloner.clone(this._root); + cloned.recentlyUsed = (BplusTreeLeafNode) treeCloner.clone(recentlyUsed); + cloned.lastNode = (BplusTreeLeafNode) treeCloner.clone(lastNode); + cloned.hit = this.hit; + cloned.miss = this.miss; + cloned.cacheDisabled = this.cacheDisabled; + return cloned; + } + public BplusTree() { this(false); } diff --git a/src/main/java/bplustree/BplusTreeBranchNode.java b/src/main/java/bplustree/BplusTreeBranchNode.java index 73f6424..bf80b1f 100644 --- a/src/main/java/bplustree/BplusTreeBranchNode.java +++ b/src/main/java/bplustree/BplusTreeBranchNode.java @@ -6,7 +6,7 @@ import static utility.Utils.searchRightmostKey; class BplusTreeBranchNode extends BplusTreeNode{ - private CircularFifoQueue> children; + protected CircularFifoQueue> children; public BplusTreeBranchNode(BplusTreeBranchNode parent) { this(new CircularFifoQueue<>(CAPACITY), new CircularFifoQueue<>(CAPACITY), parent); diff --git a/src/main/java/bplustree/BplusTreeCloner.java b/src/main/java/bplustree/BplusTreeCloner.java new file mode 100644 index 0000000..634c860 --- /dev/null +++ b/src/main/java/bplustree/BplusTreeCloner.java @@ -0,0 +1,107 @@ +package bplustree; + +import utility.CircularFifoQueue; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +public class BplusTreeCloner { + private BplusTree tree; + private Map nodeMap = new HashMap<>(); + + BplusTreeCloner(BplusTree tree) { + this.tree = tree; + } + + public BplusTreeNode clone(BplusTreeNode node) throws CloneNotSupportedException { + if (node == null) + return null; + if (node instanceof BplusTreeLeafNode) + return clone((BplusTreeLeafNode) node); + if (node instanceof BplusTreeBranchNode) + return clone((BplusTreeBranchNode) node); + + throw new IllegalArgumentException("node is in illegal state"); + } + + private BplusTreeLeafNode clone(BplusTreeLeafNode node) throws CloneNotSupportedException { + if (node == null) + return null; + if (nodeMap.containsKey(node)) + return (BplusTreeLeafNode) nodeMap.get(node); + + BplusTreeLeafNode cloned = new BplusTreeLeafNode(null, null, null, tree); + nodeMap.put(node, cloned); + + cloneBplusTreeNodeInternals(node, cloned); + cloned.leaves = node.leaves.clone(); + + + if (node.next != null) + cloned.next = clone(node.next); + if (node.prev != null) + cloned.prev = clone(node.prev); + + return cloned; + } + + private BplusTreeBranchNode clone(BplusTreeBranchNode node) throws CloneNotSupportedException { + if (node == null) + return null; + if (nodeMap.containsKey(node)) + return (BplusTreeBranchNode) nodeMap.get(node); + + BplusTreeBranchNode cloned = new BplusTreeBranchNode(null); + nodeMap.put(node, cloned); + + cloneBplusTreeNodeInternals(node, cloned); + cloned.children = node.children.clone(this); + + return cloned; + } + + private void cloneBplusTreeNodeInternals(BplusTreeNode base, BplusTreeNode cloned) throws CloneNotSupportedException { + cloned.parent = clone(base.parent); + cloned.keys = base.keys.clone(); + if (base.LeftRangeKey instanceof Cloneable) + try { + try { + cloned.LeftRangeKey = (Comparable) base.LeftRangeKey.getClass().getMethod("clone").invoke(base.LeftRangeKey); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } catch (NoSuchMethodException e) { + cloned.LeftRangeKey = base.LeftRangeKey; + throw new CloneNotSupportedException("Key is not cloneable"); + } + else + cloned.LeftRangeKey = base.LeftRangeKey; + } + + private Object getFieldOfCircularFifoQueue(CircularFifoQueue queue, String fieldName) throws NoSuchFieldException, IllegalAccessException { + Field field = queue.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(queue); + } + + private Object[] getArrayFieldOfCircularFifoQueue(CircularFifoQueue queue) throws NoSuchFieldException, IllegalAccessException { + Field field = queue.getClass().getDeclaredField("elements"); + field.setAccessible(true); + return (Object[]) field.get(queue); + } + + private void setFieldOfCircularFifoQueue(CircularFifoQueue queue, String fieldName, T value) throws NoSuchFieldException, IllegalAccessException { + Field field = queue.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + field.set(queue, value); + } + + private void copyCircularFifoQueueField(CircularFifoQueue from, CircularFifoQueue to, String fieldName) throws NoSuchFieldException, IllegalAccessException { + T value = (T)getFieldOfCircularFifoQueue(from, fieldName); + Field field = to.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + field.set(to, value); + } +} \ No newline at end of file diff --git a/src/main/java/bplustree/BplusTreeLeafNode.java b/src/main/java/bplustree/BplusTreeLeafNode.java index 438984e..dbd036a 100644 --- a/src/main/java/bplustree/BplusTreeLeafNode.java +++ b/src/main/java/bplustree/BplusTreeLeafNode.java @@ -5,9 +5,10 @@ import static utility.Utils.searchLeftmostKey; class BplusTreeLeafNode, Value> extends BplusTreeNode { - private CircularFifoQueue leaves; - private BplusTreeLeafNode next, prev; - private BplusTree tree; + protected CircularFifoQueue leaves; + protected BplusTreeLeafNode next; + protected BplusTreeLeafNode prev; + protected BplusTree tree; public BplusTreeLeafNode getNext() { return next; diff --git a/src/main/java/bplustree/BplusTreeNode.java b/src/main/java/bplustree/BplusTreeNode.java index ffd3b93..e67e4de 100644 --- a/src/main/java/bplustree/BplusTreeNode.java +++ b/src/main/java/bplustree/BplusTreeNode.java @@ -2,7 +2,7 @@ import utility.CircularFifoQueue; -abstract class BplusTreeNode { +public abstract class BplusTreeNode { protected static final int CAPACITY = 127; protected BplusTreeBranchNode parent; protected CircularFifoQueue keys; diff --git a/src/main/java/utility/CircularFifoQueue.java b/src/main/java/utility/CircularFifoQueue.java index 93ea376..e800c8d 100644 --- a/src/main/java/utility/CircularFifoQueue.java +++ b/src/main/java/utility/CircularFifoQueue.java @@ -17,6 +17,8 @@ * limitations under the License. */ +import bplustree.BplusTreeCloner; +import bplustree.BplusTreeNode; import com.google.common.annotations.Beta; import java.io.IOException; @@ -135,19 +137,37 @@ public CircularFifoQueue clone() throws CloneNotSupportedException { cloned.start = this.start; cloned.end = this.end; cloned.full = this.full; + cloned.elements = new Object[maxElements]; for (int i = 0; i < this.maxElements; i++) if (this.elements[i] != null) { - try { + if (this.elements[i] instanceof Cloneable) try { - cloned.elements[i] = this.elements[i].getClass().getMethod("clone").invoke(this.elements[i]); - } catch (IllegalAccessException | InvocationTargetException e) { + try { + cloned.elements[i] = this.elements[i].getClass().getMethod("clone").invoke(this.elements[i]); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } catch (NoSuchMethodException e) { e.printStackTrace(); + throw new CloneNotSupportedException("E is not cloneable"); } - } catch (NoSuchMethodException e) { - e.printStackTrace(); - throw new CloneNotSupportedException("E is not cloneable"); - } + else + cloned.elements[i] = this.elements[i]; + } + return cloned; + } + + public CircularFifoQueue clone(BplusTreeCloner cloner) throws CloneNotSupportedException { + CircularFifoQueue cloned = (CircularFifoQueue) super.clone(); + cloned.start = this.start; + cloned.end = this.end; + cloned.full = this.full; + cloned.elements = new BplusTreeNode[maxElements]; + + for (int i = 0; i < this.maxElements; i++) + if (this.elements[i] != null) { + cloned.elements[i] = cloner.clone((BplusTreeNode) this.elements[i]); } return cloned; } diff --git a/src/test/java/bplustree/BplusTreeTest.java b/src/test/java/bplustree/BplusTreeTest.java index c93ff25..659032d 100644 --- a/src/test/java/bplustree/BplusTreeTest.java +++ b/src/test/java/bplustree/BplusTreeTest.java @@ -3,6 +3,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import utility.CircularFifoQueue; +import utility.SimpleCloneable; +import utility.SimpleComparableCloneable; import java.lang.reflect.Field; import java.text.MessageFormat; @@ -334,4 +336,36 @@ void shouldIterateBackward() { Assertions.assertEquals(0, iterator.getValue()); Assertions.assertFalse(iterator.hasNext()); } + + @Test + void shouldCheckEquals() throws CloneNotSupportedException, BTreeException { + Assertions.assertNotEquals(bplusTree, new BplusTree()); + Assertions.assertEquals(bplusTree, bplusTree); + Assertions.assertNotEquals(bplusTree, null); + + BplusTree secondaryTree = bplusTree.clone(); + Assertions.assertEquals(bplusTree, secondaryTree); + + secondaryTree.add(-1, -1); + Assertions.assertNull(bplusTree.find(-1)); + Assertions.assertNotEquals(bplusTree, secondaryTree); + + secondaryTree.remove(-1); + Assertions.assertEquals(bplusTree, secondaryTree); + + bplusTree.add(-1, -1); + secondaryTree.add(-1, -1); + Assertions.assertEquals(bplusTree, secondaryTree); + } + + @Test + void shouldClone() throws CloneNotSupportedException, BTreeException { + BplusTree bplusTree = new BplusTree<>(); + Assertions.assertNotSame(bplusTree, bplusTree.clone()); + + BplusTree mutatedBplusTree = new BplusTree(); + mutatedBplusTree.add(new SimpleComparableCloneable(), new SimpleCloneable()); + Assertions.assertNotEquals(bplusTree, mutatedBplusTree); + Assertions.assertEquals(mutatedBplusTree, mutatedBplusTree.clone()); + } } \ No newline at end of file diff --git a/src/test/java/utility/CircularFifoQueueTest.java b/src/test/java/utility/CircularFifoQueueTest.java index 2448133..0af4eba 100644 --- a/src/test/java/utility/CircularFifoQueueTest.java +++ b/src/test/java/utility/CircularFifoQueueTest.java @@ -404,12 +404,6 @@ void peekFrontForcedFromEnd() { Assertions.assertEquals(Integer.valueOf(0), queue.peekFrontForced()); } - class SimpleCloneable implements Cloneable { - public SimpleCloneable clone() throws CloneNotSupportedException { - return (SimpleCloneable) super.clone(); - } - } - @Test void equals() { Assertions.assertEquals(queue, queue); From 0bb816e62a67434b463dd2c9cf8dc0905bd03ef9 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 29 Jun 2019 23:43:33 +0430 Subject: [PATCH 14/26] return removed value after operation --- src/main/java/bplustree/BplusTree.java | 11 +++++++---- src/main/java/bplustree/BplusTreeBranchNode.java | 4 ++-- src/main/java/bplustree/BplusTreeLeafNode.java | 5 ++++- src/main/java/bplustree/BplusTreeNode.java | 2 +- src/test/java/bplustree/BplusTreeTest.java | 8 ++++---- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index 260603d..420592f 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -65,7 +65,7 @@ public boolean cacheEnabled() { return !cacheDisabled; } - public BplusTreeBranchNode getRecentNode() { + public BplusTreeBranchNode getRecentNode() { if (cacheDisabled) return null; if (recentlyUsed != null && recentlyUsed.getParent() != null) @@ -107,12 +107,13 @@ public void add(Key key, Value value) throws BTreeException { if (lastNode.getNext() != null) lastNode = lastNode.getNext(); } - public void remove(Key key) throws BTreeException { + public Value remove(Key key) throws BTreeException { + Value result; if (getRecentNode() != null && getRecentNode().isInRange(key)) { - getRecentNode().remove(key); + result = getRecentNode().remove(key); ++hit; } else { - _root.remove(key); + result = _root.remove(key); ++miss; } @@ -122,6 +123,8 @@ public void remove(Key key) throws BTreeException { } else if (lastNode.isEmpty()) lastNode = lastNode.getPrev(); + + return result; } public void removeFrom(Key key) throws BTreeException { _root.removeFrom(key); diff --git a/src/main/java/bplustree/BplusTreeBranchNode.java b/src/main/java/bplustree/BplusTreeBranchNode.java index bf80b1f..34d10e3 100644 --- a/src/main/java/bplustree/BplusTreeBranchNode.java +++ b/src/main/java/bplustree/BplusTreeBranchNode.java @@ -116,14 +116,14 @@ public void add(Key key, Value value) throws BTreeException { } @Override - public void remove(Key key) throws BTreeException { + public Value remove(Key key) throws BTreeException { if (key == null) { throw new BTreeException("Can't search on null Value"); } int idx = searchRightmostKey(keys, key, keys.size()); idx = idx < 0 ? -(idx + 1) : idx; - children.get(idx).remove(key); + return children.get(idx).remove(key); } @Override diff --git a/src/main/java/bplustree/BplusTreeLeafNode.java b/src/main/java/bplustree/BplusTreeLeafNode.java index dbd036a..b99a8c1 100644 --- a/src/main/java/bplustree/BplusTreeLeafNode.java +++ b/src/main/java/bplustree/BplusTreeLeafNode.java @@ -124,7 +124,7 @@ public void add(Key key, Value value) throws BTreeException { } @Override - public void remove(Key key) throws BTreeException { + public Value remove(Key key) throws BTreeException { if (key == null) { throw new BTreeException("Can't work with null key"); } @@ -135,11 +135,14 @@ public void remove(Key key) throws BTreeException { throw new BTreeException("Can't delete non-existent key " + key.toString()); keys.remove(idx); + Value result = leaves.get(idx); leaves.remove(idx); if(underOccupied()) { rebalance(); } + + return result; } @Override diff --git a/src/main/java/bplustree/BplusTreeNode.java b/src/main/java/bplustree/BplusTreeNode.java index e67e4de..b8f9cf9 100644 --- a/src/main/java/bplustree/BplusTreeNode.java +++ b/src/main/java/bplustree/BplusTreeNode.java @@ -27,7 +27,7 @@ public boolean isInRange(Key key) throws BTreeException { } public abstract void add(Key searchKey, Value value) throws BTreeException; - public abstract void remove(Key searchKey) throws BTreeException; + public abstract Value remove(Key searchKey) throws BTreeException; public abstract void removeFrom(Key searchKey) throws BTreeException; public abstract Value find(Key searchKey) throws BTreeException; public abstract BplusTreeLeafNode.BplusTreeIterator peekLast(); diff --git a/src/test/java/bplustree/BplusTreeTest.java b/src/test/java/bplustree/BplusTreeTest.java index 659032d..92a3744 100644 --- a/src/test/java/bplustree/BplusTreeTest.java +++ b/src/test/java/bplustree/BplusTreeTest.java @@ -69,7 +69,7 @@ void shouldRemove() throws BTreeException { for (int i = 0; i < MAXN; i += 2) { Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.find(i)); - bplusTree.remove(i); + Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.remove(i)); Assertions.assertNull(bplusTree.find(i)); } Assertions.assertThrows(BTreeException.class, () -> bplusTree.remove(2)); @@ -80,7 +80,7 @@ void shouldRemove() throws BTreeException { void shouldRemoveRange() throws BTreeException { for (int i = MAXN / 30; i < MAXN * 29 / 30; i += 2) { Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.find(i)); - bplusTree.remove(i); + Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.remove(i)); Assertions.assertNull(bplusTree.find(i)); } for (int i = 0; i < MAXN / 30; i++) @@ -101,7 +101,7 @@ void shouldAddRemoveAlternatively() throws BTreeException { for (int i = 0; i < MAXN; i += forward - backward) { for (int j = i; j < min(i + forward, MAXN); j++) { Assertions.assertEquals(Integer.valueOf(2 * j), bplusTree.find(j)); - bplusTree.remove(j); + Assertions.assertEquals(Integer.valueOf(2 * j), bplusTree.remove(j)); Assertions.assertNull(bplusTree.find(j)); } for (int j = 1; j <= backward; j++) { @@ -121,7 +121,7 @@ void shouldRemoveAndAddHalfRepeatedly() throws BTreeException { for (int i = 0; i < range; i++) { Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.find(i)); - bplusTree.remove(i); + Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.remove(i)); Assertions.assertNull(bplusTree.find(i)); } From f5876febebf35b94ce600a5199d4f8db08239523 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sun, 21 Jul 2019 12:12:52 +0430 Subject: [PATCH 15/26] fix nested try/catch --- src/main/java/bplustree/BplusTreeCloner.java | 8 +++----- src/main/java/utility/CircularFifoQueue.java | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/bplustree/BplusTreeCloner.java b/src/main/java/bplustree/BplusTreeCloner.java index 634c860..abe50bc 100644 --- a/src/main/java/bplustree/BplusTreeCloner.java +++ b/src/main/java/bplustree/BplusTreeCloner.java @@ -67,11 +67,9 @@ private void cloneBplusTreeNodeInternals(BplusTreeNode base, BplusTreeNode clone cloned.keys = base.keys.clone(); if (base.LeftRangeKey instanceof Cloneable) try { - try { - cloned.LeftRangeKey = (Comparable) base.LeftRangeKey.getClass().getMethod("clone").invoke(base.LeftRangeKey); - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - } + cloned.LeftRangeKey = (Comparable) base.LeftRangeKey.getClass().getMethod("clone").invoke(base.LeftRangeKey); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); } catch (NoSuchMethodException e) { cloned.LeftRangeKey = base.LeftRangeKey; throw new CloneNotSupportedException("Key is not cloneable"); diff --git a/src/main/java/utility/CircularFifoQueue.java b/src/main/java/utility/CircularFifoQueue.java index e800c8d..b29e012 100644 --- a/src/main/java/utility/CircularFifoQueue.java +++ b/src/main/java/utility/CircularFifoQueue.java @@ -143,11 +143,9 @@ public CircularFifoQueue clone() throws CloneNotSupportedException { if (this.elements[i] != null) { if (this.elements[i] instanceof Cloneable) try { - try { - cloned.elements[i] = this.elements[i].getClass().getMethod("clone").invoke(this.elements[i]); - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - } + cloned.elements[i] = this.elements[i].getClass().getMethod("clone").invoke(this.elements[i]); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); throw new CloneNotSupportedException("E is not cloneable"); From 60fbe89d96285089b78358291319ce82867aa5e8 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sun, 21 Jul 2019 12:24:07 +0430 Subject: [PATCH 16/26] improve bplusTree.equals() test --- src/test/java/bplustree/BplusTreeTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/bplustree/BplusTreeTest.java b/src/test/java/bplustree/BplusTreeTest.java index 92a3744..d59937d 100644 --- a/src/test/java/bplustree/BplusTreeTest.java +++ b/src/test/java/bplustree/BplusTreeTest.java @@ -341,6 +341,7 @@ void shouldIterateBackward() { void shouldCheckEquals() throws CloneNotSupportedException, BTreeException { Assertions.assertNotEquals(bplusTree, new BplusTree()); Assertions.assertEquals(bplusTree, bplusTree); + Assertions.assertEquals(new BplusTree(), new BplusTree()); Assertions.assertNotEquals(bplusTree, null); BplusTree secondaryTree = bplusTree.clone(); @@ -348,6 +349,7 @@ void shouldCheckEquals() throws CloneNotSupportedException, BTreeException { secondaryTree.add(-1, -1); Assertions.assertNull(bplusTree.find(-1)); + Assertions.assertNotNull(secondaryTree.find(-1)); Assertions.assertNotEquals(bplusTree, secondaryTree); secondaryTree.remove(-1); @@ -356,6 +358,11 @@ void shouldCheckEquals() throws CloneNotSupportedException, BTreeException { bplusTree.add(-1, -1); secondaryTree.add(-1, -1); Assertions.assertEquals(bplusTree, secondaryTree); + + bplusTree.add(MAXN, 2 * MAXN); + Assertions.assertNotEquals(bplusTree, secondaryTree); + secondaryTree.add(MAXN, 3 * MAXN); + Assertions.assertNotEquals(bplusTree, secondaryTree); } @Test From ea9c00d738d53c817dcb9e0949d27b675712dd25 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sun, 21 Jul 2019 12:55:30 +0430 Subject: [PATCH 17/26] run tearDown after each iteration --- src/test/java/benchmark/AddNodeToBtreeBenchmark.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/benchmark/AddNodeToBtreeBenchmark.java b/src/test/java/benchmark/AddNodeToBtreeBenchmark.java index 8054e18..4a6c136 100644 --- a/src/test/java/benchmark/AddNodeToBtreeBenchmark.java +++ b/src/test/java/benchmark/AddNodeToBtreeBenchmark.java @@ -120,7 +120,7 @@ public void addBatch1m() throws BTreeException { } - @TearDown + @TearDown(Level.Iteration) public void tearDown() { System.out.println(MessageFormat.format("test finished with {0} hits and {1} misses", bplusTree.getHit(), bplusTree.getMiss())); System.out.println(MessageFormat.format("sample depth is {0}", bplusTree.getSampleDepth())); From bd874066f0fe57ebf972db161743b9cb6465ee3c Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sun, 21 Jul 2019 17:35:46 +0430 Subject: [PATCH 18/26] improve isInRange --- src/main/java/bplustree/BplusTreeLeafNode.java | 7 +++++++ src/main/java/bplustree/BplusTreeNode.java | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/bplustree/BplusTreeLeafNode.java b/src/main/java/bplustree/BplusTreeLeafNode.java index b99a8c1..3dcfbd1 100644 --- a/src/main/java/bplustree/BplusTreeLeafNode.java +++ b/src/main/java/bplustree/BplusTreeLeafNode.java @@ -98,6 +98,13 @@ protected void rebalance() throws BTreeException { parent.removeNode(LeftRangeKey); } + @Override + public boolean isInRange(Key key) throws BTreeException { + if (super.isInRange(key)) + return true; + return getNext() == null && key.compareTo(keys.peekBack()) > 0; + } + @Override public void add(Key key, Value value) throws BTreeException { if (key == null) { diff --git a/src/main/java/bplustree/BplusTreeNode.java b/src/main/java/bplustree/BplusTreeNode.java index b8f9cf9..f6fae66 100644 --- a/src/main/java/bplustree/BplusTreeNode.java +++ b/src/main/java/bplustree/BplusTreeNode.java @@ -23,6 +23,8 @@ BplusTreeBranchNode getParent() { public boolean isInRange(Key key) throws BTreeException { if (key == null) throw new BTreeException("Can't work with null key"); + if (LeftRangeKey == null) + return true; return key.compareTo(LeftRangeKey) > -1 && key.compareTo(keys.peekBack()) < 1; } From 2c35da31e057aca46be0fa02f776693e0b7b4e14 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sun, 21 Jul 2019 17:37:18 +0430 Subject: [PATCH 19/26] add benchmark for add in a row --- .../benchmark/AddNodeToBtreeBenchmark.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/test/java/benchmark/AddNodeToBtreeBenchmark.java b/src/test/java/benchmark/AddNodeToBtreeBenchmark.java index 4a6c136..09fc1bd 100644 --- a/src/test/java/benchmark/AddNodeToBtreeBenchmark.java +++ b/src/test/java/benchmark/AddNodeToBtreeBenchmark.java @@ -71,6 +71,12 @@ int getNextRandIndex() { return index * Period + ++occCounter[index]; } + void addNodeRandomPermutationBatch(int batchSize) throws BTreeException { + Integer currIndex = getNextRandPermutationIndex(); + for (int i = 0; i < batchSize; ++i) + bplusTree.add(currIndex * batchSize + i, currIndex); + } + @Benchmark public void addNodeInIncrement() throws BTreeException { Integer currIndex = getNextIndex(); @@ -85,8 +91,32 @@ public void addNodeInDecrement() throws BTreeException { @Benchmark public void addNodeRandomPermutation() throws BTreeException { - Integer currIndex = getNextRandPermutationIndex(); - bplusTree.add(currIndex, currIndex); + addNodeRandomPermutationBatch(1); + } + + @Benchmark + public void addNodeRandomPermutationBatch5() throws BTreeException { + addNodeRandomPermutationBatch(5); + } + + @Benchmark + public void addNodeRandomPermutationBatch10() throws BTreeException { + addNodeRandomPermutationBatch(10); + } + + @Benchmark + public void addNodeRandomPermutationBatch20() throws BTreeException { + addNodeRandomPermutationBatch(20); + } + + @Benchmark + public void addNodeRandomPermutationBatch30() throws BTreeException { + addNodeRandomPermutationBatch(30); + } + + @Benchmark + public void addNodeRandomPermutationBatch100() throws BTreeException { + addNodeRandomPermutationBatch(100); } @Benchmark From 4d7e85d0dd0712e88bb5a51cf475c42f176ff9c2 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Wed, 24 Jul 2019 08:29:05 +0430 Subject: [PATCH 20/26] improve tests --- src/test/java/bplustree/BplusTreeTest.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/test/java/bplustree/BplusTreeTest.java b/src/test/java/bplustree/BplusTreeTest.java index d59937d..525ba90 100644 --- a/src/test/java/bplustree/BplusTreeTest.java +++ b/src/test/java/bplustree/BplusTreeTest.java @@ -1,6 +1,7 @@ package bplustree; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import utility.CircularFifoQueue; import utility.SimpleCloneable; @@ -234,6 +235,7 @@ void shouldDisableCache() throws BTreeException { Assertions.assertEquals(1, bplusTree.getMiss() - initialMiss); } + @Disabled @org.junit.jupiter.api.Test void shouldCacheHitFind() throws BTreeException { if (bplusTree.cacheEnabled()) { @@ -247,6 +249,7 @@ void shouldCacheHitFind() throws BTreeException { } } + @Disabled @org.junit.jupiter.api.Test void shouldCacheMissFind() throws BTreeException { if (bplusTree.cacheEnabled()) { @@ -260,6 +263,7 @@ void shouldCacheMissFind() throws BTreeException { } } + @Disabled @org.junit.jupiter.api.Test void shouldCacheHitRemove() throws BTreeException { if (bplusTree.cacheEnabled()) { @@ -273,6 +277,7 @@ void shouldCacheHitRemove() throws BTreeException { } } + @Disabled @org.junit.jupiter.api.Test void shouldCacheMissRemove() throws BTreeException { if (bplusTree.cacheEnabled()) { @@ -286,6 +291,7 @@ void shouldCacheMissRemove() throws BTreeException { } } + @Disabled @org.junit.jupiter.api.Test void shouldCacheHitAdd() throws BTreeException { if (bplusTree.cacheEnabled()) { @@ -299,6 +305,7 @@ void shouldCacheHitAdd() throws BTreeException { } } + @Disabled @org.junit.jupiter.api.Test void shouldCacheMissAdd() throws BTreeException { if (bplusTree.cacheEnabled()) { @@ -319,6 +326,56 @@ void shouldCheckSampleDepth() { Assertions.assertEquals(-1, bplusTree.getSampleDepth()); } + @Test + void shouldCheckLastValueAndKey() throws BTreeException { + for (int i = MAXN - 1; i >= 0; --i) { + Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekLastKey()); + Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.peekLastValue()); + bplusTree.removeFrom(i); + } + + for (int i = 1; i < MAXN; i++) { + bplusTree.add(i, 2 * i); + Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekLastKey()); + Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.peekLastValue()); + } + bplusTree.add(0, 0); + + for (int i = MAXN - 1; i >= 0; --i) { + Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekLastKey()); + bplusTree.remove(i); + } + } + + @Test + void shouldCheckLastValueAndKeyLeafRoot() throws BTreeException { + int MAX = 5; + bplusTree = new BplusTree<>(true); + + for (int i = 1; i < MAX; i++) { + bplusTree.add(i, 2 * i); + Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekLastKey()); + Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.peekLastValue()); + } + + for (int i = MAX - 1; i > 0; --i) { + Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekLastKey()); + Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.peekLastValue()); + bplusTree.removeFrom(i); + } + + for (int i = 1; i < MAX; i++) { + bplusTree.add(i, 2 * i); + Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekLastKey()); + Assertions.assertEquals(Integer.valueOf(2 * i), bplusTree.peekLastValue()); + } + + for (int i = MAX - 1; i > 0; --i) { + Assertions.assertEquals(Integer.valueOf(i), bplusTree.peekLastKey()); + bplusTree.remove(i); + } + } + @Test void shouldIterateBackward() { Assertions.assertNull(new BplusTree().peekLast()); From b261c86802d636bc36e53f170cc5004c1bd63434 Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Wed, 24 Jul 2019 08:32:07 +0430 Subject: [PATCH 21/26] fix peekBackKey and make BplusTreeIterator public --- src/main/java/bplustree/BplusTree.java | 2 +- src/main/java/bplustree/BplusTreeLeafNode.java | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index 420592f..04d147f 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -163,7 +163,7 @@ public Value peekLastValue() { public Key peekLastKey() { if (lastNode == null) throw new RuntimeException("lastNode can not be null"); - return lastNode.peekKey(); + return lastNode.peekBackKey(); } public Value pop() throws BTreeException { Value poppedVal = _root.pop(); diff --git a/src/main/java/bplustree/BplusTreeLeafNode.java b/src/main/java/bplustree/BplusTreeLeafNode.java index 3dcfbd1..ec85015 100644 --- a/src/main/java/bplustree/BplusTreeLeafNode.java +++ b/src/main/java/bplustree/BplusTreeLeafNode.java @@ -4,7 +4,7 @@ import static utility.Utils.searchLeftmostKey; -class BplusTreeLeafNode, Value> extends BplusTreeNode { +public class BplusTreeLeafNode, Value> extends BplusTreeNode { protected CircularFifoQueue leaves; protected BplusTreeLeafNode next; protected BplusTreeLeafNode prev; @@ -199,6 +199,9 @@ public BplusTreeLeafNode peekLastNode() { public Key peekKey() { return keys.peekFront(); } + public Key peekBackKey() { + return keys.peekBack(); + } @Override public Value peekValue() { @@ -236,7 +239,7 @@ public class BplusTreeIterator implements Iterator{ private BplusTreeLeafNode node; private int index; - public BplusTreeIterator(BplusTreeLeafNode node, int index) { + BplusTreeIterator(BplusTreeLeafNode node, int index) { this.node = node; this.index = index; } From e1fe6d7a93e10dd8667f464741eed127dc1f4b5e Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Wed, 24 Jul 2019 08:33:34 +0430 Subject: [PATCH 22/26] comment currently unnecessary lines --- .../benchmark/AddNodeToBtreeBenchmark.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/test/java/benchmark/AddNodeToBtreeBenchmark.java b/src/test/java/benchmark/AddNodeToBtreeBenchmark.java index 09fc1bd..5667963 100644 --- a/src/test/java/benchmark/AddNodeToBtreeBenchmark.java +++ b/src/test/java/benchmark/AddNodeToBtreeBenchmark.java @@ -109,21 +109,21 @@ public void addNodeRandomPermutationBatch20() throws BTreeException { addNodeRandomPermutationBatch(20); } - @Benchmark - public void addNodeRandomPermutationBatch30() throws BTreeException { - addNodeRandomPermutationBatch(30); - } +// @Benchmark +// public void addNodeRandomPermutationBatch30() throws BTreeException { +// addNodeRandomPermutationBatch(30); +// } @Benchmark public void addNodeRandomPermutationBatch100() throws BTreeException { addNodeRandomPermutationBatch(100); } - @Benchmark - public void addNodeRandom() throws BTreeException { - Integer currIndex = getNextRandIndex(); - bplusTree.add(currIndex, currIndex); - } +// @Benchmark +// public void addNodeRandom() throws BTreeException { +// Integer currIndex = getNextRandIndex(); +// bplusTree.add(currIndex, currIndex); +// } @Benchmark public void addBatch1k() throws BTreeException { From dc982968f42f2d0e9e6c4ead005fcd456984ed1a Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Wed, 24 Jul 2019 15:11:13 +0430 Subject: [PATCH 23/26] disable cache and add bottomUpRemoveFrom --- src/main/java/bplustree/BplusTree.java | 63 +++++++++++++++------- src/main/java/bplustree/BplusTreeNode.java | 8 +++ 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index 04d147f..9ebc56d 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -68,8 +68,8 @@ public boolean cacheEnabled() { public BplusTreeBranchNode getRecentNode() { if (cacheDisabled) return null; - if (recentlyUsed != null && recentlyUsed.getParent() != null) - return recentlyUsed.getParent().getParent(); + if (recentlyUsed != null) + return recentlyUsed.getParent(); return null; } public int getHit() { @@ -94,10 +94,15 @@ public boolean isEmpty() { } public void add(Key key, Value value) throws BTreeException { - if (getRecentNode() != null && getRecentNode().isInRange(key)) { - ++hit; - getRecentNode().add(key, value); - } else { +// if (!cacheDisabled && lastNode.isInRange(key)) { +// ++hit; +// lastNode.add(key, value); +// } else +// if (getRecentNode() != null && getRecentNode().isInRange(key)) { +// ++hit; +// getRecentNode().add(key, value); +// } else + { _root.add(key, value); ++miss; } @@ -109,10 +114,15 @@ public void add(Key key, Value value) throws BTreeException { } public Value remove(Key key) throws BTreeException { Value result; - if (getRecentNode() != null && getRecentNode().isInRange(key)) { - result = getRecentNode().remove(key); - ++hit; - } else { +// if (!cacheDisabled && lastNode.isInRange(key)) { +// result = lastNode.remove(key); +// ++hit; +// } else +// if (getRecentNode() != null && getRecentNode().isInRange(key)) { +// result = getRecentNode().remove(key); +// ++hit; +// } else + { result = _root.remove(key); ++miss; } @@ -126,22 +136,36 @@ else if (lastNode.isEmpty()) return result; } + // TODO: CHECK Last Node public void removeFrom(Key key) throws BTreeException { - _root.removeFrom(key); + if (!cacheDisabled) { + lastNode.bottomUpRemoveFrom(key); + } else + _root.removeFrom(key); if (_root.isEmpty()) { - _root = new BplusTreeLeafNode(null, null, null, this); - lastNode = (BplusTreeLeafNode) _root; - recentlyUsed = null; + _root = new BplusTreeLeafNode<>(null, null, null, this); + lastNode = (BplusTreeLeafNode) _root; +// recentlyUsed = null; } - else if (lastNode.isEmpty()) + else if (lastNode.isEmpty() || lastNode.peekKey().compareTo(key) == 0) { + lastNode = lastNode.getPrev(); + } else if (lastNode.peekKey().compareTo(key) > 0) lastNode = _root.peekLastNode(); + +// if (recentlyUsed != null && recentlyUsed.peekKey().compareTo(key) > -1) +// recentlyUsed = lastNode; } public Value find(Key key) throws BTreeException { - if (getRecentNode() != null && getRecentNode().isInRange(key)) { - ++hit; - return (Value) getRecentNode().find(key); - } else { +// if (!cacheDisabled && lastNode.isInRange(key)) { +// ++hit; +// return lastNode.find(key); +// } else +// if (getRecentNode() != null && getRecentNode().isInRange(key)) { +// ++hit; +// return getRecentNode().find(key); +// } else + { ++miss; return _root.find(key); } @@ -170,6 +194,7 @@ public Value pop() throws BTreeException { if (_root.isEmpty()) { _root = new BplusTreeLeafNode(null, null, null, this); lastNode = (BplusTreeLeafNode) _root; +// recentlyUsed = lastNode; } return poppedVal; diff --git a/src/main/java/bplustree/BplusTreeNode.java b/src/main/java/bplustree/BplusTreeNode.java index f6fae66..140026b 100644 --- a/src/main/java/bplustree/BplusTreeNode.java +++ b/src/main/java/bplustree/BplusTreeNode.java @@ -30,6 +30,14 @@ public boolean isInRange(Key key) throws BTreeException { public abstract void add(Key searchKey, Value value) throws BTreeException; public abstract Value remove(Key searchKey) throws BTreeException; + + void bottomUpRemoveFrom(Key thresholdKey) throws BTreeException { + if (parent == null || isInRange(thresholdKey)) + removeFrom(thresholdKey); + else + parent.bottomUpRemoveFrom(thresholdKey); + } + public abstract void removeFrom(Key searchKey) throws BTreeException; public abstract Value find(Key searchKey) throws BTreeException; public abstract BplusTreeLeafNode.BplusTreeIterator peekLast(); From 58783c444dd197fd152f850e2f087a40a683359e Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 27 Jul 2019 15:26:12 +0430 Subject: [PATCH 24/26] tune capacity and disable capacity based tests --- src/main/java/bplustree/BplusTreeNode.java | 2 +- src/test/java/bplustree/BplusTreeTest.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/bplustree/BplusTreeNode.java b/src/main/java/bplustree/BplusTreeNode.java index 140026b..6e75753 100644 --- a/src/main/java/bplustree/BplusTreeNode.java +++ b/src/main/java/bplustree/BplusTreeNode.java @@ -3,7 +3,7 @@ import utility.CircularFifoQueue; public abstract class BplusTreeNode { - protected static final int CAPACITY = 127; + protected static final int CAPACITY = 63; protected BplusTreeBranchNode parent; protected CircularFifoQueue keys; protected Key LeftRangeKey; diff --git a/src/test/java/bplustree/BplusTreeTest.java b/src/test/java/bplustree/BplusTreeTest.java index 525ba90..6553b85 100644 --- a/src/test/java/bplustree/BplusTreeTest.java +++ b/src/test/java/bplustree/BplusTreeTest.java @@ -26,6 +26,7 @@ void setUp() throws BTreeException { bplusTree.add(0, 0); } + @Disabled @org.junit.jupiter.api.Test void shouldBeInRange() throws NoSuchFieldException, IllegalAccessException, BTreeException { Field _root = BplusTree.class.getDeclaredField("_root"); @@ -319,6 +320,7 @@ void shouldCacheMissAdd() throws BTreeException { } } + @Disabled @org.junit.jupiter.api.Test void shouldCheckSampleDepth() { Assertions.assertEquals(3, bplusTree.getSampleDepth()); From 197c2b8b9784a5b259289b0ba8ca25f32020920c Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 27 Jul 2019 15:26:39 +0430 Subject: [PATCH 25/26] add an optimized version of add --- src/main/java/bplustree/BplusTree.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/bplustree/BplusTree.java b/src/main/java/bplustree/BplusTree.java index 9ebc56d..e79b978 100644 --- a/src/main/java/bplustree/BplusTree.java +++ b/src/main/java/bplustree/BplusTree.java @@ -93,6 +93,17 @@ public boolean isEmpty() { return _root.isEmpty(); } + public void addWithCacheLastRecent(Key key, Value value) throws BTreeException { + if (recentlyUsed != null && recentlyUsed.isInRange(key)) { + ++hit; + recentlyUsed.add(key, value); + if (_root.getParent() != null) + _root = _root.getParent(); + if (lastNode.getNext() != null) + lastNode = lastNode.getNext(); + } else + add(key, value); + } public void add(Key key, Value value) throws BTreeException { // if (!cacheDisabled && lastNode.isInRange(key)) { // ++hit; From 32be9fa83b2e4896a32fba137d2423372ab7c9aa Mon Sep 17 00:00:00 2001 From: Abtin Bateni Date: Sat, 17 Oct 2020 21:29:14 -0700 Subject: [PATCH 26/26] add BTree photo in README.md --- BTree.png | Bin 0 -> 16420 bytes README.md | 2 ++ 2 files changed, 2 insertions(+) create mode 100644 BTree.png diff --git a/BTree.png b/BTree.png new file mode 100644 index 0000000000000000000000000000000000000000..c70a0a73ff414bc95edc71346d582e4108b645ad GIT binary patch literal 16420 zcmeHucT|&2x9<~*6eY9(fdb$ z1kvg0XqZ6IF);{&MN*T2Cso&FoFRzjoU6Jz#=+6a*89dCYfoo8aaVhL2=dEHNp!|M z#Ir{9ZwHPaKfYRZ$NXNhup{hSlWs*mnf|({K4T@19R+W6q>N@l9u8Ju!e^#Y$$&sq z=3U{VMoFF2#A3GE6`2<&H{S#XUHI`{aWN}{T!oPmQqhE&m{10w+5C-kTuxFo^CYEZ z+*}MY2vRr> zi3R}ykS950?qMB>gv^bt15ZQd`w*H6LQtvXcLshnp4tf}l0m5PO-hwIluv{Jr0dD2UnN4;l5izCJ&C@VIPo5(!%P$Z&dA!T1z7#=rpPY zV%51%R)P~)3S~?W;U5WQuDvfup@O;ro$gSl-Qc0xvZX?((yZRUk0Jl{fkNy9yqIw6 zeLG7nykioH z%oiH0{AGz^-IH@JTLPZ!bx~jRo7Ho8C5$^z90(CTh(EFFzKN!5@zsLZOOqx~u#Tk) zLur{R6Gr9vIr)=$Mi}%!hwGROm4&=m%&Rn?ET0TwBr(;fxa`x-Z?<<}ff7yOK`Do^ zhpJ&h`GT_oOk!1rg=U5N4jOZ^_xmvRBU0xiw@#+SO2;b3Di~Ll)R)|MDO@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Ex7ppQnre12zH-&j0`b literal 0 HcmV?d00001 diff --git a/README.md b/README.md index 1ba4246..0a8dc19 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,5 @@ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/18082c8908db4692bd702aa448734b33)](https://www.codacy.com/app/abateni/BPlusTree?utm_source=github.com&utm_medium=referral&utm_content=mr-bat/BPlusTree&utm_campaign=Badge_Grade) Implementing BPlusTree with Java. + +![Sample BPlus Tree](https://github.com/mr-bat/BPlusTree/blob/master/BTree.png?raw=true)