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);
}
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations