Search in sources :

Example 31 with StatementExecutionResult

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

the class SortOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    // 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;
    try (DataScanner dataScanner = inputScanner) {
        MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(inputScanner.getFieldNames(), inputScanner.getSchema());
        while (dataScanner.hasNext()) {
            DataAccessor row = dataScanner.next();
            recordSet.add(row);
        }
        recordSet.writeFinished();
        recordSet.sort(this);
        SimpleDataScanner result = new SimpleDataScanner(downstreamScanResult.dataScanner.getTransaction(), recordSet);
        return new ScanResult(downstreamScanResult.transactionId, result);
    } catch (DataScannerException ex) {
        throw new StatementExecutionException(ex);
    }
}
Also used : ScanResult(herddb.model.ScanResult) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScanner(herddb.model.DataScanner) MaterializedRecordSet(herddb.core.MaterializedRecordSet) DataAccessor(herddb.utils.DataAccessor) StatementExecutionResult(herddb.model.StatementExecutionResult) SimpleDataScanner(herddb.core.SimpleDataScanner) StatementExecutionException(herddb.model.StatementExecutionException) DataScannerException(herddb.model.DataScannerException)

Example 32 with StatementExecutionResult

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

the class AggregateOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    ScanResult downstreamScanResult = (ScanResult) input;
    final DataScanner inputScanner = downstreamScanResult.dataScanner;
    AggregatedDataScanner filtered = new AggregatedDataScanner(inputScanner, context, tableSpaceManager.getDbmanager().getRecordSetFactory());
    return new ScanResult(downstreamScanResult.transactionId, filtered);
}
Also used : ScanResult(herddb.model.ScanResult) SimpleDataScanner(herddb.core.SimpleDataScanner) DataScanner(herddb.model.DataScanner) StatementExecutionResult(herddb.model.StatementExecutionResult)

Example 33 with StatementExecutionResult

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

the class UnionAllOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    try {
        StatementExecutionResult input = this.inputs.get(0).execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
        ScanResult downstream = (ScanResult) input;
        DataScanner dataScanner = new UnionAllDataScanner(downstream.dataScanner, tableSpaceManager, transactionContext, context, lockRequired, forWrite);
        return new ScanResult(downstream.transactionId, dataScanner);
    } 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 34 with StatementExecutionResult

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

the class UpdateOp method executeAsync.

@Override
public CompletableFuture<StatementExecutionResult> executeAsync(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;
    List<DMLStatement> statements = new ArrayList<>();
    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);
            }
            Bytes key = RecordSerializer.serializeIndexKey(row, table, table.getPrimaryKey());
            DMLStatement updateStatement = new UpdateStatement(tableSpace, tableName, new ConstValueRecordFunction(key), this.recordFunction, null).setReturnValues(returnValues);
            statements.add(updateStatement);
        }
        if (statements.isEmpty()) {
            return CompletableFuture.completedFuture(new DMLStatementExecutionResult(transactionId, 0, null, null));
        }
        if (statements.size() == 1) {
            return tableSpaceManager.executeStatementAsync(statements.get(0), context, transactionContext);
        }
        CompletableFuture<StatementExecutionResult> finalResult = new CompletableFuture<>();
        AtomicInteger updateCounts = new AtomicInteger();
        AtomicReference<Bytes> lastKey = new AtomicReference<>();
        AtomicReference<Bytes> lastNewValue = new AtomicReference<>();
        class ComputeNext implements BiConsumer<StatementExecutionResult, Throwable> {

            int current;

            public ComputeNext(int current) {
                this.current = current;
            }

            @Override
            public void accept(StatementExecutionResult res, Throwable error) {
                if (error != null) {
                    finalResult.completeExceptionally(error);
                    return;
                }
                DMLStatementExecutionResult dml = (DMLStatementExecutionResult) res;
                updateCounts.addAndGet(dml.getUpdateCount());
                if (returnValues) {
                    lastKey.set(dml.getKey());
                    lastNewValue.set(dml.getNewvalue());
                }
                long newTransactionId = res.transactionId;
                if (current == statements.size()) {
                    DMLStatementExecutionResult finalDMLResult = new DMLStatementExecutionResult(newTransactionId, updateCounts.get(), lastKey.get(), lastNewValue.get());
                    finalResult.complete(finalDMLResult);
                    return;
                }
                DMLStatement nextStatement = statements.get(current);
                TransactionContext transactionContext = new TransactionContext(newTransactionId);
                CompletableFuture<StatementExecutionResult> nextPromise = tableSpaceManager.executeStatementAsync(nextStatement, context, transactionContext);
                nextPromise.whenComplete(new ComputeNext(current + 1));
            }
        }
        DMLStatement firstStatement = statements.get(0);
        tableSpaceManager.executeStatementAsync(firstStatement, context, transactionContext).whenComplete(new ComputeNext(1));
        return finalResult;
    } catch (DataScannerException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : ScanResult(herddb.model.ScanResult) UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) DataAccessor(herddb.utils.DataAccessor) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) StatementExecutionException(herddb.model.StatementExecutionException) Bytes(herddb.utils.Bytes) ConstValueRecordFunction(herddb.model.ConstValueRecordFunction) CompletableFuture(java.util.concurrent.CompletableFuture) DataScanner(herddb.model.DataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionContext(herddb.model.TransactionContext) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) DMLStatement(herddb.model.DMLStatement) BiConsumer(java.util.function.BiConsumer) DataScannerException(herddb.model.DataScannerException)

Example 35 with StatementExecutionResult

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

the class ServerSideConnectionPeer method executeUpdates.

public List<DMLResult> executeUpdates(String tableSpace, String query, long transactionId, boolean returnValues, List<List<Object>> originalBatch) throws HDBException {
    if (query == null) {
        throw new HDBException("bad query null");
    }
    List<List<Object>> batch = new ArrayList<>(originalBatch.size());
    for (int i = 0; i < originalBatch.size(); i++) {
        batch.add(PduCodec.normalizeParametersList(originalBatch.get(i)));
    }
    try {
        List<TranslatedQuery> queries = new ArrayList<>();
        for (int i = 0; i < batch.size(); i++) {
            List<Object> parameters = batch.get(i);
            TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
            queries.add(translatedQuery);
        }
        List<Long> updateCounts = new CopyOnWriteArrayList<>();
        List<Map<RawString, Object>> otherDatas = new CopyOnWriteArrayList<>();
        CompletableFuture<Long> finalResult = new CompletableFuture<>();
        class ComputeNext implements BiConsumer<StatementExecutionResult, Throwable> {

            int current;

            public ComputeNext(int current) {
                this.current = current;
            }

            @Override
            public void accept(StatementExecutionResult result, Throwable error) {
                if (error != null) {
                    finalResult.completeExceptionally(error);
                    return;
                }
                if (result instanceof DMLStatementExecutionResult) {
                    DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
                    final Map<RawString, Object> otherData;
                    if (returnValues && dml.getKey() != null) {
                        TranslatedQuery translatedQuery = queries.get(current - 1);
                        Statement statement = translatedQuery.plan.mainStatement;
                        TableAwareStatement tableStatement = (TableAwareStatement) statement;
                        Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
                        Object key = RecordSerializer.deserializePrimaryKey(dml.getKey(), table);
                        otherData = new HashMap<>();
                        otherData.put(RAWSTRING_KEY, key);
                        if (dml.getNewvalue() != null) {
                            Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
                            newvalue.forEach((k, v) -> {
                                otherData.put(RawString.of(k), v);
                            });
                        }
                    } else {
                        otherData = Collections.emptyMap();
                    }
                    updateCounts.add((long) dml.getUpdateCount());
                    otherDatas.add(otherData);
                } else if (result instanceof DDLStatementExecutionResult) {
                    Map<RawString, Object> otherData = Collections.emptyMap();
                    updateCounts.add(1L);
                    otherDatas.add(otherData);
                } else {
                    finalResult.completeExceptionally(new Exception("bad result type " + result.getClass() + " (" + result + ")"));
                    return;
                }
                long newTransactionId = result.transactionId;
                if (current == queries.size()) {
                    try {
                        finalResult.complete(newTransactionId);
                    } catch (Throwable t) {
                        finalResult.completeExceptionally(t);
                    }
                    return;
                }
                TranslatedQuery nextPlannedQuery = queries.get(current);
                TransactionContext transactionContext = new TransactionContext(newTransactionId);
                CompletableFuture<StatementExecutionResult> nextPromise = server.getManager().executePlanAsync(nextPlannedQuery.plan, nextPlannedQuery.context, transactionContext);
                nextPromise.whenComplete(new ComputeNext(current + 1));
            }
        }
        TransactionContext transactionContext = new TransactionContext(transactionId);
        TranslatedQuery firstTranslatedQuery = queries.get(0);
        server.getManager().executePlanAsync(firstTranslatedQuery.plan, firstTranslatedQuery.context, transactionContext).whenComplete(new ComputeNext(1));
        long finalTransactionId = finalResult.get();
        List<DMLResult> returnedValues = new ArrayList<>();
        for (int i = 0; i < updateCounts.size(); i++) {
            returnedValues.add(new DMLResult(updateCounts.get(i), otherDatas.get(i).get(RAWSTRING_KEY), otherDatas.get(i), finalTransactionId));
        }
        return returnedValues;
    } catch (HerdDBInternalException err) {
        throw new HDBException(err);
    } catch (InterruptedException err) {
        Thread.currentThread().interrupt();
        throw new HDBException(err);
    } catch (ExecutionException err) {
        throw new HDBException(err.getCause());
    }
}
Also used : HerdDBInternalException(herddb.core.HerdDBInternalException) RawString(herddb.utils.RawString) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) RawString(herddb.utils.RawString) HDBException(herddb.client.HDBException) CompletableFuture(java.util.concurrent.CompletableFuture) DMLResult(herddb.client.DMLResult) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) List(java.util.List) TuplesList(herddb.utils.TuplesList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Record(herddb.model.Record) StatementExecutionException(herddb.model.StatementExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TranslatedQuery(herddb.sql.TranslatedQuery) Table(herddb.model.Table) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) BeginTransactionStatement(herddb.model.commands.BeginTransactionStatement) TableAwareStatement(herddb.model.TableAwareStatement) ScanStatement(herddb.model.commands.ScanStatement) Statement(herddb.model.Statement) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) TableAwareStatement(herddb.model.TableAwareStatement) HDBException(herddb.client.HDBException) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) HerdDBInternalException(herddb.core.HerdDBInternalException) StatementExecutionException(herddb.model.StatementExecutionException) CompletionException(java.util.concurrent.CompletionException) EOFException(java.io.EOFException) ValidationException(org.apache.calcite.tools.ValidationException) NotLeaderException(herddb.model.NotLeaderException) DataScannerException(herddb.model.DataScannerException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SQLException(java.sql.SQLException) ExecutionException(java.util.concurrent.ExecutionException) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) BiConsumer(java.util.function.BiConsumer) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

StatementExecutionResult (herddb.model.StatementExecutionResult)43 StatementExecutionException (herddb.model.StatementExecutionException)35 TransactionContext (herddb.model.TransactionContext)32 DataScanner (herddb.model.DataScanner)27 Table (herddb.model.Table)26 DataScannerException (herddb.model.DataScannerException)25 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)23 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)20 ArrayList (java.util.ArrayList)20 Statement (herddb.model.Statement)18 ScanStatement (herddb.model.commands.ScanStatement)18 HashMap (java.util.HashMap)18 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)18 RollbackTransactionStatement (herddb.model.commands.RollbackTransactionStatement)17 Bytes (herddb.utils.Bytes)17 AtomicLong (java.util.concurrent.atomic.AtomicLong)17 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)16 Map (java.util.Map)16 List (java.util.List)14 Column (herddb.model.Column)12