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

simplify dispatch system

Changeset ebffd254a0e8

Parent 7ac9eb03f32f

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to one file · Browse files at ebffd254a0e8 Showing diff from parent 7ac9eb03f32f Diff from another changeset...

Change 1 of 6 Show Entire File miniredis.py Stacked
 
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
 
93
94
95
96
 
97
98
99
100
101
102
103
 
104
105
106
 
109
110
111
112
 
113
114
115
 
121
122
123
124
 
125
126
127
128
129
 
130
131
132
 
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
 
222
223
224
225
 
226
227
228
 
47
48
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
51
52
53
 
54
55
56
57
58
59
60
61
62
63
64
 
65
66
67
68
 
73
74
75
 
76
77
78
79
80
81
82
 
83
84
85
86
 
89
90
91
 
92
93
94
95
 
101
102
103
 
104
105
106
107
108
 
109
110
111
112
 
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
 
195
196
197
 
198
199
200
201
@@ -47,42 +47,22 @@
  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): + def select(self, client, db): + if db not in self.tables: + self.tables[db] = {} + client.db = db + client.table = self.tables[db] + + def handle_set(self, client, key, data):   client.table[key] = data   self.log(client, 'SET %s -> %s' % (key, data))   return True   - def get(self, client, key): + def handle_get(self, client, key):   data = client.table.get(key, None)   if isinstance(data, list):   return RedisBadValueError() @@ -93,14 +73,14 @@
  self.log(client, 'GET %s -> %s' % (key, data))   return data   - def delete(self, client, key): + def handle_del(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): + def handle_lpush(self, client, key, data):   if key not in client.table:   client.table[key] = []   elif not isinstance(client.table[key], list): @@ -109,7 +89,7 @@
  self.log(client, 'LPUSH %s %s' % (key, data))   return True   - def rpop(self, client, key): + def handle_rpop(self, client, key):   if key not in client.table:   return EMPTY_SCALAR   if not isinstance(client.table[key], list): @@ -121,12 +101,12 @@
  self.log(client, 'LPOP %s -> %s' % (key, data))   return data   - def keys(self, client, pattern): + def handle_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): + def handle_incrby(self, client, key, by):   try:   client.table[key] = int(client.table[key])   client.table[key] += int(by) @@ -135,84 +115,77 @@
  self.log(client, 'INCRBY %s %s -> %s' % (key, by, client.table[key]))   return client.table[key]   - def flushdb(self, client): + def handle_flushdb(self, client):   self.log(client, 'FLUSHDB')   client.table.clear()   return True   - def select(self, client, db): + def handle_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.select(client, db)   self.log(client, 'SELECT %s' % db)   return True   - def quit(self, client): + def handle_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): + def handle_shutdown(self, client):   self.log(client, 'SHUTDOWN')   self.halt = True   return True   - def handle_set(self, client, line): + def unwrap_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) + return self.handle_set(key, client, data)   - def handle_get(self, client, line): + def unwrap_get(self, client, line):   key = line.strip() - return self.get(client, key) + return self.handle_get(client, key)   - def handle_del(self, client, line): + def unwrap_del(self, client, line):   key = line.strip() - return self.delete(client, key) + return self.handle_del(client, key)   - def handle_lpush(self, client, line): + def unwrap_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) + return self.handle_lpush(client, key, data)   - def handle_rpop(self, client, line): + def unwrap_rpop(self, client, line):   key = line.strip() - return self.rpop(client, key) + return self.handle_rpop(client, key)   - def handle_keys(self, client, line): + def unwrap_keys(self, client, line):   pattern = line.strip() - return self.keys(client, pattern) + return self.handle_keys(client, pattern)   - def handle_incr(self, client, line): - key = line.strip() - return self.incr(client, key) + def unwrap_incrby(self, client, line): + key, by = line.split() + return self.handle_incrby(client, key, by)   - def handle_incrby(self, client, line): - key, by = line.split() - return self.incr(client, key, by) + def unwrap_flushdb(self, client, line): + return self.handle_flushdb(client)   - def handle_flushdb(self, client, line): - return self.flushdb(client) + def unwrap_select(self, client, line): + db = line.strip() + return self.handle_select(client, db)   - def handle_select(self, client, line): - db = line.strip() - return self.select(client, db) + def unwrap_quit(self, client, line): + return self.handle_quit(client)   - def handle_quit(self, client, line): - return self.quit(client) - - def handle_shutdown(self, client, line): - return self.shutdown(client) + def unwrap_shutdown(self, client, line): + return self.handle_shutdown(client)     def handle_normal(self, client, line):   command, _, rest = line.partition(' ') - return self.wrappers[command.strip().lower()](client, rest) + return getattr(self, 'unwrap_' + command.strip().lower())(client, rest)     def handle_multibulk(self, client, line):   items = int(line[1:].strip()) @@ -222,7 +195,7 @@
  args.append(client.rfile.read(length))   client.rfile.read(2) # throw out newline   command = args[0].lower() - return self.dispatch[command](client, *args[1:]) + return getattr(self, 'handle_' + command)(client, *args[1:])     def handle(self, client):   line = client.rfile.readline()