Search in sources :

Example 1 with SiddhiOnDemandQueryContext

use of io.siddhi.core.config.SiddhiOnDemandQueryContext in project siddhi by wso2.

the class ExpressionBatchWindowProcessor method constructExpression.

private MetaStateEvent constructExpression(MetaStreamEvent metaStreamEvent, SiddhiQueryContext siddhiQueryContext) {
    Expression expression = SiddhiCompiler.parseExpression(expressionString);
    MetaStreamEvent metaStreamEventFirst = new MetaStreamEventWrapper(metaStreamEvent);
    metaStreamEventFirst.setInputReferenceId("first");
    MetaStreamEvent metaStreamEventLast = new MetaStreamEventWrapper(metaStreamEvent);
    metaStreamEventLast.setInputReferenceId("last");
    MetaStateEvent metaStateEvent = new MetaStateEvent(new MetaStreamEvent[] { metaStreamEvent, metaStreamEventFirst, metaStreamEventLast });
    variableExpressionExecutors = new ArrayList<>();
    SiddhiQueryContext exprQueryContext = new SiddhiOnDemandQueryContext(siddhiQueryContext.getSiddhiAppContext(), siddhiQueryContext.getName(), expressionString);
    expressionExecutor = ExpressionParser.parseExpression(expression, metaStateEvent, 0, new HashMap<>(), variableExpressionExecutors, false, 0, ProcessingMode.SLIDE, true, exprQueryContext);
    if (expressionExecutor.getReturnType() != Attribute.Type.BOOL) {
        throw new SiddhiAppRuntimeException("Expression ('" + expressionString + "') does not return Bool");
    }
    return metaStateEvent;
}
Also used : SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) Expression(io.siddhi.query.api.expression.Expression) HashMap(java.util.HashMap) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) SiddhiOnDemandQueryContext(io.siddhi.core.config.SiddhiOnDemandQueryContext) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent)

Example 2 with SiddhiOnDemandQueryContext

use of io.siddhi.core.config.SiddhiOnDemandQueryContext in project siddhi by wso2.

the class ExpressionWindowProcessor method constructExpression.

private MetaStateEvent constructExpression(MetaStreamEvent metaStreamEvent, SiddhiQueryContext siddhiQueryContext) {
    Expression expression = SiddhiCompiler.parseExpression(expressionString);
    MetaStreamEvent metaStreamEventFirst = new MetaStreamEventWrapper(metaStreamEvent);
    metaStreamEventFirst.setInputReferenceId("first");
    MetaStreamEvent metaStreamEventLast = new MetaStreamEventWrapper(metaStreamEvent);
    metaStreamEventLast.setInputReferenceId("last");
    MetaStateEvent metaStateEvent = new MetaStateEvent(new MetaStreamEvent[] { metaStreamEvent, metaStreamEventFirst, metaStreamEventLast });
    variableExpressionExecutors = new ArrayList<>();
    SiddhiQueryContext exprQueryContext = new SiddhiOnDemandQueryContext(siddhiQueryContext.getSiddhiAppContext(), siddhiQueryContext.getName(), expressionString);
    expressionExecutor = ExpressionParser.parseExpression(expression, metaStateEvent, 0, new HashMap<>(), variableExpressionExecutors, false, 0, ProcessingMode.SLIDE, true, exprQueryContext);
    if (expressionExecutor.getReturnType() != Attribute.Type.BOOL) {
        throw new SiddhiAppRuntimeException("Expression ('" + expressionString + "') does not return Bool");
    }
    return metaStateEvent;
}
Also used : SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) Expression(io.siddhi.query.api.expression.Expression) HashMap(java.util.HashMap) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) SiddhiOnDemandQueryContext(io.siddhi.core.config.SiddhiOnDemandQueryContext) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent)

Example 3 with SiddhiOnDemandQueryContext

use of io.siddhi.core.config.SiddhiOnDemandQueryContext in project siddhi by wso2.

the class OnDemandQueryParser method parse.

public static OnDemandQueryRuntime parse(OnDemandQuery onDemandQuery, String onDemandQueryString, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap) {
    final LockWrapper lockWrapper = new LockWrapper("OnDemandQueryLock");
    lockWrapper.setLock(new ReentrantLock());
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    int metaPosition = SiddhiConstants.UNKNOWN_STATE;
    String queryName;
    Table table;
    SiddhiQueryContext siddhiQueryContext;
    Expression onCondition;
    SnapshotService.getSkipStateStorageThreadLocal().set(true);
    switch(onDemandQuery.getType()) {
        case FIND:
            Within within = null;
            Expression per = null;
            queryName = "store_select_query_" + onDemandQuery.getInputStore().getStoreId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            InputStore inputStore = onDemandQuery.getInputStore();
            try {
                onCondition = Expression.value(true);
                metaStreamEvent.setInputReferenceId(inputStore.getStoreReferenceId());
                if (inputStore instanceof AggregationInputStore) {
                    AggregationInputStore aggregationInputStore = (AggregationInputStore) inputStore;
                    if (aggregationMap.get(inputStore.getStoreId()) == null) {
                        throw new OnDemandQueryCreationException("Aggregation \"" + inputStore.getStoreId() + "\" has not been defined");
                    }
                    if (aggregationInputStore.getPer() != null && aggregationInputStore.getWithin() != null) {
                        within = aggregationInputStore.getWithin();
                        per = aggregationInputStore.getPer();
                    } else if (aggregationInputStore.getPer() != null || aggregationInputStore.getWithin() != null) {
                        throw new OnDemandQueryCreationException(inputStore.getStoreId() + " should either have both 'within' and 'per' " + "defined or none.");
                    }
                    if (((AggregationInputStore) inputStore).getOnCondition() != null) {
                        onCondition = ((AggregationInputStore) inputStore).getOnCondition();
                    }
                } else if (inputStore instanceof ConditionInputStore) {
                    if (((ConditionInputStore) inputStore).getOnCondition() != null) {
                        onCondition = ((ConditionInputStore) inputStore).getOnCondition();
                    }
                }
                List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
                table = tableMap.get(inputStore.getStoreId());
                if (table != null) {
                    return constructOnDemandQueryRuntime(table, onDemandQuery, tableMap, windowMap, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
                } else {
                    AggregationRuntime aggregation = aggregationMap.get(inputStore.getStoreId());
                    if (aggregation != null) {
                        return constructOnDemandQueryRuntime(aggregation, onDemandQuery, tableMap, windowMap, within, per, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
                    } else {
                        Window window = windowMap.get(inputStore.getStoreId());
                        if (window != null) {
                            return constructOnDemandQueryRuntime(window, onDemandQuery, tableMap, windowMap, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
                        } else {
                            throw new OnDemandQueryCreationException(inputStore.getStoreId() + " is neither a table, aggregation or window");
                        }
                    }
                }
            } finally {
                SnapshotService.getSkipStateStorageThreadLocal().set(null);
            }
        case INSERT:
            InsertIntoStream inserIntoStreamt = (InsertIntoStream) onDemandQuery.getOutputStream();
            queryName = "store_insert_query_" + inserIntoStreamt.getId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            onCondition = Expression.value(true);
            return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, inserIntoStreamt, onCondition, siddhiQueryContext);
        case DELETE:
            DeleteStream deleteStream = (DeleteStream) onDemandQuery.getOutputStream();
            queryName = "store_delete_query_" + deleteStream.getId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            onCondition = deleteStream.getOnDeleteExpression();
            return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, deleteStream, onCondition, siddhiQueryContext);
        case UPDATE:
            UpdateStream outputStream = (UpdateStream) onDemandQuery.getOutputStream();
            queryName = "store_update_query_" + outputStream.getId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            onCondition = outputStream.getOnUpdateExpression();
            return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, outputStream, onCondition, siddhiQueryContext);
        case UPDATE_OR_INSERT:
            UpdateOrInsertStream onDemandQueryOutputStream = (UpdateOrInsertStream) onDemandQuery.getOutputStream();
            queryName = "store_update_or_insert_query_" + onDemandQueryOutputStream.getId();
            siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
            onCondition = onDemandQueryOutputStream.getOnUpdateExpression();
            return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, onDemandQueryOutputStream, onCondition, siddhiQueryContext);
        default:
            return null;
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Window(io.siddhi.core.window.Window) Table(io.siddhi.core.table.Table) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) ArrayList(java.util.ArrayList) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) LockWrapper(io.siddhi.core.util.lock.LockWrapper) SiddhiOnDemandQueryContext(io.siddhi.core.config.SiddhiOnDemandQueryContext) OnDemandQueryCreationException(io.siddhi.core.exception.OnDemandQueryCreationException) AggregationInputStore(io.siddhi.query.api.execution.query.input.store.AggregationInputStore) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) UpdateOrInsertStream(io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) Expression(io.siddhi.query.api.expression.Expression) ConditionInputStore(io.siddhi.query.api.execution.query.input.store.ConditionInputStore) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) Within(io.siddhi.query.api.aggregation.Within) ConditionInputStore(io.siddhi.query.api.execution.query.input.store.ConditionInputStore) AggregationInputStore(io.siddhi.query.api.execution.query.input.store.AggregationInputStore) InputStore(io.siddhi.query.api.execution.query.input.store.InputStore) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) AggregationRuntime(io.siddhi.core.aggregation.AggregationRuntime)

Aggregations

SiddhiOnDemandQueryContext (io.siddhi.core.config.SiddhiOnDemandQueryContext)3 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)3 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)3 Expression (io.siddhi.query.api.expression.Expression)3 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)2 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)2 HashMap (java.util.HashMap)2 AggregationRuntime (io.siddhi.core.aggregation.AggregationRuntime)1 OnDemandQueryCreationException (io.siddhi.core.exception.OnDemandQueryCreationException)1 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)1 Table (io.siddhi.core.table.Table)1 AbstractQueryableRecordTable (io.siddhi.core.table.record.AbstractQueryableRecordTable)1 LockWrapper (io.siddhi.core.util.lock.LockWrapper)1 Window (io.siddhi.core.window.Window)1 Within (io.siddhi.query.api.aggregation.Within)1 AggregationInputStore (io.siddhi.query.api.execution.query.input.store.AggregationInputStore)1 ConditionInputStore (io.siddhi.query.api.execution.query.input.store.ConditionInputStore)1 InputStore (io.siddhi.query.api.execution.query.input.store.InputStore)1 DeleteStream (io.siddhi.query.api.execution.query.output.stream.DeleteStream)1 InsertIntoStream (io.siddhi.query.api.execution.query.output.stream.InsertIntoStream)1