Skip to content

Commit

Permalink
refactor: finished todo and applied function name cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sepgh committed May 5, 2024
1 parent 55fc75d commit 046caec
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import com.github.sepgh.internal.tree.node.BaseTreeNode;
import com.github.sepgh.internal.tree.node.InternalTreeNode;
import com.github.sepgh.internal.tree.node.LeafTreeNode;
import com.google.common.hash.HashCode;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class BTreeIndexManager implements IndexManager {
Expand Down Expand Up @@ -76,7 +74,6 @@ public BaseTreeNode addIndex(int table, long identifier, Pointer pointer) throws
}


// Todo: fixing siblings could potentially be faster. We could write new node, and "onComplete" update current node
IndexStorageManager.NodeData newLeafNodeData = indexStorageManager.writeNewNode(table, newLeafTreeNode.toBytes()).get();
newLeafTreeNode.setNodePointer(newLeafNodeData.pointer());
fixSiblingPointers(table, (LeafTreeNode) currentNode, newLeafTreeNode);
Expand Down Expand Up @@ -275,7 +272,7 @@ private void fixSiblingPointers(int table, LeafTreeNode currentNode, LeafTreeNod
if (optionalCurrentNext.isPresent()){
newLeafTreeNode.setNext(optionalCurrentNext.get());
IndexStorageManager.NodeData nodeData = indexStorageManager.readNode(table, optionalCurrentNext.get()).get();
BaseTreeNode baseTreeNode = BaseTreeNode.fromBytes(nodeData.bytes());
BaseTreeNode baseTreeNode = BaseTreeNode.fromBytes(nodeData.bytes(), BaseTreeNode.NodeType.LEAF);
((LeafTreeNode) baseTreeNode).setPrevious(newLeafTreeNode.getNodePointer());
indexStorageManager.updateNode(table, baseTreeNode.getData(), nodeData.pointer()).get();
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/github/sepgh/internal/tree/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ public class Pointer {
private long position;
private int chunk;

public static Pointer fromByteArray(byte[] bytes, int position){
public static Pointer fromBytes(byte[] bytes, int position){
return new Pointer(
bytes[position],
BinaryUtils.bytesToLong(bytes, position + 1),
BinaryUtils.bytesToInteger(bytes, position + 1 + Long.BYTES)
);
}

public static Pointer fromByteArray(byte[] bytes){
return Pointer.fromByteArray(bytes, 0);
public static Pointer fromBytes(byte[] bytes){
return Pointer.fromBytes(bytes, 0);
}

public byte[] toByteArray() {
public byte[] toBytes() {
byte[] bytes = new byte[BYTES];
bytes[0] = type;
System.arraycopy(Longs.toByteArray(position), 0, bytes, 1, Long.BYTES);
Expand All @@ -45,7 +45,7 @@ public byte[] toByteArray() {
}

// A quick way to write the current pointer into a specific position of a byte array (which is node itself)
public void fillByteArrayWithPointer(byte[] source, int position) {
public void fillBytes(byte[] source, int position) {
source[position] = type;
System.arraycopy(Longs.toByteArray(this.position), 0, source, position + 1, Long.BYTES);
System.arraycopy(Ints.toByteArray(chunk), 0, source, position + 1 + Long.BYTES, Integer.BYTES);
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/github/sepgh/internal/tree/TreeNodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static boolean hasChildPointerAtIndex(BaseTreeNode treeNode, int index){
* @param index to check child pointer
* @return Pointer to child node at index
*/public static Pointer getChildPointerAtIndex(BaseTreeNode treeNode, int index){
return Pointer.fromByteArray(treeNode.getData(), OFFSET_TREE_NODE_FLAGS_END + (index * (Pointer.BYTES + Long.BYTES)));
return Pointer.fromBytes(treeNode.getData(), OFFSET_TREE_NODE_FLAGS_END + (index * (Pointer.BYTES + Long.BYTES)));
}

public static void removeChildAtIndex(BaseTreeNode treeNode, int index) {
Expand All @@ -56,10 +56,10 @@ public static void removeChildAtIndex(BaseTreeNode treeNode, int index) {
*/
public static void setPointerToChild(BaseTreeNode treeNode, int index, Pointer pointer){
if (index == 0){
System.arraycopy(pointer.toByteArray(), 0, treeNode.getData(), OFFSET_TREE_NODE_FLAGS_END, Pointer.BYTES);
System.arraycopy(pointer.toBytes(), 0, treeNode.getData(), OFFSET_TREE_NODE_FLAGS_END, Pointer.BYTES);
} else {
System.arraycopy(
pointer.toByteArray(),
pointer.toBytes(),
0,
treeNode.getData(),
OFFSET_TREE_NODE_FLAGS_END + (index * (Pointer.BYTES + Long.BYTES)),
Expand Down Expand Up @@ -136,7 +136,7 @@ public static Map.Entry<Long, Pointer> getKeyValuePointerAtIndex(BaseTreeNode tr
int keyStartIndex = getKeyStartOffset(treeNode, index);
return new AbstractMap.SimpleImmutableEntry<>(
BinaryUtils.bytesToLong(treeNode.getData(), keyStartIndex),
Pointer.fromByteArray(treeNode.getData(), keyStartIndex + Long.BYTES)
Pointer.fromBytes(treeNode.getData(), keyStartIndex + Long.BYTES)
);
}

Expand All @@ -149,7 +149,7 @@ public static void setKeyValueAtIndex(BaseTreeNode treeNode, int index, long key
Long.BYTES
);

pointer.fillByteArrayWithPointer(
pointer.fillBytes(
treeNode.getData(),
OFFSET_LEAF_NODE_KEY_BEGIN + (index * (SIZE_LEAF_NODE_KEY_POINTER)) + Long.BYTES
);
Expand Down Expand Up @@ -290,13 +290,13 @@ public static Optional<Pointer> getPreviousPointer(BaseTreeNode treeNode){
}

return Optional.of(
Pointer.fromByteArray(treeNode.getData(), treeNode.getData().length - (2*Pointer.BYTES))
Pointer.fromBytes(treeNode.getData(), treeNode.getData().length - (2*Pointer.BYTES))
);
}

public static void setPreviousPointer(BaseTreeNode treeNode, Pointer pointer) {
System.arraycopy(
pointer.toByteArray(),
pointer.toBytes(),
0,
treeNode.getData(),
treeNode.getData().length - (2*Pointer.BYTES),
Expand All @@ -310,13 +310,13 @@ public static Optional<Pointer> getNextPointer(BaseTreeNode treeNode){
}

return Optional.of(
Pointer.fromByteArray(treeNode.getData(), treeNode.getData().length - (Pointer.BYTES))
Pointer.fromBytes(treeNode.getData(), treeNode.getData().length - (Pointer.BYTES))
);
}

public static void setNextPointer(BaseTreeNode treeNode, Pointer pointer) {
System.arraycopy(
pointer.toByteArray(),
pointer.toBytes(),
0,
treeNode.getData(),
treeNode.getData().length - Pointer.BYTES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import java.util.Iterator;
import java.util.List;

// Todo: This isn't reactive, but should it be?
// Todo: If AsyncFileChannels are used, do we need to use Futures here? (probably not?)

/*
Structure of a node in binary for non-leaf
[1 byte -6 empty bits- IS_ROOT | IS_LEAF] + [POINTER_SIZE bytes child] + (([LONG_SIZE bytes id] + [POINTER_SIZE bytes child]) * max node size)
Expand All @@ -37,7 +34,6 @@ public boolean isLeaf(){ // 0 0 & 0 0
return (data[0] & TYPE_LEAF_NODE_BIT) == TYPE_LEAF_NODE_BIT;
}

// Todo: use this where its needed, than fromBytes
public static BaseTreeNode fromBytes(byte[] data, NodeType type){
if (type == NodeType.INTERNAL){
return new InternalTreeNode(data);
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/github/sepgh/internal/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.sepgh.internal.utils;

import com.google.common.hash.HashCode;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import java.util.Map;
import java.util.Optional;

// Todo: add non-single key tests (refactor from single to multi)
public class TreeNodeTestCase {
private final byte[] singleKeyInternalNodeRepresentation = {
((byte) (0x00 | BaseTreeNode.TYPE_INTERNAL_NODE_BIT)), // Not leaf
BaseTreeNode.TYPE_INTERNAL_NODE_BIT, // Not leaf

// >> Start pointer to child 1
Pointer.TYPE_NODE, // type
Expand All @@ -31,7 +30,7 @@ public class TreeNodeTestCase {
};

private final byte[] singleKeyLeafNodeRepresentation = {
((byte) (0x00 | BaseTreeNode.TYPE_LEAF_NODE_BIT)), // leaf
BaseTreeNode.TYPE_LEAF_NODE_BIT, // leaf

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, // Key 1

Expand Down Expand Up @@ -74,9 +73,7 @@ public void testSingleKeyInternalTreeNodeChildren(){
Assertions.assertEquals(2, child1Pointer.getPosition());
Assertions.assertEquals(2, child1Pointer.getChunk());

Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
treeNode.getChildAtIndex(2);
});
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, () -> treeNode.getChildAtIndex(2));
}

@Test
Expand Down Expand Up @@ -119,7 +116,7 @@ public void testSingleKeyInternalNodeKeysIteration(){

Assertions.assertFalse(iterator.hasNext());

Assertions.assertEquals(15, treeNode.keyList().get(0));
Assertions.assertEquals(15, treeNode.keyList().getFirst());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void testMultiSplitAddIndex() throws IOException, ExecutionException, Int
//3rd leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2)); // Todo
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void testMultiSplitAddIndex() throws IOException, ExecutionException, Int
//3rd leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2)); // Todo
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
Expand Down Expand Up @@ -383,7 +383,7 @@ public void testMultiSplitAddIndexDifferentAddOrders() throws IOException, Execu
//3rd leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2)); // Todo
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
Expand Down Expand Up @@ -564,7 +564,7 @@ public void testMultiSplitAddIndex2() throws IOException, ExecutionException, In
//3rd leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2)); // Todo
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
Expand Down Expand Up @@ -594,7 +594,7 @@ public void testMultiSplitAddIndex2() throws IOException, ExecutionException, In

// 4th leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent()); //Todo
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), rightChildInternalNodeChildren.get(0));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
Expand All @@ -612,7 +612,7 @@ public void testMultiSplitAddIndex2() throws IOException, ExecutionException, In
// 5th leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), rightChildInternalNodeChildren.get(1)); //Todo
Assertions.assertEquals(nextPointer.get(), rightChildInternalNodeChildren.get(1));
currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
tableId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public void testMultiSplitAddIndex() throws IOException, ExecutionException, Int
//3rd leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2)); // Todo
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
Expand Down Expand Up @@ -385,7 +385,7 @@ public void testMultiSplitAddIndexDifferentAddOrders() throws IOException, Execu
//3rd leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2)); // Todo
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
Expand Down Expand Up @@ -564,7 +564,7 @@ public void testMultiSplitAddIndex2() throws IOException, ExecutionException, In
//3rd leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2)); // Todo
Assertions.assertEquals(nextPointer.get(), leftChildInternalNodeChildren.get(2));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
Expand Down Expand Up @@ -594,7 +594,7 @@ public void testMultiSplitAddIndex2() throws IOException, ExecutionException, In

// 4th leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent()); //Todo
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), rightChildInternalNodeChildren.get(0));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
Expand All @@ -612,7 +612,7 @@ public void testMultiSplitAddIndex2() throws IOException, ExecutionException, In
// 5th leaf
nextPointer = currentLeaf.getNext();
Assertions.assertTrue(nextPointer.isPresent());
Assertions.assertEquals(nextPointer.get(), rightChildInternalNodeChildren.get(1)); //Todo
Assertions.assertEquals(nextPointer.get(), rightChildInternalNodeChildren.get(1));

currentLeaf = (LeafTreeNode) BaseTreeNode.fromBytes(
fileIndexStorageManager.readNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

// Todo: add less happy tests :D0

public class BinaryUtilsTestCase {

@Test
Expand Down

0 comments on commit 046caec

Please sign in to comment.