Search in sources :

Example 46 with TransactionContext

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

the class SimpleTransactionTest method testInsertInsert.

@Test
public void testInsertInsert() 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();
    {
        Record record = new Record(key, Bytes.from_int(1));
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        try {
            manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount();
            fail();
        } catch (DuplicatePrimaryKeyException expected) {
        }
    }
}
Also used : Bytes(herddb.utils.Bytes) TransactionContext(herddb.model.TransactionContext) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 47 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 48 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 49 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(tableSpace).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(tableSpace, "t2", new Record(key, value));
    assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    GetStatement get = new GetStatement(tableSpace, "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(tableSpace, tx);
    manager.executeStatement(rollback, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    try {
        manager.get(new GetStatement(tableSpace, "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 50 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)

Aggregations

TransactionContext (herddb.model.TransactionContext)126 Test (org.junit.Test)86 Table (herddb.model.Table)74 Bytes (herddb.utils.Bytes)62 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)60 InsertStatement (herddb.model.commands.InsertStatement)60 CreateTableStatement (herddb.model.commands.CreateTableStatement)57 DataScanner (herddb.model.DataScanner)54 Record (herddb.model.Record)52 GetResult (herddb.model.GetResult)44 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)44 StatementExecutionException (herddb.model.StatementExecutionException)41 GetStatement (herddb.model.commands.GetStatement)41 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)39 Path (java.nio.file.Path)38 TransactionResult (herddb.model.TransactionResult)36 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)35 ScanStatement (herddb.model.commands.ScanStatement)32 DataAccessor (herddb.utils.DataAccessor)32 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)30