commands.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. import os
  2. from json import JSONDecodeError, loads
  3. from typing import Dict, List, Optional, Tuple, Union
  4. from redis.exceptions import DataError
  5. from redis.utils import deprecated_function
  6. from ._util import JsonType
  7. from .decoders import decode_dict_keys
  8. from .path import Path
  9. class JSONCommands:
  10. """json commands."""
  11. def arrappend(
  12. self, name: str, path: Optional[str] = Path.root_path(), *args: List[JsonType]
  13. ) -> List[Union[int, None]]:
  14. """Append the objects ``args`` to the array under the
  15. ``path` in key ``name``.
  16. For more information see `JSON.ARRAPPEND <https://redis.io/commands/json.arrappend>`_..
  17. """ # noqa
  18. pieces = [name, str(path)]
  19. for o in args:
  20. pieces.append(self._encode(o))
  21. return self.execute_command("JSON.ARRAPPEND", *pieces)
  22. def arrindex(
  23. self,
  24. name: str,
  25. path: str,
  26. scalar: int,
  27. start: Optional[int] = None,
  28. stop: Optional[int] = None,
  29. ) -> List[Union[int, None]]:
  30. """
  31. Return the index of ``scalar`` in the JSON array under ``path`` at key
  32. ``name``.
  33. The search can be limited using the optional inclusive ``start``
  34. and exclusive ``stop`` indices.
  35. For more information see `JSON.ARRINDEX <https://redis.io/commands/json.arrindex>`_.
  36. """ # noqa
  37. pieces = [name, str(path), self._encode(scalar)]
  38. if start is not None:
  39. pieces.append(start)
  40. if stop is not None:
  41. pieces.append(stop)
  42. return self.execute_command("JSON.ARRINDEX", *pieces)
  43. def arrinsert(
  44. self, name: str, path: str, index: int, *args: List[JsonType]
  45. ) -> List[Union[int, None]]:
  46. """Insert the objects ``args`` to the array at index ``index``
  47. under the ``path` in key ``name``.
  48. For more information see `JSON.ARRINSERT <https://redis.io/commands/json.arrinsert>`_.
  49. """ # noqa
  50. pieces = [name, str(path), index]
  51. for o in args:
  52. pieces.append(self._encode(o))
  53. return self.execute_command("JSON.ARRINSERT", *pieces)
  54. def arrlen(
  55. self, name: str, path: Optional[str] = Path.root_path()
  56. ) -> List[Union[int, None]]:
  57. """Return the length of the array JSON value under ``path``
  58. at key``name``.
  59. For more information see `JSON.ARRLEN <https://redis.io/commands/json.arrlen>`_.
  60. """ # noqa
  61. return self.execute_command("JSON.ARRLEN", name, str(path))
  62. def arrpop(
  63. self,
  64. name: str,
  65. path: Optional[str] = Path.root_path(),
  66. index: Optional[int] = -1,
  67. ) -> List[Union[str, None]]:
  68. """Pop the element at ``index`` in the array JSON value under
  69. ``path`` at key ``name``.
  70. For more information see `JSON.ARRPOP <https://redis.io/commands/json.arrpop>`_.
  71. """ # noqa
  72. return self.execute_command("JSON.ARRPOP", name, str(path), index)
  73. def arrtrim(
  74. self, name: str, path: str, start: int, stop: int
  75. ) -> List[Union[int, None]]:
  76. """Trim the array JSON value under ``path`` at key ``name`` to the
  77. inclusive range given by ``start`` and ``stop``.
  78. For more information see `JSON.ARRTRIM <https://redis.io/commands/json.arrtrim>`_.
  79. """ # noqa
  80. return self.execute_command("JSON.ARRTRIM", name, str(path), start, stop)
  81. def type(self, name: str, path: Optional[str] = Path.root_path()) -> List[str]:
  82. """Get the type of the JSON value under ``path`` from key ``name``.
  83. For more information see `JSON.TYPE <https://redis.io/commands/json.type>`_.
  84. """ # noqa
  85. return self.execute_command("JSON.TYPE", name, str(path))
  86. def resp(self, name: str, path: Optional[str] = Path.root_path()) -> List:
  87. """Return the JSON value under ``path`` at key ``name``.
  88. For more information see `JSON.RESP <https://redis.io/commands/json.resp>`_.
  89. """ # noqa
  90. return self.execute_command("JSON.RESP", name, str(path))
  91. def objkeys(
  92. self, name: str, path: Optional[str] = Path.root_path()
  93. ) -> List[Union[List[str], None]]:
  94. """Return the key names in the dictionary JSON value under ``path`` at
  95. key ``name``.
  96. For more information see `JSON.OBJKEYS <https://redis.io/commands/json.objkeys>`_.
  97. """ # noqa
  98. return self.execute_command("JSON.OBJKEYS", name, str(path))
  99. def objlen(self, name: str, path: Optional[str] = Path.root_path()) -> int:
  100. """Return the length of the dictionary JSON value under ``path`` at key
  101. ``name``.
  102. For more information see `JSON.OBJLEN <https://redis.io/commands/json.objlen>`_.
  103. """ # noqa
  104. return self.execute_command("JSON.OBJLEN", name, str(path))
  105. def numincrby(self, name: str, path: str, number: int) -> str:
  106. """Increment the numeric (integer or floating point) JSON value under
  107. ``path`` at key ``name`` by the provided ``number``.
  108. For more information see `JSON.NUMINCRBY <https://redis.io/commands/json.numincrby>`_.
  109. """ # noqa
  110. return self.execute_command(
  111. "JSON.NUMINCRBY", name, str(path), self._encode(number)
  112. )
  113. @deprecated_function(version="4.0.0", reason="deprecated since redisjson 1.0.0")
  114. def nummultby(self, name: str, path: str, number: int) -> str:
  115. """Multiply the numeric (integer or floating point) JSON value under
  116. ``path`` at key ``name`` with the provided ``number``.
  117. For more information see `JSON.NUMMULTBY <https://redis.io/commands/json.nummultby>`_.
  118. """ # noqa
  119. return self.execute_command(
  120. "JSON.NUMMULTBY", name, str(path), self._encode(number)
  121. )
  122. def clear(self, name: str, path: Optional[str] = Path.root_path()) -> int:
  123. """Empty arrays and objects (to have zero slots/keys without deleting the
  124. array/object).
  125. Return the count of cleared paths (ignoring non-array and non-objects
  126. paths).
  127. For more information see `JSON.CLEAR <https://redis.io/commands/json.clear>`_.
  128. """ # noqa
  129. return self.execute_command("JSON.CLEAR", name, str(path))
  130. def delete(self, key: str, path: Optional[str] = Path.root_path()) -> int:
  131. """Delete the JSON value stored at key ``key`` under ``path``.
  132. For more information see `JSON.DEL <https://redis.io/commands/json.del>`_.
  133. """
  134. return self.execute_command("JSON.DEL", key, str(path))
  135. # forget is an alias for delete
  136. forget = delete
  137. def get(
  138. self, name: str, *args, no_escape: Optional[bool] = False
  139. ) -> List[JsonType]:
  140. """
  141. Get the object stored as a JSON value at key ``name``.
  142. ``args`` is zero or more paths, and defaults to root path
  143. ```no_escape`` is a boolean flag to add no_escape option to get
  144. non-ascii characters
  145. For more information see `JSON.GET <https://redis.io/commands/json.get>`_.
  146. """ # noqa
  147. pieces = [name]
  148. if no_escape:
  149. pieces.append("noescape")
  150. if len(args) == 0:
  151. pieces.append(Path.root_path())
  152. else:
  153. for p in args:
  154. pieces.append(str(p))
  155. # Handle case where key doesn't exist. The JSONDecoder would raise a
  156. # TypeError exception since it can't decode None
  157. try:
  158. return self.execute_command("JSON.GET", *pieces)
  159. except TypeError:
  160. return None
  161. def mget(self, keys: List[str], path: str) -> List[JsonType]:
  162. """
  163. Get the objects stored as a JSON values under ``path``. ``keys``
  164. is a list of one or more keys.
  165. For more information see `JSON.MGET <https://redis.io/commands/json.mget>`_.
  166. """ # noqa
  167. pieces = []
  168. pieces += keys
  169. pieces.append(str(path))
  170. return self.execute_command("JSON.MGET", *pieces)
  171. def set(
  172. self,
  173. name: str,
  174. path: str,
  175. obj: JsonType,
  176. nx: Optional[bool] = False,
  177. xx: Optional[bool] = False,
  178. decode_keys: Optional[bool] = False,
  179. ) -> Optional[str]:
  180. """
  181. Set the JSON value at key ``name`` under the ``path`` to ``obj``.
  182. ``nx`` if set to True, set ``value`` only if it does not exist.
  183. ``xx`` if set to True, set ``value`` only if it exists.
  184. ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
  185. with utf-8.
  186. For the purpose of using this within a pipeline, this command is also
  187. aliased to JSON.SET.
  188. For more information see `JSON.SET <https://redis.io/commands/json.set>`_.
  189. """
  190. if decode_keys:
  191. obj = decode_dict_keys(obj)
  192. pieces = [name, str(path), self._encode(obj)]
  193. # Handle existential modifiers
  194. if nx and xx:
  195. raise Exception(
  196. "nx and xx are mutually exclusive: use one, the "
  197. "other or neither - but not both"
  198. )
  199. elif nx:
  200. pieces.append("NX")
  201. elif xx:
  202. pieces.append("XX")
  203. return self.execute_command("JSON.SET", *pieces)
  204. def mset(self, triplets: List[Tuple[str, str, JsonType]]) -> Optional[str]:
  205. """
  206. Set the JSON value at key ``name`` under the ``path`` to ``obj``
  207. for one or more keys.
  208. ``triplets`` is a list of one or more triplets of key, path, value.
  209. For the purpose of using this within a pipeline, this command is also
  210. aliased to JSON.MSET.
  211. For more information see `JSON.MSET <https://redis.io/commands/json.mset>`_.
  212. """
  213. pieces = []
  214. for triplet in triplets:
  215. pieces.extend([triplet[0], str(triplet[1]), self._encode(triplet[2])])
  216. return self.execute_command("JSON.MSET", *pieces)
  217. def merge(
  218. self,
  219. name: str,
  220. path: str,
  221. obj: JsonType,
  222. decode_keys: Optional[bool] = False,
  223. ) -> Optional[str]:
  224. """
  225. Merges a given JSON value into matching paths. Consequently, JSON values
  226. at matching paths are updated, deleted, or expanded with new children
  227. ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
  228. with utf-8.
  229. For more information see `JSON.MERGE <https://redis.io/commands/json.merge>`_.
  230. """
  231. if decode_keys:
  232. obj = decode_dict_keys(obj)
  233. pieces = [name, str(path), self._encode(obj)]
  234. return self.execute_command("JSON.MERGE", *pieces)
  235. def set_file(
  236. self,
  237. name: str,
  238. path: str,
  239. file_name: str,
  240. nx: Optional[bool] = False,
  241. xx: Optional[bool] = False,
  242. decode_keys: Optional[bool] = False,
  243. ) -> Optional[str]:
  244. """
  245. Set the JSON value at key ``name`` under the ``path`` to the content
  246. of the json file ``file_name``.
  247. ``nx`` if set to True, set ``value`` only if it does not exist.
  248. ``xx`` if set to True, set ``value`` only if it exists.
  249. ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
  250. with utf-8.
  251. """
  252. with open(file_name, "r") as fp:
  253. file_content = loads(fp.read())
  254. return self.set(name, path, file_content, nx=nx, xx=xx, decode_keys=decode_keys)
  255. def set_path(
  256. self,
  257. json_path: str,
  258. root_folder: str,
  259. nx: Optional[bool] = False,
  260. xx: Optional[bool] = False,
  261. decode_keys: Optional[bool] = False,
  262. ) -> List[Dict[str, bool]]:
  263. """
  264. Iterate over ``root_folder`` and set each JSON file to a value
  265. under ``json_path`` with the file name as the key.
  266. ``nx`` if set to True, set ``value`` only if it does not exist.
  267. ``xx`` if set to True, set ``value`` only if it exists.
  268. ``decode_keys`` If set to True, the keys of ``obj`` will be decoded
  269. with utf-8.
  270. """
  271. set_files_result = {}
  272. for root, dirs, files in os.walk(root_folder):
  273. for file in files:
  274. file_path = os.path.join(root, file)
  275. try:
  276. file_name = file_path.rsplit(".")[0]
  277. self.set_file(
  278. file_name,
  279. json_path,
  280. file_path,
  281. nx=nx,
  282. xx=xx,
  283. decode_keys=decode_keys,
  284. )
  285. set_files_result[file_path] = True
  286. except JSONDecodeError:
  287. set_files_result[file_path] = False
  288. return set_files_result
  289. def strlen(self, name: str, path: Optional[str] = None) -> List[Union[int, None]]:
  290. """Return the length of the string JSON value under ``path`` at key
  291. ``name``.
  292. For more information see `JSON.STRLEN <https://redis.io/commands/json.strlen>`_.
  293. """ # noqa
  294. pieces = [name]
  295. if path is not None:
  296. pieces.append(str(path))
  297. return self.execute_command("JSON.STRLEN", *pieces)
  298. def toggle(
  299. self, name: str, path: Optional[str] = Path.root_path()
  300. ) -> Union[bool, List[Optional[int]]]:
  301. """Toggle boolean value under ``path`` at key ``name``.
  302. returning the new value.
  303. For more information see `JSON.TOGGLE <https://redis.io/commands/json.toggle>`_.
  304. """ # noqa
  305. return self.execute_command("JSON.TOGGLE", name, str(path))
  306. def strappend(
  307. self, name: str, value: str, path: Optional[int] = Path.root_path()
  308. ) -> Union[int, List[Optional[int]]]:
  309. """Append to the string JSON value. If two options are specified after
  310. the key name, the path is determined to be the first. If a single
  311. option is passed, then the root_path (i.e Path.root_path()) is used.
  312. For more information see `JSON.STRAPPEND <https://redis.io/commands/json.strappend>`_.
  313. """ # noqa
  314. pieces = [name, str(path), self._encode(value)]
  315. return self.execute_command("JSON.STRAPPEND", *pieces)
  316. def debug(
  317. self,
  318. subcommand: str,
  319. key: Optional[str] = None,
  320. path: Optional[str] = Path.root_path(),
  321. ) -> Union[int, List[str]]:
  322. """Return the memory usage in bytes of a value under ``path`` from
  323. key ``name``.
  324. For more information see `JSON.DEBUG <https://redis.io/commands/json.debug>`_.
  325. """ # noqa
  326. valid_subcommands = ["MEMORY", "HELP"]
  327. if subcommand not in valid_subcommands:
  328. raise DataError("The only valid subcommands are ", str(valid_subcommands))
  329. pieces = [subcommand]
  330. if subcommand == "MEMORY":
  331. if key is None:
  332. raise DataError("No key specified")
  333. pieces.append(key)
  334. pieces.append(str(path))
  335. return self.execute_command("JSON.DEBUG", *pieces)
  336. @deprecated_function(
  337. version="4.0.0", reason="redisjson-py supported this, call get directly."
  338. )
  339. def jsonget(self, *args, **kwargs):
  340. return self.get(*args, **kwargs)
  341. @deprecated_function(
  342. version="4.0.0", reason="redisjson-py supported this, call get directly."
  343. )
  344. def jsonmget(self, *args, **kwargs):
  345. return self.mget(*args, **kwargs)
  346. @deprecated_function(
  347. version="4.0.0", reason="redisjson-py supported this, call get directly."
  348. )
  349. def jsonset(self, *args, **kwargs):
  350. return self.set(*args, **kwargs)