Skip to content

Commit

Permalink
Skiplist iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Oct 1, 2023
1 parent 186acf3 commit cef955b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ To locate an element, we start from the top level and move forward until we find
we are looking for. Then we move down to the next level and repeat the process until we find the element.

Insertions, deletions, and updates are done by first locating the element, then performing
the operation on the node. All of them have a time complexity of `O(log(n))`.
the operation on the node. All of them have an average time complexity of `O(log(n))`.

---

Expand Down
57 changes: 55 additions & 2 deletions src/main/java/com/tomfran/lsm/memtable/SkipList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,33 @@
import static java.lang.Math.ceil;
import static java.lang.Math.log;

/**
* A skip list implementation of items.
*/
public class SkipList implements Iterable<Item> {

static final int DEFAULT_ELEMENTS = 1 << 16;

final Node sentinel;

private final Node[] buffer;
private final XoRoShiRo128PlusRandom rn;

int levels;
int size;

/**
* Create a skip list with a default number of elements, 2 ^ 16.
*/
public SkipList() {
this(DEFAULT_ELEMENTS);
}

/**
* Create a skip list with a specified number of elements.
*
* @param numElements The number of elements to size the skip list for.
*/
public SkipList(int numElements) {
levels = (int) ceil(log(numElements) / log(2));
size = 0;
Expand All @@ -30,6 +44,11 @@ public SkipList(int numElements) {
buffer = new Node[levels];
}

/**
* Add an item to the skip list.
*
* @param item The item to add.
*/
public void add(Item item) {
Node current = sentinel;
for (int i = levels - 1; i >= 0; i--) {
Expand Down Expand Up @@ -58,6 +77,12 @@ private int randomLevel() {
return level;
}

/**
* Retrieve an item from the skip list.
*
* @param key The key of the item to retrieve.
* @return The item if found, null otherwise.
*/
public Item get(byte[] key) {
Node current = sentinel;
for (int i = levels - 1; i >= 0; i--) {
Expand All @@ -71,6 +96,11 @@ public Item get(byte[] key) {
return null;
}

/**
* Remove an item from the skip list.
*
* @param key The key of the item to remove.
*/
public void remove(byte[] key) {
Node current = sentinel;
for (int i = levels - 1; i >= 0; i--) {
Expand All @@ -90,13 +120,23 @@ public void remove(byte[] key) {
}
}

/**
* Get the number of items in the skip list.
*
* @return Skip list size.
*/
public int size() {
return size;
}

/**
* Get an iterator over the items in the skip list at the lowest level.
*
* @return An iterator over the items in the skip list.
*/
@Override
public Iterator<Item> iterator() {
return null;
return new SkipListIterator(sentinel);
}

@Override
Expand All @@ -114,7 +154,7 @@ public String toString() {
return sb.toString();
}

static final class Node {
private static final class Node {
Item value;
Node[] next;

Expand All @@ -124,4 +164,17 @@ static final class Node {
}
}

private record SkipListIterator(Node node) implements Iterator<Item> {

@Override
public boolean hasNext() {
return node.next[0] != null;
}

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

}
12 changes: 3 additions & 9 deletions src/main/java/com/tomfran/lsm/sstable/SSTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,7 @@ private void writeItems(String filename, Iterable<Item> items, int sampleSize, i
indexOs.close();
}

private static class SSTableIterator implements Iterator<Item> {

private final SSTable table;

public SSTableIterator(SSTable table) {
this.table = table;
}
private record SSTableIterator(SSTable table) implements Iterator<Item> {

@Override
public boolean hasNext() {
Expand All @@ -233,7 +227,7 @@ public Item next() {
*/
private static class SSTableMergerIterator extends IteratorMerger<Item> implements Iterable<Item> {

private Item last, next;
private Item last;

@SafeVarargs
public SSTableMergerIterator(Iterator<Item>... iterators) {
Expand All @@ -248,7 +242,7 @@ public boolean hasNext() {

@Override
public Item next() {
next = super.next();
Item next = super.next();
while (next != null && last.compareTo(next) == 0)
next = super.next();

Expand Down

0 comments on commit cef955b

Please sign in to comment.