Skip to content

Commit

Permalink
feat(temp): starting removeIndex implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sepgh committed May 12, 2024
1 parent 5ee397b commit cbb596c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 35 deletions.
121 changes: 87 additions & 34 deletions src/main/java/com/github/sepgh/internal/tree/BTreeIndexManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@ public BTreeIndexManager(int order, IndexStorageManager indexStorageManager){
this.indexStorageManager = indexStorageManager;
}

private BaseTreeNode getRoot(int table) throws ExecutionException, InterruptedException {
Optional<IndexStorageManager.NodeData> optionalNodeData = indexStorageManager.getRoot(table).get();
if (optionalNodeData.isPresent()){
BaseTreeNode root = BaseTreeNode.fromBytes(optionalNodeData.get().bytes());
root.setNodePointer(optionalNodeData.get().pointer());
return root;
}

byte[] emptyNode = indexStorageManager.getEmptyNode();
LeafTreeNode leafTreeNode = (LeafTreeNode) BaseTreeNode.fromBytes(emptyNode, BaseTreeNode.NodeType.LEAF);
leafTreeNode.setAsRoot();

IndexStorageManager.NodeData nodeData = indexStorageManager.fillRoot(table, leafTreeNode.getData()).get();
leafTreeNode.setNodePointer(nodeData.pointer());
return leafTreeNode;
}

@Override
public BaseTreeNode addIndex(int table, long identifier, Pointer pointer) throws ExecutionException, InterruptedException, IOException {

Expand Down Expand Up @@ -174,6 +157,93 @@ public BaseTreeNode addIndex(int table, long identifier, Pointer pointer) throws
throw new RuntimeException("Logic error: probably failed to store index?");
}

@Override
public Optional<Pointer> getIndex(int table, long identifier) throws ExecutionException, InterruptedException {
Optional<LeafTreeNode> optionalBaseTreeNode = this.getResponsibleNode(table, getRoot(table), identifier);
if (optionalBaseTreeNode.isPresent()){
for (Map.Entry<Long, Pointer> entry : optionalBaseTreeNode.get().keyValueList()) {
if (entry.getKey() == identifier)
return Optional.of(entry.getValue());
}
}
return Optional.empty();
}

@Override
public boolean removeIndex(int table, long identifier) throws ExecutionException, InterruptedException {
BaseTreeNode root = getRoot(table);

List<BaseTreeNode> path = new LinkedList<>();
getPathToResponsibleNode(table, path, root, identifier);


for (int i = 0; i < path.size(); i++){
if (i == 0){
/*
We are at the leaf which holds the key
Lets loop over key values, and see if we find a key matching the identifier
If we do, we remove it, otherwise, we return false since apparently the identifier does not exist
*/
LeafTreeNode leafNode = (LeafTreeNode) path.get(i);
boolean deletedAny = false;
int j = 0;
List<Map.Entry<Long, Pointer>> keyValueList = leafNode.keyValueList();
for (Map.Entry<Long, Pointer> entry : keyValueList) {
if (entry.getKey().equals(identifier)) {
leafNode.removeKeyValueAtIndex(j);
keyValueList.remove(i);
deletedAny = true;
break;
}
j++;
}

if (!deletedAny)
return false;


/*
If size of the key values size is now one, then we went under the minimum number of keys
If this leaf is a root, it's ok, we are finished since there was only one node in the tree
Otherwise, we should remove this node and move the key to another node
*/
if (leafNode.isRoot())
return true;

if (keyValueList.size() == 1) {
Optional<Pointer> optionalPointer = leafNode.getPrevious();
if (optionalPointer.isPresent()){
LeafTreeNode previousLeafNode = (LeafTreeNode) BaseTreeNode.fromNodeData(
indexStorageManager.readNode(table, optionalPointer.get()).get()
);
if (previousLeafNode.keyList().size() == order) {
// Previous key is full
}
}

}

}
}
return false;
}

private BaseTreeNode getRoot(int table) throws ExecutionException, InterruptedException {
Optional<IndexStorageManager.NodeData> optionalNodeData = indexStorageManager.getRoot(table).get();
if (optionalNodeData.isPresent()){
BaseTreeNode root = BaseTreeNode.fromBytes(optionalNodeData.get().bytes());
root.setNodePointer(optionalNodeData.get().pointer());
return root;
}

byte[] emptyNode = indexStorageManager.getEmptyNode();
LeafTreeNode leafTreeNode = (LeafTreeNode) BaseTreeNode.fromBytes(emptyNode, BaseTreeNode.NodeType.LEAF);
leafTreeNode.setAsRoot();

IndexStorageManager.NodeData nodeData = indexStorageManager.fillRoot(table, leafTreeNode.getData()).get();
leafTreeNode.setNodePointer(nodeData.pointer());
return leafTreeNode;
}
private ChildSplitResults splitChildren(InternalTreeNode node, long identifier, Pointer pointer) {

int mid = order / 2;
Expand Down Expand Up @@ -248,23 +318,6 @@ private List<Map.Entry<Long, Pointer>> splitKeyValues(LeafTreeNode node, long id
return pass;
}

@Override
public Optional<Pointer> getIndex(int table, long identifier) throws ExecutionException, InterruptedException {
Optional<LeafTreeNode> optionalBaseTreeNode = this.getResponsibleNode(table, getRoot(table), identifier);
if (optionalBaseTreeNode.isPresent()){
for (Map.Entry<Long, Pointer> entry : optionalBaseTreeNode.get().keyValueList()) {
if (entry.getKey() == identifier)
return Optional.of(entry.getValue());
}
}
return Optional.empty();
}

@Override
public boolean removeIndex(int table, long identifier) {
return false;
}

private void fixSiblingPointers(int table, LeafTreeNode currentNode, LeafTreeNode newLeafTreeNode) throws ExecutionException, InterruptedException {
Optional<Pointer> optionalCurrentNext = currentNode.getNext();
currentNode.setNext(newLeafTreeNode.getNodePointer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
public interface IndexManager {
BaseTreeNode addIndex(int table, long identifier, Pointer pointer) throws ExecutionException, InterruptedException, IOException;
Optional<Pointer> getIndex(int table, long identifier) throws ExecutionException, InterruptedException;
boolean removeIndex(int table, long identifier);
boolean removeIndex(int table, long identifier) throws ExecutionException, InterruptedException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.sepgh.internal.tree.node;

import com.github.sepgh.internal.storage.IndexStorageManager;
import com.github.sepgh.internal.tree.Pointer;
import com.github.sepgh.internal.tree.TreeNodeUtils;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -34,6 +35,12 @@ public boolean isLeaf(){ // 0 0 & 0 0
return (data[0] & TYPE_LEAF_NODE_BIT) == TYPE_LEAF_NODE_BIT;
}


public static BaseTreeNode fromNodeData(IndexStorageManager.NodeData nodeData) {
BaseTreeNode treeNode = BaseTreeNode.fromBytes(nodeData.bytes());
treeNode.setNodePointer(nodeData.pointer());
return treeNode;
}
public static BaseTreeNode fromBytes(byte[] data, NodeType type){
if (type == NodeType.INTERNAL){
return new InternalTreeNode(data);
Expand Down

0 comments on commit cbb596c

Please sign in to comment.