Skip to content

Commit

Permalink
refactor: renamed strategy to B(b)inaryObjectWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
sepgh committed Jun 11, 2024
1 parent 28ba38b commit 6024173
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
public class BPlusTreeIndexCreateOperation<K extends Comparable<K>, V extends Comparable<V>> {
private final int degree;
private final IndexIOSession<K> indexIOSession;
private final BinaryObjectWrapper<K> keyStrategy;
private final BinaryObjectWrapper<V> valueStrategy;
private final BinaryObjectWrapper<K> keyBinaryObjectWrapper;
private final BinaryObjectWrapper<V> valueBinaryObjectWrapper;

public BPlusTreeIndexCreateOperation(int degree, IndexIOSession<K> indexIOSession, BinaryObjectWrapper<K> keyStrategy, BinaryObjectWrapper<V> valueStrategy) {
public BPlusTreeIndexCreateOperation(int degree, IndexIOSession<K> indexIOSession, BinaryObjectWrapper<K> keyBinaryObjectWrapper, BinaryObjectWrapper<V> valueBinaryObjectWrapper) {
this.degree = degree;
this.indexIOSession = indexIOSession;
this.keyStrategy = keyStrategy;
this.valueStrategy = valueStrategy;
this.keyBinaryObjectWrapper = keyBinaryObjectWrapper;
this.valueBinaryObjectWrapper = valueBinaryObjectWrapper;
}

public AbstractTreeNode<K> addIndex(AbstractTreeNode<K> root, K identifier, V value) throws ExecutionException, InterruptedException, IOException, BinaryObjectWrapper.InvalidBinaryObjectWrapperValue {
Expand All @@ -44,15 +44,15 @@ public AbstractTreeNode<K> addIndex(AbstractTreeNode<K> root, K identifier, V va
/* current node is a leaf which should handle storing the data */

/* If current node has space, store and exit */
if (currentNode.getKeyList(degree, valueStrategy.size()).size() < (degree - 1)){
if (currentNode.getKeyList(degree, valueBinaryObjectWrapper.size()).size() < (degree - 1)){
((AbstractLeafTreeNode<K, V>) currentNode).addKeyValue(identifier, value, degree);
indexIOSession.write(currentNode);
indexIOSession.commit();
return currentNode;
}

/* Current node didn't have any space, so let's create a sibling and split */
AbstractLeafTreeNode<K, V> newSiblingLeafNode = new AbstractLeafTreeNode<>(indexIOSession.getIndexStorageManager().getEmptyNode(), keyStrategy, valueStrategy);
AbstractLeafTreeNode<K, V> newSiblingLeafNode = new AbstractLeafTreeNode<>(indexIOSession.getIndexStorageManager().getEmptyNode(), keyBinaryObjectWrapper, valueBinaryObjectWrapper);
List<LeafClusterTreeNode.KeyValue<K, V>> passingKeyValues = ((AbstractLeafTreeNode<K, V>) currentNode).split(identifier, value, degree);
newSiblingLeafNode.setKeyValues(passingKeyValues, degree);
indexIOSession.write(newSiblingLeafNode); // we want the node to have a value so that we can fix siblings
Expand All @@ -61,12 +61,12 @@ public AbstractTreeNode<K> addIndex(AbstractTreeNode<K> root, K identifier, V va
indexIOSession.write(newSiblingLeafNode);
indexIOSession.write(currentNode);

answer = currentNode.getKeyList(degree, valueStrategy.size()).contains(identifier) ? currentNode : newSiblingLeafNode;
answer = currentNode.getKeyList(degree, valueBinaryObjectWrapper.size()).contains(identifier) ? currentNode : newSiblingLeafNode;

/* this leaf doesn't have a parent! create one and deal with it right here! */
if (path.size() == 1) {
currentNode.unsetAsRoot();
InternalTreeNode<K> newRoot = new InternalTreeNode<>(indexIOSession.getIndexStorageManager().getEmptyNode(), keyStrategy);
InternalTreeNode<K> newRoot = new InternalTreeNode<>(indexIOSession.getIndexStorageManager().getEmptyNode(), keyBinaryObjectWrapper);
newRoot.setAsRoot();
newRoot.addChildPointers(
passingKeyValues.getFirst().key(),
Expand All @@ -91,7 +91,7 @@ public AbstractTreeNode<K> addIndex(AbstractTreeNode<K> root, K identifier, V va
if (currentInternalTreeNode.getKeyList(degree).size() < degree - 1) {
/* current internal node can store the key */
int indexOfAddedKey = currentInternalTreeNode.addKey(idForParentToStore, degree);
if (newChildForParent.getKeyList(degree, valueStrategy.size()).getFirst().compareTo(idForParentToStore) < 0){
if (newChildForParent.getKeyList(degree, valueBinaryObjectWrapper.size()).getFirst().compareTo(idForParentToStore) < 0){
currentInternalTreeNode.addChildAtIndex(indexOfAddedKey, newChildForParent.getPointer());
} else {
currentInternalTreeNode.addChildAtIndex(indexOfAddedKey + 1, newChildForParent.getPointer());
Expand All @@ -108,14 +108,14 @@ public AbstractTreeNode<K> addIndex(AbstractTreeNode<K> root, K identifier, V va
idForParentToStore = firstPassingChildPointers.getKey();
passingChildPointers.removeFirst();

InternalTreeNode<K> newInternalSibling = new InternalTreeNode<K>(indexIOSession.getIndexStorageManager().getEmptyNode(), keyStrategy);
InternalTreeNode<K> newInternalSibling = new InternalTreeNode<K>(indexIOSession.getIndexStorageManager().getEmptyNode(), keyBinaryObjectWrapper);
newInternalSibling.setChildPointers(passingChildPointers, degree, true);
indexIOSession.write(newInternalSibling);

// Current node was root and needs a new parent
if (currentInternalTreeNode.isRoot()){
currentInternalTreeNode.unsetAsRoot();
InternalTreeNode<K> newRoot = new InternalTreeNode<K>(indexIOSession.getIndexStorageManager().getEmptyNode(), keyStrategy);
InternalTreeNode<K> newRoot = new InternalTreeNode<K>(indexIOSession.getIndexStorageManager().getEmptyNode(), keyBinaryObjectWrapper);
newRoot.setAsRoot();

newRoot.addChildPointers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public class BPlusTreeIndexDeleteOperation<K extends Comparable<K>, V extends Co
private final int table;
private final IndexIOSession<K> indexIOSession;
private final int minKeys;
private final BinaryObjectWrapper<V> valueStrategy;
private final BinaryObjectWrapper<V> valueBinaryObjectWrapper;
private final NodeFactory<K> nodeFactory;

public BPlusTreeIndexDeleteOperation(int degree, int table, IndexIOSession<K> indexIOSession, BinaryObjectWrapper<V> valueStrategy, NodeFactory<K> nodeFactory) {
public BPlusTreeIndexDeleteOperation(int degree, int table, IndexIOSession<K> indexIOSession, BinaryObjectWrapper<V> valueBinaryObjectWrapper, NodeFactory<K> nodeFactory) {
this.degree = degree;
this.table = table;
this.indexIOSession = indexIOSession;
this.minKeys = (degree - 1) / 2;
this.valueStrategy = valueStrategy;
this.valueBinaryObjectWrapper = valueBinaryObjectWrapper;
this.nodeFactory = nodeFactory;
}

Expand Down Expand Up @@ -74,14 +74,14 @@ private void deleteInternalNode(InternalTreeNode<K> parent, InternalTreeNode<K>
List<Pointer> childrenList = node.getChildrenList();
if (idx != 0){
AbstractTreeNode<K> leftIDXChild = indexIOSession.read(childrenList.get(idx - 1));
if (leftIDXChild.getKeyList(degree, valueStrategy.size()).size() >= minKeys){
if (leftIDXChild.getKeyList(degree, valueBinaryObjectWrapper.size()).size() >= minKeys){
K pred = this.getPredecessor(node, idx);
node.setKey(idx, pred);
indexIOSession.update(node);
}
} else {
AbstractTreeNode<K> rightIDXChild = indexIOSession.read(childrenList.get(idx + 1));
if (rightIDXChild.getKeyList(degree, valueStrategy.size()).size() >= minKeys) {
if (rightIDXChild.getKeyList(degree, valueBinaryObjectWrapper.size()).size() >= minKeys) {
K succ = getSuccessor(node, idx);
node.setKey(idx, succ);
indexIOSession.update(node);
Expand All @@ -95,17 +95,17 @@ private void deleteInternalNode(InternalTreeNode<K> parent, InternalTreeNode<K>
private K getPredecessor(InternalTreeNode<K> node, int idx) throws ExecutionException, InterruptedException, IOException {
AbstractTreeNode<K> cur = indexIOSession.read(node.getChildrenList().get(idx));
while (!cur.isLeaf()) {
cur = indexIOSession.read(node.getChildrenList().get(cur.getKeyList(degree, valueStrategy.size()).size()));
cur = indexIOSession.read(node.getChildrenList().get(cur.getKeyList(degree, valueBinaryObjectWrapper.size()).size()));
}
return cur.getKeyList(degree, valueStrategy.size()).getLast();
return cur.getKeyList(degree, valueBinaryObjectWrapper.size()).getLast();
}

private K getSuccessor(InternalTreeNode<K> node, int idx) throws ExecutionException, InterruptedException, IOException {
AbstractTreeNode<K> cur = indexIOSession.read(node.getChildrenList().get(idx + 1));
while (!cur.isLeaf()) {
cur = indexIOSession.read(((InternalTreeNode<K>) cur).getChildrenList().getFirst());
}
return cur.getKeyList(degree, valueStrategy.size()).getFirst();
return cur.getKeyList(degree, valueBinaryObjectWrapper.size()).getFirst();
}

private void checkInternalNode(InternalTreeNode<K> internalTreeNode, List<AbstractTreeNode<K>> path, int nodeIndex, K identifier) throws ExecutionException, InterruptedException, IOException, BinaryObjectWrapper.InvalidBinaryObjectWrapperValue {
Expand Down Expand Up @@ -136,7 +136,7 @@ private void fillRootAtIndex(InternalTreeNode<K> internalTreeNode, int indexOfKe
table,
degree,
nodeFactory,
valueStrategy
valueBinaryObjectWrapper
);

internalTreeNode.setKey(indexOfKey, leafTreeNode.getKeyList(degree).getLast());
Expand Down Expand Up @@ -165,7 +165,7 @@ private void fillNode(AbstractTreeNode<K> currentNode, InternalTreeNode<K> paren

private boolean tryBorrowRight(InternalTreeNode<K> parentNode, int idx, AbstractTreeNode<K> child) throws ExecutionException, InterruptedException, IOException, BinaryObjectWrapper.InvalidBinaryObjectWrapperValue {
AbstractTreeNode<K> sibling = indexIOSession.read(parentNode.getChildrenList().get(idx + 1));
if (sibling.getKeyList(degree, valueStrategy.size()).size() > minKeys){
if (sibling.getKeyList(degree, valueBinaryObjectWrapper.size()).size() > minKeys){
this.borrowFromNext(parentNode, idx, child);
return true;
}
Expand All @@ -174,7 +174,7 @@ private boolean tryBorrowRight(InternalTreeNode<K> parentNode, int idx, Abstract

private boolean tryBorrowLeft(InternalTreeNode<K> parentNode, int idx, AbstractTreeNode<K> child) throws ExecutionException, InterruptedException, IOException, BinaryObjectWrapper.InvalidBinaryObjectWrapperValue {
AbstractTreeNode<K> sibling = indexIOSession.read(parentNode.getChildrenList().get(idx - 1));
if (sibling.getKeyList(degree, valueStrategy.size()).size() > minKeys){
if (sibling.getKeyList(degree, valueBinaryObjectWrapper.size()).size() > minKeys){
this.borrowFromPrev(parentNode, idx, child);
return true;
}
Expand Down Expand Up @@ -319,7 +319,7 @@ private void merge(InternalTreeNode<K> parent, AbstractTreeNode<K> child, int id
AbstractTreeNode<K> sibling = indexIOSession.read(parent.getChildrenList().get(siblingIndex));
AbstractTreeNode<K> toRemove = sibling;

if (sibling.getKeyList(degree, valueStrategy.size()).size() > child.getKeyList(degree, valueStrategy.size()).size()){
if (sibling.getKeyList(degree, valueBinaryObjectWrapper.size()).size() > child.getKeyList(degree, valueBinaryObjectWrapper.size()).size()){
// Sibling has more keys, lets merge from child to sibling and remove child
AbstractTreeNode<K> temp = child;
child = sibling;
Expand All @@ -339,7 +339,7 @@ private void merge(InternalTreeNode<K> parent, AbstractTreeNode<K> child, int id

InternalTreeNode<K> childInternalTreeNode = (InternalTreeNode<K>) child;
List<K> childKeyList = new ArrayList<>(childInternalTreeNode.getKeyList(degree));
childKeyList.addAll(sibling.getKeyList(degree, valueStrategy.size()));
childKeyList.addAll(sibling.getKeyList(degree, valueBinaryObjectWrapper.size()));
childKeyList.sort(K::compareTo);
childInternalTreeNode.setKeys(childKeyList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,35 @@ public class BPlusTreeIndexManager<K extends Comparable<K>, V extends Comparable
private final IndexStorageManager indexStorageManager;
private final IndexIOSessionFactory indexIOSessionFactory;
private final int degree;
private final BinaryObjectWrapper<K> keyStrategy;
private final BinaryObjectWrapper<V> valueStrategy;
private final BinaryObjectWrapper<K> keyBinaryObjectWrapper;
private final BinaryObjectWrapper<V> valueBinaryObjectWrapper;
private final NodeFactory<K> nodeFactory;

public BPlusTreeIndexManager(int degree, IndexStorageManager indexStorageManager, IndexIOSessionFactory indexIOSessionFactory, BinaryObjectWrapper<K> keyStrategy, BinaryObjectWrapper<V> valueStrategy, NodeFactory<K> nodeFactory){
public BPlusTreeIndexManager(int degree, IndexStorageManager indexStorageManager, IndexIOSessionFactory indexIOSessionFactory, BinaryObjectWrapper<K> keyBinaryObjectWrapper, BinaryObjectWrapper<V> valueBinaryObjectWrapper, NodeFactory<K> nodeFactory){
this.degree = degree;
this.indexStorageManager = indexStorageManager;
this.indexIOSessionFactory = indexIOSessionFactory;
this.keyStrategy = keyStrategy;
this.valueStrategy = valueStrategy;
this.keyBinaryObjectWrapper = keyBinaryObjectWrapper;
this.valueBinaryObjectWrapper = valueBinaryObjectWrapper;
this.nodeFactory = nodeFactory;
}

public BPlusTreeIndexManager(int degree, IndexStorageManager indexStorageManager, BinaryObjectWrapper<K> keyStrategy, BinaryObjectWrapper<V> valueStrategy, NodeFactory<K> nodeFactory){
this(degree, indexStorageManager, ImmediateCommitIndexIOSession.Factory.getInstance(), keyStrategy, valueStrategy, nodeFactory);
public BPlusTreeIndexManager(int degree, IndexStorageManager indexStorageManager, BinaryObjectWrapper<K> keyBinaryObjectWrapper, BinaryObjectWrapper<V> valueBinaryObjectWrapper, NodeFactory<K> nodeFactory){
this(degree, indexStorageManager, ImmediateCommitIndexIOSession.Factory.getInstance(), keyBinaryObjectWrapper, valueBinaryObjectWrapper, nodeFactory);
}

@Override
public AbstractTreeNode<K> addIndex(int table, K identifier, V value) throws ExecutionException, InterruptedException, IOException, BinaryObjectWrapper.InvalidBinaryObjectWrapperValue {
IndexIOSession<K> indexIOSession = this.indexIOSessionFactory.create(indexStorageManager, table, nodeFactory);
AbstractTreeNode<K> root = getRoot(indexIOSession);
return new BPlusTreeIndexCreateOperation<>(degree, indexIOSession, keyStrategy, valueStrategy).addIndex(root, identifier, value);
return new BPlusTreeIndexCreateOperation<>(degree, indexIOSession, keyBinaryObjectWrapper, valueBinaryObjectWrapper).addIndex(root, identifier, value);
}

@Override
public Optional<V> getIndex(int table, K identifier) throws ExecutionException, InterruptedException, IOException {
IndexIOSession<K> indexIOSession = this.indexIOSessionFactory.create(indexStorageManager, table, nodeFactory);

AbstractLeafTreeNode<K, V> baseTreeNode = BPlusTreeUtils.getResponsibleNode(indexStorageManager, getRoot(indexIOSession), identifier, table, degree, nodeFactory, valueStrategy);
AbstractLeafTreeNode<K, V> baseTreeNode = BPlusTreeUtils.getResponsibleNode(indexStorageManager, getRoot(indexIOSession), identifier, table, degree, nodeFactory, valueBinaryObjectWrapper);
for (AbstractLeafTreeNode.KeyValue<K, V> entry : baseTreeNode.getKeyValueList(degree)) {
if (entry.key() == identifier)
return Optional.of(entry.value());
Expand All @@ -63,7 +63,7 @@ public Optional<V> getIndex(int table, K identifier) throws ExecutionException,
public boolean removeIndex(int table, K identifier) throws ExecutionException, InterruptedException, IOException {
IndexIOSession<K> indexIOSession = this.indexIOSessionFactory.create(indexStorageManager, table, nodeFactory);
AbstractTreeNode<K> root = getRoot(indexIOSession);
return new BPlusTreeIndexDeleteOperation<>(degree, table, indexIOSession, valueStrategy, nodeFactory).removeIndex(root, identifier);
return new BPlusTreeIndexDeleteOperation<>(degree, table, indexIOSession, valueBinaryObjectWrapper, nodeFactory).removeIndex(root, identifier);
}

@Override
Expand All @@ -74,19 +74,19 @@ public int size(int table) throws InterruptedException, ExecutionException, IOEx

AbstractTreeNode<K> root = nodeFactory.fromNodeData(optionalNodeData.get());
if (root.isLeaf()){
return root.getKeyList(degree, valueStrategy.size()).size();
return root.getKeyList(degree, valueBinaryObjectWrapper.size()).size();
}

AbstractTreeNode<K> curr = root;
while (!curr.isLeaf()) {
curr = IndexTreeNodeIO.read(indexStorageManager, table, ((InternalTreeNode<K>) curr).getChildrenList().getFirst(), nodeFactory);
}

int size = curr.getKeyList(degree, valueStrategy.size()).size();
int size = curr.getKeyList(degree, valueBinaryObjectWrapper.size()).size();
Optional<Pointer> optionalNext = ((LeafClusterTreeNode<K>) curr).getNextSiblingPointer(degree);
while (optionalNext.isPresent()){
AbstractTreeNode<K> nextNode = IndexTreeNodeIO.read(indexStorageManager, table, optionalNext.get(), nodeFactory);
size += nextNode.getKeyList(degree, valueStrategy.size()).size();
size += nextNode.getKeyList(degree, valueBinaryObjectWrapper.size()).size();
optionalNext = ((LeafClusterTreeNode<K>) nextNode).getNextSiblingPointer(degree);
}

Expand Down
Loading

0 comments on commit 6024173

Please sign in to comment.