From cb9bc8e97eb282c28c62246b8035ed998abd71e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20Mej=C3=ADa=20Su=C3=A1rez?= Date: Fri, 31 May 2024 03:27:25 -0500 Subject: [PATCH] Add a `evalReadOnly` overload that accepts the script as a `String` (#2868) * Add a evalReadOnly overload that accepts the script as a String * Fix @since annotation for 7.0 --- .../core/AbstractRedisAsyncCommands.java | 6 ++++++ .../core/AbstractRedisReactiveCommands.java | 6 ++++++ .../async/RedisScriptingAsyncCommands.java | 13 +++++++++++++ .../RedisScriptingReactiveCommands.java | 13 +++++++++++++ .../core/api/sync/RedisScriptingCommands.java | 13 +++++++++++++ .../NodeSelectionScriptingAsyncCommands.java | 13 +++++++++++++ .../sync/NodeSelectionScriptingCommands.java | 13 +++++++++++++ .../RedisScriptingCoroutinesCommands.kt | 19 ++++++++++++++++++- .../RedisScriptingCoroutinesCommandsImpl.kt | 8 +++++++- .../core/api/RedisScriptingCommands.java | 13 +++++++++++++ .../ScriptingCommandIntegrationTests.java | 2 +- 11 files changed, 116 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java b/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java index 4749ad984f..b7da8fb2fd 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java +++ b/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java @@ -741,6 +741,12 @@ public RedisFuture eval(byte[] script, ScriptOutputType type, K[] keys, V return (RedisFuture) dispatch(commandBuilder.eval(script, type, keys, values)); } + @Override + @SuppressWarnings("unchecked") + public RedisFuture evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values) { + return evalReadOnly(encodeScript(script), type, keys, values); + } + @Override public RedisFuture evalReadOnly(byte[] script, ScriptOutputType type, K[] keys, V... values) { return (RedisFuture) dispatch(commandBuilder.eval(script, type, true, keys, values)); diff --git a/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java b/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java index fa5630ed71..38404c1ea4 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java +++ b/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java @@ -797,6 +797,12 @@ public Flux eval(byte[] script, ScriptOutputType type, K[] keys, V... val return createFlux(() -> commandBuilder.eval(script, type, keys, values)); } + @Override + @SuppressWarnings("unchecked") + public Flux evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values) { + return evalReadOnly(encodeScript(script), type, keys, values); + } + @Override public Flux evalReadOnly(byte[] script, ScriptOutputType type, K[] keys, V... values) { return createFlux(() -> commandBuilder.eval(script, type, true, keys, values)); diff --git a/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java b/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java index 1367b489f2..0158e92f78 100644 --- a/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java +++ b/src/main/java/io/lettuce/core/api/async/RedisScriptingAsyncCommands.java @@ -85,6 +85,19 @@ public interface RedisScriptingAsyncCommands { */ RedisFuture eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 7.0 + */ + RedisFuture evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java b/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java index 0272f0cda0..1d9d7917c6 100644 --- a/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java +++ b/src/main/java/io/lettuce/core/api/reactive/RedisScriptingReactiveCommands.java @@ -84,6 +84,19 @@ public interface RedisScriptingReactiveCommands { */ Flux eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 7.0 + */ + Flux evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java b/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java index cc359cb42a..d6161251e5 100644 --- a/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java +++ b/src/main/java/io/lettuce/core/api/sync/RedisScriptingCommands.java @@ -84,6 +84,19 @@ public interface RedisScriptingCommands { */ T eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type output type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 7.0 + */ + T evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java index e116ce20b4..df0b551786 100644 --- a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionScriptingAsyncCommands.java @@ -84,6 +84,19 @@ public interface NodeSelectionScriptingAsyncCommands { */ AsyncExecutions eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 7.0 + */ + AsyncExecutions evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java index e1c2306626..b7b40351b2 100644 --- a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionScriptingCommands.java @@ -84,6 +84,19 @@ public interface NodeSelectionScriptingCommands { */ Executions eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 7.0 + */ + Executions evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt index 020ac1a873..711e7569b8 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommands.kt @@ -85,6 +85,24 @@ interface RedisScriptingCoroutinesCommands { */ suspend fun eval(script: ByteArray, type: ScriptOutputType, keys: Array, vararg values: V): T? + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 7.0 + */ + suspend fun evalReadOnly( + script: String, + type: ScriptOutputType, + keys: Array, + vararg values: V + ): T? + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * @@ -214,4 +232,3 @@ interface RedisScriptingCoroutinesCommands { suspend fun digest(script: ByteArray): String? } - diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommandsImpl.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommandsImpl.kt index 403cb26164..30d0625b4e 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommandsImpl.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisScriptingCoroutinesCommandsImpl.kt @@ -51,6 +51,13 @@ internal class RedisScriptingCoroutinesCommandsImpl(internal v override suspend fun eval(script: ByteArray, type: ScriptOutputType, keys: Array, vararg values: V): T? = ops.eval(script, type, keys, *values).awaitFirstOrNull() + override suspend fun evalReadOnly( + script: String, + type: ScriptOutputType, + keys: Array, + vararg values: V + ): T? = ops.evalReadOnly(script, type, keys, *values).awaitFirstOrNull() + override suspend fun evalReadOnly( script: ByteArray, type: ScriptOutputType, @@ -86,4 +93,3 @@ internal class RedisScriptingCoroutinesCommandsImpl(internal v override suspend fun digest(script: ByteArray): String = ops.digest(script) } - diff --git a/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java b/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java index ed47b21ca7..96e09c3940 100644 --- a/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java +++ b/src/main/templates/io/lettuce/core/api/RedisScriptingCommands.java @@ -83,6 +83,19 @@ public interface RedisScriptingCommands { */ T eval(byte[] script, ScriptOutputType type, K[] keys, V... values); + /** + * This is a read-only variant of the EVAL command that cannot execute commands that modify data. + * + * @param script Lua 5.1 script. + * @param type the type. + * @param keys the keys. + * @param values the values. + * @param expected return type. + * @return script result. + * @since 7.0 + */ + T evalReadOnly(String script, ScriptOutputType type, K[] keys, V... values); + /** * This is a read-only variant of the EVAL command that cannot execute commands that modify data. * diff --git a/src/test/java/io/lettuce/core/commands/ScriptingCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/ScriptingCommandIntegrationTests.java index 96cd29db3f..1c7afd30e2 100644 --- a/src/test/java/io/lettuce/core/commands/ScriptingCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/ScriptingCommandIntegrationTests.java @@ -134,7 +134,7 @@ void evalWithArgs() { @EnabledOnCommand("EVAL_RO") // Redis 7.0 void evalReadOnly() { String[] keys = new String[] { "key1" }; - assertThat((String) redis.evalReadOnly("return KEYS[1]".getBytes(), STATUS, keys, "a")).isEqualTo("key1"); + assertThat((String) redis.evalReadOnly("return KEYS[1]", STATUS, keys, "a")).isEqualTo("key1"); } @Test