use of herddb.model.StatementExecutionException in project herddb by diennea.
the class TableSpaceManager method rollbackTransaction.
private CompletableFuture<StatementExecutionResult> rollbackTransaction(RollbackTransactionStatement statement, StatementEvaluationContext context) throws StatementExecutionException {
long txId = statement.getTransactionId();
validateTransactionBeforeTxCommand(txId);
LogEntry entry = LogEntryFactory.rollbackTransaction(txId);
long lockStamp = context.getTableSpaceLock();
boolean lockAcquired = false;
if (lockStamp == 0) {
lockStamp = acquireReadLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
CommitLogResult pos = log.log(entry, true);
CompletableFuture<StatementExecutionResult> res = pos.logSequenceNumber.thenApplyAsync((lsn) -> {
apply(pos, entry, false);
return new TransactionResult(txId, TransactionResult.OutcomeType.ROLLBACK);
}, callbacksExecutor);
if (lockAcquired) {
res = releaseReadLock(res, lockStamp, statement).thenApply(s -> {
context.setTableSpaceLock(0);
return s;
});
}
return res;
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class TableSpaceManager method createTable.
private StatementExecutionResult createTable(CreateTableStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
boolean lockAcquired = false;
if (context.getTableSpaceLock() == 0) {
long lockStamp = acquireWriteLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
try {
if (tables.containsKey(statement.getTableDefinition().name)) {
if (statement.isIfExistsClause()) {
return new DDLStatementExecutionResult(transaction != null ? transaction.transactionId : 0);
}
throw new TableAlreadyExistsException(statement.getTableDefinition().name);
}
for (Index additionalIndex : statement.getAdditionalIndexes()) {
AbstractIndexManager exists = indexes.get(additionalIndex.name);
if (exists != null) {
LOGGER.log(Level.INFO, "Error while creating index " + additionalIndex.name + ", there is already an index " + exists.getIndex().name + " on table " + exists.getIndex().table);
throw new IndexAlreadyExistsException(additionalIndex.name);
}
}
Table table = statement.getTableDefinition();
// validate foreign keys
if (table.foreignKeys != null) {
for (ForeignKeyDef def : table.foreignKeys) {
AbstractTableManager parentTableManager = null;
for (AbstractTableManager ab : tables.values()) {
if (ab.getTable().uuid.equals(def.parentTableId)) {
parentTableManager = ab;
break;
}
}
if (parentTableManager == null) {
throw new StatementExecutionException("Table " + def.parentTableId + " does not exist in tablespace " + tableSpaceName);
}
Table parentTable = parentTableManager.getTable();
int i = 0;
for (String col : def.columns) {
Column column = table.getColumn(col);
Column parentColumn = parentTable.getColumn(def.parentTableColumns[i]);
if (column == null) {
throw new StatementExecutionException("Cannot find column " + col);
}
if (parentColumn == null) {
throw new StatementExecutionException("Cannot find column " + def.parentTableColumns[i]);
}
if (!ColumnTypes.sameRawDataType(column.type, parentColumn.type)) {
throw new StatementExecutionException("Column " + table.name + "." + column.name + " is not the same tyepe of column " + parentTable.name + "." + parentColumn.name);
}
i++;
}
}
}
LogEntry entry = LogEntryFactory.createTable(statement.getTableDefinition(), transaction);
CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
apply(pos, entry, false);
for (Index additionalIndex : statement.getAdditionalIndexes()) {
LogEntry index_entry = LogEntryFactory.createIndex(additionalIndex, transaction);
CommitLogResult index_pos = log.log(index_entry, index_entry.transactionId <= 0);
apply(index_pos, index_entry, false);
}
return new DDLStatementExecutionResult(entry.transactionId);
} catch (DataStorageManagerException | LogNotAvailableException err) {
throw new StatementExecutionException(err);
} finally {
if (lockAcquired) {
releaseWriteLock(context.getTableSpaceLock(), statement);
context.setTableSpaceLock(0);
}
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class TableSpaceManager method createIndex.
private StatementExecutionResult createIndex(CreateIndexStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
boolean lockAcquired = false;
if (context.getTableSpaceLock() == 0) {
long lockStamp = acquireWriteLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
try {
AbstractIndexManager exists = indexes.get(statement.getIndexDefinition().name);
if (exists != null) {
LOGGER.log(Level.INFO, "Error while creating index " + statement.getIndexDefinition().name + ", there is already an index " + exists.getIndex().name + " on table " + exists.getIndex().table);
throw new IndexAlreadyExistsException(statement.getIndexDefinition().name);
}
LogEntry entry = LogEntryFactory.createIndex(statement.getIndexDefinition(), transaction);
CommitLogResult pos;
try {
pos = log.log(entry, entry.transactionId <= 0);
} catch (LogNotAvailableException ex) {
throw new StatementExecutionException(ex);
}
apply(pos, entry, false);
return new DDLStatementExecutionResult(entry.transactionId);
} catch (DataStorageManagerException err) {
throw new StatementExecutionException(err);
} finally {
if (lockAcquired) {
releaseWriteLock(context.getTableSpaceLock(), statement);
context.setTableSpaceLock(0);
}
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class TableManager method executeStatementAsync.
@Override
public CompletableFuture<StatementExecutionResult> executeStatementAsync(Statement statement, Transaction transaction, StatementEvaluationContext context) {
CompletableFuture<StatementExecutionResult> res;
long lockStamp = checkpointLock.readLock();
if (statement instanceof UpdateStatement) {
UpdateStatement update = (UpdateStatement) statement;
res = executeUpdateAsync(update, transaction, context);
} else if (statement instanceof InsertStatement) {
InsertStatement insert = (InsertStatement) statement;
res = executeInsertAsync(insert, transaction, context);
} else if (statement instanceof GetStatement) {
GetStatement get = (GetStatement) statement;
res = executeGetAsync(get, transaction, context);
} else if (statement instanceof DeleteStatement) {
DeleteStatement delete = (DeleteStatement) statement;
res = executeDeleteAsync(delete, transaction, context);
} else if (statement instanceof TruncateTableStatement) {
try {
TruncateTableStatement truncate = (TruncateTableStatement) statement;
res = CompletableFuture.completedFuture(executeTruncate(truncate, transaction, context));
} catch (StatementExecutionException err) {
LOGGER.log(Level.SEVERE, "Truncate table failed", err);
res = Futures.exception(err);
}
} else if (statement instanceof TableConsistencyCheckStatement) {
DBManager manager = this.tableSpaceManager.getDbmanager();
res = CompletableFuture.completedFuture(manager.createTableCheckSum((TableConsistencyCheckStatement) statement, context));
} else {
res = Futures.exception(new StatementExecutionException("not implemented " + statement.getClass()));
}
res = res.whenComplete((r, error) -> {
checkpointLock.unlockRead(lockStamp);
});
if (statement instanceof TruncateTableStatement) {
res = res.whenComplete((r, error) -> {
if (error == null) {
try {
flush();
} catch (DataStorageManagerException err) {
throw new HerdDBInternalException(new StatementExecutionException("internal data error: " + err, err));
}
}
});
}
return res;
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class TableManager method rebuildNextPrimaryKeyValue.
private void rebuildNextPrimaryKeyValue() throws DataStorageManagerException {
LOGGER.log(Level.INFO, "rebuildNextPrimaryKeyValue");
try {
Stream<Entry<Bytes, Long>> scanner = keyToPage.scanner(null, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), tableContext, null);
scanner.forEach((Entry<Bytes, Long> t) -> {
Bytes key = t.getKey();
long pk_logical_value;
if (table.getColumn(table.primaryKey[0]).type == ColumnTypes.INTEGER || table.getColumn(table.primaryKey[0]).type == ColumnTypes.NOTNULL_INTEGER) {
pk_logical_value = key.to_int();
} else {
pk_logical_value = key.to_long();
}
nextPrimaryKeyValue.accumulateAndGet(pk_logical_value + 1, EnsureLongIncrementAccumulator.INSTANCE);
});
LOGGER.log(Level.INFO, "rebuildNextPrimaryKeyValue, newPkValue : " + nextPrimaryKeyValue.get());
} catch (StatementExecutionException impossible) {
throw new DataStorageManagerException(impossible);
}
}
Aggregations