Skip to content

Commit

Permalink
Reviewed flushing and compaction
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Feb 16, 2024
1 parent 1ec7983 commit 77c7b72
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 136 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ tasks.test {

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

static final Path DIR = Path.of("tree_add_benchmark");
static final int NUM_ITEMS = 1000000;
static final int MEMTABLE_SIZE = 1 << 18;
static final int LEVEL_SIZE = 5;
static final int MEMTABLE_SIZE = 1024 * 1024 * 256;
static final int LEVEL_SIZE = 2;
static int index = 0;

LSMTree tree;
Expand Down
4 changes: 2 additions & 2 deletions src/jmh/java/com/tomfran/lsm/tree/LSMTreeGetBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class LSMTreeGetBenchmark {

static final Path DIR = Path.of("tree_get_benchmark");
static final int NUM_ITEMS = 1000000;
static final int MEMTABLE_SIZE = 1 << 18;
static final int LEVEL_SIZE = 5;
static final int MEMTABLE_SIZE = 1024 * 1024 * 256;
static final int LEVEL_SIZE = 4;
static int index = 0;

LSMTree tree;
Expand Down
4 changes: 2 additions & 2 deletions src/jmh/java/com/tomfran/lsm/utils/BenchmarkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

public class BenchmarkUtils {

public static LSMTree initTree(Path dir, int memSize, int levelSize) {
public static LSMTree initTree(Path dir, int memSize, int immutableSize) {
if (Files.exists(dir))
deleteDir(dir);

return new LSMTree(memSize, levelSize, dir.toString());
return new LSMTree(memSize, immutableSize, dir.toString());
}

public static void stopTreeAndCleanDisk(LSMTree tree, Path dir) {
Expand Down
49 changes: 36 additions & 13 deletions src/main/java/com/tomfran/lsm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;
import java.util.Scanner;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Main {
Expand All @@ -18,30 +20,34 @@ public static void main(String[] args) throws InterruptedException {
if (new File(DIRECTORY).exists())
deleteDir();

LSMTree tree = new LSMTree(5, 2, DIRECTORY);
LSMTree tree = new LSMTree(1024 * 1024 * 5, 2, DIRECTORY);

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\n");

String intro = """
| __| \\ | __ __| \s
| \\__ \\ |\\/ | ____| | _| -_) -_)\s
____| ____/ _| _| _| _| \\___| \\___|\s
""";
| __| \\ | __ __| \s
| \\__ \\ |\\/ | ____| | _| -_) -_)\s
____| ____/ _| _| _| _| \\___| \\___|\s
""";

String help = """
Commands:
- s/set <key> <value> : insert a key-value pair;
- g/get <key> : get a key value;
- d/del <key> : delete a key;
- e/exit : stop the console;
- h/help : show this message.
""";
Commands:
- s/set <key> <value> : insert a key-value pair;
- r/rgn <start> <end> : insert this range of numeric keys with random values;
- g/get <key> : get a key value;
- d/del <key> : delete a key;
- p/prt : print current tree status;
- e/exit : stop the console;
- h/help : show this message.
""";

System.out.println(intro);
System.out.println(help);

Random r = new Random();

boolean exit = false;

while (!exit) {
Expand All @@ -56,6 +62,12 @@ public static void main(String[] args) throws InterruptedException {
tree.add(new ByteArrayPair(parts[1].getBytes(), parts[2].getBytes()));
System.out.println("ok");
}
case "r", "rng" -> {
IntStream.range(Integer.parseInt(parts[1]), Integer.parseInt(parts[2]))
.forEach(i -> tree.add(new ByteArrayPair(intToBytes(i), intToBytes(r.nextInt()))));

System.out.println("ok");
}
case "d", "del" -> {
tree.delete(parts[1].getBytes());
System.out.println("ok");
Expand All @@ -64,12 +76,14 @@ public static void main(String[] args) throws InterruptedException {
byte[] value = tree.get(parts[1].getBytes());
System.out.println((value == null || value.length == 0) ? "not found" : new String(value));
}
case "p", "prt" -> System.out.println(tree);
case "h", "help" -> System.out.println(help);
case "e", "exit" -> exit = true;
default -> System.out.println("Unknown command");
}
} catch (Exception e) {
System.out.printf("### error while executing command: \"%s\"\n", command);
e.printStackTrace();
}
}
tree.stop();
Expand All @@ -85,4 +99,13 @@ static private void deleteDir() {
}
}

static byte[] intToBytes(int i) {
byte[] result = new byte[4];
result[0] = (byte) (i & 0xFF);
result[1] = (byte) ((i >> 8) & 0xFF);
result[2] = (byte) ((i >> 16) & 0xFF);
result[3] = (byte) ((i >> 24) & 0xFF);
return result;
}

}
Loading

0 comments on commit 77c7b72

Please sign in to comment.