-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_repository.py
286 lines (232 loc) · 8.94 KB
/
redis_repository.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from models import BaseModel, Session, User, Channel, ChannelUser, Message
from redis_mapper import BaseMapper, UserMapper, SessionMapper, ChannelMapper, MessageMapper, ChannelUserMapper
from common_exception import CommonException
from tornado.gen import coroutine
class BaseRepository(object):
model_attributes = ('id', )
model = BaseModel
def __init__(self, connection):
super(BaseRepository, self).__init__()
self.mapper = BaseMapper(connection)
@coroutine
def get_one(self, _id):
data = yield self.mapper.get_one(_id)
return self._create_model(data)
@coroutine
def get_many(self, ids):
data = yield self.mapper.get_many(ids)
return [self._create_model(d) for d in data]
@coroutine
def filter(self, query):
raise NotImplemented()
@coroutine
def delete(self, instance):
yield self.mapper.delete(instance)
@coroutine
def save(self, instance):
_id = yield self.mapper.get_new_id()
if _id is None:
raise CommonException('Save error')
instance.id = _id
values = self._get_model_attributes(instance)
yield self.mapper.save(values)
return instance
def _get_model_attributes(self, instance):
return {attr: getattr(instance, attr) for attr in self.model_attributes}
def _create_model(self, data):
if not data:
return None
return self.model(**data)
class UserRepository(BaseRepository):
model_attributes = ('id', 'name', 'password', 'admin')
model = User
def __init__(self, connection):
super(UserRepository, self).__init__(connection)
self.mapper = UserMapper(connection)
@coroutine
def save(self, user):
lock = yield self.mapper.acquire_lock(user.name)
_id = yield self.mapper.get_index_value(user.name)
if _id:
raise CommonException('Already exist')
_id = yield self.mapper.get_new_id()
if _id is None:
raise CommonException('Save error')
yield self.mapper.set_index_value(_id, user.name)
user.id = _id
values = self._get_model_attributes(user)
yield self.mapper.save(values)
yield self.mapper.release_lock(lock)
return user
@coroutine
def delete(self, user):
yield self.mapper.delete(user)
yield self.mapper.delete_index(user.name)
@coroutine
def filter(self, query):
if 'name' in query:
_id = yield self.mapper.get_index_value(query.get('name'))
result = yield self.get_one(_id)
return result
return None
class SessionRepository(BaseRepository):
model_attributes = ('id', 'key', 'user', 'expires', 'timezone')
model = Session
def __init__(self, connection):
super(SessionRepository, self).__init__(connection)
self.mapper = SessionMapper(connection)
@coroutine
def save(self, session):
lock = yield self.mapper.acquire_lock(session.key)
_id = yield self.mapper.get_index_value(session.key)
if _id:
raise CommonException('Already exist')
_id = yield self.mapper.get_new_id()
if _id is None:
raise CommonException('Save error')
yield self.mapper.set_index_value(_id, session.key)
session.id = _id
values = self._get_model_attributes(session)
values['user'] = session.user.id
yield self.mapper.save(values)
yield self.mapper.release_lock(lock)
return session
@coroutine
def filter(self, query):
if 'key' in query:
_id = yield self.mapper.get_index_value(query.get('key'))
result = yield self.get_one(_id)
return result
return None
@coroutine
def delete(self, session):
yield self.mapper.delete(session)
yield self.mapper.delete_index(session.key)
def _create_model(self, data):
if not data:
return None
data['expires'] = int(data['expires'])
return self.model(**data)
def _get_model_attributes(self, message):
result = {}
for attr in self.model_attributes:
result[attr] = getattr(message, attr)
if attr == 'user':
result[attr] = result[attr].id
return result
class ChannelRepository(BaseRepository):
model_attributes = ('id', 'name')
model = Channel
def __init__(self, connection):
super(ChannelRepository, self).__init__(connection)
self.mapper = ChannelMapper(connection)
@coroutine
def save(self, channel):
lock = yield self.mapper.acquire_lock(channel.name)
_id = yield self.mapper.get_index_value(channel.name)
if _id:
raise CommonException('Already exist')
_id = yield self.mapper.get_new_id()
if _id is None:
raise CommonException('Save error')
yield self.mapper.set_index_value(_id, channel.name)
channel.id = _id
values = self._get_model_attributes(channel)
yield self.mapper.save(values)
yield self.mapper.release_lock(lock)
return channel
@coroutine
def filter(self, query):
if 'name' in query:
_id = yield self.mapper.get_index_value(query.get('name'))
result = yield self.get_one(_id)
return result
return None
@coroutine
def delete(self, channel):
yield self.mapper.delete(channel)
yield self.mapper.delete_index(channel.name)
class MessageRepository(BaseRepository):
model_attributes = ('id', 'text', 'channel', 'user', 'timestamp')
model = Message
def __init__(self, connection):
super(MessageRepository, self).__init__(connection)
self.mapper = MessageMapper(connection)
@coroutine
def save(self, message):
message = yield super(MessageRepository, self).save(message)
yield self.mapper.save_foreign_keys_relations(message, 'channel', 'user')
if message.user:
yield self.mapper.save_foreign_keys_relations(message, 'user', 'channel')
return message
@coroutine
def filter(self, query):
if 'channel' in query:
channel = query['channel']
data = yield self.mapper.get_by_channel(channel)
return [self._create_model(d) for d in data]
return None
def _create_model(self, data):
if data is None:
return None
data['timestamp'] = int(data['timestamp'])
model = self.model(**data)
return model
@coroutine
def publish_message(self, message):
yield self.mapper.publish(message)
def _get_model_attributes(self, message):
result = {}
for attr in self.model_attributes:
result[attr] = getattr(message, attr)
if attr == 'user':
result[attr] = result[attr].id if result[attr] else None
if attr == 'channel':
result[attr] = result[attr].id
return result
class ChannelUserRepository(BaseRepository):
model_attributes = ('id', 'channel', 'user', 'subscribed', 'admin')
model = ChannelUser
def __init__(self, connection):
super(ChannelUserRepository, self).__init__(connection)
self.mapper = ChannelUserMapper(connection)
@coroutine
def save(self, channel_user):
channel_user = yield super(ChannelUserRepository, self).save(channel_user)
yield self.mapper.save_foreign_keys_relations(channel_user, 'channel', 'user')
yield self.mapper.save_foreign_keys_relations(channel_user, 'user', 'channel')
return channel_user
@coroutine
def filter(self, query):
if 'user' in query and 'channel' in query:
user = query['user']
channel = query['channel']
data = yield self.mapper.get_by_user_and_channel(user, channel)
if not data:
return None
data['user'] = user
data['channel'] = channel
return self._create_model(data)
elif 'user' in query:
data = yield self.mapper.get_by_user(query['user'])
return [self._create_model(d) for d in data]
elif 'channel' in query:
data = yield self.mapper.get_by_channel(query['channel'])
return [self._create_model(d) for d in data]
return None
@coroutine
def delete(self, model):
yield self.mapper.delete_foreign_keys_relation(model, 'channel', 'user')
yield self.mapper.delete_foreign_keys_relation(model, 'user', 'channel')
yield super(ChannelUserRepository, self).delete(model)
def _get_model_attributes(self, message):
result = {}
for attr in self.model_attributes:
result[attr] = getattr(message, attr)
if attr == 'user':
result[attr] = result[attr].id
if attr == 'channel':
result[attr] = result[attr].id
return result