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

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import jakarta.nosql.Settings;
import jakarta.nosql.Value;
import jakarta.nosql.keyvalue.BucketManager;
import jakarta.nosql.keyvalue.BucketManagerFactory;
import jakarta.nosql.keyvalue.KeyValueConfiguration;

import org.eclipse.jnosql.diana.memcached.keyvalue.MemcachedKeyValueConfiguration;

public class TestFunctional {
    private static void test(BucketManager cache) {
        // basic put, get and remove
        cache.put("v", 123);
        System.out.println((Integer)cache.get("v").get().get());
        cache.delete("v");
        Optional<Value> temp;
        temp = cache.get("v");
        System.out.println(temp.isPresent() ? (Integer)temp.get().get() : "(not exist)");
        cache.put("v", 123);
        System.out.println((Integer)cache.get("v").get().get());
        // persistence
        Integer counter;
        temp = cache.get("counter");
        if(temp.isPresent()) {
            counter = (Integer)temp.get().get();
        } else {
            counter = 0;
        }
        counter++;
        System.out.println(counter);
        cache.put("counter", counter);
        // list all not supported
        // listener not supported
    }
    public static void main(String[] args) throws Exception {
        KeyValueConfiguration cfg = new MemcachedKeyValueConfiguration();
        Map<String, Object> props = new HashMap<>();
        props.put("memcached.host", "localhost:11211"); // necessary
        BucketManagerFactory bmf = cfg.get(Settings.of(props));
        BucketManager cache = bmf.getBucketManager("functional_cache"); // Map not supported
        test(cache);
        bmf.close();
    }
}
