Skip to content

Commit 3dea1b9

Browse files
authored
Add C API getJSONFromIter (RedisJSON#816)
1 parent ae0fa8a commit 3dea1b9

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/c_api.rs

+29
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ pub fn json_api_get_json<M: Manager>(
131131
create_rmstring(ctx, &res, str)
132132
}
133133

134+
pub fn json_api_get_json_from_iter<M: Manager>(
135+
_: M,
136+
iter: *mut c_void,
137+
ctx: *mut rawmod::RedisModuleCtx,
138+
str: *mut *mut rawmod::RedisModuleString,
139+
) -> c_int {
140+
let iter = unsafe { &*(iter.cast::<ResultsIterator<M::V>>()) };
141+
if iter.pos >= iter.results.len() {
142+
Status::Err as c_int
143+
} else {
144+
let res = KeyValue::<M::V>::serialize_object(&iter.results, None, None, None);
145+
create_rmstring(ctx, &res, str);
146+
Status::Ok as c_int
147+
}
148+
}
149+
134150
#[allow(clippy::not_unsafe_ptr_arg_deref)]
135151
pub fn json_api_get_int<M: Manager>(_: M, json: *const c_void, val: *mut c_longlong) -> c_int {
136152
let json = unsafe { &*(json.cast::<M::V>()) };
@@ -393,6 +409,17 @@ macro_rules! redis_json_module_export_shared_api {
393409
)
394410
}
395411

412+
#[no_mangle]
413+
pub extern "C" fn JSONAPI_getJSONFromIter(iter: *mut c_void,
414+
ctx: *mut rawmod::RedisModuleCtx,
415+
str: *mut *mut rawmod::RedisModuleString,) -> c_int {
416+
run_on_manager!(
417+
pre_command: ||$pre_command_function_expr(&get_llapi_ctx(), &Vec::new()),
418+
get_mngr: $get_manager_expr,
419+
run: |mngr|{json_api_get_json_from_iter(mngr, iter, ctx, str)},
420+
)
421+
}
422+
396423
#[no_mangle]
397424
pub extern "C" fn JSONAPI_isJSON(key: *mut rawmod::RedisModuleKey) -> c_int {
398425
run_on_manager!(
@@ -475,6 +502,7 @@ macro_rules! redis_json_module_export_shared_api {
475502
pathFree: JSONAPI_pathFree,
476503
pathIsSingle: JSONAPI_pathIsSingle,
477504
pathHasDefinedOrder: JSONAPI_pathHasDefinedOrder,
505+
getJSONFromIter: JSONAPI_getJSONFromIter,
478506
};
479507

480508
#[repr(C)]
@@ -514,6 +542,7 @@ macro_rules! redis_json_module_export_shared_api {
514542
pub pathFree: extern "C" fn(json_path: *mut c_void),
515543
pub pathIsSingle: extern "C" fn(json_path: *const c_void) -> c_int,
516544
pub pathHasDefinedOrder: extern "C" fn(json_path: *const c_void) -> c_int,
545+
pub getJSONFromIter: extern "C" fn(iter: *mut c_void, ctx: *mut rawmod::RedisModuleCtx, str: *mut *mut rawmod::RedisModuleString,) -> c_int,
517546
}
518547
};
519548
}

src/include/rejson_api.h

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ typedef struct RedisJSONAPI {
8585
int (*pathIsSingle)(JSONPath);
8686
int (*pathHasDefinedOrder)(JSONPath);
8787

88+
// Return JSON String representation from an iterator (without consuming the iterator)
89+
// The caller gains ownership of `str`
90+
int (*getJSONFromIter)(JSONResultsIterator iter, RedisModuleCtx *ctx, RedisModuleString **str);
91+
8892
} RedisJSONAPI;
8993

9094
#ifdef __cplusplus

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use redis_module::{Context, RedisResult};
1515
#[cfg(not(feature = "as-library"))]
1616
use crate::c_api::{
1717
get_llapi_ctx, json_api_free_iter, json_api_get, json_api_get_at, json_api_get_boolean,
18-
json_api_get_double, json_api_get_int, json_api_get_json, json_api_get_len,
19-
json_api_get_string, json_api_get_type, json_api_is_json, json_api_len, json_api_next,
20-
json_api_open_key_internal, LLAPI_CTX,
18+
json_api_get_double, json_api_get_int, json_api_get_json, json_api_get_json_from_iter,
19+
json_api_get_len, json_api_get_string, json_api_get_type, json_api_is_json, json_api_len,
20+
json_api_next, json_api_open_key_internal, LLAPI_CTX,
2121
};
2222
use crate::redisjson::Format;
2323

0 commit comments

Comments
 (0)