Search in sources :

Example 16 with BeginTransactionStatement

use of herddb.model.commands.BeginTransactionStatement in project herddb by diennea.

the class ScanTest method testTransaction.

@Test
public void testTransaction() throws Exception {
    for (int i = 0; i < 5; i++) {
        Map<String, Object> data = new HashMap<>();
        data.put("id", "key_" + i);
        data.put("number", i);
        Record record = RecordSerializer.toRecord(data, table);
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    BeginTransactionStatement begin = new BeginTransactionStatement(tableSpace);
    long tx = ((TransactionResult) manager.executeStatement(begin, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
    for (int i = 5; i < 10; i++) {
        Map<String, Object> data = new HashMap<>();
        data.put("id", "key_" + i);
        data.put("number", i);
        Record record = RecordSerializer.toRecord(data, table);
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    {
        ScanStatement scan = new ScanStatement(tableSpace, table, new Predicate() {

            @Override
            public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
                return true;
            }
        });
        DataScanner scanner = manager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        List<?> result = scanner.consumeAndClose();
        assertEquals(10, result.size());
    }
    for (int i = 0; i < 10; i++) {
        DeleteStatement st = new DeleteStatement(tableSpace, tableName, Bytes.from_string("key_" + i), null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    {
        ScanStatement scan = new ScanStatement(tableSpace, table, new Predicate() {

            @Override
            public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
                return true;
            }
        });
        DataScanner scanner = manager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        List<?> result = scanner.consumeAndClose();
        assertEquals(0, result.size());
    }
}
Also used : TransactionResult(herddb.model.TransactionResult) HashMap(java.util.HashMap) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Predicate(herddb.model.Predicate) DataScanner(herddb.model.DataScanner) TransactionContext(herddb.model.TransactionContext) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) Record(herddb.model.Record) List(java.util.List) StatementEvaluationContext(herddb.model.StatementEvaluationContext) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 17 with BeginTransactionStatement

use of herddb.model.commands.BeginTransactionStatement 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;
    }
}
Also used : AlterTableStatement(herddb.model.commands.AlterTableStatement) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) CreateTableStatement(herddb.model.commands.CreateTableStatement) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) TableAwareStatement(herddb.model.TableAwareStatement) StatementExecutionException(herddb.model.StatementExecutionException) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) DropIndexStatement(herddb.model.commands.DropIndexStatement) Transaction(herddb.model.Transaction) TransactionContext(herddb.model.TransactionContext) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) DropTableStatement(herddb.model.commands.DropTableStatement)

Example 18 with BeginTransactionStatement

use of herddb.model.commands.BeginTransactionStatement in project herddb by diennea.

the class RestartPendingTransactionTest method recoverUpdateInTransaction2.

@Test
public void recoverUpdateInTransaction2() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    Bytes key1 = Bytes.from_string("k1");
    Bytes key2 = Bytes.from_string("k2");
    Bytes key3 = Bytes.from_string("k3");
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), 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();
        manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key1, key1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key2, key2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key3, key3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.checkpoint();
        long tx = ((TransactionResult) manager.executeStatement(new BeginTransactionStatement("tblspace1"), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION)).getTransactionId();
        manager.executeStatement(new DeleteStatement("tblspace1", table.name, key2, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key2, key2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        manager.executeStatement(new UpdateStatement("tblspace1", table.name, new ConstValueRecordFunction(key2.data), new ConstValueRecordFunction(key3.data), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        manager.executeStatement(new CommitTransactionStatement("tblspace1", tx), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    // transaction which contains the update will be replayed at reboot
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
        manager.start();
        manager.waitForTablespace("tblspace1", 10000);
        GetResult result = manager.get(new GetStatement("tblspace1", "t1", key1, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
    }
}
Also used : Path(java.nio.file.Path) TransactionResult(herddb.model.TransactionResult) UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Bytes(herddb.utils.Bytes) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) ConstValueRecordFunction(herddb.model.ConstValueRecordFunction) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) Record(herddb.model.Record) Test(org.junit.Test)

Example 19 with BeginTransactionStatement

use of herddb.model.commands.BeginTransactionStatement in project herddb by diennea.

the class RestartTest method recoverTableCreatedInTransaction2.

@Test
public void recoverTableCreatedInTransaction2() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    Bytes key = Bytes.from_string("k1");
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), 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);
        assertTrue(manager.waitForTablespace("tblspace1", 10000));
        Table table = Table.builder().tablespace("tblspace1").name("t1").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();
        manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key, key)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        manager.checkpoint();
        DMLStatementExecutionResult executeStatement = (DMLStatementExecutionResult) manager.executeStatement(new UpdateStatement("tblspace1", "t1", new Record(key, key), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        assertEquals(1, executeStatement.getUpdateCount());
        manager.executeStatement(new CommitTransactionStatement("tblspace1", tx), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        DMLStatementExecutionResult executeStatement = (DMLStatementExecutionResult) manager.executeStatement(new UpdateStatement("tblspace1", "t1", new Record(key, key), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertEquals(1, executeStatement.getUpdateCount());
    // on the log there is an UPDATE for a record which is not on the log
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        GetResult result = manager.get(new GetStatement("tblspace1", "t1", key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
    }
}
Also used : Path(java.nio.file.Path) TransactionResult(herddb.model.TransactionResult) UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) InsertStatement(herddb.model.commands.InsertStatement) Bytes(herddb.utils.Bytes) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) Record(herddb.model.Record) Test(org.junit.Test)

Example 20 with BeginTransactionStatement

use of herddb.model.commands.BeginTransactionStatement in project herddb by diennea.

the class RestartTest method recoverTableCreatedInTransaction.

@Test
public void recoverTableCreatedInTransaction() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    Bytes key = Bytes.from_string("k1");
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), 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);
        assertTrue(manager.waitForTablespace("tblspace1", 10000));
        ;
        Table table = Table.builder().tablespace("tblspace1").name("t1").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();
        manager.executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        manager.executeStatement(new InsertStatement("tblspace1", table.name, new Record(key, key)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
        manager.checkpoint();
        manager.executeStatement(new CommitTransactionStatement("tblspace1", tx), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        DMLStatementExecutionResult executeStatement = (DMLStatementExecutionResult) manager.executeStatement(new UpdateStatement("tblspace1", "t1", new Record(key, key), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertEquals(1, executeStatement.getUpdateCount());
    // on the log there is an UPDATE for a record which is not on the log
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath, 64 * 1024 * 1024), tmoDir, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        GetResult result = manager.get(new GetStatement("tblspace1", "t1", key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
    }
}
Also used : Path(java.nio.file.Path) TransactionResult(herddb.model.TransactionResult) UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) InsertStatement(herddb.model.commands.InsertStatement) Bytes(herddb.utils.Bytes) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) Record(herddb.model.Record) Test(org.junit.Test)

Aggregations

TransactionContext (herddb.model.TransactionContext)33 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)33 TransactionResult (herddb.model.TransactionResult)31 CreateTableStatement (herddb.model.commands.CreateTableStatement)30 Record (herddb.model.Record)29 Table (herddb.model.Table)29 InsertStatement (herddb.model.commands.InsertStatement)29 Bytes (herddb.utils.Bytes)29 Test (org.junit.Test)29 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)28 Path (java.nio.file.Path)28 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)27 GetResult (herddb.model.GetResult)26 GetStatement (herddb.model.commands.GetStatement)26 FileCommitLogManager (herddb.file.FileCommitLogManager)16 FileDataStorageManager (herddb.file.FileDataStorageManager)16 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)16 UpdateStatement (herddb.model.commands.UpdateStatement)14 DeleteStatement (herddb.model.commands.DeleteStatement)8 ConstValueRecordFunction (herddb.model.ConstValueRecordFunction)7