Search in sources :

Example 56 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class BetterExecuteSyntaxTest method betterSyntax.

@Test
public void betterSyntax() throws Exception {
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        execute(manager, "CREATE TABLESPACE 'tblspace1'", Collections.emptyList());
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "ALTER TABLESPACE 'TBLSPACE1','expectedreplicacount:2'", Collections.emptyList());
        long tx = ((TransactionResult) execute(manager, "BEGIN TRANSACTION 'tblspace1'", Collections.emptyList())).getTransactionId();
        execute(manager, "COMMIT TRANSACTION 'tblspace1'," + tx, Collections.emptyList());
        long tx2 = ((TransactionResult) execute(manager, "BEGIN TRANSACTION 'tblspace1'", Collections.emptyList())).getTransactionId();
        execute(manager, "ROLLBACK TRANSACTION 'tblspace1'," + tx2, Collections.emptyList());
        execute(manager, "DROP TABLESPACE 'tblspace1'", Collections.emptyList());
        try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM systablespaces WHERE tablespace_name=?", Arrays.asList("tblspace1"))) {
            DataAccessor first = scan.consume().get(0);
            Number count = (Number) first.get(first.getFieldNames()[0]);
            assertEquals(0, count.intValue());
        }
    }
}
Also used : TransactionResult(herddb.model.TransactionResult) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 57 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class CalcitePlannerTest method zeroCopyProjectionTest.

@Test
public void zeroCopyProjectionTest() 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," + "s1 string)", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.tsql (k1 ," + "s1) values('testk1','tests1')", Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList())) {
            List<DataAccessor> consume = scan.consume();
            assertEquals(1, consume.size());
            DataAccessor record = consume.get(0);
            assertThat(record, instanceOf(DataAccessorForFullRecord.class));
            assertEquals("k1", record.getFieldNames()[0]);
            assertEquals("s1", record.getFieldNames()[1]);
            assertEquals(RawString.of("testk1"), record.get(0));
            assertEquals(RawString.of("tests1"), record.get(1));
            assertEquals(RawString.of("testk1"), record.get("k1"));
            assertEquals(RawString.of("tests1"), record.get("s1"));
        }
        try (DataScanner scan = scan(manager, "SELECT k1,s1 FROM tblspace1.tsql", Collections.emptyList())) {
            List<DataAccessor> consume = scan.consume();
            assertEquals(1, consume.size());
            DataAccessor record = consume.get(0);
            assertThat(record, instanceOf(DataAccessorForFullRecord.class));
            assertEquals("k1", record.getFieldNames()[0]);
            assertEquals("s1", record.getFieldNames()[1]);
            assertEquals(RawString.of("testk1"), record.get(0));
            assertEquals(RawString.of("tests1"), record.get(1));
            assertEquals(RawString.of("testk1"), record.get("k1"));
            assertEquals(RawString.of("tests1"), record.get("s1"));
        }
        try (DataScanner scan = scan(manager, "SELECT s1,k1 FROM tblspace1.tsql", Collections.emptyList())) {
            List<DataAccessor> consume = scan.consume();
            assertEquals(1, consume.size());
            DataAccessor record = consume.get(0);
            assertThat(record, instanceOf(ZeroCopyProjection.RuntimeProjectedDataAccessor.class));
            assertEquals("k1", record.getFieldNames()[1]);
            assertEquals("s1", record.getFieldNames()[0]);
            assertEquals(RawString.of("testk1"), record.get(1));
            assertEquals(RawString.of("tests1"), record.get(0));
            assertEquals(RawString.of("testk1"), record.get("k1"));
            assertEquals(RawString.of("tests1"), record.get("s1"));
        }
        try (DataScanner scan = scan(manager, "SELECT s1 FROM tblspace1.tsql", Collections.emptyList())) {
            List<DataAccessor> consume = scan.consume();
            assertEquals(1, consume.size());
            DataAccessor record = consume.get(0);
            assertThat(record, instanceOf(RuntimeProjectedDataAccessor.class));
            assertEquals("s1", record.getFieldNames()[0]);
            assertEquals(RawString.of("tests1"), record.get(0));
            assertEquals(RawString.of("tests1"), record.get("s1"));
        }
        try (DataScanner scan = scan(manager, "SELECT k1 FROM tblspace1.tsql", Collections.emptyList())) {
            List<DataAccessor> consume = scan.consume();
            assertEquals(1, consume.size());
            DataAccessor record = consume.get(0);
            assertThat(record, instanceOf(RuntimeProjectedDataAccessor.class));
            assertEquals("k1", record.getFieldNames()[0]);
            assertEquals(RawString.of("testk1"), record.get(0));
            assertEquals(RawString.of("testk1"), record.get("k1"));
        }
    }
}
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) DataAccessorForFullRecord(herddb.codec.DataAccessorForFullRecord) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 58 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class CalcitePlannerTest method showCreateTableTest.

@Test
public void showCreateTableTest() 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.test (k1 string primary key," + "s1 string not null default 'mydefault')", Collections.emptyList());
        Table table = manager.getTableSpaceManager("tblspace1").getTableManager("test").getTable();
        Column c = table.getColumn("s1");
        assertEquals(ColumnTypes.NOTNULL_STRING, c.type);
        assertEquals("mydefault", c.defaultValue.to_string());
        execute(manager, "CREATE TABLE tblspace1.test22 (k1 string not null ," + "s1 string not null, n1 int, primary key(k1,n1))", Collections.emptyList());
        TranslatedQuery translatedQuery = manager.getPlanner().translate("tblspace1", "SHOW CREATE TABLE tblspace1.test", Collections.emptyList(), true, false, true, -1);
        if (translatedQuery.plan.mainStatement instanceof SQLPlannedOperationStatement || translatedQuery.plan.mainStatement instanceof ScanStatement) {
            ScanResult scanResult = (ScanResult) manager.executePlan(translatedQuery.plan, translatedQuery.context, TransactionContext.NO_TRANSACTION);
            DataScanner dataScanner = scanResult.dataScanner;
            String[] columns = dataScanner.getFieldNames();
            List<DataAccessor> records = dataScanner.consume(2);
            TuplesList tuplesList = new TuplesList(columns, records);
            assertTrue(tuplesList.columnNames[0].equalsIgnoreCase("tabledef"));
            Tuple values = (Tuple) records.get(0);
            assertTrue("CREATE TABLE tblspace1.test(k1 string,s1 string not null DEFAULT 'mydefault',PRIMARY KEY(k1))".equalsIgnoreCase(values.get("tabledef").toString()));
        }
        translatedQuery = manager.getPlanner().translate("tblspace1", "SHOW CREATE TABLE tblspace1.test22", Collections.emptyList(), true, false, true, -1);
        if (translatedQuery.plan.mainStatement instanceof SQLPlannedOperationStatement || translatedQuery.plan.mainStatement instanceof ScanStatement) {
            ScanResult scanResult = (ScanResult) manager.executePlan(translatedQuery.plan, translatedQuery.context, TransactionContext.NO_TRANSACTION);
            DataScanner dataScanner = scanResult.dataScanner;
            String[] columns = dataScanner.getFieldNames();
            List<DataAccessor> records = dataScanner.consume(2);
            TuplesList tuplesList = new TuplesList(columns, records);
            assertTrue(tuplesList.columnNames[0].equalsIgnoreCase("tabledef"));
            Tuple values = (Tuple) records.get(0);
            assertTrue("CREATE TABLE tblspace1.test22(k1 string not null,s1 string not null,n1 integer,Primary key(k1,n1))".equalsIgnoreCase(values.get("tabledef").toString()));
        }
    }
}
Also used : ScanResult(herddb.model.ScanResult) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) DataAccessor(herddb.utils.DataAccessor) RawString(herddb.utils.RawString) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) Column(herddb.model.Column) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Tuple(herddb.model.Tuple) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) TuplesList(herddb.utils.TuplesList) Test(org.junit.Test)

Example 59 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class CalcitePlannerTest method showCreateTableTest_with_Indexes.

@Test
public void showCreateTableTest_with_Indexes() 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.test23 (k1 string not null ," + "s1 string not null, n1 int, primary key(k1,n1))", Collections.emptyList());
        execute(manager, "CREATE INDEX ixn1s1 on tblspace1.test23(n1,s1)", Collections.emptyList());
        execute(manager, "CREATE UNIQUE INDEX ixn1s2 on tblspace1.test23(n1,s1,k1)", Collections.emptyList());
        TranslatedQuery translatedQuery = manager.getPlanner().translate("tblspace1", "SHOW CREATE TABLE tblspace1.test23 WITH INDEXES", Collections.emptyList(), true, false, true, -1);
        assertTrue(translatedQuery.plan.mainStatement instanceof SQLPlannedOperationStatement || translatedQuery.plan.mainStatement instanceof ScanStatement);
        ScanResult scanResult = (ScanResult) manager.executePlan(translatedQuery.plan, translatedQuery.context, TransactionContext.NO_TRANSACTION);
        DataScanner dataScanner = scanResult.dataScanner;
        String[] columns = dataScanner.getFieldNames();
        List<DataAccessor> records = dataScanner.consumeAndClose();
        TuplesList tuplesList = new TuplesList(columns, records);
        assertTrue(tuplesList.columnNames[0].equalsIgnoreCase("tabledef"));
        Tuple values = (Tuple) records.get(0);
        assertEquals("CREATE TABLE tblspace1.test23(k1 string not null,s1 string not null,n1 integer,PRIMARY KEY(k1,n1),INDEX ixn1s1(n1,s1),UNIQUE KEY ixn1s2 (n1,s1,k1))", values.get("tabledef").toString());
        // Drop the table and indexes and recreate them again.
        String showCreateCommandOutput = values.get("tabledef").toString();
        // drop the table
        execute(manager, "DROP TABLE tblspace1.test23", Collections.emptyList());
        // ensure table has been dropped
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='test23'", Collections.emptyList())) {
            assertTrue(scan.consume().isEmpty());
        }
        // ensure index has been dropped
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.sysindexes where index_name='ixn1s1'", Collections.emptyList())) {
            assertTrue(scan.consume().isEmpty());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.sysindexes where index_name='ixn1s2'", Collections.emptyList())) {
            assertTrue(scan.consume().isEmpty());
        }
        execute(manager, showCreateCommandOutput, Collections.emptyList());
        // Ensure the table is getting created
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='test23'", Collections.emptyList())) {
            assertFalse(scan.consume().isEmpty());
        }
        // Ensure index got created as well.
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.sysindexes where index_name='ixn1s1'", Collections.emptyList())) {
            assertFalse(scan.consume().isEmpty());
        }
    }
}
Also used : ScanResult(herddb.model.ScanResult) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) DataAccessor(herddb.utils.DataAccessor) RawString(herddb.utils.RawString) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Tuple(herddb.model.Tuple) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) TuplesList(herddb.utils.TuplesList) Test(org.junit.Test)

Example 60 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class SimpleScanZeroCopyTest method testMultiPkFirst.

@Test
public void testMultiPkFirst() 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 primary key,k1 string primary key, s1 string)", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.tsql (k1,n1 ,s1) values (?,?,?)", Arrays.asList("a", 1, "b"));
        try (DataScanner scan = TestUtils.scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> data = scan.consume();
            assertEquals(1, data.size());
            // read from the full record
            assertTrue(data.get(0) instanceof DataAccessorForFullRecord);
            assertEquals(1, data.get(0).get("n1"));
            assertEquals(1, data.get(0).get(0));
            assertEquals(RawString.of("a"), data.get(0).get("k1"));
            assertEquals(RawString.of("a"), data.get(0).get(1));
            assertEquals(RawString.of("b"), data.get(0).get("s1"));
            assertEquals(RawString.of("b"), data.get(0).get(2));
            String[] fieldNames = scan.getFieldNames();
            for (String fName : fieldNames) {
                Object value = data.get(0).get(fName);
                System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
            }
            IntHolder current = new IntHolder();
            data.get(0).forEach((fName, value) -> {
                System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
                assertEquals(fName, fieldNames[current.value++]);
            });
        }
        try (DataScanner scan = TestUtils.scan(manager, "SELECT k1 FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> data = scan.consume();
            assertEquals(1, data.size());
            // read from the full record, keeping only some field
            assertTrue(data.get(0) instanceof ProjectedDataAccessor || data.get(0) instanceof ZeroCopyProjection.RuntimeProjectedDataAccessor);
            assertEquals(RawString.of("a"), data.get(0).get("k1"));
            assertEquals(RawString.of("a"), data.get(0).get(0));
            String[] fieldNames = scan.getFieldNames();
            for (String fName : fieldNames) {
                Object value = data.get(0).get(fName);
                System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
            }
            IntHolder current = new IntHolder();
            data.get(0).forEach((fName, value) -> {
                System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
                assertEquals(fName, fieldNames[current.value++]);
            });
        }
        try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> data = scan.consume();
            assertEquals(1, data.size());
            assertTrue(data.get(0) instanceof Tuple);
        }
    }
}
Also used : MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) ProjectedDataAccessor(herddb.utils.ProjectedDataAccessor) RawString(herddb.utils.RawString) ProjectedDataAccessor(herddb.utils.ProjectedDataAccessor) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) IntHolder(herddb.utils.IntHolder) DataAccessorForFullRecord(herddb.codec.DataAccessorForFullRecord) Tuple(herddb.model.Tuple) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

DataAccessor (herddb.utils.DataAccessor)164 DataScanner (herddb.model.DataScanner)118 Test (org.junit.Test)110 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)83 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)82 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)82 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)76 RawString (herddb.utils.RawString)70 DBManager (herddb.core.DBManager)52 TransactionContext (herddb.model.TransactionContext)44 StatementExecutionException (herddb.model.StatementExecutionException)38 Table (herddb.model.Table)34 List (java.util.List)31 DataScannerException (herddb.model.DataScannerException)28 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)27 RuntimeProjectedDataAccessor (herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor)27 Column (herddb.model.Column)22 ScanResult (herddb.model.ScanResult)22 ScanStatement (herddb.model.commands.ScanStatement)22 Bytes (herddb.utils.Bytes)22