final int maxValSize = 16384;
// FSUtils.delTree(new File(dbDir));
// start the database
final BabuDB databaseSystem = BabuDBFactory.createBabuDB(new BabuDBConfig(dbDir, dbDir, numThreads,
maxLogFileSize, 10, SyncMode.ASYNC, 1000, 0, false, 16, 1024 * 1024 * 256));
DatabaseManager dbm = databaseSystem.getDatabaseManager();
// for (int i = 0; i < numDBs; i++)
// dbm.createDatabase("DB" + i, numIndices);
dbm.createDatabase("blub", numIndices);
final Database[] dbs = new Database[numDBs];
for (int i = 0; i < dbs.length; i++)
dbs[i] = dbm.getDatabase("DB" + i);
Thread writeThread = new Thread() {
public void run() {
try {
System.out.println("starting write thread...");
// insert a random entry in a random database
for (int i = 0; i < NUM_INSERTS; i++) {
int db = (int) (Math.random() * numDBs);
byte[] key = randomBytes(minKeySize, maxKeySize);
byte[] val = randomBytes(minValSize, maxValSize);
int index = (int) (Math.random() * numIndices);
dbs[db].singleInsert(index, key, val, null).get();
if (i % 1000 == 0) {
System.out.print(".");
Thread.sleep(5000);
}
if (i % 10000 == 9999) {
System.out.println("\ncheckpoint...");
databaseSystem.getCheckpointer().checkpoint();
System.out.println("done");
}
if (i % 40000 == 39999)
System.exit(0);
}
System.out.println("write thread finished");
} catch (Throwable th) {
th.printStackTrace();
}
}
};
Thread readThread = new Thread() {
// public void run() {
//
// try {
//
// System.out.println("starting read thread...");
//
// // insert a random entry in a random database
//
// for (int i = 0; i < NUM_READS; i++) {
//
// int db = (int) (Math.random() * numDBs);
// byte[] key = new byte[0];
// int index = (int) (Math.random() * numIndices);
//
// Iterator<Entry<byte[], byte[]>> it = dbs[db]
// .prefixLookup(index, key, null).get();
//
// synchronized (lock) {
// while (it.hasNext())
// it.next();
// }
// }
//
// System.out.println("read thread finished");
//
// } catch (Throwable th) {
// th.printStackTrace();
// }
// }
};
writeThread.start();
readThread.start();
writeThread.join();
readThread.join();
// create a checkpoint for faster start-ups
// databaseSystem.getCheckpointer().checkpoint();
// shutdown database
databaseSystem.shutdown();
}