diff --git a/src/main/java/com/tomfran/lsm/io/BaseInputStream.java b/src/main/java/com/tomfran/lsm/io/BaseInputStream.java index 753cea3..2658e9a 100644 --- a/src/main/java/com/tomfran/lsm/io/BaseInputStream.java +++ b/src/main/java/com/tomfran/lsm/io/BaseInputStream.java @@ -82,15 +82,6 @@ public ByteArrayPair readBytePair() { } } - public byte[] readByteArray() { - try { - int len = readVByteInt(); - return fis.readNBytes(len); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - public long skip(int n) { try { return fis.skip(n); @@ -107,14 +98,6 @@ public void seek(long offset) { } } - public boolean hasNext() { - try { - return fis.available() > 0; - } catch (IOException e) { - throw new RuntimeException(e); - } - } - public void close() { try { fis.close(); diff --git a/src/main/java/com/tomfran/lsm/memtable/Memtable.java b/src/main/java/com/tomfran/lsm/memtable/Memtable.java index 3a28a0c..4441579 100644 --- a/src/main/java/com/tomfran/lsm/memtable/Memtable.java +++ b/src/main/java/com/tomfran/lsm/memtable/Memtable.java @@ -21,7 +21,7 @@ public void add(ByteArrayPair item) { list.add(item); } - public ByteArrayPair get(byte[] key) { + public byte[] get(byte[] key) { return list.get(key); } diff --git a/src/main/java/com/tomfran/lsm/memtable/SkipList.java b/src/main/java/com/tomfran/lsm/memtable/SkipList.java index 74ebe86..a198e27 100644 --- a/src/main/java/com/tomfran/lsm/memtable/SkipList.java +++ b/src/main/java/com/tomfran/lsm/memtable/SkipList.java @@ -52,13 +52,13 @@ public SkipList(int numElements) { public void add(ByteArrayPair item) { Node current = sentinel; for (int i = levels - 1; i >= 0; i--) { - while (current.next[i] != null && current.next[i].value.compareTo(item) < 0) + while (current.next[i] != null && current.next[i].val.compareTo(item) < 0) current = current.next[i]; buffer[i] = current; } - if (current.next[0] != null && current.next[0].value.compareTo(item) == 0) { - current.next[0].value = item; + if (current.next[0] != null && current.next[0].val.compareTo(item) == 0) { + current.next[0].val = item; return; } @@ -83,15 +83,15 @@ private int randomLevel() { * @param key The key of the item to retrieve. * @return The item if found, null otherwise. */ - public ByteArrayPair get(byte[] key) { + public byte[] get(byte[] key) { Node current = sentinel; for (int i = levels - 1; i >= 0; i--) { - while (current.next[i] != null && compare(current.next[i].value.key(), key) < 0) + while (current.next[i] != null && compare(current.next[i].val.key(), key) < 0) current = current.next[i]; } - if (current.next[0] != null && compare(current.next[0].value.key(), key) == 0) - return current.next[0].value; + if (current.next[0] != null && compare(current.next[0].val.key(), key) == 0) + return current.next[0].val.value(); return null; } @@ -104,12 +104,12 @@ public ByteArrayPair get(byte[] key) { public void remove(byte[] key) { Node current = sentinel; for (int i = levels - 1; i >= 0; i--) { - while (current.next[i] != null && compare(current.next[i].value.key(), key) < 0) + while (current.next[i] != null && compare(current.next[i].val.key(), key) < 0) current = current.next[i]; buffer[i] = current; } - if (current.next[0] != null && compare(current.next[0].value.key(), key) == 0) { + if (current.next[0] != null && compare(current.next[0].val.key(), key) == 0) { boolean last = current.next[0].next[0] == null; for (int i = 0; i < levels; i++) { if (buffer[i].next[i] != current.next[0]) @@ -146,7 +146,7 @@ public String toString() { sb.append(String.format("Level %2d: ", i)); Node current = sentinel; while (current.next[i] != null) { - sb.append(current.next[i].value).append(" -> "); + sb.append(current.next[i].val).append(" -> "); current = current.next[i]; } sb.append("END\n"); @@ -155,11 +155,11 @@ public String toString() { } private static final class Node { - ByteArrayPair value; + ByteArrayPair val; Node[] next; - Node(ByteArrayPair value, int numLevels) { - this.value = value; + Node(ByteArrayPair val, int numLevels) { + this.val = val; this.next = new Node[numLevels]; } } @@ -173,7 +173,7 @@ public boolean hasNext() { @Override public ByteArrayPair next() { - return node.next[0].value; + return node.next[0].val; } } diff --git a/src/main/java/com/tomfran/lsm/tree/LSMTree.java b/src/main/java/com/tomfran/lsm/tree/LSMTree.java index 297e843..bf57877 100644 --- a/src/main/java/com/tomfran/lsm/tree/LSMTree.java +++ b/src/main/java/com/tomfran/lsm/tree/LSMTree.java @@ -37,8 +37,8 @@ private void checkMemtableSize() { } } - public ByteArrayPair get(byte[] key) { - ByteArrayPair result; + public byte[] get(byte[] key) { + byte[] result; if ((result = mutableMemtable.get(key)) != null) return result; @@ -47,10 +47,9 @@ public ByteArrayPair get(byte[] key) { if ((result = memtable.get(key)) != null) return result; - byte[] tmp; for (SSTable table : tables) - if ((tmp = table.get(key)) != null) - return new ByteArrayPair(key, tmp); + if ((result = table.get(key)) != null) + return result; return null; } diff --git a/src/test/java/com/tomfran/lsm/memtable/SkipListTest.java b/src/test/java/com/tomfran/lsm/memtable/SkipListTest.java index 58ac74a..8410217 100644 --- a/src/test/java/com/tomfran/lsm/memtable/SkipListTest.java +++ b/src/test/java/com/tomfran/lsm/memtable/SkipListTest.java @@ -36,8 +36,7 @@ public void shouldFind() { for (ByteArrayPair item : items) { var found = l.get(item.key()); assert found != null; - assert compare(found.key(), item.key()) == 0; - assert compare(found.value(), item.value()) == 0; + assert compare(found, item.value()) == 0; } } @@ -56,8 +55,7 @@ public void shouldRemove() { assert found == null; } else { assert found != null; - assert compare(found.key(), item.key()) == 0; - assert compare(found.value(), item.value()) == 0; + assert compare(found, item.value()) == 0; } } }