@@ -18,7 +18,7 @@ use crate::key_value::KeyValue;
18
18
use json_path:: select_value:: { SelectValue , SelectValueType } ;
19
19
use json_path:: { compile, create} ;
20
20
use redis_module:: raw as rawmod;
21
- use redis_module:: { Context , RedisString , Status } ;
21
+ use redis_module:: { key :: KeyFlags , Context , RedisString , Status } ;
22
22
23
23
use crate :: manager:: { Manager , ReadHolder } ;
24
24
@@ -78,6 +78,21 @@ pub fn json_api_open_key_internal<M: Manager>(
78
78
null ( )
79
79
}
80
80
81
+ pub fn json_api_open_key_with_flags_internal < M : Manager > (
82
+ manager : M ,
83
+ ctx : * mut rawmod:: RedisModuleCtx ,
84
+ key : RedisString ,
85
+ flags : KeyFlags ,
86
+ ) -> * const M :: V {
87
+ let ctx: Context = Context :: new ( ctx) ;
88
+ if let Ok ( h) = manager. open_key_read_with_flags ( & ctx, & key, flags) {
89
+ if let Ok ( Some ( v) ) = h. get_value ( ) {
90
+ return v;
91
+ }
92
+ }
93
+ null ( )
94
+ }
95
+
81
96
pub fn json_api_get_at < M : Manager > ( _: M , json : * const c_void , index : size_t ) -> * const c_void {
82
97
let json = unsafe { & * ( json. cast :: < M :: V > ( ) ) } ;
83
98
match json. get_type ( ) {
@@ -313,6 +328,7 @@ macro_rules! redis_json_module_export_shared_api {
313
328
pre_command_function: $pre_command_function_expr: expr,
314
329
) => {
315
330
use std:: ptr:: NonNull ;
331
+ use std:: ffi:: CString ;
316
332
317
333
#[ no_mangle]
318
334
pub extern "C" fn JSONAPI_openKey (
@@ -326,6 +342,26 @@ macro_rules! redis_json_module_export_shared_api {
326
342
)
327
343
}
328
344
345
+ #[ no_mangle]
346
+ pub extern "C" fn JSONAPI_openKey_withFlags (
347
+ ctx: * mut rawmod:: RedisModuleCtx ,
348
+ key_str: * mut rawmod:: RedisModuleString ,
349
+ flags: c_int,
350
+ ) -> * mut c_void {
351
+ run_on_manager!(
352
+ pre_command: ||$pre_command_function_expr( & get_llapi_ctx( ) , & Vec :: new( ) ) ,
353
+ get_mngr: $get_manager_expr,
354
+ run: |mngr| {
355
+ json_api_open_key_with_flags_internal(
356
+ mngr,
357
+ ctx,
358
+ RedisString :: new( NonNull :: new( ctx) , key_str) ,
359
+ KeyFlags :: from_bits_truncate( flags as i32 ) ,
360
+ ) as * mut c_void
361
+ } ,
362
+ )
363
+ }
364
+
329
365
#[ no_mangle]
330
366
#[ allow( clippy:: not_unsafe_ptr_arg_deref) ]
331
367
pub extern "C" fn JSONAPI_openKeyFromStr (
@@ -543,39 +579,24 @@ macro_rules! redis_json_module_export_shared_api {
543
579
)
544
580
}
545
581
546
- static REDISJSON_GETAPI_V1 : & str = concat!( "RedisJSON_V1" , "\0 " ) ;
547
- static REDISJSON_GETAPI_V2 : & str = concat!( "RedisJSON_V2" , "\0 " ) ;
548
- static REDISJSON_GETAPI_V3 : & str = concat!( "RedisJSON_V3" , "\0 " ) ;
549
- static REDISJSON_GETAPI_V4 : & str = concat!( "RedisJSON_V4" , "\0 " ) ;
582
+ // The apiname argument of export_shared_api should be a string literal with static lifetime
583
+ static mut VEC_EXPORT_SHARED_API_NAME : Vec <CString > = Vec :: new( ) ;
550
584
551
585
pub fn export_shared_api( ctx: & Context ) {
552
586
unsafe {
553
587
LLAPI_CTX = Some ( rawmod:: RedisModule_GetThreadSafeContext . unwrap( ) (
554
588
std:: ptr:: null_mut( ) ,
555
589
) ) ;
556
- ctx. export_shared_api(
557
- ( & JSONAPI_CURRENT as * const RedisJSONAPI_CURRENT ) . cast:: <c_void>( ) ,
558
- REDISJSON_GETAPI_V1 . as_ptr( ) . cast:: <c_char>( ) ,
559
- ) ;
560
- ctx. log_notice( "Exported RedisJSON_V1 API" ) ;
561
-
562
- ctx. export_shared_api(
563
- ( & JSONAPI_CURRENT as * const RedisJSONAPI_CURRENT ) . cast:: <c_void>( ) ,
564
- REDISJSON_GETAPI_V2 . as_ptr( ) . cast:: <c_char>( ) ,
565
- ) ;
566
- ctx. log_notice( "Exported RedisJSON_V2 API" ) ;
567
-
568
- ctx. export_shared_api(
569
- ( & JSONAPI_CURRENT as * const RedisJSONAPI_CURRENT ) . cast:: <c_void>( ) ,
570
- REDISJSON_GETAPI_V3 . as_ptr( ) . cast:: <c_char>( ) ,
571
- ) ;
572
- ctx. log_notice( "Exported RedisJSON_V3 API" ) ;
573
-
574
- ctx. export_shared_api(
575
- ( & JSONAPI_CURRENT as * const RedisJSONAPI_CURRENT ) . cast:: <c_void>( ) ,
576
- REDISJSON_GETAPI_V4 . as_ptr( ) . cast:: <c_char>( ) ,
577
- ) ;
578
- ctx. log_notice( "Exported RedisJSON_V4 API" ) ;
590
+
591
+ for v in 1 ..6 {
592
+ let version = format!( "RedisJSON_V{}" , v) ;
593
+ VEC_EXPORT_SHARED_API_NAME . push( CString :: new( version. as_str( ) ) . unwrap( ) ) ;
594
+ ctx. export_shared_api(
595
+ ( & JSONAPI_CURRENT as * const RedisJSONAPI_CURRENT ) . cast:: <c_void>( ) ,
596
+ VEC_EXPORT_SHARED_API_NAME [ v-1 ] . as_ptr( ) . cast:: <c_char>( ) ,
597
+ ) ;
598
+ ctx. log_notice( & format!( "Exported {} API" , version) ) ;
599
+ }
579
600
} ;
580
601
}
581
602
@@ -608,6 +629,8 @@ macro_rules! redis_json_module_export_shared_api {
608
629
getKeyValues: JSONAPI_getKeyValues ,
609
630
nextKeyValue: JSONAPI_nextKeyValue ,
610
631
freeKeyValuesIter: JSONAPI_freeKeyValuesIter ,
632
+ // V5 entries
633
+ openKeyWithFlags: JSONAPI_openKey_withFlags ,
611
634
} ;
612
635
613
636
#[ repr( C ) ]
@@ -657,6 +680,13 @@ macro_rules! redis_json_module_export_shared_api {
657
680
str : * mut * mut rawmod:: RedisModuleString
658
681
) -> * const c_void,
659
682
pub freeKeyValuesIter: extern "C" fn ( iter: * mut c_void) ,
683
+ // V5
684
+ pub openKeyWithFlags: extern "C" fn (
685
+ ctx: * mut rawmod:: RedisModuleCtx ,
686
+ key_str: * mut rawmod:: RedisModuleString ,
687
+ flags: c_int,
688
+ ) -> * mut c_void,
689
+
660
690
}
661
691
} ;
662
692
}
0 commit comments