Search in sources :

Example 1 with Annotation

use of org.ballerinalang.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.

the class AggregationParser method initDefaultTables.

private static HashMap<TimePeriod.Duration, Table> initDefaultTables(String aggregatorName, List<TimePeriod.Duration> durations, StreamDefinition streamDefinition, SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder, List<Annotation> annotations, List<Variable> groupByVariableList) {
    HashMap<TimePeriod.Duration, Table> aggregationTableMap = new HashMap<>();
    // Create annotations for primary key
    Annotation primaryKeyAnnotation = new Annotation(SiddhiConstants.ANNOTATION_PRIMARY_KEY);
    primaryKeyAnnotation.element(null, "AGG_TIMESTAMP");
    for (Variable groupByVariable : groupByVariableList) {
        primaryKeyAnnotation.element(null, groupByVariable.getAttributeName());
    }
    annotations.add(primaryKeyAnnotation);
    for (TimePeriod.Duration duration : durations) {
        String tableId = aggregatorName + "_" + duration.toString();
        TableDefinition tableDefinition = TableDefinition.id(tableId);
        for (Attribute attribute : streamDefinition.getAttributeList()) {
            tableDefinition.attribute(attribute.getName(), attribute.getType());
        }
        annotations.forEach(tableDefinition::annotation);
        siddhiAppRuntimeBuilder.defineTable(tableDefinition);
        aggregationTableMap.put(duration, siddhiAppRuntimeBuilder.getTableMap().get(tableId));
    }
    return aggregationTableMap;
}
Also used : Table(org.ballerinalang.siddhi.core.table.Table) Variable(org.ballerinalang.siddhi.query.api.expression.Variable) HashMap(java.util.HashMap) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute) TimePeriod(org.ballerinalang.siddhi.query.api.aggregation.TimePeriod) TableDefinition(org.ballerinalang.siddhi.query.api.definition.TableDefinition) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation)

Example 2 with Annotation

use of org.ballerinalang.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.

the class EventHolderPasser method parse.

public static EventHolder parse(AbstractDefinition tableDefinition, StreamEventPool tableStreamEventPool, SiddhiAppContext siddhiAppContext) {
    ZeroStreamEventConverter eventConverter = new ZeroStreamEventConverter();
    PrimaryKeyReferenceHolder[] primaryKeyReferenceHolders = null;
    Map<String, Integer> indexMetaData = new HashMap<String, Integer>();
    // primaryKey.
    Annotation primaryKeyAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PRIMARY_KEY, tableDefinition.getAnnotations());
    if (primaryKeyAnnotation != null) {
        if (primaryKeyAnnotation.getElements().size() == 0) {
            throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_PRIMARY_KEY + " annotation " + "contains " + primaryKeyAnnotation.getElements().size() + " element, at '" + tableDefinition.getId() + "'");
        }
        primaryKeyReferenceHolders = primaryKeyAnnotation.getElements().stream().map(element -> element.getValue().trim()).map(key -> new PrimaryKeyReferenceHolder(key, tableDefinition.getAttributePosition(key))).toArray(PrimaryKeyReferenceHolder[]::new);
    }
    // indexes.
    Annotation indexAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_INDEX, tableDefinition.getAnnotations());
    if (indexAnnotation != null) {
        if (indexAnnotation.getElements().size() == 0) {
            throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation contains " + indexAnnotation.getElements().size() + " element");
        }
        for (Element element : indexAnnotation.getElements()) {
            Integer previousValue = indexMetaData.put(element.getValue().trim(), tableDefinition.getAttributePosition(element.getValue().trim()));
            if (previousValue != null) {
                throw new SiddhiAppCreationException("Multiple " + SiddhiConstants.ANNOTATION_INDEX + " " + "annotations defined with same attribute '" + element.getValue().trim() + "', at '" + tableDefinition.getId() + "'", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
            }
        }
    }
    // not support indexBy.
    Annotation indexByAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_INDEX_BY, tableDefinition.getAnnotations());
    if (indexByAnnotation != null) {
        throw new OperationNotSupportedException(SiddhiConstants.ANNOTATION_INDEX_BY + " annotation is not " + "supported anymore, please use @PrimaryKey or @Index annotations instead," + " at '" + tableDefinition.getId() + "'");
    }
    if (primaryKeyReferenceHolders != null || indexMetaData.size() > 0) {
        boolean isNumeric = false;
        if (primaryKeyReferenceHolders != null) {
            if (primaryKeyReferenceHolders.length == 1) {
                Attribute.Type type = tableDefinition.getAttributeType(primaryKeyReferenceHolders[0].getPrimaryKeyAttribute());
                if (type == Attribute.Type.DOUBLE || type == Attribute.Type.FLOAT || type == Attribute.Type.INT || type == Attribute.Type.LONG) {
                    isNumeric = true;
                }
            }
        }
        return new IndexEventHolder(tableStreamEventPool, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
    } else {
        return new ListEventHolder(tableStreamEventPool, eventConverter);
    }
}
Also used : EventHolder(org.ballerinalang.siddhi.core.table.holder.EventHolder) Logger(org.slf4j.Logger) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation) SiddhiConstants(org.ballerinalang.siddhi.core.util.SiddhiConstants) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) SiddhiAppContext(org.ballerinalang.siddhi.core.config.SiddhiAppContext) ZeroStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool) Element(org.ballerinalang.siddhi.query.api.annotation.Element) PrimaryKeyReferenceHolder(org.ballerinalang.siddhi.core.table.holder.PrimaryKeyReferenceHolder) AnnotationHelper(org.ballerinalang.siddhi.query.api.util.AnnotationHelper) OperationNotSupportedException(org.ballerinalang.siddhi.core.exception.OperationNotSupportedException) Map(java.util.Map) IndexEventHolder(org.ballerinalang.siddhi.core.table.holder.IndexEventHolder) ListEventHolder(org.ballerinalang.siddhi.core.table.holder.ListEventHolder) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) OperationNotSupportedException(org.ballerinalang.siddhi.core.exception.OperationNotSupportedException) IndexEventHolder(org.ballerinalang.siddhi.core.table.holder.IndexEventHolder) HashMap(java.util.HashMap) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) Element(org.ballerinalang.siddhi.query.api.annotation.Element) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) PrimaryKeyReferenceHolder(org.ballerinalang.siddhi.core.table.holder.PrimaryKeyReferenceHolder) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation) ZeroStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter) ListEventHolder(org.ballerinalang.siddhi.core.table.holder.ListEventHolder)

Example 3 with Annotation

use of org.ballerinalang.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.

the class DefinitionParserHelper method getAttributeMappings.

private static AttributesHolder getAttributeMappings(Annotation mapAnnotation, String mapType, StreamDefinition streamDefinition) {
    List<Annotation> attributeAnnotations = mapAnnotation.getAnnotations(SiddhiConstants.ANNOTATION_ATTRIBUTES);
    DefinitionParserHelper.AttributesHolder attributesHolder = new DefinitionParserHelper.AttributesHolder();
    if (attributeAnnotations.size() > 0) {
        Map<String, String> elementMap = new HashMap<>();
        List<String> elementList = new ArrayList<>();
        Boolean attributesNameDefined = null;
        for (Element element : attributeAnnotations.get(0).getElements()) {
            if (element.getKey() == null) {
                if (attributesNameDefined != null && attributesNameDefined) {
                    throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream'" + streamDefinition.getId() + "', some attributes are defined and some are not defined.", element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
                }
                attributesNameDefined = false;
                elementList.add(element.getValue());
            } else {
                if (attributesNameDefined != null && !attributesNameDefined) {
                    throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', some attributes are defined and some are not defined.", element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
                }
                attributesNameDefined = true;
                elementMap.put(element.getKey(), element.getValue());
            }
        }
        if (elementMap.size() > 0) {
            List<Attribute> attributeList = streamDefinition.getAttributeList();
            for (int i = 0, attributeListSize = attributeList.size(); i < attributeListSize; i++) {
                Attribute attribute = attributeList.get(i);
                String value = elementMap.get(attribute.getName());
                if (value == null) {
                    throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', attribute '" + attribute.getName() + "' is not mapped.", mapAnnotation.getQueryContextStartIndex(), mapAnnotation.getQueryContextEndIndex());
                }
                assignMapping(attributesHolder, elementMap, i, attribute);
            }
        } else {
            List<Attribute> attributeList = streamDefinition.getAttributeList();
            if (elementList.size() != attributeList.size()) {
                throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', '" + elementList.size() + "' mapping attributes are " + "provided but expected attributes are '" + attributeList.size() + "'.", mapAnnotation.getQueryContextStartIndex(), mapAnnotation.getQueryContextEndIndex());
            }
            for (int i = 0; i < attributeList.size(); i++) {
                Attribute attribute = attributeList.get(i);
                assignMapping(attributesHolder, elementMap, i, attribute);
            }
        }
    }
    return attributesHolder;
}
Also used : HashMap(java.util.HashMap) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) Element(org.ballerinalang.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation)

Example 4 with Annotation

use of org.ballerinalang.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.

the class DefinitionParserHelper method addEventSource.

public static void addEventSource(StreamDefinition streamDefinition, ConcurrentMap<String, List<Source>> eventSourceMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sourceAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SOURCE.equalsIgnoreCase(sourceAnnotation.getName())) {
            sourceAnnotation = updateAnnotationRef(sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE, siddhiAppContext);
            Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sourceAnnotation.getAnnotations());
            if (mapAnnotation == null) {
                mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
            }
            final String sourceType = sourceAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
            final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
            if (sourceType != null && mapType != null) {
                SourceHandlerManager sourceHandlerManager = siddhiAppContext.getSiddhiContext().getSourceHandlerManager();
                SourceHandler sourceHandler = null;
                if (sourceHandlerManager != null) {
                    sourceHandler = sourceHandlerManager.generateSourceHandler();
                }
                // load input transport extension
                Extension sourceExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SOURCE, sourceType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE);
                Source source = (Source) SiddhiClassLoader.loadExtensionImplementation(sourceExtension, SourceExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sourceExtension.getNamespace(), sourceExtension.getName());
                // load input mapper extension
                Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE_MAPPER);
                SourceMapper sourceMapper = (SourceMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SourceMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(mapperExtension.getNamespace(), mapperExtension.getName());
                validateSourceMapperCompatibility(streamDefinition, sourceType, mapType, source, sourceMapper, sourceAnnotation);
                OptionHolder sourceOptionHolder = constructOptionHolder(streamDefinition, sourceAnnotation, source.getClass().getAnnotation(org.ballerinalang.siddhi.annotation.Extension.class), null);
                OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sourceMapper.getClass().getAnnotation(org.ballerinalang.siddhi.annotation.Extension.class), null);
                AttributesHolder attributesHolder = getAttributeMappings(mapAnnotation, mapType, streamDefinition);
                String[] transportPropertyNames = getTransportPropertyNames(attributesHolder);
                try {
                    source.init(sourceType, sourceOptionHolder, sourceMapper, transportPropertyNames, configReader, mapType, mapOptionHolder, attributesHolder.payloadMappings, attributesHolder.transportMappings, mapperConfigReader, sourceHandler, streamDefinition, siddhiAppContext);
                } catch (Throwable t) {
                    ExceptionUtil.populateQueryContext(t, sourceAnnotation, siddhiAppContext);
                    throw t;
                }
                siddhiAppContext.getSnapshotService().addSnapshotable(source.getStreamDefinition().getId(), source);
                if (sourceHandlerManager != null) {
                    sourceHandlerManager.registerSourceHandler(sourceHandler.getElementId(), sourceHandler);
                    siddhiAppContext.getSnapshotService().addSnapshotable(streamDefinition.getId(), sourceHandler);
                }
                List<Source> eventSources = eventSourceMap.get(streamDefinition.getId());
                if (eventSources == null) {
                    eventSources = new ArrayList<>();
                    eventSources.add(source);
                    eventSourceMap.put(streamDefinition.getId(), eventSources);
                } else {
                    eventSources.add(source);
                }
            } else {
                throw new SiddhiAppCreationException("Both @Sink(type=) and @map(type=) are required.", sourceAnnotation.getQueryContextStartIndex(), sourceAnnotation.getQueryContextEndIndex());
            }
        }
    }
}
Also used : SourceHandler(org.ballerinalang.siddhi.core.stream.input.source.SourceHandler) SourceHandlerManager(org.ballerinalang.siddhi.core.stream.input.source.SourceHandlerManager) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.ballerinalang.siddhi.core.util.config.ConfigReader) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation) Source(org.ballerinalang.siddhi.core.stream.input.source.Source) Extension(org.ballerinalang.siddhi.query.api.extension.Extension) OptionHolder(org.ballerinalang.siddhi.core.util.transport.OptionHolder) SourceMapper(org.ballerinalang.siddhi.core.stream.input.source.SourceMapper)

Example 5 with Annotation

use of org.ballerinalang.siddhi.query.api.annotation.Annotation in project ballerina by ballerina-lang.

the class SiddhiQLBaseVisitorImpl method visitApp_annotation.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Annotation visitApp_annotation(@NotNull SiddhiQLParser.App_annotationContext ctx) {
    Annotation annotation = new Annotation((String) visit(ctx.name()));
    for (SiddhiQLParser.Annotation_elementContext elementContext : ctx.annotation_element()) {
        annotation.element((Element) visit(elementContext));
    }
    populateQueryContext(annotation, ctx);
    return annotation;
}
Also used : SiddhiQLParser(org.ballerinalang.siddhi.query.compiler.SiddhiQLParser) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation)

Aggregations

Annotation (org.ballerinalang.siddhi.query.api.annotation.Annotation)10 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)5 Element (org.ballerinalang.siddhi.query.api.annotation.Element)4 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ConfigReader (org.ballerinalang.siddhi.core.util.config.ConfigReader)3 Extension (org.ballerinalang.siddhi.query.api.extension.Extension)3 SiddhiAppContext (org.ballerinalang.siddhi.core.config.SiddhiAppContext)2 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)2 Table (org.ballerinalang.siddhi.core.table.Table)2 OptionHolder (org.ballerinalang.siddhi.core.util.transport.OptionHolder)2 SiddhiAppValidationException (org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException)2 SiddhiQLParser (org.ballerinalang.siddhi.query.compiler.SiddhiQLParser)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Map (java.util.Map)1 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)1 StreamEventCloner (org.ballerinalang.siddhi.core.event.stream.StreamEventCloner)1 ZeroStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter)1 OperationNotSupportedException (org.ballerinalang.siddhi.core.exception.OperationNotSupportedException)1