void rundb(int count, TestOptions options)
throws DatabaseException, FileNotFoundException
{
String name;
Database db;
if(options.database == null)
{
if (options.db_env != null)
name = DATABASETEST_DBNAME;
else
name = TestUtils.getDBFileName(DATABASETEST_DBNAME);
if(count == 0)
options.db_config.setAllowCreate(true);
if(options.db_env == null)
db = new Database(name, null, options.db_config);
else
db = options.db_env.openDatabase(null, name, null, options.db_config);
} else {
db = options.database;
}
// The bit map of keys we've seen
long bitmap = 0;
// The bit map of keys we expect to see
long expected = (1 << (count+1)) - 1;
byte outbuf[] = new byte[count+1];
int i;
for (i=0; i<count; i++) {
outbuf[i] = (byte)('0' + i);
}
outbuf[i++] = (byte)'x';
DatabaseEntry key = new DatabaseEntry(outbuf, 0, i);
DatabaseEntry data = new DatabaseEntry(outbuf, 0, i);
TestUtils.DEBUGOUT("Put: " + (char)outbuf[0] + ": " + new String(outbuf, 0, i));
db.putNoOverwrite(null, key, data);
// Acquire a cursor for the table.
Cursor dbcp = db.openCursor(null, CursorConfig.DEFAULT);
// Walk through the table, checking
DatabaseEntry readkey = new DatabaseEntry();
DatabaseEntry readdata = new DatabaseEntry();
DatabaseEntry whoknows = new DatabaseEntry();
/*
* NOTE: Maybe want to change from user-buffer to DB buffer
* depending on the flag options.user_buffer (setReuseBuffer)
* The old version set MALLOC/REALLOC here - not sure if it is the same.
*/
TestUtils.DEBUGOUT("Dbc.get");
while (dbcp.getNext(readkey, readdata, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
String key_string =
new String(readkey.getData(), 0, readkey.getSize());
String data_string =
new String(readdata.getData(), 0, readkey.getSize());
TestUtils.DEBUGOUT("Got: " + key_string + ": " + data_string);
int len = key_string.length();
if (len <= 0 || key_string.charAt(len-1) != 'x') {
TestUtils.ERR("reread terminator is bad");
}
len--;
long bit = (1 << len);
if (len > count) {
TestUtils.ERR("reread length is bad: expect " + count + " got "+ len + " (" + key_string + ")" );
}
else if (!data_string.equals(key_string)) {
TestUtils.ERR("key/data don't match");
}
else if ((bitmap & bit) != 0) {
TestUtils.ERR("key already seen");
}
else if ((expected & bit) == 0) {
TestUtils.ERR("key was not expected");
}
else {
bitmap |= bit;
expected &= ~(bit);
for (i=0; i<len; i++) {
if (key_string.charAt(i) != ('0' + i)) {
System.out.print(" got " + key_string
+ " (" + (int)key_string.charAt(i)
+ "), wanted " + i
+ " (" + (int)('0' + i)
+ ") at position " + i + "\n");
TestUtils.ERR("key is corrupt");
}
}
}
}
if (expected != 0) {
System.out.print(" expected more keys, bitmap is: " + expected + "\n");
TestUtils.ERR("missing keys in database");
}
dbcp.close();
TestUtils.DEBUGOUT("options.save_db " + options.save_db + " options.database " + options.database);
if(options.save_db == false)
db.close(false);
else if (options.database == null)
options.database = db;
}