Search in sources :

Example 6 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class RawSQLTest method updateTest.

@Test
public void updateTest() 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());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,s1) values(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), "value1")).getUpdateCount());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where s1='value1'", Collections.emptyList()).consumeAndClose().size());
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set s1=k1  where k1=?", Arrays.asList("mykey")).getUpdateCount());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where s1='mykey'", Collections.emptyList()).consumeAndClose().size());
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 7 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class RawSQLTest method simpleCountTest.

@Test
public void simpleCountTest() 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());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
        {
            try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc FROM tblspace1.tsql", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(1, result.size());
                assertEquals(Long.valueOf(4), result.get(0).get(0));
                assertEquals(Long.valueOf(4), result.get(0).get("cc"));
            }
        }
        try (DataScanner scan1 = scan(manager, "SELECT COUNT(*)  FROM tblspace1.tsql", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(1, result.size());
            assertEquals(Long.valueOf(4), result.get(0).get(0));
            assertEquals(Long.valueOf(4), result.get(0).get("expr$0"));
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc FROM tblspace1.tsql WHERE k1='mykey3'", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(1, result.size());
                assertEquals(Long.valueOf(1), result.get(0).get(0));
                assertEquals(Long.valueOf(1), result.get(0).get("cc"));
            }
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT COUNT(*),k1 FROM tblspace1.tsql", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                Assert.fail();
            } catch (StatementExecutionException error) {
                assertTrue("field k1 MUST appear in GROUP BY clause".equals(error.getMessage()) || error.getMessage().equals("From line 1, column 17 to line 1, column 18: Expression 'K1' is not being grouped"));
            }
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,k1 FROM tblspace1.tsql GROUP BY k1", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(4, result.size());
                for (DataAccessor t : result) {
                    assertEquals(Long.valueOf(1), t.get("cc"));
                    switch(t.get("k1") + "") {
                        case "mykey":
                        case "mykey2":
                        case "mykey3":
                        case "mykey4":
                            break;
                        default:
                            fail();
                    }
                }
            }
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 8 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class RawSQLTest method simpleExitLoopTest.

@Test
public void simpleExitLoopTest() 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());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
        {
            try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 1", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(1, result.size());
            }
            try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 2", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(2, result.size());
            }
            try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 10", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(4, result.size());
            }
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 9 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class RawSQLTest method testDMLOutOfOrderThanCreateTable.

@Test
public void testDMLOutOfOrderThanCreateTable() 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.IP (\n" + "   field0             INT          NOT NULL PRIMARY KEY,\n" + "   field1          VARCHAR(15)  NOT NULL,\n" + "   field2         VARCHAR(255) NOT NULL,\n" + "   field3           INT          NOT NULL,\n" + "   field4           INT          NOT NULL,\n" + "   field5            NCLOB        /* NOT NULL */\n" + ")", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.IP (field0,field1,field3,field4,field2,field5) values(?,?,?,?,?,?)", Arrays.asList(1, "1.2.3.4", 2, 5, "localhost", "aaa")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.IP set field3=?, field2=?, field4=?, field5=? WHERE field0=?", Arrays.asList(1, "localhost", 8, "aaa", 1)).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.IP set field3=?, field4=?, field2=?, field5=? WHERE field0=?", Arrays.asList(1, 8, "localhost", "aaa", 1)).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT field5,field4,field2,field3 FROM tblspace1.IP where field0=?", Arrays.asList(1))) {
            while (scan.hasNext()) {
                DataAccessor next = scan.next();
                assertThat(next, instanceOf(RuntimeProjectedDataAccessor.class));
                assertArrayEquals(new String[] { "field5", "field4", "field2", "field3" }, next.getFieldNames());
                assertArrayEquals(new Object[] { RawString.of("aaa"), 8, RawString.of("localhost"), 1 }, next.getValues());
            }
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 10 with DBManager

use of herddb.core.DBManager in project herddb by diennea.

the class RawSQLTest method caseWhenTest.

@Test
public void caseWhenTest() 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,t1 timestamp)", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey2", Integer.valueOf(1235))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey3", Integer.valueOf(1236))).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT k1, " + "CASE " + "WHEN k1='mykey'  THEN 'a' " + "WHEN k1='mykey2' THEN 'b' " + "ELSE 'c'  " + "END as mycase " + "FROM tblspace1.tsql " + "ORDER BY k1", Collections.emptyList())) {
            List<DataAccessor> res = scan.consume();
            for (DataAccessor t : res) {
                System.out.println("t:" + t);
            }
            assertEquals(3, res.size());
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mycase", "a"))));
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mycase", "b"))));
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mycase", "c"))));
        }
        try (DataScanner scan = scan(manager, "SELECT k1, " + "CASE " + "WHEN k1='mykey'  THEN 'a' " + "WHEN k1='mykey2' THEN 'b' " + "END as mycase " + "FROM tblspace1.tsql " + "ORDER BY k1", Collections.emptyList())) {
            List<DataAccessor> res = scan.consume();
            for (DataAccessor t : res) {
                System.out.println("t:" + t);
            }
            assertEquals(3, res.size());
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mycase", "a"))));
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mycase", "b"))));
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mycase", null))));
        }
        if (manager.isFullSQLSupportEnabled()) {
            try (DataScanner scan = scan(manager, "SELECT k1, " + "SUM(CASE " + "WHEN k1='mykey'  THEN 1 " + "WHEN k1='mykey2' THEN 2 " + "ELSE 3  " + "END) as mysum " + "FROM tblspace1.tsql " + "GROUP BY k1", Collections.emptyList())) {
                List<DataAccessor> res = scan.consume();
                for (DataAccessor t : res) {
                    System.out.println("t2:" + t);
                }
                assertEquals(3, res.size());
                assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mysum", 1L))));
                assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mysum", 2L))));
                assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mysum", 3L))));
            }
            try (DataScanner scan = scan(manager, "SELECT " + "SUM(CASE " + "WHEN k1='mykey'  THEN 1 " + "WHEN k1='mykey2' THEN 2 " + "ELSE 3  " + "END) as mysum " + "FROM tblspace1.tsql " + "", Collections.emptyList())) {
                List<DataAccessor> res = scan.consume();
                for (DataAccessor t : res) {
                    System.out.println("t:" + t);
                }
                assertEquals(1, res.size());
                assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("mysum", 6L))));
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) Date(java.util.Date) Assert.assertNotSame(org.junit.Assert.assertNotSame) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) Table(herddb.model.Table) TransactionResult(herddb.model.TransactionResult) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) TestUtils.scan(herddb.core.TestUtils.scan) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) MapUtils(herddb.utils.MapUtils) RawString(herddb.utils.RawString) Index(herddb.model.Index) ScanResult(herddb.model.ScanResult) DataAccessor(herddb.utils.DataAccessor) DataAccessorForFullRecord(herddb.codec.DataAccessorForFullRecord) TableDoesNotExistException(herddb.model.TableDoesNotExistException) DataScanner(herddb.model.DataScanner) StatementExecutionException(herddb.model.StatementExecutionException) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) RecordSerializer(herddb.codec.RecordSerializer) ScanStatement(herddb.model.commands.ScanStatement) List(java.util.List) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) Assert.assertFalse(org.junit.Assert.assertFalse) GetStatement(herddb.model.commands.GetStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) Assume.assumeTrue(org.junit.Assume.assumeTrue) TableSpace(herddb.model.TableSpace) Bytes(herddb.utils.Bytes) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) GetResult(herddb.model.GetResult) SimpleDateFormat(java.text.SimpleDateFormat) PrimaryIndexSeek(herddb.index.PrimaryIndexSeek) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Assert.assertSame(org.junit.Assert.assertSame) TestUtils(herddb.core.TestUtils) Calendar(java.util.Calendar) TransactionContext(herddb.model.TransactionContext) Transaction(herddb.model.Transaction) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestUtils.scanKeepReadLocks(herddb.core.TestUtils.scanKeepReadLocks) TestUtils.commitTransaction(herddb.core.TestUtils.commitTransaction) IndexDoesNotExistException(herddb.model.IndexDoesNotExistException) TestUtils.executeUpdate(herddb.core.TestUtils.executeUpdate) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) DBManager(herddb.core.DBManager) Assert.assertNull(org.junit.Assert.assertNull) StatementEvaluationContext(herddb.model.StatementEvaluationContext) AbstractIndexManager(herddb.core.AbstractIndexManager) TestUtils.execute(herddb.core.TestUtils.execute) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

DBManager (herddb.core.DBManager)153 Test (org.junit.Test)143 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)133 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)133 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)132 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)126 DataScanner (herddb.model.DataScanner)92 RawString (herddb.utils.RawString)82 Table (herddb.model.Table)53 DataAccessor (herddb.utils.DataAccessor)53 CreateTableStatement (herddb.model.commands.CreateTableStatement)34 ScanStatement (herddb.model.commands.ScanStatement)34 RuntimeProjectedDataAccessor (herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor)28 List (java.util.List)28 TransactionContext (herddb.model.TransactionContext)27 Index (herddb.model.Index)25 TranslatedQuery (herddb.sql.TranslatedQuery)23 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)21 SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)18 Timestamp (java.sql.Timestamp)15