io.siddhi.core.util.snapshot.state.State Java Examples

The following examples show how to use io.siddhi.core.util.snapshot.state.State. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IncrementalStartTimeEndTimeFunctionExecutor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    long startTime;
    long endTime;

    if (data[0] instanceof Long) {
        startTime = (long) data[0];
    } else {
        startTime = IncrementalUnixTimeFunctionExecutor.getUnixTimeStamp(data[0].toString());
    }
    if (data[1] instanceof Long) {
        endTime = (long) data[1];
    } else {
        endTime = IncrementalUnixTimeFunctionExecutor.getUnixTimeStamp(data[1].toString());
    }

    if (!(startTime < endTime)) {
        throw new SiddhiAppRuntimeException("The start time must be less than the end time in the within clause. "
                + "However, the given start time is " + startTime + " and given end time is " + endTime
                + ", in unix time. Hence, start time is not less than end time.");
    }
    return new Long[]{startTime, endTime};
}
 
Example #2
Source File: IncrementalDataAggregator.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private synchronized ComplexEventChunk<StreamEvent> getProcessedEventChunk() {
    ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<>();
    Map<String, State> valueStoreMap = this.valueStateHolder.getAllGroupByStates();
    try {
        for (State aState : valueStoreMap.values()) {
            ValueState state = (ValueState) aState;
            StreamEvent streamEvent = streamEventFactory.newInstance();
            long timestamp = state.lastTimestamp;
            streamEvent.setTimestamp(timestamp);
            state.setValue(timestamp, 0);
            streamEvent.setOutputData(state.values);
            streamEventChunk.add(streamEvent);
        }
    } finally {
        this.valueStateHolder.returnGroupByStates(valueStoreMap);
    }
    return streamEventChunk;
}
 
Example #3
Source File: OutOfOrderEventsDataAggregator.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private synchronized ComplexEventChunk<StreamEvent> createEventChunkFromAggregatedData() {
    ComplexEventChunk<StreamEvent> streamEventChunk = new ComplexEventChunk<>();
    Map<String, State> valueStoreMap = this.valueStateHolder.getAllGroupByStates();
    try {
        for (State aState : valueStoreMap.values()) {
            ValueState state = (ValueState) aState;
            StreamEvent streamEvent = streamEventFactory.newInstance();
            long timestamp = state.lastTimestamp;
            streamEvent.setTimestamp(timestamp);
            state.setValue(timestamp, 0);
            streamEvent.setOutputData(state.values);
            streamEventChunk.add(streamEvent);
        }
    } finally {
        this.valueStateHolder.returnGroupByStates(valueStoreMap);
    }
    return streamEventChunk;
}
 
Example #4
Source File: TestAsyncInMemory.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected StateFactory<State> init(StreamDefinition outputStreamDefinition, OptionHolder optionHolder,
                                   ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    this.siddhiAppContext = siddhiAppContext;
    optionHolder.validateAndGetOption(TOPIC_KEY);
    super.init(outputStreamDefinition, optionHolder, sinkConfigReader, siddhiAppContext);
    return null;
}
 
Example #5
Source File: StringSubtractFunctionExtension.java    From eagle with Apache License 2.0 5 votes vote down vote up
/**
 * The main execution method which will be called upon event arrival.
 * when there are more than one function parameter
 * This method calculates subtraction of two List Of Strings
 * Each String is a jobs string needs to be loaded
 * @param data the runtime values of function parameters
 * @return the function result
 */
@Override
protected Object execute(Object[] data, State state) {
    try {
        List<String> ths = JsonUtils.jsonStringToList((String) data[0]);
        List<String> rhs = JsonUtils.jsonStringToList((String) data[1]);

        return org.apache.commons.lang.StringUtils.join(ListUtils.subtract(ths, rhs), "\n");
    } catch (Exception e) {
        LOG.warn("exception found {0}", e);
        return null;
    }
}
 
Example #6
Source File: IfThenElseFunctionExecutor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    // check whether first argument true or null
    if (Boolean.TRUE.equals(data[0])) {
        return data[1];
    } else {
        return data[2];
    }
}
 
Example #7
Source File: RegexpIgnoreCaseFunctionExtension.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    String regex;
    Pattern pattern;
    Matcher matcher;

    if (data[0] == null) {
        throw new SiddhiAppRuntimeException("Invalid input given to str:regexpIgnoreCase() function. "
                + "First argument cannot be null");
    }
    if (data[1] == null) {
        throw new SiddhiAppRuntimeException("Invalid input given to str:regexpIgnoreCase() function. "
                + "Second argument cannot be null");
    }
    String source = (String) data[0];

    if (!isRegexConstant) {
        regex = (String) data[1];
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        matcher = pattern.matcher(source);
        return matcher.matches();

    } else {
        matcher = patternConstant.matcher(source);
        return matcher.matches();
    }
}
 
Example #8
Source File: StringListSizeFunctionExtension.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
protected Object execute(Object data, State state) {
    try {
        return JsonUtils.jsonStringToList((String) data).size();
    } catch (Exception e) {
        LOG.warn("exception found {0}", e);
        return 0;
    }
}
 
Example #9
Source File: IncrementalAggregateBaseTimeFunctionExecutor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    long time = (long) data[0];
    String durationName = (String) data[1];
    TimePeriod.Duration duration;
    switch (durationName) {
        case "SECONDS":
            duration = TimePeriod.Duration.SECONDS;
            break;
        case "MINUTES":
            duration = TimePeriod.Duration.MINUTES;
            break;
        case "HOURS":
            duration = TimePeriod.Duration.HOURS;
            break;
        case "DAYS":
            duration = TimePeriod.Duration.DAYS;
            break;
        case "MONTHS":
            duration = TimePeriod.Duration.MONTHS;
            break;
        case "YEARS":
            duration = TimePeriod.Duration.YEARS;
            break;
        default:
            // Will not get hit
            throw new SiddhiAppRuntimeException("Duration '" + durationName + "' used for " +
                    "incrementalAggregator:aggregateBaseTime() is invalid.");
    }
    return IncrementalTimeConverterUtil.getStartTimeOfAggregates(time, duration, timeZone);
}
 
Example #10
Source File: IncrementalTimeGetTimeZone.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected Object execute(Object data, State state) {
    if (data == null) {
        return ZoneOffset.systemDefault().getRules().getOffset(Instant.now()).getId();
    }
    return getTimeZone(data.toString());
}
 
Example #11
Source File: ContainsIgnoreCaseExtension.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    if (data[0] == null) {
        throw new SiddhiAppRuntimeException("Invalid input given to str:containsIgnoreCase() function. "
                + "First argument cannot be null");
    }
    if (data[1] == null) {
        throw new SiddhiAppRuntimeException("Invalid input given to str:containsIgnoreCase() function. "
                + "Second argument cannot be null");
    }
    String str1 = (String) data[0];
    String str2 = (String) data[1];
    return str1.toUpperCase().contains(str2.toUpperCase());
}
 
Example #12
Source File: ConcatFunctionExtension.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    StringBuilder sb = new StringBuilder();
    for (Object aData : data) {
        if (aData != null) {
            sb.append(aData);
        }
    }
    return sb.toString();
}
 
Example #13
Source File: InMemorySink.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(Object payload, DynamicOptions dynamicOptions, State s) throws ConnectionUnavailableException {
    try {
        InMemoryBroker.publish(topicOption.getValue(dynamicOptions), payload);
    } catch (SubscriberUnAvailableException e) {
        onError(payload, dynamicOptions, new ConnectionUnavailableException(e.getMessage(), e));
    }
}
 
Example #14
Source File: LogSink.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected StateFactory<State> init(StreamDefinition outputStreamDefinition, OptionHolder optionHolder,
                                   ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    String defaultPrefix = siddhiAppContext.getName() + " : " + outputStreamDefinition.getId();
    logPrefix = optionHolder.validateAndGetStaticValue(PREFIX, defaultPrefix);
    logPriority = LogPriority.valueOf(optionHolder.validateAndGetStaticValue(PRIORITY, "INFO")
            .toUpperCase());
    return null;
}
 
Example #15
Source File: TestFailingInMemorySink2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(Object payload, DynamicOptions dynamicOptions, State state)
        throws ConnectionUnavailableException {
    if (fail || failOnce) {
        failOnce = false;
        numberOfErrorOccurred++;
        throw new ConnectionUnavailableException("Connection unavailable during publishing");
    }
    super.publish(payload, dynamicOptions, state);
}
 
Example #16
Source File: DistributedTransport.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(Object payload, DynamicOptions transportOptions, State state)
        throws ConnectionUnavailableException {
    int errorCount = 0;
    StringBuilder errorMessages = null;
    List<Integer> destinationsToPublish = strategy.getDestinationsToPublish(payload, transportOptions);
    int destinationsToPublishSize = destinationsToPublish.size();
    if (destinationsToPublishSize == 0) {
        throw new ConnectionUnavailableException("Error on '" + siddhiAppContext.getName() + "' at Sink '" + type +
                "' stream '" + streamDefinition.getId() + "' as no connections are available to publish data.");
    }
    for (Integer destinationId : destinationsToPublish) {
        try {
            publish(payload, transportOptions, destinationId, state);
        } catch (ConnectionUnavailableException e) {
            errorCount++;
            if (errorMessages == null) {
                errorMessages = new StringBuilder();
            }
            errorMessages.append("[Destination ").append(destinationId).append("]:").append(e.getMessage());
        }
    }

    if (errorCount == destinationsToPublish.size()) {
        throw new ConnectionUnavailableException("Error on '" + siddhiAppContext.getName() + "'. " + errorCount +
                "/" + destinationsToPublish.size() + " connections failed while trying to publish with following" +
                " error messages:" + errorMessages.toString());
    }
}
 
Example #17
Source File: TestFailingInMemorySource.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(ConnectionCallback connectionCallback, State state) throws ConnectionUnavailableException {
    this.connectionCallback = connectionCallback;
    if (fail) {
        numberOfErrorOccurred++;
        throw new ConnectionUnavailableException("Connection failed!");
    }
    super.connect(connectionCallback, state);
}
 
Example #18
Source File: TestTrpInMemorySource.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public StateFactory<State> init(SourceEventListener sourceEventListener, OptionHolder optionHolder,
                                String[] requestedTransportPropertyNames, ConfigReader configReader,
                                SiddhiAppContext siddhiAppContext) {
    super.init(sourceEventListener, optionHolder, requestedTransportPropertyNames, configReader, siddhiAppContext);
    symbol = optionHolder.validateAndGetStaticValue("prop1");
    price = optionHolder.validateAndGetStaticValue("prop2");
    fail = optionHolder.validateAndGetStaticValue("fail", "false");
    this.sourceEventListener = sourceEventListener;
    String topic = optionHolder.validateAndGetStaticValue(TOPIC_KEY, "input inMemory source");
    this.subscriber = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object event) {
            if (fail.equals("true")) {
                sourceEventListener.onEvent(event, new String[]{symbol});
            } else {
                if (requestedTransportPropertyNames.length == 2 &&
                        requestedTransportPropertyNames[0].equals("symbol") &&
                        requestedTransportPropertyNames[1].equals("price")) {
                    sourceEventListener.onEvent(event, new String[]{symbol, price});
                }
            }
        }

        @Override
        public String getTopic() {
            return topic;
        }
    };
    return null;
}
 
Example #19
Source File: WindowWindowProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected void processEventChunk(ComplexEventChunk streamEventChunk, Processor nextProcessor,
                                 StreamEventCloner streamEventCloner,
                                 ComplexEventPopulater complexEventPopulater, State state) {
    streamEventChunk.reset();
    nextProcessor.process(streamEventChunk);
}
 
Example #20
Source File: TestDepInMemorySource.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public StateFactory<State> init(SourceEventListener sourceEventListener, OptionHolder optionHolder,
                                String[] requestedTransportPropertyNames, ConfigReader configReader,
                                SiddhiAppContext siddhiAppContext) {
    super.init(sourceEventListener, optionHolder, requestedTransportPropertyNames, configReader, siddhiAppContext);
    symbol = optionHolder.validateAndGetStaticValue("prop1");
    price = optionHolder.validateAndGetStaticValue("prop2");
    fail = optionHolder.validateAndGetStaticValue("fail", "false");
    this.sourceEventListener = sourceEventListener;
    String topic = optionHolder.validateAndGetStaticValue(TOPIC_KEY, "input inMemory source");
    this.subscriber = new InMemoryBroker.Subscriber() {
        @Override
        public void onMessage(Object event) {
            if (fail.equals("true")) {
                sourceEventListener.onEvent(event, new String[]{symbol});
            } else {
                if (requestedTransportPropertyNames.length == 2 &&
                        requestedTransportPropertyNames[0].equals("symbol") &&
                        requestedTransportPropertyNames[1].equals("price")) {
                    sourceEventListener.onEvent(event, new String[]{symbol, price});
                }
            }
        }

        @Override
        public String getTopic() {
            return topic;
        }
    };
    return null;
}
 
Example #21
Source File: SingleClientDistributedSink.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(Object payload, DynamicOptions transportOptions, Integer destinationId, State s)
        throws ConnectionUnavailableException {
    try {
        transportOptions.setVariableOptionIndex(destinationId);
        sink.publish(payload, transportOptions, s);
    } catch (ConnectionUnavailableException e) {
        sink.setConnected(false);
        strategy.destinationFailed(destinationId);
        log.warn("Failed to publish payload to destination ID " + destinationId + ".", e);
        sink.connectWithRetry();
        throw e;
    }
}
 
Example #22
Source File: TestFailingInMemorySink2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected StateFactory<State> init(StreamDefinition outputStreamDefinition, OptionHolder optionHolder,
                                   ConfigReader sinkConfigReader, SiddhiAppContext
                                           siddhiAppContext) {
    optionHolder.validateAndGetOption(TOPIC_KEY);
    optionHolder.validateAndGetOption(TEST_KEY);
    super.init(outputStreamDefinition, optionHolder, sinkConfigReader, siddhiAppContext);
    return null;
}
 
Example #23
Source File: TestFailingInMemorySink.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(Object payload, DynamicOptions dynamicOptions, State state)
        throws ConnectionUnavailableException {
    if (fail || failOnce || publishAlwaysFail) {
        failOnce = false;
        numberOfErrorOccurred++;
        throw new ConnectionUnavailableException("Connection unavailable during publishing");
    }
    super.publish(payload, dynamicOptions, state);
}
 
Example #24
Source File: CoalesceFunctionExecutor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
protected Object execute(Object[] obj, State state) {
    for (Object aObj : obj) {
        if (aObj != null) {
            return aObj;
        }
    }
    return null;
}
 
Example #25
Source File: DefaultFunctionExecutor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    if (data[0] == null) {
        return data[1];
    } else {
        return data[0];
    }
}
 
Example #26
Source File: TestOptionInMemorySink.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected StateFactory<State> init(StreamDefinition outputStreamDefinition, OptionHolder optionHolder,
                                   ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    topicPrefix = (String) siddhiAppContext.getAttributes().get("TopicPrefix");
    topicOption = optionHolder.validateAndGetOption(TOPIC_KEY);
    return null;
}
 
Example #27
Source File: DefaultFunctionExecutor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Override
protected Object execute(Object data, State state) {
    //this will not occur
    return null;
}
 
Example #28
Source File: CreateSetFunctionExecutor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    return null; //Since the createSet function takes in only 1 parameter, this method does not get called.
    // Hence, not implemented.
}
 
Example #29
Source File: InstanceOfLongFunctionExecutor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Override
protected Object execute(Object[] data, State state) {
    return null; //Since the instanceOfLong function takes in 1 parameter, this method does not get called. Hence,
    // not implemented.
}
 
Example #30
Source File: InstanceOfLongFunctionExecutor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Override
protected Object execute(Object data, State state) {
    return data instanceof Long;
}