Skip to content

Commit

Permalink
add serialization and deserialization of arrays using DataBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
intbyte-100 committed Apr 27, 2022
1 parent 029272f commit 019ae96
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 103 deletions.
89 changes: 44 additions & 45 deletions src/main/java/com/intbyte/bdb/ArrayBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,103 +13,102 @@
import java.util.ArrayList;

public class ArrayBuffer {
private final ArrayList<ArrayNode<?>> nodes = new ArrayList<>();
private int size = 0;

ArrayList<ArrayNode<?>> nodes = new ArrayList<>();

public void put(byte[] array) {
nodes.add(new ByteArrayNode(array, size));
size += 8 + array.length;
private int put(ArrayNode<?> node) {
nodes.add(node);
size += 8 + node.sizeOfByteArray();
return nodes.size() - 1;
}

public void put(short[] array) {
nodes.add(new ShortArrayNode(array, size));
size += 8 + array.length * 2;
public int put(byte[] array) {
return put(new ByteArrayNode(array, size));
}

public void put(int[] array) {
nodes.add(new IntArrayNode(array, size));
size += 8 + array.length * 4;
public int put(short[] array) {
return put(new ShortArrayNode(array, size));
}

public void put(float[] array) {
nodes.add(new FloatArrayNode(array, size));
size += 8 + array.length * 4;
public int put(int[] array) {
return put(new IntArrayNode(array, size));
}

public void put(double[] array) {
nodes.add(new DoubleArrayNode(array, size));
size += 8 + array.length * 8;
public int put(float[] array) {
return put(new FloatArrayNode(array, size));
}

public void put(long[] array) {
nodes.add(new LongArrayNode(array, size));
size += 8 + array.length * 8;
public int put(double[] array) {
return put(new DoubleArrayNode(array, size));
}

public byte[] toBytes(){
public int put(long[] array) {
return put(new LongArrayNode(array, size));
}

public byte[] toBytes() {
ByteBuffer buffer = ByteBuffer.allocate(size);
nodes.forEach(i -> i.putToArrayBuffer(buffer));
return buffer.array();
}

public void readByteBuffer(int bytesCount, ByteBuffer byteBuffer){
public void readByteBuffer(int bytesCount, ByteBuffer byteBuffer) {
int index = 0;

ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
IntBuffer intBuffer = byteBuffer.asIntBuffer();
FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
DoubleBuffer doubleBuffer = byteBuffer.asDoubleBuffer();
LongBuffer longBuffer = byteBuffer.asLongBuffer();

while (index < bytesCount) {
int type = byteBuffer.get() - ArrayNode.ID;
int type = byteBuffer.getInt();
int size = byteBuffer.getInt();

switch (type) {
case ByteNode.ID: {
byte[] array = new byte[size];
nodes.add(new ByteArrayNode(array));
byteBuffer.get(array);
put(array);
break;
}
case ShortNode.ID: {
short[] array = new short[size/2];
nodes.add(new ShortArrayNode(array));
shortBuffer.get(array);
short[] array = new short[size / 2];
byteBuffer.asShortBuffer().get(array);
put(array);
break;
}
case IntNode.ID: {
int[] array = new int[size/4];
nodes.add(new IntArrayNode(array));
intBuffer.get(array);
int[] array = new int[size / 4];
byteBuffer.asIntBuffer().get(array);
put(array);
break;
}
case FloatNode.ID: {
float[] array = new float[size/4];
nodes.add(new FloatArrayNode(array));
floatBuffer.get(array);
float[] array = new float[size / 4];
byteBuffer.asFloatBuffer().get(array);
put(array);
break;
}
case DoubleNode.ID: {
double[] array = new double[size/8];
nodes.add(new DoubleArrayNode(array));
doubleBuffer.get(array);
double[] array = new double[size / 8];
byteBuffer.asDoubleBuffer().get(array);
put(array);
break;
}
case LongNode.ID: {
long[] array = new long[size/8];
nodes.add(new LongArrayNode(array));
longBuffer.get(array);
long[] array = new long[size / 8];
byteBuffer.asLongBuffer().get(array);
put(array);
break;
}
}
index += 5 + size;
index += 8 + size;
}
}

void clear(){
void clear() {
nodes.clear();
size = 0;
}

public ArrayNode<?> getNode(int index) {
return nodes.get(index);
}
}
79 changes: 53 additions & 26 deletions src/main/java/com/intbyte/bdb/DataBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.jetbrains.annotations.NotNull;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;

final public class DataBuffer {
Expand All @@ -24,34 +23,58 @@ private void put(String key, @NotNull Node node) {
nodes.add(node);
}

public void putChar(String key, char value) {
public void put(String key, char value) {
put(key, new CharNode(value));
}

public void putByte(String key, byte value) {
public void put(String key, byte value) {
put(key, new ByteNode(value));
}

public void putShort(String key, short value) {
public void put(String key, short value) {
put(key, new ShortNode(value));
}

public void putInt(String key, int value) {
public void put(String key, int value) {
put(key, new IntNode(value));
}

public void putFloat(String key, float value) {
public void put(String key, float value) {
put(key, new FloatNode(value));
}

public void putDouble(String key, double value) {
public void put(String key, double value) {
put(key, new DoubleNode(value));
}

public void putLong(String key, long value) {
public void put(String key, long value) {
put(key, new LongNode(value));
}

public void put(String key, byte[] array) {
put(key, arrayBuffer.put(array));
}

public void put(String key, short[] array) {
put(key, arrayBuffer.put(array));
}

public void put(String key, int[] array) {
put(key, arrayBuffer.put(array));
}

public void put(String key, float[] array) {
put(key, arrayBuffer.put(array));
}

public void put(String key, double[] array) {
put(key, arrayBuffer.put(array));
}

public void put(String key, long[] array) {
put(key, arrayBuffer.put(array));
}

public byte[] toBytes() {
byte[] arrayBufferData = arrayBuffer.toBytes();
byte[] bytes = new byte[byteArraySize + arrayBufferData.length + 4];
Expand All @@ -71,7 +94,7 @@ public byte[] toBytes() {
}


public void clear(){
public void clear() {
byteArraySize = 0;
nodes.clear();
arrayBuffer.clear();
Expand Down Expand Up @@ -103,35 +126,35 @@ public void readBytes(byte[] bytes) {
case ShortNode.ID:
System.arraycopy(bytes, index, value, 0, 2);
node = new ShortNode(ByteBuffer.wrap(value).getShort(0));
index+=2;
index += 2;
break;
case CharNode.ID:
System.arraycopy(bytes, index, value, 0, 2);
node = new CharNode(ByteBuffer.wrap(value).getChar(0));
index+=2;
index += 2;
break;
case IntNode.ID:
System.arraycopy(bytes, index, value, 0, 4);
node = new IntNode(ByteBuffer.wrap(value).getInt(0));
index+=4;
index += 4;
break;
case FloatNode.ID:
System.arraycopy(bytes, index, value, 0, 4);
node = new FloatNode(ByteBuffer.wrap(value).getFloat(0));
index+=4;
index += 4;
break;
case DoubleNode.ID:
System.arraycopy(bytes, index, value, 0, 8);
node = new DoubleNode(ByteBuffer.wrap(value).getDouble(0));
index+=8;
index += 8;
break;
case LongNode.ID:
System.arraycopy(bytes, index, value, 0, 8);
node = new LongNode(ByteBuffer.wrap(value).getLong(0));
index+=8;
index += 8;
break;
default:
throw new IllegalStateException("unknown data type "+id);
throw new IllegalStateException("unknown data type " + id);
}

node.key = key;
Expand All @@ -140,41 +163,45 @@ public void readBytes(byte[] bytes) {
}
}

private Node find(String key){
private Node find(String key) {
Key nodeKey = provider.generateKey(key);
return nodes.stream().filter( node -> provider.compareKeys(nodeKey, node.key)).findAny().get();
return nodes.stream().filter(node -> provider.compareKeys(nodeKey, node.key)).findAny().get();
}

public boolean contains(String key){
public boolean contains(String key) {
Key nodeKey = provider.generateKey(key);
return nodes.stream().anyMatch(node -> provider.compareKeys(nodeKey, node.key));
}

public byte getByte(String key){
public byte getByte(String key) {
return ((ByteNode) find(key)).value;
}

public short getShort(String key){
public short getShort(String key) {
return ((ShortNode) find(key)).value;
}

public char getChar(String key){
public char getChar(String key) {
return ((CharNode) find(key)).value;
}

public float getFloat(String key){
public float getFloat(String key) {
return ((FloatNode) find(key)).value;
}

public double getDouble(String key){
public double getDouble(String key) {
return ((DoubleNode) find(key)).value;
}

public int getInt(String key){
public int getInt(String key) {
return ((IntNode) find(key)).value;
}

public long getLong(String key){
public long getLong(String key) {
return ((LongNode) find(key)).value;
}

public <T> T getArray(String key){
return (T) arrayBuffer.getNode(getInt(key)).array;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/intbyte/bdb/node/CharNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final public class CharNode extends Node {
public static final byte ID = 1;
public char value;

public CharNode(char value){
public CharNode(char value) {
this.value = value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/intbyte/bdb/node/DoubleNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final public class DoubleNode extends Node {
public static final byte ID = 2;
public double value;

public DoubleNode(double value){
public DoubleNode(double value) {
this.value = value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/intbyte/bdb/node/FloatNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final public class FloatNode extends Node {
public static final byte ID = 3;
public float value;

public FloatNode(float value){
public FloatNode(float value) {
this.value = value;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/intbyte/bdb/node/IntNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ final public class IntNode extends Node {
public static final byte ID = 4;
public int value;

public IntNode(int value){
public IntNode(int value) {
this.value = value;
}

@Override
public byte[] toBytes() {
return ByteBuffer.allocate(4).putInt(value).array();
}

@Override
public byte getId() {
return ID;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/intbyte/bdb/node/ShortNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final public class ShortNode extends Node {
public static final byte ID = 6;
public short value;

public ShortNode(short value){
public ShortNode(short value) {
this.value = value;
}

Expand Down
Loading

0 comments on commit 019ae96

Please sign in to comment.