diff --git a/src/main/java/io/lettuce/core/LcsArgs.java b/src/main/java/io/lettuce/core/LcsArgs.java index 7ccba1de05..a1b671ad13 100644 --- a/src/main/java/io/lettuce/core/LcsArgs.java +++ b/src/main/java/io/lettuce/core/LcsArgs.java @@ -1,22 +1,14 @@ /* - * Copyright 2011-2024 the original author or authors. + * Copyright 2011-Present, Redis Ltd. and Contributors + * All rights reserved. * - * 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. + * Licensed under the MIT License. */ package io.lettuce.core; import io.lettuce.core.internal.LettuceAssert; import io.lettuce.core.protocol.CommandArgs; +import io.lettuce.core.protocol.CommandKeyword; /** * Argument list builder for the Redis LCS command. Static import the methods from @@ -26,6 +18,7 @@ * * @author Seonghwan Lee * @since 6.6 + * @see LCS command refference */ public class LcsArgs implements CompositeArgument { @@ -92,7 +85,7 @@ public LcsArgs withMatchLen() { } /** - * Request match position in each strings for results. + * Request match position in each string for results. * * @return {@code this} {@link LcsArgs}. */ @@ -118,19 +111,19 @@ public void build(CommandArgs args) { args.add(key); } if (justLen) { - args.add("LEN"); + args.add(CommandKeyword.LEN); } if (withIdx) { - args.add("IDX"); + args.add(CommandKeyword.IDX); } if (minMatchLen > 0) { - args.add("MINMATCHLEN"); + args.add(CommandKeyword.MINMATCHLEN); args.add(minMatchLen); } if (withMatchLen) { - args.add("WITHMATCHLEN"); + args.add(CommandKeyword.WITHMATCHLEN); } } diff --git a/src/main/java/io/lettuce/core/RedisCommandBuilder.java b/src/main/java/io/lettuce/core/RedisCommandBuilder.java index 8a9dc3da1b..aa1ce4435b 100644 --- a/src/main/java/io/lettuce/core/RedisCommandBuilder.java +++ b/src/main/java/io/lettuce/core/RedisCommandBuilder.java @@ -58,6 +58,7 @@ * @author Mikhael Sokolov * @author Tihomir Mateev * @author Ali Takavci + * @author Seonghwan Lee */ @SuppressWarnings({ "unchecked", "varargs" }) class RedisCommandBuilder extends BaseRedisCommandBuilder { diff --git a/src/main/java/io/lettuce/core/StrAlgoArgs.java b/src/main/java/io/lettuce/core/StrAlgoArgs.java index 2df6daccac..2aed2c9bad 100644 --- a/src/main/java/io/lettuce/core/StrAlgoArgs.java +++ b/src/main/java/io/lettuce/core/StrAlgoArgs.java @@ -32,8 +32,11 @@ * {@link StrAlgoArgs} is a mutable object and instances should be used only once to avoid shared mutable state. * * @author dengliming + * @deprecated As of 6.6 in favor of {@link LcsArgs} + * @see LCS command refference * @since 6.0 */ +@Deprecated public class StrAlgoArgs implements CompositeArgument { private boolean justLen; diff --git a/src/main/java/io/lettuce/core/api/async/RedisStringAsyncCommands.java b/src/main/java/io/lettuce/core/api/async/RedisStringAsyncCommands.java index f94e44bec3..7b524c0925 100644 --- a/src/main/java/io/lettuce/core/api/async/RedisStringAsyncCommands.java +++ b/src/main/java/io/lettuce/core/api/async/RedisStringAsyncCommands.java @@ -411,6 +411,8 @@ public interface RedisStringAsyncCommands { /** * The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest * common substring). + *

+ * Command is no longer available in Redis server versions 7.0.x and later. * *

    *
  • Without modifiers the string representing the longest common substring is returned.
  • @@ -423,6 +425,7 @@ public interface RedisStringAsyncCommands { * * @param strAlgoArgs command arguments. * @return StringMatchResult. + * @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}. * @since 6.0 */ @Deprecated @@ -432,15 +435,16 @@ public interface RedisStringAsyncCommands { * The LCS command implements the longest common subsequence algorithm. * *
      - *
    • Without modifiers the string representing the longest common substring is returned.
    • + *
    • Without modifiers, the string representing the longest common substring is returned.
    • *
    • When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.
    • *
    • 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.
    • *
    * - * @param lcsArgs command arguments. - * @return StringMatchResult. + * @param lcsArgs command arguments supplied by the {@link LcsArgs}. + * @return StringMatchResult + * @see LCS command refference * @since 6.6 */ RedisFuture lcs(LcsArgs lcsArgs); diff --git a/src/main/java/io/lettuce/core/api/reactive/RedisStringReactiveCommands.java b/src/main/java/io/lettuce/core/api/reactive/RedisStringReactiveCommands.java index c091bdf02e..e7722b4394 100644 --- a/src/main/java/io/lettuce/core/api/reactive/RedisStringReactiveCommands.java +++ b/src/main/java/io/lettuce/core/api/reactive/RedisStringReactiveCommands.java @@ -415,6 +415,8 @@ public interface RedisStringReactiveCommands { /** * The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest * common substring). + *

    + * Command is no longer available in Redis server versions 7.0.x and later. * *

      *
    • Without modifiers the string representing the longest common substring is returned.
    • @@ -427,6 +429,7 @@ public interface RedisStringReactiveCommands { * * @param strAlgoArgs command arguments. * @return StringMatchResult. + * @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}. * @since 6.0 */ @Deprecated @@ -436,15 +439,16 @@ public interface RedisStringReactiveCommands { * The LCS command implements the longest common subsequence algorithm. * *
        - *
      • Without modifiers the string representing the longest common substring is returned.
      • + *
      • Without modifiers, the string representing the longest common substring is returned.
      • *
      • When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.
      • *
      • 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.
      • *
      * - * @param lcsArgs command arguments. - * @return StringMatchResult. + * @param lcsArgs command arguments supplied by the {@link LcsArgs}. + * @return StringMatchResult + * @see LCS command refference * @since 6.6 */ Mono lcs(LcsArgs lcsArgs); diff --git a/src/main/java/io/lettuce/core/api/sync/RedisStringCommands.java b/src/main/java/io/lettuce/core/api/sync/RedisStringCommands.java index 227f379b06..df13a42aef 100644 --- a/src/main/java/io/lettuce/core/api/sync/RedisStringCommands.java +++ b/src/main/java/io/lettuce/core/api/sync/RedisStringCommands.java @@ -410,6 +410,8 @@ public interface RedisStringCommands { /** * The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest * common substring). + *

      + * Command is no longer available in Redis server versions 7.0.x and later. * *

        *
      • Without modifiers the string representing the longest common substring is returned.
      • @@ -422,6 +424,7 @@ public interface RedisStringCommands { * * @param strAlgoArgs command arguments. * @return StringMatchResult. + * @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}. * @since 6.0 */ @Deprecated @@ -431,15 +434,16 @@ public interface RedisStringCommands { * The LCS command implements the longest common subsequence algorithm. * *
          - *
        • Without modifiers the string representing the longest common substring is returned.
        • + *
        • Without modifiers, the string representing the longest common substring is returned.
        • *
        • When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.
        • *
        • 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.
        • *
        * - * @param lcsArgs command arguments. - * @return StringMatchResult. + * @param lcsArgs command arguments supplied by the {@link LcsArgs}. + * @return StringMatchResult + * @see LCS command refference * @since 6.6 */ StringMatchResult lcs(LcsArgs lcsArgs); diff --git a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionStringAsyncCommands.java b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionStringAsyncCommands.java index 2265b6cd5b..93e1701b1f 100644 --- a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionStringAsyncCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionStringAsyncCommands.java @@ -410,6 +410,8 @@ public interface NodeSelectionStringAsyncCommands { /** * The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest * common substring). + *

        + * Command is no longer available in Redis server versions 7.0.x and later. * *

          *
        • Without modifiers the string representing the longest common substring is returned.
        • @@ -422,23 +424,26 @@ public interface NodeSelectionStringAsyncCommands { * * @param strAlgoArgs command arguments. * @return StringMatchResult. + * @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}. * @since 6.0 */ + @Deprecated AsyncExecutions stralgoLcs(StrAlgoArgs strAlgoArgs); /** * The LCS command implements the longest common subsequence algorithm. * *
            - *
          • Without modifiers the string representing the longest common substring is returned.
          • + *
          • Without modifiers, the string representing the longest common substring is returned.
          • *
          • When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.
          • *
          • 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.
          • *
          * - * @param lcsArgs command arguments. - * @return StringMatchResult. + * @param lcsArgs command arguments supplied by the {@link LcsArgs}. + * @return StringMatchResult + * @see LCS command refference * @since 6.6 */ AsyncExecutions lcs(LcsArgs lcsArgs); diff --git a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionStringCommands.java b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionStringCommands.java index e4a07b109f..dcf3f27acc 100644 --- a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionStringCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionStringCommands.java @@ -410,6 +410,8 @@ public interface NodeSelectionStringCommands { /** * The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest * common substring). + *

          + * Command is no longer available in Redis server versions 7.0.x and later. * *

            *
          • Without modifiers the string representing the longest common substring is returned.
          • @@ -422,23 +424,26 @@ public interface NodeSelectionStringCommands { * * @param strAlgoArgs command arguments. * @return StringMatchResult. + * @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}. * @since 6.0 */ + @Deprecated Executions stralgoLcs(StrAlgoArgs strAlgoArgs); /** * The LCS command implements the longest common subsequence algorithm. * *
              - *
            • Without modifiers the string representing the longest common substring is returned.
            • + *
            • Without modifiers, the string representing the longest common substring is returned.
            • *
            • When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.
            • *
            • 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.
            • *
            * - * @param lcsArgs command arguments. - * @return StringMatchResult. + * @param lcsArgs command arguments supplied by the {@link LcsArgs}. + * @return StringMatchResult + * @see LCS command refference * @since 6.6 */ Executions lcs(LcsArgs lcsArgs); diff --git a/src/main/java/io/lettuce/core/output/StringMatchResultOutput.java b/src/main/java/io/lettuce/core/output/StringMatchResultOutput.java index 2217217139..62c6f35aaa 100644 --- a/src/main/java/io/lettuce/core/output/StringMatchResultOutput.java +++ b/src/main/java/io/lettuce/core/output/StringMatchResultOutput.java @@ -21,6 +21,7 @@ import io.lettuce.core.StringMatchResult; import io.lettuce.core.codec.RedisCodec; +import io.lettuce.core.protocol.CommandKeyword; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -38,7 +39,7 @@ */ public class StringMatchResultOutput extends CommandOutput { - private static final ByteBuffer LEN = StandardCharsets.US_ASCII.encode("len"); + private static final ByteBuffer LEN = StandardCharsets.US_ASCII.encode(CommandKeyword.LEN.toString().toLowerCase()); private String matchString; diff --git a/src/main/java/io/lettuce/core/protocol/CommandKeyword.java b/src/main/java/io/lettuce/core/protocol/CommandKeyword.java index 8cd1f0f9d3..597816edfe 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandKeyword.java +++ b/src/main/java/io/lettuce/core/protocol/CommandKeyword.java @@ -37,11 +37,11 @@ public enum CommandKeyword implements ProtocolKeyword { BY, BYLEX, BYSCORE, CACHING, CAT, CH, CHANNELS, COPY, COUNT, COUNTKEYSINSLOT, CONSUMERS, CREATE, DB, DELSLOTS, DELSLOTSRANGE, DELUSER, DESC, DRYRUN, SOFT, HARD, ENCODING, - FAILOVER, FORGET, FIELDS, FLAGS, FLUSH, FORCE, FREQ, FLUSHSLOTS, GENPASS, GETNAME, GETUSER, GETKEYSINSLOT, GETREDIR, GROUP, GROUPS, HTSTATS, ID, IDLE, INFO, + FAILOVER, FORGET, FIELDS, FLAGS, FLUSH, FORCE, FREQ, FLUSHSLOTS, GENPASS, GETNAME, GETUSER, GETKEYSINSLOT, GETREDIR, GROUP, GROUPS, HTSTATS, ID, IDLE, IDX, INFO, IDLETIME, JUSTID, KILL, KEYSLOT, LEFT, LEN, LIMIT, LINKS, LIST, LOAD, LOG, MATCH, - MAX, MAXLEN, MEET, MIN, MINID, MOVED, NO, NOACK, NOCOMMANDS, NODE, NODES, NOMKSTREAM, NOPASS, NOSAVE, NOT, NOVALUES, NUMSUB, SHARDCHANNELS, SHARDNUMSUB, NUMPAT, NX, OFF, ON, ONE, OR, PAUSE, PREFIXES, + MAX, MAXLEN, MEET, MIN, MINID, MINMATCHLEN, MOVED, NO, NOACK, NOCOMMANDS, NODE, NODES, NOMKSTREAM, NOPASS, NOSAVE, NOT, NOVALUES, NUMSUB, SHARDCHANNELS, SHARDNUMSUB, NUMPAT, NX, OFF, ON, ONE, OR, PAUSE, PREFIXES, REFCOUNT, REMOVE, RELOAD, REPLACE, REDIRECT, REPLICATE, REPLICAS, REV, RESET, RESETCHANNELS, RESETKEYS, RESETPASS, @@ -49,7 +49,7 @@ public enum CommandKeyword implements ProtocolKeyword { MIGRATING, IMPORTING, SAVE, SKIPME, SLAVES, STREAM, STORE, SUM, SEGFAULT, SETUSER, TAKEOVER, TRACKING, TRACKINGINFO, TYPE, UNBLOCK, USERS, USAGE, WEIGHTS, WHOAMI, - WITHSCORE, WITHSCORES, WITHVALUES, XOR, XX, YES, INDENT, NEWLINE, SPACE; + WITHMATCHLEN, WITHSCORE, WITHSCORES, WITHVALUES, XOR, XX, YES, INDENT, NEWLINE, SPACE; public final byte[] bytes; diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisStringCoroutinesCommands.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisStringCoroutinesCommands.kt index 72df1f67fb..f6500da836 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisStringCoroutinesCommands.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisStringCoroutinesCommands.kt @@ -1,5 +1,5 @@ /* - * Copyright 2020-Present, Redis Ltd. and Contributors + * Copyright 2017-Present, Redis Ltd. and Contributors * All rights reserved. * * Licensed under the MIT License. @@ -392,23 +392,22 @@ interface RedisStringCoroutinesCommands { suspend fun setrange(key: K, offset: Long, value: V): Long? /** - * The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest - * common substring). + * The LCS command implements the longest common subsequence algorithm. * *
              - *
            • Without modifiers the string representing the longest common substring is returned.
            • - *
            • When [StrAlgoArgs#justLen LEN] is given the command returns the length of the longest common substring.
            • - *
            • When [StrAlgoArgs#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 - * [StrAlgoArgs#withMatchLen WITHMATCHLEN] is given each array representing a match will also have the length of the - * match.
            • + *
            • Without modifiers, the string representing the longest common substring is returned.
            • + *
            • When [LcsArgs#justLen LEN] is given the command returns the length of the longest common substring.
            • + *
            • When [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 [LcsArgs#withMatchLen + * WITHMATCHLEN] is given each array representing a match will also have the length of the match.
            • *
            * - * @param strAlgoArgs command arguments. - * @return StringMatchResult. - * @since 6.0 + * @param lcsArgs command arguments supplied by the [LcsArgs]. + * @return StringMatchResult + * @see LCS command refference + * @since 6.6 */ - suspend fun stralgoLcs(strAlgoArgs: StrAlgoArgs): StringMatchResult? + suspend fun lcs(lcsArgs: LcsArgs): StringMatchResult? /** * Get the length of the value stored in a key. diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisStringCoroutinesCommandsImpl.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisStringCoroutinesCommandsImpl.kt index 86586c6899..8bfbb56d4d 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisStringCoroutinesCommandsImpl.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisStringCoroutinesCommandsImpl.kt @@ -116,7 +116,7 @@ internal class RedisStringCoroutinesCommandsImpl(internal val override suspend fun setrange(key: K, offset: Long, value: V): Long? = ops.setrange(key, offset, value).awaitFirstOrNull() - override suspend fun stralgoLcs(strAlgoArgs: StrAlgoArgs): StringMatchResult? = ops.stralgoLcs(strAlgoArgs).awaitFirstOrNull() + override suspend fun lcs(lcsArgs: LcsArgs): StringMatchResult? = ops.lcs(lcsArgs).awaitFirstOrNull() override suspend fun strlen(key: K): Long? = ops.strlen(key).awaitFirstOrNull() diff --git a/src/main/templates/io/lettuce/core/api/RedisStringCommands.java b/src/main/templates/io/lettuce/core/api/RedisStringCommands.java index 0a043bcccb..02a8a2562a 100644 --- a/src/main/templates/io/lettuce/core/api/RedisStringCommands.java +++ b/src/main/templates/io/lettuce/core/api/RedisStringCommands.java @@ -403,6 +403,8 @@ public interface RedisStringCommands { /** * The STRALGO command implements complex algorithms that operate on strings. This method uses the LCS algorithm (longest * common substring). + *

            + * Command is no longer available in Redis server versions 7.0.x and later. * *

              *
            • Without modifiers the string representing the longest common substring is returned.
            • @@ -415,23 +417,26 @@ public interface RedisStringCommands { * * @param strAlgoArgs command arguments. * @return StringMatchResult. + * @deprecated since 6.6 in favor of {@link #lcs(LcsArgs)}. * @since 6.0 */ + @Deprecated StringMatchResult stralgoLcs(StrAlgoArgs strAlgoArgs); /** * The LCS command implements the longest common subsequence algorithm. * *
                - *
              • Without modifiers the string representing the longest common substring is returned.
              • + *
              • Without modifiers, the string representing the longest common substring is returned.
              • *
              • When {@link LcsArgs#justLen() LEN} is given the command returns the length of the longest common substring.
              • *
              • 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.
              • *
              * - * @param lcsArgs command arguments. - * @return StringMatchResult. + * @param lcsArgs command arguments supplied by the {@link LcsArgs}. + * @return StringMatchResult + * @see LCS command refference * @since 6.6 */ StringMatchResult lcs(LcsArgs lcsArgs); diff --git a/src/test/java/io/lettuce/core/commands/StringCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/StringCommandIntegrationTests.java index 0382a0d427..a90c3c27ad 100644 --- a/src/test/java/io/lettuce/core/commands/StringCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/StringCommandIntegrationTests.java @@ -68,6 +68,10 @@ public class StringCommandIntegrationTests extends TestSupport { private final RedisCommands redis; + protected final String KEY_1 = "key1{k}"; + + protected final String KEY_2 = "key2{k}"; + @Inject protected StringCommandIntegrationTests(RedisCommands redis) { this.redis = redis; @@ -328,10 +332,10 @@ void strAlgo() { @EnabledOnCommand("STRALGO") void strAlgoUsingKeys() { - redis.set("key1{k}", "ohmytext"); - redis.set("key2{k}", "mynewtext"); + redis.set(KEY_1, "ohmytext"); + redis.set(KEY_2, "mynewtext"); - StringMatchResult matchResult = redis.stralgoLcs(StrAlgoArgs.Builder.keys("key1{k}", "key2{k}")); + StringMatchResult matchResult = redis.stralgoLcs(StrAlgoArgs.Builder.keys(KEY_1, KEY_2)); assertThat(matchResult.getMatchString()).isEqualTo("mytext"); // STRALGO LCS STRINGS a b @@ -382,68 +386,105 @@ void strAlgoWithIdx() { @Test @EnabledOnCommand("LCS") void lcs() { - redis.set("key1{k}", "ohmytext"); - redis.set("key2{k}", "mynewtext"); - - // LCS key1{k} key2{k} IDX MINMATCHLEN 4 WITHMATCHLEN - StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys("key1{k}", "key2{k}")); + redis.set(KEY_1, "ohmytext"); + redis.set(KEY_2, "mynewtext"); + // > LCS key1 key2 + StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys(KEY_1, KEY_2)); assertThat(matchResult.getMatchString()).isEqualTo("mytext"); + assertThat(matchResult.getMatches().size()).isEqualTo(0); + assertThat(matchResult.getLen()).isEqualTo(0); + } @Test @EnabledOnCommand("LCS") - void lcsUsingKeys() { - redis.set("key1{k}", "ohmytext"); - redis.set("key2{k}", "mynewtext"); + void lcsNonExistantKeys() { - StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys("key1{k}", "key2{k}")); - - assertThat(matchResult.getMatchString()).isEqualTo("mytext"); + // > LCS a b IDX MINMATCHLEN 4 WITHMATCHLEN + // Keys don't exist. + StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys("a{k}", "b{k}").minMatchLen(4).withMatchLen()); + assertThat(matchResult.getMatchString()).isNullOrEmpty(); + assertThat(matchResult.getMatches()).isNullOrEmpty(); + assertThat(matchResult.getLen()).isEqualTo(0); } @Test @EnabledOnCommand("LCS") void lcsJustLen() { - redis.set("one{k}", "ohmytext"); - redis.set("two{k}", "mynewtext"); + redis.set(KEY_1, "ohmytext"); + redis.set(KEY_2, "mynewtext"); - StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys("one{k}", "two{k}").justLen()); + // > LCS key1 key2 LEN + StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys(KEY_1, KEY_2).justLen()); + assertThat(matchResult.getLen()).isEqualTo(6); + assertThat(matchResult.getMatchString()).isNullOrEmpty(); + assertThat(matchResult.getMatches()).isNullOrEmpty(); + } + + @Test + @EnabledOnCommand("LCS") + void lcsIdx() { + redis.set(KEY_1, "ohmytext"); + redis.set(KEY_2, "mynewtext"); + + // > LCS key1 key2 IDX + StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys(KEY_1, KEY_2).withIdx()); + + assertThat(matchResult.getMatches().size()).isEqualTo(2); + assertThat(matchResult.getMatches().get(0).getA().getStart()).isEqualTo(4); + assertThat(matchResult.getMatches().get(0).getA().getEnd()).isEqualTo(7); + assertThat(matchResult.getMatches().get(0).getB().getStart()).isEqualTo(5); + assertThat(matchResult.getMatches().get(0).getB().getEnd()).isEqualTo(8); + + assertThat(matchResult.getMatches().get(1).getA().getStart()).isEqualTo(2); + assertThat(matchResult.getMatches().get(1).getA().getEnd()).isEqualTo(3); + assertThat(matchResult.getMatches().get(1).getB().getStart()).isEqualTo(0); + assertThat(matchResult.getMatches().get(1).getB().getEnd()).isEqualTo(1); assertThat(matchResult.getLen()).isEqualTo(6); + + assertThat(matchResult.getMatchString()).isNullOrEmpty(); } @Test @EnabledOnCommand("LCS") void lcsWithMinMatchLen() { - redis.set("key1{k}", "ohmytext"); - redis.set("key2{k}", "mynewtext"); + redis.set(KEY_1, "ohmytext"); + redis.set(KEY_2, "mynewtext"); - StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys("key1{k}", "key2{k}").minMatchLen(4)); + // > LCS key1 key2 IDX MINMATCHLEN 4 + StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys(KEY_1, KEY_2).withIdx().minMatchLen(4)); - assertThat(matchResult.getMatchString()).isEqualTo("mytext"); + assertThat(matchResult.getMatches().get(0).getA().getStart()).isEqualTo(4); + assertThat(matchResult.getMatches().get(0).getA().getEnd()).isEqualTo(7); + assertThat(matchResult.getMatches().get(0).getB().getStart()).isEqualTo(5); + assertThat(matchResult.getMatches().get(0).getB().getEnd()).isEqualTo(8); + + assertThat(matchResult.getLen()).isEqualTo(6); + + assertThat(matchResult.getMatchString()).isNullOrEmpty(); } @Test @EnabledOnCommand("LCS") void lcsMinMatchLenIdxMatchLen() { - redis.set("key1{k}", "ohmytext"); - redis.set("key2{k}", "mynewtext"); + redis.set(KEY_1, "ohmytext"); + redis.set(KEY_2, "mynewtext"); - StringMatchResult matchResult = redis - .lcs(LcsArgs.Builder.keys("key1{k}", "key2{k}").minMatchLen(4).withMatchLen().withIdx()); + // > LCS key1 key2 IDX MINMATCHLEN 4 WITHMATCHLEN + StringMatchResult matchResult = redis.lcs(LcsArgs.Builder.keys(KEY_1, KEY_2).minMatchLen(4).withMatchLen().withIdx()); - assertThat(matchResult.getMatches()).hasSize(1); - assertThat(matchResult.getMatches().get(0).getMatchLen()).isEqualTo(4); + assertThat(matchResult.getMatches().get(0).getA().getStart()).isEqualTo(4); + assertThat(matchResult.getMatches().get(0).getA().getEnd()).isEqualTo(7); + assertThat(matchResult.getMatches().get(0).getB().getStart()).isEqualTo(5); + assertThat(matchResult.getMatches().get(0).getB().getEnd()).isEqualTo(8); - Position a = matchResult.getMatches().get(0).getA(); - Position b = matchResult.getMatches().get(0).getB(); + assertThat(matchResult.getMatches().get(0).getMatchLen()).isEqualTo(4); - assertThat(a.getStart()).isEqualTo(4); - assertThat(a.getEnd()).isEqualTo(7); - assertThat(b.getStart()).isEqualTo(5); - assertThat(b.getEnd()).isEqualTo(8); assertThat(matchResult.getLen()).isEqualTo(6); + + assertThat(matchResult.getMatchString()).isNullOrEmpty(); } }