-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathfilter1.rs
More file actions
112 lines (99 loc) · 3.72 KB
/
filter1.rs
File metadata and controls
112 lines (99 loc) · 3.72 KB
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
use std::ops::Deref;
use std::sync::RwLock;
use valkey_module::alloc::ValkeyAlloc;
use valkey_module::logging::log_notice;
use valkey_module::{
valkey_module, CommandFilter, CommandFilterCtx, Context, RedisModuleCommandFilterCtx, Status,
ValkeyResult, ValkeyString, VALKEYMODULE_CMDFILTER_NOSELF,
};
// used to register filters using init and deinit, this is more complicated, best use the macro approach below
static INFO_FILTER: RwLock<Option<CommandFilter>> = RwLock::new(None);
fn init(ctx: &Context, _args: &[ValkeyString]) -> Status {
let info_filter = ctx.register_command_filter(info_filter_fn, VALKEYMODULE_CMDFILTER_NOSELF);
if info_filter.is_null() {
return Status::Err;
}
let mut info_guard = INFO_FILTER.write().unwrap();
*info_guard = Some(info_filter);
Status::Ok
}
fn deinit(ctx: &Context) -> Status {
let info_guard = INFO_FILTER.read().unwrap();
if let Some(ref info_filter) = info_guard.deref() {
ctx.unregister_command_filter(info_filter);
};
Status::Ok
}
// this is used to register and unregister the filter in init and deinit
// has to be extern "C", better to use the macro approach
extern "C" fn info_filter_fn(ctx: *mut RedisModuleCommandFilterCtx) {
let cf_ctx = CommandFilterCtx::new(ctx);
// we want the filter to be very efficient as it will be called for every command
// check if there is only 1 arg, the command
if cf_ctx.args_count() != 1 {
return;
}
// check if cmd (first arg) is info
let cmd = cf_ctx.arg_get_try_as_str(0).unwrap();
if !cmd.eq_ignore_ascii_case("info") {
return;
}
// grab client_id
let client_id = cf_ctx.get_client_id();
log_notice(&format!("info filter for client_id {}", client_id));
// replace info with info2 as the command name
cf_ctx.arg_replace(0, "info2");
}
// custom command that will replace info command
fn info2(ctx: &Context, _args: Vec<ValkeyString>) -> ValkeyResult {
ctx.log_notice("info2 command");
// do something different here
Ok("info2\n".into())
}
// this will be registered via valkey_module! macro, can be a regular Rust fn (not extern "C").
// this is the recommended approach for registering filters as it is simpler and cleaner
fn set_filter_fn(ctx: *mut RedisModuleCommandFilterCtx) {
let cf_ctx = CommandFilterCtx::new(ctx);
if cf_ctx.args_count() != 3 {
return;
}
// check if cmd (first arg) is set
let cmd = cf_ctx.cmd_get_try_as_str().unwrap();
if !cmd.eq_ignore_ascii_case("set") {
return;
}
let all_args = cf_ctx.get_all_args_wo_cmd();
log_notice(&format!("all_args: {:?}", all_args));
let key = cf_ctx.arg_get_try_as_str(1).unwrap();
let value = cf_ctx.arg_get_try_as_str(2).unwrap();
log_notice(&format!("set key: {}, value {}", key, value));
// delete 2nd arg key
cf_ctx.arg_delete(1);
// insert new key
cf_ctx.arg_insert(1, "new_key");
// replace 3rd arg value
cf_ctx.arg_replace(2, "new_value");
}
fn filter1_fn(_ctx: *mut RedisModuleCommandFilterCtx) {
// do something here, registered via valkey_module! macro
}
fn filter2_fn(_ctx: *mut RedisModuleCommandFilterCtx) {
// do something here, registered via valkey_module! macro
}
valkey_module! {
name: "filter1",
version: 1,
allocator: (ValkeyAlloc, ValkeyAlloc),
data_types: [],
init: init,
deinit: deinit,
commands: [
["info2", info2, "readonly", 0, 0, 0],
],
filters: [
// need to add paste crate to your Cargo.toml or you will get error: use of undeclared crate or module `paste`
[set_filter_fn, VALKEYMODULE_CMDFILTER_NOSELF],
[filter1_fn, VALKEYMODULE_CMDFILTER_NOSELF],
[filter2_fn, VALKEYMODULE_CMDFILTER_NOSELF]
]
}