forked from kmerenkov/brukva
/
client.py
1145 lines (921 loc) · 38.6 KB
/
client.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import socket
from functools import partial
from itertools import izip
import logging
from collections import Iterable, defaultdict
import weakref
import traceback
from tornado.ioloop import IOLoop
from tornado.iostream import IOStream
from adisp import async, process
from datetime import datetime
from brukva.exceptions import RequestError, ConnectionError, ResponseError, InvalidResponse
log = logging.getLogger('brukva.client')
log_blob = logging.getLogger('brukva.client.blob')
class ExecutionContext(object):
def __init__(self, callbacks, error_wrapper=None):
self.callbacks = callbacks
self.error_wrapper = error_wrapper
self.is_active = True
def _call_callbacks(self, callbacks, value):
if callbacks:
if isinstance(callbacks, Iterable):
for cb in callbacks:
cb(value)
else:
callbacks(value)
def __enter__(self):
return self
def __exit__(self, type_, value, tb):
if type_ is None:
return True
if self.error_wrapper:
value = self.error_wrapper(value)
else:
value = value or Exception(
'Strange exception with None value type: %s; tb: %s' %
(type_, '\n'.join(traceback.format_tb(tb))
))
if self.is_active:
log.error(value, exc_info=(type_, value, tb))
self.ret_call(value)
return True
else:
return False
def disable(self):
self.is_active = False
def enable(self):
self.is_active = True
def ret_call(self, value):
self.is_active = False
self._call_callbacks(self.callbacks, value)
self.is_active = True
def safe_call(self, callbacks, value):
self.is_active = False
self._call_callbacks(callbacks, value)
self.is_active = True
def execution_context(callbacks, error_wrapper=None):
"""
Syntax sugar.
If some error occurred inside with block,
it will be suppressed and forwarded to callbacks.
Use contex.ret_call(value) method to call callbacks.
@type callbacks: callable or iterator over callables
@rtype: context
"""
return ExecutionContext(callbacks, error_wrapper)
class Message(object):
def __init__(self, kind, channel, body):
self.kind = kind
self.channel = channel
self.body = body
def __unicode__(self):
return u'<PubSub %s on channel %s: %s >' % (self.kind, self.channel, self.body)
def __str__(self):
return '<PubSub %s on channel %s: %s >' % (self.kind, self.channel, self.body)
def __repr__(self):
return repr(str(self))
class CmdLine(object):
def __init__(self, cmd, *args, **kwargs):
self.cmd = cmd
self.args = args
self.kwargs = kwargs
def __repr__(self):
return self.cmd + '(' + str(self.args) + ',' + str(self.kwargs) + ')'
def string_keys_to_dict(key_string, callback):
return dict([(key, callback) for key in key_string.split()])
def dict_merge(*dicts):
merged = {}
[merged.update(d) for d in dicts]
return merged
def encode(value):
if isinstance(value, str):
return value
elif isinstance(value, unicode):
return value.encode('utf-8')
# pray and hope
return str(value)
def format(*tokens):
cmds = []
for t in tokens:
e_t = encode(t)
cmds.append('$%s\r\n%s\r\n' % (len(e_t), e_t))
return '*%s\r\n%s' % (len(tokens), ''.join(cmds))
def format_pipeline_request(command_stack):
return ''.join(format(c.cmd, *c.args, **c.kwargs) for c in command_stack)
class Connection(object):
def __init__(self, host, port, idx,
on_connect, on_disconnect,
timeout=None, io_loop=None):
# FIXME: io_loop can't be None
self.host = host
self.port = port
self.idx = idx
self.on_connect = on_connect
self.on_disconnect = on_disconnect
self.timeout = timeout
self._io_loop = io_loop
self._stream = None
self.try_left = 2
self.is_initialized = False
self.in_progress = False
self.read_queue = []
def connect(self):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
sock.settimeout(self.timeout)
sock.connect((self.host, self.port))
self._stream = IOStream(sock, io_loop=self._io_loop)
self.connected()
except socket.error, e:
raise ConnectionError(str(e))
self.on_connect()
def disconnect(self):
if self._stream:
try:
self._stream.close()
except socket.error, e:
pass
self._stream = None
def write(self, data, try_left=None):
if try_left is None:
try_left = self.try_left
if not self._stream:
self.connect()
if not self._stream:
raise ConnectionError('Tried to write to non-existent connection')
if try_left > 0:
try:
log_blob.debug('try write %r to %s', data, self.idx)
self._stream.write(data)
except IOError:
self.disconnect()
self.write(data, try_left - 1)
else:
raise ConnectionError('Tried to write to non-existent connection')
def read(self, length, callback):
try:
if not self._stream:
self.disconnect()
raise ConnectionError('Tried to read from non-existent connection')
log_blob.debug('read bytes %s bytes from %s', length, self.idx)
self._stream.read_bytes(length, callback)
except IOError:
self.on_disconnect()
def readline(self, callback):
try:
if not self._stream:
self.disconnect()
raise ConnectionError('Tried to read from non-existent connection')
log_blob.debug('readline from %s', self.idx)
self._stream.read_until('\r\n', callback)
except IOError:
self.on_disconnect()
#def try_to_perform_read(self):
# if not self.in_progress and self.read_queue:
# self.in_progress = True
# self._io_loop.add_callback(partial(self.read_queue.pop(0), None) )
#
#@async
#def queue_wait(self, callback):
# self.read_queue.append(callback)
# self.try_to_perform_read()
#
#def read_done(self):
# self.in_progress = False
# self.on_read_done()
def connected(self):
if self._stream:
return True
return False
class ConnectionPool(object):
def __init__(self, connection_args, on_connect, on_disconnect, io_loop, pool_size=4):
"""
connection_args:
{
'host' = 'localhost',
port = 6379,
timeout = None,
selected_db = None,
auth = None,
}
"""
self.pool_size = pool_size
self.connection_args = connection_args
self._on_connect = on_connect
self._on_disconnect = on_disconnect
self.io_loop = io_loop
self.connection_args['io_loop'] = io_loop
self.connection_args['on_disconnect'] = on_disconnect
self.connection_requests_queue = []
def connect(self):
"""
Create connection pool, connect all connection
and perform AUTH, SELECT if needed.
"""
self.connections = {}
self.free_connections = set()
connection_args = self.connection_args
for idx in xrange(self.pool_size):
@process
def on_connect(connection):
yield async(self._on_connect)(connection)
self.free_connections.add(connection.idx)
log.info('new connection %s added to pool',
connection.idx)
self.io_loop.add_callback(self._give_out_pending_requests)
connection_args['on_connect'] = on_connect
connection_args['idx'] = idx
connection = Connection(**connection_args)
connection.connect()
self.connections[idx] = connection
def _give_out_pending_requests(self):
while self.connection_requests_queue and self.free_connections:
log_blob.debug('late leasing connection')
callback = self.connection_requests_queue.pop(0)
self._lease_connection(callback)
def return_connection(self, connection):
self.free_connections.add(connection.idx)
log_blob.debug('returned connection: %s', connection.idx)
self._give_out_pending_requests()
def _lease_connection(self, callback):
idx = self.free_connections.pop()
log_blob.debug('leasing connection %s', idx)
connection = self.connections[idx]
self.io_loop.add_callback(lambda: callback(connection))
def request_connection(self, callback):
if not self.free_connections:
log_blob.debug('no free connections, waiting')
self.connection_requests_queue.append(callback)
else:
self._lease_connection(callback)
def reply_to_bool(r, *args, **kwargs):
return bool(r)
def make_reply_assert_msg(msg):
def reply_assert_msg(r, *args, **kwargs):
return r == msg
return reply_assert_msg
def reply_set(r, *args, **kwargs):
return set(r)
def reply_dict_from_pairs(r, *args, **kwargs):
return dict(izip(r[::2], r[1::2]))
def reply_str(r, *args, **kwargs):
return r or ''
def reply_int(r, *args, **kwargs):
return int(r) if r is not None else None
def reply_float(r, *args, **kwargs):
return float(r) if r is not None else None
def reply_datetime(r, *args, **kwargs):
return datetime.fromtimestamp(int(r))
def reply_pubsub_message(r, *args, **kwargs):
return Message(*r)
def reply_zset(r, *args, **kwargs):
if (not r ) or (not 'WITHSCORES' in args):
return r
return zip(r[::2], map(float, r[1::2]))
def reply_hmget(r, key, *fields, **kwargs):
return dict(zip(fields, r))
def reply_info(response):
info = {}
def get_value(value):
if ',' not in value:
return value
sub_dict = {}
for item in value.split(','):
k, v = item.split('=')
try:
sub_dict[k] = int(v)
except ValueError:
sub_dict[k] = v
return sub_dict
for line in response.splitlines():
key, value = line.split(':')
try:
info[key] = int(value)
except ValueError:
info[key] = get_value(value)
return info
def reply_ttl(r, *args, **kwargs):
return r != -1 and r or None
PUB_SUB_COMMANDS = set([
'SUBSCRIBE',
'UNSUBSCRIBE',
'LISTEN',
])
class _AsyncWrapper(object):
def __init__(self, obj):
self.obj = obj
self.memoized = {}
def __getattr__(self, item):
if item not in self.memoized:
self.memoized[item] = async(getattr(self.obj, item), cbname='callbacks')
return self.memoized[item]
class Client(object):
def __init__(self, host='localhost', port=6379, password=None,
selected_db=None, io_loop=None):
self._io_loop = io_loop or IOLoop.instance()
self.host = host
self.port = port
self.connection_pool = ConnectionPool({
'host': host,
'port': port,
},
on_connect = self._initialize_connection,
on_disconnect = self.on_disconnect,
io_loop = self._io_loop
)
self.async = _AsyncWrapper(weakref.proxy(self))
self.queue = []
self.current_cmd_line = None
self.subscribed = False
self.password = password
self.selected_db = selected_db
self.REPLY_MAP = dict_merge(
string_keys_to_dict('AUTH BGREWRITEAOF BGSAVE DEL EXISTS EXPIRE HDEL HEXISTS '
'HMSET MOVE MSET MSETNX SAVE SETNX',
reply_to_bool),
string_keys_to_dict('FLUSHALL FLUSHDB SELECT SET SETEX SHUTDOWN '
'RENAME RENAMENX WATCH UNWATCH',
make_reply_assert_msg('OK')),
string_keys_to_dict('SMEMBERS SINTER SUNION SDIFF',
reply_set),
string_keys_to_dict('HGETALL BRPOP BLPOP',
reply_dict_from_pairs),
string_keys_to_dict('HGET',
reply_str),
string_keys_to_dict('SUBSCRIBE UNSUBSCRIBE LISTEN',
reply_pubsub_message),
string_keys_to_dict('ZRANK ZREVRANK',
reply_int),
string_keys_to_dict('ZSCORE ZINCRBY ZCOUNT ZCARD',
reply_int),
string_keys_to_dict('ZRANGE ZRANGEBYSCORE ZREVRANGE',
reply_zset),
{'HMGET': reply_hmget},
{'PING': make_reply_assert_msg('PONG')},
{'LASTSAVE': reply_datetime },
{'TTL': reply_ttl } ,
{'INFO': reply_info},
{'MULTI_PART': make_reply_assert_msg('QUEUED')},
)
self._waiting_callbacks = defaultdict(list)
self._pipeline = None
def __repr__(self):
return '<Brukva client %s:%s>' % (self.host, self.port)
def pipeline(self, transactional=False):
if not self._pipeline:
self._pipeline = Pipeline(
selected_db=self.selected_db,
io_loop = self._io_loop,
transactional=transactional
)
self._pipeline.connection_pool = self.connection_pool
return self._pipeline
#### connection
def connect(self):
self.connection_pool.connect()
def disconnect(self):
raise NotImplementedError()
pass
#self.connection.disconnect()
def on_disconnect(self):
if self.subscribed:
self.subscribed = False
raise ConnectionError("Socket closed on remote end")
####
#### formatting
def encode(self, value):
if isinstance(value, str):
return value
elif isinstance(value, unicode):
return value.encode('utf-8')
# pray and hope
return str(value)
def format(self, *tokens):
cmds = []
for t in tokens:
e_t = self.encode(t)
cmds.append('$%s\r\n%s\r\n' % (len(e_t), e_t))
return '*%s\r\n%s' % (len(tokens), ''.join(cmds))
def format_reply(self, cmd_line, data):
if cmd_line.cmd not in self.REPLY_MAP:
return data
try:
res = self.REPLY_MAP[cmd_line.cmd](data, *cmd_line.args, **cmd_line.kwargs)
except Exception, e:
raise ResponseError(
'failed to format reply to %s, raw data: %s; err message: %s' %
(cmd_line, data, e), cmd_line
)
return res
####
#### new AsIO
def call_callbacks(self, callbacks, *args, **kwargs):
for cb in callbacks:
cb(*args, **kwargs)
@process
def _initialize_connection(self, connection, callback):
"""
call after connection creation
"""
with execution_context(callback) as ctx:
cmds = []
if self.password:
cmds.append(CmdLine('AUTH', self.password))
if self.selected_db:
cmds.append(CmdLine('SELECT', self.selected_db))
if cmds:
try:
for cmd_line in cmds:
connection.write(self.format(
cmd_line.cmd,
*cmd_line.args,
**cmd_line.kwargs
))
except Exception, e:
connection.disconnect()
raise e
for cmd_line in cmds:
data = yield async(connection.readline)()
response = yield self.process_data(connection, data, cmd_line)
result = self.format_reply(cmd_line, response)
log_blob.debug('got result %s on cmd %s', result, cmd_line)
connection.is_initialized = True
ctx.ret_call(True)
@process
def execute_command(self, cmd, callbacks, *args, **kwargs):
cmd_line = CmdLine(cmd, *args, **kwargs)
with execution_context(callbacks) as ctx:
if callbacks is None:
callbacks = []
elif not hasattr(callbacks, '__iter__'):
callbacks = [callbacks]
if self.subscribed and cmd not in PUB_SUB_COMMANDS:
ctx.ret_call(RequestError('Calling not pub/sub command during subscribed state', cmd_line))
return
log_blob.debug('try to get connection for %s', cmd_line)
connection = yield async(self.connection_pool.request_connection)()
log_blob.debug('got connection %s for %s cmd_line',
connection.idx, cmd_line)
try:
connection.write(self.format(cmd, *args, **kwargs))
except Exception, e:
connection.disconnect()
raise e
#if self.subscribed and cmd in ('SUBSCRIBE', 'UNSUBSCRIBE'):
# self._waiting_callbacks[cmd].append(callbacks)
# return
data = yield async(connection.readline)()
if not data:
self.connection_pool.return_connection(connection)
raise Exception('TODO: [no data from connection %s->readline' % connection.idx)
else:
response = yield self.process_data(connection, data, cmd_line)
result = self.format_reply(cmd_line, response)
self.connection_pool.return_connection(connection)
log_blob.debug('got result %s on cmd %s with connection %s', result, cmd_line, connection.idx)
ctx.ret_call(result)
@async
@process
def process_data(self, connection, data, cmd_line, callback):
with execution_context(callback) as ctx:
data = data[:-2] # strip \r\n
if data == '$-1':
response = None
elif data == '*0' or data == '*-1':
response = []
else:
if len(data) == 0:
raise IOError('Disconnected')
head, tail = data[0], data[1:]
if head == '*':
response = yield self.consume_multibulk(connection, int(tail), cmd_line)
elif head == '$':
response = yield self.consume_bulk(connection, int(tail)+2)
elif head == '+':
response = tail
elif head == ':':
response = int(tail)
elif head == '-':
if tail.startswith('ERR'):
tail = tail[4:]
response = ResponseError(tail, cmd_line)
else:
raise ResponseError('Unknown response type %s' % head, cmd_line)
ctx.ret_call(response)
@async
@process
def consume_multibulk(self, connection, length, cmd_line, callback):
with execution_context(callback) as ctx:
tokens = []
while len(tokens) < length:
data = yield async(connection.readline)()
if not data:
raise ResponseError(
'Not enough data in response to %s, accumulated tokens: %s'%
(cmd_line, tokens), cmd_line
)
token = yield self.process_data(connection, data, cmd_line) #FIXME error
tokens.append( token )
ctx.ret_call(tokens)
@async
@process
def consume_bulk(self, connection, length, callback):
with execution_context(callback) as ctx:
data = yield async(connection.read)(length)
if isinstance(data, Exception):
raise data
if not data:
raise ResponseError('EmptyResponse')
else:
data = data[:-2]
ctx.ret_call(data)
### MAINTENANCE
def bgrewriteaof(self, callbacks=None):
self.execute_command('BGREWRITEAOF', callbacks)
def dbsize(self, callbacks=None):
self.execute_command('DBSIZE', callbacks)
def flushall(self, callbacks=None):
self.execute_command('FLUSHALL', callbacks)
def flushdb(self, callbacks=None):
self.execute_command('FLUSHDB', callbacks)
def ping(self, callbacks=None):
self.execute_command('PING', callbacks)
def info(self, callbacks=None):
self.execute_command('INFO', callbacks)
def shutdown(self, callbacks=None):
self.execute_command('SHUTDOWN', callbacks)
def save(self, callbacks=None):
self.execute_command('SAVE', callbacks)
def bgsave(self, callbacks=None):
self.execute_command('BGSAVE', callbacks)
def lastsave(self, callbacks=None):
self.execute_command('LASTSAVE', callbacks)
def keys(self, pattern, callbacks=None):
self.execute_command('KEYS', callbacks, pattern)
### BASIC KEY COMMANDS
def append(self, key, value, callbacks=None):
self.execute_command('APPEND', callbacks, key, value)
def expire(self, key, ttl, callbacks=None):
self.execute_command('EXPIRE', callbacks, key, ttl)
def ttl(self, key, callbacks=None):
self.execute_command('TTL', callbacks, key)
def type(self, key, callbacks=None):
self.execute_command('TYPE', callbacks, key)
def randomkey(self, callbacks=None):
self.execute_command('RANDOMKEY', callbacks)
def rename(self, src, dst, callbacks=None):
self.execute_command('RENAME', callbacks, src, dst)
def renamenx(self, src, dst, callbacks=None):
self.execute_command('RENAMENX', callbacks, src, dst)
def move(self, key, db, callbacks=None):
self.execute_command('MOVE', callbacks, key, db)
def substr(self, key, start, end, callbacks=None):
self.execute_command('SUBSTR', callbacks, key, start, end)
def delete(self, key, callbacks=None):
self.execute_command('DEL', callbacks, key)
def set(self, key, value, callbacks=None):
self.execute_command('SET', callbacks, key, value)
def setex(self, key, ttl, value, callbacks=None):
self.execute_command('SETEX', callbacks, key, ttl, value)
def setnx(self, key, value, callbacks=None):
self.execute_command('SETNX', callbacks, key, value)
def mset(self, mapping, callbacks=None):
items = []
[ items.extend(pair) for pair in mapping.iteritems() ]
self.execute_command('MSET', callbacks, *items)
def msetnx(self, mapping, callbacks=None):
items = []
[ items.extend(pair) for pair in mapping.iteritems() ]
self.execute_command('MSETNX', callbacks, *items)
def get(self, key, callbacks=None):
self.execute_command('GET', callbacks, key)
def mget(self, keys, callbacks=None):
self.execute_command('MGET', callbacks, *keys)
def getset(self, key, value, callbacks=None):
self.execute_command('GETSET', callbacks, key, value)
def exists(self, key, callbacks=None):
self.execute_command('EXISTS', callbacks, key)
def sort(self, key, start=None, num=None, by=None, get=None, desc=False, alpha=False, store=None, callbacks=None):
if (start is not None and num is None) or (num is not None and start is None):
raise ValueError("``start`` and ``num`` must both be specified")
tokens = [key]
if by is not None:
tokens.append('BY')
tokens.append(by)
if start is not None and num is not None:
tokens.append('LIMIT')
tokens.append(start)
tokens.append(num)
if get is not None:
tokens.append('GET')
tokens.append(get)
if desc:
tokens.append('DESC')
if alpha:
tokens.append('ALPHA')
if store is not None:
tokens.append('STORE')
tokens.append(store)
return self.execute_command('SORT', callbacks, *tokens)
### COUNTERS COMMANDS
def incr(self, key, callbacks=None):
self.execute_command('INCR', callbacks, key)
def decr(self, key, callbacks=None):
self.execute_command('DECR', callbacks, key)
def incrby(self, key, amount, callbacks=None):
self.execute_command('INCRBY', callbacks, key, amount)
def decrby(self, key, amount, callbacks=None):
self.execute_command('DECRBY', callbacks, key, amount)
### LIST COMMANDS
def blpop(self, keys, timeout=0, callbacks=None):
tokens = list(keys)
tokens.append(timeout)
self.execute_command('BLPOP', callbacks, *tokens)
def brpop(self, keys, timeout=0, callbacks=None):
tokens = list(keys)
tokens.append(timeout)
self.execute_command('BRPOP', callbacks, *tokens)
def brpoplpush(self, src, dst, timeout=1, callbacks=None):
tokens = [src, dst, timeout]
self.execute_command('BRPOPLPUSH', callbacks, *tokens)
def lindex(self, key, index, callbacks=None):
self.execute_command('LINDEX', callbacks, key, index)
def llen(self, key, callbacks=None):
self.execute_command('LLEN', callbacks, key)
def lrange(self, key, start, end, callbacks=None):
self.execute_command('LRANGE', callbacks, key, start, end)
def lrem(self, key, value, num=0, callbacks=None):
self.execute_command('LREM', callbacks, key, num, value)
def lset(self, key, index, value, callbacks=None):
self.execute_command('LSET', callbacks, key, index, value)
def ltrim(self, key, start, end, callbacks=None):
self.execute_command('LTRIM', callbacks, key, start, end)
def lpush(self, key, value, callbacks=None):
self.execute_command('LPUSH', callbacks, key, value)
def rpush(self, key, value, callbacks=None):
self.execute_command('RPUSH', callbacks, key, value)
def lpop(self, key, callbacks=None):
self.execute_command('LPOP', callbacks, key)
def rpop(self, key, callbacks=None):
self.execute_command('RPOP', callbacks, key)
def rpoplpush(self, src, dst, callbacks=None):
self.execute_command('RPOPLPUSH', callbacks, src, dst)
### SET COMMANDS
def sadd(self, key, value, callbacks=None):
self.execute_command('SADD', callbacks, key, value)
def srem(self, key, value, callbacks=None):
self.execute_command('SREM', callbacks, key, value)
def scard(self, key, callbacks=None):
self.execute_command('SCARD', callbacks, key)
def spop(self, key, callbacks=None):
self.execute_command('SPOP', callbacks, key)
def smove(self, src, dst, value, callbacks=None):
self.execute_command('SMOVE', callbacks, src, dst, value)
def sismember(self, key, value, callbacks=None):
self.execute_command('SISMEMBER', callbacks, key, value)
def smembers(self, key, callbacks=None):
self.execute_command('SMEMBERS', callbacks, key)
def srandmember(self, key, callbacks=None):
self.execute_command('SRANDMEMBER', callbacks, key)
def sinter(self, keys, callbacks=None):
self.execute_command('SINTER', callbacks, *keys)
def sdiff(self, keys, callbacks=None):
self.execute_command('SDIFF', callbacks, *keys)
def sunion(self, keys, callbacks=None):
self.execute_command('SUNION', callbacks, *keys)
def sinterstore(self, keys, dst, callbacks=None):
self.execute_command('SINTERSTORE', callbacks, dst, *keys)
def sunionstore(self, keys, dst, callbacks=None):
self.execute_command('SUNIONSTORE', callbacks, dst, *keys)
def sdiffstore(self, keys, dst, callbacks=None):
self.execute_command('SDIFFSTORE', callbacks, dst, *keys)
### SORTED SET COMMANDS
def zadd(self, key, score, value, callbacks=None):
self.execute_command('ZADD', callbacks, key, score, value)
def zcard(self, key, callbacks=None):
self.execute_command('ZCARD', callbacks, key)
def zincrby(self, key, value, amount, callbacks=None):
self.execute_command('ZINCRBY', callbacks, key, amount, value)
def zrank(self, key, value, callbacks=None):
self.execute_command('ZRANK', callbacks, key, value)
def zrevrank(self, key, value, callbacks=None):
self.execute_command('ZREVRANK', callbacks, key, value)
def zrem(self, key, value, callbacks=None):
self.execute_command('ZREM', callbacks, key, value)
def zcount(self, key, start, end, offset=None, limit=None, with_scores=None, callbacks=None):
tokens = [key, start, end]
if offset is not None:
tokens.append('LIMIT')
tokens.append(offset)
tokens.append(limit)
if with_scores:
tokens.append('WITHSCORES')
self.execute_command('ZCOUNT', callbacks, *tokens)
def zcard(self, key, callbacks=None):
self.execute_command('ZCARD', callbacks, key)
def zscore(self, key, value, callbacks=None):
self.execute_command('ZSCORE', callbacks, key, value)
def zrange(self, key, start, num, with_scores, callbacks=None):
tokens = [key, start, num]
if with_scores:
tokens.append('WITHSCORES')
self.execute_command('ZRANGE', callbacks, *tokens)
def zrevrange(self, key, start, num, with_scores, callbacks=None):
tokens = [key, start, num]
if with_scores:
tokens.append('WITHSCORES')
self.execute_command('ZREVRANGE', callbacks, *tokens)
def zrangebyscore(self, key, start, end, offset=None, limit=None, with_scores=False, callbacks=None):
tokens = [key, start, end]
if offset is not None:
tokens.append('LIMIT')
tokens.append(offset)
tokens.append(limit)
if with_scores:
tokens.append('WITHSCORES')
self.execute_command('ZRANGEBYSCORE', callbacks, *tokens)
def zremrangebyrank(self, key, start, end, callbacks=None):
self.execute_command('ZREMRANGEBYRANK', callbacks, key, start, end)
def zremrangebyscore(self, key, start, end, callbacks=None):
self.execute_command('ZREMRANGEBYSCORE', callbacks, key, start, end)
def zinterstore(self, dest, keys, aggregate=None, callbacks=None):
return self._zaggregate('ZINTERSTORE', dest, keys, aggregate, callbacks)
def zunionstore(self, dest, keys, aggregate=None, callbacks=None):
return self._zaggregate('ZUNIONSTORE', dest, keys, aggregate, callbacks)
def _zaggregate(self, command, dest, keys, aggregate, callbacks):
tokens = [dest, len(keys)]
if isinstance(keys, dict):
items = keys.items()
keys = [i[0] for i in items]
weights = [i[1] for i in items]
else:
weights = None
tokens.extend(keys)
if weights:
tokens.append('WEIGHTS')
tokens.extend(weights)
if aggregate:
tokens.append('AGGREGATE')
tokens.append(aggregate)
return self.execute_command(command, callbacks, *tokens)
### HASH COMMANDS
def hgetall(self, key, callbacks=None):
self.execute_command('HGETALL', callbacks, key)
def hmset(self, key, mapping, callbacks=None):
items = []
[ items.extend(pair) for pair in mapping.iteritems() ]
self.execute_command('HMSET', callbacks, key, *items)
def hset(self, key, field, value, callbacks=None):
self.execute_command('HSET', callbacks, key, field, value)
def hget(self, key, field, callbacks=None):
self.execute_command('HGET', callbacks, key, field)
def hdel(self, key, field, callbacks=None):
self.execute_command('HDEL', callbacks, key, field)
def hlen(self, key, callbacks=None):
self.execute_command('HLEN', callbacks, key)
def hexists(self, key, field, callbacks=None):
self.execute_command('HEXISTS', callbacks, key, field)
def hincrby(self, key, field, amount=1, callbacks=None):
self.execute_command('HINCRBY', callbacks, key, field, amount)
def hkeys(self, key, callbacks=None):
self.execute_command('HKEYS', callbacks, key)
def hmget(self, key, fields, callbacks=None):
self.execute_command('HMGET', callbacks, key, *fields)
def hvals(self, key, callbacks=None):
self.execute_command('HVALS', callbacks, key)
### PUBSUB
def subscribe(self, channels, callbacks=None):
callbacks = callbacks or []
if not isinstance(callbacks, Iterable):
callbacks = [callbacks]
if isinstance(channels, basestring):
channels = [channels]
if not self.subscribed:
callbacks = list(callbacks) + [self.on_subscribed]
self.execute_command('SUBSCRIBE', callbacks, *channels)
def on_subscribed(self, result):
self.subscribed = True