@@ -40,7 +40,14 @@ fn json_del(ctx: &Context, args: Vec<String>) -> RedisResult {
40
40
41
41
let key = ctx. open_key_writable ( & key) ;
42
42
let deleted = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
43
- Some ( doc) => doc. delete_path ( & path) ?,
43
+ Some ( doc) => {
44
+ if path == "$" {
45
+ key. delete ( ) ?;
46
+ 1
47
+ } else {
48
+ doc. delete_path ( & path) ?
49
+ }
50
+ }
44
51
None => 0 ,
45
52
} ;
46
53
Ok ( deleted. into ( ) )
@@ -74,12 +81,25 @@ fn json_set(ctx: &Context, args: Vec<String>) -> RedisResult {
74
81
( None , Some ( SetOptions :: AlreadyExists ) ) => Ok ( ( ) . into ( ) ) ,
75
82
( None , _) => {
76
83
let doc = RedisJSON :: from_str ( & value) ?;
77
- key. set_value ( & REDIS_JSON_TYPE , doc) ?;
78
- REDIS_OK
84
+ if path == "$" {
85
+ key. set_value ( & REDIS_JSON_TYPE , doc) ?;
86
+ REDIS_OK
87
+ } else {
88
+ Err ( "ERR new objects must be created at the root" . into ( ) )
89
+ }
79
90
}
80
91
}
81
92
}
82
93
94
+ ///
95
+ /// JSON.GET <key>
96
+ /// [INDENT indentation-string]
97
+ /// [NEWLINE line-break-string]
98
+ /// [SPACE space-string]
99
+ /// [NOESCAPE]
100
+ /// [path ...]
101
+ ///
102
+ /// TODO add support for multi path
83
103
fn json_get ( ctx : & Context , args : Vec < String > ) -> RedisResult {
84
104
let mut args = args. into_iter ( ) . skip ( 1 ) ;
85
105
@@ -111,6 +131,9 @@ fn json_get(ctx: &Context, args: Vec<String>) -> RedisResult {
111
131
Ok ( value)
112
132
}
113
133
134
+ ///
135
+ /// JSON.MGET <key> [key ...] <path>
136
+ ///
114
137
fn json_mget ( ctx : & Context , args : Vec < String > ) -> RedisResult {
115
138
if args. len ( ) < 3 {
116
139
return Err ( RedisError :: WrongArity ) ;
@@ -119,7 +142,7 @@ fn json_mget(ctx: &Context, args: Vec<String>) -> RedisResult {
119
142
let path = backward_path ( path. to_string ( ) ) ;
120
143
let mut results: Vec < String > = Vec :: with_capacity ( args. len ( ) - 2 ) ;
121
144
for key in & args[ 1 ..args. len ( ) - 1 ] {
122
- let redis_key = ctx. open_key_writable ( & key) ;
145
+ let redis_key = ctx. open_key ( & key) ;
123
146
match redis_key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
124
147
Some ( doc) => {
125
148
let result = doc. to_string ( & path) ?;
@@ -139,7 +162,7 @@ fn json_str_len(ctx: &Context, args: Vec<String>) -> RedisResult {
139
162
let key = args. next_string ( ) ?;
140
163
let path = backward_path ( args. next_string ( ) ?) ;
141
164
142
- let key = ctx. open_key_writable ( & key) ;
165
+ let key = ctx. open_key ( & key) ;
143
166
144
167
let length = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
145
168
Some ( doc) => doc. str_len ( & path) ?. into ( ) ,
@@ -154,7 +177,7 @@ fn json_type(ctx: &Context, args: Vec<String>) -> RedisResult {
154
177
let key = args. next_string ( ) ?;
155
178
let path = backward_path ( args. next_string ( ) ?) ;
156
179
157
- let key = ctx. open_key_writable ( & key) ;
180
+ let key = ctx. open_key ( & key) ;
158
181
159
182
let value = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
160
183
Some ( doc) => doc. get_type ( & path) ?. into ( ) ,
@@ -214,6 +237,9 @@ fn json_arr_len(ctx: &Context, args: Vec<String>) -> RedisResult {
214
237
json_len ( ctx, args, |doc, path| doc. arr_len ( path) )
215
238
}
216
239
240
+ ///
241
+ /// JSON.ARRPOP <key> [path [index]]
242
+ ///
217
243
fn json_arr_pop ( _ctx : & Context , _args : Vec < String > ) -> RedisResult {
218
244
Err ( "Command was not implemented" . into ( ) )
219
245
}
@@ -234,10 +260,6 @@ fn json_debug(_ctx: &Context, _args: Vec<String>) -> RedisResult {
234
260
Err ( "Command was not implemented" . into ( ) )
235
261
}
236
262
237
- fn json_forget ( _ctx : & Context , _args : Vec < String > ) -> RedisResult {
238
- Err ( "Command was not implemented" . into ( ) )
239
- }
240
-
241
263
fn json_resp ( _ctx : & Context , _args : Vec < String > ) -> RedisResult {
242
264
Err ( "Command was not implemented" . into ( ) )
243
265
}
@@ -249,10 +271,9 @@ fn json_len<F: Fn(&RedisJSON, &String) -> Result<usize, Error>>(
249
271
) -> RedisResult {
250
272
let mut args = args. into_iter ( ) . skip ( 1 ) ;
251
273
let key = args. next_string ( ) ?;
252
- let path = args. next_string ( ) ?;
253
-
254
- let key = ctx. open_key_writable ( & key) ;
274
+ let path = backward_path ( args. next_string ( ) ?) ;
255
275
276
+ let key = ctx. open_key ( & key) ;
256
277
let length = match key. get_value :: < RedisJSON > ( & REDIS_JSON_TYPE ) ? {
257
278
Some ( doc) => fun ( & doc, & path) ?. into ( ) ,
258
279
None => ( ) . into ( ) ,
@@ -261,6 +282,13 @@ fn json_len<F: Fn(&RedisJSON, &String) -> Result<usize, Error>>(
261
282
Ok ( length)
262
283
}
263
284
285
+ fn json_cache_info ( _ctx : & Context , _args : Vec < String > ) -> RedisResult {
286
+ Err ( "Command was not implemented" . into ( ) )
287
+ }
288
+
289
+ fn json_cache_init ( _ctx : & Context , _args : Vec < String > ) -> RedisResult {
290
+ Err ( "Command was not implemented" . into ( ) )
291
+ }
264
292
//////////////////////////////////////////////////////
265
293
266
294
redis_module ! {
@@ -275,21 +303,23 @@ redis_module! {
275
303
[ "json.mget" , json_mget, "" ] ,
276
304
[ "json.set" , json_set, "write" ] ,
277
305
[ "json.type" , json_type, "" ] ,
278
- [ "json.numincrby" , json_num_incrby, "" ] ,
279
- [ "json.nummultby" , json_num_multby, "" ] ,
280
- [ "json.numpowby" , json_num_powby, "" ] ,
281
- [ "json.strappend" , json_str_append, "" ] ,
306
+ [ "json.numincrby" , json_num_incrby, "write " ] ,
307
+ [ "json.nummultby" , json_num_multby, "write " ] ,
308
+ [ "json.numpowby" , json_num_powby, "write " ] ,
309
+ [ "json.strappend" , json_str_append, "write " ] ,
282
310
[ "json.strlen" , json_str_len, "" ] ,
283
- [ "json.arrappend" , json_arr_append, "" ] ,
311
+ [ "json.arrappend" , json_arr_append, "write " ] ,
284
312
[ "json.arrindex" , json_arr_index, "" ] ,
285
- [ "json.arrinsert" , json_arr_insert, "" ] ,
313
+ [ "json.arrinsert" , json_arr_insert, "write " ] ,
286
314
[ "json.arrlen" , json_arr_len, "" ] ,
287
- [ "json.arrpop" , json_arr_pop, "" ] ,
288
- [ "json.arrtrim" , json_arr_trim, "" ] ,
315
+ [ "json.arrpop" , json_arr_pop, "write " ] ,
316
+ [ "json.arrtrim" , json_arr_trim, "write " ] ,
289
317
[ "json.objkeys" , json_obj_keys, "" ] ,
290
318
[ "json.objlen" , json_obj_len, "" ] ,
291
319
[ "json.debug" , json_debug, "" ] ,
292
- [ "json.forget" , json_forget , "" ] ,
320
+ [ "json.forget" , json_del , "write " ] ,
293
321
[ "json.resp" , json_resp, "" ] ,
322
+ [ "json._cacheinfo" , json_cache_info, "" ] ,
323
+ [ "json._cacheinit" , json_cache_init, "write" ] ,
294
324
] ,
295
325
}
0 commit comments