// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package cache.ignite;

import java.util.Collections;
import java.util.Iterator;

import javax.cache.Cache.Entry;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteEvents;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
////import org.apache.ignite.events.CacheEvent;
import org.apache.ignite.events.EventType;
////import org.apache.ignite.lang.IgnitePredicate;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;

public class TestFunctional {
    private static void test(IgniteCache<String,Integer> cache, IgniteEvents events) throws InterruptedException {
        // basic put, get and remove
        cache.put("v", 123);
        System.out.println(cache.get("v"));
        cache.remove("v");
        System.out.println(cache.get("v"));
        cache.put("v", 123);
        System.out.println(cache.get("v"));
        // persistence
        Integer counter = cache.get("counter");
        if(counter == null) {
            counter = 0;
        }
        counter++;
        System.out.println(counter);
        cache.put("counter", counter);
        // list all (old style)
        Iterator<Entry<String,Integer>> it = cache.iterator();
        while(it.hasNext()) {
            Entry<String,Integer> e = it.next();
            System.out.printf("%s = %d\n", e.getKey(), e.getValue());
        }
        // list all (new style)
        cache.forEach(e -> System.out.printf("%s = %d\n", e.getKey(), e.getValue()));
        // listener  does not work for me in 2.7.6
        ////events.enableLocal(EventType.EVT_CACHE_OBJECT_PUT, EventType.EVT_CACHE_OBJECT_REMOVED);
        ////IgnitePredicate<CacheEvent> addlist = e -> { if(e.cacheName().equals("functional_cache")) System.out.printf("Add : %s = %d\n", e.key(), e.newValue()); return true; };
        ////events.localListen(addlist, EventType.EVT_CACHE_OBJECT_PUT);
        ////IgnitePredicate<CacheEvent> removelist = e -> { if(e.cacheName().equals("functional_cache")) System.out.printf("Remove : %s\n", e.key()); return true; };
        ////events.localListen(removelist, EventType.EVT_CACHE_OBJECT_REMOVED);
        ////cache.put("x", 123);
        ////cache.remove("x");
        ////Thread.sleep(10); // ensure sufficient time to call listener 
    }
    public static void main(String[] args) throws Exception {
        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setClientMode(true);
        cfg.setIncludeEventTypes(EventType.EVT_CACHE_OBJECT_PUT, EventType.EVT_CACHE_OBJECT_REMOVED);
        TcpDiscoveryVmIpFinder ipf = new TcpDiscoveryVmIpFinder();
        ipf.setAddresses(Collections.singletonList("localhost:47500"));
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipf));
        Ignite cli = Ignition.start(cfg);
        IgniteCache<String,Integer> cache = cli.getOrCreateCache("functional_cache");
        IgniteEvents events = cli.events();
        test(cache, events);
        cli.close();
    }
}
