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;
}
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);
}
}
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);
}
}
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());
}
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);
}
Aggregations