1
1
#[ macro_use]
2
2
extern crate redis_module;
3
3
4
- use redis_module:: native_types:: RedisType ;
5
4
use redis_module:: raw:: RedisModuleTypeMethods ;
5
+ use redis_module:: { native_types:: RedisType , NotifyEvent } ;
6
6
use redis_module:: { raw as rawmod, NextArg } ;
7
7
use redis_module:: { Context , RedisError , RedisResult , RedisValue , REDIS_OK } ;
8
8
use serde_json:: { Number , Value } ;
@@ -41,6 +41,11 @@ static REDIS_JSON_TYPE: RedisType = RedisType::new(
41
41
aux_load : None ,
42
42
aux_save : None ,
43
43
aux_save_triggers : rawmod:: Aux :: Before as i32 ,
44
+
45
+ free_effort : None ,
46
+ unlink : None ,
47
+ copy : None ,
48
+ defrag : None ,
44
49
} ,
45
50
) ;
46
51
@@ -69,15 +74,16 @@ fn json_del(ctx: &Context, args: Vec<String>) -> RedisResult {
69
74
let key = args. next_string ( ) ?;
70
75
let path = backwards_compat_path ( args. next_string ( ) ?) ;
71
76
72
- let key = ctx. open_key_writable ( & key) ;
73
- let deleted = match key . get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
77
+ let redis_key = ctx. open_key_writable ( & key) ;
78
+ let deleted = match redis_key . get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
74
79
Some ( doc) => {
75
80
let res = if path == "$" {
76
- key . delete ( ) ?;
81
+ redis_key . delete ( ) ?;
77
82
1
78
83
} else {
79
84
doc. delete_path ( & path) ?
80
85
} ;
86
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , "json_del" , key. as_str ( ) ) ;
81
87
ctx. replicate_verbatim ( ) ;
82
88
res
83
89
}
@@ -116,6 +122,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
116
122
match ( current, set_option) {
117
123
( Some ( ref mut doc) , ref op) => {
118
124
if doc. set_value ( & value, & path, op, format) ? {
125
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , "json_set" , key. as_str ( ) ) ;
119
126
ctx. replicate_verbatim ( ) ;
120
127
REDIS_OK
121
128
} else {
@@ -127,6 +134,7 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
127
134
let doc = RedisJSON :: from_str ( & value, format) ?;
128
135
if path == "$" {
129
136
redis_key. set_value ( & REDIS_JSON_TYPE , doc) ?;
137
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , "json_set" , key. as_str ( ) ) ;
130
138
ctx. replicate_verbatim ( ) ;
131
139
REDIS_OK
132
140
} else {
@@ -256,24 +264,36 @@ fn json_type(ctx: &Context, args: Vec<String>) -> RedisResult {
256
264
/// JSON.NUMINCRBY <key> <path> <number>
257
265
///
258
266
fn json_num_incrby ( ctx : & Context , args : Vec < String > ) -> RedisResult {
259
- json_num_op ( ctx, args, |i1, i2| i1 + i2, |f1, f2| f1 + f2)
267
+ json_num_op ( ctx, "json_incrby" , args, |i1, i2| i1 + i2, |f1, f2| f1 + f2)
260
268
}
261
269
262
270
///
263
271
/// JSON.NUMMULTBY <key> <path> <number>
264
272
///
265
273
fn json_num_multby ( ctx : & Context , args : Vec < String > ) -> RedisResult {
266
- json_num_op ( ctx, args, |i1, i2| i1 * i2, |f1, f2| f1 * f2)
274
+ json_num_op ( ctx, "json_multby" , args, |i1, i2| i1 * i2, |f1, f2| f1 * f2)
267
275
}
268
276
269
277
///
270
278
/// JSON.NUMPOWBY <key> <path> <number>
271
279
///
272
280
fn json_num_powby ( ctx : & Context , args : Vec < String > ) -> RedisResult {
273
- json_num_op ( ctx, args, |i1, i2| i1. pow ( i2 as u32 ) , |f1, f2| f1. powf ( f2) )
281
+ json_num_op (
282
+ ctx,
283
+ "json_numpowby" ,
284
+ args,
285
+ |i1, i2| i1. pow ( i2 as u32 ) ,
286
+ |f1, f2| f1. powf ( f2) ,
287
+ )
274
288
}
275
289
276
- fn json_num_op < I , F > ( ctx : & Context , args : Vec < String > , op_i64 : I , op_f64 : F ) -> RedisResult
290
+ fn json_num_op < I , F > (
291
+ ctx : & Context ,
292
+ cmd : & str ,
293
+ args : Vec < String > ,
294
+ op_i64 : I ,
295
+ op_f64 : F ,
296
+ ) -> RedisResult
277
297
where
278
298
I : Fn ( i64 , i64 ) -> i64 ,
279
299
F : Fn ( f64 , f64 ) -> f64 ,
@@ -284,15 +304,17 @@ where
284
304
let path = backwards_compat_path ( args. next_string ( ) ?) ;
285
305
let number = args. next_string ( ) ?;
286
306
287
- let key = ctx. open_key_writable ( & key) ;
307
+ let redis_key = ctx. open_key_writable ( & key) ;
288
308
289
- key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
309
+ redis_key
310
+ . get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
290
311
. ok_or_else ( RedisError :: nonexistent_key)
291
312
. and_then ( |doc| {
292
313
doc. value_op ( & path, |value| {
293
314
do_json_num_op ( & number, value, & op_i64, & op_f64)
294
315
} )
295
316
. map ( |v| {
317
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , cmd, key. as_str ( ) ) ;
296
318
ctx. replicate_verbatim ( ) ;
297
319
v. to_string ( ) . into ( )
298
320
} )
@@ -360,13 +382,15 @@ fn json_str_append(ctx: &Context, args: Vec<String>) -> RedisResult {
360
382
json = path_or_json;
361
383
}
362
384
363
- let key = ctx. open_key_writable ( & key) ;
385
+ let redis_key = ctx. open_key_writable ( & key) ;
364
386
365
- key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
387
+ redis_key
388
+ . get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
366
389
. ok_or_else ( RedisError :: nonexistent_key)
367
390
. and_then ( |doc| {
368
391
doc. value_op ( & path, |value| do_json_str_append ( & json, value) )
369
392
. map ( |v| {
393
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , "json_strappend" , key. as_str ( ) ) ;
370
394
ctx. replicate_verbatim ( ) ;
371
395
v. as_str ( ) . map_or ( usize:: MAX , |v| v. len ( ) ) . into ( )
372
396
} )
@@ -401,13 +425,15 @@ fn json_arr_append(ctx: &Context, args: Vec<String>) -> RedisResult {
401
425
// We require at least one JSON item to append
402
426
args. peek ( ) . ok_or ( RedisError :: WrongArity ) ?;
403
427
404
- let key = ctx. open_key_writable ( & key) ;
428
+ let redis_key = ctx. open_key_writable ( & key) ;
405
429
406
- key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
430
+ redis_key
431
+ . get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
407
432
. ok_or_else ( RedisError :: nonexistent_key)
408
433
. and_then ( |doc| {
409
434
doc. value_op ( & path, |value| do_json_arr_append ( args. clone ( ) , value) )
410
435
. map ( |v| {
436
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , "json_arrappend" , key. as_str ( ) ) ;
411
437
ctx. replicate_verbatim ( ) ;
412
438
v. as_array ( ) . map_or ( usize:: MAX , |v| v. len ( ) ) . into ( )
413
439
} )
@@ -470,15 +496,17 @@ fn json_arr_insert(ctx: &Context, args: Vec<String>) -> RedisResult {
470
496
// We require at least one JSON item to append
471
497
args. peek ( ) . ok_or ( RedisError :: WrongArity ) ?;
472
498
473
- let key = ctx. open_key_writable ( & key) ;
499
+ let redis_key = ctx. open_key_writable ( & key) ;
474
500
475
- key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
501
+ redis_key
502
+ . get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
476
503
. ok_or_else ( RedisError :: nonexistent_key)
477
504
. and_then ( |doc| {
478
505
doc. value_op ( & path, |value| {
479
506
do_json_arr_insert ( args. clone ( ) , index, value)
480
507
} )
481
508
. map ( |v| {
509
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , "json_arrinsert" , key. as_str ( ) ) ;
482
510
ctx. replicate_verbatim ( ) ;
483
511
v. as_array ( ) . map_or ( usize:: MAX , |v| v. len ( ) ) . into ( )
484
512
} )
@@ -537,14 +565,16 @@ fn json_arr_pop(ctx: &Context, args: Vec<String>) -> RedisResult {
537
565
} )
538
566
. unwrap_or ( ( "$" . to_string ( ) , i64:: MAX ) ) ;
539
567
540
- let key = ctx. open_key_writable ( & key) ;
568
+ let redis_key = ctx. open_key_writable ( & key) ;
541
569
let mut res = Value :: Null ;
542
570
543
- key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
571
+ redis_key
572
+ . get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
544
573
. ok_or_else ( RedisError :: nonexistent_key)
545
574
. and_then ( |doc| {
546
575
doc. value_op ( & path, |value| do_json_arr_pop ( index, & mut res, value) )
547
576
. map ( |v| {
577
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , "json_arrpop" , key. as_str ( ) ) ;
548
578
ctx. replicate_verbatim ( ) ;
549
579
v
550
580
} )
@@ -587,13 +617,15 @@ fn json_arr_trim(ctx: &Context, args: Vec<String>) -> RedisResult {
587
617
let start = args. next_i64 ( ) ?;
588
618
let stop = args. next_i64 ( ) ?;
589
619
590
- let key = ctx. open_key_writable ( & key) ;
620
+ let redis_key = ctx. open_key_writable ( & key) ;
591
621
592
- key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
622
+ redis_key
623
+ . get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ?
593
624
. ok_or_else ( RedisError :: nonexistent_key)
594
625
. and_then ( |doc| {
595
626
doc. value_op ( & path, |value| do_json_arr_trim ( start, stop, & value) )
596
627
. map ( |v| {
628
+ ctx. notify_keyspace_event ( NotifyEvent :: MODULE , "json_arrtrim" , key. as_str ( ) ) ;
597
629
ctx. replicate_verbatim ( ) ;
598
630
v. as_array ( ) . map_or ( usize:: MAX , |v| v. len ( ) ) . into ( )
599
631
} )
0 commit comments