Skip to content

Commit

Permalink
Add support for variadic hset invocation #1217
Browse files Browse the repository at this point in the history
Original pull request: #1220.
  • Loading branch information
Hodur Heidarsson authored and mp911de committed Feb 10, 2020
1 parent f73b810 commit bd2f18c
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,11 @@ public RedisFuture<Boolean> hset(K key, K field, V value) {
return dispatch(commandBuilder.hset(key, field, value));
}

@Override
public RedisFuture<Long> hset(K key, Map<K, V> map) {
return dispatch(commandBuilder.hset(key, map));
}

@Override
public RedisFuture<Boolean> hsetnx(K key, K field, V value) {
return dispatch(commandBuilder.hsetnx(key, field, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,11 @@ public Mono<Boolean> hset(K key, K field, V value) {
return createMono(() -> commandBuilder.hset(key, field, value));
}

@Override
public Mono<Long> hset(K key, Map<K, V> map) {
return createMono(() -> commandBuilder.hset(key, map));
}

@Override
public Mono<Boolean> hsetnx(K key, K field, V value) {
return createMono(() -> commandBuilder.hsetnx(key, field, value));
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,15 @@ Command<K, V, Boolean> hset(K key, K field, V value) {
return createCommand(HSET, new BooleanOutput<>(codec), args);
}

Command<K, V, Long> hset(K key, Map<K, V> map) {
notNullKey(key);
LettuceAssert.notNull(map, "Map " + MUST_NOT_BE_NULL);
LettuceAssert.isTrue(!map.isEmpty(), "Map " + MUST_NOT_BE_EMPTY);

CommandArgs<K, V> args = new CommandArgs<>(codec).addKey(key).add(map);
return createCommand(HSET, new IntegerOutput<>(codec), args);
}

Command<K, V, Boolean> hsetnx(K key, K field, V value) {
notNullKey(key);
LettuceAssert.notNull(field, "Field " + MUST_NOT_BE_NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ public interface RedisHashAsyncCommands<K, V> {
*/
RedisFuture<Boolean> hset(K key, K field, V value);

/**
* Set multiple hash fields to multiple values.
*
* @param key the key of the hash
* @param map the field/value pairs to update
* @return Long integer-reply: the number of fields that were added.
*/
RedisFuture<Long> hset(K key, Map<K, V> map);

/**
* Set the value of a hash field, only if the field does not exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ public interface RedisHashReactiveCommands<K, V> {
*/
Mono<Boolean> hset(K key, K field, V value);

/**
* Set multiple hash fields to multiple values.
*
* @param key the key of the hash
* @param map the field/value pairs to update
* @return Long integer-reply: the number of fields that were added.
*/
Mono<Long> hset(K key, Map<K, V> map);

/**
* Set the value of a hash field, only if the field does not exist.
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/lettuce/core/api/sync/RedisHashCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ public interface RedisHashCommands<K, V> {
*/
Boolean hset(K key, K field, V value);

/**
* Set multiple hash fields to multiple values.
*
* @param key the key of the hash
* @param map the field/value pairs to update
* @return Long integer-reply: the number of fields that were added.
*/
Long hset(K key, Map<K, V> map);

/**
* Set the value of a hash field, only if the field does not exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ public interface NodeSelectionHashAsyncCommands<K, V> {
*/
AsyncExecutions<Boolean> hset(K key, K field, V value);

/**
* Set multiple hash fields to multiple values.
*
* @param key the key of the hash
* @param map the field/value pairs to update
* @return Long integer-reply: the number of fields that were added.
*/
AsyncExecutions<Long> hset(K key, Map<K, V> map);

/**
* Set the value of a hash field, only if the field does not exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ public interface NodeSelectionHashCommands<K, V> {
*/
Executions<Boolean> hset(K key, K field, V value);

/**
* Set multiple hash fields to multiple values.
*
* @param key the key of the hash
* @param map the field/value pairs to update
* @return Long integer-reply: the number of fields that were added.
*/
Executions<Long> hset(K key, Map<K, V> map);

/**
* Set the value of a hash field, only if the field does not exist.
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/templates/io/lettuce/core/api/RedisHashCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ public interface RedisHashCommands<K, V> {
*/
Boolean hset(K key, K field, V value);

/**
* Set multiple hash fields to multiple values.
*
* @param key the key of the hash
* @param map the field/value pairs to update
* @return Long integer-reply: the number of fields that were added.
*/
Long hset(K key, Map<K, V> map);

/**
* Set the value of a hash field, only if the field does not exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ void hset() {
assertThat(redis.hset(key, "one", "1")).isFalse();
}

@Test
void hsetMap() {
Map<String, String> hash = new LinkedHashMap<>();
hash.put("two", "2");
hash.put("three", "3");
assertThat(redis.hset(key, hash)).isEqualTo(2);

hash.put("two", "second");
assertThat(redis.hset(key, hash)).isEqualTo(0);
assertThat(redis.hget(key, "two")).isEqualTo("second");
}

@Test
void hsetnx() {
redis.hset(key, "one", "1");
Expand Down

0 comments on commit bd2f18c

Please sign in to comment.