forked from RedisJSON/RedisJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisjson.rs
277 lines (254 loc) · 8.44 KB
/
redisjson.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
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
// RedisJSON Redis module.
//
// Translate between JSON and tree of Redis objects:
// User-provided JSON is converted to a tree. This tree is stored transparently in Redis.
// It can be operated on (e.g. INCR) and serialized back to JSON.
use redis_module::raw;
use std::os::raw::{c_int, c_void};
use crate::backward;
use crate::error::Error;
use crate::ivalue_manager::RedisIValueJsonKeyManager;
use crate::manager::{Manager, RedisJsonKeyManager};
use crate::{get_manager_type, ManagerType};
use serde::Serialize;
use std::fmt;
use std::fmt::Display;
use std::marker::PhantomData;
use std::str::FromStr;
/// Returns normalized start index
#[must_use]
pub fn normalize_arr_start_index(start: i64, len: i64) -> i64 {
if start < 0 {
0.max(len + start)
} else {
// start >= 0
start.min(len - 1)
}
}
/// Return normalized `(start, end)` indices as a tuple
#[must_use]
pub fn normalize_arr_indices(start: i64, end: i64, len: i64) -> (i64, i64) {
// Normalize start
let start = normalize_arr_start_index(start, len);
// Normalize end
let end = match end {
0 => len,
e if e < 0 => 0.max(len + end),
_ => end.min(len),
};
(start, end)
}
#[derive(Debug, PartialEq)]
pub enum SetOptions {
NotExists,
AlreadyExists,
None,
}
#[derive(Debug, PartialEq)]
pub enum Format {
JSON,
BSON,
}
impl FromStr for Format {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"JSON" => Ok(Self::JSON),
"BSON" => Ok(Self::BSON),
_ => Err("ERR wrong format".into()),
}
}
}
///
/// Backwards compatibility convertor for `RedisJSON` 1.x clients
///
pub struct Path<'a> {
original_path: &'a str,
fixed_path: Option<String>,
}
impl<'a> Path<'a> {
#[must_use]
pub fn new(path: &'a str) -> Path {
let fixed_path = if path.starts_with('$') {
None
} else {
let mut cloned = path.to_string();
if path == "." {
cloned.replace_range(..1, "$");
} else if path.starts_with('.') {
cloned.insert(0, '$');
} else {
cloned.insert_str(0, "$.");
}
Some(cloned)
};
Path {
original_path: path,
fixed_path,
}
}
#[must_use]
pub const fn is_legacy(&self) -> bool {
self.fixed_path.is_some()
}
pub fn get_path(&self) -> &str {
self.fixed_path
.as_ref()
.map_or(self.original_path, String::as_str)
}
#[must_use]
pub fn get_original(&self) -> &'a str {
self.original_path
}
}
impl Display for Path<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.get_path())
}
}
#[derive(Debug)]
pub struct RedisJSON<T> {
//FIXME: make private and expose array/object Values without requiring a path
pub data: T,
}
pub mod type_methods {
use super::*;
use std::{ffi::CString, ptr::null_mut};
pub extern "C" fn rdb_load(rdb: *mut raw::RedisModuleIO, encver: c_int) -> *mut c_void {
let json_string = value_rdb_load_json(rdb, encver);
match json_string {
Ok(json_string) => match get_manager_type() {
ManagerType::SerdeValue => {
let m = RedisJsonKeyManager {
phantom: PhantomData,
};
let v = m.from_str(&json_string, Format::JSON);
match v {
Ok(res) => Box::into_raw(Box::new(res)).cast::<libc::c_void>(),
Err(_) => null_mut(),
}
}
ManagerType::IValue => {
let m = RedisIValueJsonKeyManager {
phantom: PhantomData,
};
let v = m.from_str(&json_string, Format::JSON);
match v {
Ok(res) => Box::into_raw(Box::new(res)).cast::<libc::c_void>(),
Err(_) => null_mut(),
}
}
},
Err(_) => null_mut(),
}
}
#[allow(non_snake_case, unused)]
pub fn value_rdb_load_json(
rdb: *mut raw::RedisModuleIO,
encver: c_int,
) -> Result<String, Error> {
Ok(match encver {
0 => {
let v = backward::json_rdb_load(rdb)?;
let mut out = serde_json::Serializer::new(Vec::new());
v.serialize(&mut out).unwrap();
String::from_utf8(out.into_inner()).unwrap()
}
2 => {
let data = raw::load_string(rdb)?;
// Backward support for modules that had AUX field for RediSarch
// TODO remove in future versions
let u = raw::load_unsigned(rdb)?;
if u > 0 {
raw::load_string(rdb)?;
raw::load_string(rdb)?;
}
data.try_as_str()?.to_string()
}
3 => {
let data = raw::load_string(rdb)?;
data.try_as_str()?.to_string()
}
_ => panic!("Can't load old RedisJSON RDB"),
})
}
/// # Safety
#[allow(non_snake_case, unused)]
pub unsafe extern "C" fn free(value: *mut c_void) {
if value.is_null() {
// on Redis 6.0 we might get a NULL value here, so we need to handle it.
return;
}
match get_manager_type() {
ManagerType::SerdeValue => {
let v = value.cast::<RedisJSON<serde_json::Value>>();
// Take ownership of the data from Redis (causing it to be dropped when we return)
Box::from_raw(v);
}
ManagerType::IValue => {
let v = value.cast::<RedisJSON<ijson::IValue>>();
// Take ownership of the data from Redis (causing it to be dropped when we return)
Box::from_raw(v);
}
};
}
/// # Safety
#[allow(non_snake_case, unused)]
pub unsafe extern "C" fn rdb_save(rdb: *mut raw::RedisModuleIO, value: *mut c_void) {
let mut out = serde_json::Serializer::new(Vec::new());
let json = match get_manager_type() {
ManagerType::SerdeValue => {
let v = unsafe { &*value.cast::<RedisJSON<serde_json::Value>>() };
v.data.serialize(&mut out).unwrap();
String::from_utf8(out.into_inner()).unwrap()
}
ManagerType::IValue => {
let v = unsafe { &*value.cast::<RedisJSON<ijson::IValue>>() };
v.data.serialize(&mut out).unwrap();
String::from_utf8(out.into_inner()).unwrap()
}
};
let cjson = CString::new(json).unwrap();
raw::save_string(rdb, cjson.to_str().unwrap());
}
/// # Safety
#[allow(non_snake_case, unused)]
pub unsafe extern "C" fn copy(
fromkey: *mut raw::RedisModuleString,
tokey: *mut raw::RedisModuleString,
value: *const c_void,
) -> *mut c_void {
match get_manager_type() {
ManagerType::SerdeValue => {
let v = unsafe { &*value.cast::<RedisJSON<serde_json::Value>>() };
let value = v.data.clone();
Box::into_raw(Box::new(value)).cast::<c_void>()
}
ManagerType::IValue => {
let v = unsafe { &*value.cast::<RedisJSON<ijson::IValue>>() };
let value = v.data.clone();
Box::into_raw(Box::new(value)).cast::<c_void>()
}
}
}
/// # Safety
#[allow(non_snake_case, unused)]
pub unsafe extern "C" fn mem_usage(value: *const c_void) -> usize {
match get_manager_type() {
ManagerType::SerdeValue => {
let json = unsafe { &*(value as *mut RedisJSON<serde_json::Value>) };
let manager = RedisJsonKeyManager {
phantom: PhantomData,
};
manager.get_memory(&json.data).unwrap_or(0)
}
ManagerType::IValue => {
let json = unsafe { &*(value as *mut RedisJSON<ijson::IValue>) };
let manager = RedisIValueJsonKeyManager {
phantom: PhantomData,
};
manager.get_memory(&json.data).unwrap_or(0)
}
}
}
}