Skip to content

Commit

Permalink
minor change on skiplist
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Oct 3, 2023
1 parent 826d331 commit c7fb7cc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 41 deletions.
17 changes: 0 additions & 17 deletions src/main/java/com/tomfran/lsm/io/BaseInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tomfran/lsm/memtable/Memtable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/tomfran/lsm/memtable/SkipList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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])
Expand Down Expand Up @@ -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");
Expand All @@ -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];
}
}
Expand All @@ -173,7 +173,7 @@ public boolean hasNext() {

@Override
public ByteArrayPair next() {
return node.next[0].value;
return node.next[0].val;
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/tomfran/lsm/tree/LSMTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/com/tomfran/lsm/memtable/SkipListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit c7fb7cc

Please sign in to comment.