Search in sources :

Example 6 with PortableFactory

use of com.hazelcast.nio.serialization.PortableFactory in project hazelcast by hazelcast.

the class MapTransactionTest method testKeySet_whenPortableKeysetAndValuesWithPredicates.

@Test
public void testKeySet_whenPortableKeysetAndValuesWithPredicates() throws Exception {
    final String mapName = randomString();
    final Config config = getConfig();
    config.getSerializationConfig().addPortableFactory(666, new PortableFactory() {

        public Portable create(int classId) {
            return new SampleObjects.PortableEmployee();
        }
    });
    final HazelcastInstance instance = createHazelcastInstance(config);
    IMap map = instance.getMap(mapName);
    final SampleObjects.PortableEmployee emp1 = new SampleObjects.PortableEmployee(34, "abc-123-xvz");
    final SampleObjects.PortableEmployee emp2 = new SampleObjects.PortableEmployee(20, "abc-123-xvz");
    map.put(emp1, emp1);
    final TransactionContext context = instance.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);
    assertNull(txMap.put(emp2, emp2));
    assertEquals(2, txMap.size());
    assertEquals(2, txMap.keySet().size());
    assertEquals(0, txMap.keySet(new SqlPredicate("a = 10")).size());
    assertEquals(0, txMap.values(new SqlPredicate("a = 10")).size());
    assertEquals(2, txMap.keySet(new SqlPredicate("a >= 10")).size());
    assertEquals(2, txMap.values(new SqlPredicate("a >= 10")).size());
    context.commitTransaction();
    assertEquals(2, map.size());
    assertEquals(2, map.values().size());
}
Also used : TransactionalMap(com.hazelcast.core.TransactionalMap) SampleObjects(com.hazelcast.query.SampleObjects) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) SqlPredicate(com.hazelcast.query.SqlPredicate) Portable(com.hazelcast.nio.serialization.Portable) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with PortableFactory

use of com.hazelcast.nio.serialization.PortableFactory in project hazelcast by hazelcast.

the class AbstractNearCacheSerializationCountTest method prepareSerializationConfig.

/**
     * Adds the serialization configuration for the used {@link Portable} domain object to the given {@link SerializationConfig}.
     *
     * @param serializationConfig the given {@link SerializationConfig} for the {@link DataStructureAdapter}
     */
protected static void prepareSerializationConfig(SerializationConfig serializationConfig) {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(SerializationCountingData.FACTORY_ID, SerializationCountingData.CLASS_ID).build();
    serializationConfig.addClassDefinition(classDefinition);
    serializationConfig.addPortableFactory(SerializationCountingData.FACTORY_ID, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            return new SerializationCountingData();
        }
    });
}
Also used : Portable(com.hazelcast.nio.serialization.Portable) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder)

Example 8 with PortableFactory

use of com.hazelcast.nio.serialization.PortableFactory in project hazelcast by hazelcast.

the class QueryAdvancedTest method testUnknownPortableField_notCausesQueryException_withIndex.

@Test
public // see: https://github.com/hazelcast/hazelcast/issues/3927
void testUnknownPortableField_notCausesQueryException_withIndex() {
    String mapName = "default";
    Config config = getConfig();
    config.getSerializationConfig().addPortableFactory(666, new PortableFactory() {

        public Portable create(int classId) {
            return new PortableEmployee();
        }
    });
    config.getMapConfig(mapName).addMapIndexConfig(new MapIndexConfig("notExist", false)).addMapIndexConfig(new MapIndexConfig("n", false));
    HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
    IMap<Integer, PortableEmployee> map = hazelcastInstance.getMap(mapName);
    for (int i = 0; i < 5; i++) {
        map.put(i, new PortableEmployee(i, "name_" + i));
    }
    Collection values = map.values(new SqlPredicate("n = name_2 OR notExist = name_0"));
    assertEquals(1, values.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Portable(com.hazelcast.nio.serialization.Portable) MapIndexConfig(com.hazelcast.config.MapIndexConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) PortableEmployee(com.hazelcast.query.SampleObjects.PortableEmployee) Collection(java.util.Collection) SqlPredicate(com.hazelcast.query.SqlPredicate) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with PortableFactory

use of com.hazelcast.nio.serialization.PortableFactory in project hazelcast by hazelcast.

the class MorphingPortableReaderTest method before.

@Before
public void before() throws Exception {
    service1 = (SerializationServiceV1) new DefaultSerializationServiceBuilder().addPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID, new PortableFactory() {

        public Portable create(int classId) {
            return new MorphingBasePortable();
        }
    }).build();
    service2 = (SerializationServiceV1) new DefaultSerializationServiceBuilder().addPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID, new PortableFactory() {

        public Portable create(int classId) {
            return new MorphingPortable();
        }
    }).build();
    Data data = service1.toData(new MorphingBasePortable((byte) 1, true, (char) 2, (short) 3, 4, 5, 1f, 2d, "test"));
    BufferObjectDataInput in = service2.createObjectDataInput(data);
    PortableSerializer portableSerializer = service2.getPortableSerializer();
    reader = portableSerializer.createMorphingReader(in);
}
Also used : MorphingBasePortable(com.hazelcast.nio.serialization.MorphingBasePortable) MorphingPortable(com.hazelcast.nio.serialization.MorphingPortable) Data(com.hazelcast.nio.serialization.Data) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) BufferObjectDataInput(com.hazelcast.nio.BufferObjectDataInput) Before(org.junit.Before)

Example 10 with PortableFactory

use of com.hazelcast.nio.serialization.PortableFactory in project hazelcast by hazelcast.

the class PortableSerializer method createNewPortableInstance.

private Portable createNewPortableInstance(int factoryId, int classId) {
    final PortableFactory portableFactory = factories.get(factoryId);
    if (portableFactory == null) {
        throw new HazelcastSerializationException("Could not find PortableFactory for factory-id: " + factoryId);
    }
    final Portable portable = portableFactory.create(classId);
    if (portable == null) {
        throw new HazelcastSerializationException("Could not create Portable for class-id: " + classId);
    }
    return portable;
}
Also used : Portable(com.hazelcast.nio.serialization.Portable) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) PortableFactory(com.hazelcast.nio.serialization.PortableFactory)

Aggregations

PortableFactory (com.hazelcast.nio.serialization.PortableFactory)18 Portable (com.hazelcast.nio.serialization.Portable)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Test (org.junit.Test)9 ParallelTest (com.hazelcast.test.annotation.ParallelTest)8 ClientConfig (com.hazelcast.client.config.ClientConfig)6 Config (com.hazelcast.config.Config)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 MapStoreConfig (com.hazelcast.config.MapStoreConfig)4 SerializationConfig (com.hazelcast.config.SerializationConfig)3 SqlPredicate (com.hazelcast.query.SqlPredicate)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Before (org.junit.Before)3 MapIndexConfig (com.hazelcast.config.MapIndexConfig)2 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)2 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)2 PortableEmployee (com.hazelcast.query.SampleObjects.PortableEmployee)2 NightlyTest (com.hazelcast.test.annotation.NightlyTest)2 Collection (java.util.Collection)2