use of herddb.model.TransactionContext in project herddb by diennea.
the class TableSpaceManager method executeStatement.
public StatementExecutionResult executeStatement(Statement statement, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
boolean rollbackOnError = false;
/* Do not autostart transaction on alter table statements */
if (transactionContext.transactionId == TransactionContext.AUTOTRANSACTION_ID && statement.supportsTransactionAutoCreate()) {
StatementExecutionResult newTransaction = beginTransaction();
transactionContext = new TransactionContext(newTransaction.transactionId);
rollbackOnError = true;
}
Transaction transaction = transactions.get(transactionContext.transactionId);
if (transaction != null && !transaction.tableSpace.equals(tableSpaceName)) {
throw new StatementExecutionException("transaction " + transaction.transactionId + " is for tablespace " + transaction.tableSpace + ", not for " + tableSpaceName);
}
if (transactionContext.transactionId > 0 && transaction == null) {
throw new StatementExecutionException("transaction " + transactionContext.transactionId + " not found on tablespace " + tableSpaceName);
}
try {
if (statement instanceof TableAwareStatement) {
return executeTableAwareStatement(statement, transaction, context);
}
if (statement instanceof SQLPlannedOperationStatement) {
return executePlannedOperationStatement(statement, transactionContext, context);
}
if (statement instanceof BeginTransactionStatement) {
if (transaction != null) {
throw new IllegalArgumentException("transaction already started");
}
return beginTransaction();
}
if (statement instanceof RollbackTransactionStatement) {
return rollbackTransaction((RollbackTransactionStatement) statement);
}
if (statement instanceof CommitTransactionStatement) {
return commitTransaction((CommitTransactionStatement) statement);
}
if (statement instanceof CreateTableStatement) {
return createTable((CreateTableStatement) statement, transaction);
}
if (statement instanceof CreateIndexStatement) {
return createIndex((CreateIndexStatement) statement, transaction);
}
if (statement instanceof DropTableStatement) {
return dropTable((DropTableStatement) statement, transaction);
}
if (statement instanceof DropIndexStatement) {
return dropIndex((DropIndexStatement) statement, transaction);
}
if (statement instanceof AlterTableStatement) {
return alterTable((AlterTableStatement) statement, transactionContext);
}
throw new StatementExecutionException("unsupported statement " + statement);
} catch (StatementExecutionException error) {
if (rollbackOnError) {
rollbackTransaction(new RollbackTransactionStatement(tableSpaceName, transactionContext.transactionId));
}
throw error;
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class DBManager method executeJoinedScansPlan.
private StatementExecutionResult executeJoinedScansPlan(List<DataScanner> scanResults, StatementEvaluationContext context, TransactionContext transactionContext, ExecutionPlan plan) throws StatementExecutionException {
try {
List<Column> composedSchema = new ArrayList<>();
for (DataScanner ds : scanResults) {
composedSchema.addAll(Arrays.asList(ds.getSchema()));
}
Column[] finalSchema = new Column[composedSchema.size()];
composedSchema.toArray(finalSchema);
String[] finalSchemaFieldNames = Column.buildFieldNamesList(finalSchema);
MaterializedRecordSet finalResultSet = recordSetFactory.createRecordSet(finalSchemaFieldNames, finalSchema);
DataScannerJoinExecutor joinExecutor;
if (plan.joinProjection != null) {
if (plan.joinFilter != null) {
TuplePredicate joinFilter = plan.joinFilter;
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
if (joinFilter.matches(t, context)) {
finalResultSet.add(plan.joinProjection.map(t, context));
}
});
} else {
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
finalResultSet.add(plan.joinProjection.map(t, context));
});
}
} else {
if (plan.joinFilter != null) {
TuplePredicate joinFilter = plan.joinFilter;
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
if (joinFilter.matches(t, context)) {
finalResultSet.add(t);
}
});
} else {
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
finalResultSet.add(t);
});
}
}
joinExecutor.executeJoin();
finalResultSet.writeFinished();
finalResultSet.sort(plan.comparator);
finalResultSet.applyLimits(plan.limits, context);
return new ScanResult(transactionContext.transactionId, new SimpleDataScanner(transactionContext.transactionId, finalResultSet));
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class DeleteOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
ScanResult downstreamScanResult = (ScanResult) input;
final Table table = tableSpaceManager.getTableManager(tableName).getTable();
long transactionId = transactionContext.transactionId;
int updateCount = 0;
Bytes key = null;
try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
while (inputScanner.hasNext()) {
DataAccessor row = inputScanner.next();
long transactionIdFromScanner = inputScanner.getTransactionId();
if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
transactionId = transactionIdFromScanner;
transactionContext = new TransactionContext(transactionId);
}
key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
DMLStatement deleteStatement = new DeleteStatement(tableSpace, tableName, null, new RawKeyEquals(key));
DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(deleteStatement, context, transactionContext);
updateCount += _result.getUpdateCount();
if (_result.transactionId > 0 && _result.transactionId != transactionId) {
transactionId = _result.transactionId;
transactionContext = new TransactionContext(transactionId);
}
key = _result.getKey();
}
return new DMLStatementExecutionResult(transactionId, updateCount, key, null);
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class JoinOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
ScanResult resLeft = (ScanResult) left.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
transactionContext = new TransactionContext(resLeft.transactionId);
ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
final long resTransactionId = resRight.transactionId;
final String[] fieldNamesFromLeft = resLeft.dataScanner.getFieldNames();
final String[] fieldNamesFromRight = resRight.dataScanner.getFieldNames();
Enumerable<DataAccessor> result = mergeJoin ? EnumerableDefaults.mergeJoin(resLeft.dataScanner.createEnumerable(), resRight.dataScanner.createEnumerable(), JoinKey.keyExtractor(leftKeys), JoinKey.keyExtractor(rightKeys), resultProjection(fieldNamesFromLeft, fieldNamesFromRight), generateNullsOnLeft, generateNullsOnRight) : EnumerableDefaults.join(resLeft.dataScanner.createEnumerable(), resRight.dataScanner.createEnumerable(), JoinKey.keyExtractor(leftKeys), JoinKey.keyExtractor(rightKeys), resultProjection(fieldNamesFromLeft, fieldNamesFromRight), null, generateNullsOnLeft, generateNullsOnRight);
EnumerableDataScanner joinedScanner = new EnumerableDataScanner(resTransactionId, fieldNames, columns, result);
return new ScanResult(resTransactionId, joinedScanner);
}
use of herddb.model.TransactionContext in project herddb by diennea.
the class UpdateOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
ScanResult downstreamScanResult = (ScanResult) input;
final Table table = tableSpaceManager.getTableManager(tableName).getTable();
long transactionId = transactionContext.transactionId;
int updateCount = 0;
Bytes key = null;
Bytes newValue = null;
try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
while (inputScanner.hasNext()) {
DataAccessor row = inputScanner.next();
long transactionIdFromScanner = inputScanner.getTransactionId();
if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
transactionId = transactionIdFromScanner;
transactionContext = new TransactionContext(transactionId);
}
key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
Predicate pred = new RawKeyEquals(key);
DMLStatement updateStatement = new UpdateStatement(tableSpace, tableName, null, this.recordFunction, pred).setReturnValues(returnValues);
DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(updateStatement, context, transactionContext);
updateCount += _result.getUpdateCount();
if (_result.transactionId > 0 && _result.transactionId != transactionId) {
transactionId = _result.transactionId;
transactionContext = new TransactionContext(transactionId);
}
key = _result.getKey();
newValue = _result.getNewvalue();
}
return new DMLStatementExecutionResult(transactionId, updateCount, key, newValue);
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
Aggregations