CursorConfig cursorConfig = null;
if (cdbMode) {
cursorConfig = new CursorConfig();
DbCompat.setWriteCursor(cursorConfig, true);
}
Cursor cursor = null;
Transaction txn = null;
try {
if (txnMode) {
txn = db.getEnvironment().beginTransaction(null, null);
}
cursor = db.openCursor(txn, cursorConfig);
/* Get the current class ID. */
DatabaseEntry key = new DatabaseEntry(LAST_CLASS_ID_KEY);
DatabaseEntry data = new DatabaseEntry();
OperationStatus status = cursor.getSearchKey(key, data,
writeLockMode);
if (status != OperationStatus.SUCCESS) {
throw DbCompat.unexpectedState("Class ID not initialized");
}
byte[] idBytes = getBytes(data);
/* Increment the ID by one and write the updated record. */
idBytes = incrementID(idBytes);
data.setData(idBytes);
cursor.put(key, data);
/*
* Write the new class format record whose key is the ID just
* assigned.
*/
byte[] keyBytes = new byte[1 + idBytes.length];
keyBytes[0] = REC_CLASS_FORMAT;
System.arraycopy(idBytes, 0, keyBytes, 1, idBytes.length);
key.setData(keyBytes);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(classFormat);
} catch (IOException e) {
throw RuntimeExceptionWrapper.wrapIfNeeded(e);
}
data.setData(baos.toByteArray());
cursor.put(key, data);
/*
* Write the new class info record, using the key passed in; this
* is done last so that a reader who gets the class info record
* first will always find the corresponding class format record.
*/
classInfo.setClassID(idBytes);
classInfo.toDbt(data);
cursor.put(classKey, data);
/*
* Update the maps before closing the cursor, so that the cursor
* lock prevents other writers from duplicating this entry.
*/
classInfo.setClassFormat(classFormat);
classMap.put(className, classInfo);
formatMap.put(new BigInteger(idBytes), classFormat);
return classInfo;
} finally {
if (cursor != null) {
cursor.close();
}
if (txn != null) {
txn.commit();
}
}