1
- from .path import Path , str_path
1
+ from .path import Path
2
2
from .helpers import decode_dict_keys
3
+ from deprecated import deprecated
4
+ from redis .exceptions import DataError
3
5
4
6
5
7
class JSONCommands :
@@ -9,7 +11,7 @@ def arrappend(self, name, path=Path.rootPath(), *args):
9
11
"""Append the objects ``args`` to the array under the
10
12
``path` in key ``name``.
11
13
"""
12
- pieces = [name , str_path (path )]
14
+ pieces = [name , str (path )]
13
15
for o in args :
14
16
pieces .append (self ._encode (o ))
15
17
return self .execute_command ("JSON.ARRAPPEND" , * pieces )
@@ -23,75 +25,72 @@ def arrindex(self, name, path, scalar, start=0, stop=-1):
23
25
and exclusive ``stop`` indices.
24
26
"""
25
27
return self .execute_command (
26
- "JSON.ARRINDEX" , name , str_path (path ), self ._encode (scalar ),
28
+ "JSON.ARRINDEX" , name , str (path ), self ._encode (scalar ),
27
29
start , stop
28
30
)
29
31
30
32
def arrinsert (self , name , path , index , * args ):
31
33
"""Insert the objects ``args`` to the array at index ``index``
32
34
under the ``path` in key ``name``.
33
35
"""
34
- pieces = [name , str_path (path ), index ]
36
+ pieces = [name , str (path ), index ]
35
37
for o in args :
36
38
pieces .append (self ._encode (o ))
37
39
return self .execute_command ("JSON.ARRINSERT" , * pieces )
38
40
39
- def forget (self , name , path = Path .rootPath ()):
40
- """Alias for jsondel (delete the JSON value)."""
41
- return self .execute_command ("JSON.FORGET" , name , str_path (path ))
42
-
43
41
def arrlen (self , name , path = Path .rootPath ()):
44
42
"""Return the length of the array JSON value under ``path``
45
43
at key``name``.
46
44
"""
47
- return self .execute_command ("JSON.ARRLEN" , name , str_path (path ))
45
+ return self .execute_command ("JSON.ARRLEN" , name , str (path ))
48
46
49
47
def arrpop (self , name , path = Path .rootPath (), index = - 1 ):
50
48
"""Pop the element at ``index`` in the array JSON value under
51
49
``path`` at key ``name``.
52
50
"""
53
- return self .execute_command ("JSON.ARRPOP" , name , str_path (path ), index )
51
+ return self .execute_command ("JSON.ARRPOP" , name , str (path ), index )
54
52
55
53
def arrtrim (self , name , path , start , stop ):
56
54
"""Trim the array JSON value under ``path`` at key ``name`` to the
57
55
inclusive range given by ``start`` and ``stop``.
58
56
"""
59
- return self .execute_command ("JSON.ARRTRIM" , name , str_path (path ),
57
+ return self .execute_command ("JSON.ARRTRIM" , name , str (path ),
60
58
start , stop )
61
59
62
60
def type (self , name , path = Path .rootPath ()):
63
61
"""Get the type of the JSON value under ``path`` from key ``name``."""
64
- return self .execute_command ("JSON.TYPE" , name , str_path (path ))
62
+ return self .execute_command ("JSON.TYPE" , name , str (path ))
65
63
66
64
def resp (self , name , path = Path .rootPath ()):
67
65
"""Return the JSON value under ``path`` at key ``name``."""
68
- return self .execute_command ("JSON.RESP" , name , str_path (path ))
66
+ return self .execute_command ("JSON.RESP" , name , str (path ))
69
67
70
68
def objkeys (self , name , path = Path .rootPath ()):
71
69
"""Return the key names in the dictionary JSON value under ``path`` at
72
70
key ``name``."""
73
- return self .execute_command ("JSON.OBJKEYS" , name , str_path (path ))
71
+ return self .execute_command ("JSON.OBJKEYS" , name , str (path ))
74
72
75
73
def objlen (self , name , path = Path .rootPath ()):
76
74
"""Return the length of the dictionary JSON value under ``path`` at key
77
75
``name``.
78
76
"""
79
- return self .execute_command ("JSON.OBJLEN" , name , str_path (path ))
77
+ return self .execute_command ("JSON.OBJLEN" , name , str (path ))
80
78
81
79
def numincrby (self , name , path , number ):
82
80
"""Increment the numeric (integer or floating point) JSON value under
83
81
``path`` at key ``name`` by the provided ``number``.
84
82
"""
85
83
return self .execute_command (
86
- "JSON.NUMINCRBY" , name , str_path (path ), self ._encode (number )
84
+ "JSON.NUMINCRBY" , name , str (path ), self ._encode (number )
87
85
)
88
86
87
+ @deprecated (version = '4.0.0' , reason = 'deprecated since redisjson 1.0.0' )
89
88
def nummultby (self , name , path , number ):
90
89
"""Multiply the numeric (integer or floating point) JSON value under
91
90
``path`` at key ``name`` with the provided ``number``.
92
91
"""
93
92
return self .execute_command (
94
- "JSON.NUMMULTBY" , name , str_path (path ), self ._encode (number )
93
+ "JSON.NUMMULTBY" , name , str (path ), self ._encode (number )
95
94
)
96
95
97
96
def clear (self , name , path = Path .rootPath ()):
@@ -102,11 +101,14 @@ def clear(self, name, path=Path.rootPath()):
102
101
Return the count of cleared paths (ignoring non-array and non-objects
103
102
paths).
104
103
"""
105
- return self .execute_command ("JSON.CLEAR" , name , str_path (path ))
104
+ return self .execute_command ("JSON.CLEAR" , name , str (path ))
105
+
106
+ def delete (self , key , path = Path .rootPath ()):
107
+ """Delete the JSON value stored at key ``key`` under ``path``."""
108
+ return self .execute_command ("JSON.DEL" , key , str (path ))
106
109
107
- def delete (self , name , path = Path .rootPath ()):
108
- """Delete the JSON value stored at key ``name`` under ``path``."""
109
- return self .execute_command ("JSON.DEL" , name , str_path (path ))
110
+ # forget is an alias for delete
111
+ forget = delete
110
112
111
113
def get (self , name , * args , no_escape = False ):
112
114
"""
@@ -125,7 +127,7 @@ def get(self, name, *args, no_escape=False):
125
127
126
128
else :
127
129
for p in args :
128
- pieces .append (str_path (p ))
130
+ pieces .append (str (p ))
129
131
130
132
# Handle case where key doesn't exist. The JSONDecoder would raise a
131
133
# TypeError exception since it can't decode None
@@ -134,13 +136,14 @@ def get(self, name, *args, no_escape=False):
134
136
except TypeError :
135
137
return None
136
138
137
- def mget (self , path , * args ):
138
- """Get the objects stored as a JSON values under ``path`` from keys
139
- ``args``.
139
+ def mget (self , keys , path ):
140
+ """
141
+ Get the objects stored as a JSON values under ``path``. ``keys``
142
+ is a list of one or more keys.
140
143
"""
141
144
pieces = []
142
- pieces . extend ( args )
143
- pieces .append (str_path (path ))
145
+ pieces += keys
146
+ pieces .append (str (path ))
144
147
return self .execute_command ("JSON.MGET" , * pieces )
145
148
146
149
def set (self , name , path , obj , nx = False , xx = False , decode_keys = False ):
@@ -155,7 +158,7 @@ def set(self, name, path, obj, nx=False, xx=False, decode_keys=False):
155
158
if decode_keys :
156
159
obj = decode_dict_keys (obj )
157
160
158
- pieces = [name , str_path (path ), self ._encode (obj )]
161
+ pieces = [name , str (path ), self ._encode (obj )]
159
162
160
163
# Handle existential modifiers
161
164
if nx and xx :
@@ -169,29 +172,43 @@ def set(self, name, path, obj, nx=False, xx=False, decode_keys=False):
169
172
pieces .append ("XX" )
170
173
return self .execute_command ("JSON.SET" , * pieces )
171
174
172
- def strlen (self , name , path = Path . rootPath () ):
175
+ def strlen (self , name , path = None ):
173
176
"""Return the length of the string JSON value under ``path`` at key
174
177
``name``.
175
178
"""
176
- return self .execute_command ("JSON.STRLEN" , name , str_path (path ))
179
+ pieces = [name ]
180
+ if path is not None :
181
+ pieces .append (str (path ))
182
+ return self .execute_command ("JSON.STRLEN" , * pieces )
177
183
178
184
def toggle (self , name , path = Path .rootPath ()):
179
185
"""Toggle boolean value under ``path`` at key ``name``.
180
186
returning the new value.
181
187
"""
182
- return self .execute_command ("JSON.TOGGLE" , name , str_path (path ))
188
+ return self .execute_command ("JSON.TOGGLE" , name , str (path ))
183
189
184
- def strappend (self , name , string , path = Path .rootPath ()):
185
- """Append to the string JSON value under ``path`` at key ``name``
186
- the provided ``string``.
190
+ def strappend (self , name , value , path = Path .rootPath ()):
191
+ """Append to the string JSON value. If two options are specified after
192
+ the key name, the path is determined to be the first. If a single
193
+ option is passed, then the rootpath (i.e Path.rootPath()) is used.
187
194
"""
195
+ pieces = [name , str (path ), value ]
188
196
return self .execute_command (
189
- "JSON.STRAPPEND" , name , str_path ( path ), self . _encode ( string )
197
+ "JSON.STRAPPEND" , * pieces
190
198
)
191
199
192
- def debug (self , name , path = Path .rootPath ()):
200
+ def debug (self , subcommand , key = None , path = Path .rootPath ()):
193
201
"""Return the memory usage in bytes of a value under ``path`` from
194
202
key ``name``.
195
203
"""
196
- return self .execute_command ("JSON.DEBUG" , "MEMORY" ,
197
- name , str_path (path ))
204
+ valid_subcommands = ["MEMORY" , "HELP" ]
205
+ if subcommand not in valid_subcommands :
206
+ raise DataError ("The only valid subcommands are " ,
207
+ str (valid_subcommands ))
208
+ pieces = [subcommand ]
209
+ if subcommand == "MEMORY" :
210
+ if key is None :
211
+ raise DataError ("No key specified" )
212
+ pieces .append (key )
213
+ pieces .append (str (path ))
214
+ return self .execute_command ("JSON.DEBUG" , * pieces )
0 commit comments