Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions hazelcast/serialization/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
from hazelcast import six


def empty_partitioning_strategy(key):
_int_type_to_type_id = {
IntType.BYTE: CONSTANT_TYPE_BYTE,
IntType.SHORT: CONSTANT_TYPE_SHORT,
IntType.INT: CONSTANT_TYPE_INTEGER,
IntType.LONG: CONSTANT_TYPE_LONG,
IntType.BIG_INT: JAVA_DEFAULT_TYPE_BIG_INTEGER,
}


def empty_partitioning_strategy(_):
return None


Expand Down Expand Up @@ -85,6 +94,7 @@ def to_object(self, data):
"""
if not isinstance(data, Data):
return data

if is_null_data(data):
return None

Expand Down Expand Up @@ -163,6 +173,7 @@ def __init__(self, int_type):

self._registration_lock = RLock()
self.int_type = int_type
self._int_type_id = _int_type_to_type_id.get(int_type, None)

def serializer_by_type_id(self, type_id):
"""Find and return the serializer for the type-id
Expand Down Expand Up @@ -226,26 +237,18 @@ def serializer_for(self, obj):
def lookup_default_serializer(self, obj_type, obj):
if isinstance(obj, IdentifiedDataSerializable):
return self._data_serializer

if isinstance(obj, Portable):
return self._portable_serializer

if isinstance(obj, six.string_types):
return self.serializer_by_type_id(CONSTANT_TYPE_STRING)

type_id = None
# LOCATE NUMERIC TYPES
if obj_type in six.integer_types:
if self.int_type == IntType.BYTE:
type_id = CONSTANT_TYPE_BYTE
elif self.int_type == IntType.SHORT:
type_id = CONSTANT_TYPE_SHORT
elif self.int_type == IntType.INT:
type_id = CONSTANT_TYPE_INTEGER
elif self.int_type == IntType.LONG:
type_id = CONSTANT_TYPE_LONG
elif self.int_type == IntType.BIG_INT:
type_id = JAVA_DEFAULT_TYPE_BIG_INTEGER
elif self.int_type == IntType.VAR:
type_id = self._int_type_id
if type_id is None:
# VAR size
if MIN_BYTE <= obj <= MAX_BYTE:
type_id = CONSTANT_TYPE_BYTE
elif MIN_SHORT <= obj <= MAX_SHORT:
Expand All @@ -256,8 +259,8 @@ def lookup_default_serializer(self, obj_type, obj):
type_id = CONSTANT_TYPE_LONG
else:
type_id = JAVA_DEFAULT_TYPE_BIG_INTEGER
if type_id:
return self.serializer_by_type_id(type_id)

return self.serializer_by_type_id(type_id)

return self._constant_type_dict.get(obj_type, None)

Expand Down
26 changes: 8 additions & 18 deletions hazelcast/serialization/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Data(object):
It stores binary form of an object serialized by serialization service.
"""

def __init__(self, buff=None):
self._buffer = buff
def __init__(self, buf):
self._buffer = buf

def to_bytes(self):
"""Returns byte array representation of internal binary format.
Expand All @@ -33,7 +33,7 @@ def get_type(self):
Returns:
int: Serialization type of binary form.
"""
if self.total_size() == 0:
if len(self._buffer) == 0:
return CONSTANT_TYPE_NULL
return BE_INT.unpack_from(self._buffer, TYPE_OFFSET)[0]

Expand All @@ -43,15 +43,15 @@ def total_size(self):
Returns:
int: Total size of Data in bytes.
"""
return len(self._buffer) if self._buffer is not None else 0
return len(self._buffer)

def data_size(self):
"""Returns size of internal binary data in bytes.

Returns:
int: Size of internal binary data in bytes.
"""
return max(self.total_size() - HEAP_DATA_OVERHEAD, 0)
return max(len(self._buffer) - HEAP_DATA_OVERHEAD, 0)

def get_partition_hash(self):
"""Returns partition hash calculated for serialized object.
Expand All @@ -64,8 +64,9 @@ def get_partition_hash(self):
Returns:
int: Partition hash.
"""
if self.has_partition_hash():
return BE_INT.unpack_from(self._buffer, PARTITION_HASH_OFFSET)[0]
partition_hash = BE_INT.unpack_from(self._buffer, PARTITION_HASH_OFFSET)[0]
if partition_hash != 0:
return partition_hash
return self.hash_code()

def is_portable(self):
Expand All @@ -76,17 +77,6 @@ def is_portable(self):
"""
return CONSTANT_TYPE_PORTABLE == self.get_type()

def has_partition_hash(self):
"""Determines whether this ``Data`` has partition hash or not.

Returns:
bool: ``True`` if ``Data`` has partition hash, ``False`` otherwise.

"""
return self._buffer is not None \
and len(self._buffer) >= HEAP_DATA_OVERHEAD \
and BE_INT.unpack_from(self._buffer, PARTITION_HASH_OFFSET)[0] != 0

def hash_code(self):
"""Returns the murmur hash of the internal data.

Expand Down
1 change: 0 additions & 1 deletion hazelcast/serialization/input.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from hazelcast.serialization.api import *
from hazelcast.serialization.bits import *
from hazelcast.serialization.data import Data
from hazelcast import six
from hazelcast.six.moves import range

Expand Down
22 changes: 7 additions & 15 deletions hazelcast/serialization/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ def write_object(self, val):
self._service.write_object(self, val)

def to_byte_array(self):
if self._buffer is None or self._pos == 0:
return bytearray()
new_buffer = bytearray(self._pos)
new_buffer[:] = self._buffer[:self._pos]
return new_buffer
return self._buffer[:self._pos]

def is_big_endian(self):
return self._is_big_endian
Expand All @@ -148,18 +144,14 @@ def _write_array_fnc(self, val, item_write_fnc):

def _ensure_available(self, length):
if self._available() < length:
if self._buffer is not None:
buffer_length = len(self._buffer)
new_length = max(buffer_length << 1, buffer_length + length)
new_buffer = bytearray(new_length)
new_buffer[:self._pos] = self._buffer[:self._pos]
self._buffer = new_buffer
else:
new_length = length * 2 if length > self._init_size // 2 else self._init_size
self._buffer = bytearray(new_length)
buffer_length = len(self._buffer)
new_length = max(buffer_length << 1, buffer_length + length)
new_buffer = bytearray(new_length)
new_buffer[:self._pos] = self._buffer[:self._pos]
self._buffer = new_buffer

def _available(self):
return len(self._buffer) - self._pos if self._buffer is not None else 0
return len(self._buffer) - self._pos

def __repr__(self):
from binascii import hexlify
Expand Down
4 changes: 0 additions & 4 deletions tests/data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ def test_data(self):
self.assertEqual(self._total_size, self._data.total_size())
self.assertEqual(self._total_size - DATA_OFFSET, self._data.data_size())
self.assertEqual(0x01020304, self._data.get_type())
self.assertTrue(self._data.has_partition_hash())
self.assertFalse(self._data.is_portable())
self.assertEqual(1545424565, self._data.hash_code())
self.assertEqual(0x12345678, self._data.get_partition_hash())

def test_data_len(self):
self.assertEqual(10, len(Data("1"* 10)))

if __name__ == '__main__':
unittest.main()
4 changes: 2 additions & 2 deletions tests/serialization/serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_python_pickle_serialization_with_super_type(self):
self.assertEqual(obj, obj2)

def test_null_data(self):
data = Data()
data = Data(bytearray(0))
obj = self.service.to_object(data)
self.assertIsNone(obj)

Expand All @@ -73,7 +73,7 @@ def test_none_serialize(self):
self.assertIsNone(data)

def test_serialize_data(self):
data = Data()
data = Data(bytearray(0))
obj = self.service.to_data(data)
self.assertTrue(isinstance(obj, Data))

Expand Down
8 changes: 2 additions & 6 deletions tests/serialization/string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_ascii_encode(self):

def test_ascii_decode(self):
data_byte = to_data_byte(TEST_DATA_ASCII)
data = Data(buff=data_byte)
data = Data(data_byte)
actual_ascii = self.service.to_object(data)
self.assertEqual(TEST_DATA_ASCII, actual_ascii)

Expand All @@ -54,15 +54,11 @@ def test_utf8_encode(self):

def test_utf8_decode(self):
data_byte = to_data_byte(TEST_DATA_ALL)
data = Data(buff=data_byte)
data = Data(data_byte)
actual_ascii = self.service.to_object(data)
self.assertEqual(TEST_DATA_ALL, actual_ascii)

def test_None_str_encode_decode(self):
none_str = self.service.to_data(None)
decoded = self.service.to_object(none_str)
self.assertIsNone(decoded)


if __name__ == '__main__':
unittest.main()