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

try { Fs.unlinkSync('insert.kch'); } catch(e) {}
db.open('insert.kch', K.PolyDB.OWRITER | K.PolyDB.OCREATE, begin);

function begin(err) {
  var start = Date.now(),
      index = 0,
      limit = 1000000;

  each(err);

  function each(err) {
    if (err)
      throw err;
    else if (index == limit)
      stop();
    else
      db.set('key/' + index, 'value/' + (index++), each);
  }

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

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