rndKeys.add(i);
}
Collections.shuffle(rndKeys, new Random(123));
}
final Transaction txn = env.beginTransaction(null, null);
final Cursor cursor = db.openCursor(txn, null);
boolean success = false;
try {
cursor.setCacheMode(CacheMode.EVICT_LN);
for (int i = 0; i <= lastKey; i += 1) {
final int keyVal = orderedInsertion ? i : rndKeys.get(i);
byte[] keyBytes = padLeft
(BigInteger.valueOf(keyVal).toByteArray(), maxKeyBytes);
setKeyData(keyBytes, keyOffset, keyEntry, dataEntry);
final OperationStatus status;
if (duplicates) {
status = cursor.putNoDupData(keyEntry, dataEntry);
} else {
status = cursor.putNoOverwrite(keyEntry, dataEntry);
}
if (status == OperationStatus.KEYEXIST && !orderedInsertion) {
i -= 1;
continue;
}
if (status != OperationStatus.SUCCESS) {
throw new IllegalStateException("Could not insert: " +
status);
}
if (i % 10000 == 0) {
final EnvironmentStats stats = env.getStats(null);
if (stats.getNNodesScanned() > 0) {
throw new IllegalStateException
("*** Ran out of cache memory at record " + i +
" -- try increasing Java heap size ***");
}
if (out != null) {
out.print(".");
out.flush();
}
}
}
success = true;
} finally {
cursor.close();
if (success) {
txn.commit();
} else {
txn.abort();
}
}
/* Checkpoint to reset the memory budget. */
env.checkpoint(new CheckpointConfig().setForce(true));