1
- import socket
1
+ import asyncio
2
2
import hashlib
3
3
4
- from aerospike_py .connection import SocketConnection
4
+ from aerospike_py .connection import AsyncConnection
5
5
from aerospike_py .info import request_info_keys
6
6
from aerospike_py .result_code import ASMSGProtocolException
7
7
from aerospike_py .message import ASIOException
@@ -16,11 +16,11 @@ def hash_key(set='', key=''):
16
16
17
17
18
18
class AerospikeClient :
19
- def __init__ (self , sck ):
20
- self .sck = sck
19
+ def __init__ (self , conn ):
20
+ self .conn = conn
21
21
22
22
def info (self , keys ):
23
- return request_info_keys (self .sck , keys )[1 ]
23
+ return request_info_keys (self .conn , keys )[1 ]
24
24
25
25
def _process_bucket (self , asmsg_ops ):
26
26
buckets = {}
@@ -29,10 +29,11 @@ def _process_bucket(self, asmsg_ops):
29
29
30
30
return buckets
31
31
32
+ @asyncio .coroutine
32
33
def _submit_message (self , envelope , retry_count = 3 ):
33
34
while retry_count :
34
35
try :
35
- outer , asmsg_hdr , asmsg_fields , asmsg_ops = aerospike_py .message .submit_message (self .sck , envelope )
36
+ outer , asmsg_hdr , asmsg_fields , asmsg_ops = yield from aerospike_py .message .submit_message (self .conn , envelope )
36
37
return self ._process_bucket (asmsg_ops )
37
38
except ASMSGProtocolException as e :
38
39
if e .result_code not in (14 ,):
@@ -45,10 +46,11 @@ def _submit_message(self, envelope, retry_count=3):
45
46
46
47
return buckets
47
48
49
+ @asyncio .coroutine
48
50
def _submit_batch (self , envelope , retry_count = 3 ):
49
51
while retry_count :
50
52
try :
51
- messages = aerospike_py .message .submit_multi_message (self .sck , envelope )
53
+ messages = yield from aerospike_py .message .submit_multi_message (self .conn , envelope )
52
54
return [self ._process_bucket (x [3 ]) for x in messages ]
53
55
except ASMSGProtocolException as e :
54
56
if e .result_code not in (14 ,):
@@ -61,6 +63,7 @@ def _submit_batch(self, envelope, retry_count=3):
61
63
62
64
return buckets
63
65
66
+ @asyncio .coroutine
64
67
def get (self , namespace , set = '' , key = '' , bins = [], record_ttl = 0 , retry_count = 3 ):
65
68
flags = aerospike_py .message .AS_INFO1_READ
66
69
if not bins :
@@ -77,6 +80,7 @@ def get(self, namespace, set='', key='', bins=[], record_ttl=0, retry_count=3):
77
80
78
81
return self ._submit_message (envelope , retry_count )
79
82
83
+ @asyncio .coroutine
80
84
def mget (self , namespace , groups = [], bins = {}, record_ttl = 0 , retry_count = 3 ):
81
85
flags = aerospike_py .message .AS_INFO1_READ # | aerospike_py.message.AS_INFO1_BATCH
82
86
if not bins :
@@ -98,6 +102,7 @@ def mget(self, namespace, groups=[], bins={}, record_ttl=0, retry_count=3):
98
102
99
103
return self ._submit_batch (envelope , retry_count )
100
104
105
+ @asyncio .coroutine
101
106
def put (self , namespace , set = '' , key = '' , bins = {}, create_only = False , bin_create_only = False , record_ttl = 0 , retry_count = 3 ):
102
107
flags = aerospike_py .message .AS_INFO2_WRITE
103
108
if create_only :
@@ -118,6 +123,7 @@ def put(self, namespace, set='', key='', bins={}, create_only=False, bin_create_
118
123
119
124
return self ._submit_message (envelope , retry_count )
120
125
126
+ @asyncio .coroutine
121
127
def delete (self , namespace , set = '' , key = '' , record_ttl = 0 , retry_count = 3 ):
122
128
envelope = aerospike_py .message .pack_asmsg (0 , aerospike_py .message .AS_INFO2_WRITE | aerospike_py .message .AS_INFO2_DELETE , 0 , 0 , record_ttl , 0 ,
123
129
[
@@ -129,6 +135,7 @@ def delete(self, namespace, set='', key='', record_ttl=0, retry_count=3):
129
135
130
136
return self ._submit_message (envelope , retry_count )
131
137
138
+ @asyncio .coroutine
132
139
def incr (self , namespace , set = '' , key = '' , bin = '' , incr_by = 0 , record_ttl = 0 , retry_count = 3 ):
133
140
flags = aerospike_py .message .AS_INFO2_WRITE
134
141
@@ -143,6 +150,7 @@ def incr(self, namespace, set='', key='', bin='', incr_by=0, record_ttl=0, retry
143
150
144
151
return self ._submit_message (envelope , retry_count )
145
152
153
+ @asyncio .coroutine
146
154
def _append_op (self , namespace , set = '' , key = '' , bin = '' , append_blob = '' , op = aerospike_py .message .AS_MSG_OP_APPEND , record_ttl = 0 , retry_count = 3 ):
147
155
flags = aerospike_py .message .AS_INFO2_WRITE
148
156
@@ -158,16 +166,19 @@ def _append_op(self, namespace, set='', key='', bin='', append_blob='', op=aeros
158
166
159
167
return self ._submit_message (envelope , retry_count )
160
168
169
+ @asyncio .coroutine
161
170
def append (self , namespace , set = '' , key = '' , bin = '' , append_blob = '' , record_ttl = 0 ):
162
171
return self ._append_op (namespace , set , key , bin , append_blob , aerospike_py .message .AS_MSG_OP_APPEND , record_ttl )
163
172
173
+ @asyncio .coroutine
164
174
def prepend (self , namespace , set = '' , key = '' , bin = '' , append_blob = '' , record_ttl = 0 ):
165
175
return self ._append_op (namespace , set , key , bin , append_blob , aerospike_py .message .AS_MSG_OP_PREPEND , record_ttl )
166
176
177
+ @asyncio .coroutine
167
178
def touch (self , namespace , set , key , bin = '' , record_ttl = 0 ):
168
179
return self ._append_op (namespace , set , key , bin , None , aerospike_py .message .AS_MSG_OP_TOUCH , record_ttl )
169
180
170
181
171
182
def connect (host : str , port : int ) -> AerospikeClient :
172
- return AerospikeClient (SocketConnection ( socket . create_connection (( host , port )) ))
183
+ return AerospikeClient (AsyncConnection ( host , port ))
173
184
0 commit comments