-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlists.rs
49 lines (41 loc) · 1.42 KB
/
lists.rs
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
use valkey_module::alloc::ValkeyAlloc;
use valkey_module::raw::KeyType;
use valkey_module::{
valkey_module, Context, NextArg, ValkeyError, ValkeyResult, ValkeyString, ValkeyValue,
};
// LPOPRPUSH source destination
// Pops and returns the first element (head) of the list stored at 'source'
// and pushes the element to the last position (tail) of the list stored at
// 'destination'.
fn lpoprpush(ctx: &Context, args: Vec<ValkeyString>) -> ValkeyResult {
let mut args = args.into_iter().skip(1);
let src = args.next_arg()?;
let dst = args.next_arg()?;
let src_key = ctx.open_key_writable(&src);
let dst_key = ctx.open_key_writable(&dst);
let src_type = src_key.key_type();
let dst_type = dst_key.key_type();
if (src_type != KeyType::Empty && src_type != KeyType::List)
|| (dst_type != KeyType::Empty && dst_type != KeyType::List)
{
return Err(ValkeyError::WrongType);
}
match src_key.list_pop_head() {
None => Ok(ValkeyValue::Null),
Some(value) => {
let ret_cpy = value.clone();
dst_key.list_push_tail(value);
Ok(ValkeyValue::BulkString(ret_cpy.into()))
}
}
}
//////////////////////////////////////////////////////
valkey_module! {
name: "lists",
version: 1,
allocator: (ValkeyAlloc, ValkeyAlloc),
data_types: [],
commands: [
["LPOPRPUSH", lpoprpush, "write fast deny-oom", 1, 2, 1],
],
}