-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathkeys_pos.rs
34 lines (28 loc) · 923 Bytes
/
keys_pos.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
use valkey_module::alloc::ValkeyAlloc;
use valkey_module::{valkey_module, Context, ValkeyError, ValkeyResult, ValkeyString, ValkeyValue};
fn keys_pos(ctx: &Context, args: Vec<ValkeyString>) -> ValkeyResult {
// Number of args (excluding command name) must be even
if (args.len() - 1) % 2 != 0 {
return Err(ValkeyError::WrongArity);
}
if ctx.is_keys_position_request() {
for i in 1..args.len() {
if (i - 1) % 2 == 0 {
ctx.key_at_pos(i as i32);
}
}
return Ok(ValkeyValue::NoReply);
}
let reply: Vec<_> = args.iter().skip(1).step_by(2).collect();
Ok(reply.into())
}
//////////////////////////////////////////////////////
valkey_module! {
name: "keys_pos",
version: 1,
allocator: (ValkeyAlloc, ValkeyAlloc),
data_types: [],
commands: [
["keys_pos", keys_pos, "getkeys-api", 1, 1, 1],
],
}