public List<Annotation> getAnnotations(String name) { List<Annotation> results = new ArrayList<>(); for (Annotation annotation : annotations) { if (annotation.getName().equalsIgnoreCase(name)) { results.add(annotation); } } return results; }
public static Annotation getAnnotation(String annotationName, List<Annotation> annotationList) { Annotation annotation = null; for (Annotation aAnnotation : annotationList) { if (annotationName.equalsIgnoreCase(aAnnotation.getName())) { if (annotation == null) { annotation = aAnnotation; } else { throw new DuplicateAnnotationException("Annotation @" + annotationName + " is defined twice", aAnnotation.getQueryContextStartIndex(), aAnnotation.getQueryContextEndIndex()); } } } return annotation; }
public List<Annotation> getAnnotations(String name) { List<Annotation> results = new ArrayList<>(); for (Annotation annotation : annotations) { if (annotation.getName().equalsIgnoreCase(name)) { results.add(annotation); } } return results; }
/** * Traverse annotations and returns the name * * @param annotations * @return */ private static String getName(List<Annotation> annotations) { String name = UUID.randomUUID().toString(); if (annotations != null) { for (Annotation annotation : annotations) { if (annotation.getName().equals(EventProcessorConstants.NAME)) { name = annotation.getElements().get(0).getValue(); } } } return name; }
/** * Extracts the query name from the annotation list, or returns the default query name * @param annotations Query annotation list * @return query name name of the query */ private String generateQueryName(List<Annotation> annotations) { for (Annotation annotation : annotations) { if (annotation.getName().equalsIgnoreCase("info")) { preserveCodeSegment(annotation); return annotation.getElement("name"); } } return DEFAULT_QUERY_NAME; }
/** * @param aggregationId Id of the aggregation which Store value to be checked. * @return True if the given aggregation uses in-memory store. */ private boolean isInMemoryStore(String aggregationId) { String storeType = SiddhiTopologyCreatorConstants.INMEMORY; for (Annotation annotation : siddhiApp.getAggregationDefinitionMap().get(aggregationId).getAnnotations()) { if (annotation.getName().equals(SiddhiTopologyCreatorConstants.PERSISTENCETABLE_IDENTIFIER)) { storeType = annotation.getElement(SiddhiTopologyCreatorConstants.TYPE_IDENTIFIER); break; } } return storeType.equalsIgnoreCase(SiddhiTopologyCreatorConstants.INMEMORY); }
/** * Generates a string list of Siddhi Annotations, from the given list of Siddhi Annotations. * Ignores the 'info' annotation * @param queryAnnotations List of Siddhi Annotations of a query * @return List of strings, each representing a Siddhi Query Annotation */ private List<String> generateAnnotationList(List<Annotation> queryAnnotations) { List<String> annotationList = new ArrayList<>(); AnnotationConfigGenerator annotationConfigGenerator = new AnnotationConfigGenerator(); for (Annotation queryAnnotation : queryAnnotations) { if (!queryAnnotation.getName().equalsIgnoreCase("info")) { annotationList.add(annotationConfigGenerator.generateAnnotationConfig(queryAnnotation)); } } preserveCodeSegmentsOf(annotationConfigGenerator); return annotationList; }
/** * Traverse the annotation and returns the execute group id * * @param annotations * @return */ private static String getExecuteGroup(List<Annotation> annotations) { String id = null; if (annotations != null) { for (Annotation annotation : annotations) { if (annotation.getName().equals(EventProcessorConstants.DIST)) { if (annotation.getElement(EventProcessorConstants.EXEC_GROUP) != null) { id = annotation.getElement(EventProcessorConstants.EXEC_GROUP); } } } } return id; }
/** * Traverse the annotation and returns the parallelism hint * * @param annotations * @return */ private static int getParallelism(List<Annotation> annotations, String elementKey) { int parallelism = 1; if (annotations != null) { for (Annotation annotation : annotations) { if (annotation.getName().equals(EventProcessorConstants.DIST)) { if (annotation.getElement(elementKey) != null) { parallelism = Integer.parseInt(annotation.getElement(elementKey)); if (parallelism == 0) { parallelism = 1; } return parallelism; } } } } return parallelism; }
/** * Generates MapperPayloadOrAttribute object, with the given Siddhi Annotation * @param attributesOrPayloadAnnotation Siddhi Annotation, denoting @attribute or @payload * @return MapperPayloadOrAttribute object */ private MapperPayloadOrAttribute generateMapperPayloadOrAttributes(Annotation attributesOrPayloadAnnotation) { List<PayloadOrAttributeElement> elements = new ArrayList<>(); for (Element element : attributesOrPayloadAnnotation.getElements()) { elements.add(generatePayloadOrAttributesElement(element)); } MapperPayloadOrAttributeType mapperPayloadOrAttributesType = getMapperAttributeOrPayloadType(elements); if (mapperPayloadOrAttributesType == MapperPayloadOrAttributeType.MAP) { return generateMapperMapAttribute(attributesOrPayloadAnnotation.getName(), elements); } return generateMapperListAttribute(attributesOrPayloadAnnotation.getName(), elements); }
/** * Generates AnnotationConfig String for the given Siddhi Annotation * @param annotation Siddhi Annotation * @return String representing the Annotation */ public String generateAnnotationConfig(Annotation annotation) { StringBuilder annotationConfig = new StringBuilder(); annotationConfig.append("@"); annotationConfig.append(annotation.getName()); annotationConfig.append("("); List<String> annotationMembers = new ArrayList<>(); for (Element element : annotation.getElements()) { annotationMembers.add(element.toString()); } for (Annotation innerAnnotation : annotation.getAnnotations()) { annotationMembers.add(generateAnnotationConfig(innerAnnotation)); } annotationConfig.append(String.join(", ", annotationMembers)); annotationConfig.append(")"); preserveCodeSegment(annotation); return annotationConfig.toString(); }
public static Annotation getAnnotation(String annotationName, List<Annotation> annotationList) { Annotation annotation = null; for (Annotation aAnnotation : annotationList) { if (annotationName.equalsIgnoreCase(aAnnotation.getName())) { if (annotation == null) { annotation = aAnnotation; } else { throw new DuplicateAnnotationException("Annotation @" + annotationName + " is defined twice", aAnnotation.getQueryContextStartIndex(), aAnnotation.getQueryContextEndIndex()); } } } return annotation; }
/** * If the Stream definition string contains {@link SiddhiTopologyCreatorConstants#SINK_IDENTIFIER} or * {@link SiddhiTopologyCreatorConstants#SOURCE_IDENTIFIER} ,meta info related to Sink/Source configuration is * removed. * * @return Stream definition String after removing Sink/Source configuration */ private String removeMetaInfoStream(String streamId, String streamDefinition, String identifier) { int[] queryContextStartIndex; int[] queryContextEndIndex; for (Annotation annotation : siddhiApp.getStreamDefinitionMap().get(streamId).getAnnotations()) { if (annotation.getName().toLowerCase().equals(identifier.replace("@", ""))) { queryContextStartIndex = annotation.getQueryContextStartIndex(); queryContextEndIndex = annotation.getQueryContextEndIndex(); streamDefinition = streamDefinition.replace( ExceptionUtil.getContext(queryContextStartIndex, queryContextEndIndex, siddhiTopologyDataHolder.getUserDefinedSiddhiApp()), ""); } } return streamDefinition; }
/** * If {@link Query,Partition} string contains {@link SiddhiTopologyCreatorConstants#DISTRIBUTED_IDENTIFIER}, * meta info related to distributed deployment is removed. */ private String removeMetaInfoQuery(ExecutionElement executionElement, String queryElement) { int[] queryContextStartIndex; int[] queryContextEndIndex; for (Annotation annotation : executionElement.getAnnotations()) { if (annotation.getName().toLowerCase() .equals(SiddhiTopologyCreatorConstants.DISTRIBUTED_IDENTIFIER)) { queryContextStartIndex = annotation.getQueryContextStartIndex(); queryContextEndIndex = annotation.getQueryContextEndIndex(); queryElement = queryElement.replace( ExceptionUtil.getContext(queryContextStartIndex, queryContextEndIndex, siddhiTopologyDataHolder.getUserDefinedSiddhiApp()), ""); break; } } return queryElement; }
/** * Loads App level Annotations from the Siddhi app * * @return A reference to this object */ public EventFlowBuilder loadAppAnnotations() { String siddhiAppName = ""; String siddhiAppDescription = ""; List<String> appAnnotations = new ArrayList<>(); AnnotationConfigGenerator annotationConfigGenerator = new AnnotationConfigGenerator(); for (Annotation annotation : siddhiApp.getAnnotations()) { if (annotation.getName().equalsIgnoreCase("NAME")) { siddhiAppName = annotation.getElements().get(0).getValue(); annotationConfigGenerator.preserveCodeSegment(annotation); } else if (annotation.getName().equalsIgnoreCase("DESCRIPTION")) { siddhiAppDescription = annotation.getElements().get(0).getValue(); annotationConfigGenerator.preserveCodeSegment(annotation); } else { appAnnotations.add( "@App:" + annotationConfigGenerator.generateAnnotationConfig(annotation).split("@")[1]); } } siddhiAppConfig.setSiddhiAppName(siddhiAppName); siddhiAppConfig.setSiddhiAppDescription(siddhiAppDescription); siddhiAppConfig.setAppAnnotationList(appAnnotations); siddhiAppConfig.addElementCodeSegments(annotationConfigGenerator.getPreservedCodeSegments()); return this; }
/** * Generates StreamConfig object, with given Siddhi StreamDefinition * * @param streamDefinition Siddhi StreamDefinition object * @return StreamConfig object */ public StreamConfig generateStreamConfig(StreamDefinition streamDefinition) { List<String> annotationConfigs = new ArrayList<>(); List<Annotation> annotationListObjects = new ArrayList<>(); List<String> streamElementAnnotationNames = new ArrayList<>(Arrays.asList("SOURCE", "SINK", "STORE")); AnnotationConfigGenerator annotationConfigGenerator = new AnnotationConfigGenerator(); for (Annotation annotation : streamDefinition.getAnnotations()) { if (!streamElementAnnotationNames.contains(annotation.getName().toUpperCase())) { // Since these annotations represent the stream itself annotationListObjects.add(annotation); annotationConfigs.add(annotationConfigGenerator.generateAnnotationConfig(annotation)); } } AttributeConfigListGenerator attributeConfigListGenerator = new AttributeConfigListGenerator(); StreamConfig streamConfig = new StreamConfig(streamDefinition.getId(), streamDefinition.getId(), attributeConfigListGenerator.generateAttributeConfigList(streamDefinition.getAttributeList()), annotationConfigs, annotationListObjects); preserveCodeSegmentsOf(annotationConfigGenerator, attributeConfigListGenerator); preserveAndBindCodeSegment(streamDefinition, streamConfig); return streamConfig; } }
/** * Method for initializing mongoClientURI and database name. * * @param storeAnnotation the source annotation which contains the needed parameters. * @param configReader {@link ConfigReader} ConfigurationReader. * @throws MongoTableException when store annotation does not contain mongodb.uri or contains an illegal * argument for mongodb.uri */ private void initializeConnectionParameters(Annotation storeAnnotation, ConfigReader configReader) { String mongoClientURI = storeAnnotation.getElement(MongoTableConstants.ANNOTATION_ELEMENT_URI); if (mongoClientURI != null) { MongoClientOptions.Builder mongoClientOptionsBuilder = MongoTableUtils.extractMongoClientOptionsBuilder(storeAnnotation, configReader); try { this.mongoClientURI = new MongoClientURI(mongoClientURI, mongoClientOptionsBuilder); this.databaseName = this.mongoClientURI.getDatabase(); } catch (IllegalArgumentException e) { throw new SiddhiAppCreationException("Annotation '" + storeAnnotation.getName() + "' contains " + "illegal value for 'mongodb.uri' as '" + mongoClientURI + "'. Please check your query and " + "try again.", e); } } else { throw new SiddhiAppCreationException("Annotation '" + storeAnnotation.getName() + "' must contain the element 'mongodb.uri'. Please check your query and try again."); } }
/** * Util method which validates the elements from an annotation to verify that they comply to the accepted standards. * * @param annotation the annotation to be validated. */ public static void validateAnnotation(Annotation annotation) { if (annotation == null) { return; } List<Element> elements = annotation.getElements(); for (Element element : elements) { if (isEmpty(element.getValue())) { throw new RDBMSTableException("Annotation '" + annotation.getName() + "' contains illegal value(s). " + "Please check your query and try again."); } } }
/** * Removes the @dist annotation from given aggregation definition. * @param aggregationDefinition Aggregation definition of which meta info to be removed. * @return String definition after the removal of @dist annotation. */ //This method is not necessary for the current design of aggregation distribution. But this left here considering // the future works on adding parallel annotation to aggregations. private String removeMetaInfoAggregation(AggregationDefinition aggregationDefinition) { int[] queryContextStartIndex; int[] queryContextEndIndex; String aggregationDef = ExceptionUtil.getContext(aggregationDefinition.getQueryContextStartIndex(), aggregationDefinition.getQueryContextEndIndex(), siddhiTopologyDataHolder.getUserDefinedSiddhiApp()); for (Annotation annotation : aggregationDefinition.getAnnotations()) { if (annotation.getName().toLowerCase() .equals(SiddhiTopologyCreatorConstants.DISTRIBUTED_IDENTIFIER)) { queryContextStartIndex = annotation.getQueryContextStartIndex(); queryContextEndIndex = annotation.getQueryContextEndIndex(); aggregationDef = aggregationDef.replace(ExceptionUtil.getContext(queryContextStartIndex, queryContextEndIndex, siddhiTopologyDataHolder.getUserDefinedSiddhiApp()), ""); break; } } return aggregationDef; }
/** * Utility method which can be used to check if the given primary key is valid i.e. non empty * and is made up of attributes and return an index model when PrimaryKey is valid. * * @param primaryKey the PrimaryKey annotation which contains the primary key attributes. * @param attributeNames List containing names of the attributes. * @return List of String with primary key attributes. */ public static IndexModel extractPrimaryKey(Annotation primaryKey, List<String> attributeNames) { if (primaryKey == null) { return null; } Document primaryKeyIndex = new Document(); primaryKey.getElements().forEach( element -> { if (!isEmpty(element.getValue()) && attributeNames.contains(element.getValue())) { primaryKeyIndex.append(element.getValue(), 1); } else { throw new SiddhiAppCreationException("Annotation '" + primaryKey.getName() + "' contains " + "value '" + element.getValue() + "' which is not present in the attributes of the " + "Event Table."); } } ); return new IndexModel(primaryKeyIndex, new IndexOptions().unique(true)); }