-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LSM tree first draft, flushing not working
- Loading branch information
Showing
18 changed files
with
445 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.tomfran.lsm.tree; | ||
|
||
import com.tomfran.lsm.types.ByteArrayPair; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.tomfran.lsm.TestUtils.getRandomPair; | ||
|
||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@State(Scope.Benchmark) | ||
public class LSMTreeBenchmark { | ||
|
||
static final Path DIR = Path.of("tree_benchmark"); | ||
static final int NUM_ITEMS = 300000; | ||
|
||
static ByteArrayPair[] items; | ||
static int index = 0; | ||
|
||
LSMTree tree; | ||
|
||
@Setup | ||
public void setup() throws IOException { | ||
// setup directory | ||
if (Files.exists(DIR)) | ||
deleteDir(); | ||
|
||
// generate random items | ||
items = new ByteArrayPair[NUM_ITEMS]; | ||
for (int i = 0; i < NUM_ITEMS; i++) | ||
items[i] = getRandomPair(); | ||
|
||
// setup tree | ||
tree = new LSMTree(1 << 15, DIR.toString()); | ||
} | ||
|
||
@TearDown | ||
public void teardown() throws IOException { | ||
tree.stop(); | ||
deleteDir(); | ||
} | ||
|
||
private void deleteDir() throws IOException { | ||
try (var files = Files.list(DIR)) { | ||
files.forEach(f -> { | ||
try { | ||
Files.delete(f); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
Files.delete(DIR); | ||
} | ||
|
||
@Benchmark | ||
public void add() { | ||
var item = items[index]; | ||
tree.add(item); | ||
|
||
index = (index + 1) % NUM_ITEMS; | ||
} | ||
|
||
@Benchmark | ||
public void get(Blackhole bh) { | ||
var item = items[index]; | ||
var value = tree.get(item.key()); | ||
|
||
bh.consume(value); | ||
|
||
index = (index + 1) % NUM_ITEMS; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.tomfran.lsm; | ||
|
||
import com.tomfran.lsm.tree.LSMTree; | ||
import com.tomfran.lsm.types.ByteArrayPair; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
|
||
static final String DIRECTORY = "LSM-data"; | ||
|
||
public static void main(String[] args) throws IOException { | ||
|
||
if (new File(DIRECTORY).exists()) | ||
deleteDir(); | ||
|
||
LSMTree tree = new LSMTree(3, DIRECTORY); | ||
|
||
Scanner scanner = new Scanner(System.in); | ||
scanner.useDelimiter("\n"); | ||
|
||
System.out.println( | ||
""" | ||
LSM Tree console | ||
Commands: | ||
- ins <key> <value> : insert a key-value pair | ||
- get <key> : get a value for a key | ||
- del <key> : delete a key-value pair | ||
- exit : exit the application | ||
""" | ||
); | ||
|
||
boolean exit = false; | ||
|
||
while (!exit) { | ||
System.out.print("Enter a command: "); | ||
String command = scanner.nextLine(); | ||
|
||
var parts = command.split(" "); | ||
|
||
switch (parts[0]) { | ||
case "exit" -> { | ||
System.out.println("Exiting..."); | ||
exit = true; | ||
} | ||
case "ins" -> tree.add(new ByteArrayPair(parts[1].getBytes(), parts[2].getBytes())); | ||
case "del" -> tree.delete(parts[1].getBytes()); | ||
case "get" -> { | ||
String key = parts[1]; | ||
byte[] value = tree.get(key.getBytes()); | ||
|
||
var msg = (value == null || value.length == 0) ? "No value found for key " + key : | ||
"Value for key " + key + " is " + new String(value); | ||
System.out.println(msg); | ||
} | ||
default -> System.out.println("Unknown command: " + command); | ||
} | ||
System.out.println(); | ||
} | ||
tree.stop(); | ||
scanner.close(); | ||
|
||
deleteDir(); | ||
} | ||
|
||
static private void deleteDir() throws IOException { | ||
try (var files = Files.list(Path.of(DIRECTORY))) { | ||
files.forEach(f -> { | ||
try { | ||
Files.delete(f); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
Files.delete(Path.of(DIRECTORY)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.