var K = require('./build/default/_kyoto'),
    Fs = require('fs'),
    db = new K.PolyDB();

db.open('insert.kch', K.PolyDB.OREADER, begin);

function begin(err) {
  var start = Date.now(),
      found = 0,
      cursor = new K.Cursor(db);

  cursor.next(count);

  function count(err, val) {
    if (err)
      throw err;
    else if (val === undefined)
      stop();
    else {
      found++;
      cursor.next(count);
    }
  }

  function stop() {
    var time = Date.now() - start;
    console.log('Read %d items in %d ms.', found, time);
    db.close(exit);
  }
}

function exit(err) {
  if (err) throw err;
}
