Search in sources :

Example 1 with EntryCostEstimator

use of com.hazelcast.map.impl.EntryCostEstimator in project hazelcast by hazelcast.

the class EvictionMaxSizePolicyTest method setTestSizeEstimator.

public static void setTestSizeEstimator(IMap map, final long oneEntryHeapCostInBytes) {
    final MapProxyImpl mapProxy = (MapProxyImpl) map;
    final MapService mapService = (MapService) mapProxy.getService();
    final MapServiceContext mapServiceContext = mapService.getMapServiceContext();
    final NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
    final IPartitionService partitionService = nodeEngine.getPartitionService();
    for (int i = 0; i < partitionService.getPartitionCount(); i++) {
        final Address owner = partitionService.getPartitionOwner(i);
        if (nodeEngine.getThisAddress().equals(owner)) {
            final PartitionContainer container = mapServiceContext.getPartitionContainer(i);
            if (container == null) {
                continue;
            }
            final RecordStore recordStore = container.getRecordStore(map.getName());
            final DefaultRecordStore defaultRecordStore = (DefaultRecordStore) recordStore;
            defaultRecordStore.setSizeEstimator(new EntryCostEstimator() {

                long size;

                @Override
                public long getEstimate() {
                    return size;
                }

                @Override
                public void adjustEstimateBy(long size) {
                    this.size += size;
                }

                @Override
                public long calculateValueCost(Object record) {
                    if (record == null) {
                        return 0L;
                    }
                    return oneEntryHeapCostInBytes;
                }

                @Override
                public long calculateEntryCost(Object key, Object record) {
                    if (record == null) {
                        return 0L;
                    }
                    return 2 * oneEntryHeapCostInBytes;
                }

                @Override
                public void reset() {
                    size = 0;
                }
            });
        }
    }
}
Also used : Address(com.hazelcast.cluster.Address) PartitionContainer(com.hazelcast.map.impl.PartitionContainer) IPartitionService(com.hazelcast.internal.partition.IPartitionService) EntryCostEstimator(com.hazelcast.map.impl.EntryCostEstimator) MapServiceContext(com.hazelcast.map.impl.MapServiceContext) NodeEngine(com.hazelcast.spi.impl.NodeEngine) MapProxyImpl(com.hazelcast.map.impl.proxy.MapProxyImpl) DefaultRecordStore(com.hazelcast.map.impl.recordstore.DefaultRecordStore) RecordStore(com.hazelcast.map.impl.recordstore.RecordStore) MapService(com.hazelcast.map.impl.MapService) DefaultRecordStore(com.hazelcast.map.impl.recordstore.DefaultRecordStore)

Aggregations

Address (com.hazelcast.cluster.Address)1 IPartitionService (com.hazelcast.internal.partition.IPartitionService)1 EntryCostEstimator (com.hazelcast.map.impl.EntryCostEstimator)1 MapService (com.hazelcast.map.impl.MapService)1 MapServiceContext (com.hazelcast.map.impl.MapServiceContext)1 PartitionContainer (com.hazelcast.map.impl.PartitionContainer)1 MapProxyImpl (com.hazelcast.map.impl.proxy.MapProxyImpl)1 DefaultRecordStore (com.hazelcast.map.impl.recordstore.DefaultRecordStore)1 RecordStore (com.hazelcast.map.impl.recordstore.RecordStore)1 NodeEngine (com.hazelcast.spi.impl.NodeEngine)1