Skip to content

Commit 7b3a358

Browse files
christophstroblThomas Darimont
authored andcommitted
DATAREDIS-313 - Overhaul SCAN, HSCAN, SSCAN, ZSCAN operations.
Unify naming and return types. Update supported commands documentation. Remove key from BoundHashOperations and use getKey() instead. Prevent error when trying to read values when there are no values present.
1 parent d8b53a4 commit 7b3a358

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,8 +3112,7 @@ protected ScanIteration<Entry<byte[], byte[]>> doScan(byte[] key, long cursorId,
31123112
List<?> result = eval(script.getBytes(), ReturnType.MULTI, 0);
31133113
String nextCursorId = LettuceConverters.bytesToString().convert((byte[]) result.get(0));
31143114

3115-
@SuppressWarnings("unchecked")
3116-
Map<byte[], byte[]> values = LettuceConverters.toMap((List<byte[]>) result.get(1));
3115+
Map<byte[], byte[]> values = failsafeReadScanValues(result, LettuceConverters.bytesListToMapConverter());
31173116
return new ScanIteration<Entry<byte[], byte[]>>(Long.valueOf(nextCursorId), values.entrySet());
31183117
}
31193118
}.open();
@@ -3139,7 +3138,6 @@ public Cursor<byte[]> sScan(byte[] key, long cursorId, ScanOptions options) {
31393138

31403139
return new KeyBoundCursor<byte[]>(key, cursorId, options) {
31413140

3142-
@SuppressWarnings("unchecked")
31433141
@Override
31443142
protected ScanIteration<byte[]> doScan(byte[] key, long cursorId, ScanOptions options) {
31453143

@@ -3154,7 +3152,8 @@ protected ScanIteration<byte[]> doScan(byte[] key, long cursorId, ScanOptions op
31543152
List<?> result = eval(script.getBytes(), ReturnType.MULTI, 0);
31553153
String nextCursorId = LettuceConverters.bytesToString().convert((byte[]) result.get(0));
31563154

3157-
return new ScanIteration<byte[]>(Long.valueOf(nextCursorId), ((ArrayList<byte[]>) result.get(1)));
3155+
List<byte[]> values = failsafeReadScanValues(result, null);
3156+
return new ScanIteration<byte[]>(Long.valueOf(nextCursorId), values);
31583157
}
31593158
}.open();
31603159
}
@@ -3179,7 +3178,6 @@ public Cursor<Tuple> zScan(byte[] key, long cursorId, ScanOptions options) {
31793178

31803179
return new KeyBoundCursor<Tuple>(key, cursorId, options) {
31813180

3182-
@SuppressWarnings("unchecked")
31833181
@Override
31843182
protected ScanIteration<Tuple> doScan(byte[] key, long cursorId, ScanOptions options) {
31853183

@@ -3194,12 +3192,23 @@ protected ScanIteration<Tuple> doScan(byte[] key, long cursorId, ScanOptions opt
31943192
List<?> result = eval(script.getBytes(), ReturnType.MULTI, 0);
31953193
String nextCursorId = LettuceConverters.bytesToString().convert((byte[]) result.get(0));
31963194

3197-
return new ScanIteration<Tuple>(Long.valueOf(nextCursorId), LettuceConverters.toTuple((List<byte[]>) result
3198-
.get(1)));
3195+
List<Tuple> values = failsafeReadScanValues(result, LettuceConverters.bytesListToTupleListConverter());
3196+
return new ScanIteration<Tuple>(Long.valueOf(nextCursorId), values);
31993197
}
32003198
}.open();
32013199
}
32023200

3201+
@SuppressWarnings("unchecked")
3202+
private <T> T failsafeReadScanValues(List<?> source, @SuppressWarnings("rawtypes") Converter converter) {
3203+
3204+
try {
3205+
return (T) (converter != null ? converter.convert(source.get(1)) : source.get(1));
3206+
} catch (IndexOutOfBoundsException e) {
3207+
// ignore this one
3208+
}
3209+
return null;
3210+
}
3211+
32033212
/**
32043213
* Specifies if pipelined and transaction results should be converted to the expected data type. If false, results of
32053214
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Lettuce driver

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ public static List<Tuple> toTuple(List<byte[]> list) {
174174
return BYTES_LIST_TO_TUPLE_LIST_CONVERTER.convert(list);
175175
}
176176

177+
public static Converter<List<byte[]>, List<Tuple>> bytesListToTupleListConverter() {
178+
return BYTES_LIST_TO_TUPLE_LIST_CONVERTER;
179+
}
180+
177181
public static Converter<String, List<RedisClientInfo>> stringToRedisClientListConverter() {
178182
return new Converter<String, List<RedisClientInfo>>() {
179183

@@ -290,6 +294,10 @@ public static Map<byte[], byte[]> toMap(List<byte[]> source) {
290294
return BYTES_LIST_TO_MAP.convert(source);
291295
}
292296

297+
public static Converter<List<byte[]>, Map<byte[], byte[]>> bytesListToMapConverter() {
298+
return BYTES_LIST_TO_MAP;
299+
}
300+
293301
public static SortArgs toSortArgs(SortParameters params) {
294302
SortArgs args = new SortArgs();
295303
if (params == null) {

0 commit comments

Comments
 (0)