use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class TableSpaceManager method executeTableAwareStatement.
private CompletableFuture<StatementExecutionResult> executeTableAwareStatement(Statement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
long lockStamp = context.getTableSpaceLock();
boolean lockAcquired = false;
if (lockStamp == 0) {
lockStamp = acquireReadLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
TableAwareStatement st = (TableAwareStatement) statement;
String table = st.getTable();
AbstractTableManager manager = tables.get(table);
CompletableFuture<StatementExecutionResult> res;
if (manager == null) {
res = Futures.exception(new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName));
} else if (manager.getCreatedInTransaction() > 0 && (transaction == null || transaction.transactionId != manager.getCreatedInTransaction())) {
res = Futures.exception(new TableDoesNotExistException("no table " + table + " in tablespace " + tableSpaceName + ". created temporary in transaction " + manager.getCreatedInTransaction()));
} else {
res = manager.executeStatementAsync(statement, transaction, context);
}
if (lockAcquired) {
res = releaseReadLock(res, lockStamp, statement).whenComplete((s, err) -> {
context.setTableSpaceLock(0);
});
}
return res;
}
use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class TableSpaceManager method alterTable.
private StatementExecutionResult alterTable(AlterTableStatement statement, TransactionContext transactionContext, StatementEvaluationContext context) throws StatementExecutionException {
boolean lockAcquired = false;
if (context.getTableSpaceLock() == 0) {
long lockStamp = acquireWriteLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
try {
if (transactionContext.transactionId > 0) {
Transaction transaction = transactions.get(transactionContext.transactionId);
if (transactionContext.transactionId > 0 && transaction == null) {
throw new StatementExecutionException("transaction " + transactionContext.transactionId + " does not exist on tablespace " + tableSpaceName);
}
if (transaction != null && !transaction.tableSpace.equals(tableSpaceName)) {
throw new StatementExecutionException("transaction " + transaction.transactionId + " is for tablespace " + transaction.tableSpace + ", not for " + tableSpaceName);
}
LOGGER.log(Level.INFO, "Implicitly committing transaction " + transactionContext.transactionId + " due to an ALTER TABLE statement in tablespace " + tableSpaceName);
try {
commitTransaction(new CommitTransactionStatement(tableSpaceName, transactionContext.transactionId), context).join();
} catch (CompletionException err) {
throw new StatementExecutionException(err);
}
transactionContext = TransactionContext.NO_TRANSACTION;
}
AbstractTableManager tableManager = tables.get(statement.getTable());
if (tableManager == null) {
throw new TableDoesNotExistException("no table " + statement.getTable() + " in tablespace " + tableSpaceName + "," + " only " + tables.keySet());
}
Table oldTable = tableManager.getTable();
Table[] childrenTables = collectChildrenTables(oldTable);
if (childrenTables != null) {
for (Table child : childrenTables) {
for (String col : statement.getDropColumns()) {
for (ForeignKeyDef fk : child.foreignKeys) {
if (fk.parentTableId.equals(oldTable.uuid)) {
if (Stream.of(fk.parentTableColumns).anyMatch(c -> c.equalsIgnoreCase(col))) {
throw new StatementExecutionException("Cannot drop column " + oldTable.name + "." + col + " because of foreign key constraint " + fk.name + " on table " + child.name);
}
}
}
}
}
}
Table newTable;
try {
newTable = tableManager.getTable().applyAlterTable(statement);
} catch (IllegalArgumentException error) {
throw new StatementExecutionException(error);
}
validateAlterTable(newTable, context);
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);
}
// Here transactionId is always 0, because transaction is implicitly committed
return new DDLStatementExecutionResult(transactionContext.transactionId);
} finally {
if (lockAcquired) {
releaseWriteLock(context.getTableSpaceLock(), statement);
context.setTableSpaceLock(0);
}
}
}
use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class TruncateTableSQLTest method truncateTableNoTransaction.
@Test
public void truncateTableNoTransaction() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "CREATE BRIN INDEX test1 ON tblspace1.tsql (k1)", Collections.emptyList());
execute(manager, "CREATE HASH INDEX test2 ON tblspace1.tsql (k1)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
} catch (TableDoesNotExistException ok) {
}
execute(manager, "TRUNCATE TABLE tblspace1.tsql", Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
assertEquals(0, scan.consume().size());
} catch (TableDoesNotExistException ok) {
}
}
}
use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class TruncateTableSQLTest method truncateTableFileDataStore.
@Test
public void truncateTableFileDataStore() throws Exception {
String nodeId = "localhost";
Path dataPath = folder.newFolder("data").toPath();
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new FileDataStorageManager(dataPath), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int not nul,s1 string)", Collections.emptyList());
execute(manager, "CREATE INDEX test1 ON tblspace1.tsql (n1)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1) values('a',1)", Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
} catch (TableDoesNotExistException ok) {
}
// truncate, table name non case sensitive
execute(manager, "TRUNCATE TABLE tblspace1.Tsql", Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
assertEquals(0, scan.consume().size());
} catch (TableDoesNotExistException ok) {
}
}
}
use of herddb.model.TableDoesNotExistException in project herddb by diennea.
the class SimpleRecoveryTest method rollbackCreateTableOnRestart.
@Test
public void rollbackCreateTableOnRestart() throws Exception {
Path dataPath = folder.newFolder("data").toPath();
Path logsPath = folder.newFolder("logs").toPath();
Path metadataPath = folder.newFolder("metadata").toPath();
Bytes key = Bytes.from_int(1234);
Bytes value = Bytes.from_long(8888);
Path tmoDir = folder.newFolder("tmoDir").toPath();
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
CreateTableStatement st2 = new CreateTableStatement(table);
manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.checkpoint();
Table transacted_table = Table.builder().tablespace("tblspace1").name("t2").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
long tx = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
CreateTableStatement st_create = new CreateTableStatement(transacted_table);
manager.executeStatement(st_create, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
InsertStatement insert = new InsertStatement("tblspace1", "t2", new Record(key, value));
assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
GetStatement get = new GetStatement("tblspace1", "t2", key, null, false);
GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
assertTrue(result.found());
assertEquals(key, result.getRecord().key);
assertEquals(value, result.getRecord().value);
// transaction is not committed, and the leader dies, the table will disappear
}
try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
manager.waitForTablespace("tblspace1", 10000);
GetStatement get = new GetStatement("tblspace1", "t2", key, null, false);
try {
manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
fail();
} catch (TableDoesNotExistException error) {
}
}
}
Aggregations