3535
3636class ObjectArrayObject (GridGainDataType ):
3737 """
38- Array of objects of any type. Its Python representation is
39- tuple(type_id, iterable of any type).
38+ Array of Ignite objects of any consistent type. Its Python representation
39+ is tuple(type_id, iterable of any type). The only type ID that makes sense
40+ in Python client is :py:attr:`~OBJECT`, that corresponds directly to
41+ the root object type in Java type hierarchy (`java.lang.Object`).
4042 """
43+ OBJECT = - 1
44+
4145 _type_name = NAME_OBJ_ARR
4246 _type_id = TYPE_OBJ_ARR
4347 type_code = TC_OBJECT_ARRAY
44- type_or_id_name = 'type_id'
4548
4649 @staticmethod
4750 def hashcode (value : Iterable ) -> int :
@@ -95,7 +98,7 @@ def to_python(cls, ctype_object, *args, **kwargs):
9598 * args , ** kwargs
9699 )
97100 )
98- return getattr ( ctype_object , cls . type_or_id_name ) , result
101+ return ctype_object . type_id , result
99102
100103 @classmethod
101104 def from_python (cls , value ):
@@ -112,7 +115,7 @@ def from_python(cls, value):
112115 value = [value ]
113116 length = 1
114117 header .length = length
115- setattr ( header , cls . type_or_id_name , type_or_id )
118+ header . type_id = type_or_id
116119 buffer = bytearray (header )
117120
118121 for x in value :
@@ -176,18 +179,45 @@ def from_python(cls, value):
176179 raise ParseError ('Send unwrapped data.' )
177180
178181
179- class CollectionObject (ObjectArrayObject ):
182+ class CollectionObject (GridGainDataType ):
180183 """
181- Just like object array, but contains deserialization type hint instead of
182- type id. This hint is also useless in Python, because the list type along
183- covers all the use cases.
184-
185- Also represented as tuple(type_id, iterable of any type) in Python.
184+ Similar to object array, but contains platform-agnostic deserialization
185+ type hint instead of type ID.
186+
187+ Represented as tuple(hint, iterable of any type) in Python. Hints are:
188+
189+ * :py:attr:`~pygridgain.datatypes.complex.CollectionObject.USER_SET` −
190+ a set of unique Ignite thin data objects. The exact Java type of a set
191+ is undefined,
192+ * :py:attr:`~pygridgain.datatypes.complex.CollectionObject.USER_COL` −
193+ a collection of Ignite thin data objects. The exact Java type
194+ of a collection is undefined,
195+ * :py:attr:`~pygridgain.datatypes.complex.CollectionObject.ARR_LIST` −
196+ represents the `java.util.ArrayList` type,
197+ * :py:attr:`~pygridgain.datatypes.complex.CollectionObject.LINKED_LIST` −
198+ represents the `java.util.LinkedList` type,
199+ * :py:attr:`~pygridgain.datatypes.complex.CollectionObject.HASH_SET`−
200+ represents the `java.util.HashSet` type,
201+ * :py:attr:`~pygridgain.datatypes.complex.CollectionObject.LINKED_HASH_SET` −
202+ represents the `java.util.LinkedHashSet` type,
203+ * :py:attr:`~pygridgain.datatypes.complex.CollectionObject.SINGLETON_LIST` −
204+ represents the return type of the `java.util.Collection.singletonList`
205+ method.
206+
207+ It is safe to say that `USER_SET` (`set` in Python) and `USER_COL` (`list`)
208+ can cover all the imaginable use cases from Python perspective.
186209 """
210+ USER_SET = - 1
211+ USER_COL = 0
212+ ARR_LIST = 1
213+ LINKED_LIST = 2
214+ HASH_SET = 3
215+ LINKED_HASH_SET = 4
216+ SINGLETON_LIST = 5
217+
187218 _type_name = NAME_COL
188219 _type_id = TYPE_COL
189220 type_code = TC_COLLECTION
190- type_or_id_name = 'type'
191221 pythonic = list
192222 default = []
193223
@@ -211,13 +241,69 @@ def build_header(cls):
211241 }
212242 )
213243
244+ @classmethod
245+ def parse (cls , client : 'Client' ):
246+ header_class = cls .build_header ()
247+ buffer = client .recv (ctypes .sizeof (header_class ))
248+ header = header_class .from_buffer_copy (buffer )
249+ fields = []
250+
251+ for i in range (header .length ):
252+ c_type , buffer_fragment = AnyDataObject .parse (client )
253+ buffer += buffer_fragment
254+ fields .append (('element_{}' .format (i ), c_type ))
255+
256+ final_class = type (
257+ cls .__name__ ,
258+ (header_class ,),
259+ {
260+ '_pack_' : 1 ,
261+ '_fields_' : fields ,
262+ }
263+ )
264+ return final_class , buffer
265+
266+ @classmethod
267+ def to_python (cls , ctype_object , * args , ** kwargs ):
268+ result = []
269+ for i in range (ctype_object .length ):
270+ result .append (
271+ AnyDataObject .to_python (
272+ getattr (ctype_object , 'element_{}' .format (i )),
273+ * args , ** kwargs
274+ )
275+ )
276+ return ctype_object .type , result
277+
278+ @classmethod
279+ def from_python (cls , value ):
280+ type_or_id , value = value
281+ header_class = cls .build_header ()
282+ header = header_class ()
283+ header .type_code = int .from_bytes (
284+ cls .type_code ,
285+ byteorder = PROTOCOL_BYTE_ORDER
286+ )
287+ try :
288+ length = len (value )
289+ except TypeError :
290+ value = [value ]
291+ length = 1
292+ header .length = length
293+ header .type = type_or_id
294+ buffer = bytearray (header )
295+
296+ for x in value :
297+ buffer += infer_from_python (x )
298+ return bytes (buffer )
299+
214300
215301class Map (GridGainDataType ):
216302 """
217303 Dictionary type, payload-only.
218304
219- GridGain does not track the order of key-value pairs in its caches, hence
220- the ordinary Python dict type, not the collections.OrderedDict .
305+ Keys and values in map are independent data objects, but `count`
306+ counts pairs. Very annoying .
221307 """
222308 _type_name = NAME_MAP
223309 _type_id = TYPE_MAP
@@ -304,11 +390,12 @@ def from_python(cls, value, type_id=None):
304390
305391class MapObject (Map ):
306392 """
307- This is a dictionary type. Type conversion hint can be a `HASH_MAP`
308- (ordinary dict) or `LINKED_HASH_MAP` (collections.OrderedDict).
393+ This is a dictionary type.
309394
310- Keys and values in map are independent data objects, but `count`
311- counts pairs. Very annoying.
395+ Represented as tuple(type_id, value).
396+
397+ Type ID can be a :py:attr:`~HASH_MAP` (corresponds to an ordinary `dict`
398+ in Python) or a :py:attr:`~LINKED_HASH_MAP` (`collections.OrderedDict`).
312399 """
313400 _type_name = NAME_MAP
314401 _type_id = TYPE_MAP
0 commit comments