// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;

public class TestO {
    private static void dump(PrimaryIndex<String, DataO> pk, String key) throws ClassNotFoundException, IOException {
        System.out.printf("Key=%s\n", key);
        if(pk.contains(key)) {
            DataO o = pk.get(key);
            System.out.println(o);
        } else {
            System.out.println("Not found");
        }
    }
    private static final int NREC = 1000;
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // open database
        EnvironmentConfig envcfg = new EnvironmentConfig();
        envcfg.setAllowCreateVoid(true);
        Environment env = new Environment(new File("data/"), envcfg);
        StoreConfig stcfg = new StoreConfig();
        stcfg.setAllowCreate(true);
        EntityStore est = new EntityStore(env, "testdb", stcfg);
        PrimaryIndex<String, DataO> pk = est.getPrimaryIndex(String.class, DataO.class);
        // put data
        for(int i = 0; i < NREC; i++) {
            String key = "Key#" + (1000 + i + 1);
            DataO o = new DataO();
            o.setKey(key);
            o.setIv(i + 1);
            o.setXv(i + 1.0);
            o.setSv(String.format("This is value %d", i + 1));
            pk.put(o);
        }
        //
        String key;
        key = "Key#" + 1077;
        // get
        dump(pk, key);
        // delete
        pk.delete(key);
        // get non existing
        dump(pk, key);
        //
        key = "Key#" + 1088;
        // update and get
        dump(pk, key);
        DataO o = pk.get(key);
        o.setIv(o.getIv() + 1);
        o.setXv(o.getXv() + 0.1);
        o.setSv(o.getSv() + " updated");
        pk.put(o);
        dump(pk, key);
        // list all
        EntityCursor<DataO> c = pk.entities();
        Iterator<DataO> it = c.iterator();
        int n = 0;
        while(it.hasNext()) {
            DataO ito = it.next();
            if(!ito.getKey().startsWith("Key#")) {
                System.out.println("Unexpected key: " + ito.getKey());
            }
            if(ito.getIv() < 1 || NREC < ito.getIv()) {
                System.out.println("Unexpected value :" + ito);
            }
            n++;
        }
        c.close();
        System.out.println(n);
        // list keys where "Key#1075" <= key < "Key#1085"
        EntityCursor<DataO> c2 = pk.entities("Key#1075", true, "Key#1085", false);
        Iterator<DataO> it2 = c2.iterator();
        int n2 = 0;
        while(it2.hasNext()) {
            @SuppressWarnings("unused")
            DataO ito2 = it2.next();
            n2++;
        }
        c2.close();
        System.out.println(n2);
        // close database
        est.close();
        env.close();
    }
}
