Search in sources :

Example 16 with DataAccessor

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

the class SimpleJoinTest method testSubQueryInSelect.

@Test
public void testSubQueryInSelect() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        assumeThat(manager.getPlanner(), instanceOf(CalcitePlanner.class));
        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.table1 (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
        execute(manager, "CREATE TABLE tblspace1.table2 (k2 string primary key,n2 int,s2 string)", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.table1 (k1,n1,s1) values('a',1,'A')", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.table1 (k1,n1,s1) values('b',2,'B')", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.table2 (k2,n2,s2) values('c',3,'A')", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.table2 (k2,n2,s2) values('d',4,'A')", Collections.emptyList());
        {
            List<DataAccessor> tuples = scan(manager, "SELECT t1.k1, max(n1) as maxn1, max(select n2 from tblspace1.table2 t2 WHERE t1.s1=t2.s2) as maxn2 FROM " + " tblspace1.table1 t1 " + " group by k1", Collections.emptyList()).consume();
            for (DataAccessor t : tuples) {
                System.out.println("t:" + t);
                assertEquals(3, t.getFieldNames().length);
                assertEquals("k1", t.getFieldNames()[0]);
                assertEquals("maxn1", t.getFieldNames()[1]);
                assertEquals("maxn2", t.getFieldNames()[2]);
            }
            assertEquals(2, tuples.size());
            assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", RawString.of("a"), "maxn1", 1, "maxn2", 4))));
            assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", RawString.of("b"), "maxn1", 2, "maxn2", null))));
        }
        {
            List<DataAccessor> tuples = scan(manager, "SELECT t1.k1, max(n1) as maxn1, max(select n2 from tblspace1.table2 t2 WHERE t1.s1=t2.s2) as maxn2 FROM " + " tblspace1.table1 t1 " + " group by k1" + " order by max(select n2 from tblspace1.table2 t2 WHERE t1.s1=t2.s2) desc", Collections.emptyList()).consume();
            for (DataAccessor t : tuples) {
                System.out.println("t:" + t);
                assertEquals(3, t.getFieldNames().length);
                assertEquals("k1", t.getFieldNames()[0]);
                assertEquals("maxn1", t.getFieldNames()[1]);
                assertEquals("maxn2", t.getFieldNames()[2]);
            }
            assertEquals(2, tuples.size());
            assertTrue(tuples.get(0).toMap().equals(MapUtils.map("k1", RawString.of("b"), "maxn1", 2, "maxn2", null)));
            assertTrue(tuples.get(1).toMap().equals(MapUtils.map("k1", RawString.of("a"), "maxn1", 1, "maxn2", 4)));
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CalcitePlanner(herddb.sql.CalcitePlanner) List(java.util.List) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 17 with DataAccessor

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

the class MultiServerCreateTableSpaceWaitTest method test_start_table_space_and_wait.

@Test
public void test_start_table_space_and_wait() throws Exception {
    ServerConfiguration serverconfig_1 = new ServerConfiguration(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_PORT, 7867);
    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);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath()).set(ServerConfiguration.PROPERTY_PORT, 7868);
    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();
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            TestUtils.execute(server_1.getManager(), "CREATE TABLESPACE 'ttt','leader:" + server_2.getNodeId() + "','wait:60000'", Collections.emptyList());
            try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM SYSTABLESPACEREPLICASTATE where tablespace_name='ttt' and nodeId='" + server_2.getNodeId() + "'", Collections.emptyList())) {
                List<DataAccessor> tuples = scan.consume();
                // for (Tuple t : tuples) {
                // System.out.println("tuple:" + t);
                // }
                assertEquals(1, tuples.size());
            }
        }
    }
}
Also used : Table(herddb.model.Table) DataScanner(herddb.model.DataScanner) DataAccessor(herddb.utils.DataAccessor) Test(org.junit.Test)

Example 18 with DataAccessor

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

the class TablespaceReplicasStateTest method test_leader_online_log_available.

@Test
public void test_leader_online_log_available() throws Exception {
    ServerConfiguration serverconfig_1 = new ServerConfiguration(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_PORT, 7867);
    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);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath()).set(ServerConfiguration.PROPERTY_PORT, 7868);
    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);
        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", 2, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
            try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate", Collections.emptyList())) {
                List<DataAccessor> results = scan.consume();
                assertEquals(2, results.size());
            }
            try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate " + "where nodeId='" + server_2.getNodeId() + "' and mode='follower'", Collections.emptyList())) {
                assertEquals(1, scan.consume().size());
            }
            try (DataScanner scan = scan(server_2.getManager(), "SELECT * FROM systablespacereplicastate " + "where nodeId='" + server_2.getNodeId() + "' and mode='follower'", Collections.emptyList())) {
                assertEquals(1, scan.consume().size());
            }
            try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate " + "where nodeId='" + server_1.getNodeId() + "' and mode='leader'", Collections.emptyList())) {
                assertEquals(1, scan.consume().size());
            }
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            for (int i = 0; i < 100; i++) {
                try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate where nodeId='" + server_2.getNodeId() + "' and mode='stopped'", Collections.emptyList())) {
                    if (scan.consume().size() > 0) {
                        break;
                    }
                }
            }
            try (DataScanner scan = scan(server_1.getManager(), "SELECT * FROM systablespacereplicastate where nodeId='" + server_2.getNodeId() + "' and mode='stopped'", Collections.emptyList())) {
                assertEquals(1, scan.consume().size());
            }
        }
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) DataScanner(herddb.model.DataScanner) DataAccessor(herddb.utils.DataAccessor) CreateTableStatement(herddb.model.commands.CreateTableStatement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with DataAccessor

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

the class DBManager method executePlan.

public StatementExecutionResult executePlan(ExecutionPlan plan, StatementEvaluationContext context, TransactionContext transactionContext) throws StatementExecutionException {
    context.setManager(this);
    plan.validateContext(context);
    if (plan.mainStatement instanceof ScanStatement) {
        DataScanner result = scan((ScanStatement) plan.mainStatement, context, transactionContext);
        // transction can be auto generated during the scan
        transactionContext = new TransactionContext(result.transactionId);
        return executeDataScannerPlan(plan, result, context, transactionContext);
    } else if (plan.dataSource != null) {
        // INSERT from SELECT
        try {
            ScanResult data = (ScanResult) executePlan(plan.dataSource, context, transactionContext);
            int insertCount = 0;
            try {
                // transction can be auto generated during the scan
                transactionContext = new TransactionContext(data.transactionId);
                while (data.dataScanner.hasNext()) {
                    DataAccessor tuple = data.dataScanner.next();
                    SQLStatementEvaluationContext tmp_context = new SQLStatementEvaluationContext("--", Arrays.asList(tuple.getValues()));
                    DMLStatementExecutionResult res = (DMLStatementExecutionResult) executeStatement(plan.mainStatement, tmp_context, transactionContext);
                    insertCount += res.getUpdateCount();
                }
            } finally {
                data.dataScanner.close();
            }
            return new DMLStatementExecutionResult(transactionContext.transactionId, insertCount);
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    } else if (plan.joinStatements != null) {
        List<DataScanner> scanResults = new ArrayList<>();
        for (ScanStatement statement : plan.joinStatements) {
            DataScanner result = scan(statement, context, transactionContext);
            // transction can be auto generated during the scan
            transactionContext = new TransactionContext(result.transactionId);
            scanResults.add(result);
        }
        return executeJoinedScansPlan(scanResults, context, transactionContext, plan);
    } else if (plan.insertStatements != null) {
        int insertCount = 0;
        for (InsertStatement insert : plan.insertStatements) {
            DMLStatementExecutionResult res = (DMLStatementExecutionResult) executeStatement(insert, context, transactionContext);
            // transction can be auto generated during the loop
            transactionContext = new TransactionContext(res.transactionId);
            insertCount += res.getUpdateCount();
        }
        return new DMLStatementExecutionResult(transactionContext.transactionId, insertCount);
    } else {
        return executeStatement(plan.mainStatement, context, transactionContext);
    }
}
Also used : ScanResult(herddb.model.ScanResult) DataScanner(herddb.model.DataScanner) LimitedDataScanner(herddb.model.LimitedDataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) DataAccessor(herddb.utils.DataAccessor) SQLStatementEvaluationContext(herddb.sql.SQLStatementEvaluationContext) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) ScanStatement(herddb.model.commands.ScanStatement) DataScannerException(herddb.model.DataScannerException)

Example 20 with DataAccessor

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

the class RoutedClientSideConnection method executeScan.

ScanResultSet executeScan(String tableSpace, String query, List<Object> params, long tx, int maxRows, int fetchSize) throws HDBException, ClientSideMetadataProviderException {
    Channel _channel = ensureOpen();
    try {
        String scannerId = this.clientId + ":" + SCANNERID_GENERATOR.incrementAndGet();
        Message message = Message.OPEN_SCANNER(clientId, tableSpace, query, scannerId, tx, params, fetchSize, maxRows);
        LOGGER.log(Level.FINEST, "open scanner {0} for query {1}, params {2}", new Object[] { scannerId, query, params });
        Message reply = _channel.sendMessageWithReply(message, timeout);
        if (reply.type == Message.TYPE_ERROR) {
            boolean notLeader = reply.parameters.get("notLeader") != null;
            if (notLeader) {
                this.connection.requestMetadataRefresh();
                throw new RetryRequestException(reply + "");
            }
            throw new HDBException(reply);
        }
        TuplesList data = (TuplesList) reply.parameters.get("data");
        List<DataAccessor> initialFetchBuffer = data.tuples;
        String[] columnNames = data.columnNames;
        boolean last = (Boolean) reply.parameters.get("last");
        long transactionId = (Long) reply.parameters.get("tx");
        // LOGGER.log(Level.SEVERE, "received first " + initialFetchBuffer.size() + " records for query " + query);
        ScanResultSetImpl impl = new ScanResultSetImpl(scannerId, columnNames, initialFetchBuffer, fetchSize, last, transactionId);
        return impl;
    } catch (InterruptedException | TimeoutException err) {
        throw new HDBException(err);
    }
}
Also used : Message(herddb.network.Message) DataAccessor(herddb.utils.DataAccessor) Channel(herddb.network.Channel) RetryRequestException(herddb.client.impl.RetryRequestException) AtomicLong(java.util.concurrent.atomic.AtomicLong) TuplesList(herddb.utils.TuplesList) TimeoutException(java.util.concurrent.TimeoutException)

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