Skip to content

Commit

Permalink
Support stralgo command (#2586)
Browse files Browse the repository at this point in the history
* add redis6.0 command `stralgo` support

* Support stralgo command

StrAlgoParams for pass params to redis and StringMatchResult is the
result

* Add strAlgoLCSStrings command and remove strings option from
StrAlgoLCSParams

* Update src/main/java/redis/clients/jedis/resps/LCSMatchResult.java

Co-authored-by: ljwlc <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>
  • Loading branch information
3 people committed Aug 7, 2021
1 parent 91d6abb commit b4c1848
Show file tree
Hide file tree
Showing 26 changed files with 501 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,14 @@ public void strlen(final byte[] key) {
sendCommand(STRLEN, key);
}

public void strAlgoLCSKeys(final byte[] keyA, final byte[] keyB, final StrAlgoLCSParams params) {
sendCommand(STRALGO, params.getByteParams(Keyword.KEYS, keyA, keyB));
}

public void strAlgoLCSStrings(final byte[] strA, final byte[] strB, final StrAlgoLCSParams params) {
sendCommand(STRALGO, params.getByteParams(Keyword.STRINGS, strA, strB));
}

/**
* @deprecated This will be removed in next major release.
*/
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;

import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.args.*;
import redis.clients.jedis.commands.AdvancedBinaryJedisCommands;
import redis.clients.jedis.commands.BasicCommands;
Expand Down Expand Up @@ -3710,6 +3708,20 @@ public Long strlen(final byte[] key) {
return client.getIntegerReply();
}

@Override
public LCSMatchResult strAlgoLCSKeys(final byte[] keyA, final byte[] keyB, final StrAlgoLCSParams params) {
checkIsInMultiOrPipeline();
client.strAlgoLCSKeys(keyA, keyB, params);
return BuilderFactory.STR_ALGO_LCS_RESULT_BUILDER.build(client.getOne());
}

@Override
public LCSMatchResult strAlgoLCSStrings(final byte[] strA, final byte[] strB, final StrAlgoLCSParams params) {
checkIsInMultiOrPipeline();
client.strAlgoLCSStrings(strA, strB, params);
return BuilderFactory.STR_ALGO_LCS_RESULT_BUILDER.build(client.getOne());
}

/**
* @deprecated This will be removed in next major release.
*/
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package redis.clients.jedis;

import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.args.*;
import redis.clients.jedis.commands.BinaryJedisClusterCommands;
import redis.clients.jedis.commands.JedisClusterBinaryScriptingCommands;
Expand Down Expand Up @@ -989,6 +987,26 @@ public Long execute(Jedis connection) {
}.runBinary(key);
}

@Override
public LCSMatchResult strAlgoLCSKeys(final byte[] keyA, final byte[] keyB, final StrAlgoLCSParams params) {
return new JedisClusterCommand<LCSMatchResult>(connectionHandler, maxAttempts, maxTotalRetriesDuration) {
@Override
public LCSMatchResult execute(Jedis connection) {
return connection.strAlgoLCSKeys(keyA, keyB, params);
}
}.runBinary(2, keyA, keyB);
}

@Override
public LCSMatchResult strAlgoLCSStrings(final byte[] strA, final byte[] strB, final StrAlgoLCSParams params) {
return new JedisClusterCommand<LCSMatchResult>(connectionHandler, maxAttempts, maxTotalRetriesDuration) {
@Override
public LCSMatchResult execute(Jedis connection) {
return connection.strAlgoLCSStrings(strA, strB, params);
}
}.runWithAnyNode();
}

@Override
public Long zadd(final byte[] key, final double score, final byte[] member) {
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts, maxTotalRetriesDuration) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import redis.clients.jedis.params.GetExParams;
import redis.clients.jedis.params.RestoreParams;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.params.StrAlgoLCSParams;
import redis.clients.jedis.params.XAddParams;
import redis.clients.jedis.params.XAutoClaimParams;
import redis.clients.jedis.params.XClaimParams;
Expand All @@ -23,6 +24,7 @@
import redis.clients.jedis.params.ZAddParams;
import redis.clients.jedis.params.ZIncrByParams;
import redis.clients.jedis.params.LPosParams;
import redis.clients.jedis.resps.LCSMatchResult;
import redis.clients.jedis.util.Hashing;
import redis.clients.jedis.util.Sharded;

Expand Down Expand Up @@ -1342,4 +1344,10 @@ public Object sendBlockingCommand(ProtocolCommand cmd, byte[]... args) {
public Object sendCommand(ProtocolCommand cmd) {
return sendCommand(cmd, dummyArray);
}

@Override
public LCSMatchResult strAlgoLCSStrings(final byte[] strA, final byte[] strB, final StrAlgoLCSParams params) {
Jedis j = getShard("");
return j.strAlgoLCSStrings(strA, strB, params);
}
}
48 changes: 48 additions & 0 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.resps.LCSMatchResult.MatchedPosition;
import redis.clients.jedis.resps.LCSMatchResult.Position;
import redis.clients.jedis.resps.*;
import redis.clients.jedis.util.JedisByteHashMap;
import redis.clients.jedis.util.SafeEncoder;
Expand Down Expand Up @@ -1095,4 +1097,50 @@ private BuilderFactory() {
throw new InstantiationError("Must not instantiate this class");
}

public static final Builder<LCSMatchResult> STR_ALGO_LCS_RESULT_BUILDER = new Builder<LCSMatchResult>() {
@Override
public LCSMatchResult build(Object data) {
if (data == null) {
return null;
}

if (data instanceof byte[]) {
return new LCSMatchResult(STRING.build(data));
} else if (data instanceof Long) {
return new LCSMatchResult(LONG.build(data));
} else {
long len = 0;
List<MatchedPosition> matchedPositions = new ArrayList<>();

List<Object> objectList = (List<Object>) data;
if ("matches".equalsIgnoreCase(STRING.build(objectList.get(0)))) {
List<Object> matches = (List<Object>)objectList.get(1);
for (Object obj : matches) {
if (obj instanceof List<?>) {
List<Object> positions = (List<Object>) obj;
Position a = new Position(
LONG.build(((List<Object>) positions.get(0)).get(0)),
LONG.build(((List<Object>) positions.get(0)).get(1))
);
Position b = new Position(
LONG.build(((List<Object>) positions.get(1)).get(0)),
LONG.build(((List<Object>) positions.get(1)).get(1))
);
long matchLen = 0;
if (positions.size() >= 3) {
matchLen = LONG.build(positions.get(2));
}
matchedPositions.add(new MatchedPosition(a, b, matchLen));
}
}
}

if ("len".equalsIgnoreCase(STRING.build(objectList.get(2)))) {
len = LONG.build(objectList.get(3));
}
return new LCSMatchResult(matchedPositions, len);
}
}
};

}
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,16 @@ public void strlen(final String key) {
strlen(SafeEncoder.encode(key));
}

@Override
public void strAlgoLCSKeys(final String keyA, final String keyB, final StrAlgoLCSParams params) {
strAlgoLCSKeys(SafeEncoder.encode(keyA), SafeEncoder.encode(keyB), params);
}

@Override
public void strAlgoLCSStrings(final String strA, final String strB, final StrAlgoLCSParams params) {
strAlgoLCSStrings(SafeEncoder.encode(strA), SafeEncoder.encode(strB), params);
}

@Override
public void lpushx(final String key, final String... string) {
lpushx(SafeEncoder.encode(key), SafeEncoder.encodeMany(string));
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,34 @@ public Long strlen(final String key) {
return client.getIntegerReply();
}

/**
* Calculate the longest common subsequence of keyA and keyB.
* @param keyA keyA
* @param keyB keyB
* @param params the params
* @return According to StrAlgoLCSParams to decide to return content to fill LCSMatchResult.
*/
@Override
public LCSMatchResult strAlgoLCSKeys(final String keyA, final String keyB, final StrAlgoLCSParams params) {
checkIsInMultiOrPipeline();
client.strAlgoLCSKeys(keyA, keyB, params);
return BuilderFactory.STR_ALGO_LCS_RESULT_BUILDER.build(client.getOne());
}

/**
* Calculate the longest common subsequence of strA and strB.
* @param strA strA
* @param strB strB
* @param params the params
* @return According to StrAlgoLCSParams to decide to return content to fill LCSMatchResult.
*/
@Override
public LCSMatchResult strAlgoLCSStrings(final String strA, final String strB, final StrAlgoLCSParams params) {
checkIsInMultiOrPipeline();
client.strAlgoLCSStrings(strA, strB, params);
return BuilderFactory.STR_ALGO_LCS_RESULT_BUILDER.build(client.getOne());
}

@Override
public Long lpushx(final String key, final String... string) {
checkIsInMultiOrPipeline();
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,26 @@ public Long execute(Jedis connection) {
}.run(key);
}

@Override
public LCSMatchResult strAlgoLCSKeys(final String keyA, final String keyB, final StrAlgoLCSParams params) {
return new JedisClusterCommand<LCSMatchResult>(connectionHandler, maxAttempts, maxTotalRetriesDuration) {
@Override
public LCSMatchResult execute(Jedis connection) {
return connection.strAlgoLCSKeys(keyA, keyB, params);
}
}.run(2, keyA, keyB);
}

@Override
public LCSMatchResult strAlgoLCSStrings(final String strA, final String strB, final StrAlgoLCSParams params) {
return new JedisClusterCommand<LCSMatchResult>(connectionHandler, maxAttempts, maxTotalRetriesDuration) {
@Override
public LCSMatchResult execute(Jedis connection) {
return connection.strAlgoLCSStrings(strA, strB, params);
}
}.runWithAnyNode();
}

@Override
public Long zadd(final String key, final double score, final String member) {
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts, maxTotalRetriesDuration) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/MultiKeyPipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,16 @@ public Response<List<Map.Entry<String, List<StreamEntry>>>> xreadGroup(final Str
client.xreadGroup(groupname, consumer, xReadGroupParams, streams);
return getResponse(BuilderFactory.STREAM_READ_RESPONSE);
}

@Override
public Response<LCSMatchResult> strAlgoLCSKeys(final String keyA, final String keyB, final StrAlgoLCSParams params) {
client.strAlgoLCSKeys(keyA, keyB, params);
return getResponse(BuilderFactory.STR_ALGO_LCS_RESULT_BUILDER);
}

@Override
public Response<LCSMatchResult> strAlgoLCSKeys(final byte[] keyA, final byte[] keyB, final StrAlgoLCSParams params) {
client.strAlgoLCSKeys(keyA, keyB, params);
return getResponse(BuilderFactory.STR_ALGO_LCS_RESULT_BUILDER);
}
}
16 changes: 16 additions & 0 deletions src/main/java/redis/clients/jedis/PipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import redis.clients.jedis.params.GetExParams;
import redis.clients.jedis.params.RestoreParams;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.params.StrAlgoLCSParams;
import redis.clients.jedis.params.XAddParams;
import redis.clients.jedis.params.XAutoClaimParams;
import redis.clients.jedis.params.XClaimParams;
Expand All @@ -20,6 +21,7 @@
import redis.clients.jedis.params.ZAddParams;
import redis.clients.jedis.params.ZIncrByParams;
import redis.clients.jedis.params.LPosParams;
import redis.clients.jedis.resps.LCSMatchResult;

public abstract class PipelineBase extends Queable implements BinaryRedisPipeline, RedisPipeline {

Expand Down Expand Up @@ -2440,4 +2442,18 @@ public Response<Object> sendCommand(final byte[] sampleKey, final ProtocolComman
getClient(sampleKey).sendCommand(cmd, args);
return getResponse(BuilderFactory.RAW_OBJECT);
}

@Override
public Response<LCSMatchResult> strAlgoLCSStrings(final String strA, final String strB,
final StrAlgoLCSParams params) {
getClient("").strAlgoLCSStrings(strA, strB, params);
return getResponse(BuilderFactory.STR_ALGO_LCS_RESULT_BUILDER);
}

@Override
public Response<LCSMatchResult> strAlgoLCSStrings(final byte[] strA, final byte[] strB,
final StrAlgoLCSParams params) {
getClient("").strAlgoLCSStrings(strA, strB, params);
return getResponse(BuilderFactory.STR_ALGO_LCS_RESULT_BUILDER);
}
}
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static enum Command implements ProtocolCommand {
READONLY, READWRITE, GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS, GEORADIUS_RO, GEORADIUSBYMEMBER,
GEORADIUSBYMEMBER_RO, MODULE, BITFIELD, HSTRLEN, TOUCH, SWAPDB, MEMORY, XADD, XLEN, XDEL,
XTRIM, XRANGE, XREVRANGE, XREAD, XACK, XGROUP, XREADGROUP, XPENDING, XCLAIM, XAUTOCLAIM, ACL, XINFO,
BITFIELD_RO, LPOS, SMISMEMBER, ZMSCORE, BZPOPMIN, BZPOPMAX, BLMOVE, LMOVE, COPY, ROLE, FAILOVER;
BITFIELD_RO, LPOS, SMISMEMBER, ZMSCORE, BZPOPMIN, BZPOPMAX, BLMOVE, LMOVE, COPY, ROLE, FAILOVER, STRALGO;

private final byte[] raw;

Expand All @@ -290,7 +290,7 @@ public static enum Keyword implements Rawable {
BLOCK, NOACK, STREAMS, KEY, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, ID,
IDLE, TIME, RETRYCOUNT, FORCE, USAGE, SAMPLES, STREAM, GROUPS, CONSUMERS, HELP, FREQ, SETUSER,
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR, SAVE, JUSTID, WITHVALUES, UNBLOCK,
NOMKSTREAM, MINID, DB, ABSTTL, TO, TIMEOUT, ABORT;
NOMKSTREAM, MINID, DB, ABSTTL, TO, TIMEOUT, ABORT, LCS, STRINGS;

/**
* @deprecated This will be private in future. Use {@link #getRaw()}.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/ShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import redis.clients.jedis.params.GetExParams;
import redis.clients.jedis.params.RestoreParams;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.params.StrAlgoLCSParams;
import redis.clients.jedis.params.XAddParams;
import redis.clients.jedis.params.XAutoClaimParams;
import redis.clients.jedis.params.XClaimParams;
Expand All @@ -23,6 +24,7 @@
import redis.clients.jedis.params.ZIncrByParams;
import redis.clients.jedis.params.LPosParams;
import redis.clients.jedis.resps.KeyedListElement;
import redis.clients.jedis.resps.LCSMatchResult;
import redis.clients.jedis.util.Hashing;

public class ShardedJedis extends BinaryShardedJedis implements JedisCommands, Closeable {
Expand Down Expand Up @@ -1315,4 +1317,10 @@ public Object sendBlockingCommand(ProtocolCommand cmd, String... args) {
Jedis j = getShard(sampleKey);
return j.sendBlockingCommand(cmd, args);
}

@Override
public LCSMatchResult strAlgoLCSStrings(final String strA, final String strB, final StrAlgoLCSParams params) {
Jedis j = getShard("");
return j.strAlgoLCSStrings(strA, strB, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import redis.clients.jedis.*;
import redis.clients.jedis.params.*;
import redis.clients.jedis.resps.LCSMatchResult;

/**
* Common interface for sharded and non-sharded BinaryJedis
Expand Down Expand Up @@ -482,4 +483,6 @@ List<Object> xautoclaimJustId(byte[] key, byte[] groupName, byte[] consumerName,
List<StreamConsumersInfo> xinfoConsumers(byte[] key, byte[] group);

List<Object> xinfoConsumersBinary(byte[] key, byte[] group);

LCSMatchResult strAlgoLCSStrings(final byte[] strA, final byte[] strB, final StrAlgoLCSParams params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import redis.clients.jedis.*;
import redis.clients.jedis.params.*;
import redis.clients.jedis.resps.LCSMatchResult;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -450,4 +451,6 @@ Response<List<Object>> xautoclaimJustId(byte[] key, byte[] group, byte[] consume
Response<String> psetex(byte[] key, long milliseconds, byte[] value);

Response<Double> hincrByFloat(byte[] key, byte[] field, double increment);

Response<LCSMatchResult> strAlgoLCSStrings(final byte[] strA, final byte[] strB, final StrAlgoLCSParams params);
}
5 changes: 5 additions & 0 deletions src/main/java/redis/clients/jedis/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import redis.clients.jedis.params.ClientKillParams;
import redis.clients.jedis.params.RestoreParams;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.params.StrAlgoLCSParams;
import redis.clients.jedis.params.XAddParams;
import redis.clients.jedis.params.XAutoClaimParams;
import redis.clients.jedis.params.XClaimParams;
Expand Down Expand Up @@ -578,4 +579,8 @@ void xautoclaimJustId(String key, String group, String consumerName,
void xinfoGroup (String key);

void xinfoConsumers (String key, String group);

void strAlgoLCSKeys(final String keyA, final String keyB, final StrAlgoLCSParams params);

void strAlgoLCSStrings(final String strA, final String strB, final StrAlgoLCSParams params);
}
Loading

0 comments on commit b4c1848

Please sign in to comment.