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
use std::ffi::CString;
use std::os::raw::{c_char, c_int, c_long};
use std::ptr;
use crate::key::{RedisKey, RedisKeyWritable};
use crate::raw;
use crate::LogLevel;
use crate::{RedisError, RedisResult, RedisString, RedisValue};
#[cfg(feature = "experimental-api")]
mod timer;
pub struct Context {
pub(crate) ctx: *mut raw::RedisModuleCtx,
}
impl Context {
pub fn new(ctx: *mut raw::RedisModuleCtx) -> Self {
Self { ctx }
}
pub fn dummy() -> Self {
Self {
ctx: ptr::null_mut(),
}
}
#[cfg(feature = "experimental-api")]
pub fn get_thread_safe_context() -> Self {
let ctx = unsafe { raw::RedisModule_GetThreadSafeContext.unwrap()(ptr::null_mut()) };
Context::new(ctx)
}
#[cfg(feature = "experimental-api")]
pub fn lock(&self) {
unsafe { raw::RedisModule_ThreadSafeContextLock.unwrap()(self.ctx) };
}
#[cfg(feature = "experimental-api")]
pub fn unlock(&self) {
unsafe { raw::RedisModule_ThreadSafeContextUnlock.unwrap()(self.ctx) };
}
pub fn log(&self, level: LogLevel, message: &str) {
let level = CString::new(format!("{:?}", level).to_lowercase()).unwrap();
let fmt = CString::new(message).unwrap();
unsafe { raw::RedisModule_Log.unwrap()(self.ctx, level.as_ptr(), fmt.as_ptr()) }
}
pub fn log_debug(&self, message: &str) {
self.log(LogLevel::Notice, message);
}
pub fn auto_memory(&self) {
unsafe {
raw::RedisModule_AutoMemory.unwrap()(self.ctx);
}
}
pub fn is_keys_position_request(&self) -> bool {
if cfg!(feature = "test") {
return false;
}
let result = unsafe { raw::RedisModule_IsKeysPositionRequest.unwrap()(self.ctx) };
result != 0
}
pub fn key_at_pos(&self, pos: i32) {
unsafe {
raw::RedisModule_KeyAtPos.unwrap()(self.ctx, pos as c_int);
}
}
pub fn call(&self, command: &str, args: &[&str]) -> RedisResult {
let terminated_args: Vec<RedisString> = args
.iter()
.map(|s| RedisString::create(self.ctx, s))
.collect();
let inner_args: Vec<*mut raw::RedisModuleString> =
terminated_args.iter().map(|s| s.inner).collect();
let cmd = CString::new(command).unwrap();
let reply: *mut raw::RedisModuleCallReply = unsafe {
let p_call = raw::RedisModule_Call.unwrap();
p_call(
self.ctx,
cmd.as_ptr(),
raw::FMT,
inner_args.as_ptr() as *mut c_char,
terminated_args.len(),
)
};
let result = Self::parse_call_reply(reply);
if !reply.is_null() {
raw::free_call_reply(reply);
}
result
}
fn parse_call_reply(reply: *mut raw::RedisModuleCallReply) -> RedisResult {
match raw::call_reply_type(reply) {
raw::ReplyType::Error => Err(RedisError::String(raw::call_reply_string(reply))),
raw::ReplyType::Unknown => Err(RedisError::Str("Error on method call")),
raw::ReplyType::Array => {
let length = raw::call_reply_length(reply);
let mut vec = Vec::with_capacity(length);
for i in 0..length {
vec.push(Self::parse_call_reply(raw::call_reply_array_element(
reply, i,
))?)
}
Ok(RedisValue::Array(vec))
}
raw::ReplyType::Integer => Ok(RedisValue::Integer(raw::call_reply_integer(reply))),
raw::ReplyType::String => Ok(RedisValue::SimpleString(raw::call_reply_string(reply))),
raw::ReplyType::Null => Ok(RedisValue::Null),
}
}
pub fn reply(&self, r: RedisResult) -> raw::Status {
match r {
Ok(RedisValue::Integer(v)) => unsafe {
raw::RedisModule_ReplyWithLongLong.unwrap()(self.ctx, v).into()
},
Ok(RedisValue::Float(v)) => unsafe {
raw::RedisModule_ReplyWithDouble.unwrap()(self.ctx, v).into()
},
Ok(RedisValue::SimpleStringStatic(s)) => unsafe {
let msg = CString::new(s).unwrap();
raw::RedisModule_ReplyWithSimpleString.unwrap()(self.ctx, msg.as_ptr()).into()
},
Ok(RedisValue::SimpleString(s)) => unsafe {
let msg = CString::new(s).unwrap();
raw::RedisModule_ReplyWithSimpleString.unwrap()(self.ctx, msg.as_ptr()).into()
},
Ok(RedisValue::BulkString(s)) => unsafe {
raw::RedisModule_ReplyWithString.unwrap()(
self.ctx,
RedisString::create(self.ctx, s.as_ref()).inner,
)
.into()
},
Ok(RedisValue::Array(array)) => {
unsafe {
raw::RedisModule_ReplyWithArray.unwrap()(self.ctx, array.len() as c_long);
}
for elem in array {
self.reply(Ok(elem));
}
raw::Status::Ok
}
Ok(RedisValue::Null) => unsafe {
raw::RedisModule_ReplyWithNull.unwrap()(self.ctx).into()
},
Ok(RedisValue::NoReply) => raw::Status::Ok,
Err(RedisError::WrongArity) => unsafe {
if self.is_keys_position_request() {
raw::Status::Err
} else {
raw::RedisModule_WrongArity.unwrap()(self.ctx).into()
}
},
Err(RedisError::String(s)) => unsafe {
let msg = CString::new(s).unwrap();
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
},
Err(RedisError::Str(s)) => unsafe {
let msg = CString::new(s).unwrap();
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
},
}
}
pub fn open_key(&self, key: &str) -> RedisKey {
RedisKey::open(self.ctx, key)
}
pub fn open_key_writable(&self, key: &str) -> RedisKeyWritable {
RedisKeyWritable::open(self.ctx, key)
}
pub fn replicate_verbatim(&self) {
raw::replicate_verbatim(self.ctx);
}
pub fn create_string(&self, s: &str) -> RedisString {
RedisString::create(self.ctx, s)
}
}