DatabaseImpl db = null;
synchronized (getDBModificationLock()) {
synchronized (dbs.getCheckpointer()) {
if (dbsByName.containsKey(operation.getDatabaseName())) {
throw new BabuDBException(ErrorCode.DB_EXISTS, "database '" +
operation.getDatabaseName() + "' already exists");
}
final int dbId = nextDbId++;
db = new DatabaseImpl(dbs, new LSMDatabase(operation.getDatabaseName(),
dbId, dbs.getConfig().getBaseDir() + operation.getDatabaseName() +
File.separatorChar, numIndices, false, com, dbs.getConfig()
.getCompression(), dbs.getConfig().getMaxNumRecordsPerBlock(), dbs
.getConfig().getMaxBlockFileSize(),
dbs.getConfig().getDisableMMap(), dbs.getConfig().getMMapLimit()));
dbsById.put(dbId, db);
dbsByName.put(operation.getDatabaseName(), db);
dbs.getDBConfigFile().save();
}
}
return db;
}
});
dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_DELETE_DB,
new InMemoryProcessing() {
@Override
public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
serialized.getInt(); // do not use, deprecated
String dbName = serialized.getString();
serialized.flip();
return new Object[] { dbName };
}
@Override
public OperationInternal convertToOperation(Object[] args) {
return new BabuDBTransaction.BabuDBOperation(Operation.TYPE_DELETE_DB, (String) args[0],
null);
}
@Override
public Object process(OperationInternal operation) throws BabuDBException {
int dbId = InsertRecordGroup.DB_ID_UNKNOWN;
synchronized (getDBModificationLock()) {
synchronized (dbs.getCheckpointer()) {
if (!dbsByName.containsKey(operation.getDatabaseName())) {
throw new BabuDBException(ErrorCode.NO_SUCH_DB, "database '" +
operation.getDatabaseName() + "' does not exists");
}
final LSMDatabase db = getDatabase(operation.getDatabaseName()).getLSMDB();
dbId = db.getDatabaseId();
dbsByName.remove(operation.getDatabaseName());
dbsById.remove(dbId);
dbs.getSnapshotManager().deleteAllSnapshots(operation.getDatabaseName());
dbs.getDBConfigFile().save();
File dbDir = new File(dbs.getConfig().getBaseDir(),
operation.getDatabaseName());
if (dbDir.exists()) {
FSUtils.delTree(dbDir);
}
}
}
return null;
}
});
dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_COPY_DB,
new InMemoryProcessing() {
@Override
public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
serialized.getInt(); // do not use, deprecated
serialized.getInt(); // do not use, deprecated
String sourceDB = serialized.getString();
String destDB = serialized.getString();
serialized.flip();
return new Object[] { sourceDB, destDB };
}
@Override
public OperationInternal convertToOperation(Object[] args) {
return new BabuDBTransaction.BabuDBOperation(Operation.TYPE_COPY_DB, (String) args[0],
new Object[] { args[1] });
}
@Override
public Object process(OperationInternal operation) throws BabuDBException {
// parse args
String destDB = (String) operation.getParams()[0];
DatabaseInternal sDB = getDatabase(operation.getDatabaseName());
int dbId;
synchronized (getDBModificationLock()) {
synchronized (dbs.getCheckpointer()) {
if (dbsByName.containsKey(destDB)) {
throw new BabuDBException(ErrorCode.DB_EXISTS, "database '" + destDB
+ "' already exists");
}
dbId = nextDbId++;
// just "reserve" the name
dbsByName.put(destDB, null);
dbs.getDBConfigFile().save();
}
}
// materializing the snapshot takes some time, we should not
// hold the
// lock meanwhile!
try {
sDB.proceedSnapshot(destDB);
} catch (InterruptedException i) {
throw new BabuDBException(ErrorCode.INTERNAL_ERROR,
"Snapshot creation was interrupted.", i);
}
// create new DB and load from snapshot
DatabaseInternal newDB = new DatabaseImpl(dbs, new LSMDatabase(destDB, dbId,
dbs.getConfig().getBaseDir() + destDB + File.separatorChar,
sDB.getLSMDB().getIndexCount(), true, sDB.getComparators(),
dbs.getConfig().getCompression(),
dbs.getConfig().getMaxNumRecordsPerBlock(),
dbs.getConfig().getMaxBlockFileSize(), dbs.getConfig().getDisableMMap(),
dbs.getConfig().getMMapLimit()));
// insert real database
synchronized (dbModificationLock) {
dbsById.put(dbId, newDB);
dbsByName.put(destDB, newDB);
dbs.getDBConfigFile().save();
}
return null;
}
});
dbs.getTransactionManager().registerInMemoryProcessing(Operation.TYPE_GROUP_INSERT,
new InMemoryProcessing() {
@Override
public Object[] deserializeRequest(ReusableBuffer serialized) throws BabuDBException {
InsertRecordGroup irg = InsertRecordGroup.deserialize(serialized);
serialized.flip();
return new Object[] { irg, null };
}
@Override
public OperationInternal convertToOperation(Object[] args) {
return new BabuDBTransaction.BabuDBOperation(Operation.TYPE_GROUP_INSERT, (String) null,
new Object[] { args[0] });
}
@Override
public Object process(OperationInternal operation) throws BabuDBException {
Object[] args = operation.getParams();
// parse args
InsertRecordGroup irg = (InsertRecordGroup) args[0];
LSMDatabase lsmDB = null;
if (args.length > 1 && args[1] instanceof LSMDatabase) {
lsmDB = (LSMDatabase) args[1];
}
// complete the arguments
if (lsmDB == null) {
// set the DB ID, if unknown
if (irg.getDatabaseId() == InsertRecordGroup.DB_ID_UNKNOWN) {
irg.setDatabaseId(getDatabase(
operation.getDatabaseName()).getLSMDB().getDatabaseId());
}
lsmDB = getDatabase(irg.getDatabaseId()).getLSMDB();
operation.updateParams(new Object[] { irg, lsmDB });
}
if (operation.getDatabaseName() == null) {
operation.updateDatabaseName(lsmDB.getDatabaseName());
}
int numIndices = lsmDB.getIndexCount();
// check for user errors
for (InsertRecord ir : irg.getInserts()) {
if ((ir.getIndexId() >= numIndices) || (ir.getIndexId() < 0)) {
throw new BabuDBException(ErrorCode.NO_SUCH_INDEX, "index " +
ir.getIndexId() + " does not exist");
}
}
// insert into the in-memory-tree