/* * Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.hazelcast.internal.nearcache.impl.store; import com.hazelcast.internal.eviction.EvictionCandidate; import com.hazelcast.internal.eviction.EvictionListener; import com.hazelcast.internal.nearcache.NearCacheRecord; import com.hazelcast.internal.nearcache.impl.SampleableNearCacheRecordMap; import com.hazelcast.nio.serialization.SerializableByConvention; import com.hazelcast.spi.serialization.SerializationService; import com.hazelcast.util.SampleableConcurrentHashMap; /** * {@link SampleableNearCacheRecordMap} implementation for on-heap Near Caches. * * @param <K> the type of the key stored in Near Cache * @param <V> the type of the value stored in Near Cache */ @SerializableByConvention public class HeapNearCacheRecordMap<K, V extends NearCacheRecord> extends SampleableConcurrentHashMap<K, V> implements SampleableNearCacheRecordMap<K, V> { private final SerializationService serializationService; HeapNearCacheRecordMap(SerializationService serializationService, int initialCapacity) { super(initialCapacity); this.serializationService = serializationService; } public class NearCacheEvictableSamplingEntry extends SamplingEntry<K, V> implements EvictionCandidate<K, V> { NearCacheEvictableSamplingEntry(K key, V value) { super(key, value); } @Override public K getAccessor() { return key; } @Override public V getEvictable() { return value; } @Override public Object getKey() { return serializationService.toObject(key); } @Override public Object getValue() { return serializationService.toObject(value.getValue()); } @Override public long getCreationTime() { return value.getCreationTime(); } @Override public long getLastAccessTime() { return value.getLastAccessTime(); } @Override public long getAccessHit() { return value.getAccessHit(); } } @Override @SuppressWarnings("unchecked") protected <E extends SamplingEntry> E createSamplingEntry(K key, V value) { return (E) new NearCacheEvictableSamplingEntry(key, value); } @Override public <C extends EvictionCandidate<K, V>> int evict(Iterable<C> evictionCandidates, EvictionListener<K, V> evictionListener) { if (evictionCandidates == null) { return 0; } int actualEvictedCount = 0; for (EvictionCandidate<K, V> evictionCandidate : evictionCandidates) { if (remove(evictionCandidate.getAccessor()) != null) { actualEvictedCount++; if (evictionListener != null) { evictionListener.onEvict(evictionCandidate.getAccessor(), evictionCandidate.getEvictable(), false); } } } return actualEvictedCount; } @Override public Iterable<NearCacheEvictableSamplingEntry> sample(int sampleCount) { return super.getRandomSamples(sampleCount); } }