use of herddb.model.TableAwareStatement in project herddb by diennea.
the class TableSpaceManager method executeTableAwareStatement.
private StatementExecutionResult executeTableAwareStatement(Statement statement, Transaction transaction, StatementEvaluationContext context) throws TableDoesNotExistException, StatementExecutionException {
generalLock.readLock().lock();
try {
TableAwareStatement st = (TableAwareStatement) statement;
String table = st.getTable();
AbstractTableManager manager = tables.get(table);
if (manager == null) {
throw new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName);
}
if (manager.getCreatedInTransaction() > 0) {
if (transaction == null || transaction.transactionId != manager.getCreatedInTransaction()) {
throw new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName + ". created temporary in transaction " + manager.getCreatedInTransaction());
}
}
return manager.executeStatement(statement, transaction, context);
} finally {
generalLock.readLock().unlock();
}
}
use of herddb.model.TableAwareStatement 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.TableAwareStatement in project herddb by diennea.
the class ServerSideConnectionPeer method handleExecuteStatement.
private void handleExecuteStatement(Message message, Channel _channel) {
Long tx = (Long) message.parameters.get("tx");
long txId = tx != null ? tx : TransactionContext.NOTRANSACTION_ID;
String query = (String) message.parameters.get("query");
String tableSpace = (String) message.parameters.get("tableSpace");
Boolean returnValues = (Boolean) message.parameters.get("returnValues");
if (returnValues == null) {
returnValues = Boolean.FALSE;
}
List<Object> parameters = (List<Object>) message.parameters.get("params");
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "query " + query + " with " + parameters);
}
try {
TransactionContext transactionContext = new TransactionContext(txId);
TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
Statement statement = translatedQuery.plan.mainStatement;
// LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", plan: " + translatedQuery.plan);
StatementExecutionResult result = server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
// LOGGER.log(Level.SEVERE, "query " + query + ", " + parameters + ", result:" + result);
if (result instanceof DMLStatementExecutionResult) {
DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
Map<String, Object> otherData = null;
if (returnValues && dml.getKey() != null) {
TableAwareStatement tableStatement = statement.unwrap(TableAwareStatement.class);
Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
otherData = new HashMap<>();
Object key = RecordSerializer.deserializePrimaryKey(dml.getKey().data, table);
otherData.put("key", key);
if (dml.getNewvalue() != null) {
Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
otherData.put("newvalue", newvalue);
}
}
_channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(dml.getUpdateCount(), otherData, dml.transactionId));
} else if (result instanceof GetResult) {
GetResult get = (GetResult) result;
if (!get.found()) {
_channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(0, null, get.transactionId));
} else {
Map<String, Object> record = get.getRecord().toBean(get.getTable());
_channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, record, get.transactionId));
}
} else if (result instanceof TransactionResult) {
TransactionResult txresult = (TransactionResult) result;
Map<String, Object> data = new HashMap<>();
Set<Long> transactionsForTableSpace = openTransactions.computeIfAbsent(statement.getTableSpace(), k -> new ConcurrentSkipListSet<>());
switch(txresult.getOutcome()) {
case BEGIN:
{
transactionsForTableSpace.add(txresult.getTransactionId());
break;
}
case COMMIT:
case ROLLBACK:
transactionsForTableSpace.remove(txresult.getTransactionId());
break;
}
data.put("tx", txresult.getTransactionId());
_channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, data, txresult.transactionId));
} else if (result instanceof DDLStatementExecutionResult) {
DDLStatementExecutionResult ddl = (DDLStatementExecutionResult) result;
_channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULT(1, null, ddl.transactionId));
} else {
_channel.sendReplyMessage(message, Message.ERROR(null, new Exception("unknown result type " + result.getClass() + " (" + result + ")")));
}
} catch (DuplicatePrimaryKeyException err) {
LOGGER.log(Level.SEVERE, "error on query " + query + ", parameters: " + parameters + ": err", err);
Message error = Message.ERROR(null, err);
_channel.sendReplyMessage(message, error);
} catch (NotLeaderException err) {
Message error = Message.ERROR(null, err);
error.setParameter("notLeader", "true");
_channel.sendReplyMessage(message, error);
} catch (StatementExecutionException err) {
Message error = Message.ERROR(null, err);
_channel.sendReplyMessage(message, error);
} catch (RuntimeException err) {
LOGGER.log(Level.SEVERE, "unexpected error on query " + query + ", parameters: " + parameters + ": err", err);
Message error = Message.ERROR(null, err);
_channel.sendReplyMessage(message, error);
}
}
use of herddb.model.TableAwareStatement in project herddb by diennea.
the class ServerSideConnectionPeer method handleExecuteStatements.
private void handleExecuteStatements(Message message, Channel _channel) {
Long tx = (Long) message.parameters.get("tx");
long txId = tx != null ? tx : TransactionContext.NOTRANSACTION_ID;
long transactionId = txId;
String query = (String) message.parameters.get("query");
String tableSpace = (String) message.parameters.get("tableSpace");
Boolean returnValues = (Boolean) message.parameters.get("returnValues");
if (returnValues == null) {
returnValues = Boolean.FALSE;
}
List<List<Object>> batch = (List<List<Object>>) message.parameters.get("params");
try {
List<Long> updateCounts = new ArrayList<>(batch.size());
List<Map<String, Object>> otherDatas = new ArrayList<>(batch.size());
for (int i = 0; i < batch.size(); i++) {
List<Object> parameters = batch.get(i);
TransactionContext transactionContext = new TransactionContext(transactionId);
TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
Statement statement = translatedQuery.plan.mainStatement;
StatementExecutionResult result = server.getManager().executePlan(translatedQuery.plan, translatedQuery.context, transactionContext);
if (transactionId > 0 && result.transactionId > 0 && transactionId != result.transactionId) {
throw new StatementExecutionException("transactionid changed during batch execution, " + transactionId + "<>" + result.transactionId);
}
transactionId = result.transactionId;
if (result instanceof DMLStatementExecutionResult) {
DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
Map<String, Object> otherData = Collections.emptyMap();
if (returnValues && dml.getKey() != null) {
TableAwareStatement tableStatement = (TableAwareStatement) statement;
Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
Object key = RecordSerializer.deserializePrimaryKey(dml.getKey().data, table);
otherData = new HashMap<>();
otherData.put("key", key);
if (dml.getNewvalue() != null) {
Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
otherData.put("newvalue", newvalue);
}
}
updateCounts.add(Long.valueOf(dml.getUpdateCount()));
otherDatas.add(otherData);
} else {
_channel.sendReplyMessage(message, Message.ERROR(null, new Exception("bad result type " + result.getClass() + " (" + result + ")")));
}
}
_channel.sendReplyMessage(message, Message.EXECUTE_STATEMENT_RESULTS(updateCounts, otherDatas, transactionId));
} catch (HerdDBInternalException err) {
Message error = Message.ERROR(null, err);
if (err instanceof NotLeaderException) {
error.setParameter("notLeader", "true");
}
_channel.sendReplyMessage(message, error);
}
}
Aggregations