Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Script::load function #603

Merged
merged 4 commits into from Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 22 additions & 32 deletions src/script.rs
Expand Up @@ -4,6 +4,7 @@ use sha1_smol::Sha1;
use crate::cmd::cmd;
use crate::connection::ConnectionLike;
use crate::types::{ErrorKind, FromRedisValue, RedisResult, ToRedisArgs};
use crate::Cmd;

/// Represents a lua script.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -124,23 +125,12 @@ impl<'a> ScriptInvocation<'a> {
/// Invokes the script and returns the result.
#[inline]
pub fn invoke<T: FromRedisValue>(&self, con: &mut dyn ConnectionLike) -> RedisResult<T> {
let mut eval_cmd = cmd("EVALSHA");
eval_cmd
.arg(self.script.hash.as_bytes())
.arg(self.keys.len())
.arg(&*self.keys)
.arg(&*self.args);

let eval_cmd = self.eval_cmd();
match eval_cmd.query(con) {
Ok(val) => Ok(val),
Err(err) => {
if err.kind() == ErrorKind::NoScriptError {
let mut load_cmd = cmd("SCRIPT");
load_cmd
.arg("LOAD")
.arg(self.script.code.as_bytes())
.query(con)?;

self.load_cmd().query(con)?;
eval_cmd.query(con)
} else {
Err(err)
Expand All @@ -157,13 +147,7 @@ impl<'a> ScriptInvocation<'a> {
C: crate::aio::ConnectionLike,
T: FromRedisValue,
{
let mut eval_cmd = cmd("EVALSHA");
eval_cmd
.arg(self.script.hash.as_bytes())
.arg(self.keys.len())
.arg(&*self.keys)
.arg(&*self.args);

let eval_cmd = self.eval_cmd();
match eval_cmd.query_async(con).await {
Ok(val) => {
// Return the value from the script evaluation
Expand All @@ -172,10 +156,7 @@ impl<'a> ScriptInvocation<'a> {
Err(err) => {
// Load the script into Redis if the script hash wasn't there already
if err.kind() == ErrorKind::NoScriptError {
let mut load_cmd = cmd("SCRIPT");
load_cmd.arg("LOAD").arg(self.script.code.as_bytes());

load_cmd.query_async(con).await?;
self.load_cmd().query_async(con).await?;
eval_cmd.query_async(con).await
} else {
Err(err)
Expand All @@ -187,10 +168,7 @@ impl<'a> ScriptInvocation<'a> {
/// Loads the script and returns the SHA1 of it.
#[inline]
pub fn load(&self, con: &mut dyn ConnectionLike) -> RedisResult<String> {
let hash: String = cmd("SCRIPT")
.arg("LOAD")
.arg(self.script.code.as_bytes())
.query(con)?;
let hash: String = self.load_cmd().query(con)?;

debug_assert_eq!(hash, self.script.hash);

Expand All @@ -204,13 +182,25 @@ impl<'a> ScriptInvocation<'a> {
where
C: crate::aio::ConnectionLike,
{
let hash: String = cmd("SCRIPT")
.arg("LOAD")
.arg(self.script.code.as_bytes())
.query_async(con).await?;
let hash: String = self.load_cmd().query_async(con).await?;

debug_assert_eq!(hash, self.script.hash);

Ok(hash)
}

fn load_cmd(&self) -> Cmd {
let mut cmd = cmd("SCRIPT");
djc marked this conversation as resolved.
Show resolved Hide resolved
cmd.arg("LOAD").arg(self.script.code.as_bytes());
cmd
}

fn eval_cmd(&self) -> Cmd {
let mut cmd = cmd("EVALSHA");
djc marked this conversation as resolved.
Show resolved Hide resolved
cmd.arg(self.script.hash.as_bytes())
.arg(self.keys.len())
.arg(&*self.keys)
.arg(&*self.args);
cmd
}
}
13 changes: 4 additions & 9 deletions tests/test_async.rs
Expand Up @@ -354,19 +354,14 @@ fn test_script() {
#[cfg(feature = "script")]
fn test_script_load() {
let ctx = TestContext::new();
let mut con = ctx.connection();

let script = redis::Script::new("return 'Hello World'");

block_on_all(async move {
let mut con = ctx.multiplexed_async_connection().await?;
let mut con = ctx.multiplexed_async_connection().await.unwrap();

let hash = script.prepare_invoke().load_async(&mut con);
assert_eq!(hash, Ok(script.get_hash().to_string()));

Ok(())
})
.unwrap();
let hash = script.prepare_invoke().load_async(&mut con).await.unwrap();
assert_eq!(hash, script.get_hash().to_string());
});
}

#[test]
Expand Down
11 changes: 4 additions & 7 deletions tests/test_async_async_std.rs
Expand Up @@ -295,14 +295,11 @@ fn test_script_load() {
let script = redis::Script::new("return 'Hello World'");

block_on_all(async move {
let mut con = ctx.multiplexed_async_connection_async_std().await?;

let hash = script.prepare_invoke().load_async(&mut con);
assert_eq!(hash, Ok(script.get_hash().to_string()));
let mut con = ctx.multiplexed_async_connection_async_std().await.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change these to unwrap()?

Copy link
Contributor Author

@zhiburt zhiburt May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix the build :) (I may mistaken but it was necessary to specify a Result::Err type)

Do you think it matters?


Ok(())
})
.unwrap();
let hash = script.prepare_invoke().load_async(&mut con).await.unwrap();
assert_eq!(hash, script.get_hash().to_string());
});
}

#[test]
Expand Down