Skip to content

Commit

Permalink
refactor: buildable beautified branch!
Browse files Browse the repository at this point in the history
  • Loading branch information
sepgh committed May 16, 2024
1 parent b4f38f7 commit 994f020
Show file tree
Hide file tree
Showing 16 changed files with 927 additions and 1,587 deletions.
37 changes: 37 additions & 0 deletions scripts/descriptor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys


def parse_leaf(binary: str, degree: int):
print()
print("'%-10s'" % "0-2", "", binary[0:2])
print()

offset = 2
for i in range(0, degree - 1):
print(
"'%-10s'" % f"{offset}-{offset+42}",
"",
binary[offset: offset + 16],
binary[offset + 16: offset + 18],
binary[offset + 18: offset + 34],
binary[offset + 34: offset + 42]
)
offset += 42
print()
for _ in range(0, 2):
print(
"'%-10s'" % f"{offset}-{offset+26}",
"",
binary[offset: offset + 2],
binary[offset + 2: offset + 18],
binary[offset + 18: offset + 26],
)
offset += 26

print()
a = binary[offset:]
print("'%-10s'" % f"{offset}-...", "", ' '.join([a[i:i+4] for i in range(0, len(a), 4)]))


if __name__ == "__main__":
parse_leaf(sys.argv[2], int(sys.argv[1]))
1 change: 1 addition & 0 deletions src/main/java/com/github/sepgh/internal/EngineConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class EngineConfig {

private final int bTreeNodeMaxKey;
private final int bTreeGrowthNodeAllocationCount;
@Builder.Default
private long bTreeMaxFileSize = -1L;


Expand Down
395 changes: 121 additions & 274 deletions src/main/java/com/github/sepgh/internal/tree/BTreeIndexManager.java

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/main/java/com/github/sepgh/internal/tree/TreeNodeIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.sepgh.internal.tree;

import com.github.sepgh.internal.storage.IndexStorageManager;
import com.github.sepgh.internal.tree.node.BaseTreeNode;

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

public class TreeNodeIO {
public static CompletableFuture<IndexStorageManager.NodeData> write(BaseTreeNode node, IndexStorageManager indexStorageManager, int table) throws IOException, ExecutionException, InterruptedException {
IndexStorageManager.NodeData nodeData = new IndexStorageManager.NodeData(node.getPointer(), node.getData());
if (!node.isModified() && node.getPointer() != null){
return CompletableFuture.completedFuture(nodeData);
}
CompletableFuture<IndexStorageManager.NodeData> output = new CompletableFuture<>();

if (node.getPointer() == null){
indexStorageManager.writeNewNode(table, node.getData(), node.isRoot()).whenComplete((nodeData1, throwable) -> {
if (throwable != null){
output.completeExceptionally(throwable);
return;
}
node.setPointer(nodeData1.pointer());
output.complete(nodeData1);
});
} else {
indexStorageManager.updateNode(table, node.getData(), node.getPointer()).whenComplete((integer, throwable) -> {
if (throwable != null){
output.completeExceptionally(throwable);
return;
}
output.complete(nodeData);
});
}
return output;
}

public static BaseTreeNode read(IndexStorageManager indexStorageManager, int table, Pointer pointer) throws ExecutionException, InterruptedException {
return BaseTreeNode.fromNodeData(indexStorageManager.readNode(table, pointer).get());
}
}
41 changes: 31 additions & 10 deletions src/main/java/com/github/sepgh/internal/tree/TreeNodeUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.sepgh.internal.tree;

import com.github.sepgh.internal.tree.node.BaseTreeNode;
import com.github.sepgh.internal.tree.node.InternalTreeNode;
import com.github.sepgh.internal.utils.BinaryUtils;
import com.google.common.hash.HashCode;
import com.google.common.primitives.Longs;

import java.util.AbstractMap;
Expand Down Expand Up @@ -327,44 +329,63 @@ public static int addKeyAndGetIndex(BaseTreeNode treeNode, long key) {
return indexToFill;
}

public static Optional<Pointer> getPreviousPointer(BaseTreeNode treeNode){
if (treeNode.getData()[treeNode.getData().length - (2*Pointer.BYTES)] == (byte) 0x0){
public static Optional<Pointer> getPreviousPointer(BaseTreeNode treeNode, int degree){
if (treeNode.getData()[OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER)] == (byte) 0x0){
return Optional.empty();
}

return Optional.of(
Pointer.fromBytes(treeNode.getData(), treeNode.getData().length - (2*Pointer.BYTES))
Pointer.fromBytes(treeNode.getData(), OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER))
);
}

public static void setPreviousPointer(BaseTreeNode treeNode, Pointer pointer) {
public static void setPreviousPointer(BaseTreeNode treeNode, int degree, Pointer pointer) {
System.arraycopy(
pointer.toBytes(),
0,
treeNode.getData(),
treeNode.getData().length - (2*Pointer.BYTES),
OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER),
Pointer.BYTES
);
}

public static Optional<Pointer> getNextPointer(BaseTreeNode treeNode){
if (treeNode.getData()[treeNode.getData().length - (Pointer.BYTES)] == (byte) 0x0){
public static Optional<Pointer> getNextPointer(BaseTreeNode treeNode, int degree) {
System.out.println("getNextPointer is called on " + treeNode.getPointer() + " with keys:" + treeNode.getKeyList());
System.out.println("get results: " + HashCode.fromBytes(Arrays.copyOfRange(treeNode.getData(), OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER) + Pointer.BYTES, OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER) + Pointer.BYTES + Pointer.BYTES)));
System.out.println("get results full: " + HashCode.fromBytes(treeNode.getData()));

if (treeNode.getData()[OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER) + Pointer.BYTES] == (byte) 0x0){
return Optional.empty();
}

return Optional.of(
Pointer.fromBytes(treeNode.getData(), treeNode.getData().length - (Pointer.BYTES))
Pointer.fromBytes(treeNode.getData(), OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER) + Pointer.BYTES)
);
}

public static void setNextPointer(BaseTreeNode treeNode, Pointer pointer) {
public static void setNextPointer(BaseTreeNode treeNode, int degree, Pointer pointer) {
System.out.println("Called setNextPointer on node: " + treeNode.getPointer() + " to set " + pointer);
System.arraycopy(
pointer.toBytes(),
0,
treeNode.getData(),
treeNode.getData().length - Pointer.BYTES,
OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER) + Pointer.BYTES,
Pointer.BYTES
);
System.out.println("pointer as bytes " + HashCode.fromBytes(pointer.toBytes()));
System.out.println("OFFSET_LEAF_NODE_KEY_BEGIN: " + OFFSET_LEAF_NODE_KEY_BEGIN + ", SIZE_LEAF_NODE_KEY_POINTER: " + SIZE_LEAF_NODE_KEY_POINTER + ", Pointer.BYTES: " + Pointer.BYTES + ", DEGREE: " + degree);
System.out.println("Location to copy:" + (OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER) + Pointer.BYTES));
System.out.println("Post arraycopy results: " + HashCode.fromBytes(Arrays.copyOfRange(treeNode.getData(), OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER) + Pointer.BYTES, OFFSET_LEAF_NODE_KEY_BEGIN + ((degree - 1) * SIZE_LEAF_NODE_KEY_POINTER) + Pointer.BYTES + Pointer.BYTES)));
}

public static void cleanChildrenPointers(InternalTreeNode treeNode, int degree) {
byte[] cleanup = new byte[((degree - 1) * (SIZE_INTERNAL_NODE_KEY_POINTER) + Pointer.BYTES)];
System.arraycopy(
cleanup,
0,
treeNode.getData(),
OFFSET_TREE_NODE_FLAGS_END,
cleanup.length
);
}
}
43 changes: 26 additions & 17 deletions src/main/java/com/github/sepgh/internal/tree/node/BaseTreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
*/
@Getter
public abstract class BaseTreeNode {
public static byte TYPE_LEAF_NODE_BIT = 0x03; // 0 0 1 0
public static byte TYPE_LEAF_NODE_BIT = 0x02; // 0 0 1 0
public static byte TYPE_INTERNAL_NODE_BIT = 0x01; // 0 0 0 1
public static byte ROOT_BIT = 0x04; // 0 1 0 0

@Setter
private Pointer nodePointer;
private Pointer pointer;
private final byte[] data;
@Getter
private boolean modified = false;

public BaseTreeNode(byte[] data) {
this.data = data;
Expand All @@ -35,14 +37,18 @@ public boolean isLeaf(){ // 0 0 & 0 0
return (data[0] & TYPE_LEAF_NODE_BIT) == TYPE_LEAF_NODE_BIT;
}

protected void modified(){
this.modified = true;
}


public static BaseTreeNode fromNodeData(IndexStorageManager.NodeData nodeData) {
BaseTreeNode treeNode = BaseTreeNode.fromBytes(nodeData.bytes());
treeNode.setNodePointer(nodeData.pointer());
treeNode.setPointer(nodeData.pointer());
return treeNode;
}
public static BaseTreeNode fromBytes(byte[] data, NodeType type){
if (type == NodeType.INTERNAL){
public static BaseTreeNode fromBytes(byte[] data, Type type){
if (type == Type.INTERNAL){
return new InternalTreeNode(data);
}
return new LeafTreeNode(data);
Expand All @@ -58,44 +64,47 @@ public byte[] toBytes(){
return data;
}

public void setType(NodeType type) {
public void setType(Type type) {
modified();
// Only can be called if the node is empty, otherwise changing type of already constructed node will F things up
this.data[0] = (byte) (data[0] | type.getSign());
}

public NodeType getType(){
return isLeaf() ? NodeType.LEAF : NodeType.INTERNAL;
public Type getType(){
if ((data[0] & TYPE_LEAF_NODE_BIT) == TYPE_LEAF_NODE_BIT)
return Type.LEAF;
if ((data[0] & TYPE_INTERNAL_NODE_BIT) == TYPE_INTERNAL_NODE_BIT)
return Type.INTERNAL;
return null;
}

public void setAsRoot(){
modified();
this.data[0] = (byte) (data[0] | ROOT_BIT);
}

public void unsetAsRoot(){
modified();
this.data[0] = (byte) (data[0] & ~ROOT_BIT);
}

public Iterator<Long> keys(){
public Iterator<Long> getKeys(){
return new TreeNodeKeysIterator(this);
}

public List<Long> keyList(){
return ImmutableList.copyOf(keys());
}

public int addKey(long key) {
return TreeNodeUtils.addKeyAndGetIndex(this, key);
public List<Long> getKeyList(){
return ImmutableList.copyOf(getKeys());
}

public boolean isRoot() {
return (data[0] & ROOT_BIT) == ROOT_BIT;
}

public enum NodeType {
public enum Type {
LEAF(TYPE_LEAF_NODE_BIT), INTERNAL(TYPE_INTERNAL_NODE_BIT);
private final byte sign;

NodeType(byte sign) {
Type(byte sign) {
this.sign = sign;
}

Expand Down
Loading

0 comments on commit 994f020

Please sign in to comment.