Skip to content

Commit 0955a4f

Browse files
committed
DATAREDIS-716 - Polishing.
Add OBJECT command support for Jedis Cluster. Original pull request: #337.
1 parent 70a3e5d commit 0955a4f

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

src/main/java/org/springframework/data/redis/connection/ClusterCommandExecutor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map.Entry;
2525
import java.util.concurrent.ExecutionException;
2626
import java.util.concurrent.Future;
27+
import java.util.function.Function;
2728
import java.util.stream.Collectors;
2829

2930
import org.springframework.beans.factory.DisposableBean;
@@ -493,6 +494,22 @@ public RedisClusterNode getNode() {
493494
public byte[] getKey() {
494495
return key.getArray();
495496
}
497+
498+
/**
499+
* Apply the {@link Function mapper function} to the value and return the mapped value.
500+
*
501+
* @param mapper must not be {@literal null}.
502+
* @param <U> type of the mapped value.
503+
* @return the mapped value.
504+
* @since 2.1
505+
*/
506+
@Nullable
507+
public <U> U mapValue(Function<? super T, ? extends U> mapper) {
508+
509+
Assert.notNull(mapper, "Mapper function must not be null!");
510+
511+
return mapper.apply(getValue());
512+
}
496513
}
497514

498515
/**

src/main/java/org/springframework/data/redis/connection/jedis/JedisClusterKeyCommands.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,13 @@ public Long exists(byte[]... keys) {
568568
@Nullable
569569
@Override
570570
public ValueEncoding encodingOf(byte[] key) {
571-
throw new UnsupportedOperationException("Jedis does not support OBJECT ENCODING in cluster!");
571+
572+
Assert.notNull(key, "Key must not be null!");
573+
574+
return connection.getClusterCommandExecutor()
575+
.executeCommandOnSingleNode((JedisClusterCommandCallback<byte[]>) client -> client.objectEncoding(key),
576+
connection.clusterGetNodeForKey(key))
577+
.mapValue(JedisConverters::toEncoding);
572578
}
573579

574580
/*
@@ -578,7 +584,13 @@ public ValueEncoding encodingOf(byte[] key) {
578584
@Nullable
579585
@Override
580586
public Duration idletime(byte[] key) {
581-
throw new UnsupportedOperationException("Jedis does not support OBJECT IDLETIME in cluster!");
587+
588+
Assert.notNull(key, "Key must not be null!");
589+
590+
return connection.getClusterCommandExecutor()
591+
.executeCommandOnSingleNode((JedisClusterCommandCallback<Long>) client -> client.objectIdletime(key),
592+
connection.clusterGetNodeForKey(key))
593+
.mapValue(Converters::secondsToDuration);
582594
}
583595

584596
/*
@@ -588,7 +600,14 @@ public Duration idletime(byte[] key) {
588600
@Nullable
589601
@Override
590602
public Long refcount(byte[] key) {
591-
throw new UnsupportedOperationException("Jedis does not support OBJECT REFCOUNT in cluster!");
603+
604+
Assert.notNull(key, "Key must not be null!");
605+
606+
return connection.getClusterCommandExecutor()
607+
.executeCommandOnSingleNode((JedisClusterCommandCallback<Long>) client -> client.objectRefcount(key),
608+
connection.clusterGetNodeForKey(key))
609+
.getValue();
610+
592611
}
593612

594613
private DataAccessException convertJedisAccessException(Exception ex) {

src/test/java/org/springframework/data/redis/connection/jedis/JedisClusterConnectionTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import java.io.IOException;
3030
import java.nio.charset.Charset;
31+
import java.time.Duration;
3132
import java.util.*;
3233
import java.util.concurrent.TimeUnit;
3334

@@ -57,6 +58,7 @@
5758
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
5859
import org.springframework.data.redis.connection.RedisZSetCommands.Range;
5960
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
61+
import org.springframework.data.redis.connection.ValueEncoding.RedisValueEncoding;
6062
import org.springframework.data.redis.core.Cursor;
6163
import org.springframework.data.redis.core.ScanOptions;
6264
import org.springframework.data.redis.core.types.Expiration;
@@ -2292,4 +2294,43 @@ public void bitPosShouldReturnPositionInRangeCorrectly() {
22922294
org.springframework.data.domain.Range.of(Bound.inclusive(2L), Bound.unbounded())), is(16L));
22932295
}
22942296

2297+
@Test // DATAREDIS-716
2298+
public void encodingReturnsCorrectly() {
2299+
2300+
nativeConnection.set(KEY_1_BYTES, "1000".getBytes());
2301+
2302+
assertThat(clusterConnection.keyCommands().encodingOf(KEY_1_BYTES), is(RedisValueEncoding.INT));
2303+
}
2304+
2305+
@Test // DATAREDIS-716
2306+
public void encodingReturnsVacantWhenKeyDoesNotExist() {
2307+
assertThat(clusterConnection.keyCommands().encodingOf(KEY_2_BYTES), is(RedisValueEncoding.VACANT));
2308+
}
2309+
2310+
@Test // DATAREDIS-716
2311+
public void idletimeReturnsCorrectly() {
2312+
2313+
nativeConnection.set(KEY_1_BYTES, VALUE_1_BYTES);
2314+
nativeConnection.get(KEY_1_BYTES);
2315+
2316+
assertThat(clusterConnection.keyCommands().idletime(KEY_1_BYTES), is(Duration.ofSeconds(0)));
2317+
}
2318+
2319+
@Test // DATAREDIS-716
2320+
public void idldetimeReturnsNullWhenKeyDoesNotExist() {
2321+
assertThat(clusterConnection.keyCommands().idletime(KEY_3_BYTES), is(nullValue()));
2322+
}
2323+
2324+
@Test // DATAREDIS-716
2325+
public void refcountReturnsCorrectly() {
2326+
2327+
nativeConnection.lpush(KEY_1_BYTES, VALUE_1_BYTES);
2328+
2329+
assertThat(clusterConnection.keyCommands().refcount(KEY_1_BYTES), is(1L));
2330+
}
2331+
2332+
@Test // DATAREDIS-716
2333+
public void refcountReturnsNullWhenKeyDoesNotExist() {
2334+
assertThat(clusterConnection.keyCommands().refcount(KEY_3_BYTES), is(nullValue()));
2335+
}
22952336
}

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnectionTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
5353
import org.springframework.data.redis.connection.RedisZSetCommands.Range;
5454
import org.springframework.data.redis.connection.RedisZSetCommands.Tuple;
55+
import org.springframework.data.redis.connection.ValueEncoding.RedisValueEncoding;
5556
import org.springframework.data.redis.connection.jedis.JedisConverters;
5657
import org.springframework.data.redis.core.Cursor;
5758
import org.springframework.data.redis.core.ScanOptions;
@@ -2317,4 +2318,44 @@ public void bitPosShouldReturnPositionInRangeCorrectly() {
23172318
assertThat(clusterConnection.stringCommands().bitPos(KEY_1_BYTES, true,
23182319
org.springframework.data.domain.Range.of(Bound.inclusive(2L), Bound.unbounded())), is(16L));
23192320
}
2321+
2322+
@Test // DATAREDIS-716
2323+
public void encodingReturnsCorrectly() {
2324+
2325+
nativeConnection.set(KEY_1, "1000");
2326+
2327+
assertThat(clusterConnection.keyCommands().encodingOf(KEY_1_BYTES), is(RedisValueEncoding.INT));
2328+
}
2329+
2330+
@Test // DATAREDIS-716
2331+
public void encodingReturnsVacantWhenKeyDoesNotExist() {
2332+
assertThat(clusterConnection.keyCommands().encodingOf(KEY_2_BYTES), is(RedisValueEncoding.VACANT));
2333+
}
2334+
2335+
@Test // DATAREDIS-716
2336+
public void idletimeReturnsCorrectly() {
2337+
2338+
nativeConnection.set(KEY_1, VALUE_1);
2339+
nativeConnection.get(KEY_1);
2340+
2341+
assertThat(clusterConnection.keyCommands().idletime(KEY_1_BYTES), is(Duration.ofSeconds(0)));
2342+
}
2343+
2344+
@Test // DATAREDIS-716
2345+
public void idldetimeReturnsNullWhenKeyDoesNotExist() {
2346+
assertThat(clusterConnection.keyCommands().idletime(KEY_3_BYTES), is(nullValue()));
2347+
}
2348+
2349+
@Test // DATAREDIS-716
2350+
public void refcountReturnsCorrectly() {
2351+
2352+
nativeConnection.lpush(KEY_1, VALUE_1);
2353+
2354+
assertThat(clusterConnection.keyCommands().refcount(KEY_1_BYTES), is(1L));
2355+
}
2356+
2357+
@Test // DATAREDIS-716
2358+
public void refcountReturnsNullWhenKeyDoesNotExist() {
2359+
assertThat(clusterConnection.keyCommands().refcount(KEY_3_BYTES), is(nullValue()));
2360+
}
23202361
}

0 commit comments

Comments
 (0)