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

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 TestX {
    private static void dump(IsamSource db, String id) throws IsamException, RecordException {
        System.out.printf("Key=%s\n", id);
        DataX d = db.read(DataX.class, new Key0<String>(id));
        if(d != null) {
            System.out.println(d);
        } else {
            System.out.println("Not found");
        }
    }
    private static final int NREC = 1000;
    private static final int JAVAX_OFFSET = 5 * NREC;
    public static void main(String[] args) throws IsamException, RecordException {
        try {
            // open
            IsamSource db = new LocalIsamSource("testdb.isq", "dk.vajhoej.vbisam.VBISAM", false);
            // put data
            for(int i = 0; i < NREC; i++) {
                DataX d = new DataX("Key#" + (JAVAX_OFFSET + i + 1), i + 1, new BigDecimal(i + 1).setScale(2), i + 1.0, String.format("This is value %d", i + 1));
                db.create(d);
            }
            //
            DataX d;
            String id;
            id = "Key#" + (JAVAX_OFFSET + 77);
            // get
            dump(db, id);
            // delete
            db.delete(DataX.class, new Key0<String>(id));
            // get non existing
            dump(db, id);
            // get and update
            id = "Key#" + (JAVAX_OFFSET + 88);
            dump(db, id);
            d = db.read(DataX.class, new Key0<String>(id));
            d.setIv(d.getIv() + 1);
            d.setZv(d.getZv().add(new BigDecimal("0.10")));
            d.setXv(d.getXv() +  0.1);
            d.setSv(d.getSv() + " updated");
            db.update(d);
            dump(db, id);
            // list all
            IsamResult<DataX> it = db.readGE(DataX.class, new Key0<String>("       "));
            int n = 0;
            while(it.read()) {
                d = it.current();
                if(!d.getId().startsWith("Key#")) {
                    System.out.println("Unexpected key: " + d.getId());
                }
             if(d.getIv() < 1 || NREC < d.getIv() ||
                d.getZv().compareTo(BigDecimal.ONE) < 0 || new BigDecimal(NREC).compareTo(d.getZv()) < 0 ||
                d.getXv() < 1 || NREC < d.getXv()) {
                   System.out.println("Unexpected value :" + d);
                }
                n++;
            }
            System.out.println(n);
            // list keys where "Key#n075" <= key < "Key#n085"
            IsamResult<DataX> it2 = db.readGE(DataX.class, new Key0<String>("Key#" + (JAVAX_OFFSET + 75)));
            int n2 = 0;
            while(it2.read()) {
                d = it2.current();
                if(d.getId().compareTo("Key#" + (JAVAX_OFFSET + 85)) >= 0) break;
                n2++;
            }
            System.out.println(n2);
            // close
            db.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
