12
12
import msgpack
13
13
14
14
import base64
15
- from const import IPROTO_GREETING_SIZE
16
15
17
16
try :
18
17
from ctypes import c_ssize_t
53
52
from tarantool .schema import Schema
54
53
from tarantool .utils import check_key
55
54
56
- class Connection (object ):
57
55
58
- '''\
56
+ class Connection (object ):
57
+ '''
59
58
Represents connection to the Tarantool server.
60
59
61
60
This class is responsible for connection and network exchange with
@@ -81,7 +80,7 @@ def __init__(self, host, port,
81
80
reconnect_max_attempts = RECONNECT_MAX_ATTEMPTS ,
82
81
reconnect_delay = RECONNECT_DELAY ,
83
82
connect_now = True ):
84
- '''\
83
+ '''
85
84
Initialize a connection to the server.
86
85
87
86
:param str host: Server hostname or IP-address
@@ -104,16 +103,15 @@ def __init__(self, host, port,
104
103
if connect_now :
105
104
self .connect ()
106
105
107
-
108
106
def close (self ):
109
- '''\
107
+ '''
110
108
Close connection to the server
111
109
'''
112
110
self ._socket .close ()
113
111
self ._socket = None
114
112
115
113
def connect_basic (self ):
116
- '''\
114
+ '''
117
115
Create connection to the host and port specified in __init__().
118
116
:raise: `NetworkError`
119
117
'''
@@ -136,7 +134,7 @@ def handshake(self):
136
134
self .authenticate (self .user , self .password )
137
135
138
136
def connect (self ):
139
- '''\
137
+ '''
140
138
Create connection to the host and port specified in __init__().
141
139
Usually there is no need to call this method directly,
142
140
since it is called when you create an `Connection` instance.
@@ -161,7 +159,7 @@ def _recv(self, to_read):
161
159
tmp = self ._socket .recv (to_read )
162
160
if not tmp :
163
161
raise NetworkError (socket .error (errno .ECONNRESET ,
164
- "Lost connection to server during query" ))
162
+ "Lost connection to server during query" ))
165
163
to_read -= len (tmp )
166
164
buf += tmp
167
165
return buf
@@ -179,7 +177,7 @@ def _read_response(self):
179
177
return self ._recv (length )
180
178
181
179
def _send_request_wo_reconnect (self , request ):
182
- '''\
180
+ '''
183
181
:rtype: `Response` instance
184
182
185
183
:raise: NetworkError
@@ -200,7 +198,7 @@ def _send_request_wo_reconnect(self, request):
200
198
raise DatabaseError (response .return_code , response .return_message )
201
199
202
200
def _opt_reconnect (self ):
203
- '''\
201
+ '''
204
202
Check that connection is alive using low-level recv from libc(ctypes)
205
203
**Due to bug in python - timeout is internal python construction.
206
204
'''
@@ -210,7 +208,7 @@ def _opt_reconnect(self):
210
208
def check (): # Check that connection is alive
211
209
buf = ctypes .create_string_buffer (2 )
212
210
self ._sys_recv (self ._socket .fileno (), buf , 1 ,
213
- socket .MSG_DONTWAIT | socket .MSG_PEEK )
211
+ socket .MSG_DONTWAIT | socket .MSG_PEEK )
214
212
if ctypes .get_errno () == errno .EAGAIN :
215
213
ctypes .set_errno (0 )
216
214
return errno .EAGAIN
@@ -234,7 +232,8 @@ def check(): # Check that connection is alive
234
232
warn ("Reconnect attempt %d of %d" %
235
233
(attempt , self .reconnect_max_attempts ), NetworkWarning )
236
234
if attempt == self .reconnect_max_attempts :
237
- raise NetworkError (socket .error (last_errno , errno .errorcode [last_errno ]))
235
+ raise NetworkError (
236
+ socket .error (last_errno , errno .errorcode [last_errno ]))
238
237
attempt += 1
239
238
240
239
self .handshake ()
@@ -245,7 +244,7 @@ def check(): # Check that connection is alive
245
244
self ._socket .settimeout (self .socket_timeout )
246
245
247
246
def _send_request (self , request ):
248
- '''\
247
+ '''
249
248
Send the request to the server through the socket.
250
249
Return an instance of `Response` class.
251
250
@@ -266,7 +265,7 @@ def flush_schema(self):
266
265
self .schema .flush ()
267
266
268
267
def call (self , func_name , * args ):
269
- '''\
268
+ '''
270
269
Execute CALL request. Call stored Lua function.
271
270
272
271
:param func_name: stored Lua function name
@@ -287,7 +286,7 @@ def call(self, func_name, *args):
287
286
return response
288
287
289
288
def eval (self , expr , * args ):
290
- '''\
289
+ '''
291
290
Execute EVAL request. Eval Lua expression.
292
291
293
292
:param expr: Lua expression
@@ -307,7 +306,6 @@ def eval(self, expr, *args):
307
306
response = self ._send_request (request )
308
307
return response
309
308
310
-
311
309
def replace (self , space_name , values ):
312
310
'''
313
311
Execute REPLACE request.
@@ -327,12 +325,12 @@ def replace(self, space_name, values):
327
325
return self ._send_request (request )
328
326
329
327
def authenticate (self , user , password ):
330
- self .user = user ;
328
+ self .user = user
331
329
self .password = password
332
330
if not self ._socket :
333
331
return self ._opt_reconnect ()
334
332
335
- request = RequestAuthenticate (self , self ._salt , self .user , \
333
+ request = RequestAuthenticate (self , self ._salt , self .user ,
336
334
self .password )
337
335
return self ._send_request_wo_reconnect (request )
338
336
@@ -341,21 +339,21 @@ def join(self, server_uuid):
341
339
resp = self ._send_request (request )
342
340
while True :
343
341
yield resp
344
- if resp .code == REQUEST_TYPE_OK or \
345
- resp .code >= REQUEST_TYPE_ERROR :
342
+ if resp .code == REQUEST_TYPE_OK or resp .code >= REQUEST_TYPE_ERROR :
346
343
return
347
344
resp = Response (self , self ._read_response ())
348
- self .close () # close connection after JOIN
345
+ self .close () # close connection after JOIN
349
346
350
- def subscribe (self , cluster_uuid , server_uuid , vclock = {}):
347
+ def subscribe (self , cluster_uuid , server_uuid , vclock = {}):
348
+ # FIXME rudnyh: ^ 'vclock={}'? really? sure?
351
349
request = RequestSubscribe (self , cluster_uuid , server_uuid , vclock )
352
350
resp = self ._send_request (request )
353
351
while True :
354
352
yield resp
355
353
if resp .code >= REQUEST_TYPE_ERROR :
356
354
return
357
355
resp = Response (self , self ._read_response ())
358
- self .close () # close connection after SUBSCRIBE
356
+ self .close () # close connection after SUBSCRIBE
359
357
360
358
def insert (self , space_name , values ):
361
359
'''
@@ -376,7 +374,7 @@ def insert(self, space_name, values):
376
374
return self ._send_request (request )
377
375
378
376
def delete (self , space_name , key , ** kwargs ):
379
- '''\
377
+ '''
380
378
Execute DELETE request.
381
379
Delete single record identified by `key` (using primary index).
382
380
@@ -398,7 +396,7 @@ def delete(self, space_name, key, **kwargs):
398
396
return self ._send_request (request )
399
397
400
398
def update (self , space_name , key , op_list , ** kwargs ):
401
- '''\
399
+ '''
402
400
Execute UPDATE request.
403
401
Update single record identified by `key` (using primary index).
404
402
@@ -428,7 +426,7 @@ def update(self, space_name, key, op_list, **kwargs):
428
426
return self ._send_request (request )
429
427
430
428
def ping (self , notime = False ):
431
- '''\
429
+ '''
432
430
Execute PING request.
433
431
Send empty request and receive empty response from server.
434
432
@@ -438,15 +436,15 @@ def ping(self, notime=False):
438
436
439
437
request = RequestPing (self )
440
438
t0 = time .time ()
441
- response = self ._send_request (request )
439
+ self ._send_request (request )
442
440
t1 = time .time ()
443
441
444
442
if notime :
445
443
return "Success"
446
444
return t1 - t0
447
445
448
446
def select (self , space_name , key = None , ** kwargs ):
449
- '''\
447
+ '''
450
448
Execute SELECT request.
451
449
Select and retrieve data from the database.
452
450
@@ -500,13 +498,13 @@ def select(self, space_name, key=None, **kwargs):
500
498
space_name = self .schema .get_space (space_name ).sid
501
499
if isinstance (index_name , basestring ):
502
500
index_name = self .schema .get_index (space_name , index_name ).iid
503
- request = RequestSelect (
504
- self , space_name , index_name , key , offset , limit , iterator_type )
501
+ request = RequestSelect (self , space_name , index_name , key , offset ,
502
+ limit , iterator_type )
505
503
response = self ._send_request (request )
506
504
return response
507
505
508
506
def space (self , space_name ):
509
- '''\
507
+ '''
510
508
Create `Space` instance for particular space
511
509
512
510
`Space` instance encapsulates the identifier of the space and provides
0 commit comments