8000 Add MSETEX command support by a-TODO-rov · Pull Request #4361 · redis/jedis · GitHub
[go: up one dir, main page]

Skip to content
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@
<include>src/test/java/redis/clients/jedis/commands/jedis/ClusterStreamsCommandsTest.java</include>
<include>src/test/java/redis/clients/jedis/commands/jedis/PooledStreamsCommandsTest.java</include>
<include>src/test/java/redis/clients/jedis/resps/StreamEntryDeletionResultTest.java</include>
<include>src/test/java/redis/clients/jedis/commands/commandobjects/CommandObjectsStringCommandsTest.java</include>
<include>src/test/java/redis/clients/jedis/commands/jedis/BinaryValuesCommandsTest.java</include>
<include>src/test/java/redis/clients/jedis/commands/jedis/StringValuesCommandsTest.java</include>
<include>src/test/java/redis/clients/jedis/commands/unified/BinaryValuesCommandsTestBase.java</include>
<include>src/test/java/redis/clients/jedis/commands/unified/StringValuesCommandsTestBase.java</include>
<include>src/test/java/redis/clients/jedis/commands/unified/cluster/ClusterStringValuesCommandsTest.java</include>
<include>**/VectorSet*.java</include>
<include>**/VectorTestUtils.java</include>
<include>**/VAddParams.java</include>
Expand All @@ -538,6 +544,7 @@
<include>**/ClientTestUtil.java</include>
<include>**/ReflectionTestUtil.java</include>
<include>**/*CommandFlags*.java</include>
<include>**/*MSetExParams*.java</include>
</includes>
</configuration>
<executions>
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected RedisProtocol getProtocol() {

protected volatile CommandKeyArgumentPreProcessor keyPreProcessor = null;
private JedisBroadcastAndRoundRobinConfig broadcastAndRoundRobinConfig = null;
private Lock mapperLock = new ReentrantLock(true);
private Lock mapperLock = new ReentrantLock(true);
private volatile JsonObjectMapper jsonObjectMapper;
private final AtomicInteger searchDialect = new AtomicInteger(SearchProtocol.DEFAULT_DIALECT);

Expand Down Expand Up @@ -588,6 +588,20 @@ public final CommandObject<Long> msetnx(String... keysvalues) {
return new CommandObject<>(addFlatKeyValueArgs(commandArguments(MSETNX), keysvalues), BuilderFactory.LONG);
}

public final CommandObject<Boolean> msetex(MSetExParams params, String... keysvalues) {
CommandArguments args = commandArguments(Command.MSETEX).add(keysvalues.length / 2);
addFlatKeyValueArgs(args, keysvalues);
args.addParams(params);
return new CommandObject<>(args, BuilderFactory.BOOLEAN);
}

public final CommandObject<Boolean> msetex(MSetExParams params, byte[]... keysvalues) {
CommandArguments args = commandArguments(Command.MSETEX).add(keysvalues.length / 2);
addFlatKeyValueArgs(args, keysvalues);
args.addParams(params);
return new CommandObject<>(args, BuilderFactory.BOOLEAN);
}

public final CommandObject<String> mset(byte[]... keysvalues) {
return new CommandObject<>(addFlatKeyValueArgs(commandArguments(MSET), keysvalues), BuilderFactory.STRING);
}
Expand Down Expand Up @@ -2977,7 +2991,7 @@ public final CommandObject<List<Object>> xreadGroup(byte[] groupName, byte[] con
}

public final CommandObject<List<Map.Entry<byte[], List<StreamEntryBinary>>>> xreadGroupBinary(
byte[] groupName, byte[] consumer, XReadGroupParams xReadGroupParams,
byte[] groupName, byte[] consumer, XReadGroupParams xReadGroupParams,
Map.Entry<byte[], StreamEntryID>... streams) {
CommandArguments args = commandArguments(XREADGROUP)
.add(GROUP).add(groupName).add(consumer)
Expand All @@ -2992,7 +3006,7 @@ public final CommandObject<List<Map.Entry<byte[], List<StreamEntryBinary>>>> xre
}

public final CommandObject<Map<byte[], List<StreamEntryBinary>>> xreadGroupBinaryAsMap(
byte[] groupName, byte[] consumer, XReadGroupParams xReadGroupParams,
byte[] groupName, byte[] consumer, XReadGroupParams xReadGroupParams,
Map.Entry<byte[], StreamEntryID>... streams) {
CommandArguments args = commandArguments(XREADGROUP)
.add(GROUP).add(groupName).add(consumer)
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,12 @@ public long msetnx(final byte[]... keysvalues) {
return connection.executeCommand(commandObjects.msetnx(keysvalues));
}

@Override
public boolean msetex(final MSetExParams params, final byte[]... keysvalues) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.msetex(params, keysvalues));
}

/**
* DECRBY work just like {@link Jedis#decr(byte[]) DECR} but instead to decrement by 1 the
* decrement is integer.
Expand Down Expand Up @@ -5597,6 +5603,12 @@ public long msetnx(final String... keysvalues) {
return connection.executeCommand(commandObjects.msetnx(keysvalues));
}

@Override
public boolean msetex(final MSetExParams params, final String... keysvalues) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.msetex(params, keysvalues));
}

/**
* IDECRBY work just like {@link Jedis#decr(String) INCR} but instead to decrement by 1 the
* decrement is integer.
Expand Down Expand Up @@ -5805,13 +5817,13 @@ public String hget(final String key, final String field) {
}

@Override
public List<String> hgetex(String key, HGetExParams params, String... fields) {
public List<String> hgetex(String key, HGetExParams params, String... fields) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hgetex(key, params, fields));
}

@Override
public List<String> hgetdel(String key, String... fields) {
public List<String> hgetdel(String key, String... fields) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hgetdel(key, fields));
}
Expand Down
44 changes: 27 additions & 17 deletions src/main/java/redis/clients/jedis/PipeliningBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ public Response<Long> msetnx(String... keysvalues) {
return appendCommand(commandObjects.msetnx(keysvalues));
}

@Override
public Response<Boolean> msetex(MSetExParams params, String... keysvalues) {
return appendCommand(commandObjects.msetex(params, keysvalues));
}

@Override
public Response<Long> incr(String key) {
return appendCommand(commandObjects.incr(key));
Expand Down Expand Up @@ -630,13 +635,13 @@ public Response<Long> hset(String key, Map<String, String> hash) {
* and optionally set their expiration. Use `HSetExParams` object to specify expiration parameters.
* This command can overwrite any existing fields in the hash.
* If key does not exist, a new key holding a hash is created.
*
*
* @param key the key of the hash
* @param params additional parameters for the HSETEX command
* @param field the field in the hash to set
* @param value the value to set in the specified field
* @return 0 if no fields were set, 1 if all the fields were set
*
* @return 0 if no fields were set, 1 if all the fields were set
*
* @see HSetExParams
*/
@Override
Expand All @@ -649,12 +654,12 @@ public Response<Long> hsetex(String key, HSetExParams params, String field, Stri
* and optionally set their expiration. Use `HSetExParams` object to specify expiration parameters.
* This command can overwrite any existing fields in the hash.
* If key does not exist, a new key holding a hash is created.
*
*
* @param key the key of the hash
* @param params the parameters for the HSetEx command
* @param hash the map containing field-value pairs to set in the hash
* @return 0 if no fields were set, 1 if all the fields were set
*
* @return 0 if no fields were set, 1 if all the fields were set
*
* @see HSetExParams
*/
@Override
Expand All @@ -668,14 +673,14 @@ public Response<String> hget(String key, String field) {
}

/**
* Retrieves the values associated with the specified fields in a hash stored at the given key
* Retrieves the values associated with the specified fields in a hash stored at the given key
* and optionally sets their expiration. Use `HGetExParams` object to specify expiration parameters.
*
* @param key the key of the hash
* @param params additional parameters for the HGETEX command
* @param fields the fields whose values are to be retrieved
* @return a list of the value associated with each field or nil if the field doesn’t exist.
*
*
* @see HGetExParams
*/
@Override
Expand Down Expand Up @@ -2062,13 +2067,13 @@ public Response<Long> hset(byte[] key, Map<byte[], byte[]> hash) {
* and optionally set their expiration. Use `HSetExParams` object to specify expiration parameters.
* This command can overwrite any existing fields in the hash.
* If key does not exist, a new key holding a hash is created.
*
*
* @param key the key of the hash
* @param params the parameters for the HSetEx command
* @param field the field in the hash to set
* @param value the value to set in the specified field
* @return 0 if no fields were set, 1 if all the fields were set
*
* @return 0 if no fields were set, 1 if all the fields were set
*
* @see HSetExParams
*/
@Override
Expand All @@ -2081,12 +2086,12 @@ public Response<Long> hsetex(byte[] key, HSetExParams params, byte[] field, byte
* and optionally set their expiration. Use `HSetExParams` object to specify expiration parameters.
* This command can overwrite any existing fields in the hash.
* If key does not exist, a new key holding a hash is created.
*
*
* @param key the key of the hash
* @param params the parameters for the HSetEx command
* @param hash the map containing field-value pairs to set in the hash
* @return 0 if no fields were set, 1 if all the fields were set
*
* @return 0 if no fields were set, 1 if all the fields were set
*
* @see HSetExParams
*/
@Override
Expand All @@ -2098,16 +2103,16 @@ public Response<Long> hsetex(byte[] key, HSetExParams params, Map<byte[], byte[]
public Response<byte[]> hget(byte[] key, byte[] field) {
return appendCommand(commandObjects.hget(key, field));
}

/**
* Retrieves the values associated with the specified fields in a hash stored at the given key
* Retrieves the values associated with the specified fields in a hash stored at the given key
* and optionally sets their expiration. Use `HGetExParams` object to specify expiration parameters.
*
* @param key the key of the hash
* @param params additional parameters for the HGETEX command
* @param fields the fields whose values are to be retrieved
* @return a list of the value associated with each field or nil if the field doesn’t exist.
*
*
* @see HGetExParams
*/
@Override
Expand Down Expand Up @@ -3538,6 +3543,11 @@ public Response<String> mset(byte[]... keysvalues) {
return appendCommand(commandObjects.mset(keysvalues));
}

@Override
public Response<Boolean> msetex(MSetExParams params, byte[]... keysvalues) {
return appendCommand(commandObjects.msetex(params, keysvalues));
}

@Override
public Response<Long> msetnx(byte[]... keysvalues) {
return appendCommand(commandObjects.msetnx(keysvalues));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public static enum Command implements ProtocolCommand {
KEYS, RANDOMKEY, RENAME, RENAMENX, DUMP, RESTORE, DBSIZE, SELECT, SWAPDB, MIGRATE, ECHO, //
EXPIRE, EXPIREAT, EXPIRETIME, PEXPIRE, PEXPIREAT, PEXPIRETIME, TTL, PTTL, // <-- key expiration
MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, SORT_RO, INFO, SHUTDOWN, MONITOR, CONFIG, LCS, //
GETSET, MGET, SETNX, SETEX, PSETEX, MSET, MSETNX, DECR, DECRBY, INCR, INCRBY, INCRBYFLOAT,
GETSET, MGET, SETNX, SETEX, PSETEX, MSETEX, MSET, MSETNX, DECR, DECRBY, INCR, INCRBY, INCRBYFLOAT,
STRLEN, APPEND, SUBSTR, // <-- string
SETBIT, GETBIT, BITPOS, SETRANGE, GETRANGE, BITCOUNT, BITOP, BITFIELD, BITFIELD_RO, // <-- bit (string)
HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, HSTRLEN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ static void initialize(StaticCommandFlagsRegistry.Builder builder) {
builder.register("LSET", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
builder.register("MSET", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
builder.register("MSETNX", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
builder.register("MSETEX", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
builder.register("PFMERGE", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
builder.register("PSETEX", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
builder.register("RESTORE", EnumSet.of(CommandFlag.DENYOOM, CommandFlag.WRITE));
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,11 @@ public long msetnx(String... keysvalues) {
return executeCommand(commandObjects.msetnx(keysvalues));
}

@Override
public boolean msetex(MSetExParams params, String... keysvalues) {
return executeCommand(commandObjects.msetex(params, keysvalues));
}

@Override
public List<byte[]> mget(byte[]... keys) {
return executeCommand(commandObjects.mget(keys));
Expand All @@ -978,6 +983,11 @@ public String mset(byte[]... keysvalues) {
return executeCommand(commandObjects.mset(keysvalues));
}

@Override
public boolean msetex(MSetExParams params, byte[]... keysvalues) {
return executeCommand(commandObjects.msetex(params, keysvalues));
}
AB16
@Override
public long msetnx(byte[]... keysvalues) {
return executeCommand(commandObjects.msetnx(keysvalues));
Expand Down Expand Up @@ -1446,12 +1456,12 @@ public long hsetex(String key, HSetExParams params, String field, String value)
public long hsetex(String key, HSetExParams params, Map<String, String> hash) {
return executeCommand(commandObjects.hsetex(key, params, hash));
}

@Override
public String hget(String key, String field) {
return executeCommand(commandObjects.hget(key, field));
}

@Override
public List<String> hgetex(String key, HGetExParams params, String... fields) {
return executeCommand(commandObjects.hgetex(key, params, fields));
Expand Down Expand Up @@ -3997,7 +4007,7 @@ public SearchResult ftSearch(String indexName, String query, FTSearchParams para

/**
* {@link FTSearchParams#limit(int, int)} will be ignored.
*
*
* @param batchSize batch size
* @param indexName index name
* @param query query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import redis.clients.jedis.params.GetExParams;
import redis.clients.jedis.params.SetParams;
60AA import redis.clients.jedis.params.MSetExParams;

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

Expand Down Expand Up @@ -45,6 +47,28 @@ public interface StringBinaryCommands extends BitBinaryCommands {

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

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

long incr(byte[] key);

long incrBy(byte[] key, long increment);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/redis/clients/jedis/commands/StringCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import redis.clients.jedis.params.GetExParams;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.params.MSetExParams;

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

Expand Down Expand Up @@ -210,6 +212,28 @@ public interface StringCommands extends BitCommands {
*/
long msetnx(String... keysvalues);

/**
* Multi-set with optional condition and expiration.
* <p>
* Sets the respective keys to the respective values, similar to {@link #mset(String...) MSET},
* but allows conditional set (NX|XX) and expiration options via {@link MSetExParams}.
* If the condition is not met for any key, no key is set.
* <p>
* Both MSET and MSETEX are atomic operations. This means that if multiple keys are provided,
* another client will either see the changes for all keys at once, or no changes at all.
* <p>
* Options (in {@link MSetExParams}): NX or XX, and expiration: EX seconds | PX milliseconds |
* EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL.
* <p>
* Time complexity: O(N) where N is the number of keys to set.
* @param params condition and expiration parameters
* @param keysvalues pairs of keys and their values, e.g. {@code msetex(params, "foo", "foovalue", "bar", "barvalue")}
* @return {@code true} if all the keys were set, {@code false} if none were set (condition not satisfied)
* @see #mset(String...)
* @see #msetnx(String...)
*/
boolean msetex(MSetExParams params, String... keysvalues);

/**
* <b><a href="http://redis.io/commands/incr">Incr Command</a></b>
* Increment the number stored at key by one. If the key does not exist or contains a value of a
Expand Down
Loading
Loading
0