Search in sources :

Example 1 with Attribute

use of org.wso2.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class AggregationRuntime method createNewMetaStreamEventWithStartEnd.

private static MetaStreamEvent createNewMetaStreamEventWithStartEnd(MatchingMetaInfoHolder matchingMetaInfoHolder, List<Attribute> additionalAttributes) {
    MetaStreamEvent metaStreamEventWithStartEnd;
    StreamDefinition streamDefinitionWithStartEnd = new StreamDefinition();
    if (matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length == 1) {
        metaStreamEventWithStartEnd = new MetaStreamEvent();
    } else {
        metaStreamEventWithStartEnd = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(matchingMetaInfoHolder.getMatchingStreamEventIndex());
        cloneStreamDefinition((StreamDefinition) metaStreamEventWithStartEnd.getLastInputDefinition(), streamDefinitionWithStartEnd);
    }
    streamDefinitionWithStartEnd.attribute(additionalAttributes.get(0).getName(), additionalAttributes.get(0).getType());
    streamDefinitionWithStartEnd.attribute(additionalAttributes.get(1).getName(), additionalAttributes.get(1).getType());
    initMetaStreamEvent(metaStreamEventWithStartEnd, streamDefinitionWithStartEnd);
    return metaStreamEventWithStartEnd;
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 2 with Attribute

use of org.wso2.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class LogStreamProcessor method init.

/**
 * The init method of the StreamFunction
 *
 * @param inputDefinition              the incoming stream definition
 * @param attributeExpressionExecutors the executors for the function parameters
 * @param siddhiAppContext         siddhi app context
 * @param configReader this hold the {@link LogStreamProcessor} configuration reader.
 * @return the additional output attributes introduced by the function
 */
@Override
protected List<Attribute> init(AbstractDefinition inputDefinition, ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
    int inputExecutorLength = attributeExpressionExecutors.length;
    if (inputExecutorLength == 1) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING) {
            logMessageExpressionExecutor = attributeExpressionExecutors[0];
        } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.BOOL) {
            isLogEventExpressionExecutor = attributeExpressionExecutors[0];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'isEventLogged (Bool)' " + "or 'logMessage (String)' or 'isEventLogged (Bool), logMessage (String)' or 'priority " + "(String), isEventLogged (Bool), logMessage (String)', but its 1st attribute is " + attributeExpressionExecutors[0].getReturnType());
        }
    } else if (inputExecutorLength == 2) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING && attributeExpressionExecutors[1].getReturnType() == Attribute.Type.BOOL) {
            logMessageExpressionExecutor = attributeExpressionExecutors[0];
            isLogEventExpressionExecutor = attributeExpressionExecutors[1];
        } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING && attributeExpressionExecutors[1].getReturnType() == Attribute.Type.STRING) {
            if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
                logPriority = LogPriority.valueOf(((String) attributeExpressionExecutors[0].execute(null)).toUpperCase());
            } else {
                logPriorityExpressionExecutor = attributeExpressionExecutors[0];
            }
            logMessageExpressionExecutor = attributeExpressionExecutors[1];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'logMessage (String), " + "isEventLogged (Bool)' or 'priority (String), logMessage (String)', but its returning are '" + attributeExpressionExecutors[0].getReturnType() + ", " + attributeExpressionExecutors[1].getReturnType() + "'");
        }
    } else if (inputExecutorLength == 3) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING) {
            if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
                logPriority = LogPriority.valueOf(((String) attributeExpressionExecutors[0].execute(null)).toUpperCase());
            } else {
                logPriorityExpressionExecutor = attributeExpressionExecutors[0];
            }
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 1st attribute is returning " + attributeExpressionExecutors[0].getReturnType());
        }
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.STRING) {
            logMessageExpressionExecutor = attributeExpressionExecutors[1];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 2nd attribute is returning " + attributeExpressionExecutors[1].getReturnType());
        }
        if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.BOOL) {
            isLogEventExpressionExecutor = attributeExpressionExecutors[2];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 3rd attribute is returning " + attributeExpressionExecutors[2].getReturnType());
        }
    } else if (inputExecutorLength > 3) {
        throw new SiddhiAppValidationException("Input parameters for Log can be logMessage (String), " + "isEventLogged (Bool), but there are " + attributeExpressionExecutors.length + " in the input!");
    }
    logPrefix = siddhiAppContext.getName() + ": ";
    return new ArrayList<Attribute>();
}
Also used : ArrayList(java.util.ArrayList) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Example 3 with Attribute

use of org.wso2.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class IndexEventHolder method findEvents.

@Override
public Collection<StreamEvent> findEvents(String attribute, Compare.Operator operator, Object value) {
    if (primaryKeyData != null && attribute.equals(primaryKeyAttributes)) {
        StreamEvent resultEvent;
        HashSet<StreamEvent> resultEventSet;
        switch(operator) {
            case LESS_THAN:
                return ((TreeMap<Object, StreamEvent>) primaryKeyData).headMap(value, false).values();
            case GREATER_THAN:
                return ((TreeMap<Object, StreamEvent>) primaryKeyData).tailMap(value, false).values();
            case LESS_THAN_EQUAL:
                return ((TreeMap<Object, StreamEvent>) primaryKeyData).headMap(value, true).values();
            case GREATER_THAN_EQUAL:
                return ((TreeMap<Object, StreamEvent>) primaryKeyData).tailMap(value, true).values();
            case EQUAL:
                resultEventSet = new HashSet<StreamEvent>();
                resultEvent = primaryKeyData.get(value);
                if (resultEvent != null) {
                    resultEventSet.add(resultEvent);
                }
                return resultEventSet;
            case NOT_EQUAL:
                if (primaryKeyData.size() > 0) {
                    resultEventSet = new HashSet<StreamEvent>(primaryKeyData.values());
                } else {
                    return new HashSet<StreamEvent>();
                }
                resultEvent = primaryKeyData.get(value);
                if (resultEvent != null) {
                    resultEventSet.remove(resultEvent);
                }
                return resultEventSet;
        }
    } else {
        HashSet<StreamEvent> resultEventSet = new HashSet<StreamEvent>();
        TreeMap<Object, Set<StreamEvent>> currentIndexedData = indexData.get(attribute);
        Set<StreamEvent> resultEvents;
        switch(operator) {
            case LESS_THAN:
                for (Set<StreamEvent> eventSet : currentIndexedData.headMap(value, false).values()) {
                    resultEventSet.addAll(eventSet);
                }
                return resultEventSet;
            case GREATER_THAN:
                for (Set<StreamEvent> eventSet : currentIndexedData.tailMap(value, false).values()) {
                    resultEventSet.addAll(eventSet);
                }
                return resultEventSet;
            case LESS_THAN_EQUAL:
                for (Set<StreamEvent> eventSet : currentIndexedData.headMap(value, true).values()) {
                    resultEventSet.addAll(eventSet);
                }
                return resultEventSet;
            case GREATER_THAN_EQUAL:
                for (Set<StreamEvent> eventSet : currentIndexedData.tailMap(value, true).values()) {
                    resultEventSet.addAll(eventSet);
                }
                return resultEventSet;
            case EQUAL:
                resultEvents = currentIndexedData.get(value);
                if (resultEvents != null) {
                    resultEventSet.addAll(resultEvents);
                }
                return resultEventSet;
            case NOT_EQUAL:
                if (currentIndexedData.size() > 0) {
                    resultEventSet = new HashSet<StreamEvent>();
                    for (Set<StreamEvent> eventSet : currentIndexedData.values()) {
                        resultEventSet.addAll(eventSet);
                    }
                } else {
                    resultEventSet = new HashSet<StreamEvent>();
                }
                resultEvents = currentIndexedData.get(value);
                if (resultEvents != null) {
                    resultEventSet.removeAll(resultEvents);
                }
                return resultEventSet;
        }
    }
    throw new OperationNotSupportedException(operator + " not supported for '" + value + "' by " + getClass().getName());
}
Also used : OperationNotSupportedException(org.wso2.siddhi.core.exception.OperationNotSupportedException) Set(java.util.Set) HashSet(java.util.HashSet) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) HashSet(java.util.HashSet)

Example 4 with Attribute

use of org.wso2.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class IndexEventHolder method delete.

@Override
public void delete(String attribute, Compare.Operator operator, Object value) {
    if (primaryKeyData != null && attribute.equals(primaryKeyAttributes)) {
        switch(operator) {
            case LESS_THAN:
                for (Iterator<StreamEvent> iterator = ((TreeMap<Object, StreamEvent>) primaryKeyData).headMap(value, false).values().iterator(); iterator.hasNext(); ) {
                    StreamEvent toDeleteEvent = iterator.next();
                    iterator.remove();
                    deleteFromIndexes(toDeleteEvent);
                }
                return;
            case GREATER_THAN:
                for (Iterator<StreamEvent> iterator = ((TreeMap<Object, StreamEvent>) primaryKeyData).tailMap(value, false).values().iterator(); iterator.hasNext(); ) {
                    StreamEvent toDeleteEvent = iterator.next();
                    iterator.remove();
                    deleteFromIndexes(toDeleteEvent);
                }
                return;
            case LESS_THAN_EQUAL:
                for (Iterator<StreamEvent> iterator = ((TreeMap<Object, StreamEvent>) primaryKeyData).headMap(value, true).values().iterator(); iterator.hasNext(); ) {
                    StreamEvent toDeleteEvent = iterator.next();
                    iterator.remove();
                    deleteFromIndexes(toDeleteEvent);
                }
                return;
            case GREATER_THAN_EQUAL:
                for (Iterator<StreamEvent> iterator = ((TreeMap<Object, StreamEvent>) primaryKeyData).tailMap(value, true).values().iterator(); iterator.hasNext(); ) {
                    StreamEvent toDeleteEvent = iterator.next();
                    iterator.remove();
                    deleteFromIndexes(toDeleteEvent);
                }
                return;
            case EQUAL:
                StreamEvent deletedEvent = primaryKeyData.remove(value);
                if (deletedEvent != null) {
                    deleteFromIndexes(deletedEvent);
                }
                return;
            case NOT_EQUAL:
                StreamEvent streamEvent = primaryKeyData.get(value);
                deleteAll();
                if (streamEvent != null) {
                    add(streamEvent);
                }
                return;
        }
    } else {
        switch(operator) {
            case LESS_THAN:
                for (Iterator<Set<StreamEvent>> iterator = indexData.get(attribute).headMap(value, false).values().iterator(); iterator.hasNext(); ) {
                    Set<StreamEvent> deletedEventSet = iterator.next();
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                    iterator.remove();
                }
                return;
            case GREATER_THAN:
                for (Iterator<Set<StreamEvent>> iterator = indexData.get(attribute).tailMap(value, false).values().iterator(); iterator.hasNext(); ) {
                    Set<StreamEvent> deletedEventSet = iterator.next();
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                    iterator.remove();
                }
                return;
            case LESS_THAN_EQUAL:
                for (Iterator<Set<StreamEvent>> iterator = indexData.get(attribute).headMap(value, true).values().iterator(); iterator.hasNext(); ) {
                    Set<StreamEvent> deletedEventSet = iterator.next();
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                    iterator.remove();
                }
                return;
            case GREATER_THAN_EQUAL:
                for (Iterator<Set<StreamEvent>> iterator = indexData.get(attribute).tailMap(value, true).values().iterator(); iterator.hasNext(); ) {
                    Set<StreamEvent> deletedEventSet = iterator.next();
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                    iterator.remove();
                }
                return;
            case EQUAL:
                Set<StreamEvent> deletedEventSet = indexData.get(attribute).remove(value);
                if (deletedEventSet != null && deletedEventSet.size() > 0) {
                    deleteFromIndexesAndPrimaryKey(attribute, deletedEventSet);
                }
                return;
            case NOT_EQUAL:
                Set<StreamEvent> matchingEventSet = indexData.get(attribute).get(value);
                deleteAll();
                for (StreamEvent matchingEvent : matchingEventSet) {
                    add(matchingEvent);
                }
                return;
        }
    }
    throw new OperationNotSupportedException(operator + " not supported for '" + value + "' by " + getClass().getName());
}
Also used : OperationNotSupportedException(org.wso2.siddhi.core.exception.OperationNotSupportedException) Set(java.util.Set) HashSet(java.util.HashSet) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) TreeMap(java.util.TreeMap)

Example 5 with Attribute

use of org.wso2.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class AbstractQueryableRecordTable method compileSelection.

public CompiledSelection compileSelection(Selector selector, List<Attribute> expectedOutputAttributes, MatchingMetaInfoHolder matchingMetaInfoHolder, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, String queryName) {
    List<OutputAttribute> outputAttributes = selector.getSelectionList();
    if (outputAttributes.size() == 0) {
        MetaStreamEvent metaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(matchingMetaInfoHolder.getStoreEventIndex());
        List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
        for (Attribute attribute : attributeList) {
            outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
        }
    }
    List<SelectAttributeBuilder> selectAttributeBuilders = new ArrayList<>(outputAttributes.size());
    for (OutputAttribute outputAttribute : outputAttributes) {
        ExpressionBuilder expressionBuilder = new ExpressionBuilder(outputAttribute.getExpression(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
        selectAttributeBuilders.add(new SelectAttributeBuilder(expressionBuilder, outputAttribute.getRename()));
    }
    List<ExpressionBuilder> groupByExpressionBuilders = null;
    if (selector.getGroupByList().size() != 0) {
        groupByExpressionBuilders = new ArrayList<>(outputAttributes.size());
        for (Variable variable : selector.getGroupByList()) {
            groupByExpressionBuilders.add(new ExpressionBuilder(variable, matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName));
        }
    }
    ExpressionBuilder havingExpressionBuilder = null;
    if (selector.getHavingExpression() != null) {
        havingExpressionBuilder = new ExpressionBuilder(selector.getHavingExpression(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    }
    List<OrderByAttributeBuilder> orderByAttributeBuilders = null;
    if (selector.getOrderByList().size() != 0) {
        orderByAttributeBuilders = new ArrayList<>(selector.getOrderByList().size());
        for (OrderByAttribute orderByAttribute : selector.getOrderByList()) {
            ExpressionBuilder expressionBuilder = new ExpressionBuilder(orderByAttribute.getVariable(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
            orderByAttributeBuilders.add(new OrderByAttributeBuilder(expressionBuilder, orderByAttribute.getOrder()));
        }
    }
    Long limit = null;
    if (selector.getLimit() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), matchingMetaInfoHolder.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        limit = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
    }
    CompiledSelection compiledSelection = compileSelection(selectAttributeBuilders, groupByExpressionBuilders, havingExpressionBuilder, orderByAttributeBuilders, limit);
    Map<String, ExpressionExecutor> expressionExecutorMap = new HashMap<>();
    if (selectAttributeBuilders.size() != 0) {
        for (SelectAttributeBuilder selectAttributeBuilder : selectAttributeBuilders) {
            expressionExecutorMap.putAll(selectAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
        }
    }
    if (groupByExpressionBuilders != null && groupByExpressionBuilders.size() != 0) {
        for (ExpressionBuilder groupByExpressionBuilder : groupByExpressionBuilders) {
            expressionExecutorMap.putAll(groupByExpressionBuilder.getVariableExpressionExecutorMap());
        }
    }
    if (havingExpressionBuilder != null) {
        expressionExecutorMap.putAll(havingExpressionBuilder.getVariableExpressionExecutorMap());
    }
    if (orderByAttributeBuilders != null && orderByAttributeBuilders.size() != 0) {
        for (OrderByAttributeBuilder orderByAttributeBuilder : orderByAttributeBuilders) {
            expressionExecutorMap.putAll(orderByAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
        }
    }
    return new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
}
Also used : Variable(org.wso2.siddhi.query.api.expression.Variable) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) Attribute(org.wso2.siddhi.query.api.definition.Attribute) OrderByAttribute(org.wso2.siddhi.query.api.execution.query.selection.OrderByAttribute) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) CompiledSelection(org.wso2.siddhi.core.util.collection.operator.CompiledSelection) OrderByAttribute(org.wso2.siddhi.query.api.execution.query.selection.OrderByAttribute) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

Test (org.testng.annotations.Test)126 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)103 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)89 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)89 Event (org.wso2.siddhi.core.event.Event)85 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)83 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)83 Query (org.wso2.siddhi.query.api.execution.query.Query)83 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)73 ArrayList (java.util.ArrayList)52 HashMap (java.util.HashMap)47 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)44 Attribute (org.wso2.siddhi.query.api.definition.Attribute)42 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)38 Attribute (org.wso2.charon3.core.attributes.Attribute)36 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)36 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)34 CharonException (org.wso2.charon3.core.exceptions.CharonException)29 Map (java.util.Map)23 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)22