Skip to content

Commit

Permalink
sstable list
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Oct 8, 2023
1 parent d90b6b8 commit ab62125
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ tasks.test {
}

jmh {
// includes = ['LSMTreeBenchmark.add']
// includes = ['SSTableBenchmark.*']
fork = 1
warmupIterations = 5
iterations = 10
warmupIterations = 3
iterations = 5
benchmarkMode = ['avgt']
jmhTimeout = '15s'
jmhVersion = '1.37'
Expand Down
4 changes: 2 additions & 2 deletions src/jmh/java/com/tomfran/lsm/tree/LSMTreeBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
public class LSMTreeBenchmark {

static final Path DIR = Path.of("tree_benchmark");
static final int NUM_ITEMS = 300000;
static final int MEMTABLE_SIZE = 1 << 16;
static final int NUM_ITEMS = 1000000;
static final int MEMTABLE_SIZE = 1 << 18;

static ByteArrayPair[] items;
static int index = 0;
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/com/tomfran/lsm/tree/LSMTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class LSMTree {

Memtable mutableMemtable;
LinkedList<Memtable> immutableMemtables;
SSTable table;
LinkedList<SSTable> tables;
ExecutorService memtableFlusher;

/**
Expand All @@ -49,6 +49,7 @@ public LSMTree(int memtableMaxSize, String dataDir) {

mutableMemtable = new Memtable(memtableMaxSize);
immutableMemtables = new LinkedList<>();
tables = new LinkedList<>();
memtableFlusher = newSingleThreadExecutor();
}

Expand Down Expand Up @@ -100,8 +101,9 @@ public byte[] get(byte[] key) {
}

synchronized (tableLock) {
if ((result = table.get(key)) != null)
return result;
for (SSTable table : tables)
if ((result = table.get(key)) != null)
return result;
}

return null;
Expand Down Expand Up @@ -136,13 +138,14 @@ private void flushLastMemtable() {
String filename = String.format("%s/sst_%d", dataDir, System.currentTimeMillis());

synchronized (tableLock) {
if (table == null)
table = new SSTable(filename, memtableToFlush.iterator(), DEFAULT_SSTABLE_SAMPLE_SIZE);
else {
SSTable newTable = SSTable.merge(filename, DEFAULT_SSTABLE_SAMPLE_SIZE, memtableToFlush, table);
table.deleteFiles();
table = newTable;
}
tables.addFirst(new SSTable(filename, memtableToFlush.iterator(), DEFAULT_SSTABLE_SAMPLE_SIZE));
// if (table == null)
// table = ;
// else {
// SSTable newTable = SSTable.merge(filename, DEFAULT_SSTABLE_SAMPLE_SIZE, memtableToFlush, table);
// table.deleteFiles();
// table = newTable;
// }
}

// remove flushed memtable from immutable memtables
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/tomfran/lsm/tree/LSMTreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public void writeFlush() throws InterruptedException {

Thread.sleep(2000);

assert tree.mutableMemtable.size() == 1 : "mutable memtable size is " + tree.mutableMemtable.size();
assert tree.table != null : "table is null";
assert tree.mutableMemtable.size() >= 1 : "mutable memtable size is " + tree.mutableMemtable.size();
assert !tree.tables.isEmpty() : "table is null";
}

@Test
Expand All @@ -37,7 +37,7 @@ public void writeFlow() throws InterruptedException {

Object2ObjectArrayMap<byte[], byte[]> items = new Object2ObjectArrayMap<>();

IntStream.range(0, 2 * maxSize).forEach(i -> {
IntStream.range(0, 10 * maxSize).forEach(i -> {
var it = getRandomPair();
tree.add(it);
items.put(it.key(), it.value());
Expand Down

0 comments on commit ab62125

Please sign in to comment.