Search in sources :

Example 1 with CacheObjectRecord

use of com.hazelcast.cache.impl.record.CacheObjectRecord in project hazelcast by hazelcast.

the class CacheDataSerializerHook method createFactory.

public DataSerializableFactory createFactory() {
    ConstructorFunction<Integer, IdentifiedDataSerializable>[] constructors = new ConstructorFunction[LEN];
    constructors[GET] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheGetOperation();
        }
    };
    constructors[CONTAINS_KEY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheContainsKeyOperation();
        }
    };
    constructors[PUT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CachePutOperation();
        }
    };
    constructors[PUT_IF_ABSENT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CachePutIfAbsentOperation();
        }
    };
    constructors[REMOVE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheRemoveOperation();
        }
    };
    constructors[GET_AND_REMOVE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheGetAndRemoveOperation();
        }
    };
    constructors[REPLACE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheReplaceOperation();
        }
    };
    constructors[GET_AND_REPLACE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheGetAndReplaceOperation();
        }
    };
    constructors[PUT_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CachePutBackupOperation();
        }
    };
    constructors[PUT_ALL_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CachePutAllBackupOperation();
        }
    };
    constructors[REMOVE_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheRemoveBackupOperation();
        }
    };
    constructors[SIZE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheSizeOperation();
        }
    };
    constructors[SIZE_FACTORY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheSizeOperationFactory();
        }
    };
    constructors[CLEAR_FACTORY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheClearOperationFactory();
        }
    };
    constructors[GET_ALL] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheGetAllOperation();
        }
    };
    constructors[GET_ALL_FACTORY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheGetAllOperationFactory();
        }
    };
    constructors[LOAD_ALL] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheLoadAllOperation();
        }
    };
    constructors[LOAD_ALL_FACTORY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheLoadAllOperationFactory();
        }
    };
    constructors[EXPIRY_POLICY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        @Override
        public IdentifiedDataSerializable createNew(Integer arg) {
            return new HazelcastExpiryPolicy();
        }
    };
    constructors[KEY_ITERATOR] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheKeyIteratorOperation();
        }
    };
    constructors[KEY_ITERATION_RESULT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheKeyIterationResult();
        }
    };
    constructors[ENTRY_PROCESSOR] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheEntryProcessorOperation();
        }
    };
    constructors[CLEAR_RESPONSE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheClearResponse();
        }
    };
    constructors[CREATE_CONFIG] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheCreateConfigOperation();
        }
    };
    constructors[GET_CONFIG] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheGetConfigOperation();
        }
    };
    constructors[MANAGEMENT_CONFIG] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheManagementConfigOperation();
        }
    };
    constructors[LISTENER_REGISTRATION] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheListenerRegistrationOperation();
        }
    };
    constructors[DESTROY_CACHE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheDestroyOperation();
        }
    };
    constructors[CACHE_EVENT_DATA] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheEventDataImpl();
        }
    };
    constructors[CACHE_EVENT_DATA_SET] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheEventSet();
        }
    };
    constructors[BACKUP_ENTRY_PROCESSOR] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        @Override
        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheBackupEntryProcessorOperation();
        }
    };
    constructors[CLEAR] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheClearOperation();
        }
    };
    constructors[CLEAR_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheClearBackupOperation();
        }
    };
    constructors[REMOVE_ALL] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheRemoveAllOperation();
        }
    };
    constructors[REMOVE_ALL_BACKUP] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheRemoveAllBackupOperation();
        }
    };
    constructors[REMOVE_ALL_FACTORY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheRemoveAllOperationFactory();
        }
    };
    constructors[PUT_ALL] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CachePutAllOperation();
        }
    };
    constructors[MERGE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheMergeOperation();
        }
    };
    constructors[ENTRY_ITERATOR] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheEntryIteratorOperation();
        }
    };
    constructors[ENTRY_ITERATION_RESULT] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheEntryIterationResult();
        }
    };
    constructors[CACHE_PARTITION_LOST_EVENT_FILTER] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CachePartitionLostEventFilter();
        }
    };
    constructors[DEFAULT_CACHE_ENTRY_VIEW] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new DefaultCacheEntryView();
        }
    };
    constructors[CACHE_REPLICATION] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheReplicationOperation();
        }
    };
    constructors[CACHE_POST_JOIN] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new PostJoinCacheOperation();
        }
    };
    constructors[CACHE_DATA_RECORD] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheDataRecord();
        }
    };
    constructors[CACHE_OBJECT_RECORD] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheObjectRecord();
        }
    };
    constructors[CACHE_PARTITION_EVENT_DATA] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CachePartitionEventData();
        }
    };
    constructors[CACHE_INVALIDATION_METADATA] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheGetInvalidationMetaDataOperation();
        }
    };
    constructors[CACHE_INVALIDATION_METADATA_RESPONSE] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheGetInvalidationMetaDataOperation.MetaDataResponse();
        }
    };
    constructors[CACHE_ASSIGN_AND_GET_UUIDS] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheAssignAndGetUuidsOperation();
        }
    };
    constructors[CACHE_ASSIGN_AND_GET_UUIDS_FACTORY] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheAssignAndGetUuidsOperationFactory();
        }
    };
    constructors[CACHE_NEAR_CACHE_STATE_HOLDER] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheNearCacheStateHolder();
        }
    };
    constructors[CACHE_EVENT_LISTENER_ADAPTOR] = new ConstructorFunction<Integer, IdentifiedDataSerializable>() {

        public IdentifiedDataSerializable createNew(Integer arg) {
            return new CacheEventListenerAdaptor();
        }
    };
    return new ArrayDataSerializableFactory(constructors);
}
Also used : CacheGetOperation(com.hazelcast.cache.impl.operation.CacheGetOperation) CacheLoadAllOperation(com.hazelcast.cache.impl.operation.CacheLoadAllOperation) CacheEntryIteratorOperation(com.hazelcast.cache.impl.operation.CacheEntryIteratorOperation) CacheCreateConfigOperation(com.hazelcast.cache.impl.operation.CacheCreateConfigOperation) CacheRemoveAllOperationFactory(com.hazelcast.cache.impl.operation.CacheRemoveAllOperationFactory) HazelcastExpiryPolicy(com.hazelcast.cache.HazelcastExpiryPolicy) CacheAssignAndGetUuidsOperationFactory(com.hazelcast.client.impl.protocol.task.cache.CacheAssignAndGetUuidsOperationFactory) CacheGetAndRemoveOperation(com.hazelcast.cache.impl.operation.CacheGetAndRemoveOperation) CacheGetAllOperationFactory(com.hazelcast.cache.impl.operation.CacheGetAllOperationFactory) CacheEntryProcessorOperation(com.hazelcast.cache.impl.operation.CacheEntryProcessorOperation) IdentifiedDataSerializable(com.hazelcast.nio.serialization.IdentifiedDataSerializable) CacheRemoveAllOperation(com.hazelcast.cache.impl.operation.CacheRemoveAllOperation) CacheAssignAndGetUuidsOperation(com.hazelcast.client.impl.protocol.task.cache.CacheAssignAndGetUuidsOperation) CachePutOperation(com.hazelcast.cache.impl.operation.CachePutOperation) CacheReplicationOperation(com.hazelcast.cache.impl.operation.CacheReplicationOperation) CacheClearOperation(com.hazelcast.cache.impl.operation.CacheClearOperation) CachePutAllBackupOperation(com.hazelcast.cache.impl.operation.CachePutAllBackupOperation) CacheSizeOperationFactory(com.hazelcast.cache.impl.operation.CacheSizeOperationFactory) CacheGetAndReplaceOperation(com.hazelcast.cache.impl.operation.CacheGetAndReplaceOperation) DefaultCacheEntryView(com.hazelcast.cache.impl.merge.entry.DefaultCacheEntryView) CacheRemoveBackupOperation(com.hazelcast.cache.impl.operation.CacheRemoveBackupOperation) CacheGetAllOperation(com.hazelcast.cache.impl.operation.CacheGetAllOperation) CacheObjectRecord(com.hazelcast.cache.impl.record.CacheObjectRecord) CacheDestroyOperation(com.hazelcast.cache.impl.operation.CacheDestroyOperation) CacheNearCacheStateHolder(com.hazelcast.cache.impl.operation.CacheNearCacheStateHolder) CacheLoadAllOperationFactory(com.hazelcast.cache.impl.operation.CacheLoadAllOperationFactory) CacheSizeOperation(com.hazelcast.cache.impl.operation.CacheSizeOperation) CacheClearBackupOperation(com.hazelcast.cache.impl.operation.CacheClearBackupOperation) CachePutIfAbsentOperation(com.hazelcast.cache.impl.operation.CachePutIfAbsentOperation) CacheGetInvalidationMetaDataOperation(com.hazelcast.cache.impl.operation.CacheGetInvalidationMetaDataOperation) CacheRemoveAllBackupOperation(com.hazelcast.cache.impl.operation.CacheRemoveAllBackupOperation) CacheListenerRegistrationOperation(com.hazelcast.cache.impl.operation.CacheListenerRegistrationOperation) CacheKeyIteratorOperation(com.hazelcast.cache.impl.operation.CacheKeyIteratorOperation) CacheContainsKeyOperation(com.hazelcast.cache.impl.operation.CacheContainsKeyOperation) CachePutBackupOperation(com.hazelcast.cache.impl.operation.CachePutBackupOperation) PostJoinCacheOperation(com.hazelcast.cache.impl.operation.PostJoinCacheOperation) CacheRemoveOperation(com.hazelcast.cache.impl.operation.CacheRemoveOperation) CacheClearOperationFactory(com.hazelcast.cache.impl.operation.CacheClearOperationFactory) ConstructorFunction(com.hazelcast.util.ConstructorFunction) CacheBackupEntryProcessorOperation(com.hazelcast.cache.impl.operation.CacheBackupEntryProcessorOperation) CacheReplaceOperation(com.hazelcast.cache.impl.operation.CacheReplaceOperation) CacheManagementConfigOperation(com.hazelcast.cache.impl.operation.CacheManagementConfigOperation) CacheDataRecord(com.hazelcast.cache.impl.record.CacheDataRecord) CacheGetConfigOperation(com.hazelcast.cache.impl.operation.CacheGetConfigOperation) CachePutAllOperation(com.hazelcast.cache.impl.operation.CachePutAllOperation) CachePartitionLostEventFilter(com.hazelcast.cache.impl.event.CachePartitionLostEventFilter) ArrayDataSerializableFactory(com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory) CacheMergeOperation(com.hazelcast.cache.impl.operation.CacheMergeOperation)

Example 2 with CacheObjectRecord

use of com.hazelcast.cache.impl.record.CacheObjectRecord in project hazelcast by hazelcast.

the class CacheEntryViewsTest method doCacheEntryViewTest.

private void doCacheEntryViewTest(CacheEntryViews.CacheEntryViewType cacheEntryViewType) {
    String key = "testKey";
    String value = "testValue";
    CacheObjectRecord record = new CacheObjectRecord(value, System.currentTimeMillis(), 1234L);
    CacheEntryView cacheEntryView = CacheEntryViews.createEntryView(serializationService.toData(key), serializationService.toData(value), record, cacheEntryViewType);
    assertEquals(key, serializationService.toObject(cacheEntryView.getKey()));
    assertEquals(value, serializationService.toObject(cacheEntryView.getValue()));
    assertEquals(record.getAccessHit(), cacheEntryView.getAccessHit());
    assertEquals(record.getExpirationTime(), cacheEntryView.getExpirationTime());
    assertEquals(record.getLastAccessTime(), cacheEntryView.getLastAccessTime());
}
Also used : CacheObjectRecord(com.hazelcast.cache.impl.record.CacheObjectRecord)

Example 3 with CacheObjectRecord

use of com.hazelcast.cache.impl.record.CacheObjectRecord in project hazelcast by hazelcast.

the class EvictionPolicyEvaluatorTest method test_evictionPolicyLRU.

private void test_evictionPolicyLRU(boolean useExpiredEntry) {
    final int RECORD_COUNT = 100;
    final int EXPECTED_EVICTED_RECORD_VALUE = RECORD_COUNT / 2;
    final int EXPECTED_EXPIRED_RECORD_VALUE = useExpiredEntry ? RECORD_COUNT / 4 : -1;
    EvictionConfiguration evictionConfig = new EvictionConfiguration() {

        @Override
        public EvictionStrategyType getEvictionStrategyType() {
            return null;
        }

        @Override
        public EvictionPolicyType getEvictionPolicyType() {
            return EvictionPolicyType.LRU;
        }

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

        @Override
        public EvictionPolicyComparator getComparator() {
            return null;
        }
    };
    EvictionPolicyEvaluator evictionPolicyEvaluator = EvictionPolicyEvaluatorProvider.getEvictionPolicyEvaluator(evictionConfig, null);
    List<EvictionCandidate<Integer, CacheObjectRecord>> records = new ArrayList<EvictionCandidate<Integer, CacheObjectRecord>>();
    long baseTime = System.currentTimeMillis();
    for (int i = 0; i < RECORD_COUNT; i++) {
        long creationTime = baseTime + (i * 100);
        CacheObjectRecord record = new CacheObjectRecord(i, creationTime, Long.MAX_VALUE);
        if (i == EXPECTED_EVICTED_RECORD_VALUE) {
            // The record in the middle will be minimum access time.
            // So, it will be selected for eviction
            record.setAccessTime(baseTime - 1000);
        } else if (i == EXPECTED_EXPIRED_RECORD_VALUE) {
            record.setExpirationTime(System.currentTimeMillis());
        } else {
            record.setAccessTime(creationTime + 1000);
        }
        records.add(new SimpleEvictionCandidate<Integer, CacheObjectRecord>(i, record));
    }
    sleepAtLeastMillis(1);
    Iterable<EvictionCandidate<Integer, CacheObjectRecord>> evictedRecords = evictionPolicyEvaluator.evaluate(records);
    assertNotNull(evictedRecords);
    Iterator<EvictionCandidate<Integer, CacheObjectRecord>> evictedRecordsIterator = evictedRecords.iterator();
    assertTrue(evictedRecordsIterator.hasNext());
    EvictionCandidate<Integer, CacheObjectRecord> candidateEvictedRecord = evictedRecordsIterator.next();
    assertNotNull(candidateEvictedRecord);
    assertFalse(evictedRecordsIterator.hasNext());
    CacheObjectRecord evictedRecord = candidateEvictedRecord.getEvictable();
    assertNotNull(evictedRecord);
    if (useExpiredEntry) {
        assertEquals(EXPECTED_EXPIRED_RECORD_VALUE, evictedRecord.getValue());
    } else {
        assertEquals(EXPECTED_EVICTED_RECORD_VALUE, evictedRecord.getValue());
    }
}
Also used : EvictionPolicyEvaluator(com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator) ArrayList(java.util.ArrayList) CacheObjectRecord(com.hazelcast.cache.impl.record.CacheObjectRecord)

Example 4 with CacheObjectRecord

use of com.hazelcast.cache.impl.record.CacheObjectRecord in project hazelcast by hazelcast.

the class EvictionPolicyEvaluatorTest method test_evictionPolicyLFU.

private void test_evictionPolicyLFU(boolean useExpiredEntry) {
    final int RECORD_COUNT = 100;
    final int EXPECTED_EVICTED_RECORD_VALUE = RECORD_COUNT / 2;
    final int EXPECTED_EXPIRED_RECORD_VALUE = useExpiredEntry ? RECORD_COUNT / 4 : -1;
    EvictionConfiguration evictionConfig = new EvictionConfiguration() {

        @Override
        public EvictionStrategyType getEvictionStrategyType() {
            return null;
        }

        @Override
        public EvictionPolicyType getEvictionPolicyType() {
            return EvictionPolicyType.LFU;
        }

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

        @Override
        public EvictionPolicyComparator getComparator() {
            return null;
        }
    };
    EvictionPolicyEvaluator evictionPolicyEvaluator = EvictionPolicyEvaluatorProvider.getEvictionPolicyEvaluator(evictionConfig, null);
    List<EvictionCandidate<Integer, CacheObjectRecord>> records = new ArrayList<EvictionCandidate<Integer, CacheObjectRecord>>();
    for (int i = 0; i < RECORD_COUNT; i++) {
        CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
        if (i == EXPECTED_EVICTED_RECORD_VALUE) {
            // The record in the middle will be minimum access hit.
            // So, it will be selected for eviction
            record.setAccessHit(0);
        } else if (i == EXPECTED_EXPIRED_RECORD_VALUE) {
            record.setExpirationTime(System.currentTimeMillis());
        } else {
            record.setAccessHit(i + 1);
        }
        records.add(new SimpleEvictionCandidate<Integer, CacheObjectRecord>(i, record));
    }
    Iterable<EvictionCandidate<Integer, CacheObjectRecord>> evictedRecords = evictionPolicyEvaluator.evaluate(records);
    assertNotNull(evictedRecords);
    Iterator<EvictionCandidate<Integer, CacheObjectRecord>> evictedRecordsIterator = evictedRecords.iterator();
    assertTrue(evictedRecordsIterator.hasNext());
    EvictionCandidate<Integer, CacheObjectRecord> candidateEvictedRecord = evictedRecordsIterator.next();
    assertNotNull(candidateEvictedRecord);
    assertFalse(evictedRecordsIterator.hasNext());
    CacheObjectRecord evictedRecord = candidateEvictedRecord.getEvictable();
    assertNotNull(evictedRecord);
    if (useExpiredEntry) {
        assertEquals(EXPECTED_EXPIRED_RECORD_VALUE, evictedRecord.getValue());
    } else {
        assertEquals(EXPECTED_EVICTED_RECORD_VALUE, evictedRecord.getValue());
    }
}
Also used : EvictionPolicyEvaluator(com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator) ArrayList(java.util.ArrayList) CacheObjectRecord(com.hazelcast.cache.impl.record.CacheObjectRecord)

Example 5 with CacheObjectRecord

use of com.hazelcast.cache.impl.record.CacheObjectRecord 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)

Aggregations

CacheObjectRecord (com.hazelcast.cache.impl.record.CacheObjectRecord)5 EvictionPolicyEvaluator (com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator)3 ArrayList (java.util.ArrayList)2 HazelcastExpiryPolicy (com.hazelcast.cache.HazelcastExpiryPolicy)1 CacheContext (com.hazelcast.cache.impl.CacheContext)1 ICacheService (com.hazelcast.cache.impl.ICacheService)1 CachePartitionLostEventFilter (com.hazelcast.cache.impl.event.CachePartitionLostEventFilter)1 DefaultCacheEntryView (com.hazelcast.cache.impl.merge.entry.DefaultCacheEntryView)1 CacheBackupEntryProcessorOperation (com.hazelcast.cache.impl.operation.CacheBackupEntryProcessorOperation)1 CacheClearBackupOperation (com.hazelcast.cache.impl.operation.CacheClearBackupOperation)1 CacheClearOperation (com.hazelcast.cache.impl.operation.CacheClearOperation)1 CacheClearOperationFactory (com.hazelcast.cache.impl.operation.CacheClearOperationFactory)1 CacheContainsKeyOperation (com.hazelcast.cache.impl.operation.CacheContainsKeyOperation)1 CacheCreateConfigOperation (com.hazelcast.cache.impl.operation.CacheCreateConfigOperation)1 CacheDestroyOperation (com.hazelcast.cache.impl.operation.CacheDestroyOperation)1 CacheEntryIteratorOperation (com.hazelcast.cache.impl.operation.CacheEntryIteratorOperation)1 CacheEntryProcessorOperation (com.hazelcast.cache.impl.operation.CacheEntryProcessorOperation)1 CacheGetAllOperation (com.hazelcast.cache.impl.operation.CacheGetAllOperation)1 CacheGetAllOperationFactory (com.hazelcast.cache.impl.operation.CacheGetAllOperationFactory)1 CacheGetAndRemoveOperation (com.hazelcast.cache.impl.operation.CacheGetAndRemoveOperation)1