forked from RedisJSON/RedisJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_api.rs
590 lines (529 loc) · 19 KB
/
c_api.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use libc::size_t;
use std::ffi::CString;
use std::os::raw::{c_double, c_int, c_longlong};
use std::ptr::{null, null_mut};
use std::{
ffi::CStr,
os::raw::{c_char, c_void},
};
use crate::commands::KeyValue;
use jsonpath_lib::select::select_value::{SelectValue, SelectValueType};
use jsonpath_lib::select::Selector;
use redis_module::raw as rawmod;
use redis_module::{Context, RedisString, Status};
use crate::manager::{Manager, ReadHolder};
// extern crate readies_wd40;
// use crate::readies_wd40::{BB, _BB, getenv};
//
// structs
//
#[repr(C)]
pub enum JSONType {
String = 0,
Int = 1,
Double = 2,
Bool = 3,
Object = 4,
Array = 5,
Null = 6,
}
struct ResultsIterator<'a, V: SelectValue> {
results: Vec<&'a V>,
pos: usize,
}
//---------------------------------------------------------------------------------------------
pub static mut LLAPI_CTX: Option<*mut rawmod::RedisModuleCtx> = None;
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn create_rmstring(
ctx: *mut rawmod::RedisModuleCtx,
from_str: &str,
str: *mut *mut rawmod::RedisModuleString,
) -> c_int {
if let Ok(s) = CString::new(from_str) {
let p = s.as_bytes_with_nul().as_ptr().cast::<c_char>();
let len = s.as_bytes().len();
unsafe { *str = rawmod::RedisModule_CreateString.unwrap()(ctx, p, len) };
return Status::Ok as c_int;
}
Status::Err as c_int
}
pub fn json_api_open_key_internal<M: Manager>(
manager: M,
ctx: *mut rawmod::RedisModuleCtx,
key: RedisString,
) -> *const M::V {
let ctx = Context::new(ctx);
if let Ok(h) = manager.open_key_read(&ctx, &key) {
if let Ok(Some(v)) = h.get_value() {
return v;
}
}
null()
}
pub fn json_api_get_at<M: Manager>(_: M, json: *const c_void, index: size_t) -> *const c_void {
let json = unsafe { &*(json.cast::<M::V>()) };
match json.get_type() {
SelectValueType::Array => match json.get_index(index) {
Some(v) => (v as *const M::V).cast::<c_void>(),
_ => null(),
},
_ => null(),
}
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn json_api_get_len<M: Manager>(_: M, json: *const c_void, count: *mut libc::size_t) -> c_int {
let json = unsafe { &*(json.cast::<M::V>()) };
let len = match json.get_type() {
SelectValueType::String => Some(json.get_str().len()),
SelectValueType::Array | SelectValueType::Object => Some(json.len().unwrap()),
_ => None,
};
match len {
Some(l) => {
unsafe { *count = l };
Status::Ok as c_int
}
None => Status::Err as c_int,
}
}
pub fn json_api_get_type<M: Manager>(_: M, json: *const c_void) -> c_int {
json_api_get_type_internal(unsafe { &*(json.cast::<M::V>()) }) as c_int
}
pub fn json_api_get_string<M: Manager>(
_: M,
json: *const c_void,
str: *mut *const c_char,
len: *mut size_t,
) -> c_int {
let json = unsafe { &*(json.cast::<M::V>()) };
match json.get_type() {
SelectValueType::String => {
let s = json.as_str();
set_string(s, str, len);
Status::Ok as c_int
}
_ => Status::Err as c_int,
}
}
pub fn json_api_get_json<M: Manager>(
_: M,
json: *const c_void,
ctx: *mut rawmod::RedisModuleCtx,
str: *mut *mut rawmod::RedisModuleString,
) -> c_int {
let json = unsafe { &*(json.cast::<M::V>()) };
let res = KeyValue::<M::V>::serialize_object(json, None, None, None);
create_rmstring(ctx, &res, str)
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn json_api_get_int<M: Manager>(_: M, json: *const c_void, val: *mut c_longlong) -> c_int {
let json = unsafe { &*(json.cast::<M::V>()) };
match json.get_type() {
SelectValueType::Long => {
unsafe { *val = json.get_long() };
Status::Ok as c_int
}
_ => Status::Err as c_int,
}
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn json_api_get_double<M: Manager>(_: M, json: *const c_void, val: *mut c_double) -> c_int {
let json = unsafe { &*(json.cast::<M::V>()) };
match json.get_type() {
SelectValueType::Double => {
unsafe { *val = json.get_double() };
Status::Ok as c_int
}
SelectValueType::Long => {
unsafe { *val = json.get_long() as f64 };
Status::Ok as c_int
}
_ => Status::Err as c_int,
}
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn json_api_get_boolean<M: Manager>(_: M, json: *const c_void, val: *mut c_int) -> c_int {
let json = unsafe { &*(json.cast::<M::V>()) };
match json.get_type() {
SelectValueType::Bool => {
unsafe { *val = json.get_bool() as c_int };
Status::Ok as c_int
}
_ => Status::Err as c_int,
}
}
//---------------------------------------------------------------------------------------------
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn set_string(from_str: &str, str: *mut *const c_char, len: *mut size_t) -> c_int {
if !str.is_null() {
unsafe {
*str = from_str.as_ptr().cast::<c_char>();
*len = from_str.len();
}
return Status::Ok as c_int;
}
Status::Err as c_int
}
fn json_api_get_type_internal<V: SelectValue>(v: &V) -> JSONType {
match v.get_type() {
SelectValueType::Null => JSONType::Null,
SelectValueType::Bool => JSONType::Bool,
SelectValueType::Long => JSONType::Int,
SelectValueType::Double => JSONType::Double,
SelectValueType::String => JSONType::String,
SelectValueType::Array => JSONType::Array,
SelectValueType::Object => JSONType::Object,
}
}
pub fn json_api_next<M: Manager>(_: M, iter: *mut c_void) -> *const c_void {
let iter = unsafe { &mut *(iter.cast::<ResultsIterator<M::V>>()) };
if iter.pos >= iter.results.len() {
null_mut()
} else {
let res = (iter.results[iter.pos] as *const M::V).cast::<c_void>();
iter.pos += 1;
res
}
}
pub fn json_api_len<M: Manager>(_: M, iter: *const c_void) -> size_t {
let iter = unsafe { &*(iter.cast::<ResultsIterator<M::V>>()) };
iter.results.len() as size_t
}
pub fn json_api_free_iter<M: Manager>(_: M, iter: *mut c_void) {
unsafe {
Box::from_raw(iter.cast::<ResultsIterator<M::V>>());
}
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn json_api_get<M: Manager>(_: M, val: *const c_void, path: *const c_char) -> *const c_void {
let v = unsafe { &*(val.cast::<M::V>()) };
let mut selector = Selector::new();
selector.value(v);
let path = unsafe { CStr::from_ptr(path).to_str().unwrap() };
if selector.str_path(path).is_err() {
return null();
}
match selector.select() {
Ok(s) => Box::into_raw(Box::new(ResultsIterator { results: s, pos: 0 })).cast::<c_void>(),
Err(_) => null(),
}
}
pub fn json_api_is_json<M: Manager>(m: M, key: *mut rawmod::RedisModuleKey) -> c_int {
match m.is_json(key) {
Ok(res) => res as c_int,
Err(_) => 0,
}
}
pub fn get_llapi_ctx() -> Context {
Context::new(unsafe { LLAPI_CTX.unwrap() })
}
#[macro_export]
macro_rules! redis_json_module_export_shared_api {
(
get_manage: $get_manager_expr:expr,
pre_command_function: $pre_command_function_expr:expr,
) => {
#[no_mangle]
pub extern "C" fn JSONAPI_openKey(
ctx: *mut rawmod::RedisModuleCtx,
key_str: *mut rawmod::RedisModuleString,
) -> *mut c_void {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_open_key_internal(mngr, ctx, RedisString::new(ctx, key_str))
as *mut c_void,
None => json_api_open_key_internal(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
ctx,
RedisString::new(ctx, key_str),
) as *mut c_void,
}
}
#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn JSONAPI_openKeyFromStr(
ctx: *mut rawmod::RedisModuleCtx,
path: *const c_char,
) -> *mut c_void {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let key = unsafe { CStr::from_ptr(path).to_str().unwrap() };
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_open_key_internal(mngr, ctx, RedisString::create(ctx, key))
as *mut c_void,
None => json_api_open_key_internal(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
ctx,
RedisString::create(ctx, key),
) as *mut c_void,
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_get(key: *const c_void, path: *const c_char) -> *const c_void {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get(mngr, key, path),
None => json_api_get(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
key,
path,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_next(iter: *mut c_void) -> *const c_void {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_next(mngr, iter),
None => json_api_next(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
iter,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_len(iter: *const c_void) -> size_t {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_len(mngr, iter),
None => json_api_len(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
iter,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_freeIter(iter: *mut c_void) {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_free_iter(mngr, iter),
None => json_api_free_iter(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
iter,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_getAt(json: *const c_void, index: size_t) -> *const c_void {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get_at(mngr, json, index),
None => json_api_get_at(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
json,
index,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_getLen(json: *const c_void, count: *mut size_t) -> c_int {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get_len(mngr, json, count),
None => json_api_get_len(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
json,
count,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_getType(json: *const c_void) -> c_int {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get_type(mngr, json),
None => json_api_get_type(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
json,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_getInt(json: *const c_void, val: *mut c_longlong) -> c_int {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get_int(mngr, json, val),
None => json_api_get_int(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
json,
val,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_getDouble(json: *const c_void, val: *mut c_double) -> c_int {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get_double(mngr, json, val),
None => json_api_get_double(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
json,
val,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_getBoolean(json: *const c_void, val: *mut c_int) -> c_int {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get_boolean(mngr, json, val),
None => json_api_get_boolean(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
json,
val,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_getString(
json: *const c_void,
str: *mut *const c_char,
len: *mut size_t,
) -> c_int {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get_string(mngr, json, str, len),
None => json_api_get_string(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
json,
str,
len,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_getJSON(
json: *const c_void,
ctx: *mut rawmod::RedisModuleCtx,
str: *mut *mut rawmod::RedisModuleString,
) -> c_int {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_get_json(mngr, json, ctx, str),
None => json_api_get_json(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
json,
ctx,
str,
),
}
}
#[no_mangle]
pub extern "C" fn JSONAPI_isJSON(key: *mut rawmod::RedisModuleKey) -> c_int {
$pre_command_function_expr(&get_llapi_ctx(), &Vec::new());
let m = $get_manager_expr;
match m {
Some(mngr) => json_api_is_json(mngr, key),
None => json_api_is_json(
manager::RedisJsonKeyManager {
phantom: PhantomData,
},
key,
),
}
}
static REDISJSON_GETAPI: &str = concat!("RedisJSON_V1", "\0");
pub fn export_shared_api(ctx: &Context) {
unsafe {
ctx.log_notice("Exported RedisJSON_V1 API");
LLAPI_CTX = Some(rawmod::RedisModule_GetThreadSafeContext.unwrap()(
std::ptr::null_mut(),
));
ctx.export_shared_api(
(&JSONAPI as *const RedisJSONAPI_V1).cast::<c_void>(),
REDISJSON_GETAPI.as_ptr().cast::<c_char>(),
);
};
}
static JSONAPI: RedisJSONAPI_V1 = RedisJSONAPI_V1 {
openKey: JSONAPI_openKey,
openKeyFromStr: JSONAPI_openKeyFromStr,
get: JSONAPI_get,
next: JSONAPI_next,
len: JSONAPI_len,
freeIter: JSONAPI_freeIter,
getAt: JSONAPI_getAt,
getLen: JSONAPI_getLen,
getType: JSONAPI_getType,
getInt: JSONAPI_getInt,
getDouble: JSONAPI_getDouble,
getBoolean: JSONAPI_getBoolean,
getString: JSONAPI_getString,
getJSON: JSONAPI_getJSON,
isJSON: JSONAPI_isJSON,
};
#[repr(C)]
#[derive(Copy, Clone)]
#[allow(non_snake_case)]
pub struct RedisJSONAPI_V1 {
pub openKey: extern "C" fn(
ctx: *mut rawmod::RedisModuleCtx,
key_str: *mut rawmod::RedisModuleString,
) -> *mut c_void,
pub openKeyFromStr:
extern "C" fn(ctx: *mut rawmod::RedisModuleCtx, path: *const c_char) -> *mut c_void,
pub get: extern "C" fn(val: *const c_void, path: *const c_char) -> *const c_void,
pub next: extern "C" fn(iter: *mut c_void) -> *const c_void,
pub len: extern "C" fn(iter: *const c_void) -> size_t,
pub freeIter: extern "C" fn(iter: *mut c_void),
pub getAt: extern "C" fn(json: *const c_void, index: size_t) -> *const c_void,
pub getLen: extern "C" fn(json: *const c_void, len: *mut size_t) -> c_int,
pub getType: extern "C" fn(json: *const c_void) -> c_int,
pub getInt: extern "C" fn(json: *const c_void, val: *mut c_longlong) -> c_int,
pub getDouble: extern "C" fn(json: *const c_void, val: *mut c_double) -> c_int,
pub getBoolean: extern "C" fn(json: *const c_void, val: *mut c_int) -> c_int,
pub getString: extern "C" fn(
json: *const c_void,
str: *mut *const c_char,
len: *mut size_t,
) -> c_int,
pub getJSON: extern "C" fn(
json: *const c_void,
ctx: *mut rawmod::RedisModuleCtx,
str: *mut *mut rawmod::RedisModuleString,
) -> c_int,
pub isJSON: extern "C" fn(key: *mut rawmod::RedisModuleKey) -> c_int,
}
};
}