Add support for HRANDFIELD.

Closes: #2048
Original Pull Request: #2104
This commit is contained in:
Mark Paluch
2021-06-25 11:55:14 +02:00
committed by Christoph Strobl
parent 961ba9035d
commit 0b8fbae2c3
22 changed files with 1125 additions and 19 deletions

View File

@@ -1269,6 +1269,52 @@ public abstract class AbstractConnectionIntegrationTests {
verifyResults(Arrays.asList(new Object[] { true, largeNumber, -largeNumber }));
}
@Test // GH-2048
@EnabledOnCommand("HRANDFIELD")
void testHRandField() {
String key = "hash";
Map<String, String> hash = new HashMap<>();
hash.put("key1", "val1");
hash.put("key2", "val2");
hash.put("key3", "val3");
hash.put("key4", "val4");
connection.hMSet(key, hash);
actual.add(connection.hRandField(key));
actual.add(connection.hRandField(key, 2));
actual.add(connection.hRandField(key, -10));
List<Object> results = getResults();
assertThat((String) results.get(0)).isIn(hash.keySet());
assertThat((List<String>) results.get(1)).hasSize(2);
assertThat((List<String>) results.get(2)).hasSize(10);
}
@Test // GH-2048
@EnabledOnCommand("HRANDFIELD")
void testHRandFieldWithValues() {
String key = "hash";
Map<String, String> hash = new HashMap<>();
hash.put("key1", "val1");
hash.put("key2", "val2");
hash.put("key3", "val3");
hash.put("key4", "val4");
connection.hMSet(key, hash);
actual.add(connection.hRandFieldWithValues(key));
actual.add(connection.hRandFieldWithValues(key, 2));
actual.add(connection.hRandFieldWithValues(key, -10));
List<Object> results = getResults();
assertThat(results.get(0)).isNotNull();
assertThat((List<?>) results.get(1)).hasSize(2);
// Oh Jedis, JedisByteHashMap.ByteArrayWrapper. Why?
assertThat((List<?>) results.get(2)).hasSizeGreaterThan(2);
}
@Test
void testIncDecr() {

View File

@@ -657,6 +657,22 @@ class RedisConnectionUnitTests {
return delegate.hGetAll(key);
}
public byte[] hRandField(byte[] key) {
return delegate.hRandField(key);
}
public Entry<byte[], byte[]> hRandFieldWithValues(byte[] key) {
return delegate.hRandFieldWithValues(key);
}
public List<byte[]> hRandField(byte[] key, long count) {
return delegate.hRandField(key, count);
}
public List<Entry<byte[], byte[]>> hRandFieldWithValues(byte[] key, long count) {
return delegate.hRandFieldWithValues(key, count);
}
public Boolean move(byte[] key, int dbIndex) {
return delegate.move(key, dbIndex);
}

View File

@@ -162,4 +162,43 @@ public class DefaultHashOperationsIntegrationTests<K, HK, HV> {
assertThat(hashOps.lengthOfValue(key, key1)).isEqualTo(Long.valueOf(val1.toString().length()));
}
@ParameterizedRedisTest // GH-2048
void randomField() {
K key = keyFactory.instance();
HK key1 = hashKeyFactory.instance();
HV val1 = hashValueFactory.instance();
HK key2 = hashKeyFactory.instance();
HV val2 = hashValueFactory.instance();
hashOps.put(key, key1, val1);
hashOps.put(key, key2, val2);
assertThat(hashOps.randomField(key)).isIn(key1, key2);
assertThat(hashOps.randomFields(key, 2)).hasSize(2).contains(key1, key2);
}
@ParameterizedRedisTest // GH-2048
void randomValue() {
assumeThat(hashKeyFactory).isNotInstanceOf(RawObjectFactory.class);
K key = keyFactory.instance();
HK key1 = hashKeyFactory.instance();
HV val1 = hashValueFactory.instance();
HK key2 = hashKeyFactory.instance();
HV val2 = hashValueFactory.instance();
hashOps.put(key, key1, val1);
hashOps.put(key, key2, val2);
Map.Entry<HK, HV> entry = hashOps.randomValue(key);
if (entry.getKey().equals(key1)) {
assertThat(entry.getValue()).isEqualTo(val1);
} else {
assertThat(entry.getValue()).isEqualTo(val2);
}
Map<HK, HV> values = hashOps.randomValues(key, 10);
assertThat(values).hasSize(2).containsEntry(key1, val1).containsEntry(key2, val2);
}
}

View File

@@ -38,6 +38,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.test.condition.EnabledOnCommand;
import org.springframework.data.redis.test.extension.LettuceTestClientResources;
import org.springframework.data.redis.test.extension.parametrized.MethodSource;
import org.springframework.data.redis.test.extension.parametrized.ParameterizedRedisTest;
@@ -228,6 +229,81 @@ public class DefaultReactiveHashOperationsIntegrationTests<K, HK, HV> {
.verifyComplete();
}
@ParameterizedRedisTest // GH-2048
@EnabledOnCommand("HRANDFIELD")
void randomField() {
assumeThat(hashValueFactory).isNotInstanceOf(RawObjectFactory.class);
K key = keyFactory.instance();
HK hashkey1 = hashKeyFactory.instance();
HV hashvalue1 = hashValueFactory.instance();
HK hashkey2 = hashKeyFactory.instance();
HV hashvalue2 = hashValueFactory.instance();
hashOperations.put(key, hashkey1, hashvalue1) //
.as(StepVerifier::create) //
.expectNext(true) //
.verifyComplete();
hashOperations.put(key, hashkey2, hashvalue2) //
.as(StepVerifier::create) //
.expectNext(true) //
.verifyComplete();
hashOperations.randomField(key) //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual).isIn(hashkey1, hashkey2);
}).verifyComplete();
hashOperations.randomFields(key, -10) //
.collectList().as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual).hasSize(10);
}).verifyComplete();
}
@ParameterizedRedisTest // GH-2048
@EnabledOnCommand("HRANDFIELD")
void randomValue() {
assumeThat(hashValueFactory).isNotInstanceOf(RawObjectFactory.class);
K key = keyFactory.instance();
HK hashkey1 = hashKeyFactory.instance();
HV hashvalue1 = hashValueFactory.instance();
HK hashkey2 = hashKeyFactory.instance();
HV hashvalue2 = hashValueFactory.instance();
hashOperations.put(key, hashkey1, hashvalue1) //
.as(StepVerifier::create) //
.expectNext(true) //
.verifyComplete();
hashOperations.put(key, hashkey2, hashvalue2) //
.as(StepVerifier::create) //
.expectNext(true) //
.verifyComplete();
hashOperations.randomValue(key) //
.as(StepVerifier::create) //
.assertNext(actual -> {
if (actual.getKey().equals(hashkey1)) {
assertThat(actual.getValue()).isEqualTo(hashvalue1);
} else {
assertThat(actual.getValue()).isEqualTo(hashvalue2);
}
}).verifyComplete();
hashOperations.randomValues(key, -10) //
.collectList().as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual).hasSize(10);
}).verifyComplete();
}
@ParameterizedRedisTest // DATAREDIS-602
@SuppressWarnings("unchecked")
void incrementDouble() {