Search in sources :

Example 96 with SerializationService

use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.

the class SetEqualsHashTest method testCollectionItem_equalsAndHash.

@Test
public void testCollectionItem_equalsAndHash() {
    SerializationServiceBuilder serializationServiceBuilder = new DefaultSerializationServiceBuilder();
    SerializationService build = serializationServiceBuilder.build();
    Data value = build.toData(randomString());
    CollectionItem firstItem = new CollectionItem(1, value);
    CollectionItem secondItem = new CollectionItem(2, value);
    assertTrue(firstItem.equals(secondItem));
    assertEquals(firstItem.hashCode(), secondItem.hashCode());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationServiceBuilder(com.hazelcast.internal.serialization.SerializationServiceBuilder) DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 97 with SerializationService

use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.

the class CollectionTestUtil method getBackupList.

/**
 * Returns all backup items of an {@link IList} by a given list name.
 * <p>
 * Note: You have to provide the {@link HazelcastInstance} you want to retrieve the backups from.
 * Use {@link getBackupInstance} to retrieve the backup instance for a given replica index.
 *
 * @param backupInstance the {@link HazelcastInstance} to retrieve the backups from
 * @param listName       the list name
 * @return a {@link List} with the backup items
 */
public static <E> List<E> getBackupList(HazelcastInstance backupInstance, String listName) {
    NodeEngineImpl nodeEngine = getNodeEngineImpl(backupInstance);
    CollectionService service = nodeEngine.getService(ListService.SERVICE_NAME);
    CollectionContainer collectionContainer = service.getContainerMap().get(listName);
    if (collectionContainer == null) {
        return emptyList();
    }
    // the replica items are retrieved via `getMap()`, the primary items via `getCollection()`
    Map<Long, CollectionItem> map = collectionContainer.getMap();
    List<E> backupList = new ArrayList<E>(map.size());
    SerializationService serializationService = nodeEngine.getSerializationService();
    for (CollectionItem collectionItem : map.values()) {
        E value = serializationService.toObject(collectionItem.getValue());
        backupList.add(value);
    }
    return backupList;
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) Accessors.getNodeEngineImpl(com.hazelcast.test.Accessors.getNodeEngineImpl) CollectionService(com.hazelcast.collection.impl.collection.CollectionService) ArrayList(java.util.ArrayList) SerializationService(com.hazelcast.internal.serialization.SerializationService) CollectionContainer(com.hazelcast.collection.impl.collection.CollectionContainer) CollectionItem(com.hazelcast.collection.impl.collection.CollectionItem)

Example 98 with SerializationService

use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.

the class WanReplicationRefTest method testSerialization.

@Test
public void testSerialization() {
    ref.setName("myWanReplicationRef");
    ref.setMergePolicyClassName("myMergePolicy");
    ref.setRepublishingEnabled(true);
    ref.addFilter("myFilter");
    SerializationService serializationService = new DefaultSerializationServiceBuilder().build();
    Data serialized = serializationService.toData(ref);
    WanReplicationRef deserialized = serializationService.toObject(serialized);
    assertEquals(ref.getName(), deserialized.getName());
    assertEquals(ref.getMergePolicyClassName(), deserialized.getMergePolicyClassName());
    assertEquals(ref.isRepublishingEnabled(), deserialized.isRepublishingEnabled());
    assertEquals(ref.getFilters(), deserialized.getFilters());
    assertEquals(ref.toString(), deserialized.toString());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) 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 99 with SerializationService

use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.

the class EvictionStrategyTest method evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy.

@Test
public void evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy() {
    final int recordCount = 100;
    final int expectedEvictedRecordValue = recordCount / 2;
    Node node = getNode(instance);
    SerializationService serializationService = node.getSerializationService();
    ICacheService cacheService = node.getNodeEngine().getService(ICacheService.SERVICE_NAME);
    CacheContext cacheContext = cacheService.getOrCreateCacheContext("MyCache");
    SamplingEvictionStrategy<K, V, S> evictionStrategy = SamplingEvictionStrategy.INSTANCE;
    CacheRecordHashMap cacheRecordMap = new CacheRecordHashMap(serializationService, 1000, cacheContext);
    CacheObjectRecord expectedEvictedRecord = null;
    Data expectedData = null;
    for (int i = 0; i < recordCount; i++) {
        CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
        Data data = serializationService.toData(i);
        cacheRecordMap.put(data, record);
        if (i == expectedEvictedRecordValue) {
            expectedEvictedRecord = record;
            expectedData = data;
        }
    }
    assertNotNull(expectedEvictedRecord);
    assertNotNull(expectedData);
    final SimpleEvictionCandidate evictionCandidate = new SimpleEvictionCandidate((K) expectedData, (V) expectedEvictedRecord);
    // we are testing "EvictionStrategy" in this test, so we mock "EvictionPolicyEvaluator" (it's tested in another test)
    EvictionPolicyEvaluator evictionPolicyEvaluator = mock(EvictionPolicyEvaluator.class);
    when(evictionPolicyEvaluator.evaluate(Matchers.any(Iterable.class))).thenReturn(evictionCandidate);
    when(evictionPolicyEvaluator.getEvictionPolicyComparator()).thenReturn(null);
    assertEquals(recordCount, cacheRecordMap.size());
    assertTrue(cacheRecordMap.containsKey(expectedData));
    assertTrue(cacheRecordMap.containsValue(expectedEvictedRecord));
    boolean evicted = evictionStrategy.evict((S) cacheRecordMap, evictionPolicyEvaluator, EVICT_ALWAYS, NO_LISTENER);
    assertTrue(evicted);
    assertEquals(recordCount - 1, cacheRecordMap.size());
    assertFalse(cacheRecordMap.containsKey(expectedData));
    assertFalse(cacheRecordMap.containsValue(expectedEvictedRecord));
}
Also used : EvictionPolicyEvaluator(com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator) Accessors.getNode(com.hazelcast.test.Accessors.getNode) Node(com.hazelcast.instance.impl.Node) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) CacheObjectRecord(com.hazelcast.cache.impl.record.CacheObjectRecord) CacheContext(com.hazelcast.cache.impl.CacheContext) CacheRecordHashMap(com.hazelcast.cache.impl.record.CacheRecordHashMap) EVICT_ALWAYS(com.hazelcast.internal.eviction.EvictionChecker.EVICT_ALWAYS) ICacheService(com.hazelcast.cache.impl.ICacheService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 100 with SerializationService

use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.

the class PortableVersionTest method test_nestedPortable_versionedSerializer.

// Test for issue https://github.com/hazelcast/hazelcast/issues/12733
@Test
public void test_nestedPortable_versionedSerializer() {
    SerializationServiceBuilder builder1 = new DefaultSerializationServiceBuilder();
    builder1.setPortableVersion(6);
    builder1.addPortableFactory(1, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            if (classId == 1) {
                return new Parent();
            } else if (classId == 2) {
                return new Child();
            }
            return null;
        }
    });
    SerializationService ss1 = builder1.build();
    SerializationServiceBuilder builder2 = new DefaultSerializationServiceBuilder();
    builder2.setPortableVersion(6);
    builder2.addPortableFactory(1, new PortableFactory() {

        @Override
        public Portable create(int classId) {
            if (classId == 1) {
                return new Parent();
            } else if (classId == 2) {
                return new Child();
            }
            return null;
        }
    });
    SerializationService ss2 = builder2.build();
    // make sure ss2 cached class definition of Child
    ss2.toData(new Child("sancar"));
    // serialized parent from ss1
    Parent parent = new Parent(new Child("sancar"));
    Data data = ss1.toData(parent);
    // cached class definition of Child and the class definition from data coming from ss1 should be compatible
    assertEquals(parent, ss2.toObject(data));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) Portable(com.hazelcast.nio.serialization.Portable) SerializationServiceBuilder(com.hazelcast.internal.serialization.SerializationServiceBuilder) DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SerializationService (com.hazelcast.internal.serialization.SerializationService)170 Data (com.hazelcast.internal.serialization.Data)117 Test (org.junit.Test)100 QuickTest (com.hazelcast.test.annotation.QuickTest)99 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)59 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)50 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)43 ArrayList (java.util.ArrayList)15 SerializationConfig (com.hazelcast.config.SerializationConfig)14 HazelcastInstance (com.hazelcast.core.HazelcastInstance)13 Node (com.hazelcast.instance.impl.Node)12 CustomSerializationTest (com.hazelcast.internal.serialization.impl.CustomSerializationTest)12 Accessors.getNode (com.hazelcast.test.Accessors.getNode)12 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)11 EmployeeDTO (example.serialization.EmployeeDTO)11 ExternalizableEmployeeDTO (example.serialization.ExternalizableEmployeeDTO)11 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)8 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8