Search in sources :

Example 51 with CreateTableSpaceStatement

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

the class AutoIncrementTest method testAutoIncrement_other_sintax.

@Test
public void testAutoIncrement_other_sintax() 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 (n1 int auto_increment, s1 string, primary key (n1))", Collections.emptyList());
        {
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa")).getUpdateCount());
            assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa")).getUpdateCount());
            List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consumeAndClose();
            assertEquals(6, rows.size());
            for (int i = 1; i <= 6; i++) {
                int _i = i;
                assertTrue(rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
            }
            DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"));
            assertEquals(1, result.getUpdateCount());
            assertEquals(Bytes.from_int(7), result.getKey());
            assertEquals(7, executeUpdate(manager, "delete from tblspace1.tsql", Collections.emptyList()).getUpdateCount());
        }
        // the same but in transaction
        {
            DMLStatementExecutionResult res = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), TransactionContext.AUTOTRANSACTION_TRANSACTION);
            assertEquals(1, res.getUpdateCount());
            long tx = res.transactionId;
            assertTrue(tx > 0);
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
            assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), new TransactionContext(tx)).getUpdateCount());
            List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).consumeAndClose();
            assertEquals(6, rows.size());
            for (int i = 8; i <= 13; i++) {
                int _i = i;
                assertTrue("can't find " + i, rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
            }
            DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx));
            assertEquals(1, result.getUpdateCount());
            assertEquals(Bytes.from_int(14), result.getKey());
            assertEquals(tx, res.transactionId);
        // transaction not committed
        }
        // the same but in transaction, multi values as first
        {
            DMLStatementExecutionResult res = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), TransactionContext.AUTOTRANSACTION_TRANSACTION);
            assertEquals(2, res.getUpdateCount());
            long tx = res.transactionId;
            assertTrue(tx > 0);
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx)).getUpdateCount());
            assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?),(?)", Arrays.asList("aa", "aa"), new TransactionContext(tx)).getUpdateCount());
            List<DataAccessor> rows = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).consumeAndClose();
            assertEquals(6, rows.size());
            for (int i = 15; i <= 20; i++) {
                int _i = i;
                assertTrue(rows.stream().filter(t -> t.get("n1").equals(Integer.valueOf(_i))).findAny().isPresent());
            }
            DMLStatementExecutionResult result = executeUpdate(manager, "INSERT INTO tblspace1.tsql(s1) values(?)", Arrays.asList("aa"), new TransactionContext(tx));
            assertEquals(1, result.getUpdateCount());
            assertEquals(Bytes.from_int(21), result.getKey());
            assertEquals(tx, res.transactionId);
            assertEquals(7, executeUpdate(manager, "delete from tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).getUpdateCount());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 52 with CreateTableSpaceStatement

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

the class AutocheckPointTest method autoCheckPointTest.

@Test
public void autoCheckPointTest() 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();
    String nodeId = "localhost";
    try (DBManager manager = new DBManager(nodeId, 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(), NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.tsql (K1 int ,s1 string,n1 int, primary key(k1))", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(1, "a", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(2, "a", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(3, "a", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList(4, "a", Integer.valueOf(1234))).getUpdateCount());
        manager.checkpoint();
        long tx = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
        execute(manager, "UPDATE tblspace1.tsql set s1='b' where k1=1", Collections.emptyList(), new TransactionContext(tx));
        long lastCheckpont = manager.getLastCheckPointTs();
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE N1=1234", Collections.emptyList(), new TransactionContext(tx))) {
            List<DataAccessor> data = scan.consume();
            assertEquals(4, data.size());
        }
        manager.setCheckpointPeriod(1000);
        for (int i = 0; i < 100; i++) {
            if (lastCheckpont != manager.getLastCheckPointTs()) {
                break;
            }
            Thread.sleep(100);
        }
        assertNotEquals(lastCheckpont, manager.getLastCheckPointTs());
        execute(manager, "EXECUTE committransaction 'tblspace1'," + tx, Collections.emptyList());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql WHERE N1=1234 and s1='b'", Collections.emptyList()).consumeAndClose().size());
    }
}
Also used : Path(java.nio.file.Path) TransactionResult(herddb.model.TransactionResult) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) DataAccessor(herddb.utils.DataAccessor) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) TransactionContext(herddb.model.TransactionContext) FileDataStorageManager(herddb.file.FileDataStorageManager) Test(org.junit.Test)

Example 53 with CreateTableSpaceStatement

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

the class CreateTableNoDefinitionTest method createTable1.

@Test
public void createTable1() throws Exception {
    exceptionRule.expect(StatementExecutionException.class);
    exceptionRule.expectMessage("A table must have at least 1 column");
    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 test", Collections.emptyList());
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 54 with CreateTableSpaceStatement

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

the class DeleteTest method deleteThenInsert.

/**
 * Check delete then insert (#320, not every row delete really was run).
 *
 * @author diego.salvi
 */
@Test
public void deleteThenInsert() throws Exception {
    int runs = 50;
    int inserts = 100;
    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(), NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        for (int run = 0; run < runs; ++run) {
            execute(manager, "CREATE TABLE tblspace1.tsql (k1 int, n1 int, primary key(k1))", Collections.emptyList());
            for (int i = 0; i < inserts; ++i) {
                assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList(Integer.valueOf(i), Integer.valueOf(1))).getUpdateCount());
            }
            long tx = beginTransaction(manager, "tblspace1");
            TransactionContext ctx = new TransactionContext(tx);
            executeUpdate(manager, "DELETE FROM tblspace1.tsql", Collections.emptyList(), ctx);
            for (int i = 0; i < inserts; ++i) {
                assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList(Integer.valueOf(i), Integer.valueOf(1)), ctx).getUpdateCount());
            }
            commitTransaction(manager, "tblspace1", tx);
            for (int i = 0; i < inserts; ++i) {
                assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql WHERE k1 = ?", Arrays.asList(Integer.valueOf(i), ctx)).consumeAndClose().size());
            }
            execute(manager, "DROP TABLE tblspace1.tsql", Collections.emptyList());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) TransactionContext(herddb.model.TransactionContext) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 55 with CreateTableSpaceStatement

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

the class DropTableSQLTest method dropTableNoTransaction.

@Test
public void dropTableNoTransaction() 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());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        execute(manager, "DROP TABLE tblspace1.tsql", Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            assertTrue(scan.consume().isEmpty());
        }
        execute(manager, "CREATE TABLE tsql2 (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM systables where table_name='tsql2'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        // name is not case sensitive
        execute(manager, "DROP TABLE tSQL2", Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM systables where table_name='tsql2'", Collections.emptyList())) {
            assertTrue(scan.consume().isEmpty());
        }
        herddb.utils.TestUtils.assertThrows(StatementExecutionException.class, () -> {
            execute(manager, "DROP TABLE tSQL2", Collections.emptyList());
        });
        execute(manager, "DROP TABLE IF EXISTS tSQL2", Collections.emptyList());
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)308 Test (org.junit.Test)295 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)241 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)240 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)236 DataScanner (herddb.model.DataScanner)164 RawString (herddb.utils.RawString)139 Table (herddb.model.Table)132 DBManager (herddb.core.DBManager)126 CreateTableStatement (herddb.model.commands.CreateTableStatement)111 ScanStatement (herddb.model.commands.ScanStatement)78 DataAccessor (herddb.utils.DataAccessor)77 TransactionContext (herddb.model.TransactionContext)69 TranslatedQuery (herddb.sql.TranslatedQuery)69 Path (java.nio.file.Path)67 Bytes (herddb.utils.Bytes)54 List (java.util.List)54 Index (herddb.model.Index)53 GetStatement (herddb.model.commands.GetStatement)53 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)51