Search in sources :

Example 26 with TransactionContext

use of herddb.model.TransactionContext in project herddb by diennea.

the class SimpleTransactionTest method testRollbackInsertUpdateDelete.

@Test
public void testRollbackInsertUpdateDelete() throws Exception {
    Bytes key = Bytes.from_string("key1");
    Record record = new Record(key, Bytes.from_int(0));
    InsertStatement st_insert = new InsertStatement(tableSpace, tableName, record);
    assertEquals(1, manager.executeUpdate(st_insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    long tx = beginTransaction();
    Record record2 = new Record(key, Bytes.from_int(1));
    // UPDATE
    UpdateStatement st_update = new UpdateStatement(tableSpace, tableName, record2, null);
    assertEquals(1, manager.executeUpdate(st_update, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    // DELETE
    DeleteStatement st_delete = new DeleteStatement(tableSpace, tableName, key, null);
    assertEquals(1, manager.executeUpdate(st_delete, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    rollbackTransaction(tx);
    GetResult get_after_rollback = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    assertTrue(get_after_rollback.found());
    assertEquals(get_after_rollback.getRecord().value, record.value);
}
Also used : Bytes(herddb.utils.Bytes) UpdateStatement(herddb.model.commands.UpdateStatement) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 27 with TransactionContext

use of herddb.model.TransactionContext in project herddb by diennea.

the class SimpleTransactionTest method testInsertThenDelete.

@Test
public void testInsertThenDelete() throws Exception {
    Bytes key = Bytes.from_string("key1");
    long tx = beginTransaction();
    {
        Record record = new Record(key, Bytes.from_int(0));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    {
        DeleteStatement st = new DeleteStatement(tableSpace, tableName, key, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    commitTransaction(tx);
    GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    assertFalse(get.found());
}
Also used : Bytes(herddb.utils.Bytes) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 28 with TransactionContext

use of herddb.model.TransactionContext in project herddb by diennea.

the class SimpleTransactionTest method rollbackCreateTable.

@Test
public void rollbackCreateTable() throws Exception {
    Bytes key = Bytes.from_int(1234);
    Bytes value = Bytes.from_long(8888);
    Table transacted_table = Table.builder().tablespace("tblspace1").name("t2").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
    long tx = beginTransaction();
    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);
    RollbackTransactionStatement rollback = new RollbackTransactionStatement("tblspace1", tx);
    manager.executeStatement(rollback, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    try {
        manager.get(new GetStatement("tblspace1", "t2", key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        fail();
    } catch (TableDoesNotExistException error) {
    }
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) Bytes(herddb.utils.Bytes) Table(herddb.model.Table) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) CreateTableStatement(herddb.model.commands.CreateTableStatement) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 29 with TransactionContext

use of herddb.model.TransactionContext in project herddb by diennea.

the class SimpleTransactionTest method testCommit.

@Test
public void testCommit() throws Exception {
    long tx = beginTransaction();
    Bytes key = Bytes.from_string("key1");
    {
        Record record = new Record(key, Bytes.from_int(0));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    commitTransaction(tx);
    GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    assertTrue(get.found());
}
Also used : Bytes(herddb.utils.Bytes) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 30 with TransactionContext

use of herddb.model.TransactionContext in project herddb by diennea.

the class SimpleTransactionTest method testInsertDelete.

@Test
public void testInsertDelete() throws Exception {
    Bytes key = Bytes.from_string("key1");
    {
        Record record = new Record(key, Bytes.from_int(0));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    long tx = beginTransaction();
    {
        DeleteStatement st = new DeleteStatement(tableSpace, tableName, key, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    {
        Record record = new Record(key, Bytes.from_int(1));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    }
    commitTransaction(tx);
    GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    assertTrue(get.found());
    assertEquals(Bytes.from_int(1), get.getRecord().value);
}
Also used : Bytes(herddb.utils.Bytes) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Aggregations

TransactionContext (herddb.model.TransactionContext)63 Test (org.junit.Test)49 InsertStatement (herddb.model.commands.InsertStatement)39 Bytes (herddb.utils.Bytes)38 Table (herddb.model.Table)35 Record (herddb.model.Record)34 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)32 CreateTableStatement (herddb.model.commands.CreateTableStatement)31 GetResult (herddb.model.GetResult)30 GetStatement (herddb.model.commands.GetStatement)29 Path (java.nio.file.Path)23 DataScanner (herddb.model.DataScanner)22 TransactionResult (herddb.model.TransactionResult)22 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)20 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)20 FileDataStorageManager (herddb.file.FileDataStorageManager)19 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)19 FileCommitLogManager (herddb.file.FileCommitLogManager)18 StatementExecutionException (herddb.model.StatementExecutionException)17 DataAccessor (herddb.utils.DataAccessor)16