Search in sources :

Example 46 with DataScanner

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

the class TmpMapImpl method forEach.

@Override
public void forEach(BiSink<K, V> sink) throws CollectionsException, SinkException {
    try (DataScanner dataScanner = tableSpaceManager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION, false, false)) {
        while (dataScanner.hasNext()) {
            DataAccessorForFullRecord next = (DataAccessorForFullRecord) dataScanner.next();
            Object key = next.get(0);
            if (key instanceof RawString) {
                key = key.toString();
            }
            Object value = valuesSerializer.deserialize(next.getRecord().value);
            try {
                if (!sink.accept((K) key, (V) value)) {
                    return;
                }
            } catch (Exception err) {
                throw new SinkException(err);
            }
        }
    } catch (SinkException err) {
        throw err;
    } catch (Exception err) {
        throw new CollectionsException(err);
    }
}
Also used : DataScanner(herddb.model.DataScanner) RawString(herddb.utils.RawString) DataAccessorForFullRecord(herddb.codec.DataAccessorForFullRecord) DataScannerException(herddb.model.DataScannerException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException)

Example 47 with DataScanner

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

the class ServerSideConnectionPeer method handleFetchScannerData.

private void handleFetchScannerData(Pdu message, Channel channel) {
    long scannerId = PduCodec.FetchScannerData.readScannerId(message);
    int fetchSize = PduCodec.FetchScannerData.readFetchSize(message);
    if (fetchSize <= 0) {
        fetchSize = 10;
    }
    ServerSideScannerPeer scanner = scanners.get(scannerId);
    if (scanner != null) {
        try {
            DataScanner dataScanner = scanner.getScanner();
            List<DataAccessor> records = dataScanner.consume(fetchSize);
            String[] columns = dataScanner.getFieldNames();
            TuplesList tuplesList = new TuplesList(columns, records);
            boolean last = false;
            if (dataScanner.isFinished()) {
                LOGGER.log(Level.FINEST, "unregistering scanner {0}, resultset is finished", scannerId);
                scanners.remove(scannerId);
                last = true;
            }
            // LOGGER.log(Level.SEVERE, "sending " + converted.size() + " records to scanner " + scannerId);
            try {
                ByteBuf result = PduCodec.ResultSetChunk.write(message.messageId, tuplesList, last, dataScanner.getTransactionId());
                channel.sendReplyMessage(message.messageId, result);
            } catch (HerdDBInternalException err) {
                // do not leak an unserializable scanner
                scanners.remove(scannerId);
                scanner.close();
                throw err;
            }
            if (last) {
                dataScanner.close();
            }
        } catch (DataScannerException | StatementExecutionException err) {
            ByteBuf error = composeErrorResponse(message.messageId, err);
            channel.sendReplyMessage(message.messageId, error);
        }
    } else {
        ByteBuf error = PduCodec.ErrorResponse.write(message.messageId, "no such scanner " + scannerId);
        channel.sendReplyMessage(message.messageId, error);
    }
}
Also used : HerdDBInternalException(herddb.core.HerdDBInternalException) DataAccessor(herddb.utils.DataAccessor) RawString(herddb.utils.RawString) ByteBuf(io.netty.buffer.ByteBuf) StatementExecutionException(herddb.model.StatementExecutionException) DataScanner(herddb.model.DataScanner) TuplesList(herddb.utils.TuplesList) DataScannerException(herddb.model.DataScannerException)

Example 48 with DataScanner

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

the class FilterOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    try {
        // TODO merge projection + scan + sort + limit
        StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
        ScanResult downstreamScanResult = (ScanResult) input;
        final DataScanner inputScanner = downstreamScanResult.dataScanner;
        FilteredDataScanner filtered = new FilteredDataScanner(inputScanner, condition, context);
        return new ScanResult(downstreamScanResult.transactionId, filtered);
    } catch (DataScannerException ex) {
        throw new StatementExecutionException(ex);
    }
}
Also used : ScanResult(herddb.model.ScanResult) DataScanner(herddb.model.DataScanner) StatementExecutionResult(herddb.model.StatementExecutionResult) StatementExecutionException(herddb.model.StatementExecutionException) DataScannerException(herddb.model.DataScannerException)

Example 49 with DataScanner

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

the class NestedLoopJoinOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    ScanResult resLeft = (ScanResult) left.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    DataScanner leftScanner = resLeft.dataScanner;
    transactionContext = new TransactionContext(resLeft.transactionId);
    ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    DataScanner rightScanner = resRight.dataScanner;
    final JoinType linq4jJoinType = CalciteEnumUtils.toLinq4jJoinType(joinRelType);
    if (!linq4jJoinType.generatesNullsOnLeft() && !rightScanner.isRewindSupported()) {
        try {
            MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(rightScanner.getFieldNames(), rightScanner.getSchema());
            rightScanner.forEach(d -> {
                recordSet.add(d);
            });
            recordSet.writeFinished();
            SimpleDataScanner materialized = new SimpleDataScanner(rightScanner.getTransaction(), recordSet);
            rightScanner.close();
            rightScanner = materialized;
        } catch (DataScannerException err) {
            throw new StatementExecutionException(err);
        }
    }
    final long resTransactionId = resRight.transactionId;
    final String[] fieldNamesFromLeft = leftScanner.getFieldNames();
    final String[] fieldNamesFromRight = rightScanner.getFieldNames();
    final Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection = resultProjection(fieldNamesFromLeft, fieldNamesFromRight);
    Enumerable<DataAccessor> result = EnumerableDefaults.nestedLoopJoin(leftScanner.createNonRewindableEnumerable(), rightScanner.createRewindOnCloseEnumerable(), predicate(resultProjection, context), resultProjection, linq4jJoinType);
    EnumerableDataScanner joinedScanner = new EnumerableDataScanner(rightScanner.getTransaction(), fieldNames, columns, result, leftScanner, rightScanner);
    return new ScanResult(resTransactionId, joinedScanner);
}
Also used : ScanResult(herddb.model.ScanResult) MaterializedRecordSet(herddb.core.MaterializedRecordSet) DataAccessor(herddb.utils.DataAccessor) JoinType(org.apache.calcite.linq4j.JoinType) StatementExecutionException(herddb.model.StatementExecutionException) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScanner(herddb.model.DataScanner) TransactionContext(herddb.model.TransactionContext) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScannerException(herddb.model.DataScannerException)

Example 50 with DataScanner

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

the class AutoTransactionTest method testAutoTransactionOnScan.

@Test
public void testAutoTransactionOnScan() throws Exception {
    int i = 1;
    Map<String, Object> data = new HashMap<>();
    Bytes key = Bytes.from_string("key_" + i);
    data.put("id", "key_" + i);
    data.put("number", i);
    Record record = RecordSerializer.toRecord(data, table);
    InsertStatement st = new InsertStatement(tableSpace, tableName, record);
    DMLStatementExecutionResult executeUpdate = manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    long tx;
    try (DataScanner scan = manager.scan(new ScanStatement(tableSpace, tableName, Projection.IDENTITY(table.columnNames, table.getColumns()), null, null, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION)) {
        tx = scan.getTransactionId();
        assertEquals(1, scan.consume().size());
        assertEquals(0, tx);
    }
    try (DataScanner scan = manager.scan(new ScanStatement(tableSpace, tableName, Projection.IDENTITY(table.columnNames, table.getColumns()), null, null, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT_KEEP_READ_LOCKS(), TransactionContext.AUTOTRANSACTION_TRANSACTION)) {
        tx = scan.getTransactionId();
        assertEquals(1, scan.consume().size());
    }
    TestUtils.commitTransaction(manager, tableSpace, tx);
}
Also used : Bytes(herddb.utils.Bytes) DataScanner(herddb.model.DataScanner) HashMap(java.util.HashMap) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Aggregations

DataScanner (herddb.model.DataScanner)247 Test (org.junit.Test)195 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)163 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)155 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)155 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)151 DataAccessor (herddb.utils.DataAccessor)116 Table (herddb.model.Table)101 ScanStatement (herddb.model.commands.ScanStatement)100 RawString (herddb.utils.RawString)91 DBManager (herddb.core.DBManager)90 CreateTableStatement (herddb.model.commands.CreateTableStatement)81 TranslatedQuery (herddb.sql.TranslatedQuery)79 Index (herddb.model.Index)66 TransactionContext (herddb.model.TransactionContext)59 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)59 SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)51 StatementExecutionException (herddb.model.StatementExecutionException)47 List (java.util.List)47 InsertStatement (herddb.model.commands.InsertStatement)40