Skip to content

Commit e2d8e50

Browse files
Dltmd202tishun
authored andcommitted
Deprecate the STRALGO command and implement the LCS in its place (redis#3037)
* Deprecate the STRALGO command and implement the LCS in its place * Polishing --------- Co-authored-by: Tihomir Mateev <[email protected]>
1 parent 4ceb011 commit e2d8e50

17 files changed

+374
-90
lines changed

src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,11 @@ public RedisFuture<StringMatchResult> stralgoLcs(StrAlgoArgs args) {
24122412
return dispatch(commandBuilder.stralgoLcs(args));
24132413
}
24142414

2415+
@Override
2416+
public RedisFuture<StringMatchResult> lcs(LcsArgs args) {
2417+
return dispatch(commandBuilder.lcs(args));
2418+
}
2419+
24152420
@Override
24162421
public RedisFuture<Set<V>> sunion(K... keys) {
24172422
return dispatch(commandBuilder.sunion(keys));

src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,11 @@ public Mono<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs) {
24952495
return createMono(() -> commandBuilder.stralgoLcs(strAlgoArgs));
24962496
}
24972497

2498+
@Override
2499+
public Mono<StringMatchResult> lcs(LcsArgs lcsArgs) {
2500+
return createMono(() -> commandBuilder.lcs(lcsArgs));
2501+
}
2502+
24982503
@Override
24992504
public Flux<V> sunion(K... keys) {
25002505
return createDissolvingFlux(() -> commandBuilder.sunion(keys));
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2011-Present, Redis Ltd. and Contributors
3+
* All rights reserved.
4+
*
5+
* Licensed under the MIT License.
6+
*/
7+
package io.lettuce.core;
8+
9+
import io.lettuce.core.internal.LettuceAssert;
10+
import io.lettuce.core.protocol.CommandArgs;
11+
import io.lettuce.core.protocol.CommandKeyword;
12+
13+
/**
14+
* Argument list builder for the Redis <a href="https://redis.io/commands/lcs">LCS</a> command. Static import the methods from
15+
* {@link LcsArgs.Builder} and call the methods: {@code keys(…)} .
16+
* <p>
17+
* {@link LcsArgs} is a mutable object and instances should be used only once to avoid shared mutable state.
18+
*
19+
* @author Seonghwan Lee
20+
* @since 6.6
21+
* @see <a href="https://redis.io/commands/lcs">LCS command refference</a>
22+
*/
23+
public class LcsArgs implements CompositeArgument {
24+
25+
private boolean justLen;
26+
27+
private int minMatchLen;
28+
29+
private boolean withMatchLen;
30+
31+
private boolean withIdx;
32+
33+
private String[] keys;
34+
35+
/**
36+
* Builder entry points for {@link LcsArgs}.
37+
*/
38+
public static class Builder {
39+
40+
/**
41+
* Utility constructor.
42+
*/
43+
private Builder() {
44+
}
45+
46+
/**
47+
* Creates new {@link LcsArgs} by keys.
48+
*
49+
* @return new {@link LcsArgs} with {@literal By KEYS} set.
50+
*/
51+
public static LcsArgs keys(String... keys) {
52+
return new LcsArgs().by(keys);
53+
}
54+
55+
}
56+
57+
/**
58+
* restrict the list of matches to the ones of a given minimal length.
59+
*
60+
* @return {@code this} {@link LcsArgs}.
61+
*/
62+
public LcsArgs minMatchLen(int minMatchLen) {
63+
this.minMatchLen = minMatchLen;
64+
return this;
65+
}
66+
67+
/**
68+
* Request just the length of the match for results.
69+
*
70+
* @return {@code this} {@link LcsArgs}.
71+
*/
72+
public LcsArgs justLen() {
73+
justLen = true;
74+
return this;
75+
}
76+
77+
/**
78+
* Request match len for results.
79+
*
80+
* @return {@code this} {@link LcsArgs}.
81+
*/
82+
public LcsArgs withMatchLen() {
83+
withMatchLen = true;
84+
return this;
85+
}
86+
87+
/**
88+
* Request match position in each string for results.
89+
*
90+
* @return {@code this} {@link LcsArgs}.
91+
*/
92+
public LcsArgs withIdx() {
93+
withIdx = true;
94+
return this;
95+
}
96+
97+
public LcsArgs by(String... keys) {
98+
LettuceAssert.notEmpty(keys, "Keys must not be empty");
99+
100+
this.keys = keys;
101+
return this;
102+
}
103+
104+
public boolean isWithIdx() {
105+
return withIdx;
106+
}
107+
108+
@Override
109+
public <K, V> void build(CommandArgs<K, V> args) {
110+
for (String key : keys) {
111+
args.add(key);
112+
}
113+
if (justLen) {
114+
args.add(CommandKeyword.LEN);
115+
}
116+
if (withIdx) {
117+
args.add(CommandKeyword.IDX);
118+
}
119+
120+
if (minMatchLen > 0) {
121+
args.add(CommandKeyword.MINMATCHLEN);
122+
args.add(minMatchLen);
123+
}
124+
125+
if (withMatchLen) {
126+
args.add(CommandKeyword.WITHMATCHLEN);
127+
}
128+
}
129+
130+
}

src/main/java/io/lettuce/core/RedisCommandBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
* @author Mikhael Sokolov
5959
* @author Tihomir Mateev
6060
* @author Ali Takavci
61+
* @author Seonghwan Lee
6162
*/
6263
@SuppressWarnings({ "unchecked", "varargs" })
6364
class RedisCommandBuilder<K, V> extends BaseRedisCommandBuilder<K, V> {
@@ -2912,6 +2913,14 @@ Command<K, V, StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs) {
29122913
return createCommand(STRALGO, new StringMatchResultOutput<>(codec), args);
29132914
}
29142915

2916+
Command<K, V, StringMatchResult> lcs(LcsArgs lcsArgs) {
2917+
LettuceAssert.notNull(lcsArgs, "lcsArgs" + MUST_NOT_BE_NULL);
2918+
2919+
CommandArgs<K, V> args = new CommandArgs<>(codec);
2920+
lcsArgs.build(args);
2921+
return createCommand(LCS, new StringMatchResultOutput<>(codec), args);
2922+
}
2923+
29152924
Command<K, V, Set<V>> sunion(K... keys) {
29162925
notEmpty(keys);
29172926

src/main/java/io/lettuce/core/StrAlgoArgs.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232
* {@link StrAlgoArgs} is a mutable object and instances should be used only once to avoid shared mutable state.
3333
*
3434
* @author dengliming
35+
* @deprecated As of 6.6 in favor of {@link LcsArgs}
36+
* @see <a href="https://redis.io/commands/lcs">LCS command refference</a>
3537
* @since 6.0
3638
*/
39+
@Deprecated
3740
public class StrAlgoArgs implements CompositeArgument {
3841

3942
private boolean justLen;

src/main/java/io/lettuce/core/api/async/RedisStringAsyncCommands.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.lettuce.core.KeyValue;
2828
import io.lettuce.core.RedisFuture;
2929
import io.lettuce.core.SetArgs;
30+
import io.lettuce.core.LcsArgs;
3031
import io.lettuce.core.StrAlgoArgs;
3132
import io.lettuce.core.StringMatchResult;
3233
import io.lettuce.core.output.KeyValueStreamingChannel;
@@ -410,6 +411,8 @@ public interface RedisStringAsyncCommands<K, V> {
410411
/**
411412
* The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest
412413
* common substring).
414+
* <p>
415+
* Command is no longer available in Redis server versions 7.0.x and later.
413416
*
414417
* <ul>
415418
* <li>Without modifiers the string representing the longest common substring is returned.</li>
@@ -422,10 +425,30 @@ public interface RedisStringAsyncCommands<K, V> {
422425
*
423426
* @param strAlgoArgs command arguments.
424427
* @return StringMatchResult.
428+
* @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}.
425429
* @since 6.0
426430
*/
431+
@Deprecated
427432
RedisFuture<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs);
428433

434+
/**
435+
* The LCS command implements the longest common subsequence algorithm.
436+
*
437+
* <ul>
438+
* <li>Without modifiers, the string representing the longest common substring is returned.</li>
439+
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
440+
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
441+
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
442+
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
443+
* </ul>
444+
*
445+
* @param lcsArgs command arguments supplied by the {@link LcsArgs}.
446+
* @return StringMatchResult
447+
* @see <a href="https://redis.io/commands/lcs">LCS command refference</a>
448+
* @since 6.6
449+
*/
450+
RedisFuture<StringMatchResult> lcs(LcsArgs lcsArgs);
451+
429452
/**
430453
* Get the length of the value stored in a key.
431454
*

src/main/java/io/lettuce/core/api/reactive/RedisStringReactiveCommands.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.lettuce.core.KeyValue;
2929
import io.lettuce.core.SetArgs;
3030
import io.lettuce.core.StrAlgoArgs;
31+
import io.lettuce.core.LcsArgs;
3132
import io.lettuce.core.StringMatchResult;
3233
import io.lettuce.core.Value;
3334
import io.lettuce.core.output.KeyValueStreamingChannel;
@@ -414,6 +415,8 @@ public interface RedisStringReactiveCommands<K, V> {
414415
/**
415416
* The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest
416417
* common substring).
418+
* <p>
419+
* Command is no longer available in Redis server versions 7.0.x and later.
417420
*
418421
* <ul>
419422
* <li>Without modifiers the string representing the longest common substring is returned.</li>
@@ -426,10 +429,30 @@ public interface RedisStringReactiveCommands<K, V> {
426429
*
427430
* @param strAlgoArgs command arguments.
428431
* @return StringMatchResult.
432+
* @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}.
429433
* @since 6.0
430434
*/
435+
@Deprecated
431436
Mono<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs);
432437

438+
/**
439+
* The LCS command implements the longest common subsequence algorithm.
440+
*
441+
* <ul>
442+
* <li>Without modifiers, the string representing the longest common substring is returned.</li>
443+
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
444+
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
445+
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
446+
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
447+
* </ul>
448+
*
449+
* @param lcsArgs command arguments supplied by the {@link LcsArgs}.
450+
* @return StringMatchResult
451+
* @see <a href="https://redis.io/commands/lcs">LCS command refference</a>
452+
* @since 6.6
453+
*/
454+
Mono<StringMatchResult> lcs(LcsArgs lcsArgs);
455+
433456
/**
434457
* Get the length of the value stored in a key.
435458
*

src/main/java/io/lettuce/core/api/sync/RedisStringCommands.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.lettuce.core.KeyValue;
2828
import io.lettuce.core.SetArgs;
2929
import io.lettuce.core.StrAlgoArgs;
30+
import io.lettuce.core.LcsArgs;
3031
import io.lettuce.core.StringMatchResult;
3132
import io.lettuce.core.output.KeyValueStreamingChannel;
3233

@@ -409,6 +410,8 @@ public interface RedisStringCommands<K, V> {
409410
/**
410411
* The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest
411412
* common substring).
413+
* <p>
414+
* Command is no longer available in Redis server versions 7.0.x and later.
412415
*
413416
* <ul>
414417
* <li>Without modifiers the string representing the longest common substring is returned.</li>
@@ -421,10 +424,30 @@ public interface RedisStringCommands<K, V> {
421424
*
422425
* @param strAlgoArgs command arguments.
423426
* @return StringMatchResult.
427+
* @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}.
424428
* @since 6.0
425429
*/
430+
@Deprecated
426431
StringMatchResult stralgoLcs(StrAlgoArgs strAlgoArgs);
427432

433+
/**
434+
* The LCS command implements the longest common subsequence algorithm.
435+
*
436+
* <ul>
437+
* <li>Without modifiers, the string representing the longest common substring is returned.</li>
438+
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
439+
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
440+
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
441+
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
442+
* </ul>
443+
*
444+
* @param lcsArgs command arguments supplied by the {@link LcsArgs}.
445+
* @return StringMatchResult
446+
* @see <a href="https://redis.io/commands/lcs">LCS command refference</a>
447+
* @since 6.6
448+
*/
449+
StringMatchResult lcs(LcsArgs lcsArgs);
450+
428451
/**
429452
* Get the length of the value stored in a key.
430453
*

src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionStringAsyncCommands.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.lettuce.core.SetArgs;
2929
import io.lettuce.core.StrAlgoArgs;
3030
import io.lettuce.core.StringMatchResult;
31+
import io.lettuce.core.LcsArgs;
3132
import io.lettuce.core.output.KeyValueStreamingChannel;
3233

3334
/**
@@ -409,6 +410,8 @@ public interface NodeSelectionStringAsyncCommands<K, V> {
409410
/**
410411
* The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest
411412
* common substring).
413+
* <p>
414+
* Command is no longer available in Redis server versions 7.0.x and later.
412415
*
413416
* <ul>
414417
* <li>Without modifiers the string representing the longest common substring is returned.</li>
@@ -421,10 +424,30 @@ public interface NodeSelectionStringAsyncCommands<K, V> {
421424
*
422425
* @param strAlgoArgs command arguments.
423426
* @return StringMatchResult.
427+
* @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}.
424428
* @since 6.0
425429
*/
430+
@Deprecated
426431
AsyncExecutions<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs);
427432

433+
/**
434+
* The LCS command implements the longest common subsequence algorithm.
435+
*
436+
* <ul>
437+
* <li>Without modifiers, the string representing the longest common substring is returned.</li>
438+
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
439+
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
440+
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
441+
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
442+
* </ul>
443+
*
444+
* @param lcsArgs command arguments supplied by the {@link LcsArgs}.
445+
* @return StringMatchResult
446+
* @see <a href="https://redis.io/commands/lcs">LCS command refference</a>
447+
* @since 6.6
448+
*/
449+
AsyncExecutions<StringMatchResult> lcs(LcsArgs lcsArgs);
450+
428451
/**
429452
* Get the length of the value stored in a key.
430453
*

0 commit comments

Comments
 (0)