forked from ifduyue/pyssdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyssdb.py
271 lines (223 loc) · 8.1 KB
/
pyssdb.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
pyssdb
~~~~~~~
A SSDB Client Library for Python.
:copyright: (c) 2013-2017 by Yue Du.
:license: BSD 2-clause License, see LICENSE for more details.
'''
from __future__ import print_function
import os
import sys
import socket
import functools
import itertools
import numbers
__version__ = '0.4.1'
__author__ = 'Yue Du <ifduyue@gmail.com>'
__url__ = 'https://github.com/ifduyue/pyssdb'
__license__ = 'BSD 2-Clause License'
PY3 = sys.version_info >= (3,)
if PY3:
unicode = str
from itertools import zip_longest
else:
from itertools import izip_longest as zip_longest
def utf8(s):
s = str(s) if isinstance(s, numbers.Real) else s
return s.encode('utf8') if isinstance(s, unicode) else s
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
"Copy From itertools Recipes"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
class error(Exception):
def __init__(self, reason, *args):
super(error, self).__init__(reason, *args)
self.reason = reason
self.message = ' '.join(args)
class Connection(object):
def __init__(self, host='127.0.0.1', port=8888, socket_timeout=None, password=None):
self.pid = os.getpid()
self.host = host
self.port = port
self.socket_timeout = socket_timeout
self._sock = None
self._fp = None
self.password = password
def connect(self):
if self._sock:
return
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# make it reusable,防止过快建立链接而没有端口可用,即减少TIME_WAIT,端口重用
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(self.socket_timeout)
sock.connect((self.host, self.port))
self._sock = sock
self._fp = sock.makefile('rb')
if self.password:
# 如果密码存在,则进行密码认证
self.send('auth', self.password)
except socket.error:
raise
def disconnect(self):
if self._sock is None:
return
try:
self._sock.close()
except socket.error:
pass
self._sock = self._fp = None
close = disconnect
def reconnect(self):
self.disconnect()
self.connect()
def send(self, cmd, *args):
if cmd == 'delete':
cmd = 'del'
self.last_cmd = cmd
if self._sock is None:
self.connect()
args = [utf8(cmd)] + [utf8(i) for i in args]
buf = utf8('').join(utf8('%d\n%s\n') % (len(i), i) for i in args) + utf8('\n')
self._sock.sendall(buf)
def recv(self):
cmd = self.last_cmd
ret = []
while True:
line = self._fp.readline().rstrip(utf8('\n'))
if not line:
break
data = self._fp.read(int(line))
self._fp.read(1) # discard '\n'
ret.append(data)
status, ret = ret[0], ret[1:]
st = status.decode('utf8')
if st == 'not_found':
return None
elif st == 'ok':
if cmd.endswith('keys') or cmd.endswith('hgetall') or cmd.endswith('list') or \
cmd.endswith('scan') or cmd.endswith('range') or \
(cmd.startswith('multi_') and cmd.endswith('get')) or \
cmd.endswith('getall'):
ret = list(map(lambda x: x.decode('utf-8'), ret))
return ret
elif cmd == 'info':
ret = list(map(lambda x: x.decode('utf-8'), ret))
return ret[1:]
elif len(ret) == 1:
if cmd.endswith('set') or cmd.endswith('del') or \
cmd.endswith('incr') or cmd.endswith('decr') or \
cmd.endswith('size') or cmd.endswith('rank') or \
cmd in ('setx', 'zget', 'qtrim_front', 'qtrim_back'):
value = ret[0].decode('utf-8')
return int(value)
else:
value = ret[0].decode('utf-8')
return value
elif not ret:
return True
else:
return ret
elif st == 'error':
raise error(st, ret[0].decode('utf-8'))
ret = list(map(lambda a: str(a, 'utf-8'), ret))
raise error(status, *ret)
class ConnectionPool(object):
def __init__(self, connection_class=Connection, max_connections=1048576,
**connection_kwargs):
self.pid = os.getpid()
self.connection_class = connection_class
self.connection_kwargs = connection_kwargs
self.max_connections = max_connections
self.idle_connections = []
self.active_connections = set()
def checkpid(self):
if self.pid != os.getpid():
self.disconnect()
self.__init__(self.connection_class, self.max_connections,
**self.connection_kwargs)
def get_connection(self):
self.checkpid()
try:
connection = self.idle_connections.pop()
except IndexError:
connection = self.new_connection()
self.active_connections.add(connection)
return connection
def new_connection(self):
count = len(self.active_connections) + len(self.idle_connections)
if count > self.max_connections:
raise error("Too many connections")
return self.connection_class(**self.connection_kwargs)
def release(self, connection):
self.checkpid()
if connection.pid == self.pid:
self.active_connections.remove(connection)
self.idle_connections.append(connection)
def disconnect(self):
acs, self.active_connections = self.active_connections, set()
ics, self.idle_connections = self.idle_connections, []
for connection in itertools.chain(acs, ics):
connection.disconnect()
close = disconnect
def command_post_processing(func):
@functools.wraps(func)
def wrapper(self, cmd, *args):
data = func(self, cmd, *args)
if 'info' == cmd:
return dict(grouper(data, 2, None))
else:
return data
return wrapper
class Client(object):
def __init__(self, host='127.0.0.1', port=8888, password=None, connection_pool=None,
socket_timeout=None, max_connections=1048576):
self.password = password
if not connection_pool:
connection_pool = ConnectionPool(host=host, port=port, password=password,
socket_timeout=socket_timeout,
max_connections=max_connections)
self.connection_pool = connection_pool
connection = self.connection_pool.new_connection()
connection.connect()
self.connection_pool.idle_connections.append(connection)
@command_post_processing
def execute_command(self, cmd, *args):
connection = self.connection_pool.get_connection()
try:
connection.send(cmd, *args)
data = connection.recv()
except:
connection.close()
raise
else:
self.connection_pool.release(connection)
return data
def disconnect(self):
self.connection_pool.disconnect()
close = disconnect
def __getattr__(self, cmd):
if cmd not in self.__dict__:
self.__dict__[cmd] = functools.partial(self.execute_command, cmd)
return self.__dict__[cmd]
if __name__ == '__main__':
c = Client()
print(c.set('key', 'value'))
print(c.get('key'))
import string
for i in string.ascii_letters:
c.incr(i)
print(c.keys('a', 'z', 1))
print(c.keys('a', 'z', 10))
print(c.get('z'))
print(c.get('a'))
print(c.set('中文', '你好'))
print(c.get('中文'))
print(c.info())
c.hset("3", 1, 2)
c.hclear("3")
c.disconnect()