Skip to content

Commit

Permalink
faster random generation in skiplist
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Oct 3, 2023
1 parent c7fb7cc commit da5ac61
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/jmh/java/com/tomfran/lsm/sstable/SSTableBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void setup() throws IOException {

// sort and divide into inserted and skipped
var items = l.stream()
.sorted((a, b) -> ByteArrayComparator.compare(a.key(), b.key()))
.toList();
.sorted((a, b) -> ByteArrayComparator.compare(a.key(), b.key()))
.toList();

var inserted = new ObjectArrayList<ByteArrayPair>();
var skipped = new ObjectArrayList<ByteArrayPair>();
Expand Down Expand Up @@ -106,4 +106,5 @@ public void negativeAccess(Blackhole bh) {

index = (index + 1) % skippedArray.length;
}

}
1 change: 1 addition & 0 deletions src/main/java/com/tomfran/lsm/bloom/BloomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,5 @@ public void writeToFile(String filename) {

os.close();
}

}
1 change: 1 addition & 0 deletions src/main/java/com/tomfran/lsm/io/BaseInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ public void close() {
throw new RuntimeException(e);
}
}

}
1 change: 1 addition & 0 deletions src/main/java/com/tomfran/lsm/io/BaseOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ public void close() {
throw new RuntimeException(e);
}
}

}
6 changes: 5 additions & 1 deletion src/main/java/com/tomfran/lsm/memtable/SkipList.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public void add(ByteArrayPair item) {

private int randomLevel() {
int level = 1;
while (rn.nextBoolean() && level < levels)
long n = rn.nextLong();
while (level < levels && (n & (1L << level)) != 0)
level++;
return level;
}
Expand Down Expand Up @@ -155,13 +156,15 @@ public String toString() {
}

private static final class Node {

ByteArrayPair val;
Node[] next;

Node(ByteArrayPair val, int numLevels) {
this.val = val;
this.next = new Node[numLevels];
}

}

private record SkipListIterator(Node node) implements Iterator<ByteArrayPair> {
Expand All @@ -175,6 +178,7 @@ public boolean hasNext() {
public ByteArrayPair next() {
return node.next[0].val;
}

}

}
2 changes: 2 additions & 0 deletions src/main/java/com/tomfran/lsm/sstable/SSTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public ByteArrayPair next() {
remaining--;
return table.is.readBytePair();
}

}

/**
Expand Down Expand Up @@ -313,6 +314,7 @@ public ByteArrayPair next() {
public Iterator<ByteArrayPair> iterator() {
return this;
}

}

}
1 change: 1 addition & 0 deletions src/main/java/com/tomfran/lsm/tree/LSMTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ public byte[] get(byte[] key) {

return null;
}

}
1 change: 1 addition & 0 deletions src/main/java/com/tomfran/lsm/types/ByteArrayPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ public String toString() {
sb.append(")");
return sb.toString();
}

}
1 change: 1 addition & 0 deletions src/main/java/com/tomfran/lsm/utils/IteratorMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ public T next() {

return result;
}

}
5 changes: 3 additions & 2 deletions src/test/java/com/tomfran/lsm/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public static byte[] getRandomByteArray() {

public static List<byte[]> getRandomByteArrayList(int size) {
return IntStream.range(0, size)
.mapToObj(i -> getRandomByteArray())
.toList();
.mapToObj(i -> getRandomByteArray())
.toList();
}

public static void assertPairEqual(ByteArrayPair a, ByteArrayPair b) {
assert compare(a.key(), b.key()) == 0;
assert compare(a.value(), b.value()) == 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ void testAdd() {
});

int falsePositives = data.stream().limit(INSERTIONS)
.mapToInt(d -> b.mightContain(d) ? 1 : 0).sum();
.mapToInt(d -> b.mightContain(d) ? 1 : 0).sum();

double falsePositiveRate = falsePositives / (double) INSERTIONS;
results.add(falsePositiveRate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public void shouldReconstruct() {
assert bf.bits[i] == bf2.bits[i];
}
}

}
4 changes: 2 additions & 2 deletions src/test/java/com/tomfran/lsm/memtable/SkipListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static void setup() {
items = new ObjectArrayList<>();

Stream.generate(TestUtils::getRandomPair)
.limit(100)
.forEach(items::add);
.limit(100)
.forEach(items::add);

items.forEach(l::add);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static void setup() throws IOException {
}

var items = l.stream()
.sorted((a, b) -> compare(a.key(), b.key()))
.toList();
.sorted((a, b) -> compare(a.key(), b.key()))
.toList();

t1 = new SSTable(tempDirectory + FILE1, items, 3, items.size());

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/tomfran/lsm/sstable/SSTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static void setup() {

// sort and divide into inserted and skipped
var items = l.stream()
.sorted((a, b) -> ByteArrayComparator.compare(a.key(), b.key()))
.toList();
.sorted((a, b) -> ByteArrayComparator.compare(a.key(), b.key()))
.toList();

inserted = new ObjectArrayList<>();
skipped = new ObjectArrayList<>();
Expand Down Expand Up @@ -88,4 +88,5 @@ public void iteratorTest() {

assert !it2.hasNext();
}

}
4 changes: 2 additions & 2 deletions src/test/java/com/tomfran/lsm/utils/IteratorMergerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class IteratorMergerTest {
public void shouldMerge() {

List<Iterator<Integer>> iteratorList = IntStream.range(0, 10)
.mapToObj(i -> IntStream.range(i * 10, i * 10 + 10).boxed().iterator())
.collect(Collectors.toList());
.mapToObj(i -> IntStream.range(i * 10, i * 10 + 10).boxed().iterator())
.collect(Collectors.toList());

Collections.shuffle(iteratorList);

Expand Down

0 comments on commit da5ac61

Please sign in to comment.