Search in sources :

Example 21 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class AbstractRecordTable method add.

@Override
public void add(ComplexEventChunk<StreamEvent> addingEventChunk) throws ConnectionUnavailableException {
    List<Object[]> records = new ArrayList<>();
    addingEventChunk.reset();
    long timestamp = 0L;
    while (addingEventChunk.hasNext()) {
        StreamEvent event = addingEventChunk.next();
        records.add(event.getOutputData());
        timestamp = event.getTimestamp();
    }
    if (recordTableHandler != null) {
        recordTableHandler.add(timestamp, records);
    } else {
        add(records);
    }
}
Also used : StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 22 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowProcessor method init.

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
    this.outputExpectsExpiredEvents = outputExpectsExpiredEvents;
    if (outputExpectsExpiredEvents) {
        this.expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
        this.storeExpiredEvents = true;
    }
    if (attributeExpressionExecutors.length >= 2 && attributeExpressionExecutors.length <= 5) {
        if (!(attributeExpressionExecutors[0] instanceof VariableExpressionExecutor)) {
            throw new SiddhiAppValidationException("ExternalTime window's 1st parameter timestamp should be a" + " variable, but found " + attributeExpressionExecutors[0].getClass());
        }
        if (attributeExpressionExecutors[0].getReturnType() != Attribute.Type.LONG) {
            throw new SiddhiAppValidationException("ExternalTime window's 1st parameter timestamp should be " + "type long, but found " + attributeExpressionExecutors[0].getReturnType());
        }
        timestampExpressionExecutor = (VariableExpressionExecutor) attributeExpressionExecutors[0];
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
            timeToKeep = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue();
        } else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.LONG) {
            timeToKeep = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue();
        } else {
            throw new SiddhiAppValidationException("ExternalTimeBatch window's 2nd parameter windowTime " + "should be either int or long, but found " + attributeExpressionExecutors[1].getReturnType());
        }
        if (attributeExpressionExecutors.length >= 3) {
            isStartTimeEnabled = true;
            if ((attributeExpressionExecutors[2] instanceof ConstantExpressionExecutor)) {
                if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.INT) {
                    startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[2]).getValue()));
                } else if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.LONG) {
                    startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[2]).getValue()));
                } else {
                    throw new SiddhiAppValidationException("ExternalTimeBatch window's 3rd parameter " + "startTime should either be a constant (of type int or long) or an attribute (of type" + " long), but found " + attributeExpressionExecutors[2].getReturnType());
                }
            } else if (attributeExpressionExecutors[2].getReturnType() != Attribute.Type.LONG) {
                throw new SiddhiAppValidationException("ExternalTimeBatch window's 3rd parameter startTime " + "should either be a constant (of type int or long) or an attribute (of type long), but " + "found " + attributeExpressionExecutors[2].getReturnType());
            } else {
                startTimeAsVariable = attributeExpressionExecutors[2];
            }
        }
        if (attributeExpressionExecutors.length >= 4) {
            if (attributeExpressionExecutors[3].getReturnType() == Attribute.Type.INT) {
                schedulerTimeout = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[3]).getValue()));
            } else if (attributeExpressionExecutors[3].getReturnType() == Attribute.Type.LONG) {
                schedulerTimeout = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[3]).getValue()));
            } else {
                throw new SiddhiAppValidationException("ExternalTimeBatch window's 4th parameter timeout " + "should be either int or long, but found " + attributeExpressionExecutors[3].getReturnType());
            }
        }
        if (attributeExpressionExecutors.length == 5) {
            if (attributeExpressionExecutors[4].getReturnType() == Attribute.Type.BOOL) {
                replaceTimestampWithBatchEndTime = Boolean.parseBoolean(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[4]).getValue()));
            } else {
                throw new SiddhiAppValidationException("ExternalTimeBatch window's 5th parameter " + "replaceTimestampWithBatchEndTime should be bool, but found " + attributeExpressionExecutors[4].getReturnType());
            }
        }
    } else {
        throw new SiddhiAppValidationException("ExternalTimeBatch window should only have two to five " + "parameters (<long> timestamp, <int|long|time> windowTime, <long> startTime, <int|long|time> " + "timeout, <bool> replaceTimestampWithBatchEndTime), but found " + attributeExpressionExecutors.length + " input attributes");
    }
    if (schedulerTimeout > 0) {
        if (expiredEventChunk == null) {
            this.expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
        }
    }
}
Also used : StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)

Example 23 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class LossyFrequentWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        StreamEvent streamEvent = streamEventChunk.getFirst();
        streamEventChunk.clear();
        while (streamEvent != null) {
            StreamEvent next = streamEvent.getNext();
            streamEvent.setNext(null);
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            totalCount++;
            if (totalCount != 1) {
                currentBucketId = Math.ceil(totalCount / windowWidth);
            }
            String currentKey = generateKey(streamEvent);
            StreamEvent oldEvent = map.put(currentKey, clonedEvent);
            if (oldEvent != null) {
                // this event is already in the store
                countMap.put(currentKey, countMap.get(currentKey).incrementCount());
            } else {
                // This is a new event
                LossyCount lCount;
                lCount = new LossyCount(1, (int) currentBucketId - 1);
                countMap.put(currentKey, lCount);
            }
            // calculating all the events in the system which match the
            // requirement provided by the user
            List<String> keys = new ArrayList<String>();
            keys.addAll(countMap.keySet());
            for (String key : keys) {
                LossyCount lossyCount = countMap.get(key);
                if (lossyCount.getCount() >= ((support - error) * totalCount)) {
                    // among the selected events, if the newly arrive event is there we mark it as an inEvent
                    if (key.equals(currentKey)) {
                        streamEventChunk.add(streamEvent);
                    }
                }
            }
            if (totalCount % windowWidth == 0) {
                // its time to run the data-structure prune code
                keys = new ArrayList<String>();
                keys.addAll(countMap.keySet());
                for (String key : keys) {
                    LossyCount lossyCount = countMap.get(key);
                    if (lossyCount.getCount() + lossyCount.getBucketId() <= currentBucketId) {
                        log.info("Removing the Event: " + key + " from the window");
                        countMap.remove(key);
                        StreamEvent expirtedEvent = map.remove(key);
                        expirtedEvent.setTimestamp(currentTime);
                        streamEventChunk.add(expirtedEvent);
                    }
                }
            }
            streamEvent = next;
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 24 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class IndexEventHolder method add.

private void add(StreamEvent streamEvent) {
    StreamEvent existingValue = null;
    if (primaryKeyData != null) {
        Object primaryKey = constructPrimaryKey(streamEvent, primaryKeyReferenceHolders);
        existingValue = primaryKeyData.putIfAbsent(primaryKey, streamEvent);
        if (existingValue != null) {
            log.error("Siddhi App '" + siddhiAppName + "' table '" + tableName + "' drooping event : " + streamEvent + ", as there is already an event stored with primary key '" + primaryKey + "'");
        }
    }
    if (indexData != null) {
        for (Map.Entry<String, Integer> indexEntry : indexMetaData.entrySet()) {
            TreeMap<Object, Set<StreamEvent>> indexMap = indexData.get(indexEntry.getKey());
            Object key = streamEvent.getOutputData()[indexEntry.getValue()];
            Set<StreamEvent> values = indexMap.get(key);
            if (values == null) {
                values = new HashSet<StreamEvent>();
                values.add(streamEvent);
                indexMap.put(streamEvent.getOutputData()[indexEntry.getValue()], values);
            } else {
                values.add(streamEvent);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 25 with StreamEvent

use of org.ballerinalang.siddhi.core.event.stream.StreamEvent in project ballerina by ballerina-lang.

the class AbsentStreamPostStateProcessor method process.

/**
 * This method just mark the state changed but does not send the stateEvent to the next processors.
 *
 * @param stateEvent        the state event
 * @param complexEventChunk the ComplexEventChunk
 */
protected void process(StateEvent stateEvent, ComplexEventChunk complexEventChunk) {
    // Mark the state changed
    thisStatePreProcessor.stateChanged();
    // Update the timestamp
    StreamEvent streamEvent = stateEvent.getStreamEvent(stateId);
    stateEvent.setTimestamp(streamEvent.getTimestamp());
    // This is the notification to AbsentStreamPreStateProcessor that this event has been processed
    this.isEventReturned = true;
    if (thisStatePreProcessor.isStartState) {
        if (nextEveryStatePerProcessor != null && nextEveryStatePerProcessor == thisStatePreProcessor) {
            // nextEveryStatePerProcessor refers the AbsentStreamPreStateProcessor
            nextEveryStatePerProcessor.addEveryState(stateEvent);
        }
    }
    ((AbsentPreStateProcessor) thisStatePreProcessor).updateLastArrivalTime(streamEvent.getTimestamp());
}
Also used : StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Aggregations

StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)117 ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)41 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)40 StateEvent (org.ballerinalang.siddhi.core.event.state.StateEvent)19 Test (org.testng.annotations.Test)18 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)17 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)16 Event (org.ballerinalang.siddhi.core.event.Event)15 ArrayList (java.util.ArrayList)11 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)10 Map (java.util.Map)9 HashSet (java.util.HashSet)8 StreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter)8 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)8 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)7 ConversionStreamEventChunk (org.ballerinalang.siddhi.core.event.stream.converter.ConversionStreamEventChunk)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)5 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)5