Skip to content

Commit

Permalink
Keywords implements ProtocolCommand (#24)
Browse files Browse the repository at this point in the history
* Keywords implements ProtocolCommand
* remove duplicated code
  • Loading branch information
gkorland authored Aug 19, 2020
1 parent 16c525c commit 23ef2ae
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
25 changes: 16 additions & 9 deletions src/main/java/io/rebloom/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ public void createFilter(String name, long initCapacity, double errorRate) {
* @return true if the item was not previously in the filter.
*/
public boolean add(String name, String value) {
try (Jedis conn = _conn()) {
return sendCommand(conn, Command.ADD, SafeEncoder.encode(name), SafeEncoder.encode(value)).getIntegerReply() != 0;
}
return add(name, SafeEncoder.encode(value));
}

/**
Expand All @@ -113,7 +111,7 @@ public boolean add(String name, byte[] value) {
public boolean[] insert(String name, InsertOptions options, String... items) {
final List<byte[]> args = new ArrayList<>();
args.addAll(options.getOptions());
args.add(Keyword.ITEMS.raw);
args.add(Keywords.ITEMS.getRaw());
for (String item : items) {
args.add(SafeEncoder.encode(item));
}
Expand Down Expand Up @@ -147,14 +145,25 @@ private final boolean[] sendMultiCommand(Command cmd, byte[] name, byte[]... val
* filter or not. A true value means the item did not previously exist, whereas a
* false value means it may have previously existed.
*
* @see #add(String, String)
* @see #add(String, byte[])
*/
public boolean[] addMulti(String name, byte[] ...values) {
return sendMultiCommand(Command.MADD, SafeEncoder.encode(name), values);
}

/**
* Add one or more items to a filter
* @param name Name of the filter
* @param values values to add to the filter.
* @return An array of booleans of the same length as the number of values.
* Each boolean values indicates whether the corresponding element was previously in the
* filter or not. A true value means the item did not previously exist, whereas a
* false value means it may have previously existed.
*
* @see #add(String, String)
*/
public boolean[] addMulti(String name, String ...values) {
return sendMultiCommand(Command.MADD, SafeEncoder.encode(name), SafeEncoder.encodeMany(values));
return addMulti(name, SafeEncoder.encodeMany(values));
}

/**
Expand All @@ -164,9 +173,7 @@ public boolean[] addMulti(String name, String ...values) {
* @return true if the item may exist in the filter, false if the item does not exist in the filter
*/
public boolean exists(String name, String value) {
try (Jedis conn = _conn()) {
return sendCommand(conn, Command.EXISTS, SafeEncoder.encode(name), SafeEncoder.encode(value)).getIntegerReply() != 0;
}
return exists(name, SafeEncoder.encode(value));
}

/**
Expand Down
22 changes: 5 additions & 17 deletions src/main/java/io/rebloom/client/ClusterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,7 @@ public Boolean execute(Jedis connection) {
* @return true if the item was not previously in the filter.
*/
public boolean add(String name, String value) {
return (new JedisClusterCommand<Boolean>(this.connectionHandler, this.maxAttempts) {
public Boolean execute(Jedis connection) {
Connection conn = connection.getClient();
conn.sendCommand(Command.ADD, name, value);
return conn.getIntegerReply() != 0;
}
}).run(name);
return add(name, SafeEncoder.encode(value));
}

/**
Expand Down Expand Up @@ -165,7 +159,7 @@ public boolean[] execute(Jedis connection) {
Connection conn = connection.getClient();
final List<byte[]> args = new ArrayList<>();
args.addAll(options.getOptions());
args.add(Keyword.ITEMS.raw);
args.add(Keywords.ITEMS.getRaw());
for (String item : items) {
args.add(SafeEncoder.encode(item));
}
Expand All @@ -181,13 +175,7 @@ public boolean[] execute(Jedis connection) {
* @return true if the item may exist in the filter, false if the item does not exist in the filter
*/
public boolean exists(String name, String value) {
return (new JedisClusterCommand<Boolean>(this.connectionHandler, this.maxAttempts) {
public Boolean execute(Jedis connection) {
Connection conn = connection.getClient();
conn.sendCommand(Command.EXISTS, name, value);
return conn.getIntegerReply() != 0;
}
}).run(name);
return exists(name, SafeEncoder.encode(value));
}

/**
Expand Down Expand Up @@ -410,10 +398,10 @@ private final <T> boolean[] sendMultiCommand(Connection conn, Command cmd, T nam
arr.addAll(Arrays.asList(value));
List<Long> reps;
if (name instanceof String) {
conn.sendCommand(cmd, (String[]) arr.toArray((String[]) value));
conn.sendCommand(cmd, arr.toArray((String[]) value));
reps = conn.getIntegerMultiBulkReply();
} else {
conn.sendCommand(cmd, (byte[][]) arr.toArray((byte[][]) value));
conn.sendCommand(cmd, arr.toArray((byte[][]) value));
reps = conn.getIntegerMultiBulkReply();
}
boolean[] ret = new boolean[value.length];
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/rebloom/client/InsertOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Collections;
import java.util.List;

import static io.rebloom.client.Keyword.*;
import static io.rebloom.client.Keywords.*;

public class InsertOptions {
private final List<byte[]> options = new ArrayList<>();
Expand All @@ -17,7 +17,7 @@ public class InsertOptions {
* @return
*/
public InsertOptions capacity(final long capacity) {
options.add(CAPACITY.raw);
options.add(CAPACITY.getRaw());
options.add(Protocol.toByteArray(capacity));
return this;
}
Expand All @@ -28,7 +28,7 @@ public InsertOptions capacity(final long capacity) {
* @return
*/
public InsertOptions error(final double errorRate) {
options.add(ERROR.raw);
options.add(ERROR.getRaw());
options.add(Protocol.toByteArray(errorRate));
return this;
}
Expand All @@ -39,7 +39,7 @@ public InsertOptions error(final double errorRate) {
* @return
*/
public InsertOptions nocreate() {
options.add(NOCREATE.raw);
options.add(NOCREATE.getRaw());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package io.rebloom.client;

import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.util.SafeEncoder;

import java.util.Locale;

public enum Keyword {
public enum Keywords implements ProtocolCommand {
CAPACITY,
ERROR,
NOCREATE,
ITEMS;

public final byte[] raw;
private final byte[] raw;

Keyword() {
Keywords() {
raw = SafeEncoder.encode(this.name().toLowerCase(Locale.ENGLISH));
}

@Override
public byte[] getRaw() {
return raw;
}
}

0 comments on commit 23ef2ae

Please sign in to comment.