Search in sources :

Example 61 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class SiddhiApp method defineTrigger.

public SiddhiApp defineTrigger(TriggerDefinition triggerDefinition) {
    if (triggerDefinition == null) {
        throw new SiddhiAppValidationException("Trigger Definition should not be null");
    } else if (triggerDefinition.getId() == null) {
        throw new SiddhiAppValidationException("Trigger Id should not be null for Trigger Definition", triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    StreamDefinition streamDefinition = StreamDefinition.id(triggerDefinition.getId()).attribute(SiddhiConstants.TRIGGERED_TIME, Attribute.Type.LONG);
    streamDefinition.setQueryContextStartIndex(triggerDefinition.getQueryContextStartIndex());
    streamDefinition.setQueryContextEndIndex(triggerDefinition.getQueryContextEndIndex());
    try {
        checkDuplicateDefinition(streamDefinition);
    } catch (DuplicateDefinitionException e) {
        throw new DuplicateDefinitionException("Trigger '" + triggerDefinition.getId() + "' cannot be defined as," + " " + e.getMessageWithOutContext(), e, triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    if (triggerDefinitionMap.containsKey(triggerDefinition.getId())) {
        throw new DuplicateDefinitionException("Trigger Definition with same Id '" + triggerDefinition.getId() + "' already exist '" + triggerDefinitionMap.get(triggerDefinition.getId()) + "', hence cannot add '" + triggerDefinition + "'", triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    this.triggerDefinitionMap.put(triggerDefinition.getId(), triggerDefinition);
    this.streamDefinitionMap.put(streamDefinition.getId(), streamDefinition);
    return this;
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException)

Example 62 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class PartitionRuntime method clonePartition.

private synchronized void clonePartition(String key) {
    PartitionInstanceRuntime partitionInstance = this.partitionInstanceRuntimeMap.get(key);
    if (partitionInstance == null) {
        List<QueryRuntime> queryRuntimeList = new ArrayList<QueryRuntime>();
        List<QueryRuntime> partitionedQueryRuntimeList = new ArrayList<QueryRuntime>();
        for (QueryRuntime queryRuntime : metaQueryRuntimeMap.values()) {
            QueryRuntime clonedQueryRuntime = queryRuntime.clone(key, localStreamJunctionMap);
            queryRuntimeList.add(clonedQueryRuntime);
            QueryParserHelper.registerMemoryUsageTracking(clonedQueryRuntime.getQueryId(), queryRuntime, SiddhiConstants.METRIC_INFIX_QUERIES, siddhiAppContext, memoryUsageTracker);
            if (queryRuntime.isFromLocalStream()) {
                for (int i = 0; i < clonedQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().size(); i++) {
                    String streamId = queryRuntime.getStreamRuntime().getSingleStreamRuntimes().get(i).getProcessStreamReceiver().getStreamId();
                    StreamDefinition streamDefinition;
                    if (streamId.startsWith("#")) {
                        streamDefinition = (StreamDefinition) localStreamDefinitionMap.get(streamId);
                    } else {
                        streamDefinition = (StreamDefinition) streamDefinitionMap.get(streamId);
                    }
                    StreamJunction streamJunction = localStreamJunctionMap.get(streamId + key);
                    if (streamJunction == null) {
                        streamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), siddhiAppContext);
                        localStreamJunctionMap.put(streamId + key, streamJunction);
                    }
                    streamJunction.subscribe(clonedQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().get(i).getProcessStreamReceiver());
                }
            } else {
                partitionedQueryRuntimeList.add(clonedQueryRuntime);
            }
        }
        partitionInstanceRuntimeMap.putIfAbsent(key, new PartitionInstanceRuntime(key, queryRuntimeList));
        updatePartitionStreamReceivers(key, partitionedQueryRuntimeList);
    }
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) QueryRuntime(org.ballerinalang.siddhi.core.query.QueryRuntime) StreamJunction(org.ballerinalang.siddhi.core.stream.StreamJunction) ArrayList(java.util.ArrayList)

Example 63 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class SiddhiQLBaseVisitorImpl method visitDefinition_stream.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public StreamDefinition visitDefinition_stream(@NotNull SiddhiQLParser.Definition_streamContext ctx) {
    Source source = (Source) visit(ctx.source());
    if (source.isInnerStream) {
        throw newSiddhiParserException(ctx, " InnerStreams cannot be defined!");
    }
    try {
        StreamDefinition streamDefinition = StreamDefinition.id(source.streamId);
        populateQueryContext(streamDefinition, ctx);
        List<SiddhiQLParser.Attribute_nameContext> attribute_names = ctx.attribute_name();
        List<SiddhiQLParser.Attribute_typeContext> attribute_types = ctx.attribute_type();
        for (int i = 0; i < attribute_names.size(); i++) {
            SiddhiQLParser.Attribute_nameContext attributeNameContext = attribute_names.get(i);
            SiddhiQLParser.Attribute_typeContext attributeTypeContext = attribute_types.get(i);
            streamDefinition.attribute((String) visit(attributeNameContext), (Attribute.Type) visit(attributeTypeContext));
        }
        for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
            streamDefinition.annotation((Annotation) visit(annotationContext));
        }
        return streamDefinition;
    } catch (Throwable t) {
        throw newSiddhiParserException(ctx, t.getMessage(), t);
    }
}
Also used : SiddhiQLParser(org.ballerinalang.siddhi.query.compiler.SiddhiQLParser) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) OrderByAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OrderByAttribute) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute)

Example 64 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class DefineStreamTestCase method test1.

@Test
public void test1() throws SiddhiParserException {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("define stream cseStream ( symbol " + "string, price int, volume float )");
    AssertJUnit.assertEquals(StreamDefinition.id("cseStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).attribute("volume", Attribute.Type.FLOAT).toString(), streamDefinition.toString());
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Test(org.testng.annotations.Test)

Example 65 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class DefineStreamTestCase method testCreatingStreamDefinition.

// define stream StockStream (symbol string, price int, volume float );
@Test
public void testCreatingStreamDefinition() {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("define stream StockStream ( symbol " + "string, price int, volume float );");
    StreamDefinition api = StreamDefinition.id("StockStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).attribute("volume", Attribute.Type.FLOAT);
    AssertJUnit.assertEquals(api, streamDefinition);
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Test(org.testng.annotations.Test)

Aggregations

StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)110 Test (org.testng.annotations.Test)98 Event (org.ballerinalang.siddhi.core.event.Event)83 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)82 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)80 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)80 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)80 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)77 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)73 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)9 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)8 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)8 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)8 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)5 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)5 ArrayList (java.util.ArrayList)3 SelectiveStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.SelectiveStreamEventConverter)3 SimpleStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.SimpleStreamEventConverter)3 StreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter)3 ZeroStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter)3