From ab62125ffab3aec42dc0f1a5f71bb32841b8a076 Mon Sep 17 00:00:00 2001 From: Fran Date: Sun, 8 Oct 2023 22:24:43 +0200 Subject: [PATCH] sstable list --- build.gradle | 6 ++--- .../tomfran/lsm/tree/LSMTreeBenchmark.java | 4 ++-- .../java/com/tomfran/lsm/tree/LSMTree.java | 23 +++++++++++-------- .../com/tomfran/lsm/tree/LSMTreeTest.java | 6 ++--- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index 757d7bf..d0874f3 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/jmh/java/com/tomfran/lsm/tree/LSMTreeBenchmark.java b/src/jmh/java/com/tomfran/lsm/tree/LSMTreeBenchmark.java index 9daefc4..a914cae 100644 --- a/src/jmh/java/com/tomfran/lsm/tree/LSMTreeBenchmark.java +++ b/src/jmh/java/com/tomfran/lsm/tree/LSMTreeBenchmark.java @@ -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; diff --git a/src/main/java/com/tomfran/lsm/tree/LSMTree.java b/src/main/java/com/tomfran/lsm/tree/LSMTree.java index ffc3e75..80bd381 100644 --- a/src/main/java/com/tomfran/lsm/tree/LSMTree.java +++ b/src/main/java/com/tomfran/lsm/tree/LSMTree.java @@ -26,7 +26,7 @@ public class LSMTree { Memtable mutableMemtable; LinkedList immutableMemtables; - SSTable table; + LinkedList tables; ExecutorService memtableFlusher; /** @@ -49,6 +49,7 @@ public LSMTree(int memtableMaxSize, String dataDir) { mutableMemtable = new Memtable(memtableMaxSize); immutableMemtables = new LinkedList<>(); + tables = new LinkedList<>(); memtableFlusher = newSingleThreadExecutor(); } @@ -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; @@ -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 diff --git a/src/test/java/com/tomfran/lsm/tree/LSMTreeTest.java b/src/test/java/com/tomfran/lsm/tree/LSMTreeTest.java index a002c0d..2cc0a1a 100644 --- a/src/test/java/com/tomfran/lsm/tree/LSMTreeTest.java +++ b/src/test/java/com/tomfran/lsm/tree/LSMTreeTest.java @@ -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 @@ -37,7 +37,7 @@ public void writeFlow() throws InterruptedException { Object2ObjectArrayMap 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());