Skip to content

Commit

Permalink
Deprecate the STRALGO command and implement the LCS in its place
Browse files Browse the repository at this point in the history
  • Loading branch information
Dltmd202 committed Nov 12, 2024
1 parent 114755d commit 63ee6b9
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 63 deletions.
5 changes: 5 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,11 @@ public RedisFuture<StringMatchResult> stralgoLcs(StrAlgoArgs args) {
return dispatch(commandBuilder.stralgoLcs(args));
}

@Override
public RedisFuture<StringMatchResult> lcs(LcsArgs args) {
return dispatch(commandBuilder.lcs(args));
}

@Override
public RedisFuture<Set<V>> sunion(K... keys) {
return dispatch(commandBuilder.sunion(keys));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,11 @@ public Mono<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs) {
return createMono(() -> commandBuilder.stralgoLcs(strAlgoArgs));
}

@Override
public Mono<StringMatchResult> lcs(LcsArgs lcsArgs) {
return createMono(() -> commandBuilder.lcs(lcsArgs));
}

@Override
public Flux<V> sunion(K... keys) {
return createDissolvingFlux(() -> commandBuilder.sunion(keys));
Expand Down
137 changes: 137 additions & 0 deletions src/main/java/io/lettuce/core/LcsArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.lettuce.core;

import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.protocol.CommandArgs;

/**
* Argument list builder for the Redis <a href="https://redis.io/commands/lcs">LCS</a> command. Static import the methods from
* {@link LcsArgs.Builder} and call the methods: {@code keys(…)} .
* <p>
* {@link LcsArgs} is a mutable object and instances should be used only once to avoid shared mutable state.
*
* @author Seonghwan Lee
* @since 6.6
*/
public class LcsArgs implements CompositeArgument {

private boolean justLen;

private int minMatchLen;

private boolean withMatchLen;

private boolean withIdx;

private String[] keys;

/**
* Builder entry points for {@link LcsArgs}.
*/
public static class Builder {

/**
* Utility constructor.
*/
private Builder() {
}

/**
* Creates new {@link LcsArgs} by keys.
*
* @return new {@link LcsArgs} with {@literal By KEYS} set.
*/
public static LcsArgs keys(String... keys) {
return new LcsArgs().by(keys);
}

}

/**
* restrict the list of matches to the ones of a given minimal length.
*
* @return {@code this} {@link LcsArgs}.
*/
public LcsArgs minMatchLen(int minMatchLen) {
this.minMatchLen = minMatchLen;
return this;
}

/**
* Request just the length of the match for results.
*
* @return {@code this} {@link LcsArgs}.
*/
public LcsArgs justLen() {
justLen = true;
return this;
}

/**
* Request match len for results.
*
* @return {@code this} {@link LcsArgs}.
*/
public LcsArgs withMatchLen() {
withMatchLen = true;
return this;
}

/**
* Request match position in each strings for results.
*
* @return {@code this} {@link LcsArgs}.
*/
public LcsArgs withIdx() {
withIdx = true;
return this;
}

public LcsArgs by(String... keys) {
LettuceAssert.notEmpty(keys, "Keys must not be empty");

this.keys = keys;
return this;
}

public boolean isWithIdx() {
return withIdx;
}

@Override
public <K, V> void build(CommandArgs<K, V> args) {
for (String key : keys) {
args.add(key);
}
if (justLen) {
args.add("LEN");
}
if (withIdx) {
args.add("IDX");
}

if (minMatchLen > 0) {
args.add("MINMATCHLEN");
args.add(minMatchLen);
}

if (withMatchLen) {
args.add("WITHMATCHLEN");
}
}

}
8 changes: 8 additions & 0 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,14 @@ Command<K, V, StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs) {
return createCommand(STRALGO, new StringMatchResultOutput<>(codec), args);
}

Command<K, V, StringMatchResult> lcs(LcsArgs lcsArgs) {
LettuceAssert.notNull(lcsArgs, "lcsArgs" + MUST_NOT_BE_NULL);

CommandArgs<K, V> args = new CommandArgs<>(codec);
lcsArgs.build(args);
return createCommand(LCS, new StringMatchResultOutput<>(codec), args);
}

Command<K, V, Set<V>> sunion(K... keys) {
notEmpty(keys);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.lettuce.core.KeyValue;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.SetArgs;
import io.lettuce.core.LcsArgs;
import io.lettuce.core.StrAlgoArgs;
import io.lettuce.core.StringMatchResult;
import io.lettuce.core.output.KeyValueStreamingChannel;
Expand Down Expand Up @@ -424,8 +425,26 @@ public interface RedisStringAsyncCommands<K, V> {
* @return StringMatchResult.
* @since 6.0
*/
@Deprecated
RedisFuture<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs);

/**
* The LCS command implements the longest common subsequence algorithm.
*
* <ul>
* <li>Without modifiers the string representing the longest common substring is returned.</li>
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
* </ul>
*
* @param lcsArgs command arguments.
* @return StringMatchResult.
* @since 6.6
*/
RedisFuture<StringMatchResult> lcs(LcsArgs lcsArgs);

/**
* Get the length of the value stored in a key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.lettuce.core.KeyValue;
import io.lettuce.core.SetArgs;
import io.lettuce.core.StrAlgoArgs;
import io.lettuce.core.LcsArgs;
import io.lettuce.core.StringMatchResult;
import io.lettuce.core.Value;
import io.lettuce.core.output.KeyValueStreamingChannel;
Expand Down Expand Up @@ -428,8 +429,26 @@ public interface RedisStringReactiveCommands<K, V> {
* @return StringMatchResult.
* @since 6.0
*/
@Deprecated
Mono<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs);

/**
* The LCS command implements the longest common subsequence algorithm.
*
* <ul>
* <li>Without modifiers the string representing the longest common substring is returned.</li>
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
* </ul>
*
* @param lcsArgs command arguments.
* @return StringMatchResult.
* @since 6.6
*/
Mono<StringMatchResult> lcs(LcsArgs lcsArgs);

/**
* Get the length of the value stored in a key.
*
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/lettuce/core/api/sync/RedisStringCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.lettuce.core.KeyValue;
import io.lettuce.core.SetArgs;
import io.lettuce.core.StrAlgoArgs;
import io.lettuce.core.LcsArgs;
import io.lettuce.core.StringMatchResult;
import io.lettuce.core.output.KeyValueStreamingChannel;

Expand Down Expand Up @@ -423,8 +424,26 @@ public interface RedisStringCommands<K, V> {
* @return StringMatchResult.
* @since 6.0
*/
@Deprecated
StringMatchResult stralgoLcs(StrAlgoArgs strAlgoArgs);

/**
* The LCS command implements the longest common subsequence algorithm.
*
* <ul>
* <li>Without modifiers the string representing the longest common substring is returned.</li>
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
* </ul>
*
* @param lcsArgs command arguments.
* @return StringMatchResult.
* @since 6.6
*/
StringMatchResult lcs(LcsArgs lcsArgs);

/**
* Get the length of the value stored in a key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.lettuce.core.SetArgs;
import io.lettuce.core.StrAlgoArgs;
import io.lettuce.core.StringMatchResult;
import io.lettuce.core.LcsArgs;
import io.lettuce.core.output.KeyValueStreamingChannel;

/**
Expand Down Expand Up @@ -425,6 +426,23 @@ public interface NodeSelectionStringAsyncCommands<K, V> {
*/
AsyncExecutions<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs);

/**
* The LCS command implements the longest common subsequence algorithm.
*
* <ul>
* <li>Without modifiers the string representing the longest common substring is returned.</li>
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
* </ul>
*
* @param lcsArgs command arguments.
* @return StringMatchResult.
* @since 6.6
*/
AsyncExecutions<StringMatchResult> lcs(LcsArgs lcsArgs);

/**
* Get the length of the value stored in a key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.lettuce.core.SetArgs;
import io.lettuce.core.StrAlgoArgs;
import io.lettuce.core.StringMatchResult;
import io.lettuce.core.LcsArgs;
import io.lettuce.core.output.KeyValueStreamingChannel;

/**
Expand Down Expand Up @@ -425,6 +426,23 @@ public interface NodeSelectionStringCommands<K, V> {
*/
Executions<StringMatchResult> stralgoLcs(StrAlgoArgs strAlgoArgs);

/**
* The LCS command implements the longest common subsequence algorithm.
*
* <ul>
* <li>Without modifiers the string representing the longest common substring is returned.</li>
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
* </ul>
*
* @param lcsArgs command arguments.
* @return StringMatchResult.
* @since 6.6
*/
Executions<StringMatchResult> lcs(LcsArgs lcsArgs);

/**
* Get the length of the value stored in a key.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/lettuce/core/protocol/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum CommandType implements ProtocolKeyword {

// String

APPEND, GET, GETDEL, GETEX, GETRANGE, GETSET, MGET, MSET, MSETNX, SET, SETEX, PSETEX, SETNX, SETRANGE, STRLEN, STRALGO,
APPEND, GET, GETDEL, GETEX, GETRANGE, GETSET, MGET, MSET, MSETNX, SET, SETEX, PSETEX, SETNX, SETRANGE, STRLEN, STRALGO, LCS,

// Numeric

Expand Down
17 changes: 17 additions & 0 deletions src/main/templates/io/lettuce/core/api/RedisStringCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,23 @@ public interface RedisStringCommands<K, V> {
*/
StringMatchResult stralgoLcs(StrAlgoArgs strAlgoArgs);

/**
* The LCS command implements the longest common subsequence algorithm.
*
* <ul>
* <li>Without modifiers the string representing the longest common substring is returned.</li>
* <li>When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.</li>
* <li>When {@link LcsArgs#withIdx() IDX} is given the command returns an array with the LCS length and all the ranges in
* both the strings, start and end offset for each string, where there are matches. When {@link LcsArgs#withMatchLen()
* WITHMATCHLEN} is given each array representing a match will also have the length of the match.</li>
* </ul>
*
* @param lcsArgs command arguments.
* @return StringMatchResult.
* @since 6.6
*/
StringMatchResult lcs(LcsArgs lcsArgs);

/**
* Get the length of the value stored in a key.
*
Expand Down
Loading

0 comments on commit 63ee6b9

Please sign in to comment.