Search in sources :

Example 61 with SerializationService

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

the class EvictionStrategyTest method evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy.

@Test
public void evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy() {
    final int RECORD_COUNT = 100;
    final int EXPECTED_EVICTED_COUNT = 1;
    final int EXPECTED_EVICTED_RECORD_VALUE = RECORD_COUNT / 2;
    Node node = TestUtil.getNode(instance);
    SerializationService serializationService = node.getSerializationService();
    ICacheService cacheService = node.getNodeEngine().getService(ICacheService.SERVICE_NAME);
    CacheContext cacheContext = cacheService.getOrCreateCacheContext("MyCache");
    EvictionConfiguration evictionConfig = new EvictionConfiguration() {

        @Override
        public EvictionStrategyType getEvictionStrategyType() {
            return EvictionStrategyType.SAMPLING_BASED_EVICTION;
        }

        @Override
        public EvictionPolicyType getEvictionPolicyType() {
            return null;
        }

        @Override
        public String getComparatorClassName() {
            return null;
        }

        @Override
        public EvictionPolicyComparator getComparator() {
            return null;
        }
    };
    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 < RECORD_COUNT; i++) {
        CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
        Data data = serializationService.toData(i);
        cacheRecordMap.put(data, record);
        if (i == EXPECTED_EVICTED_RECORD_VALUE) {
            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(Collections.singleton(evictionCandidate));
    when(evictionPolicyEvaluator.getEvictionPolicyComparator()).thenReturn(null);
    assertEquals(RECORD_COUNT, cacheRecordMap.size());
    assertTrue(cacheRecordMap.containsKey(expectedData));
    assertTrue(cacheRecordMap.containsValue(expectedEvictedRecord));
    int evictedCount = evictionStrategy.evict((S) cacheRecordMap, evictionPolicyEvaluator, EVICT_ALWAYS, NO_LISTENER);
    assertEquals(EXPECTED_EVICTED_COUNT, evictedCount);
    assertEquals(RECORD_COUNT - EXPECTED_EVICTED_COUNT, cacheRecordMap.size());
    assertFalse(cacheRecordMap.containsKey(expectedData));
    assertFalse(cacheRecordMap.containsValue(expectedEvictedRecord));
}
Also used : EvictionPolicyEvaluator(com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator) Node(com.hazelcast.instance.Node) SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.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 62 with SerializationService

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

the class ClientRingbufferProxy method onInitialize.

@Override
protected void onInitialize() {
    String partitionKey = StringPartitioningStrategy.getPartitionKey(name);
    partitionId = getContext().getPartitionService().getPartitionId(partitionKey);
    final SerializationService serializationService = getContext().getSerializationService();
    readManyAsyncResponseDecoder = new ClientMessageDecoder() {

        @Override
        public PortableReadResultSet decodeClientMessage(ClientMessage clientMessage) {
            final RingbufferReadManyCodec.ResponseParameters responseParameters = RingbufferReadManyCodec.decodeResponse(clientMessage);
            PortableReadResultSet readResultSet = new PortableReadResultSet(responseParameters.readCount, responseParameters.items);
            readResultSet.setSerializationService(serializationService);
            return readResultSet;
        }
    };
}
Also used : ClientMessageDecoder(com.hazelcast.client.impl.ClientMessageDecoder) PortableReadResultSet(com.hazelcast.ringbuffer.impl.client.PortableReadResultSet) SerializationService(com.hazelcast.spi.serialization.SerializationService) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 63 with SerializationService

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

the class XAResourceProxy method recover.

@Override
public Xid[] recover(int flag) throws XAException {
    ClientMessage request = XATransactionCollectTransactionsCodec.encodeRequest();
    ClientMessage response = invoke(request);
    Collection<Data> list = XATransactionCollectTransactionsCodec.decodeResponse(response).response;
    SerializableXID[] xidArray = new SerializableXID[list.size()];
    SerializationService serializationService = getContext().getSerializationService();
    int index = 0;
    for (Data xidData : list) {
        xidArray[index++] = serializationService.toObject(xidData);
    }
    return xidArray;
}
Also used : SerializableXID(com.hazelcast.transaction.impl.xa.SerializableXID) SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 64 with SerializationService

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

the class ClientSetProxy method getAll.

private Collection<E> getAll() {
    ClientMessage request = SetGetAllCodec.encodeRequest(name);
    ClientMessage response = invokeOnPartition(request);
    SetGetAllCodec.ResponseParameters resultParameters = SetGetAllCodec.decodeResponse(response);
    List<Data> resultCollection = resultParameters.response;
    SerializationService serializationService = getContext().getSerializationService();
    return new UnmodifiableLazyList<E>(resultCollection, serializationService);
}
Also used : SetGetAllCodec(com.hazelcast.client.impl.protocol.codec.SetGetAllCodec) SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) UnmodifiableLazyList(com.hazelcast.spi.impl.UnmodifiableLazyList) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 65 with SerializationService

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

the class ClientTopicProxy method publish.

@Override
public void publish(E message) {
    SerializationService serializationService = getContext().getSerializationService();
    Data data = serializationService.toData(message);
    ClientMessage request = TopicPublishCodec.encodeRequest(name, data);
    invokeOnPartition(request);
}
Also used : SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Aggregations

SerializationService (com.hazelcast.spi.serialization.SerializationService)120 Test (org.junit.Test)59 QuickTest (com.hazelcast.test.annotation.QuickTest)56 Data (com.hazelcast.nio.serialization.Data)54 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)46 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)28 ParallelTest (com.hazelcast.test.annotation.ParallelTest)22 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)18 HeapData (com.hazelcast.internal.serialization.impl.HeapData)17 Map (java.util.Map)15 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)10 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)9 ClientDelegatingFuture (com.hazelcast.client.util.ClientDelegatingFuture)8 Node (com.hazelcast.instance.Node)7 SerializationConfig (com.hazelcast.config.SerializationConfig)6 ICacheService (com.hazelcast.cache.impl.ICacheService)5 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)5 Record (com.hazelcast.map.impl.record.Record)5 ICacheRecordStore (com.hazelcast.cache.impl.ICacheRecordStore)3