Skip to content
Open
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
1 change: 0 additions & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Configuration API Documentation
.. autoclass:: NearCacheConfig
.. autoclass:: FlakeIdGeneratorConfig
.. autoclass:: ReliableTopicConfig
.. autoclass:: IntType
.. autoclass:: EvictionPolicy
.. autoclass:: InMemoryFormat
.. autoclass:: SSLProtocol
Expand Down
15 changes: 15 additions & 0 deletions examples/number_types/number_types_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import asyncio

from hazelcast.asyncio import HazelcastClient
from hazelcast import Int32


async def amain():
client = await HazelcastClient.create_and_start()
map = await client.get_map("number_test")
await map.set("i8", Int32(10))
value_i8 = await map.get("i8")
assert type(value_i8) == int


asyncio.run(amain())
3 changes: 2 additions & 1 deletion hazelcast/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "5.7.0"
__version__ = "6.0.0"

# Set the default handler to "hazelcast" loggers
# to avoid "No handlers could be found" warnings.
Expand All @@ -7,3 +7,4 @@
logging.getLogger(__name__).addHandler(logging.NullHandler())

from hazelcast.client import HazelcastClient
from hazelcast.number_types import *
55 changes: 0 additions & 55 deletions hazelcast/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,6 @@
)


class IntType:
"""Integer type options that can be used by serialization service."""

VAR = 0
"""
Integer types will be serialized as 8, 16, 32, 64 bit integers
or as Java BigInteger according to their value. This option may
cause problems when the Python client is used in conjunction with
statically typed language clients such as Java or .NET.
"""

BYTE = 1
"""
Integer types will be serialized as a 8 bit integer(as Java byte)
"""

SHORT = 2
"""
Integer types will be serialized as a 16 bit integer(as Java short)
"""

INT = 3
"""
Integer types will be serialized as a 32 bit integer(as Java int)
"""

LONG = 4
"""
Integer types will be serialized as a 64 bit integer(as Java long)
"""

BIG_INT = 5
"""
Integer types will be serialized as Java BigInteger. This option can
handle integer types which are less than -2^63 or greater than or
equal to 2^63. However, when this option is set, serializing/de-serializing
integer types is costly.
"""


class EvictionPolicy:
"""Near Cache eviction policy options."""

Expand Down Expand Up @@ -309,7 +269,6 @@ class Config:
"_class_definitions",
"_check_class_definition_errors",
"_is_big_endian",
"_default_int_type",
"_global_serializer",
"_custom_serializers",
"_near_caches",
Expand Down Expand Up @@ -370,7 +329,6 @@ def __init__(self):
self._class_definitions: typing.List[ClassDefinition] = []
self._check_class_definition_errors: bool = True
self._is_big_endian: bool = True
self._default_int_type: int = IntType.INT
self._global_serializer: typing.Optional[typing.Type[StreamSerializer]] = None
self._custom_serializers: typing.Dict[
typing.Type[typing.Any], typing.Type[StreamSerializer]
Expand Down Expand Up @@ -1021,19 +979,6 @@ def is_big_endian(self, value: bool) -> None:

self._is_big_endian = value

@property
def default_int_type(self) -> int:
"""Defines how the ``int`` type is represented on the member side.

By default, it is serialized as ``INT`` (``32`` bits). See the
:class:`hazelcast.config.IntType` for possible values.
"""
return self._default_int_type

@default_int_type.setter
def default_int_type(self, value: typing.Union[int, str]) -> None:
self._default_int_type = try_to_get_enum_value(value, IntType)

@property
def global_serializer(self) -> typing.Optional[typing.Type[StreamSerializer]]:
"""Defines the global serializer.
Expand Down
162 changes: 162 additions & 0 deletions hazelcast/number_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
from typing import Self

from hazelcast.serialization import MIN_SHORT, MAX_SHORT, MIN_INT, MAX_INT, MIN_LONG, MAX_LONG
from hazelcast.serialization.bits import MIN_BYTE, MAX_BYTE

__all__ = "Int8", "Int16", "Int32", "Int64", "Float32", "Float64", "BigInt"


class Int8:
"""Int8 represents an 8-bit signed integer

Corresponds to Java ``byte``
"""

MIN_VALUE = MIN_BYTE
MAX_VALUE = MAX_BYTE

def __init__(self, value: int):
if not (self.MIN_VALUE <= value <= self.MAX_VALUE):
raise ValueError(
"{} value must be between {} and {}".format(
self.__class__.__name__,
self.MIN_VALUE,
self.MAX_VALUE,
)
)
self.value = value

def __int__(self):
return self.value

def __repr__(self) -> str:
return str(self.value)


class Int16:
"""Int16 represents a 16-bit signed integer

Corresponds to Java ``short``.
"""

MIN_VALUE = MIN_SHORT
MAX_VALUE = MAX_SHORT

def __init__(self, value: int):
if not (self.MIN_VALUE <= value <= self.MAX_VALUE):
raise ValueError(
"{} value must be between {} and {}".format(
self.__class__.__name__,
self.MIN_VALUE,
self.MAX_VALUE,
)
)
self.value = value

def __int__(self):
return self.value

def __repr__(self) -> str:
return str(self.value)


class Int32:
"""Int32 represents a 32-bit signed integer

Corresponds to Java ``int``.
"""

MIN_VALUE = MIN_INT
MAX_VALUE = MAX_INT

def __init__(self, value: int):
if not (self.MIN_VALUE <= value <= self.MAX_VALUE):
raise ValueError(
"{} value must be between {} and {}".format(
self.__class__.__name__,
self.MIN_VALUE,
self.MAX_VALUE,
)
)
self.value = value

def __int__(self):
return self.value

def __repr__(self) -> str:
return str(self.value)


class Int64:
"""Int64 represents a 64-bit signed integer

Corresponds to Java ``long``.
"""

MIN_VALUE = MIN_LONG
MAX_VALUE = MAX_LONG

def __init__(self, value: int):
if not (self.MIN_VALUE <= value <= self.MAX_VALUE):
raise ValueError(
"{} value must be between {} and {}".format(
self.__class__.__name__,
self.MIN_VALUE,
self.MAX_VALUE,
)
)
self.value = value

def __int__(self):
return self.value

def __repr__(self) -> str:
return str(self.value)


class BigInt:
"""BigInt represents a big integer

Corresponds to Java ``java.math.BigInteger``.
"""

def __init__(self, value: int):
self.value = value

def __int__(self):
return self.value

def __repr__(self) -> str:
return str(self.value)


class Float32:
"""Float32 represents a 32-bit floating point number

Corresponds to Java ``float``.
"""

def __init__(self, value: float | int):
self.value = float(value)

def __float__(self):
return self.value

def __repr__(self) -> str:
return str(self.value)


class Float64:
"""Float32 represents a 64-bit floating point number

Corresponds to Java ``double``.
"""

def __init__(self, value: float | int):
self.value = float(value)

def __float__(self):
return self.value

def __repr__(self) -> str:
return str(self.value)
15 changes: 8 additions & 7 deletions hazelcast/serialization/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def read(self, inp):
return inp.read_byte()

def write(self, out, obj):
out.write_byte(obj)
out.write_byte(int(obj))

def get_type_id(self):
return CONSTANT_TYPE_BYTE
Expand All @@ -63,7 +63,7 @@ def read(self, inp):
return inp.read_short()

def write(self, out, obj):
out.write_short(obj)
out.write_short(int(obj))

def get_type_id(self):
return CONSTANT_TYPE_SHORT
Expand All @@ -74,7 +74,7 @@ def read(self, inp):
return inp.read_int()

def write(self, out, obj):
out.write_int(obj)
out.write_int(int(obj))

def get_type_id(self):
return CONSTANT_TYPE_INTEGER
Expand All @@ -85,7 +85,7 @@ def read(self, inp):
return inp.read_long()

def write(self, out, obj):
out.write_long(obj)
out.write_long(int(obj))

def get_type_id(self):
return CONSTANT_TYPE_LONG
Expand All @@ -95,7 +95,8 @@ class FloatSerializer(BaseSerializer):
def read(self, inp):
return inp.read_float()

# "write(self, out, obj)" is never called so not implemented here
def write(self, out, obj):
out.write_float(float(obj))

def get_type_id(self):
return CONSTANT_TYPE_FLOAT
Expand All @@ -106,7 +107,7 @@ def read(self, inp):
return inp.read_double()

def write(self, out, obj):
out.write_double(obj)
out.write_double(float(obj))

def get_type_id(self):
return CONSTANT_TYPE_DOUBLE
Expand Down Expand Up @@ -247,7 +248,7 @@ def read(self, inp):
return IOUtil.read_big_integer(inp)

def write(self, out, obj):
IOUtil.write_big_integer(out, obj)
IOUtil.write_big_integer(out, int(obj))

def get_type_id(self):
return JAVA_DEFAULT_TYPE_BIG_INTEGER
Expand Down
Loading
Loading