Skip to content

Commit 2c7b717

Browse files
authored
Add support cas cad v2 (#4363)
* Add CAS/CAD commands * Register extensions for UnifiedJedis base tests * Add tests * Rm useless code * Move compare condition. * Format compare condition * Replace null checks with asserts
1 parent fa52d66 commit 2c7b717

File tree

12 files changed

+513
-5
lines changed

12 files changed

+513
-5
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@
544544
<include>**/ClientTestUtil.java</include>
545545
<include>**/ReflectionTestUtil.java</include>
546546
<include>**/*CommandFlags*.java</include>
547+
<include>**/*CompareCondition*.java</include>
547548
<include>**/*MSetExParams*.java</include>
548549
</includes>
549550
</configuration>

src/main/java/redis/clients/jedis/CommandObjects.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import redis.clients.jedis.timeseries.*;
3333
import redis.clients.jedis.timeseries.TimeSeriesProtocol.*;
3434
import redis.clients.jedis.util.KeyValue;
35+
import redis.clients.jedis.util.CompareCondition;
3536

3637
public class CommandObjects {
3738

@@ -346,6 +347,18 @@ public final CommandObject<Long> del(byte[]... keys) {
346347
return new CommandObject<>(commandArguments(DEL).keys((Object[]) keys), BuilderFactory.LONG);
347348
}
348349

350+
public final CommandObject<Long> delex(String key, CompareCondition cond) {
351+
CommandArguments ca = commandArguments(Command.DELEX).key(key);
352+
cond.addTo(ca);
353+
return new CommandObject<>(ca, BuilderFactory.LONG);
354+
}
355+
356+
public final CommandObject<Long> delex(byte[] key, CompareCondition cond) {
357+
CommandArguments ca = commandArguments(Command.DELEX).key(key);
358+
cond.addTo(ca);
359+
return new CommandObject<>(ca, BuilderFactory.LONG);
360+
}
361+
349362
public final CommandObject<Long> unlink(String key) {
350363
return new CommandObject<>(commandArguments(UNLINK).key(key), BuilderFactory.LONG);
351364
}
@@ -462,6 +475,10 @@ public final CommandObject<String> get(String key) {
462475
return new CommandObject<>(commandArguments(Command.GET).key(key), BuilderFactory.STRING);
463476
}
464477

478+
public final CommandObject<String> digestKey(String key) {
479+
return new CommandObject<>(commandArguments(Command.DIGEST).key(key), BuilderFactory.STRING);
480+
}
481+
465482
public final CommandObject<String> setGet(String key, String value) {
466483
return new CommandObject<>(commandArguments(Command.SET).key(key).add(value).add(Keyword.GET), BuilderFactory.STRING);
467484
}
@@ -479,6 +496,10 @@ public final CommandObject<String> getEx(String key, GetExParams params) {
479496
return new CommandObject<>(commandArguments(Command.GETEX).key(key).addParams(params), BuilderFactory.STRING);
480497
}
481498

499+
public final CommandObject<byte[]> digestKey(byte[] key) {
500+
return new CommandObject<>(commandArguments(Command.DIGEST).key(key), BuilderFactory.BINARY);
501+
}
502+
482503
public final CommandObject<byte[]> get(byte[] key) {
483504
return new CommandObject<>(commandArguments(Command.GET).key(key), BuilderFactory.BINARY);
484505
}

src/main/java/redis/clients/jedis/Jedis.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import redis.clients.jedis.Protocol.*;
2424
import redis.clients.jedis.args.*;
2525
import redis.clients.jedis.commands.*;
26+
import redis.clients.jedis.util.CompareCondition;
2627
import redis.clients.jedis.exceptions.InvalidURIException;
2728
import redis.clients.jedis.exceptions.JedisConnectionException;
2829
import redis.clients.jedis.exceptions.JedisException;
@@ -505,6 +506,12 @@ public byte[] get(final byte[] key) {
505506
return connection.executeCommand(commandObjects.get(key));
506507
}
507508

509+
@Override
510+
public byte[] digestKey(final byte[] key) {
511+
checkIsInMultiOrPipeline();
512+
return connection.executeCommand(commandObjects.digestKey(key));
513+
}
514+
508515
@Override
509516
public byte[] setGet(final byte[] key, final byte[] value) {
510517
checkIsInMultiOrPipeline();
@@ -573,6 +580,12 @@ public long del(final byte[]... keys) {
573580
checkIsInMultiOrPipeline();
574581
return connection.executeCommand(commandObjects.del(keys));
575582
}
583+
@Override
584+
public long delex(final byte[] key, final CompareCondition condition) {
585+
checkIsInMultiOrPipeline();
586+
return connection.executeCommand(commandObjects.delex(key, condition));
587+
}
588+
576589

577590
@Override
578591
public long del(final byte[] key) {
@@ -5153,6 +5166,18 @@ public String getEx(String key, GetExParams params) {
51535166
return connection.executeCommand(commandObjects.getEx(key, params));
51545167
}
51555168

5169+
@Override
5170+
public String digestKey(final String key) {
5171+
checkIsInMultiOrPipeline();
5172+
return connection.executeCommand(commandObjects.digestKey(key));
5173+
}
5174+
5175+
@Override
5176+
public long delex(final String key, final CompareCondition condition) {
5177+
checkIsInMultiOrPipeline();
5178+
return connection.executeCommand(commandObjects.delex(key, condition));
5179+
}
5180+
51565181
/**
51575182
* Test if the specified keys exist. The command returns the number of keys exist.
51585183
* Time complexity: O(N)

src/main/java/redis/clients/jedis/Protocol.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public static final byte[] toByteArray(final double value) {
283283

284284
public static enum Command implements ProtocolCommand {
285285

286-
PING, AUTH, HELLO, SET, GET, GETDEL, GETEX, EXISTS, DEL, UNLINK, TYPE, FLUSHDB, FLUSHALL, MOVE,
286+
PING, AUTH, HELLO, SET, GET, GETDEL, GETEX, DIGEST, EXISTS, DEL, DELEX, UNLINK, TYPE, FLUSHDB, FLUSHALL, MOVE,
287287
KEYS, RANDOMKEY, RENAME, RENAMENX, DUMP, RESTORE, DBSIZE, SELECT, SWAPDB, MIGRATE, ECHO, //
288288
EXPIRE, EXPIREAT, EXPIRETIME, PEXPIRE, PEXPIREAT, PEXPIRETIME, TTL, PTTL, // <-- key expiration
289289
MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, SORT_RO, INFO, SHUTDOWN, MONITOR, CONFIG, LCS, //
@@ -334,7 +334,7 @@ public static enum Keyword implements Rawable {
334334
STREAMS, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, IDLE, TIME, BLOCK, NOACK, CLAIM,
335335
RETRYCOUNT, STREAM, GROUPS, CONSUMERS, JUSTID, WITHVALUES, NOMKSTREAM, MINID, CREATECONSUMER,
336336
SETUSER, GETUSER, DELUSER, WHOAMI, USERS, CAT, GENPASS, LOG, SAVE, DRYRUN, COPY, AUTH, AUTH2,
337-
NX, XX, EX, PX, EXAT, PXAT, ABSTTL, KEEPTTL, INCR, LT, GT, CH, INFO, PAUSE, UNPAUSE, UNBLOCK,
337+
NX, XX, IFEQ, IFNE, IFDEQ, IFDNE, EX, PX, EXAT, PXAT, ABSTTL, KEEPTTL, INCR, LT, GT, CH, INFO, PAUSE, UNPAUSE, UNBLOCK,
338338
REV, WITHCOORD, WITHDIST, WITHHASH, ANY, FROMMEMBER, FROMLONLAT, BYRADIUS, BYBOX, BYLEX, BYSCORE,
339339
STOREDIST, TO, FORCE, TIMEOUT, DB, UNLOAD, ABORT, IDX, MINMATCHLEN, WITHMATCHLEN, FULL,
340340
DELETE, LIBRARYNAME, WITHCODE, DESCRIPTION, GETKEYS, GETKEYSANDFLAGS, DOCS, FILTERBY, DUMP,

src/main/java/redis/clients/jedis/UnifiedJedis.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import redis.clients.jedis.commands.SampleBinaryKeyedCommands;
1818
import redis.clients.jedis.commands.SampleKeyedCommands;
1919
import redis.clients.jedis.commands.RedisModuleCommands;
20+
import redis.clients.jedis.util.CompareCondition;
2021
import redis.clients.jedis.csc.Cache;
2122
import redis.clients.jedis.csc.CacheConfig;
2223
import redis.clients.jedis.csc.CacheConnection;
@@ -26,7 +27,6 @@
2627
import redis.clients.jedis.json.JsonSetParams;
2728
import redis.clients.jedis.json.Path;
2829
import redis.clients.jedis.json.Path2;
29-
import redis.clients.jedis.mcf.MultiDbCommandExecutor;
3030
import redis.clients.jedis.params.VAddParams;
3131
import redis.clients.jedis.params.VSimParams;
3232
import redis.clients.jedis.resps.RawVector;
@@ -585,6 +585,11 @@ public long del(String key) {
585585
return executeCommand(commandObjects.del(key));
586586
}
587587

588+
@Override
589+
public long delex(String key, CompareCondition condition) {
590+
return executeCommand(commandObjects.delex(key, condition));
591+
}
592+
588593
@Override
589594
public long del(String... keys) {
590595
return executeCommand(commandObjects.del(keys));
@@ -595,6 +600,11 @@ public long unlink(String key) {
595600
return executeCommand(commandObjects.unlink(key));
596601
}
597602

603+
@Override
604+
public long delex(byte[] key, CompareCondition condition) {
605+
return executeCommand(commandObjects.delex(key, condition));
606+
}
607+
598608
@Override
599609
public long unlink(String... keys) {
600610
return executeCommand(commandObjects.unlink(keys));
@@ -760,6 +770,11 @@ public String get(String key) {
760770
return executeCommand(commandObjects.get(key));
761771
}
762772

773+
@Override
774+
public String digestKey(String key) {
775+
return executeCommand(commandObjects.digestKey(key));
776+
}
777+
763778
@Override
764779
public String setGet(String key, String value) {
765780
return executeCommand(commandObjects.setGet(key, value));
@@ -795,6 +810,11 @@ public byte[] get(byte[] key) {
795810
return executeCommand(commandObjects.get(key));
796811
}
797812

813+
@Override
814+
public byte[] digestKey(byte[] key) {
815+
return executeCommand(commandObjects.digestKey(key));
816+
}
817+
798818
@Override
799819
public byte[] setGet(byte[] key, byte[] value) {
800820
return executeCommand(commandObjects.setGet(key, value));

src/main/java/redis/clients/jedis/commands/KeyBinaryCommands.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import redis.clients.jedis.args.ExpiryOption;
77
import redis.clients.jedis.params.MigrateParams;
88
import redis.clients.jedis.params.RestoreParams;
9+
import redis.clients.jedis.annots.Experimental;
10+
import redis.clients.jedis.util.CompareCondition;
911
import redis.clients.jedis.params.ScanParams;
1012
import redis.clients.jedis.params.SortingParams;
1113
import redis.clients.jedis.resps.ScanResult;
@@ -60,6 +62,16 @@ public interface KeyBinaryCommands {
6062

6163
long del(byte[] key);
6264

65+
/**
66+
* Experimental: Compare-and-delete guarded by value/digest condition.
67+
*/
68+
@Experimental
69+
long delex(byte[] key, CompareCondition condition);
70+
71+
/** Returns the 64-bit XXH3 digest hex (ASCII bytes) of the string value stored at key, or null if missing. */
72+
@Experimental
73+
byte[] digestKey(byte[] key);
74+
6375
long del(byte[]... keys);
6476

6577
long unlink(byte[] key);

src/main/java/redis/clients/jedis/commands/KeyCommands.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import redis.clients.jedis.args.ExpiryOption;
77
import redis.clients.jedis.params.MigrateParams;
88
import redis.clients.jedis.params.RestoreParams;
9+
import redis.clients.jedis.annots.Experimental;
10+
import redis.clients.jedis.util.CompareCondition;
911
import redis.clients.jedis.params.ScanParams;
1012
import redis.clients.jedis.params.SortingParams;
1113
import redis.clients.jedis.resps.ScanResult;
@@ -418,6 +420,19 @@ public interface KeyCommands {
418420
*/
419421
long del(String... keys);
420422

423+
/**
424+
* Experimental: Compare-and-delete guarded by value/digest condition.
425+
*/
426+
@Experimental
427+
long delex(String key, CompareCondition condition);
428+
429+
/**
430+
* Compute and return the 64-bit XXH3 digest hex of the string value stored at key. Returns null
431+
* if key does not exist.
432+
*/
433+
@Experimental
434+
String digestKey(String key);
435+
421436
/**
422437
* <b><a href="http://redis.io/commands/unlink">Unlink Command</a></b>
423438
* This command is very similar to {@link KeyCommands#del(String) DEL}: it removes the specified key.

src/main/java/redis/clients/jedis/params/SetParams.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
import redis.clients.jedis.CommandArguments;
66
import redis.clients.jedis.Protocol.Keyword;
7+
import redis.clients.jedis.annots.Experimental;
8+
import redis.clients.jedis.util.CompareCondition;
79

810
public class SetParams extends BaseSetExParams<SetParams> {
911

1012
private Keyword existance;
13+
private CompareCondition condition;
1114

1215
public static SetParams setParams() {
1316
return new SetParams();
@@ -91,8 +94,24 @@ public SetParams keepTtl() {
9194
return super.keepTtl();
9295
}
9396

97+
/**
98+
* Set a compare condition for compare-and-set operations.
99+
* @param condition the condition to apply
100+
* @return {@link SetParams}
101+
*/
102+
@Experimental
103+
public SetParams condition(CompareCondition condition) {
104+
this.condition = condition;
105+
return this;
106+
}
107+
94108
@Override
95109
public void addParams(CommandArguments args) {
110+
// Condition must be added before NX/XX per Redis command syntax
111+
if (condition != null) {
112+
condition.addTo(args);
113+
}
114+
96115
if (existance != null) {
97116
args.add(existance);
98117
}
@@ -105,11 +124,13 @@ public boolean equals(Object o) {
105124
if (this == o) return true;
106125
if (o == null || getClass() != o.getClass()) return false;
107126
SetParams setParams = (SetParams) o;
108-
return Objects.equals(existance, setParams.existance) && super.equals((BaseSetExParams) o);
127+
return Objects.equals(existance, setParams.existance)
128+
&& Objects.equals(condition, setParams.condition)
129+
&& super.equals((BaseSetExParams) o);
109130
}
110131

111132
@Override
112133
public int hashCode() {
113-
return Objects.hash(existance, super.hashCode());
134+
return Objects.hash(existance, condition, super.hashCode());
114135
}
115136
}

0 commit comments

Comments
 (0)