Skip to content

Commit

Permalink
Fix bugs + improve index by using .nanos instead of .millis
Browse files Browse the repository at this point in the history
  • Loading branch information
lant committed Oct 27, 2023
1 parent 39f3538 commit 84c059e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/github/lant/wal/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, InterruptedException {
Database db = new Database();
for (int i = 0; i < 1000000; i++) {
db.writeKeyValue("key"+i, "value"+i);
if (i % 1000 == 0) {
if (i % 100000 == 0) {
System.out.println("Inserting data: " + i);
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/com/github/lant/wal/text/TextFileWal.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private String getWalFileName(int idx) {
@Override
public long write(String key, String value) {
// "serialise"
long now = System.currentTimeMillis();
long now = System.nanoTime();
String serialised = now + "-" + key + "-" + value + "\n";
try {
// write into the file.
Expand Down Expand Up @@ -123,19 +123,19 @@ public Stream<Data> getBacklog() {
// get commit log
try {
BufferedReader commitLogReader = new BufferedReader(new FileReader(commitLog));
String line = commitLogReader.readLine();
String line = commitLogReader.readLine();
commitLogReader.close();
long lastCommitedIdx;
long lastCommitedIdx;
if (line != null) {
lastCommitedIdx = Long.parseLong(line);
lastCommitedIdx = Long.parseLong(line);
} else {
lastCommitedIdx = 0L;
}
logger.info("Latest commit IDX = " + lastCommitedIdx);
logger.info("Latest commit IDX = " + lastCommitedIdx);

// go through wal files to see if we find that commit log / something newer
List<Path> walFiles = Files.list(Path.of("/tmp/wal/"))
.sorted().filter(file -> !file.getFileName().toString().matches("commit.log")).toList();
.sorted().filter(file -> !file.getFileName().toString().matches("commit.log")).toList();

return walFiles.stream()
.flatMap(this::fileToDataStream)
Expand All @@ -152,20 +152,20 @@ private boolean filterAlreadyCommitedIndexes(Data dataRecord, long lastCommitedI
if (!allowed) {
logger.info("Filtering IDX: " + dataRecord.getIdx() + " as it's lower than the last committed IDX("+lastCommitedIdx+")");
}
return allowed;
return allowed;
}

private Stream<Data> fileToDataStream(Path file) {
try {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file.toString()))) {
return bufferedReader.lines().map(line -> {
String[] parts = line.split("-");
long idx = Long.parseLong(parts[0]);
String key = parts[1];
String value = parts[2];
return new Data(key, value, idx);
});
}
BufferedReader bufferedReader = new BufferedReader(new FileReader(file.toString()));
return bufferedReader.lines().map(line -> {
String[] parts = line.split("-");
long idx = Long.parseLong(parts[0]);
String key = parts[1];
String value = parts[2];
return new Data(key, value, idx);
});

} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 84c059e

Please sign in to comment.