3636from ..stream import READ_BACKWARD
3737
3838
39- def tc_map (key : bytes , _memo_map : dict = {}):
39+ _tc_map = {}
40+
41+
42+ def tc_map (key : bytes ):
4043 """
4144 Returns a default parser/generator class for the given type code.
4245
@@ -49,7 +52,8 @@ def tc_map(key: bytes, _memo_map: dict = {}):
4952 of the “type code-type class” mapping,
5053 :return: parser/generator class for the type code.
5154 """
52- if not _memo_map :
55+ global _tc_map
56+ if not _tc_map :
5357 from pyignite .datatypes import (
5458 Null , ByteObject , ShortObject , IntObject , LongObject , FloatObject ,
5559 DoubleObject , CharObject , BoolObject , UUIDObject , DateObject ,
@@ -64,7 +68,7 @@ def tc_map(key: bytes, _memo_map: dict = {}):
6468 MapObject , BinaryObject , WrappedDataObject ,
6569 )
6670
67- _memo_map = {
71+ _tc_map = {
6872 TC_NULL : Null ,
6973
7074 TC_BYTE : ByteObject ,
@@ -110,7 +114,7 @@ def tc_map(key: bytes, _memo_map: dict = {}):
110114 TC_COMPLEX_OBJECT : BinaryObject ,
111115 TC_ARRAY_WRAPPED_OBJECTS : WrappedDataObject ,
112116 }
113- return _memo_map [key ]
117+ return _tc_map [key ]
114118
115119
116120class Conditional :
@@ -183,7 +187,7 @@ async def parse_async(self, stream):
183187 def __parse_length (self , stream ):
184188 counter_type_len = ctypes .sizeof (self .counter_type )
185189 length = int .from_bytes (
186- stream .mem_view (offset = counter_type_len ),
190+ stream .slice (offset = counter_type_len ),
187191 byteorder = PROTOCOL_BYTE_ORDER
188192 )
189193 stream .seek (counter_type_len , SEEK_CUR )
@@ -348,6 +352,9 @@ class AnyDataObject:
348352 """
349353 _python_map = None
350354 _python_array_map = None
355+ _map_obj_type = None
356+ _collection_obj_type = None
357+ _binary_obj_type = None
351358
352359 @staticmethod
353360 def get_subtype (iterable , allow_none = False ):
@@ -391,7 +398,7 @@ async def parse_async(cls, stream):
391398
392399 @classmethod
393400 def __data_class_parse (cls , stream ):
394- type_code = bytes ( stream .mem_view (offset = ctypes .sizeof (ctypes .c_byte ) ))
401+ type_code = stream .slice (offset = ctypes .sizeof (ctypes .c_byte ))
395402 try :
396403 return tc_map (type_code )
397404 except KeyError :
@@ -416,15 +423,17 @@ def __data_class_from_ctype(cls, ctype_object):
416423 return tc_map (type_code )
417424
418425 @classmethod
419- def _init_python_map (cls ):
426+ def _init_python_mapping (cls ):
420427 """
421428 Optimizes Python types→Ignite types map creation for speed.
422429
423430 Local imports seem inevitable here.
424431 """
425432 from pyignite .datatypes import (
426- LongObject , DoubleObject , String , BoolObject , Null , UUIDObject ,
427- DateObject , TimeObject , DecimalObject , ByteArrayObject ,
433+ LongObject , DoubleObject , String , BoolObject , Null , UUIDObject , DateObject , TimeObject ,
434+ DecimalObject , ByteArrayObject , LongArrayObject , DoubleArrayObject , StringArrayObject ,
435+ BoolArrayObject , UUIDArrayObject , DateArrayObject , TimeArrayObject , DecimalArrayObject ,
436+ MapObject , CollectionObject , BinaryObject
428437 )
429438
430439 cls ._python_map = {
@@ -442,17 +451,6 @@ def _init_python_map(cls):
442451 decimal .Decimal : DecimalObject ,
443452 }
444453
445- @classmethod
446- def _init_python_array_map (cls ):
447- """
448- Optimizes Python types→Ignite array types map creation for speed.
449- """
450- from pyignite .datatypes import (
451- LongArrayObject , DoubleArrayObject , StringArrayObject ,
452- BoolArrayObject , UUIDArrayObject , DateArrayObject , TimeArrayObject ,
453- DecimalArrayObject ,
454- )
455-
456454 cls ._python_array_map = {
457455 int : LongArrayObject ,
458456 float : DoubleArrayObject ,
@@ -466,18 +464,20 @@ def _init_python_array_map(cls):
466464 decimal .Decimal : DecimalArrayObject ,
467465 }
468466
467+ cls ._map_obj_type = MapObject
468+ cls ._collection_obj_type = CollectionObject
469+ cls ._binary_obj_type = BinaryObject
470+
469471 @classmethod
470472 def map_python_type (cls , value ):
471- from pyignite .datatypes import (
472- MapObject , CollectionObject , BinaryObject ,
473- )
474-
475- if cls ._python_map is None :
476- cls ._init_python_map ()
477- if cls ._python_array_map is None :
478- cls ._init_python_array_map ()
473+ if cls ._python_map is None or cls ._python_array_map is None :
474+ cls ._init_python_mapping ()
479475
480476 value_type = type (value )
477+
478+ if value_type in cls ._python_map :
479+ return cls ._python_map [value_type ]
480+
481481 if is_iterable (value ) and value_type not in (str , bytearray , bytes ):
482482 value_subtype = cls .get_subtype (value )
483483 if value_subtype in cls ._python_array_map :
@@ -490,15 +490,15 @@ def map_python_type(cls, value):
490490 isinstance (value [0 ], int ),
491491 isinstance (value [1 ], dict ),
492492 ]):
493- return MapObject
493+ return cls . _map_obj_type
494494
495495 if all ([
496496 value_subtype is None ,
497497 len (value ) == 2 ,
498498 isinstance (value [0 ], int ),
499499 is_iterable (value [1 ]),
500500 ]):
501- return CollectionObject
501+ return cls . _collection_obj_type
502502
503503 # no default for ObjectArrayObject, sorry
504504
@@ -507,10 +507,8 @@ def map_python_type(cls, value):
507507 )
508508
509509 if is_binary (value ):
510- return BinaryObject
510+ return cls . _binary_obj_type
511511
512- if value_type in cls ._python_map :
513- return cls ._python_map [value_type ]
514512 raise TypeError (
515513 'Type `{}` is invalid.' .format (value_type )
516514 )
0 commit comments