use of herddb.log.CommitLogResult in project herddb by diennea.
the class TableSpaceManager method alterTable.
private StatementExecutionResult alterTable(AlterTableStatement alterTableStatement, TransactionContext transactionContext) throws TableDoesNotExistException, StatementExecutionException {
generalLock.writeLock().lock();
try {
if (transactionContext.transactionId > 0) {
throw new StatementExecutionException("ALTER TABLE cannot be executed inside a transaction (txid=" + transactionContext.transactionId + ")");
}
AbstractTableManager tableManager = tables.get(alterTableStatement.getTable());
if (tableManager == null) {
throw new TableDoesNotExistException("no table " + alterTableStatement.getTable() + " in tablespace " + tableSpaceName);
}
Table newTable;
try {
newTable = tableManager.getTable().applyAlterTable(alterTableStatement);
} catch (IllegalArgumentException error) {
throw new StatementExecutionException(error);
}
LogEntry entry = LogEntryFactory.alterTable(newTable, null);
try {
CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
apply(pos, entry, false);
} catch (Exception err) {
throw new StatementExecutionException(err);
}
return new DDLStatementExecutionResult(transactionContext.transactionId);
} finally {
generalLock.writeLock().unlock();
}
}
use of herddb.log.CommitLogResult in project herddb by diennea.
the class TableSpaceManager method dropIndex.
private StatementExecutionResult dropIndex(DropIndexStatement statement, Transaction transaction) throws StatementExecutionException {
generalLock.writeLock().lock();
try {
if (!indexes.containsKey(statement.getIndexName())) {
if (statement.isIfExists()) {
return new DDLStatementExecutionResult(transaction != null ? transaction.transactionId : 0);
}
throw new IndexDoesNotExistException("index " + statement.getIndexName() + " does not exist " + statement.getIndexName() + " on tableSpace " + statement.getTableSpace());
}
if (transaction != null && transaction.isIndexDropped(statement.getIndexName())) {
if (statement.isIfExists()) {
return new DDLStatementExecutionResult(transaction.transactionId);
}
throw new IndexDoesNotExistException("index does not exist " + statement.getIndexName() + " on tableSpace " + statement.getTableSpace());
}
LogEntry entry = LogEntryFactory.dropIndex(statement.getIndexName(), 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 {
generalLock.writeLock().unlock();
}
}
use of herddb.log.CommitLogResult in project herddb by diennea.
the class TableSpaceManager method beginTransaction.
private StatementExecutionResult beginTransaction() throws StatementExecutionException {
long id = newTransactionId.incrementAndGet();
LogEntry entry = LogEntryFactory.beginTransaction(id);
CommitLogResult pos;
generalLock.readLock().lock();
try {
pos = log.log(entry, false);
apply(pos, entry, false);
return new TransactionResult(id, TransactionResult.OutcomeType.BEGIN);
} catch (Exception err) {
throw new StatementExecutionException(err);
} finally {
generalLock.readLock().unlock();
}
}
use of herddb.log.CommitLogResult in project herddb by diennea.
the class TableSpaceManager method commitTransaction.
private StatementExecutionResult commitTransaction(CommitTransactionStatement commitTransactionStatement) throws StatementExecutionException {
long txId = commitTransactionStatement.getTransactionId();
LogEntry entry = LogEntryFactory.commitTransaction(txId);
generalLock.readLock().lock();
try {
Transaction tx = transactions.get(txId);
if (tx == null) {
throw new StatementExecutionException("no such transaction " + commitTransactionStatement.getTransactionId());
}
CommitLogResult pos = log.log(entry, true);
apply(pos, entry, false);
} catch (Exception err) {
throw new StatementExecutionException(err);
} finally {
generalLock.readLock().unlock();
}
return new TransactionResult(txId, TransactionResult.OutcomeType.COMMIT);
}
use of herddb.log.CommitLogResult in project herddb by diennea.
the class TableSpaceManager method startAsLeader.
void startAsLeader() throws DataStorageManagerException, DDLException, LogNotAvailableException {
if (virtual) {
} else {
LOGGER.log(Level.SEVERE, "startAsLeader {0} tablespace {1}", new Object[] { nodeId, tableSpaceName });
recoverForLeadership();
// every pending transaction MUST be rollback back
List<Long> pending_transactions = new ArrayList<>(this.transactions.keySet());
log.startWriting();
LOGGER.log(Level.SEVERE, "startAsLeader {0} tablespace {1} log, there were {2} pending transactions to be rolledback", new Object[] { nodeId, tableSpaceName, pending_transactions.size() });
for (long tx : pending_transactions) {
LOGGER.log(Level.FINER, "rolling back transaction {0}", tx);
LogEntry rollback = LogEntryFactory.rollbackTransaction(tx);
// let followers see the rollback on the log
CommitLogResult pos = log.log(rollback, true);
apply(pos, rollback, false);
}
}
leader = true;
}
Aggregations