forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java: add invokeScript to run lua scripts (valkey-io#1160)
* Java: add invokeScript to run lua scripts (#151) * Initial invokeScript() command Signed-off-by: Andrew Carbonetto <[email protected]> * Add lua script eval to Java Signed-off-by: Andrew Carbonetto <[email protected]> --------- Signed-off-by: Andrew Carbonetto <[email protected]> * Update javadoc comments Signed-off-by: Andrew Carbonetto <[email protected]> * Add finalize() to Script Signed-off-by: Andrew Carbonetto <[email protected]> * Make finalize protected Signed-off-by: Andrew Carbonetto <[email protected]> * Rename function; clean up javadoc Signed-off-by: Andrew Carbonetto <[email protected]> * Update examples in invokeScript Signed-off-by: Andrew Carbonetto <[email protected]> * Clean up test Signed-off-by: Andrew Carbonetto <[email protected]> --------- Signed-off-by: Andrew Carbonetto <[email protected]>
- Loading branch information
1 parent
4151b15
commit 8207e3a
Showing
11 changed files
with
324 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.models; | ||
|
||
import static glide.ffi.resolvers.ScriptResolver.dropScript; | ||
import static glide.ffi.resolvers.ScriptResolver.storeScript; | ||
|
||
import glide.api.commands.GenericBaseCommands; | ||
import lombok.Getter; | ||
|
||
/** | ||
* A wrapper for a Script object for {@link GenericBaseCommands#invokeScript(Script)} As long as | ||
* this object is not closed, the script's code is saved in memory, and can be resent to the server. | ||
* Script should be enclosed with a try-with-resource block or {@link Script#close()} must be called | ||
* to invalidate the code hash. | ||
*/ | ||
public class Script implements AutoCloseable { | ||
|
||
/** Hash string representing the code. */ | ||
@Getter private final String hash; | ||
|
||
/** | ||
* Wraps around creating a Script object from <code>code</code>. | ||
* | ||
* @param code To execute with a ScriptInvoke call. | ||
*/ | ||
public Script(String code) { | ||
hash = storeScript(code); | ||
} | ||
|
||
/** Drop the linked script from glide-rs <code>code</code>. */ | ||
@Override | ||
public void close() throws Exception { | ||
dropScript(hash); | ||
} | ||
|
||
@Override | ||
protected void finalize() throws Throwable { | ||
try { | ||
// Drop the linked script on garbage collection. | ||
this.close(); | ||
} finally { | ||
super.finalize(); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
java/client/src/main/java/glide/api/models/commands/ScriptOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.models.commands; | ||
|
||
import glide.api.commands.GenericBaseCommands; | ||
import glide.api.models.Script; | ||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Singular; | ||
|
||
/** | ||
* Optional arguments for {@link GenericBaseCommands#invokeScript(Script, ScriptOptions)} command. | ||
* | ||
* @see <a href="https://redis.io/commands/evalsha/">redis.io</a> | ||
*/ | ||
@Builder | ||
public final class ScriptOptions { | ||
|
||
/** The keys that are used in the script. */ | ||
@Singular @Getter private final List<String> keys; | ||
|
||
/** The arguments for the script. */ | ||
@Singular @Getter private final List<String> args; | ||
} |
25 changes: 25 additions & 0 deletions
25
java/client/src/main/java/glide/ffi/resolvers/ScriptResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.ffi.resolvers; | ||
|
||
public class ScriptResolver { | ||
|
||
// TODO: consider lazy loading the glide_rs library | ||
static { | ||
System.loadLibrary("glide_rs"); | ||
} | ||
|
||
/** | ||
* Loads a Lua script into the scripts cache, without executing it. | ||
* | ||
* @param code The Lua script | ||
* @return String representing the saved hash | ||
*/ | ||
public static native String storeScript(String code); | ||
|
||
/** | ||
* Unload or drop the stored Lua script from the script cache. | ||
* | ||
* @param hash | ||
*/ | ||
public static native void dropScript(String hash); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.