コード例 #1
0
    def test_portable_class_def(self):
        builder_inner = ClassDefinitionBuilder(FACTORY_ID,
                                               InnerPortable.CLASS_ID)
        builder_inner.add_string_field("param_str")
        builder_inner.add_int_field("param_int")
        class_def_inner = builder_inner.build()

        builder = ClassDefinitionBuilder(FACTORY_ID,
                                         SerializationV1Portable.CLASS_ID)

        builder.add_byte_field("1")
        builder.add_boolean_field("2")
        builder.add_char_field("3")
        builder.add_short_field("4")
        builder.add_int_field("5")
        builder.add_long_field("6")
        builder.add_float_field("7")
        builder.add_double_field("8")
        builder.add_string_field("9")
        builder.add_byte_array_field("a1")
        builder.add_boolean_array_field("a2")
        builder.add_char_array_field("a3")
        builder.add_short_array_field("a4")
        builder.add_int_array_field("a5")
        builder.add_long_array_field("a6")
        builder.add_float_array_field("a7")
        builder.add_double_array_field("a8")
        builder.add_string_array_field("a9")
        builder.add_portable_field("p", class_def_inner)
        builder.add_portable_array_field("ap", class_def_inner)
        class_def = builder.build()

        config = _Config()
        config.portable_factories = {FACTORY_ID: the_factory}
        config.class_definitions = [
            class_def,
            class_def_inner,
        ]

        service = SerializationServiceV1(config)
        service2 = SerializationServiceV1(config)
        obj = SerializationV1Portable()

        data = service.to_data(obj)
        obj2 = service2.to_object(data)
        self.assertTrue(obj == obj2)
コード例 #2
0
class ClassDefinitionWriter(PortableWriter):
    def __init__(
        self, portable_context, factory_id=None, class_id=None, version=None, class_def_builder=None
    ):
        self.portable_context = portable_context
        if class_def_builder is None:
            self._builder = ClassDefinitionBuilder(factory_id, class_id, version)
        else:
            self._builder = class_def_builder

    def write_byte(self, field_name, value):
        self._builder.add_byte_field(field_name)

    def write_boolean(self, field_name, value):
        self._builder.add_boolean_field(field_name)

    def write_char(self, field_name, value):
        self._builder.add_char_field(field_name)

    def write_short(self, field_name, value):
        self._builder.add_short_field(field_name)

    def write_int(self, field_name, value):
        self._builder.add_int_field(field_name)

    def write_long(self, field_name, value):
        self._builder.add_long_field(field_name)

    def write_float(self, field_name, value):
        self._builder.add_float_field(field_name)

    def write_double(self, field_name, value):
        self._builder.add_double_field(field_name)

    def write_string(self, field_name, value):
        self._builder.add_string_field(field_name)

    def write_byte_array(self, field_name, values):
        self._builder.add_byte_array_field(field_name)

    def write_boolean_array(self, field_name, values):
        self._builder.add_boolean_array_field(field_name)

    def write_char_array(self, field_name, values):
        self._builder.add_char_array_field(field_name)

    def write_short_array(self, field_name, values):
        self._builder.add_short_array_field(field_name)

    def write_int_array(self, field_name, values):
        self._builder.add_int_array_field(field_name)

    def write_long_array(self, field_name, values):
        self._builder.add_long_array_field(field_name)

    def write_float_array(self, field_name, values):
        self._builder.add_float_array_field(field_name)

    def write_double_array(self, field_name, values):
        self._builder.add_double_array_field(field_name)

    def write_string_array(self, field_name, values):
        self._builder.add_string_array_field(field_name)

    def write_portable(self, field_name, portable):
        if portable is None:
            raise HazelcastSerializationError(
                "Cannot write None portable without explicitly registering class definition!"
            )
        version = util.get_portable_version(portable, self.portable_context.portable_version)

        factory_id = portable.get_factory_id()
        class_id = portable.get_class_id()
        nested_builder = ClassDefinitionBuilder(factory_id, class_id, version)
        nested_class_def = self._create_nested_class_def(portable, nested_builder)
        self._builder.add_portable_field(field_name, nested_class_def)

    def write_portable_array(self, field_name, values):
        if values is None or len(values) == 0:
            raise HazelcastSerializationError(
                "Cannot write None portable without explicitly registering class definition!"
            )
        portable = values[0]
        _class_id = portable.get_class_id()
        for i in range(1, len(values)):
            if values[i].get_class_id() != _class_id:
                raise ValueError("Detected different class-ids in portable array!")
        version = util.get_portable_version(portable, self.portable_context.portable_version)
        nested_builder = ClassDefinitionBuilder(
            portable.get_factory_id(), portable.get_class_id(), version
        )
        nested_class_def = self._create_nested_class_def(portable, nested_builder)
        self._builder.add_portable_array_field(field_name, nested_class_def)

    def write_null_portable(self, field_name, factory_id, class_id):
        nested_class_def = self.portable_context.lookup_class_definition(
            factory_id, class_id, self.portable_context.portable_version
        )
        if nested_class_def is None:
            raise HazelcastSerializationError(
                "Cannot write None portable without explicitly registering class definition!"
            )
        self._builder.add_portable_field(field_name, nested_class_def)

    def get_raw_data_output(self):
        return EmptyObjectDataOutput()

    def write_utf(self, field_name, value):
        self.write_string(field_name, value)

    def write_utf_array(self, field_name, values):
        self.write_string_array(field_name, values)

    def _create_nested_class_def(self, portable, nested_builder):
        _writer = ClassDefinitionWriter(self.portable_context, class_def_builder=nested_builder)
        portable.write_portable(_writer)
        return self.portable_context.register_class_definition(nested_builder.build())

    def register_and_get(self):
        cd = self._builder.build()
        return self.portable_context.register_class_definition(cd)