use of herddb.model.DataScannerException in project herddb by diennea.
the class DBManager method executeJoinedScansPlan.
private StatementExecutionResult executeJoinedScansPlan(List<DataScanner> scanResults, StatementEvaluationContext context, TransactionContext transactionContext, ExecutionPlan plan) throws StatementExecutionException {
try {
List<Column> composedSchema = new ArrayList<>();
for (DataScanner ds : scanResults) {
composedSchema.addAll(Arrays.asList(ds.getSchema()));
}
Column[] finalSchema = new Column[composedSchema.size()];
composedSchema.toArray(finalSchema);
String[] finalSchemaFieldNames = Column.buildFieldNamesList(finalSchema);
MaterializedRecordSet finalResultSet = recordSetFactory.createRecordSet(finalSchemaFieldNames, finalSchema);
DataScannerJoinExecutor joinExecutor;
if (plan.joinProjection != null) {
if (plan.joinFilter != null) {
TuplePredicate joinFilter = plan.joinFilter;
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
if (joinFilter.matches(t, context)) {
finalResultSet.add(plan.joinProjection.map(t, context));
}
});
} else {
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
finalResultSet.add(plan.joinProjection.map(t, context));
});
}
} else {
if (plan.joinFilter != null) {
TuplePredicate joinFilter = plan.joinFilter;
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
if (joinFilter.matches(t, context)) {
finalResultSet.add(t);
}
});
} else {
joinExecutor = new DataScannerJoinExecutor(finalSchemaFieldNames, finalSchema, scanResults, t -> {
finalResultSet.add(t);
});
}
}
joinExecutor.executeJoin();
finalResultSet.writeFinished();
finalResultSet.sort(plan.comparator);
finalResultSet.applyLimits(plan.limits, context);
return new ScanResult(transactionContext.transactionId, new SimpleDataScanner(transactionContext.transactionId, finalResultSet));
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.DataScannerException in project herddb by diennea.
the class DeleteOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
ScanResult downstreamScanResult = (ScanResult) input;
final Table table = tableSpaceManager.getTableManager(tableName).getTable();
long transactionId = transactionContext.transactionId;
int updateCount = 0;
Bytes key = null;
try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
while (inputScanner.hasNext()) {
DataAccessor row = inputScanner.next();
long transactionIdFromScanner = inputScanner.getTransactionId();
if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
transactionId = transactionIdFromScanner;
transactionContext = new TransactionContext(transactionId);
}
key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
DMLStatement deleteStatement = new DeleteStatement(tableSpace, tableName, null, new RawKeyEquals(key));
DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(deleteStatement, context, transactionContext);
updateCount += _result.getUpdateCount();
if (_result.transactionId > 0 && _result.transactionId != transactionId) {
transactionId = _result.transactionId;
transactionContext = new TransactionContext(transactionId);
}
key = _result.getKey();
}
return new DMLStatementExecutionResult(transactionId, updateCount, key, null);
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.DataScannerException in project herddb by diennea.
the class UpdateOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
ScanResult downstreamScanResult = (ScanResult) input;
final Table table = tableSpaceManager.getTableManager(tableName).getTable();
long transactionId = transactionContext.transactionId;
int updateCount = 0;
Bytes key = null;
Bytes newValue = null;
try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
while (inputScanner.hasNext()) {
DataAccessor row = inputScanner.next();
long transactionIdFromScanner = inputScanner.getTransactionId();
if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
transactionId = transactionIdFromScanner;
transactionContext = new TransactionContext(transactionId);
}
key = RecordSerializer.serializePrimaryKey(row, table, table.getPrimaryKey());
Predicate pred = new RawKeyEquals(key);
DMLStatement updateStatement = new UpdateStatement(tableSpace, tableName, null, this.recordFunction, pred).setReturnValues(returnValues);
DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(updateStatement, context, transactionContext);
updateCount += _result.getUpdateCount();
if (_result.transactionId > 0 && _result.transactionId != transactionId) {
transactionId = _result.transactionId;
transactionContext = new TransactionContext(transactionId);
}
key = _result.getKey();
newValue = _result.getNewvalue();
}
return new DMLStatementExecutionResult(transactionId, updateCount, key, newValue);
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
use of herddb.model.DataScannerException in project herddb by diennea.
the class ServerSideConnectionPeer method handleFetchScannerData.
private void handleFetchScannerData(Message message, Channel _channel) {
String scannerId = (String) message.parameters.get("scannerId");
int fetchSize = (Integer) message.parameters.get("fetchSize");
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);
_channel.sendReplyMessage(message, Message.RESULTSET_CHUNK(null, scannerId, tuplesList, last, dataScanner.transactionId));
} catch (DataScannerException error) {
_channel.sendReplyMessage(message, Message.ERROR(null, error).setParameter("scannerId", scannerId));
}
} else {
_channel.sendReplyMessage(message, Message.ERROR(null, new Exception("no such scanner " + scannerId + ", only " + scanners.keySet())).setParameter("scannerId", scannerId));
}
}
use of herddb.model.DataScannerException in project herddb by diennea.
the class TableDataChecksum method createChecksum.
public static TableChecksum createChecksum(DBManager manager, TranslatedQuery query, TableSpaceManager tableSpaceManager, String tableSpace, String tableName) throws DataScannerException {
AbstractTableManager tablemanager = tableSpaceManager.getTableManager(tableName);
String nodeID = tableSpaceManager.getDbmanager().getNodeId();
TranslatedQuery translated = query;
final Table table = manager.getTableSpaceManager(tableSpace).getTableManager(tableName).getTable();
// Number of records
long nrecords = 0;
// For example, in leader node we may not know the query
if (translated == null) {
String columns = formatColumns(table);
/*
scan = true
allowCache = false
returnValues = false
maxRows = -1
*/
translated = manager.getPlanner().translate(tableSpace, "SELECT " + columns + " FROM " + tableName + " order by " + formatPrimaryKeys(table), Collections.emptyList(), true, false, false, -1);
}
ScanStatement statement = translated.plan.mainStatement.unwrap(ScanStatement.class);
statement.setAllowExecutionFromFollower(true);
LOGGER.log(Level.INFO, "creating checksum for table {0}.{1} on node {2}", new Object[] { tableSpace, tableName, nodeID });
try (DataScanner scan = manager.scan(statement, translated.context, TransactionContext.NO_TRANSACTION)) {
StreamingXXHash64 hash64 = FACTORY.newStreamingHash64(SEED);
long _start = System.currentTimeMillis();
while (scan.hasNext()) {
nrecords++;
DataAccessor data = scan.next();
data.forEach((String key, Object value) -> {
int type = table.getColumn(key).type;
byte[] serialize = RecordSerializer.serialize(value, type);
/*
Update need three parameters
update(byte[]buff, int off, int len)
buff is the input data
off is the start offset in buff
len is the number of bytes to hash
*/
if (serialize != null) {
hash64.update(serialize, 0, serialize.length);
}
});
}
LOGGER.log(Level.INFO, "Number of processed records for table {0}.{1} on node {2} = {3} ", new Object[] { tableSpace, tableName, nodeID, nrecords });
long _stop = System.currentTimeMillis();
long nextAutoIncrementValue = tablemanager.getNextPrimaryKeyValue();
long scanduration = (_stop - _start);
LOGGER.log(Level.INFO, "Creating checksum for table {0}.{1} on node {2} finished in {3} ms", new Object[] { tableSpace, tableName, nodeID, scanduration });
SystemInstrumentation.instrumentationPoint("createChecksum", tableSpace, tableName);
return new TableChecksum(tableSpace, tableName, hash64.getValue(), HASH_TYPE, nrecords, nextAutoIncrementValue, translated.context.query, scanduration);
} catch (DataScannerException ex) {
LOGGER.log(Level.SEVERE, "Scan failled", ex);
throw new DataScannerException(ex);
}
}
Aggregations