Skip to content

Commit e7296cf

Browse files
optimize set root by caching key value (RedisJSON#418)
* optimize set root by caching key value * fix format * change pointer to referece * format fixes * review fixes * not using unsafe * review fixes * review fixes
1 parent 8813a86 commit e7296cf

File tree

3 files changed

+204
-103
lines changed

3 files changed

+204
-103
lines changed

src/c_api.rs

+98-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::{
88
os::raw::{c_char, c_void},
99
};
1010

11+
use std::marker::PhantomData;
12+
1113
use crate::commands::KeyValue;
1214
use jsonpath_lib::select::select_value::{SelectValue, SelectValueType};
1315
use jsonpath_lib::select::Selector;
@@ -86,7 +88,13 @@ pub extern "C" fn JSONAPI_openKey(
8688
let bytes = slice::from_raw_parts(bytes as *const u8, len);
8789
String::from_utf8_lossy(bytes).into_owned()
8890
};
89-
json_api_open_key_internal(RedisJsonKeyManager, ctx, &key) as *mut c_void
91+
json_api_open_key_internal(
92+
RedisJsonKeyManager {
93+
phantom: PhantomData,
94+
},
95+
ctx,
96+
&key,
97+
) as *mut c_void
9098
}
9199

92100
#[no_mangle]
@@ -95,7 +103,13 @@ pub extern "C" fn JSONAPI_openKeyFromStr(
95103
path: *const c_char,
96104
) -> *mut c_void {
97105
let key = unsafe { CStr::from_ptr(path).to_str().unwrap() };
98-
json_api_open_key_internal(RedisJsonKeyManager, ctx, &key) as *mut c_void
106+
json_api_open_key_internal(
107+
RedisJsonKeyManager {
108+
phantom: PhantomData,
109+
},
110+
ctx,
111+
&key,
112+
) as *mut c_void
99113
}
100114

101115
fn json_api_get_at<M: Manager>(_: M, json: *const c_void, index: size_t) -> *const c_void {
@@ -111,7 +125,13 @@ fn json_api_get_at<M: Manager>(_: M, json: *const c_void, index: size_t) -> *con
111125

112126
#[no_mangle]
113127
pub extern "C" fn JSONAPI_getAt(json: *const c_void, index: size_t) -> *const c_void {
114-
json_api_get_at(RedisJsonKeyManager, json, index)
128+
json_api_get_at(
129+
RedisJsonKeyManager {
130+
phantom: PhantomData,
131+
},
132+
json,
133+
index,
134+
)
115135
}
116136

117137
fn json_api_get_len<M: Manager>(_: M, json: *const c_void, count: *mut libc::size_t) -> c_int {
@@ -133,7 +153,13 @@ fn json_api_get_len<M: Manager>(_: M, json: *const c_void, count: *mut libc::siz
133153

134154
#[no_mangle]
135155
pub extern "C" fn JSONAPI_getLen(json: *const c_void, count: *mut size_t) -> c_int {
136-
json_api_get_len(RedisJsonKeyManager, json, count)
156+
json_api_get_len(
157+
RedisJsonKeyManager {
158+
phantom: PhantomData,
159+
},
160+
json,
161+
count,
162+
)
137163
}
138164

139165
fn json_api_get_type<M: Manager>(_: M, json: *const c_void) -> c_int {
@@ -142,7 +168,12 @@ fn json_api_get_type<M: Manager>(_: M, json: *const c_void) -> c_int {
142168

143169
#[no_mangle]
144170
pub extern "C" fn JSONAPI_getType(json: *const c_void) -> c_int {
145-
json_api_get_type(RedisJsonKeyManager, json)
171+
json_api_get_type(
172+
RedisJsonKeyManager {
173+
phantom: PhantomData,
174+
},
175+
json,
176+
)
146177
}
147178

148179
fn json_api_get_string<M: Manager>(
@@ -168,7 +199,14 @@ pub extern "C" fn JSONAPI_getString(
168199
str: *mut *const c_char,
169200
len: *mut size_t,
170201
) -> c_int {
171-
json_api_get_string(RedisJsonKeyManager, json, str, len)
202+
json_api_get_string(
203+
RedisJsonKeyManager {
204+
phantom: PhantomData,
205+
},
206+
json,
207+
str,
208+
len,
209+
)
172210
}
173211

174212
fn json_api_get_json<M: Manager>(
@@ -188,7 +226,14 @@ pub extern "C" fn JSONAPI_getJSON(
188226
ctx: *mut rawmod::RedisModuleCtx,
189227
str: *mut *mut rawmod::RedisModuleString,
190228
) -> c_int {
191-
json_api_get_json(RedisJsonKeyManager, json, ctx, str)
229+
json_api_get_json(
230+
RedisJsonKeyManager {
231+
phantom: PhantomData,
232+
},
233+
json,
234+
ctx,
235+
str,
236+
)
192237
}
193238

194239
#[no_mangle]
@@ -212,7 +257,13 @@ fn json_api_get_int<M: Manager>(_: M, json: *const c_void, val: *mut c_long) ->
212257

213258
#[no_mangle]
214259
pub extern "C" fn JSONAPI_getInt(json: *const c_void, val: *mut c_long) -> c_int {
215-
json_api_get_int(RedisJsonKeyManager, json, val)
260+
json_api_get_int(
261+
RedisJsonKeyManager {
262+
phantom: PhantomData,
263+
},
264+
json,
265+
val,
266+
)
216267
}
217268

218269
fn json_api_get_double<M: Manager>(_: M, json: *const c_void, val: *mut c_double) -> c_int {
@@ -228,7 +279,13 @@ fn json_api_get_double<M: Manager>(_: M, json: *const c_void, val: *mut c_double
228279

229280
#[no_mangle]
230281
pub extern "C" fn JSONAPI_getDouble(json: *const c_void, val: *mut c_double) -> c_int {
231-
json_api_get_double(RedisJsonKeyManager, json, val)
282+
json_api_get_double(
283+
RedisJsonKeyManager {
284+
phantom: PhantomData,
285+
},
286+
json,
287+
val,
288+
)
232289
}
233290

234291
fn json_api_get_boolean<M: Manager>(_: M, json: *const c_void, val: *mut c_int) -> c_int {
@@ -244,7 +301,13 @@ fn json_api_get_boolean<M: Manager>(_: M, json: *const c_void, val: *mut c_int)
244301

245302
#[no_mangle]
246303
pub extern "C" fn JSONAPI_getBoolean(json: *const c_void, val: *mut c_int) -> c_int {
247-
json_api_get_boolean(RedisJsonKeyManager, json, val)
304+
json_api_get_boolean(
305+
RedisJsonKeyManager {
306+
phantom: PhantomData,
307+
},
308+
json,
309+
val,
310+
)
248311
}
249312

250313
//---------------------------------------------------------------------------------------------
@@ -334,22 +397,43 @@ pub fn json_api_get<M: Manager>(_: M, val: *const c_void, path: *const c_char) -
334397

335398
#[no_mangle]
336399
pub extern "C" fn JSONAPI_get(key: *const c_void, path: *const c_char) -> *const c_void {
337-
json_api_get(RedisJsonKeyManager, key, path)
400+
json_api_get(
401+
RedisJsonKeyManager {
402+
phantom: PhantomData,
403+
},
404+
key,
405+
path,
406+
)
338407
}
339408

340409
#[no_mangle]
341410
pub extern "C" fn JSONAPI_len(iter: *const c_void) -> size_t {
342-
json_api_len(RedisJsonKeyManager, iter)
411+
json_api_len(
412+
RedisJsonKeyManager {
413+
phantom: PhantomData,
414+
},
415+
iter,
416+
)
343417
}
344418

345419
#[no_mangle]
346420
pub extern "C" fn JSONAPI_freeIter(iter: *mut c_void) {
347-
json_api_free_iter(RedisJsonKeyManager, iter)
421+
json_api_free_iter(
422+
RedisJsonKeyManager {
423+
phantom: PhantomData,
424+
},
425+
iter,
426+
)
348427
}
349428

350429
#[no_mangle]
351430
pub extern "C" fn JSONAPI_next(iter: *mut c_void) -> *const c_void {
352-
json_api_next(RedisJsonKeyManager, iter)
431+
json_api_next(
432+
RedisJsonKeyManager {
433+
phantom: PhantomData,
434+
},
435+
iter,
436+
)
353437
}
354438

355439
static REDISJSON_GETAPI: &str = concat!("RedisJSON_V1", "\0");

0 commit comments

Comments
 (0)