diff options
Diffstat (limited to 'redis/commands/json/commands.py')
-rw-r--r-- | redis/commands/json/commands.py | 93 |
1 files changed, 55 insertions, 38 deletions
diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py index 2f8039f..fb00e22 100644 --- a/redis/commands/json/commands.py +++ b/redis/commands/json/commands.py @@ -1,5 +1,7 @@ -from .path import Path, str_path +from .path import Path from .helpers import decode_dict_keys +from deprecated import deprecated +from redis.exceptions import DataError class JSONCommands: @@ -9,7 +11,7 @@ class JSONCommands: """Append the objects ``args`` to the array under the ``path` in key ``name``. """ - pieces = [name, str_path(path)] + pieces = [name, str(path)] for o in args: pieces.append(self._encode(o)) return self.execute_command("JSON.ARRAPPEND", *pieces) @@ -23,7 +25,7 @@ class JSONCommands: and exclusive ``stop`` indices. """ return self.execute_command( - "JSON.ARRINDEX", name, str_path(path), self._encode(scalar), + "JSON.ARRINDEX", name, str(path), self._encode(scalar), start, stop ) @@ -31,67 +33,64 @@ class JSONCommands: """Insert the objects ``args`` to the array at index ``index`` under the ``path` in key ``name``. """ - pieces = [name, str_path(path), index] + pieces = [name, str(path), index] for o in args: pieces.append(self._encode(o)) return self.execute_command("JSON.ARRINSERT", *pieces) - def forget(self, name, path=Path.rootPath()): - """Alias for jsondel (delete the JSON value).""" - return self.execute_command("JSON.FORGET", name, str_path(path)) - def arrlen(self, name, path=Path.rootPath()): """Return the length of the array JSON value under ``path`` at key``name``. """ - return self.execute_command("JSON.ARRLEN", name, str_path(path)) + return self.execute_command("JSON.ARRLEN", name, str(path)) def arrpop(self, name, path=Path.rootPath(), index=-1): """Pop the element at ``index`` in the array JSON value under ``path`` at key ``name``. """ - return self.execute_command("JSON.ARRPOP", name, str_path(path), index) + return self.execute_command("JSON.ARRPOP", name, str(path), index) def arrtrim(self, name, path, start, stop): """Trim the array JSON value under ``path`` at key ``name`` to the inclusive range given by ``start`` and ``stop``. """ - return self.execute_command("JSON.ARRTRIM", name, str_path(path), + return self.execute_command("JSON.ARRTRIM", name, str(path), start, stop) def type(self, name, path=Path.rootPath()): """Get the type of the JSON value under ``path`` from key ``name``.""" - return self.execute_command("JSON.TYPE", name, str_path(path)) + return self.execute_command("JSON.TYPE", name, str(path)) def resp(self, name, path=Path.rootPath()): """Return the JSON value under ``path`` at key ``name``.""" - return self.execute_command("JSON.RESP", name, str_path(path)) + return self.execute_command("JSON.RESP", name, str(path)) def objkeys(self, name, path=Path.rootPath()): """Return the key names in the dictionary JSON value under ``path`` at key ``name``.""" - return self.execute_command("JSON.OBJKEYS", name, str_path(path)) + return self.execute_command("JSON.OBJKEYS", name, str(path)) def objlen(self, name, path=Path.rootPath()): """Return the length of the dictionary JSON value under ``path`` at key ``name``. """ - return self.execute_command("JSON.OBJLEN", name, str_path(path)) + return self.execute_command("JSON.OBJLEN", name, str(path)) def numincrby(self, name, path, number): """Increment the numeric (integer or floating point) JSON value under ``path`` at key ``name`` by the provided ``number``. """ return self.execute_command( - "JSON.NUMINCRBY", name, str_path(path), self._encode(number) + "JSON.NUMINCRBY", name, str(path), self._encode(number) ) + @deprecated(version='4.0.0', reason='deprecated since redisjson 1.0.0') def nummultby(self, name, path, number): """Multiply the numeric (integer or floating point) JSON value under ``path`` at key ``name`` with the provided ``number``. """ return self.execute_command( - "JSON.NUMMULTBY", name, str_path(path), self._encode(number) + "JSON.NUMMULTBY", name, str(path), self._encode(number) ) def clear(self, name, path=Path.rootPath()): @@ -102,11 +101,14 @@ class JSONCommands: Return the count of cleared paths (ignoring non-array and non-objects paths). """ - return self.execute_command("JSON.CLEAR", name, str_path(path)) + return self.execute_command("JSON.CLEAR", name, str(path)) + + def delete(self, key, path=Path.rootPath()): + """Delete the JSON value stored at key ``key`` under ``path``.""" + return self.execute_command("JSON.DEL", key, str(path)) - def delete(self, name, path=Path.rootPath()): - """Delete the JSON value stored at key ``name`` under ``path``.""" - return self.execute_command("JSON.DEL", name, str_path(path)) + # forget is an alias for delete + forget = delete def get(self, name, *args, no_escape=False): """ @@ -125,7 +127,7 @@ class JSONCommands: else: for p in args: - pieces.append(str_path(p)) + pieces.append(str(p)) # Handle case where key doesn't exist. The JSONDecoder would raise a # TypeError exception since it can't decode None @@ -134,13 +136,14 @@ class JSONCommands: except TypeError: return None - def mget(self, path, *args): - """Get the objects stored as a JSON values under ``path`` from keys - ``args``. + def mget(self, keys, path): + """ + Get the objects stored as a JSON values under ``path``. ``keys`` + is a list of one or more keys. """ pieces = [] - pieces.extend(args) - pieces.append(str_path(path)) + pieces += keys + pieces.append(str(path)) return self.execute_command("JSON.MGET", *pieces) def set(self, name, path, obj, nx=False, xx=False, decode_keys=False): @@ -155,7 +158,7 @@ class JSONCommands: if decode_keys: obj = decode_dict_keys(obj) - pieces = [name, str_path(path), self._encode(obj)] + pieces = [name, str(path), self._encode(obj)] # Handle existential modifiers if nx and xx: @@ -169,29 +172,43 @@ class JSONCommands: pieces.append("XX") return self.execute_command("JSON.SET", *pieces) - def strlen(self, name, path=Path.rootPath()): + def strlen(self, name, path=None): """Return the length of the string JSON value under ``path`` at key ``name``. """ - return self.execute_command("JSON.STRLEN", name, str_path(path)) + pieces = [name] + if path is not None: + pieces.append(str(path)) + return self.execute_command("JSON.STRLEN", *pieces) def toggle(self, name, path=Path.rootPath()): """Toggle boolean value under ``path`` at key ``name``. returning the new value. """ - return self.execute_command("JSON.TOGGLE", name, str_path(path)) + return self.execute_command("JSON.TOGGLE", name, str(path)) - def strappend(self, name, string, path=Path.rootPath()): - """Append to the string JSON value under ``path`` at key ``name`` - the provided ``string``. + def strappend(self, name, value, path=Path.rootPath()): + """Append to the string JSON value. If two options are specified after + the key name, the path is determined to be the first. If a single + option is passed, then the rootpath (i.e Path.rootPath()) is used. """ + pieces = [name, str(path), value] return self.execute_command( - "JSON.STRAPPEND", name, str_path(path), self._encode(string) + "JSON.STRAPPEND", *pieces ) - def debug(self, name, path=Path.rootPath()): + def debug(self, subcommand, key=None, path=Path.rootPath()): """Return the memory usage in bytes of a value under ``path`` from key ``name``. """ - return self.execute_command("JSON.DEBUG", "MEMORY", - name, str_path(path)) + valid_subcommands = ["MEMORY", "HELP"] + if subcommand not in valid_subcommands: + raise DataError("The only valid subcommands are ", + str(valid_subcommands)) + pieces = [subcommand] + if subcommand == "MEMORY": + if key is None: + raise DataError("No key specified") + pieces.append(key) + pieces.append(str(path)) + return self.execute_command("JSON.DEBUG", *pieces) |