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));
}
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;
}
};
}
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;
}
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);
}
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);
}
Aggregations