Search in sources :

Example 36 with GetResult

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

the class SimpleFollowerTest method testForLastConfirmedBackground.

@Test
public void testForLastConfirmedBackground() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    // send a dummy write after 1 seconds of inactivity
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_MAX_IDLE_TIME, 1000);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        DMLStatementExecutionResult executeUpdateTransaction = server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 5)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
        server_1.getManager().executeStatement(new CommitTransactionStatement(TableSpace.DEFAULT, executeUpdateTransaction.transactionId), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
            // wait for data to arrive on server_2
            for (int i = 0; i < 100; i++) {
                GetResult found = server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(5), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
                if (found.found()) {
                    break;
                }
                Thread.sleep(100);
            }
            for (int i = 1; i <= 5; i++) {
                System.out.println("checking key c=" + i);
                assertTrue(server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(i), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).found());
            }
        }
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) Server(herddb.server.Server) GetResult(herddb.model.GetResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) GetStatement(herddb.model.commands.GetStatement) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) InsertStatement(herddb.model.commands.InsertStatement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 37 with GetResult

use of herddb.model.GetResult 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)

Example 38 with GetResult

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

the class SimpleTransactionTest method testRollbackUpdate1.

@Test
public void testRollbackUpdate1() 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));
    UpdateStatement st_update = new UpdateStatement(tableSpace, tableName, record2, null);
    assertEquals(1, manager.executeUpdate(st_update, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    rollbackTransaction(tx);
    GetResult get = manager.get(new GetStatement(tableSpace, tableName, key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    assertTrue(get.found());
    assertEquals(get.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) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 39 with GetResult

use of herddb.model.GetResult 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 40 with GetResult

use of herddb.model.GetResult 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)

Aggregations

GetResult (herddb.model.GetResult)96 GetStatement (herddb.model.commands.GetStatement)91 Test (org.junit.Test)85 InsertStatement (herddb.model.commands.InsertStatement)84 Table (herddb.model.Table)70 CreateTableStatement (herddb.model.commands.CreateTableStatement)68 Record (herddb.model.Record)58 Bytes (herddb.utils.Bytes)49 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)46 Path (java.nio.file.Path)43 TransactionContext (herddb.model.TransactionContext)41 TransactionResult (herddb.model.TransactionResult)30 FileCommitLogManager (herddb.file.FileCommitLogManager)29 FileDataStorageManager (herddb.file.FileDataStorageManager)29 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)29 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)29 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)28 UpdateStatement (herddb.model.commands.UpdateStatement)24 DeleteStatement (herddb.model.commands.DeleteStatement)21 ScanStatement (herddb.model.commands.ScanStatement)21