Skip to content

Support for new HFE API, hgetdel hgetex hsetex commands #4095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 24, 2025
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
3 changes: 3 additions & 0 deletions .github/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ hincrByFloat
hiredis
hlen
hset
hgetdel
hgetex
hsetex
hsetnx
hstrlen
http
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.json.*;
import redis.clients.jedis.json.JsonProtocol.JsonCommand;
import redis.clients.jedis.json.DefaultGsonObjectMapper;
import redis.clients.jedis.json.JsonObjectMapper;
import redis.clients.jedis.params.*;
import redis.clients.jedis.resps.*;
import redis.clients.jedis.search.*;
Expand Down Expand Up @@ -1020,10 +1018,30 @@ public final CommandObject<Long> hset(String key, Map<String, String> hash) {
return new CommandObject<>(addFlatMapArgs(commandArguments(HSET).key(key), hash), BuilderFactory.LONG);
}

public final CommandObject<Long> hsetex(String key, HSetExParams params, String field, String value) {
return new CommandObject<>(commandArguments(HSETEX).key(key)
.addParams(params).add(FIELDS).add(1).add(field).add(value), BuilderFactory.LONG);
}

public final CommandObject<Long> hsetex(String key, HSetExParams params, Map<String, String> hash) {
return new CommandObject<>(addFlatMapArgs(commandArguments(HSETEX).key(key)
.addParams(params).add(FIELDS).add(hash.size()), hash), BuilderFactory.LONG);
}

public final CommandObject<String> hget(String key, String field) {
return new CommandObject<>(commandArguments(HGET).key(key).add(field), BuilderFactory.STRING);
}

public final CommandObject<List<String>> hgetex(String key, HGetExParams params, String... fields) {
return new CommandObject<>(commandArguments(Command.HGETEX).key(key)
.addParams(params).add(FIELDS).add(fields.length).addObjects((Object[]) fields), BuilderFactory.STRING_LIST);
}

public final CommandObject<List<String>> hgetdel(String key, String... fields) {
return new CommandObject<>(commandArguments(HGETDEL).key(key)
.add(FIELDS).add(fields.length).addObjects((Object[]) fields), BuilderFactory.STRING_LIST);
}

public final CommandObject<Long> hsetnx(String key, String field, String value) {
return new CommandObject<>(commandArguments(HSETNX).key(key).add(field).add(value), BuilderFactory.LONG);
}
Expand All @@ -1044,10 +1062,30 @@ public final CommandObject<Long> hset(byte[] key, Map<byte[], byte[]> hash) {
return new CommandObject<>(addFlatMapArgs(commandArguments(HSET).key(key), hash), BuilderFactory.LONG);
}

public final CommandObject<Long> hsetex(byte[] key, HSetExParams params, byte[] field, byte[] value) {
return new CommandObject<>(commandArguments(HSETEX).key(key)
.addParams(params).add(FIELDS).add(1).add(field).add(value), BuilderFactory.LONG);
}

public final CommandObject<Long> hsetex(byte[] key, HSetExParams params, Map<byte[], byte[]> hash) {
return new CommandObject<>(addFlatMapArgs(commandArguments(HSETEX).key(key)
.addParams(params).add(FIELDS).add(hash.size()), hash), BuilderFactory.LONG);
}

public final CommandObject<byte[]> hget(byte[] key, byte[] field) {
return new CommandObject<>(commandArguments(HGET).key(key).add(field), BuilderFactory.BINARY);
}

public final CommandObject<List<byte[]>> hgetex(byte[] key, HGetExParams params, byte[]... fields) {
return new CommandObject<>(commandArguments(Command.HGETEX).key(key)
.addParams(params).add(FIELDS).add(fields.length).addObjects((Object[]) fields), BuilderFactory.BINARY_LIST);
}

public final CommandObject<List<byte[]>> hgetdel(byte[] key, byte[]... fields) {
return new CommandObject<>(commandArguments(HGETDEL).key(key).add(FIELDS)
.add(fields.length).addObjects((Object[]) fields), BuilderFactory.BINARY_LIST);
}

public final CommandObject<Long> hsetnx(byte[] key, byte[] field, byte[] value) {
return new CommandObject<>(commandArguments(HSETNX).key(key).add(field).add(value), BuilderFactory.LONG);
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,18 @@ public long hset(final byte[] key, final Map<byte[], byte[]> hash) {
return connection.executeCommand(commandObjects.hset(key, hash));
}

@Override
public long hsetex(byte[] key, HSetExParams params, byte[] field, byte[] value) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hsetex(key, params, field, value));
}

@Override
public long hsetex(byte[] key, HSetExParams params, Map<byte[], byte[]> hash){
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hsetex(key, params, hash));
}

/**
* If key holds a hash, retrieve the value associated to the specified field.
* <p>
Expand All @@ -1183,6 +1195,18 @@ public byte[] hget(final byte[] key, final byte[] field) {
return connection.executeCommand(commandObjects.hget(key, field));
}

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

@Override
public List<byte[]> hgetdel(byte[] key, byte[]... fields){
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hgetdel(key, fields));
}

/**
* Set the specified hash field to the specified value if the field not exists. <b>Time
* complexity:</b> O(1)
Expand Down Expand Up @@ -5687,6 +5711,18 @@ public long hset(final String key, final Map<String, String> hash) {
return connection.executeCommand(commandObjects.hset(key, hash));
}

@Override
public long hsetex(String key, HSetExParams params, String field, String value) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hsetex(key, params, field, value));
}

@Override
public long hsetex(String key, HSetExParams params, Map<String, String> hash) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hsetex(key, params, hash));
}

/**
* If key holds a hash, retrieve the value associated to the specified field.
* <p>
Expand All @@ -5703,6 +5739,18 @@ public String hget(final String key, final String field) {
return connection.executeCommand(commandObjects.hget(key, field));
}

@Override
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) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.hgetdel(key, fields));
}

/**
* Set the specified hash field to the specified value if the field not exists. <b>Time
* complexity:</b> O(1)
Expand Down
132 changes: 132 additions & 0 deletions src/main/java/redis/clients/jedis/PipeliningBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,77 @@ public Response<Long> hset(String key, Map<String, String> hash) {
return appendCommand(commandObjects.hset(key, hash));
}

/**
* Sets the specified field in the hash stored at key to the specified value with additional parameters,
* 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
*
* @see HSetExParams
*/
@Override
public Response<Long> hsetex(String key, HSetExParams params, String field, String value) {
return appendCommand(commandObjects.hsetex(key, params, field, value));
}

/**
* Sets the specified fields in the hash stored at key to the specified values with additional parameters,
* 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
*
* @see HSetExParams
*/
@Override
public Response<Long> hsetex(String key, HSetExParams params, Map<String, String> hash) {
return appendCommand(commandObjects.hsetex(key, params, hash));
}

@Override
public Response<String> hget(String key, String field) {
return appendCommand(commandObjects.hget(key, field));
}

/**
* 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
public Response<List<String>> hgetex(String key, HGetExParams params, String... fields) {
return appendCommand(commandObjects.hgetex(key, params, fields));
}

/**
* Retrieves the values associated with the specified fields in the hash stored at the given key
* and then deletes those fields from the hash.
*
* @param key the key of the hash
* @param fields the fields whose values are to be retrieved and then deleted
* @return a list of values associated with the specified fields before they were deleted
*/
@Override
public Response<List<String>> hgetdel(String key, String... fields) {
return appendCommand(commandObjects.hgetdel(key, fields));
}

@Override
public Response<Long> hsetnx(String key, String field, String value) {
return appendCommand(commandObjects.hsetnx(key, field, value));
Expand Down Expand Up @@ -1971,10 +2037,76 @@ public Response<Long> hset(byte[] key, Map<byte[], byte[]> hash) {
return appendCommand(commandObjects.hset(key, hash));
}

/**
* Sets the specified field in the hash stored at key to the specified value with additional parameters,
* 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
*
* @see HSetExParams
*/
@Override
public Response<Long> hsetex(byte[] key, HSetExParams params, byte[] field, byte[] value) {
return appendCommand(commandObjects.hsetex(key, params, field, value));
}

/**
* Sets the specified fields in the hash stored at key to the specified values with additional parameters,
* 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
*
* @see HSetExParams
*/
@Override
public Response<Long> hsetex(byte[] key, HSetExParams params, Map<byte[], byte[]> hash) {
return appendCommand(commandObjects.hsetex(key, params, hash));
}

@Override
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
* 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
public Response<List<byte[]>> hgetex(byte[] key, HGetExParams params, byte[]... fields) {
return appendCommand(commandObjects.hgetex(key, params, fields));
}

/**
* Retrieves the values associated with the specified fields in the hash stored at the given key
* and then deletes those fields from the hash.
*
* @param key the key of the hash
* @param fields the fields whose values are to be retrieved and then deleted
* @return a list of values associated with the specified fields before they were deleted
*/
@Override
public Response<List<byte[]>> hgetdel(byte[] key, byte[]... fields) {
return appendCommand(commandObjects.hgetdel(key, fields));
}

@Override
public Response<Long> hsetnx(byte[] key, byte[] field, byte[] value) {
Expand Down
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 @@ -292,7 +292,7 @@ public static enum Command implements ProtocolCommand {
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,
HEXPIRE, HPEXPIRE, HEXPIREAT, HPEXPIREAT, HTTL, HPTTL, HEXPIRETIME, HPEXPIRETIME, HPERSIST,
HRANDFIELD, HINCRBYFLOAT, // <-- hash
HRANDFIELD, HINCRBYFLOAT, HSETEX, HGETEX, HGETDEL, // <-- hash
RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, BLPOP, BRPOP, LINSERT, LPOS,
RPOPLPUSH, BRPOPLPUSH, BLMOVE, LMOVE, LMPOP, BLMPOP, LPUSHX, RPUSHX, // <-- list
SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SRANDMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE,
Expand Down Expand Up @@ -339,7 +339,7 @@ public static enum Keyword implements Rawable {
DELETE, LIBRARYNAME, WITHCODE, DESCRIPTION, GETKEYS, GETKEYSANDFLAGS, DOCS, FILTERBY, DUMP,
MODULE, ACLCAT, PATTERN, DOCTOR, LATEST, HISTORY, USAGE, SAMPLES, PURGE, STATS, LOADEX, CONFIG,
ARGS, RANK, NOW, VERSION, ADDR, SKIPME, USER, LADDR, FIELDS,
CHANNELS, NUMPAT, NUMSUB, SHARDCHANNELS, SHARDNUMSUB, NOVALUES, MAXAGE;
CHANNELS, NUMPAT, NUMSUB, SHARDCHANNELS, SHARDNUMSUB, NOVALUES, MAXAGE, FXX, FNX;

private final byte[] raw;

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1479,10 +1479,30 @@ public long hset(String key, Map<String, String> hash) {
return executeCommand(commandObjects.hset(key, hash));
}

@Override
public long hsetex(String key, HSetExParams params, String field, String value) {
return executeCommand(commandObjects.hsetex(key, params, field, value));
}

@Override
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));
}

@Override
public List<String> hgetdel(String key, String... fields) {
return executeCommand(commandObjects.hgetdel(key, fields));
}

@Override
public long hsetnx(String key, String field, String value) {
Expand All @@ -1509,11 +1529,31 @@ public long hset(byte[] key, Map<byte[], byte[]> hash) {
return executeCommand(commandObjects.hset(key, hash));
}

@Override
public long hsetex(byte[] key, HSetExParams params, byte[] field, byte[] value) {
return executeCommand(commandObjects.hsetex(key, params, field, value));
}

@Override
public long hsetex(byte[] key, HSetExParams params, Map<byte[], byte[]> hash) {
return executeCommand(commandObjects.hsetex(key, params, hash));
}

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

@Override
public List<byte[]> hgetex(byte[] key, HGetExParams params, byte[]... fields) {
return executeCommand(commandObjects.hgetex(key, params, fields));
}

@Override
public List<byte[]> hgetdel(byte[] key, byte[]... fields) {
return executeCommand(commandObjects.hgetdel(key, fields));
}

@Override
public long hsetnx(byte[] key, byte[] field, byte[] value) {
return executeCommand(commandObjects.hsetnx(key, field, value));
Expand Down
Loading
Loading