Skip to content

Commit

Permalink
Add FLUSHDB command
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanja-m committed Nov 30, 2024
1 parent f1d7cd3 commit 503cb9c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions kiwi-core/src/main/java/kiwi/core/storage/KeyValueStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface KeyValueStore<K, V> extends AutoCloseable {
boolean contains(K key);

int size();

void purge();
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ public int size() {
return keyDir.size();
}

private Supplier<LogSegment> activeSegmentSupplier() {
return () -> activeSegment;
@Override
public void purge() {
keyDir.keys().asIterator().forEachRemaining(this::delete);
}

@Override
Expand All @@ -164,6 +165,10 @@ public void close() {
writer.close();
}

private Supplier<LogSegment> activeSegmentSupplier() {
return () -> activeSegment;
}

public static Builder Builder() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ void testRollActiveSegment() throws IOException {
assertEquals("3", store.get(Bytes.wrap("c")).orElseThrow().toString());
}

@Test
void testPurge() {
BitcaskStore store = BitcaskStore.open(root);
store.put(Bytes.wrap("k1"), Bytes.wrap("v1"));
store.put(Bytes.wrap("k2"), Bytes.wrap("v2"));
store.put(Bytes.wrap("k3"), Bytes.wrap("v3"));
store.delete(Bytes.wrap("k1"));

assertEquals(2, store.size());

store.purge();

assertEquals(0, store.size());
}

@Test
void testFileClose() throws IOException {
FileChannel channel = FileChannel.open(root.resolve("test.log"), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
Expand Down
5 changes: 5 additions & 0 deletions kiwi-embedded/src/main/java/kiwi/embedded/Kiwi.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public int size() {
return store.size();
}

@Override
public void purge() {
store.purge();
}

@Override
public void close() throws Exception {
store.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public enum CommandType {
DEL,
EXISTS,
DBSIZE,
FLUSHDB,
UNKNOWN,
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected void channelRead0(ChannelHandlerContext ctx, RESPCommand command) {
case DEL -> handleDelete(ctx, command);
case EXISTS -> handleExists(ctx, command);
case DBSIZE -> handleSize(ctx, command);
case FLUSHDB -> handleFlush(ctx, command);
case UNKNOWN -> handleUnknown(ctx, command);
}
}
Expand Down Expand Up @@ -118,6 +119,11 @@ private void handleSize(ChannelHandlerContext ctx, RESPCommand ignoredCommand) {
ctx.writeAndFlush(size);
}

private void handleFlush(ChannelHandlerContext ctx, RESPCommand ignoredCommand) {
db.purge();
ctx.writeAndFlush("OK");
}

private void handleUnknown(ChannelHandlerContext ctx, RESPCommand command) {
ctx.writeAndFlush(new Throwable("unknown command: " + command.commandType()));
}
Expand Down

0 comments on commit 503cb9c

Please sign in to comment.