import com.hazelcast.config.Config;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizePolicy;
import com.hazelcast.config.NativeMemoryConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.memory.MemorySize;
import com.hazelcast.memory.MemoryUnit;

import java.util.Map;

import static com.hazelcast.config.EvictionPolicy.LRU;
import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;

/**
 * You have to set your Hazelcast Enterprise license key to make this code sample work.
 * Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
 */
public class HDEviction {

    private static final int MAX_ENTRY_COUNT = 1000;

    public static void main(String[] args) {
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(newConfig());

        Map<String, String> map = hazelcastInstance.getMap("map");
        for (int i = 0; i < 10 * MAX_ENTRY_COUNT; i++) {
            map.put("key-" + i, "value-" + i);
        }

        System.out.println("Map size is " + map.size() + " and it is below the allowed max entry count " + MAX_ENTRY_COUNT);

        hazelcastInstance.shutdown();
    }

    public static Config newConfig() {
        MapConfig mapConfig = new MapConfig();
        mapConfig.setName("default");
        mapConfig.setInMemoryFormat(InMemoryFormat.NATIVE);

        mapConfig.getEvictionConfig()
                .setMaxSizePolicy(MaxSizePolicy.PER_NODE)
                .setSize(MAX_ENTRY_COUNT).setEvictionPolicy(LRU);

        MemorySize memorySize = new MemorySize(128, MemoryUnit.MEGABYTES);
        NativeMemoryConfig memoryConfig = new NativeMemoryConfig();
        memoryConfig.setEnabled(true);
        memoryConfig.setSize(memorySize);
        memoryConfig.setAllocatorType(NativeMemoryConfig.MemoryAllocatorType.STANDARD);

        Config config = new Config();
        config.addMapConfig(mapConfig);
        config.setNativeMemoryConfig(memoryConfig);
        config.setLicenseKey(ENTERPRISE_LICENSE_KEY);

        return config;
    }
}
