forked from mohd-akram/item.tf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.py
239 lines (177 loc) · 6.83 KB
/
store.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import itertools
from collections import OrderedDict
from collections.abc import AsyncIterator, Hashable, Sized, Mapping
import rapidjson
from aioredis import Redis as _Redis
def dumps(obj):
return rapidjson.dumps(obj)
def mdumps(d):
return {k: dumps(v) for k, v in d.items()}
def loads(s):
if s is not None:
return rapidjson.loads(s)
class Redis(_Redis):
async def get(self, *args, **kwargs):
return loads(await super().get(*args, **kwargs))
async def set(self, key, value):
return await super().set(key, dumps(value))
async def setex(self, key, time, value):
return await super().setex(key, time, dumps(value))
async def mset_dict(self, map_):
return await super().mset(
*itertools.chain.from_iterable(mdumps(map_).items())
)
async def hget(self, *args, **kwargs):
return loads(await super().hget(*args, **kwargs))
async def hgetall(self, key):
self._hgetall = getattr(
self, '_hgetall',
await self.script_load(
"""
local hgetall = function (key)
local data = redis.call('HGETALL', key)
local hash = {}
for i = 1, #data, 2 do
hash[data[i]] = cjson.decode(data[i + 1])
end
return hash
end
local hashes = {}
for i = 1, #KEYS do
hashes[i] = hgetall(KEYS[i])
end
return cjson.encode(hashes)
"""
)
)
if type(key) is str:
return {k.decode(): loads(v)
for k, v in (await super().hgetall(key)).items()}
else:
return loads(await self.evalsha(self._hgetall, keys=key))
async def hset(self, key, field, value):
return await super().hset(key, field, dumps(value))
def hmset_dict(self, key, map_):
return super().hmset_dict(key, mdumps(map_))
async def hkeys(self, *args, **kwargs):
return (f.decode() for f in await super().hkeys(*args, **kwargs))
async def smembers(self, *args, **kwargs):
return (m.decode() for m in await super().smembers(*args, **kwargs))
async def srandmember(self, *args, **kwargs):
members = await super().srandmember(*args, **kwargs)
if type(members) is list:
return [member.decode() for member in members]
else:
return members.decode()
async def lrange(self, *args, **kwargs):
return (e.decode() for e in await super().lrange(*args, **kwargs))
async def delete_all(self, match, count=100):
cursor = None
while cursor != 0:
if cursor is None:
cursor = 0
cursor, keys = await self.scan(cursor, match, count)
if keys:
await self.delete(*keys)
def Hash(self, *args, **kwargs):
return Hash(self, *args, **kwargs)
def Hashes(self, *args, **kwargs):
return Hashes(self, *args, **kwargs)
def HashSet(self, *args, **kwargs):
return HashSet(self, *args, **kwargs)
async def SearchHashSet(self, *args, **kwargs):
return await SearchHashSet.create(self, *args, **kwargs)
class Hash(Hashable, Mapping):
def __init__(self, redis, key):
self.r = redis
self.key = key
def __hash__(self):
return hash(self.key)
async def __getitem__(self, field):
return await self.r.hget(self.key, field)
async def contains(self, field):
return await self.r.hexists(self.key, field)
async def __iter__(self):
return await self.r.hkeys(self.key)
async def __len__(self):
return await self.r.hlen(self.key)
async def todict(self):
return await self.r.hgetall(self.key)
class Hashes(AsyncIterator, Sized):
"""This class enables buffered iteration over a list of hashes.
The hashes are returned as regular dicts."""
def __init__(self, redis, keys, bufsize=100):
self.r = redis
self.keys = keys
self.bufsize = bufsize
self.i = -1
async def __aiter__(self):
return self
async def __anext__(self):
self.i += 1
if self.i >= len(self.keys):
raise StopAsyncIteration
if self.i % self.bufsize == 0:
self.results = await self.r.hgetall(
self.keys[self.i:self.i + self.bufsize]
)
return self.results[self.i % self.bufsize]
def __len__(self):
return len(self.keys)
class HashSet(Mapping):
def __init__(self, redis, key, tokey, sortkey=None):
self.r = redis
self.key = key
self.tokey = tokey
self.sortkey = sortkey
def __getitem__(self, member):
return Hash(self.r, self.tokey(member))
async def __contains__(self, member):
return await self.r.sismember(self.key, member)
async def __iter__(self):
if self.sortkey:
return sorted(await self.r.smembers(self.key), key=self.sortkey)
else:
return await self.r.smembers(self.key)
async def __len__(self):
return await self.r.scard(self.key)
class SearchHashSet(HashSet):
"""This class optimizes search on a set of hashes by first obtaining
and storing all the fields necessary for search, allowing faster access
to them. Other fields are accessed as required from the store."""
class SearchHash(Hash):
def __init__(self, key, member, set_):
super().__init__(set_.r, key)
self.member = member
self.set_ = set_
def __getitem__(self, field):
if field in self.set_.fields:
return self.set_._gethashfield(self, field)
else:
return super().__getitem__(field)
@classmethod
async def create(cls, redis, key, tokey, fields, sortkey=None):
self = cls(redis, key, tokey)
self.fields = fields
get = ('#',) + tuple('{}->{}'.format(tokey('*'), f) for f in fields)
self.result = tuple(await self.r.sort(key, *get))
hashes = []
for i in range(0, len(self.result), len(fields) + 1):
hashes.append((self.result[i].decode(), i + 1))
if sortkey:
hashes.sort(key=lambda k: sortkey(k[0]))
self.hashes = OrderedDict(hashes)
else:
self.hashes = dict(hashes)
return self
def __getitem__(self, member):
return self.SearchHash(self.tokey(member), str(member), self)
def __contains__(self, member):
return str(member) in self.hashes
def __iter__(self):
yield from self.hashes
def __len__(self):
return len(self.hashes)
def _gethashfield(self, hash_, field):
i = self.hashes[hash_.member] + self.fields.index(field)
return loads(self.result[i])