Public » MiniRedis Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

initial commit

Changeset 7ac9eb03f32f

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to 3 files · Browse files at 7ac9eb03f32f Diff from another changeset...

Change 1 of 1 Show Entire File .gitignore Stacked
 
 
 
1
Change 1 of 1 Show Entire File README Stacked
 
 
 
 
 
 
 
1
2
3
4
5
@@ -0,0 +1,5 @@
+miniredis is an extremely minimal server that supports exactly enough redis +to power splined. It's currently used in the development version of the +Kiln backend when Kiln runs on Windows computers. While I'm happy to accept +patches making this closer to a real Redis alternative, I currently have +no plans to implement the entire Redis suite.
Change 1 of 1 Show Entire File miniredis.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
@@ -0,0 +1,312 @@
+#!/usr/bin/env python + +import getopt +import re +import select +import socket +import sys +import threading + +class RedisError(object): + def __init__(self, message): + self.message = message + + def __repr__(self): + return '<RedisError(%s)>' % self.message + +class RedisBadValueError(RedisError): + def __init__(self): + super(RedisBadValueError, self).__init__('Operation against a key holding the wrong kind of value') + +class RedisConstant(object): + def __init__(self, type): + self.type = type + + def __repr__(self): + return '<RedisConstant(%s)>' % self.type + +EMPTY_SCALAR = RedisConstant('EmptyScalar') +EMPTY_ARRAY = RedisConstant('EmptyArray') + + +class RedisClient(object): + def __init__(self, socket): + self.socket = socket + self.wfile = socket.makefile('wb') + self.rfile = socket.makefile('rb') + self.db = None + self.table = None + +class MiniRedis(threading.Thread): + def __init__(self, host='127.0.0.1', port=56784, logging=False): + super(MiniRedis, self).__init__() + self.host = host + self.port = port + self.logging = logging + self.halt = True + self.tables = {} + self.clients = {} + + self.wrappers = {'set': self.handle_set, + 'get': self.handle_get, + 'incr': self.handle_incr, + 'incrby': self.handle_incrby, + 'del': self.handle_del, + 'keys': self.handle_keys, + 'rpop': self.handle_rpop, + 'lpush': self.handle_lpush, + 'flushdb': self.handle_flushdb, + 'select': self.handle_select, + 'quit': self.handle_quit, + 'shutdown': self.handle_shutdown} + + self.dispatch = {'set': self.set, + 'get': self.get, + 'incr': self.incr, + 'incrby': self.incr, + 'del': self.delete, + 'keys': self.keys, + 'rpop': self.rpop, + 'lpush': self.lpush, + 'flushdb': self.flushdb, + 'select': self.select, + 'quit': self.quit, + 'shutdown': self.shutdown} + + def log(self, client, s): + if self.logging: + print '%s:%s: %s' % (client.socket.getpeername() + (s,)) + + def set(self, client, key, data): + client.table[key] = data + self.log(client, 'SET %s -> %s' % (key, data)) + return True + + def get(self, client, key): + data = client.table.get(key, None) + if isinstance(data, list): + return RedisBadValueError() + if data != None: + data = str(data) + else: + data = EMPTY_SCALAR + self.log(client, 'GET %s -> %s' % (key, data)) + return data + + def delete(self, client, key): + self.log(client, 'DEL %s' % key) + if key not in client.table: + return 0 + del client.table[key] + return 1 + + def lpush(self, client, key, data): + if key not in client.table: + client.table[key] = [] + elif not isinstance(client.table[key], list): + return RedisBadValueError() + client.table[key].insert(0, data) + self.log(client, 'LPUSH %s %s' % (key, data)) + return True + + def rpop(self, client, key): + if key not in client.table: + return EMPTY_SCALAR + if not isinstance(client.table[key], list): + return RedisBadValueError() + if len(client.table[key]) > 0: + data = client.table[key].pop() + else: + data = EMPTY_SCALAR + self.log(client, 'LPOP %s -> %s' % (key, data)) + return data + + def keys(self, client, pattern): + r = re.compile(pattern.replace('*', '.*')) + self.log(client, 'KEYS %s' % pattern) + return ' '.join(k for k in client.table.keys() if r.search(k)) + + def incr(self, client, key, by=1): + try: + client.table[key] = int(client.table[key]) + client.table[key] += int(by) + except (KeyError, TypeError, ValueError): + client.table[key] = 1 + self.log(client, 'INCRBY %s %s -> %s' % (key, by, client.table[key])) + return client.table[key] + + def flushdb(self, client): + self.log(client, 'FLUSHDB') + client.table.clear() + return True + + def select(self, client, db): + db = int(db) + if db not in self.tables: + self.tables[db] = {} + client.db = db + client.table = self.tables[db] + self.log(client, 'SELECT %s' % db) + return True + + def quit(self, client): + self.log(client, 'QUIT') + client.socket.shutdown(socket.SHUT_RDWR) + client.socket.close() + del self.clients[client.socket] + return False + + def shutdown(self, client): + self.log(client, 'SHUTDOWN') + self.halt = True + return True + + def handle_set(self, client, line): + key, length = line.split() + data = client.rfile.read(int(length)) + client.rfile.read(2) # throw out newline + return self.set(key, client, data) + + def handle_get(self, client, line): + key = line.strip() + return self.get(client, key) + + def handle_del(self, client, line): + key = line.strip() + return self.delete(client, key) + + def handle_lpush(self, client, line): + key, length = line.split() + data = client.rfile.read(int(length)) + client.rfile.read(2) # throw out newline + return self.lpush(client, key, data) + + def handle_rpop(self, client, line): + key = line.strip() + return self.rpop(client, key) + + def handle_keys(self, client, line): + pattern = line.strip() + return self.keys(client, pattern) + + def handle_incr(self, client, line): + key = line.strip() + return self.incr(client, key) + + def handle_incrby(self, client, line): + key, by = line.split() + return self.incr(client, key, by) + + def handle_flushdb(self, client, line): + return self.flushdb(client) + + def handle_select(self, client, line): + db = line.strip() + return self.select(client, db) + + def handle_quit(self, client, line): + return self.quit(client) + + def handle_shutdown(self, client, line): + return self.shutdown(client) + + def handle_normal(self, client, line): + command, _, rest = line.partition(' ') + return self.wrappers[command.strip().lower()](client, rest) + + def handle_multibulk(self, client, line): + items = int(line[1:].strip()) + args = [] + for x in xrange(0, items): + length = int(client.rfile.readline().strip()[1:]) + args.append(client.rfile.read(length)) + client.rfile.read(2) # throw out newline + command = args[0].lower() + return self.dispatch[command](client, *args[1:]) + + def handle(self, client): + line = client.rfile.readline() + if not line: + self.log(client, 'client disconnected') + del self.clients[client.socket] + client.socket.close() + return + if line[0] == '*': + o = self.handle_multibulk(client, line) + else: + o = self.handle_normal(client, line) + self.dump(client, o) + + def dump(self, client, o): + nl = '\r\n' + if isinstance(o, bool): + if o: + client.wfile.write('+OK\r\n') + # Show nothing for a false return; that means be quiet + elif o == EMPTY_SCALAR: + client.wfile.write('$-1\r\n') + elif o == EMPTY_ARRAY: + client.wfile.write('*-1\r\n') + elif isinstance(o, int): + client.wfile.write(':' + str(o) + nl) + elif isinstance(o, str): + client.wfile.write('$' + str(len(o)) + nl) + client.wfile.write(o + nl) + elif isinstance(o, list): + client.wfile.write('*' + len(o)) + for val in o: + self.dump(client, val) + elif isinstance(o, RedisError): + client.wfile.write('-ERR %s\r\n' % o.message) + else: + client.wfile.write('return type not yet implemented\r\n') + client.wfile.flush() + + def stop(self): + self.halt = True + self.join() + + def run(self): + self.halt = False + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + server.bind((self.host, self.port)) + server.listen(5) + while not self.halt: + readable, _, _ = select.select([server] + self.clients.keys(), [], [], 1.0) + for sock in readable: + if sock == server: + (client_socket, address) = server.accept() + client = RedisClient(client_socket) + self.clients[client_socket] = client + self.log(client, 'client connected') + self.select(client, 0) + else: + try: + self.handle(self.clients[sock]) + except Exception, e: + self.log(client, 'exception: %s' % e) + self.quit(client) + for client_socket in self.clients.iterkeys(): + client_socket.close() + self.clients.clear() + server.close() + server = None + +def main(args): + host, port, logging = '127.0.0.1', 56784, True + opts, args = getopt.getopt(args, 'h:p:l') + for o, a in opts: + if o == '-h': + host = a + elif o == '-p': + port = int(a) + elif o == '-l': + logging = True + print 'Launching MiniRedis on %s:%s' % (host, port) + m = MiniRedis(host=host, port=port, logging=logging) + m.start() + m.join() + print 'Stopped' + +if __name__ == '__main__': + main(sys.argv[1:])