// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import dk.vajhoej.isam.IsamException;
import dk.vajhoej.isam.IsamResult;
import dk.vajhoej.isam.IsamSource;
import dk.vajhoej.isam.Key0;
import dk.vajhoej.isam.local.LocalIsamSource;
import dk.vajhoej.record.RecordException;

public class Isq {
    public static void testWriteOne(IsamSource db, String id, int val) throws IsamException, RecordException {
        db.create(new DataRec(id, val));
    }
    public static void testWrite() throws IsamException, RecordException {
        IsamSource db = new LocalIsamSource("java.isq", "dk.vajhoej.vms.rms.IndexSequential", false);
        testWriteOne(db, "CCCC", 3);
        testWriteOne(db, "BBBB", 2);
        testWriteOne(db, "AAAA", 1);
        db.close();
    }
    public static void testReadSequential() throws IsamException, RecordException {
        IsamSource db = new LocalIsamSource("java.isq", "dk.vajhoej.vms.rms.IndexSequential", true);
        IsamResult<DataRec> it = db.readGE(DataRec.class, new Key0<String>("    "));
        while(it.read()) {
            DataRec buf = it.current();
            System.out.printf("%s -> %d\n", buf.getId(), buf.getVal());
        }
        db.close();
    }
    public static void testReadRandomOne(IsamSource db, String id) throws IsamException, RecordException {
        DataRec buf = db.read(DataRec.class, new Key0<String>(id));
        System.out.printf("%s -> %d\n", buf.getId(), buf.getVal());
    }
    public static void testReadRandom() throws IsamException, RecordException {
        IsamSource db = new LocalIsamSource("java.isq", "dk.vajhoej.vms.rms.IndexSequential", true);
        testReadRandomOne(db, "CCCC");
        testReadRandomOne(db, "BBBB");
        testReadRandomOne(db, "AAAA");
        db.close();
    }
    public static void main(String[] args) throws Exception {
        System.out.println("Java");
        testWrite();
        testReadSequential();
        testReadRandom();
    }
}
