Search in sources :

Example 6 with EmployeeDTO

use of example.serialization.EmployeeDTO in project hazelcast by hazelcast.

the class CompactStreamSerializerTest method testWithExplicitSerializer_nested.

@Test
public void testWithExplicitSerializer_nested() {
    SerializationConfig serializationConfig = new SerializationConfig();
    CompactSerializationConfig compactSerializationConfig = serializationConfig.getCompactSerializationConfig();
    compactSerializationConfig.setEnabled(true);
    compactSerializationConfig.register(EmployeeDTO.class, "employee", new CompactSerializer<EmployeeDTO>() {

        @Nonnull
        @Override
        public EmployeeDTO read(@Nonnull CompactReader in) {
            return new EmployeeDTO(in.readInt32("a"), in.readInt64("i"));
        }

        @Override
        public void write(@Nonnull CompactWriter out, @Nonnull EmployeeDTO object) {
            out.writeInt32("a", object.getAge());
            out.writeInt64("i", object.getId());
        }
    });
    compactSerializationConfig.register(EmployerDTO.class, "employer", new CompactSerializer<EmployerDTO>() {

        @Nonnull
        @Override
        public EmployerDTO read(@Nonnull CompactReader in) {
            String name = in.readString("n");
            String status = in.readString("hs");
            int age = in.readInt32("a");
            long[] ids = in.readArrayOfInt64("ids");
            EmployeeDTO s = in.readCompact("s");
            EmployeeDTO[] ss = in.readArrayOfCompact("ss", EmployeeDTO.class);
            return new EmployerDTO(name, age, status == null ? null : HiringStatus.valueOf(status), ids, s, ss);
        }

        @Override
        public void write(@Nonnull CompactWriter out, @Nonnull EmployerDTO object) {
            out.writeString("n", object.getName());
            out.writeString("hs", object.getHiringStatus() == null ? null : object.getHiringStatus().name());
            out.writeInt32("a", object.getZcode());
            out.writeArrayOfInt64("ids", object.getIds());
            out.writeCompact("s", object.getSingleEmployee());
            out.writeArrayOfCompact("ss", object.getOtherEmployees());
        }
    });
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
    EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
    long[] ids = new long[2];
    ids[0] = 22;
    ids[1] = 44;
    EmployeeDTO[] employeeDTOS = new EmployeeDTO[5];
    for (int j = 0; j < employeeDTOS.length; j++) {
        employeeDTOS[j] = new EmployeeDTO(20 + j, j * 100);
    }
    EmployerDTO employerDTO = new EmployerDTO("nbss", 40, HIRING, ids, employeeDTO, employeeDTOS);
    Data data = serializationService.toData(employerDTO);
    Object object = serializationService.toObject(data);
    EmployerDTO o = (EmployerDTO) object;
    assertEquals(employerDTO, o);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactWriter(com.hazelcast.nio.serialization.compact.CompactWriter) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) Nonnull(javax.annotation.Nonnull) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) EmployerDTO(example.serialization.EmployerDTO) EmployeeDTO(example.serialization.EmployeeDTO) ExternalizableEmployeeDTO(example.serialization.ExternalizableEmployeeDTO) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with EmployeeDTO

use of example.serialization.EmployeeDTO in project hazelcast by hazelcast.

the class CompactWithSchemaStreamSerializerTest method testFromObject.

@Test
public void testFromObject() {
    SerializationService serializationService = createSerializationService();
    EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
    Data data = serializationService.toDataWithSchema(employeeDTO);
    // Create a second schema service so that schemas are not shared accross these two
    // This is to make sure that toObject call will use the schema in the data
    SerializationService serializationService2 = createSerializationService();
    EmployeeDTO actual = serializationService2.toObject(data);
    assertEquals(employeeDTO, actual);
}
Also used : EmployeeDTO(example.serialization.EmployeeDTO) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with EmployeeDTO

use of example.serialization.EmployeeDTO in project hazelcast by hazelcast.

the class CompactFormatIntegrationTest method testBasicQuery.

@Test
public void testBasicQuery() {
    IMap<Integer, EmployeeDTO> map = instance1.getMap("test");
    for (int i = 0; i < 100; i++) {
        EmployeeDTO employeeDTO = new EmployeeDTO(i, 102310312);
        map.put(i, employeeDTO);
    }
    IMap<Integer, EmployeeDTO> map2 = instance2.getMap("test");
    int size = map2.keySet(Predicates.sql("age > 19")).size();
    assertEquals(80, size);
}
Also used : EmployeeDTO(example.serialization.EmployeeDTO) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 9 with EmployeeDTO

use of example.serialization.EmployeeDTO in project hazelcast by hazelcast.

the class CompactFormatIntegrationTest method testEntryProcessor.

@Test
public void testEntryProcessor() {
    IMap<Integer, Object> map = instance1.getMap("test");
    for (int i = 0; i < 100; i++) {
        if (serverDoesNotHaveClasses) {
            GenericRecord record = GenericRecordBuilder.compact("employee").setInt32("age", i).setInt64("id", 102310312).build();
            map.put(i, record);
        } else {
            EmployeeDTO employeeDTO = new EmployeeDTO(i, 102310312);
            map.put(i, employeeDTO);
        }
    }
    IMap map2 = instance2.getMap("test");
    if (serverDoesNotHaveClasses) {
        map2.executeOnEntries(new GenericIncreaseAgeEntryProcessor());
    } else {
        map2.executeOnEntries(new IncreaseAgeEntryProcessor());
    }
    for (int i = 0; i < 100; i++) {
        if (serverDoesNotHaveClasses) {
            GenericRecord record = (GenericRecord) map2.get(i);
            assertEquals(record.getInt32("age"), 1000 + i);
        } else {
            EmployeeDTO employeeDTO = (EmployeeDTO) map.get(i);
            assertEquals(employeeDTO.getAge(), 1000 + i);
        }
    }
}
Also used : IMap(com.hazelcast.map.IMap) EmployeeDTO(example.serialization.EmployeeDTO) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with EmployeeDTO

use of example.serialization.EmployeeDTO in project hazelcast by hazelcast.

the class CompactFormatIntegrationTest method testJoinedMemberQuery.

@Test
public void testJoinedMemberQuery() {
    IMap<Integer, EmployeeDTO> map = instance1.getMap("test");
    for (int i = 0; i < 100; i++) {
        EmployeeDTO employeeDTO = new EmployeeDTO(i, 102310312);
        map.put(i, employeeDTO);
    }
    HazelcastInstance newInstance = factory.newHazelcastInstance(getConfig());
    waitClusterForSafeState(newInstance);
    IMap<Integer, EmployeeDTO> map2 = newInstance.getMap("test");
    int size = map2.keySet(Predicates.sql("age > 19")).size();
    assertEquals(80, size);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) EmployeeDTO(example.serialization.EmployeeDTO) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

EmployeeDTO (example.serialization.EmployeeDTO)21 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)20 QuickTest (com.hazelcast.test.annotation.QuickTest)20 Test (org.junit.Test)20 Data (com.hazelcast.internal.serialization.Data)11 SerializationService (com.hazelcast.internal.serialization.SerializationService)11 ExternalizableEmployeeDTO (example.serialization.ExternalizableEmployeeDTO)11 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)10 CompactSerializationConfig (com.hazelcast.config.CompactSerializationConfig)6 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)6 SerializationConfig (com.hazelcast.config.SerializationConfig)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 EmployerDTO (example.serialization.EmployerDTO)4 CompactReader (com.hazelcast.nio.serialization.compact.CompactReader)3 CompactWriter (com.hazelcast.nio.serialization.compact.CompactWriter)3 Nonnull (javax.annotation.Nonnull)3 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)2 SqlResult (com.hazelcast.sql.SqlResult)2 EmployeeDTOSerializer (example.serialization.EmployeeDTOSerializer)2 NodeDTO (example.serialization.NodeDTO)2