-
Notifications
You must be signed in to change notification settings - Fork 55
/
__init__.py
339 lines (298 loc) · 11.2 KB
/
__init__.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
#
# Copyright 2019 GridGain Systems, Inc. and Contributors.
#
# Licensed under the GridGain Community Edition License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
This module is a source of some basic information about the binary protocol.
Most importantly, it contains `Query` and `Response` base classes. They are
used internally by :mod:`pyignite.api` module. They, in turn, based on
:mod:`pyignite.datatypes` binary parser/generator classes.
"""
from collections import OrderedDict
import ctypes
from random import randint
import attr
from pyignite.api.result import APIResult
from pyignite.constants import *
from pyignite.datatypes import (
AnyDataObject, Bool, Int, Long, String, StringArray, Struct,
)
from .op_codes import *
@attr.s
class Response:
following = attr.ib(type=list, factory=list)
_response_header = None
def __attrs_post_init__(self):
# replace None with empty list
self.following = self.following or []
@classmethod
def build_header(cls):
if cls._response_header is None:
cls._response_header = type(
'ResponseHeader',
(ctypes.LittleEndianStructure,),
{
'_pack_': 1,
'_fields_': [
('length', ctypes.c_int),
('query_id', ctypes.c_longlong),
('status_code', ctypes.c_int),
],
},
)
return cls._response_header
def parse(self, client: 'Client'):
header_class = self.build_header()
buffer = client.recv(ctypes.sizeof(header_class))
header = header_class.from_buffer_copy(buffer)
fields = []
if header.status_code == OP_SUCCESS:
for name, ignite_type in self.following:
c_type, buffer_fragment = ignite_type.parse(client)
buffer += buffer_fragment
fields.append((name, c_type))
else:
c_type, buffer_fragment = String.parse(client)
buffer += buffer_fragment
fields.append(('error_message', c_type))
response_class = type(
'Response',
(header_class,),
{
'_pack_': 1,
'_fields_': fields,
}
)
return response_class, buffer
def to_python(self, ctype_object, *args, **kwargs):
result = OrderedDict()
for name, c_type in self.following:
result[name] = c_type.to_python(
getattr(ctype_object, name),
*args, **kwargs
)
return result if result else None
@attr.s
class SQLResponse(Response):
"""
The response class of SQL functions is special in the way the row-column
data is counted in it. Basically, Ignite thin client API is following a
“counter right before the counted objects” rule in most of its parts.
SQL ops are breaking this rule.
"""
include_field_names = attr.ib(type=bool, default=False)
has_cursor = attr.ib(type=bool, default=False)
def fields_or_field_count(self):
if self.include_field_names:
return 'fields', StringArray
return 'field_count', Int
def parse(self, client: 'Client'):
header_class = self.build_header()
buffer = client.recv(ctypes.sizeof(header_class))
header = header_class.from_buffer_copy(buffer)
fields = []
if header.status_code == OP_SUCCESS:
following = [
self.fields_or_field_count(),
('row_count', Int),
]
if self.has_cursor:
following.insert(0, ('cursor', Long))
body_struct = Struct(following)
body_class, body_buffer = body_struct.parse(client)
body = body_class.from_buffer_copy(body_buffer)
if self.include_field_names:
field_count = body.fields.length
else:
field_count = body.field_count
data_fields = []
data_buffer = b''
for i in range(body.row_count):
row_fields = []
row_buffer = b''
for j in range(field_count):
field_class, field_buffer = AnyDataObject.parse(client)
row_fields.append(('column_{}'.format(j), field_class))
row_buffer += field_buffer
row_class = type(
'SQLResponseRow',
(ctypes.LittleEndianStructure,),
{
'_pack_': 1,
'_fields_': row_fields,
}
)
data_fields.append(('row_{}'.format(i), row_class))
data_buffer += row_buffer
data_class = type(
'SQLResponseData',
(ctypes.LittleEndianStructure,),
{
'_pack_': 1,
'_fields_': data_fields,
}
)
fields += body_class._fields_ + [
('data', data_class),
('more', ctypes.c_bool),
]
buffer += body_buffer + data_buffer
else:
c_type, buffer_fragment = String.parse(client)
buffer += buffer_fragment
fields.append(('error_message', c_type))
final_class = type(
'SQLResponse',
(header_class,),
{
'_pack_': 1,
'_fields_': fields,
}
)
buffer += client.recv(ctypes.sizeof(final_class) - len(buffer))
return final_class, buffer
def to_python(self, ctype_object, *args, **kwargs):
if ctype_object.status_code == 0:
result = {
'more': Bool.to_python(
ctype_object.more, *args, **kwargs
),
'data': [],
}
if hasattr(ctype_object, 'fields'):
result['fields'] = StringArray.to_python(
ctype_object.fields, *args, **kwargs
)
else:
result['field_count'] = Int.to_python(
ctype_object.field_count, *args, **kwargs
)
if hasattr(ctype_object, 'cursor'):
result['cursor'] = Long.to_python(
ctype_object.cursor, *args, **kwargs
)
for row_item in ctype_object.data._fields_:
row_name = row_item[0]
row_object = getattr(ctype_object.data, row_name)
row = []
for col_item in row_object._fields_:
col_name = col_item[0]
col_object = getattr(row_object, col_name)
row.append(
AnyDataObject.to_python(col_object, *args, **kwargs)
)
result['data'].append(row)
return result
@attr.s
class Query:
op_code = attr.ib(type=int)
following = attr.ib(type=list, factory=list)
query_id = attr.ib(type=int, default=None)
_query_c_type = None
@classmethod
def build_c_type(cls):
if cls._query_c_type is None:
cls._query_c_type = type(
cls.__name__,
(ctypes.LittleEndianStructure,),
{
'_pack_': 1,
'_fields_': [
('length', ctypes.c_int),
('op_code', ctypes.c_short),
('query_id', ctypes.c_longlong),
],
},
)
return cls._query_c_type
def from_python(self, values: dict=None):
if values is None:
values = {}
buffer = b''
header_class = self.build_c_type()
header = header_class()
header.op_code = self.op_code
if self.query_id is None:
header.query_id = randint(MIN_LONG, MAX_LONG)
for name, c_type in self.following:
buffer += c_type.from_python(values[name])
header.length = (
len(buffer)
+ ctypes.sizeof(header_class)
- ctypes.sizeof(ctypes.c_int)
)
return header.query_id, bytes(header) + buffer
def perform(
self, conn: 'Connection', query_params: dict=None,
response_config: list=None,
) -> APIResult:
"""
Perform query and process result.
:param conn: connection to Ignite server,
:param query_params: (optional) dict of named query parameters.
Defaults to no parameters,
:param response_config: (optional) response configuration − list of
(name, type_hint) tuples. Defaults to empty return value,
:return: instance of :class:`~pyignite.api.result.APIResult` with raw
value (may undergo further processing in API functions).
"""
_, send_buffer = self.from_python(query_params)
conn.send(send_buffer)
response_struct = Response(response_config)
response_ctype, recv_buffer = response_struct.parse(conn)
response = response_ctype.from_buffer_copy(recv_buffer)
result = APIResult(response)
if result.status == 0:
result.value = response_struct.to_python(response)
return result
class ConfigQuery(Query):
"""
This is a special query, used for creating caches with configuration.
"""
_query_c_type = None
@classmethod
def build_c_type(cls):
if cls._query_c_type is None:
cls._query_c_type = type(
cls.__name__,
(ctypes.LittleEndianStructure,),
{
'_pack_': 1,
'_fields_': [
('length', ctypes.c_int),
('op_code', ctypes.c_short),
('query_id', ctypes.c_longlong),
('config_length', ctypes.c_int),
],
},
)
return cls._query_c_type
def from_python(self, values: dict = None):
if values is None:
values = {}
buffer = b''
header_class = self.build_c_type()
header = header_class()
header.op_code = self.op_code
if self.query_id is None:
header.query_id = randint(MIN_LONG, MAX_LONG)
for name, c_type in self.following:
buffer += c_type.from_python(values[name])
header.length = (
len(buffer)
+ ctypes.sizeof(header_class)
- ctypes.sizeof(ctypes.c_int)
)
header.config_length = header.length - ctypes.sizeof(header_class)
return header.query_id, bytes(header) + buffer