diff --git a/src/main/java/com/intbyte/bdb/ArrayBuffer.java b/src/main/java/com/intbyte/bdb/ArrayBuffer.java index 1aed09e..970953b 100644 --- a/src/main/java/com/intbyte/bdb/ArrayBuffer.java +++ b/src/main/java/com/intbyte/bdb/ArrayBuffer.java @@ -13,103 +13,102 @@ import java.util.ArrayList; public class ArrayBuffer { + private final ArrayList> nodes = new ArrayList<>(); private int size = 0; - ArrayList> 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); + } } diff --git a/src/main/java/com/intbyte/bdb/DataBuffer.java b/src/main/java/com/intbyte/bdb/DataBuffer.java index 49290d6..b4b076e 100644 --- a/src/main/java/com/intbyte/bdb/DataBuffer.java +++ b/src/main/java/com/intbyte/bdb/DataBuffer.java @@ -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 { @@ -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]; @@ -71,7 +94,7 @@ public byte[] toBytes() { } - public void clear(){ + public void clear() { byteArraySize = 0; nodes.clear(); arrayBuffer.clear(); @@ -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; @@ -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 getArray(String key){ + return (T) arrayBuffer.getNode(getInt(key)).array; + } } diff --git a/src/main/java/com/intbyte/bdb/node/CharNode.java b/src/main/java/com/intbyte/bdb/node/CharNode.java index 84d0087..5349a81 100644 --- a/src/main/java/com/intbyte/bdb/node/CharNode.java +++ b/src/main/java/com/intbyte/bdb/node/CharNode.java @@ -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; } diff --git a/src/main/java/com/intbyte/bdb/node/DoubleNode.java b/src/main/java/com/intbyte/bdb/node/DoubleNode.java index 89c3a50..b77e2ac 100644 --- a/src/main/java/com/intbyte/bdb/node/DoubleNode.java +++ b/src/main/java/com/intbyte/bdb/node/DoubleNode.java @@ -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; } diff --git a/src/main/java/com/intbyte/bdb/node/FloatNode.java b/src/main/java/com/intbyte/bdb/node/FloatNode.java index 884877f..ca70ad7 100644 --- a/src/main/java/com/intbyte/bdb/node/FloatNode.java +++ b/src/main/java/com/intbyte/bdb/node/FloatNode.java @@ -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; } diff --git a/src/main/java/com/intbyte/bdb/node/IntNode.java b/src/main/java/com/intbyte/bdb/node/IntNode.java index a11e063..a762510 100644 --- a/src/main/java/com/intbyte/bdb/node/IntNode.java +++ b/src/main/java/com/intbyte/bdb/node/IntNode.java @@ -8,7 +8,7 @@ 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; } @@ -16,6 +16,7 @@ public IntNode(int value){ public byte[] toBytes() { return ByteBuffer.allocate(4).putInt(value).array(); } + @Override public byte getId() { return ID; diff --git a/src/main/java/com/intbyte/bdb/node/ShortNode.java b/src/main/java/com/intbyte/bdb/node/ShortNode.java index ddca799..ef52d26 100644 --- a/src/main/java/com/intbyte/bdb/node/ShortNode.java +++ b/src/main/java/com/intbyte/bdb/node/ShortNode.java @@ -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; } diff --git a/src/main/java/com/intbyte/bdb/node/array/ArrayNode.java b/src/main/java/com/intbyte/bdb/node/array/ArrayNode.java index b1a7cc1..01d36dd 100644 --- a/src/main/java/com/intbyte/bdb/node/array/ArrayNode.java +++ b/src/main/java/com/intbyte/bdb/node/array/ArrayNode.java @@ -3,19 +3,19 @@ import java.nio.ByteBuffer; public abstract class ArrayNode { - public static final byte ID = 6; public T array; protected int pointer = 0; protected abstract void convertToByteArray(ByteBuffer buffer); - protected abstract int arraySize(); + public abstract int sizeOfByteArray(); protected abstract int getType(); public void putToArrayBuffer(ByteBuffer byteBuffer){ - byteBuffer.put((byte) getType()); - byteBuffer.putInt(arraySize()); + byteBuffer.putInt(getType()); + byteBuffer.putInt(sizeOfByteArray()); convertToByteArray(byteBuffer); + return; } public ArrayNode(int pointer){ diff --git a/src/main/java/com/intbyte/bdb/node/array/ByteArrayNode.java b/src/main/java/com/intbyte/bdb/node/array/ByteArrayNode.java index c851179..f767b73 100644 --- a/src/main/java/com/intbyte/bdb/node/array/ByteArrayNode.java +++ b/src/main/java/com/intbyte/bdb/node/array/ByteArrayNode.java @@ -4,14 +4,15 @@ import java.nio.ByteBuffer; -public class ByteArrayNode extends ArrayNode { ; +public class ByteArrayNode extends ArrayNode { + ; public ByteArrayNode(byte[] array, int pointer) { super(pointer); this.array = array; } - public ByteArrayNode(byte[] array){ + public ByteArrayNode(byte[] array) { super(array); } @@ -21,12 +22,12 @@ protected void convertToByteArray(ByteBuffer buffer) { } @Override - protected int arraySize() { + public int sizeOfByteArray() { return array.length; } @Override protected int getType() { - return ArrayNode.ID + ByteNode.ID; + return ByteNode.ID; } } diff --git a/src/main/java/com/intbyte/bdb/node/array/DoubleArrayNode.java b/src/main/java/com/intbyte/bdb/node/array/DoubleArrayNode.java index 10826d9..43d30d0 100644 --- a/src/main/java/com/intbyte/bdb/node/array/DoubleArrayNode.java +++ b/src/main/java/com/intbyte/bdb/node/array/DoubleArrayNode.java @@ -21,12 +21,12 @@ protected void convertToByteArray(ByteBuffer buffer) { } @Override - protected int arraySize() { + public int sizeOfByteArray() { return array.length * 8; } @Override protected int getType() { - return ArrayNode.ID + DoubleNode.ID; + return DoubleNode.ID; } } diff --git a/src/main/java/com/intbyte/bdb/node/array/FloatArrayNode.java b/src/main/java/com/intbyte/bdb/node/array/FloatArrayNode.java index 653ca41..3a9148a 100644 --- a/src/main/java/com/intbyte/bdb/node/array/FloatArrayNode.java +++ b/src/main/java/com/intbyte/bdb/node/array/FloatArrayNode.java @@ -20,12 +20,12 @@ protected void convertToByteArray(ByteBuffer buffer) { } @Override - protected int arraySize() { + public int sizeOfByteArray() { return array.length * 4; } @Override protected int getType() { - return ArrayNode.ID + FloatNode.ID; + return FloatNode.ID; } } diff --git a/src/main/java/com/intbyte/bdb/node/array/IntArrayNode.java b/src/main/java/com/intbyte/bdb/node/array/IntArrayNode.java index 11eeaa4..86752b1 100644 --- a/src/main/java/com/intbyte/bdb/node/array/IntArrayNode.java +++ b/src/main/java/com/intbyte/bdb/node/array/IntArrayNode.java @@ -21,12 +21,12 @@ protected void convertToByteArray(ByteBuffer buffer) { } @Override - protected int arraySize() { + public int sizeOfByteArray() { return array.length * 4; } @Override protected int getType() { - return ArrayNode.ID + IntNode.ID; + return IntNode.ID; } } diff --git a/src/main/java/com/intbyte/bdb/node/array/LongArrayNode.java b/src/main/java/com/intbyte/bdb/node/array/LongArrayNode.java index 4af905f..a6eb760 100644 --- a/src/main/java/com/intbyte/bdb/node/array/LongArrayNode.java +++ b/src/main/java/com/intbyte/bdb/node/array/LongArrayNode.java @@ -21,12 +21,12 @@ protected void convertToByteArray(ByteBuffer buffer) { } @Override - protected int arraySize() { + public int sizeOfByteArray() { return array.length * 8; } @Override protected int getType() { - return ArrayNode.ID + LongNode.ID; + return LongNode.ID; } } \ No newline at end of file diff --git a/src/main/java/com/intbyte/bdb/node/array/ShortArrayNode.java b/src/main/java/com/intbyte/bdb/node/array/ShortArrayNode.java index 82cee93..c367a68 100644 --- a/src/main/java/com/intbyte/bdb/node/array/ShortArrayNode.java +++ b/src/main/java/com/intbyte/bdb/node/array/ShortArrayNode.java @@ -3,7 +3,6 @@ import com.intbyte.bdb.node.ShortNode; import java.nio.ByteBuffer; -import java.nio.ShortBuffer; public class ShortArrayNode extends ArrayNode { @@ -23,12 +22,12 @@ protected void convertToByteArray(ByteBuffer buffer) { } @Override - protected int arraySize() { + public int sizeOfByteArray() { return array.length*2; } @Override protected int getType() { - return ArrayNode.ID + ShortNode.ID; + return ShortNode.ID; } } \ No newline at end of file diff --git a/src/test/java/Test.java b/src/test/java/Test.java index 430e691..7f1d952 100644 --- a/src/test/java/Test.java +++ b/src/test/java/Test.java @@ -4,7 +4,7 @@ public class Test { public static void main(String[] args) { DataBuffer dataBuffer = new DataBuffer(new HashKeyProvider()); - dataBuffer.putInt("hello", 100); + dataBuffer.put("hello", 100); dataBuffer.readBytes(dataBuffer.toBytes()); dataBuffer.readBytes(dataBuffer.toBytes()); System.out.println(dataBuffer.getInt("hello")); diff --git a/src/test/java/com/intbyte/bdb/DataBufferTest.java b/src/test/java/com/intbyte/bdb/DataBufferTest.java index 49c4d55..7d9e887 100644 --- a/src/test/java/com/intbyte/bdb/DataBufferTest.java +++ b/src/test/java/com/intbyte/bdb/DataBufferTest.java @@ -11,13 +11,21 @@ class HashDataBufferTest { { buffer = new DataBuffer(new HashKeyProvider()); - buffer.putChar("char", 'v'); - buffer.putByte("byte", (byte) 10); - buffer.putShort("short", (short) 10); - buffer.putInt("int", 10); - buffer.putFloat("float", 10.0f); - buffer.putDouble("double", 10.0); - buffer.putLong("long", 10); + buffer.put("char", 'v'); + buffer.put("byte", (byte) 10); + buffer.put("short", (short) 10); + buffer.put("int", 10); + buffer.put("float", 10.0f); + buffer.put("double", 10.0); + buffer.put("long", 10L); + + + buffer.put("byteArray", new byte[]{7}); + buffer.put("shortArray", new short[]{7}); + buffer.put("intArray", new int[]{7}); + buffer.put("floatArray", new float[]{7}); + buffer.put("doubleArray", new double[]{7}); + buffer.put("longArray", new long[]{7}); } @@ -31,6 +39,8 @@ void contains() { assertTrue(buffer.contains("float")); assertTrue(buffer.contains("double")); assertTrue(buffer.contains("long")); + + assertEquals(0, buffer.getInt("byteArray")); } @Test @@ -76,4 +86,15 @@ void getInt() { void getLong() { assertEquals(buffer.getLong("long"), 10); } + + @Test + void getArray() { + + assertArrayEquals(buffer.getArray("byteArray"), new byte[]{7}); + assertArrayEquals(buffer.getArray("shortArray"), new short[]{7}); + assertArrayEquals(buffer.getArray("floatArray"), new float[]{7}); + assertArrayEquals(buffer.getArray("doubleArray"), new double[]{7}); + assertArrayEquals(buffer.getArray("intArray"), new int[]{7}); + assertArrayEquals(buffer.getArray("longArray"), new long[]{7}); + } } \ No newline at end of file