// 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.KeyField;
import dk.vajhoej.isam.IsamSource;
import dk.vajhoej.isam.Key0;
import dk.vajhoej.isam.Key1;
import dk.vajhoej.isam.Key2;
import dk.vajhoej.isam.local.LocalIsamSource;
import dk.vajhoej.record.Alignment;
import dk.vajhoej.record.Endian;
import dk.vajhoej.record.FieldType;
import dk.vajhoej.record.Struct;
import dk.vajhoej.record.StructField;

public class MulKey {
    @Struct(endianess=Endian.LITTLE, alignment=Alignment.ALIGN1)
    public static class BaseData {
    	@KeyField(n=0)
        @StructField(n=0, type=FieldType.INT4)
        private int id;
    	@KeyField(n=1)
        @StructField(n=1, type=FieldType.FIXSTR, length=32)
        private String name;
    	@KeyField(n=2)
        @StructField(n=2, type=FieldType.FIXSTR, length=32)
        private String address;
        @StructField(n=3, type=FieldType.INT4)
        private int value;
        public BaseData() {
            this(0, "", "", 0);
        }
        public BaseData(int id, String name, String address, int value) {
            super();
            this.id = id;
            this.name = name;
            this.address = address;
            this.value = value;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public int getValue() {
            return value;
        }
        public void setValue(int value) {
            this.value = value;
        }
    }
    public static void main(String[] args) throws Exception {
        // open
        IsamSource db = new LocalIsamSource("mulkeydb.isq", "dk.vajhoej.vms.rms.IndexSequential", false);        
        // lookup by id
        System.out.println(db.read(BaseData.class, new Key0(2)).getValue());
        System.out.println(db.read(BaseData.class, new Key0(3)).getValue());
        // lookup by name
        System.out.println(db.read(BaseData.class, new Key1("B")).getValue());
        System.out.println(db.read(BaseData.class, new Key1("C")).getValue());
        // lookup by address
        System.out.println(db.read(BaseData.class, new Key2("B B B")).getValue());
        System.out.println(db.read(BaseData.class, new Key2("C C C")).getValue());
        // close 
        db.close();
    }
}
