1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
use crate::{
client::{prepare_command, PreparedCommand},
commands::FlushingMode,
resp::{
cmd, deserialize_byte_buf, CommandArgs, PrimitiveResponse, SingleArg, SingleArgCollection,
ToArgs, Response,
},
};
use serde::Deserialize;
use std::collections::HashMap;
/// A group of Redis commands related to Scripting and Functions
/// # See Also
/// [Redis Scripting and Functions Commands](https://redis.io/commands/?group=scripting)
/// [Scripting with LUA](https://redis.io/docs/manual/programmability/eval-intro/)
/// [Functions](https://redis.io/docs/manual/programmability/functions-intro/)
pub trait ScriptingCommands<'a> {
/// Invoke the execution of a server-side Lua script.
///
/// # Return
/// The return value of the script
///
/// # See Also
/// [<https://redis.io/commands/eval/>](https://redis.io/commands/eval/)
#[must_use]
fn eval<R>(self, builder: CallBuilder) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
R: Response,
{
prepare_command(self, cmd("EVAL").arg(builder))
}
/// This is a read-only variant of the [eval](ScriptingCommands::eval)]
/// command that cannot execute commands that modify data.
///
/// # Return
/// The return value of the script
///
/// # See Also
/// [<https://redis.io/commands/eval_ro/>](https://redis.io/commands/eval_ro/)
#[must_use]
fn eval_readonly<R>(self, builder: CallBuilder) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
R: Response,
{
prepare_command(self, cmd("EVAL_RO").arg(builder))
}
/// Evaluate a script from the server's cache by its SHA1 digest.
///
/// # Return
/// The return value of the script
///
/// # See Also
/// [<https://redis.io/commands/eval/>](https://redis.io/commands/eval/)
#[must_use]
fn evalsha<R>(self, builder: CallBuilder) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
R: Response,
{
prepare_command(self, cmd("EVALSHA").arg(builder))
}
/// This is a read-only variant of the [evalsha](ScriptingCommands::evalsha)
/// command that cannot execute commands that modify data.
///
/// # Return
/// The return value of the script
///
/// # See Also
/// [<https://redis.io/commands/evalsha_ro/>](https://redis.io/commands/evalsha_ro/)
#[must_use]
fn evalsha_readonly<R>(self, builder: CallBuilder) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
R: Response,
{
prepare_command(self, cmd("EVALSHA_RO").arg(builder))
}
/// Invoke a function.
///
/// # Return
/// The return value of the function
///
/// # See Also
/// [<https://redis.io/commands/fcall/>](https://redis.io/commands/fcall/)
#[must_use]
fn fcall<R>(self, builder: CallBuilder) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
R: Response,
{
prepare_command(self, cmd("FCALL").arg(builder))
}
/// Invoke a function.
///
/// # Return
/// The return value of the function
///
/// # See Also
/// [<https://redis.io/commands/fcall-ro/>](https://redis.io/commands/fcall_ro/)
#[must_use]
fn fcall_readonly<R>(self, builder: CallBuilder) -> PreparedCommand<'a, Self, R>
where
Self: Sized,
R: Response,
{
prepare_command(self, cmd("FCALL_RO").arg(builder))
}
/// Delete a library and all its functions.
///
/// # See Also
/// [<https://redis.io/commands/function-delete/>](https://redis.io/commands/function-delete/)
#[must_use]
fn function_delete<L>(self, library_name: L) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
L: SingleArg,
{
prepare_command(self, cmd("FUNCTION").arg("DELETE").arg(library_name))
}
/// Return the serialized payload of loaded libraries.
/// You can restore the serialized payload later with the
/// [`function_restore`](ScriptingCommands::function_restore) command.
///
/// # Return
/// The serialized payload
///
/// # See Also
/// [<https://redis.io/commands/function-dump/>](https://redis.io/commands/function-dump/)
#[must_use]
fn function_dump(self) -> PreparedCommand<'a, Self, FunctionDumpResult>
where
Self: Sized,
{
prepare_command(self, cmd("FUNCTION").arg("DUMP"))
}
/// Deletes all the libraries.
///
/// # See Also
/// [<https://redis.io/commands/function-flush/>](https://redis.io/commands/function-flush/)
#[must_use]
fn function_flush(self, flushing_mode: FlushingMode) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("FUNCTION").arg("FLUSH").arg(flushing_mode))
}
/// Kill a function that is currently executing.
///
/// # See Also
/// [<https://redis.io/commands/function-kill/>](https://redis.io/commands/function-kill/)
#[must_use]
fn function_kill(self) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("FUNCTION").arg("KILL"))
}
/// Return information about the functions and libraries.
///
/// # See Also
/// [<https://redis.io/commands/function-list/>](https://redis.io/commands/function-list/)
#[must_use]
fn function_list(
self,
options: FunctionListOptions,
) -> PreparedCommand<'a, Self, Vec<LibraryInfo>>
where
Self: Sized,
{
prepare_command(self, cmd("FUNCTION").arg("LIST").arg(options))
}
/// Load a library to Redis.
///
/// # Return
/// The library name that was loaded
///
/// # See Also
/// [<https://redis.io/commands/function-load/>](https://redis.io/commands/function-load/)
#[must_use]
fn function_load<F, L>(self, replace: bool, function_code: F) -> PreparedCommand<'a, Self, L>
where
Self: Sized,
F: SingleArg,
L: PrimitiveResponse,
{
prepare_command(
self,
cmd("FUNCTION")
.arg("LOAD")
.arg_if(replace, "REPLACE")
.arg(function_code),
)
}
/// Restore libraries from the serialized payload.
///
/// # See Also
/// [<https://redis.io/commands/function-restore/>](https://redis.io/commands/function-restore/)
#[must_use]
fn function_restore<P>(
self,
serialized_payload: P,
policy: FunctionRestorePolicy,
) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
P: SingleArg,
{
prepare_command(
self,
cmd("FUNCTION")
.arg("RESTORE")
.arg(serialized_payload)
.arg(policy),
)
}
/// Return information about the function that's currently running and information about the available execution engines.
///
/// # See Also
/// [<https://redis.io/commands/function-stats/>](https://redis.io/commands/function-stats/)
#[must_use]
fn function_stats(self) -> PreparedCommand<'a, Self, FunctionStats>
where
Self: Sized,
{
prepare_command(self, cmd("FUNCTION").arg("STATS"))
}
/// Set the debug mode for subsequent scripts executed with EVAL.
///
/// # See Also
/// [<https://redis.io/commands/script-debug/>](https://redis.io/commands/script-debug/)
#[must_use]
fn script_debug(self, debug_mode: ScriptDebugMode) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("SCRIPT").arg("DEBUG").arg(debug_mode))
}
/// Returns information about the existence of the scripts in the script cache.
///
/// # Return
/// The SHA1 digest of the script added into the script cache.
///
/// # See Also
/// [<https://redis.io/commands/script-exists/>](https://redis.io/commands/script-exists/)
#[must_use]
fn script_exists<S, C>(self, sha1s: C) -> PreparedCommand<'a, Self, Vec<bool>>
where
Self: Sized,
S: SingleArg,
C: SingleArgCollection<S>,
{
prepare_command(self, cmd("SCRIPT").arg("EXISTS").arg(sha1s))
}
/// Flush the Lua scripts cache.
///
/// # See Also
/// [<https://redis.io/commands/script-flush/>](https://redis.io/commands/script-flush/)
#[must_use]
fn script_flush(self, flushing_mode: FlushingMode) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("SCRIPT").arg("FLUSH").arg(flushing_mode))
}
/// Kills the currently executing EVAL script,
/// assuming no write operation was yet performed by the script.
///
/// # See Also
/// [<https://redis.io/commands/script-kill/>](https://redis.io/commands/script-kill/)
#[must_use]
fn script_kill(self) -> PreparedCommand<'a, Self, ()>
where
Self: Sized,
{
prepare_command(self, cmd("SCRIPT").arg("KILL"))
}
/// Load a script into the scripts cache, without executing it.
///
/// # Return
/// The SHA1 digest of the script added into the script cache.
///
/// # See Also
/// [<https://redis.io/commands/script-load/>](https://redis.io/commands/script-load/)
#[must_use]
fn script_load<S, V>(self, script: S) -> PreparedCommand<'a, Self, V>
where
Self: Sized,
S: SingleArg,
V: PrimitiveResponse,
{
prepare_command(self, cmd("SCRIPT").arg("LOAD").arg(script))
}
}
/// Builder for calling a script/function for the following commands:
/// * [`eval`](ScriptingCommands::eval)
/// * [`eval_readonly`](ScriptingCommands::eval_readonly)
/// * [`evalsha`](ScriptingCommands::evalsha)
/// * [`evalsha_readonly`](ScriptingCommands::evalsha_readonly)
/// * [`fcall`](ScriptingCommands::fcall)
/// * [`fcall_readonly`](ScriptingCommands::fcall_readonly)
pub struct CallBuilder {
command_args: CommandArgs,
keys_added: bool,
}
impl CallBuilder {
/// Script name when used with [`eval`](ScriptingCommands::eval)
/// and [`eval_readonly`](ScriptingCommands::eval_readonly) commands
#[must_use]
pub fn script<S: SingleArg>(script: S) -> Self {
Self {
command_args: CommandArgs::default().arg(script).build(),
keys_added: false,
}
}
/// Sha1 haxadecimal string when used with [`eval`](ScriptingCommands::evalsha)
/// and [`evalsha_readonly`](ScriptingCommands::evalsha_readonly) commands
#[must_use]
pub fn sha1<S: SingleArg>(sha1: S) -> Self {
Self {
command_args: CommandArgs::default().arg(sha1).build(),
keys_added: false,
}
}
/// Sha1 haxadecimal string when used with [`fcall`](ScriptingCommands::fcall)
/// and [`fcall_readonly`](ScriptingCommands::fcall_readonly) commands
#[must_use]
pub fn function<F: SingleArg>(function: F) -> Self {
Self {
command_args: CommandArgs::default().arg(function).build(),
keys_added: false,
}
}
/// All the keys accessed by the script.
#[must_use]
pub fn keys<K, C>(mut self, keys: C) -> Self
where
K: SingleArg,
C: SingleArgCollection<K>,
{
Self {
command_args: self.command_args.arg(keys.num_args()).arg(keys).build(),
keys_added: true,
}
}
/// Additional input arguments that should not represent names of keys.
#[must_use]
pub fn args<A, C>(mut self, args: C) -> Self
where
A: SingleArg,
C: SingleArgCollection<A>,
{
let command_args = if self.keys_added {
self.command_args.arg(args).build()
} else {
// numkeys = 0
self.command_args.arg(0).arg(args).build()
};
Self {
command_args,
keys_added: true,
}
}
}
impl ToArgs for CallBuilder {
fn write_args(&self, args: &mut CommandArgs) {
// no keys, no args
if self.command_args.len() == 1 {
args.arg(&self.command_args).arg(0);
} else {
args.arg(&self.command_args);
}
}
}
/// Policy option for the [`function_restore`](ScriptingCommands::function_restore) command.
pub enum FunctionRestorePolicy {
/// Append
Default,
/// Appends the restored libraries to the existing libraries and aborts on collision.
/// This is the default policy.
Append,
/// Deletes all existing libraries before restoring the payload.
Flush,
/// appends the restored libraries to the existing libraries,
/// replacing any existing ones in case of name collisions.
/// Note that this policy doesn't prevent function name collisions, only libraries.
Replace,
}
impl Default for FunctionRestorePolicy {
fn default() -> Self {
Self::Default
}
}
impl ToArgs for FunctionRestorePolicy {
fn write_args(&self, args: &mut CommandArgs) {
match self {
FunctionRestorePolicy::Default => {
}
FunctionRestorePolicy::Append => {
args.arg("APPEND");
}
FunctionRestorePolicy::Flush => {
args.arg("FLUSH");
}
FunctionRestorePolicy::Replace => {
args.arg("REPLACE");
}
}
}
}
/// Result for the [`function_list`](ScriptingCommands::function_list) command.
#[derive(Debug, Deserialize)]
pub struct LibraryInfo {
/// the name of the library.
pub library_name: String,
/// the engine of the library.
pub engine: String,
/// the list of functions in the library.
pub functions: Vec<FunctionInfo>,
/// the library's source code (when given the
/// [`with_code`](FunctionListOptions::with_code) modifier).
pub library_code: Option<String>,
}
/// Sub-result for the [`function_list`](ScriptingCommands::function_list) command.
#[derive(Debug, Deserialize)]
pub struct FunctionInfo {
/// the name of the function.
pub name: String,
/// the function's description.
pub description: String,
/// an array of [function flags](https://redis.io/docs/manual/programmability/functions-intro/#function-flags).
pub flags: Vec<String>,
}
/// Result for the [`function_stats`](ScriptingCommands::function_stats) command.
#[derive(Debug, Deserialize)]
pub struct FunctionStats {
/// information about the running script. If there's no in-flight function, the server replies with `None`.
pub running_script: Option<RunningScript>,
/// Each entry in the map represent a single engine.
/// Engine map contains statistics about the engine like number of functions and number of libraries.
pub engines: HashMap<String, EngineStats>,
}
/// Sub-result for the [`function_stats`](ScriptingCommands::function_stats) command.
#[derive(Debug, Deserialize)]
pub struct RunningScript {
/// the name of the function.
pub name: String,
/// the command and arguments used for invoking the function.
pub command: Vec<String>,
/// the function's runtime duration in milliseconds.
pub duration_ms: u64,
}
/// sub-result for the [`function_stats`](ScriptingCommands::function_stats) command.
#[derive(Debug, Default, Deserialize)]
pub struct EngineStats {
/// Number of libraries of functions
pub libraries_count: usize,
/// Number of functions
pub functions_count: usize,
}
/// Options for the [`script_debug`](ScriptingCommands::script_debug) command.
pub enum ScriptDebugMode {
/// Enable non-blocking asynchronous debugging of Lua scripts (changes are discarded).
Yes,
/// Enable blocking synchronous debugging of Lua scripts (saves changes to data).
Sync,
/// Disables scripts debug mode.
No,
}
impl ToArgs for ScriptDebugMode {
fn write_args(&self, args: &mut CommandArgs) {
match self {
ScriptDebugMode::Yes => args.arg("YES"),
ScriptDebugMode::Sync => args.arg("SYNC"),
ScriptDebugMode::No => args.arg("NO"),
};
}
}
/// Options for the [`function_list`](ScriptingCommands::function_list) command
#[derive(Default)]
pub struct FunctionListOptions {
command_args: CommandArgs,
}
impl FunctionListOptions {
/// specifies a pattern for matching library names.
#[must_use]
pub fn library_name_pattern<P: SingleArg>(mut self, library_name_pattern: P) -> Self {
Self {
command_args: self
.command_args
.arg("LIBRARYNAME")
.arg(library_name_pattern)
.build(),
}
}
/// will cause the server to include the libraries source implementation in the reply.
#[must_use]
pub fn with_code(mut self) -> Self {
Self {
command_args: self.command_args.arg("WITHCODE").build(),
}
}
}
impl ToArgs for FunctionListOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}
/// Result for the [`function_dump`](ScriptingCommands::function_dump) command.
#[derive(Deserialize)]
pub struct FunctionDumpResult(#[serde(deserialize_with = "deserialize_byte_buf")] pub Vec<u8>);