Skip to content

Sets commands implement #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/src/guide/overview/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ Implemented commands:

</details>

<details>
<summary>Set</summary>

- SADD
- SMEMBERS
- SISMEMBER
- SCARD
- SDIFF
- SINTER
- SUNION
- SMOVE
- SREM

</details>

<details>
<summary>Pub/Sub</summary>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public BulkReply execute() {
val threads = ManagementFactory.getThreadMXBean().getThreadCount();
String infoStr = "# Server\r\n" +
"keva_version: 1.0.0\r\n"
+ "io_threads_active: " + threads;
+ "io_threads_active: " + threads + "\r\n";
return new BulkReply(infoStr);
}
}
29 changes: 29 additions & 0 deletions server/src/main/java/dev/keva/server/command/impl/set/SAdd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

import java.util.Arrays;

@Component
@CommandImpl("sadd")
@ParamLength(type = ParamLength.Type.AT_LEAST, value = 2)
public class SAdd {
private final KevaDatabase database;

@Autowired
public SAdd(KevaDatabase database) {
this.database = database;
}

@Execute
public IntegerReply execute(byte[][] params) {
int added = database.sadd(params[0], Arrays.copyOfRange(params, 1, params.length));
return new IntegerReply(added);
}
}
27 changes: 27 additions & 0 deletions server/src/main/java/dev/keva/server/command/impl/set/SCard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

@Component
@CommandImpl("scard")
@ParamLength(1)
public class SCard {
private final KevaDatabase database;

@Autowired
public SCard(KevaDatabase database) {
this.database = database;
}

@Execute
public IntegerReply execute(byte[] key) {
int num = database.scard(key);
return new IntegerReply(num);
}
}
32 changes: 32 additions & 0 deletions server/src/main/java/dev/keva/server/command/impl/set/SDiff.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.protocol.resp.reply.MultiBulkReply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

@Component
@CommandImpl("sdiff")
@ParamLength(2)
public class SDiff {
private final KevaDatabase database;

@Autowired
public SDiff(KevaDatabase database) {
this.database = database;
}

@Execute
public MultiBulkReply execute(byte[]... keys) {
byte[][] diff = database.sdiff(keys);
BulkReply[] replies = new BulkReply[diff.length];
for (int i = 0; i < diff.length; i++) {
replies[i] = new BulkReply(diff[i]);
}
return new MultiBulkReply(replies);
}
}
32 changes: 32 additions & 0 deletions server/src/main/java/dev/keva/server/command/impl/set/SInter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.protocol.resp.reply.MultiBulkReply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

@Component
@CommandImpl("sinter")
@ParamLength(2)
public class SInter {
private final KevaDatabase database;

@Autowired
public SInter(KevaDatabase database) {
this.database = database;
}

@Execute
public MultiBulkReply execute(byte[]... keys) {
byte[][] diff = database.sinter(keys);
BulkReply[] replies = new BulkReply[diff.length];
for (int i = 0; i < diff.length; i++) {
replies[i] = new BulkReply(diff[i]);
}
return new MultiBulkReply(replies);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

@Component
@CommandImpl("sismember")
@ParamLength(2)
public class SIsMember {
private final KevaDatabase database;

@Autowired
public SIsMember(KevaDatabase database) {
this.database = database;
}

@Execute
public IntegerReply execute(byte[] key, byte[] value) {
boolean isMember = database.sismember(key, value);
return new IntegerReply(isMember ? 1 : 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.protocol.resp.reply.MultiBulkReply;
import dev.keva.protocol.resp.reply.Reply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

@Component
@CommandImpl("smembers")
@ParamLength(1)
public class SMembers {
private final KevaDatabase database;

@Autowired
public SMembers(KevaDatabase database) {
this.database = database;
}

@Execute
public MultiBulkReply execute(byte[] key) {
byte[][] result = database.smembers(key);
if (result == null) {
return new MultiBulkReply(new Reply[0]);
}
BulkReply[] replies = new BulkReply[result.length];
for (int i = 0; i < result.length; i++) {
replies[i] = new BulkReply(result[i]);
}
return new MultiBulkReply(replies);
}
}
27 changes: 27 additions & 0 deletions server/src/main/java/dev/keva/server/command/impl/set/SMove.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

@Component
@CommandImpl("smove")
@ParamLength(3)
public class SMove {
private final KevaDatabase database;

@Autowired
public SMove(KevaDatabase database) {
this.database = database;
}

@Execute
public IntegerReply execute(byte[] source, byte[] destination, byte[] member) {
int count = database.smove(source, destination, member);
return new IntegerReply(count);
}
}
29 changes: 29 additions & 0 deletions server/src/main/java/dev/keva/server/command/impl/set/SRem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.IntegerReply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

import java.util.Arrays;

@Component
@CommandImpl("srem")
@ParamLength(type = ParamLength.Type.AT_LEAST, value = 2)
public class SRem {
private final KevaDatabase database;

@Autowired
public SRem(KevaDatabase database) {
this.database = database;
}

@Execute
public IntegerReply execute(byte[][] params) {
int removed = database.srem(params[0], Arrays.copyOfRange(params, 1, params.length));
return new IntegerReply(removed);
}
}
32 changes: 32 additions & 0 deletions server/src/main/java/dev/keva/server/command/impl/set/SUnion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.keva.server.command.impl.set;

import dev.keva.ioc.annotation.Autowired;
import dev.keva.ioc.annotation.Component;
import dev.keva.protocol.resp.reply.BulkReply;
import dev.keva.protocol.resp.reply.MultiBulkReply;
import dev.keva.server.command.annotation.CommandImpl;
import dev.keva.server.command.annotation.Execute;
import dev.keva.server.command.annotation.ParamLength;
import dev.keva.store.KevaDatabase;

@Component
@CommandImpl("sunion")
@ParamLength(2)
public class SUnion {
private final KevaDatabase database;

@Autowired
public SUnion(KevaDatabase database) {
this.database = database;
}

@Execute
public MultiBulkReply execute(byte[]... keys) {
byte[][] diff = database.sunion(keys);
BulkReply[] replies = new BulkReply[diff.length];
for (int i = 0; i < diff.length; i++) {
replies[i] = new BulkReply(diff[i]);
}
return new MultiBulkReply(replies);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public void init() {
} catch (Exception e) {
log.error("", e);
if (e instanceof InvocationTargetException) {
if (e.getCause() instanceof ClassCastException) {
return new ErrorReply("ERR WRONGTYPE Operation against a key holding the wrong kind of value");
}
return new ErrorReply("ERR " + e.getCause().getMessage());
}
return new ErrorReply("ERR " + e.getMessage());
Expand Down
Loading