Add ScanOptions.pattern(…) accepting a byte[] pattern.

We now accept a byte array as pattern for scan operations in addition to String patterns.

Also, use binary Jedis scan method to avoid bytes to String conversion.

Closes: #2006
Original Pull Request: #2051
This commit is contained in:
Mark Paluch
2021-04-21 10:38:07 +02:00
committed by Christoph Strobl
parent 67deb3ef81
commit a525fc1fd9
5 changed files with 48 additions and 14 deletions

View File

@@ -529,8 +529,9 @@ public abstract class JedisConverters extends Converters {
if (options.getCount() != null) {
sp.count(options.getCount().intValue());
}
if (StringUtils.hasText(options.getPattern())) {
sp.match(options.getPattern());
byte[] pattern = options.getBytePattern();
if (pattern != null) {
sp.match(pattern);
}
}
return sp;

View File

@@ -168,9 +168,10 @@ class JedisKeyCommands implements RedisKeyCommands {
}
ScanParams params = JedisConverters.toScanParams(options);
redis.clients.jedis.ScanResult<String> result = connection.getJedis().scan(Long.toString(cursorId), params);
redis.clients.jedis.ScanResult<byte[]> result = connection.getJedis().scan(Long.toString(cursorId).getBytes(),
params);
return new ScanIteration<>(Long.parseLong(result.getCursor()),
JedisConverters.stringListToByteList().convert(result.getResult()));
result.getResult());
}
protected void doClose() {

View File

@@ -868,8 +868,9 @@ public abstract class LettuceConverters extends Converters {
ScanArgs scanArgs = new ScanArgs();
if (options.getPattern() != null) {
scanArgs.match(options.getPattern());
byte[] pattern = options.getBytePattern();
if (pattern != null) {
scanArgs.match(pattern);
}
if (options.getCount() != null) {

View File

@@ -31,14 +31,16 @@ public class ScanOptions {
/**
* Constant to apply default {@link ScanOptions} without setting a limit or matching a pattern.
*/
public static ScanOptions NONE = new ScanOptions(null, null);
public static ScanOptions NONE = new ScanOptions(null, null, null);
private final @Nullable Long count;
private final @Nullable String pattern;
private final @Nullable byte[] bytePattern;
private ScanOptions(@Nullable Long count, @Nullable String pattern) {
private ScanOptions(@Nullable Long count, @Nullable String pattern, @Nullable byte[] bytePattern) {
this.count = count;
this.pattern = pattern;
this.bytePattern = bytePattern;
}
/**
@@ -57,9 +59,24 @@ public class ScanOptions {
@Nullable
public String getPattern() {
if (bytePattern != null && pattern == null) {
return new String(bytePattern);
}
return pattern;
}
@Nullable
public byte[] getBytePattern() {
if (bytePattern == null && pattern != null) {
return pattern.getBytes();
}
return bytePattern;
}
public String toOptionString() {
if (this.equals(ScanOptions.NONE)) {
@@ -71,7 +88,8 @@ public class ScanOptions {
if (this.count != null) {
params += (", 'count', " + count);
}
if (StringUtils.hasText(this.pattern)) {
String pattern = getPattern();
if (StringUtils.hasText(pattern)) {
params += (", 'match' , '" + this.pattern + "'");
}
@@ -87,6 +105,7 @@ public class ScanOptions {
private @Nullable Long count;
private @Nullable String pattern;
private @Nullable byte[] bytePattern;
/**
@@ -111,13 +130,25 @@ public class ScanOptions {
return this;
}
/**
* Returns the current {@link ScanOptionsBuilder} configured with the given {@code pattern}.
*
* @param pattern
* @return
* @since 2.6
*/
public ScanOptionsBuilder match(byte[] pattern) {
this.bytePattern = pattern;
return this;
}
/**
* Builds a new {@link ScanOptions} objects.
*
* @return a new {@link ScanOptions} objects.
*/
public ScanOptions build() {
return new ScanOptions(count, pattern);
return new ScanOptions(count, pattern, bytePattern);
}
}
}