// 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 com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import com.sleepycat.persist.StoreConfig;

public class MultiKey {
    public static void main(String[] args) {
        // open database
        EnvironmentConfig envcfg = new EnvironmentConfig();
        envcfg.setAllowCreateVoid(true);
        envcfg.setTransactional(true);
        Environment env = new Environment(new File("data/"), envcfg);
        StoreConfig stcfg = new StoreConfig();
        stcfg.setAllowCreate(true);
        stcfg.setTransactional(true);
        EntityStore est = new EntityStore(env, "mulkeydb", stcfg);
        PrimaryIndex<Integer, BaseData> idix = est.getPrimaryIndex(Integer.class, BaseData.class);
        SecondaryIndex<String, Integer, BaseData> namix = est.getSecondaryIndex(idix, String.class, "name");
        SecondaryIndex<String, Integer, BaseData> addrix = est.getSecondaryIndex(idix, String.class, "address");
        // put 3 records
        idix.put(new BaseData(1, "A", "A A A", 123));
        idix.put(new BaseData(2, "B", "B B B", 456));
        idix.put(new BaseData(3, "C", "C C C", 789));
        // lookup by id
        System.out.println(idix.get(2).getValue());
        System.out.println(idix.get(3).getValue());
        // lookup by name
        System.out.println(namix.get("B").getValue());
        System.out.println(namix.get("C").getValue());
        // lookup by address
        System.out.println(addrix.get("B B B").getValue());
        System.out.println(addrix.get("C C C").getValue());
        // close database
        est.close();
        env.close();
    }
}
