Skip to content

Commit 25178aa

Browse files
committed
Merge branch 'master' into add-support-cas-cad-v2
2 parents 1e9fa07 + c8135e2 commit 25178aa

30 files changed

+1320
-101
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@
518518
<include>src/test/java/redis/clients/jedis/commands/jedis/ClusterStreamsCommandsTest.java</include>
519519
<include>src/test/java/redis/clients/jedis/commands/jedis/PooledStreamsCommandsTest.java</include>
520520
<include>src/test/java/redis/clients/jedis/resps/StreamEntryDeletionResultTest.java</include>
521+
<include>src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsStringCommandsTest.java</include>
522+
<include>src/test/java/redis/clients/jedis/commands/jedis/BinaryValuesCommandsTest.java</include>
523+
<include>src/test/java/redis/clients/jedis/commands/jedis/StringValuesCommandsTest.java</include>
524+
<include>src/test/java/redis/clients/jedis/commands/unified/BinaryValuesCommandsTestBase.java</include>
525+
<include>src/test/java/redis/clients/jedis/commands/unified/StringValuesCommandsTestBase.java</include>
526+
<include>src/test/java/redis/clients/jedis/commands/unified/cluster/ClusterStringValuesCommandsTest.java</include>
521527
<include>**/VectorSet*.java</include>
522528
<include>**/VectorTestUtils.java</include>
523529
<include>**/VAddParams.java</include>
@@ -539,6 +545,7 @@
539545
<include>**/ReflectionTestUtil.java</include>
540546
<include>**/*CommandFlags*.java</include>
541547
<include>**/*CompareCondition*.java</include>
548+
<include>**/*MSetExParams*.java</include>
542549
</includes>
543550
</configuration>
544551
<executions>

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

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,17 +1408,26 @@ public List<StreamEntry> build(Object data) {
14081408
String entryIdString = SafeEncoder.encode((byte[]) res.get(0));
14091409
StreamEntryID entryID = new StreamEntryID(entryIdString);
14101410
List<byte[]> hash = (List<byte[]>) res.get(1);
1411-
if (hash == null) {
1412-
responses.add(new StreamEntry(entryID, null));
1413-
continue;
1411+
1412+
Map<String, String> fieldsMap = null;
1413+
1414+
if (hash != null) {
1415+
Iterator<byte[]> hashIterator = hash.iterator();
1416+
fieldsMap = new HashMap<>(hash.size() / 2, 1f);
1417+
1418+
while (hashIterator.hasNext()) {
1419+
fieldsMap.put(SafeEncoder.encode(hashIterator.next()), SafeEncoder.encode(hashIterator.next()));
1420+
}
14141421
}
14151422

1416-
Iterator<byte[]> hashIterator = hash.iterator();
1417-
Map<String, String> map = new HashMap<>(hash.size() / 2, 1f);
1418-
while (hashIterator.hasNext()) {
1419-
map.put(SafeEncoder.encode(hashIterator.next()), SafeEncoder.encode(hashIterator.next()));
1423+
if (res.size() >= 4) {
1424+
Long millisElapsedFromDelivery = LONG.build(res.get(2));
1425+
Long deliveredCount = LONG.build(res.get(3));
1426+
responses.add(new StreamEntry(entryID, fieldsMap, millisElapsedFromDelivery, deliveredCount));
1427+
continue;
14201428
}
1421-
responses.add(new StreamEntry(entryID, map));
1429+
1430+
responses.add(new StreamEntry(entryID, fieldsMap));
14221431
}
14231432

14241433
return responses;
@@ -1959,16 +1968,25 @@ public List<StreamEntryBinary> build(Object data) {
19591968
String entryIdString = SafeEncoder.encode((byte[]) res.get(0));
19601969
StreamEntryID entryID = new StreamEntryID(entryIdString);
19611970
List<byte[]> hash = (List<byte[]>) res.get(1);
1962-
if (hash == null) {
1963-
responses.add(new StreamEntryBinary(entryID, null));
1964-
continue;
1971+
1972+
Map<byte[], byte[]> map = null;
1973+
1974+
if (hash != null) {
1975+
Iterator<byte[]> hashIterator = hash.iterator();
1976+
map = new JedisByteHashMap();
1977+
1978+
while (hashIterator.hasNext()) {
1979+
map.put(BINARY.build(hashIterator.next()), BINARY.build(hashIterator.next()));
1980+
}
19651981
}
19661982

1967-
Iterator<byte[]> hashIterator = hash.iterator();
1968-
Map<byte[], byte[]> map = new JedisByteHashMap();
1969-
while (hashIterator.hasNext()) {
1970-
map.put(BINARY.build(hashIterator.next()), BINARY.build(hashIterator.next()));
1983+
if (res.size() >= 4) {
1984+
Long millisElapsedFromDelivery = LONG.build(res.get(2));
1985+
Long deliveredCount = LONG.build(res.get(3));
1986+
responses.add(new StreamEntryBinary(entryID, map, millisElapsedFromDelivery, deliveredCount));
1987+
continue;
19711988
}
1989+
19721990
responses.add(new StreamEntryBinary(entryID, map));
19731991
}
19741992

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,20 @@ public final CommandObject<Long> msetnx(String... keysvalues) {
609609
return new CommandObject<>(addFlatKeyValueArgs(commandArguments(MSETNX), keysvalues), BuilderFactory.LONG);
610610
}
611611

612+
public final CommandObject<Boolean> msetex(MSetExParams params, String... keysvalues) {
613+
CommandArguments args = commandArguments(Command.MSETEX).add(keysvalues.length / 2);
614+
addFlatKeyValueArgs(args, keysvalues);
615+
args.addParams(params);
616+
return new CommandObject<>(args, BuilderFactory.BOOLEAN);
617+
}
618+
619+
public final CommandObject<Boolean> msetex(MSetExParams params, byte[]... keysvalues) {
620+
CommandArguments args = commandArguments(Command.MSETEX).add(keysvalues.length / 2);
621+
addFlatKeyValueArgs(args, keysvalues);
622+
args.addParams(params);
623+
return new CommandObject<>(args, BuilderFactory.BOOLEAN);
624+
}
625+
612626
public final CommandObject<String> mset(byte[]... keysvalues) {
613627
return new CommandObject<>(addFlatKeyValueArgs(commandArguments(MSET), keysvalues), BuilderFactory.STRING);
614628
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,12 @@ public long msetnx(final byte[]... keysvalues) {
997997
return connection.executeCommand(commandObjects.msetnx(keysvalues));
998998
}
999999

1000+
@Override
1001+
public boolean msetex(final MSetExParams params, final byte[]... keysvalues) {
1002+
checkIsInMultiOrPipeline();
1003+
return connection.executeCommand(commandObjects.msetex(params, keysvalues));
1004+
}
1005+
10001006
/**
10011007
* DECRBY work just like {@link Jedis#decr(byte[]) DECR} but instead to decrement by 1 the
10021008
* decrement is integer.
@@ -5622,6 +5628,12 @@ public long msetnx(final String... keysvalues) {
56225628
return connection.executeCommand(commandObjects.msetnx(keysvalues));
56235629
}
56245630

5631+
@Override
5632+
public boolean msetex(final MSetExParams params, final String... keysvalues) {
5633+
checkIsInMultiOrPipeline();
5634+
return connection.executeCommand(commandObjects.msetex(params, keysvalues));
5635+
}
5636+
56255637
/**
56265638
* IDECRBY work just like {@link Jedis#decr(String) INCR} but instead to decrement by 1 the
56275639
* decrement is integer.
@@ -5830,13 +5842,13 @@ public String hget(final String key, final String field) {
58305842
}
58315843

58325844
@Override
5833-
public List<String> hgetex(String key, HGetExParams params, String... fields) {
5845+
public List<String> hgetex(String key, HGetExParams params, String... fields) {
58345846
checkIsInMultiOrPipeline();
58355847
return connection.executeCommand(commandObjects.hgetex(key, params, fields));
58365848
}
58375849

58385850
@Override
5839-
public List<String> hgetdel(String key, String... fields) {
5851+
public List<String> hgetdel(String key, String... fields) {
58405852
checkIsInMultiOrPipeline();
58415853
return connection.executeCommand(commandObjects.hgetdel(key, fields));
58425854
}

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ public Response<Long> msetnx(String... keysvalues) {
349349
return appendCommand(commandObjects.msetnx(keysvalues));
350350
}
351351

352+
@Override
353+
public Response<Boolean> msetex(MSetExParams params, String... keysvalues) {
354+
return appendCommand(commandObjects.msetex(params, keysvalues));
355+
}
356+
352357
@Override
353358
public Response<Long> incr(String key) {
354359
return appendCommand(commandObjects.incr(key));
@@ -630,13 +635,13 @@ public Response<Long> hset(String key, Map<String, String> hash) {
630635
* and optionally set their expiration. Use `HSetExParams` object to specify expiration parameters.
631636
* This command can overwrite any existing fields in the hash.
632637
* If key does not exist, a new key holding a hash is created.
633-
*
638+
*
634639
* @param key the key of the hash
635640
* @param params additional parameters for the HSETEX command
636641
* @param field the field in the hash to set
637642
* @param value the value to set in the specified field
638-
* @return 0 if no fields were set, 1 if all the fields were set
639-
*
643+
* @return 0 if no fields were set, 1 if all the fields were set
644+
*
640645
* @see HSetExParams
641646
*/
642647
@Override
@@ -649,12 +654,12 @@ public Response<Long> hsetex(String key, HSetExParams params, String field, Stri
649654
* and optionally set their expiration. Use `HSetExParams` object to specify expiration parameters.
650655
* This command can overwrite any existing fields in the hash.
651656
* If key does not exist, a new key holding a hash is created.
652-
*
657+
*
653658
* @param key the key of the hash
654659
* @param params the parameters for the HSetEx command
655660
* @param hash the map containing field-value pairs to set in the hash
656-
* @return 0 if no fields were set, 1 if all the fields were set
657-
*
661+
* @return 0 if no fields were set, 1 if all the fields were set
662+
*
658663
* @see HSetExParams
659664
*/
660665
@Override
@@ -668,14 +673,14 @@ public Response<String> hget(String key, String field) {
668673
}
669674

670675
/**
671-
* Retrieves the values associated with the specified fields in a hash stored at the given key
676+
* Retrieves the values associated with the specified fields in a hash stored at the given key
672677
* and optionally sets their expiration. Use `HGetExParams` object to specify expiration parameters.
673678
*
674679
* @param key the key of the hash
675680
* @param params additional parameters for the HGETEX command
676681
* @param fields the fields whose values are to be retrieved
677682
* @return a list of the value associated with each field or nil if the field doesn’t exist.
678-
*
683+
*
679684
* @see HGetExParams
680685
*/
681686
@Override
@@ -2062,13 +2067,13 @@ public Response<Long> hset(byte[] key, Map<byte[], byte[]> hash) {
20622067
* and optionally set their expiration. Use `HSetExParams` object to specify expiration parameters.
20632068
* This command can overwrite any existing fields in the hash.
20642069
* If key does not exist, a new key holding a hash is created.
2065-
*
2070+
*
20662071
* @param key the key of the hash
20672072
* @param params the parameters for the HSetEx command
20682073
* @param field the field in the hash to set
20692074
* @param value the value to set in the specified field
2070-
* @return 0 if no fields were set, 1 if all the fields were set
2071-
*
2075+
* @return 0 if no fields were set, 1 if all the fields were set
2076+
*
20722077
* @see HSetExParams
20732078
*/
20742079
@Override
@@ -2081,12 +2086,12 @@ public Response<Long> hsetex(byte[] key, HSetExParams params, byte[] field, byte
20812086
* and optionally set their expiration. Use `HSetExParams` object to specify expiration parameters.
20822087
* This command can overwrite any existing fields in the hash.
20832088
* If key does not exist, a new key holding a hash is created.
2084-
*
2089+
*
20852090
* @param key the key of the hash
20862091
* @param params the parameters for the HSetEx command
20872092
* @param hash the map containing field-value pairs to set in the hash
2088-
* @return 0 if no fields were set, 1 if all the fields were set
2089-
*
2093+
* @return 0 if no fields were set, 1 if all the fields were set
2094+
*
20902095
* @see HSetExParams
20912096
*/
20922097
@Override
@@ -2098,16 +2103,16 @@ public Response<Long> hsetex(byte[] key, HSetExParams params, Map<byte[], byte[]
20982103
public Response<byte[]> hget(byte[] key, byte[] field) {
20992104
return appendCommand(commandObjects.hget(key, field));
21002105
}
2101-
2106+
21022107
/**
2103-
* Retrieves the values associated with the specified fields in a hash stored at the given key
2108+
* Retrieves the values associated with the specified fields in a hash stored at the given key
21042109
* and optionally sets their expiration. Use `HGetExParams` object to specify expiration parameters.
21052110
*
21062111
* @param key the key of the hash
21072112
* @param params additional parameters for the HGETEX command
21082113
* @param fields the fields whose values are to be retrieved
21092114
* @return a list of the value associated with each field or nil if the field doesn’t exist.
2110-
*
2115+
*
21112116
* @see HGetExParams
21122117
*/
21132118
@Override
@@ -3538,6 +3543,11 @@ public Response<String> mset(byte[]... keysvalues) {
35383543
return appendCommand(commandObjects.mset(keysvalues));
35393544
}
35403545

3546+
@Override
3547+
public Response<Boolean> msetex(MSetExParams params, byte[]... keysvalues) {
3548+
return appendCommand(commandObjects.msetex(params, keysvalues));
3549+
}
3550+
35413551
@Override
35423552
public Response<Long> msetnx(byte[]... keysvalues) {
35433553
return appendCommand(commandObjects.msetnx(keysvalues));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public static enum Command implements ProtocolCommand {
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, //
290-
GETSET, MGET, SETNX, SETEX, PSETEX, MSET, MSETNX, DECR, DECRBY, INCR, INCRBY, INCRBYFLOAT,
290+
GETSET, MGET, SETNX, SETEX, PSETEX, MSETEX, MSET, MSETNX, DECR, DECRBY, INCR, INCRBY, INCRBYFLOAT,
291291
STRLEN, APPEND, SUBSTR, // <-- string
292292
SETBIT, GETBIT, BITPOS, SETRANGE, GETRANGE, BITCOUNT, BITOP, BITFIELD, BITFIELD_RO, // <-- bit (string)
293293
HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, HSTRLEN,
@@ -331,7 +331,7 @@ public static enum Keyword implements Rawable {
331331
AGGREGATE, ALPHA, BY, GET, LIMIT, NO, NOSORT, ONE, SET, STORE, WEIGHTS, WITHSCORE, WITHSCORES,
332332
RESETSTAT, REWRITE, RESET, FLUSH, EXISTS, LOAD, LEN, HELP, SCHEDULE, MATCH, COUNT, TYPE, KEYS,
333333
REFCOUNT, ENCODING, IDLETIME, FREQ, REPLACE, GETNAME, SETNAME, SETINFO, LIST, ID, KILL, PERSIST,
334-
STREAMS, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, IDLE, TIME, BLOCK, NOACK,
334+
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,
337337
NX, XX, IFEQ, IFNE, IFDEQ, IFDNE, EX, PX, EXAT, PXAT, ABSTTL, KEEPTTL, INCR, LT, GT, CH, INFO, PAUSE, UNPAUSE, UNBLOCK,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ static void initialize(StaticCommandFlagsRegistry.Builder builder) {
299299
builder.register("LSET", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
300300
builder.register("MSET", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
301301
builder.register("MSETNX", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
302+
builder.register("MSETEX", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
302303
builder.register("PFMERGE", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
303304
builder.register("PSETEX", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
304305
builder.register("RESTORE", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,11 @@ public long msetnx(String... keysvalues) {
988988
return executeCommand(commandObjects.msetnx(keysvalues));
989989
}
990990

991+
@Override
992+
public boolean msetex(MSetExParams params, String... keysvalues) {
993+
return executeCommand(commandObjects.msetex(params, keysvalues));
994+
}
995+
991996
@Override
992997
public List<byte[]> mget(byte[]... keys) {
993998
return executeCommand(commandObjects.mget(keys));
@@ -998,6 +1003,11 @@ public String mset(byte[]... keysvalues) {
9981003
return executeCommand(commandObjects.mset(keysvalues));
9991004
}
10001005

1006+
@Override
1007+
public boolean msetex(MSetExParams params, byte[]... keysvalues) {
1008+
return executeCommand(commandObjects.msetex(params, keysvalues));
1009+
}
1010+
10011011
@Override
10021012
public long msetnx(byte[]... keysvalues) {
10031013
return executeCommand(commandObjects.msetnx(keysvalues));
@@ -1466,12 +1476,12 @@ public long hsetex(String key, HSetExParams params, String field, String value)
14661476
public long hsetex(String key, HSetExParams params, Map<String, String> hash) {
14671477
return executeCommand(commandObjects.hsetex(key, params, hash));
14681478
}
1469-
1479+
14701480
@Override
14711481
public String hget(String key, String field) {
14721482
return executeCommand(commandObjects.hget(key, field));
14731483
}
1474-
1484+
14751485
@Override
14761486
public List<String> hgetex(String key, HGetExParams params, String... fields) {
14771487
return executeCommand(commandObjects.hgetex(key, params, fields));
@@ -4017,7 +4027,7 @@ public SearchResult ftSearch(String indexName, String query, FTSearchParams para
40174027

40184028
/**
40194029
* {@link FTSearchParams#limit(int, int)} will be ignored.
4020-
*
4030+
*
40214031
* @param batchSize batch size
40224032
* @param indexName index name
40234033
* @param query query

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import redis.clients.jedis.params.GetExParams;
66
import redis.clients.jedis.params.SetParams;
7+
import redis.clients.jedis.params.MSetExParams;
8+
79
import redis.clients.jedis.params.LCSParams;
810
import redis.clients.jedis.resps.LCSMatchResult;
911

@@ -45,6 +47,28 @@ public interface StringBinaryCommands extends BitBinaryCommands {
4547

4648
long msetnx(byte[]... keysvalues);
4749

50+
/**
51+
* Multi-set with optional condition and expiration.
52+
* <p>
53+
* Sets the respective keys to the respective values, similar to {@link #mset(byte[]...) MSET},
54+
* but allows conditional set (NX|XX) and expiration options via {@link MSetExParams}.
55+
* If the condition is not met for any key, no key is set.
56+
* <p>
57+
* Both MSET and MSETEX are atomic operations. This means that if multiple keys are provided,
58+
* another client will either see the changes for all keys at once, or no changes at all.
59+
* <p>
60+
* Options (in {@link MSetExParams}): NX or XX, and expiration: EX seconds | PX milliseconds |
61+
* EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL.
62+
* <p>
63+
* Time complexity: O(N) where N is the number of keys to set.
64+
* @param params condition and expiration parameters
65+
* @param keysvalues pairs of keys and their values, e.g. {@code msetex(params, "foo".getBytes(), "foovalue".getBytes(), "bar".getBytes(), "barvalue".getBytes())}
66+
* @return {@code true} if all the keys were set, {@code false} if none were set (condition not satisfied)
67+
* @see #mset(byte[]...)
68+
* @see #msetnx(byte[]...)
69+
*/
70+
boolean msetex(MSetExParams params, byte[]... keysvalues);
71+
4872
long incr(byte[] key);
4973

5074
long incrBy(byte[] key, long increment);

0 commit comments

Comments
 (0)