All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.siddhi.core.util.SiddhiAppRuntimeBuilder Maven / Gradle / Ivy

/*
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.siddhi.core.util;

import io.siddhi.core.SiddhiAppRuntime;
import io.siddhi.core.SiddhiAppRuntimeImpl;
import io.siddhi.core.aggregation.AggregationRuntime;
import io.siddhi.core.config.SiddhiAppContext;
import io.siddhi.core.exception.SiddhiAppCreationException;
import io.siddhi.core.partition.PartitionRuntime;
import io.siddhi.core.partition.PartitionRuntimeImpl;
import io.siddhi.core.query.QueryRuntime;
import io.siddhi.core.query.QueryRuntimeImpl;
import io.siddhi.core.query.input.ProcessStreamReceiver;
import io.siddhi.core.query.input.stream.StreamRuntime;
import io.siddhi.core.query.input.stream.single.SingleStreamRuntime;
import io.siddhi.core.query.output.callback.InsertIntoStreamCallback;
import io.siddhi.core.query.output.callback.InsertIntoWindowCallback;
import io.siddhi.core.query.output.callback.OutputCallback;
import io.siddhi.core.stream.StreamJunction;
import io.siddhi.core.stream.input.InputManager;
import io.siddhi.core.stream.input.source.Source;
import io.siddhi.core.stream.output.sink.Sink;
import io.siddhi.core.table.Table;
import io.siddhi.core.trigger.Trigger;
import io.siddhi.core.util.lock.LockSynchronizer;
import io.siddhi.core.util.parser.AggregationParser;
import io.siddhi.core.util.parser.helper.DefinitionParserHelper;
import io.siddhi.core.window.Window;
import io.siddhi.query.api.definition.AbstractDefinition;
import io.siddhi.query.api.definition.AggregationDefinition;
import io.siddhi.query.api.definition.FunctionDefinition;
import io.siddhi.query.api.definition.StreamDefinition;
import io.siddhi.query.api.definition.TableDefinition;
import io.siddhi.query.api.definition.TriggerDefinition;
import io.siddhi.query.api.definition.WindowDefinition;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * For building SiddhiAppRuntime
 */
public class SiddhiAppRuntimeBuilder {
    private ConcurrentMap streamDefinitionMap =
            new ConcurrentHashMap(); //contains stream definition
    private ConcurrentMap tableDefinitionMap =
            new ConcurrentHashMap(); //contains table definition
    private ConcurrentMap windowDefinitionMap =
            new ConcurrentHashMap(); //contains window definition
    private ConcurrentMap aggregationDefinitionMap =
            new ConcurrentHashMap(); //contains aggregation definition
    private ConcurrentMap triggerDefinitionMap =
            new ConcurrentHashMap(); //contains trigger definition
    private Map queryProcessorMap =
            Collections.synchronizedMap(new LinkedHashMap());  //contains query processors
    private ConcurrentMap streamJunctionMap =
            new ConcurrentHashMap(); //contains stream junctions
    private ConcurrentMap> sourceMap =
            new ConcurrentHashMap>(); //contains sources
    private ConcurrentMap> sinkMap =
            new ConcurrentHashMap>(); //contains sinks
    private ConcurrentMap tableMap =
            new ConcurrentHashMap(); //contains tables
    private ConcurrentMap windowMap =
            new ConcurrentHashMap(); //contains windows
    private ConcurrentMap aggregationMap =
            new ConcurrentHashMap(); //contains aggregation runtime
    private ConcurrentMap triggerMap =
            new ConcurrentHashMap(); //contains triggers
    private ConcurrentMap partitionMap =
            new ConcurrentHashMap(); //contains partitions
    private ConcurrentMap siddhiAppRuntimeMap = null;
    private SiddhiAppContext siddhiAppContext;
    private InputManager inputManager;
    private LockSynchronizer lockSynchronizer = new LockSynchronizer();

    public SiddhiAppRuntimeBuilder(SiddhiAppContext siddhiAppContext) {
        this.siddhiAppContext = siddhiAppContext;
        this.inputManager = new InputManager(this.siddhiAppContext, streamDefinitionMap, streamJunctionMap, tableMap);
    }

    public void defineStream(StreamDefinition streamDefinition) {
        DefinitionParserHelper.validateDefinition(streamDefinition, streamDefinitionMap, tableDefinitionMap,
                windowDefinitionMap, aggregationDefinitionMap);
        AbstractDefinition currentDefinition = streamDefinitionMap
                .putIfAbsent(streamDefinition.getId(), streamDefinition);
        if (currentDefinition != null) {
            streamDefinition = (StreamDefinition) currentDefinition;
        }
        try {
            DefinitionParserHelper.addStreamJunction(streamDefinition, streamJunctionMap, siddhiAppContext);
        } catch (Throwable t) {
            ExceptionUtil.populateQueryContext(t, streamDefinition, siddhiAppContext);
            throw t;
        }
        DefinitionParserHelper.addEventSource(streamDefinition, sourceMap, siddhiAppContext);
        StreamJunction streamJunction = streamJunctionMap.get(streamDefinition.getId());
        DefinitionParserHelper.addEventSink(streamDefinition, streamJunction, sinkMap, siddhiAppContext);
    }

    public void defineTable(TableDefinition tableDefinition) {
        DefinitionParserHelper.validateDefinition(tableDefinition, streamDefinitionMap, tableDefinitionMap,
                windowDefinitionMap, aggregationDefinitionMap);
        AbstractDefinition currentDefinition = tableDefinitionMap.putIfAbsent(tableDefinition.getId(), tableDefinition);
        if (currentDefinition != null) {
            tableDefinition = (TableDefinition) currentDefinition;
        }
        DefinitionParserHelper.addTable(tableDefinition, tableMap, siddhiAppContext);
    }

    public void defineWindow(WindowDefinition windowDefinition) {
        DefinitionParserHelper.validateDefinition(windowDefinition, streamDefinitionMap, tableDefinitionMap,
                windowDefinitionMap, aggregationDefinitionMap);
        DefinitionParserHelper.addStreamJunction(windowDefinition, streamJunctionMap, siddhiAppContext);
        AbstractDefinition currentDefinition = windowDefinitionMap
                .putIfAbsent(windowDefinition.getId(), windowDefinition);
        if (currentDefinition != null) {
            windowDefinition = (WindowDefinition) currentDefinition;
        }
        DefinitionParserHelper.addWindow(windowDefinition, windowMap, siddhiAppContext);
    }

    public void defineTrigger(TriggerDefinition triggerDefinition) {
        DefinitionParserHelper.validateDefinition(triggerDefinition);
        TriggerDefinition currentDefinition = triggerDefinitionMap.putIfAbsent(triggerDefinition.getId(),
                triggerDefinition);
        if (currentDefinition != null) {
            triggerDefinition = currentDefinition;
        }
        DefinitionParserHelper.addEventTrigger(triggerDefinition, triggerMap, streamJunctionMap,
                siddhiAppContext);
    }

    public void defineAggregation(AggregationDefinition aggregationDefinition) {
        AggregationRuntime aggregationRuntime = AggregationParser.parse(aggregationDefinition, siddhiAppContext,
                streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap,
                windowMap, aggregationMap, this);
        DefinitionParserHelper.validateDefinition(aggregationDefinition, streamDefinitionMap, tableDefinitionMap,
                windowDefinitionMap, aggregationDefinitionMap);
        aggregationDefinitionMap.putIfAbsent(aggregationDefinition.getId(), aggregationDefinition);
        ProcessStreamReceiver processStreamReceiver = aggregationRuntime.getSingleStreamRuntime().
                getProcessStreamReceiver();
        streamJunctionMap.get(processStreamReceiver.getStreamId()).subscribe(processStreamReceiver);
        aggregationMap.putIfAbsent(aggregationDefinition.getId(), aggregationRuntime);
    }

    public void addPartition(PartitionRuntimeImpl partitionRuntime) {
        partitionMap.put(partitionRuntime.getPartitionName(), partitionRuntime);
    }

    public String addQuery(QueryRuntimeImpl queryRuntime) {
        QueryRuntime oldQueryRuntime = queryProcessorMap.put(queryRuntime.getQueryId(), queryRuntime);
        if (oldQueryRuntime != null) {
            throw new SiddhiAppCreationException("Multiple queries with name '" + queryRuntime.getQueryId() +
                    "' defined in Siddhi App '" + siddhiAppContext.getName() + "'",
                    queryRuntime.getQuery().getQueryContextStartIndex(),
                    queryRuntime.getQuery().getQueryContextEndIndex());
        }
        StreamRuntime streamRuntime = queryRuntime.getStreamRuntime();

        for (SingleStreamRuntime singleStreamRuntime : streamRuntime.getSingleStreamRuntimes()) {
            ProcessStreamReceiver processStreamReceiver = singleStreamRuntime.getProcessStreamReceiver();
            if (processStreamReceiver.toStream()) {
                StreamJunction streamJunction = streamJunctionMap.get(processStreamReceiver.getStreamId());
                if (streamJunction != null) {
                    streamJunction.subscribe(processStreamReceiver);
                } else {
                    throw new SiddhiAppCreationException("Expecting a stream, but provided '"
                            + processStreamReceiver.getStreamId() + "' is not a stream");
                }
            }
        }
        OutputCallback outputCallback = queryRuntime.getOutputCallback();

        if (outputCallback != null && outputCallback instanceof InsertIntoStreamCallback) {
            InsertIntoStreamCallback insertIntoStreamCallback = (InsertIntoStreamCallback) outputCallback;
            StreamDefinition streamDefinition = insertIntoStreamCallback.getOutputStreamDefinition();
            streamDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
            DefinitionParserHelper.validateOutputStream(streamDefinition,
                    streamDefinitionMap.get(streamDefinition.getId()));
            StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());

            if (outputStreamJunction == null) {
                outputStreamJunction = new StreamJunction(streamDefinition,
                        siddhiAppContext.getExecutorService(),
                        siddhiAppContext.getBufferSize(), null, siddhiAppContext);
                streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
            }
            insertIntoStreamCallback.init(streamJunctionMap.get(insertIntoStreamCallback.getOutputStreamDefinition()
                    .getId()));
        } else if (outputCallback != null && outputCallback instanceof InsertIntoWindowCallback) {
            InsertIntoWindowCallback insertIntoWindowCallback = (InsertIntoWindowCallback) outputCallback;
            StreamDefinition streamDefinition = insertIntoWindowCallback.getOutputStreamDefinition();
            windowDefinitionMap.putIfAbsent(streamDefinition.getId(), streamDefinition);
            DefinitionParserHelper.validateOutputStream(streamDefinition, windowDefinitionMap.get(streamDefinition
                    .getId()));
            StreamJunction outputStreamJunction = streamJunctionMap.get(streamDefinition.getId());

            if (outputStreamJunction == null) {
                outputStreamJunction = new StreamJunction(streamDefinition,
                        siddhiAppContext.getExecutorService(),
                        siddhiAppContext.getBufferSize(), null, siddhiAppContext);
                streamJunctionMap.putIfAbsent(streamDefinition.getId(), outputStreamJunction);
            }
            insertIntoWindowCallback.getWindow().setPublisher(streamJunctionMap.get(insertIntoWindowCallback
                    .getOutputStreamDefinition().getId()).constructPublisher());
        }

        return queryRuntime.getQueryId();
    }

    public void defineFunction(FunctionDefinition functionDefinition) {
        DefinitionParserHelper.addFunction(siddhiAppContext, functionDefinition);
    }

    public void setSiddhiAppRuntimeMap(ConcurrentMap siddhiAppRuntimeMap) {
        this.siddhiAppRuntimeMap = siddhiAppRuntimeMap;
    }

    public ConcurrentMap getStreamJunctions() {
        return streamJunctionMap;
    }

    public ConcurrentMap getTableMap() {
        return tableMap;
    }

    public ConcurrentMap getWindowMap() {
        return windowMap;
    }

    public ConcurrentMap getAggregationMap() {
        return aggregationMap;
    }

    public ConcurrentMap getStreamDefinitionMap() {
        return streamDefinitionMap;
    }

    public ConcurrentMap getTableDefinitionMap() {
        return tableDefinitionMap;
    }

    public ConcurrentMap> getSourceMap() {
        return sourceMap;
    }

    public ConcurrentMap> getSinkMap() {
        return sinkMap;
    }

    public ConcurrentMap getWindowDefinitionMap() {
        return windowDefinitionMap;
    }

    public ConcurrentMap getAggregationDefinitionMap() {
        return aggregationDefinitionMap;
    }

    public LockSynchronizer getLockSynchronizer() {
        return lockSynchronizer;
    }

    public SiddhiAppRuntime build() {
        return new SiddhiAppRuntimeImpl(streamDefinitionMap, tableDefinitionMap, windowDefinitionMap,
                aggregationDefinitionMap, inputManager, queryProcessorMap, streamJunctionMap, tableMap, windowMap,
                aggregationMap, sourceMap, sinkMap, partitionMap, triggerMap,
                siddhiAppContext, siddhiAppRuntimeMap);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy