forked from aio-libs-abandoned/aioredis-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rejson.py
157 lines (134 loc) · 5.3 KB
/
rejson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json
class JSONCommandsMixin:
"""Set commands mixin.
For commands details see: https://oss.redislabs.com/redisjson/commands/
"""
encoder = json
decoder = json
encode = getattr(encoder, 'dumps')
decode = getattr(decoder, 'loads')
def __init__(self, encoder=None, decoder=None):
if encoder is not None:
self.encoder = encoder
self.encode = getattr(self.encoder, 'dumps')
if decoder is not None:
self.decoder = decoder
self.decode = getattr(self.decoder, 'loads')
async def jset(self, key, path, jsonable, nx=False, xx=False):
"""
Set the JSON value at ``key`` under the ``path`` to ``jsonable``
``nx`` if set to True, set ``value`` only if it does not exist
``xx`` if set to True, set ``value`` only if it exists
"""
pieces = [self.encode(jsonable)]
if nx and xx:
raise Exception('nx and xx are mutually exclusive: use one, the '
'other or neither - but not both')
elif nx:
pieces.append('NX')
elif xx:
pieces.append('XX')
return await self.execute(b'JSON.SET', key, path, *pieces)
async def jget(self, key, *paths):
"""
Get the object stored as a JSON value at ``key``
``paths`` is zero or more paths, and defaults to root path
"""
result = await self.execute('JSON.GET', key, *paths)
return self.decode(result)
async def jdel(self, key, path='.'):
"""
Deletes the JSON value stored at ``key`` under ``path``
"""
return await self.execute(b'JSON.DEL', key, path)
async def jmget(self, *keys, path='.'):
"""
Gets the objects stored as a JSON values under ``path`` from
``keys``
"""
results = await self.execute('JSON.MGET', *keys, path)
return [self.decode(obj) for obj in results]
async def jtype(self, key, path='.'):
"""
Gets the type of the JSON value under ``path`` from ``key``
"""
return await self.execute(b'JSON.TYPE', key, path)
async def jnumincrby(self, key, path, number):
"""
Increments the numeric (integer or floating point) JSON value under
``path`` at ``key`` by the provided ``number``
"""
return await self.execute(b'JSON.NUMINCRBY', key, path, number)
async def jnummultby(self, key, path, number):
"""
Multiplies the numeric (integer or floating point) JSON value under
``path`` at ``key`` with the provided ``number``
"""
return await self.execute(b'JSON.NUMMULTBY', key, path, number)
async def jstrappend(self, key, string, path='.'):
"""
Appends to the string JSON value under ``path`` at ``key`` the
provided ``string``
"""
return await self.execute(b'JSON.STRAPPEND', key, path, self.encode(string))
async def jstrlen(self, key, path='.'):
"""
Returns the length of the string JSON value under ``path`` at
``key``
"""
return await self.execute(b'JSON.STRLEN', key, path)
async def jarrappend(self, key, path, *jsonables):
"""
Appends the objects ``args`` to the array under the ``path` in
``key``
"""
pieces = []
for o in jsonables:
pieces.append(self.encode(o))
return await self.execute(b'JSON.ARRAPPEND', key, path, *pieces)
async def jarrindex(self, key, path, scalar, start=0, stop=-1):
"""
Returns the index of ``scalar`` in the JSON array under ``path`` at
``key``. The search can be limited using the optional inclusive
``start`` and exclusive ``stop`` indices.
"""
return self.execute(b'JSON.ARRINDEX', key, path, self.encode(scalar), start, stop)
async def jarrinsert(self, key, path, index, *args):
"""
Inserts the objects ``args`` to the array at index ``index`` under the
``path` in ``key``
"""
pieces = []
for o in args:
pieces.append(self.encode(o))
return self.execute(b'JSON.ARRINSERT', key, path, index, *pieces)
async def jarrlen(self, key, path='.'):
"""
Returns the length of the array JSON value under ``path`` at
``key``
"""
return self.execute(b'JSON.ARRLEN', key, path)
async def jarrpop(self, key, path='.', index=-1):
"""
Pops the element at ``index`` in the array JSON value under ``path`` at
``key``
"""
return self.execute(b'JSON.ARRPOP', key, path, index)
async def jarrtrim(self, key, path, start, stop):
"""
Trim the array JSON value under ``path`` at ``key`` to the
inclusive range given by ``start`` and ``stop``
"""
return self.execute(b'JSON.ARRTRIM', key, path, start, stop)
async def jobjkeys(self, key, path='.'):
"""
Returns the key names in the dictionary JSON value under ``path`` at
``key``
"""
return self.execute(b'JSON.OBJKEYS', key, path)
async def jobjlen(self, key, path='.'):
"""
Returns the length of the dictionary JSON value under ``path`` at
``key``
"""
return self.execute(b'JSON.OBJLEN', key, path)