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

import DataRec

def test_write_one(db, id, val):
    db.create(DataRec(id, val))

def test_write():
    db = LocalIsamSource('jy.isq', 'dk.vajhoej.vms.rms.IndexSequential', False)
    test_write_one(db, 'CCCC', 3)
    test_write_one(db, 'BBBB', 2)
    test_write_one(db, 'AAAA', 1)
    db.close()

def test_read_sequential():
    db = LocalIsamSource('jy.isq', 'dk.vajhoej.vms.rms.IndexSequential', True)
    it = db.readGE(DataRec, Key0('    '))
    while it.read():
        res = it.current()
        print('%s -> %d' % (res.id, res.val))
    db.close()

def test_read_random_one(db, id):
    res = db.read(DataRec, Key0(id))
    print('%s -> %d' % (res.id, res.val))

def test_read_random():
    db = LocalIsamSource('jy.isq', 'dk.vajhoej.vms.rms.IndexSequential', True)
    test_read_random_one(db, 'CCCC')
    test_read_random_one(db, 'BBBB')
    test_read_random_one(db, 'AAAA')
    db.close()

print('Jython')
test_write()
test_read_sequential()
test_read_random()
