Search in sources :

Example 96 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class AbstractQueryableRecordTable method find.

@Override
public StreamEvent find(CompiledCondition compiledCondition, StateEvent matchingEvent) throws ConnectionUnavailableException {
    try {
        updateStoreTableSize();
    } catch (ConnectionUnavailableException e) {
        log.error(e);
    }
    // handle compile condition type conv
    RecordStoreCompiledCondition recordStoreCompiledCondition;
    CompiledConditionWithCache compiledConditionWithCache = null;
    findMatchingEvent = matchingEvent;
    if (cacheEnabled) {
        RecordStoreCompiledCondition compiledConditionTemp = (RecordStoreCompiledCondition) compiledCondition;
        compiledConditionWithCache = (CompiledConditionWithCache) compiledConditionTemp.getCompiledCondition();
        recordStoreCompiledCondition = new RecordStoreCompiledCondition(compiledConditionTemp.variableExpressionExecutorMap, compiledConditionWithCache.getStoreCompileCondition(), compiledConditionTemp.getSiddhiQueryContext());
    } else {
        recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    }
    StreamEvent cacheResults;
    if (cacheEnabled) {
        readWriteLock.writeLock().lock();
        try {
            // when table is smaller than max cache send results from cache
            if (storeTableSize <= maxCacheSize) {
                if (log.isDebugEnabled()) {
                    log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store table size is smaller than max cache. Sending results from cache");
                }
                cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                return cacheResults;
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store table size is bigger than cache.");
                }
                if (compiledConditionWithCache.isRouteToCache()) {
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache constraints satisfied. Checking cache");
                    }
                    cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                    if (cacheResults != null) {
                        if (log.isDebugEnabled()) {
                            log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache hit. Sending results from cache");
                        }
                        return cacheResults;
                    }
                    // cache miss
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache miss. Loading from store");
                    }
                    StreamEvent streamEvent = super.find(recordStoreCompiledCondition, matchingEvent);
                    if (streamEvent == null) {
                        if (log.isDebugEnabled()) {
                            log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store also miss. sending null");
                        }
                        return null;
                    }
                    if (cacheTable.size() == maxCacheSize) {
                        ((CacheTable) cacheTable).deleteOneEntryUsingCachePolicy();
                    }
                    ((CacheTable) cacheTable).addStreamEventUptoMaxSize(streamEvent);
                    cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": sending results from cache after loading from store");
                    }
                    return cacheResults;
                }
            }
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
    // when cache is not enabled or cache query conditions are not satisfied
    if (log.isDebugEnabled()) {
        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": sending results from store");
    }
    return super.find(recordStoreCompiledCondition, matchingEvent);
}
Also used : OnDemandQueryParser.generateMatchingMetaInfoHolderForCacheTable(io.siddhi.core.util.parser.OnDemandQueryParser.generateMatchingMetaInfoHolderForCacheTable) CacheTable(io.siddhi.core.table.CacheTable) OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent(io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException)

Example 97 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class AbstractQueryableRecordTable method handleCacheExpiry.

public void handleCacheExpiry(CompiledCondition cacheExpiryCompiledCondition, ComplexEventChunk<StateEvent> deleteEventChunk) {
    if (log.isDebugEnabled()) {
        log.debug(siddhiAppContext.getName() + ": CacheExpirer started");
    }
    StateEvent stateEventForCaching = new StateEvent(1, 0);
    StreamEvent loadedDataFromStore;
    readWriteLock.writeLock().lock();
    try {
        if (storeTableSize != -1 && storeSizeLastCheckedTime > siddhiAppContext.getTimestampGenerator().currentTime() - retentionPeriod * 10) {
            if (log.isDebugEnabled()) {
                log.debug(siddhiAppContext.getName() + ": checking size of store table");
            }
            try {
                if (storeTableSize <= maxCacheSize) {
                    AbstractQueryableRecordTable.queryStoreWithoutCheckingCache.set(Boolean.TRUE);
                    try {
                        if (cacheLastReloadTime < siddhiAppContext.getTimestampGenerator().currentTime() + retentionPeriod) {
                            loadedDataFromStore = query(stateEventForCaching, compiledConditionForCaching, compiledSelectionForCaching, outputAttributesForCaching);
                            clearCacheAndReload(loadedDataFromStore);
                            cacheLastReloadTime = siddhiAppContext.getTimestampGenerator().currentTime();
                        }
                    } finally {
                        AbstractQueryableRecordTable.queryStoreWithoutCheckingCache.set(Boolean.FALSE);
                    }
                } else {
                    cacheTable.delete(deleteEventChunk, cacheExpiryCompiledCondition);
                }
            } catch (ConnectionUnavailableException e) {
                throw new SiddhiAppRuntimeException(siddhiAppContext.getName() + ": " + e.getMessage());
            }
        } else {
            try {
                AbstractQueryableRecordTable.queryStoreWithoutCheckingCache.set(Boolean.TRUE);
                try {
                    loadedDataFromStore = query(stateEventForCaching, compiledConditionForCaching, compiledSelectionForCaching, outputAttributesForCaching);
                    storeTableSize = findEventChunkSize(loadedDataFromStore);
                    storeSizeLastCheckedTime = siddhiAppContext.getTimestampGenerator().currentTime();
                } finally {
                    AbstractQueryableRecordTable.queryStoreWithoutCheckingCache.set(Boolean.FALSE);
                }
                if (storeTableSize <= maxCacheSize) {
                    if (cacheLastReloadTime < siddhiAppContext.getTimestampGenerator().currentTime() + retentionPeriod) {
                        clearCacheAndReload(loadedDataFromStore);
                        cacheLastReloadTime = siddhiAppContext.getTimestampGenerator().currentTime();
                    }
                } else {
                    cacheTable.delete(deleteEventChunk, cacheExpiryCompiledCondition);
                }
            } catch (Exception e) {
                throw new SiddhiAppRuntimeException(siddhiAppContext.getName() + ": " + e.getMessage());
            }
        }
        if (log.isDebugEnabled()) {
            log.debug(siddhiAppContext.getName() + ": CacheExpirer ended");
        }
    } finally {
        readWriteLock.writeLock().unlock();
    }
}
Also used : OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent(io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) StateEvent(io.siddhi.core.event.state.StateEvent) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException)

Example 98 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class CollectionOperator method delete.

@Override
public void delete(ComplexEventChunk<StateEvent> deletingEventChunk, Object storeEvents) {
    if (((Collection<StreamEvent>) storeEvents).size() > 0) {
        deletingEventChunk.reset();
        while (deletingEventChunk.hasNext()) {
            StateEvent deletingEvent = deletingEventChunk.next();
            try {
                for (Iterator<StreamEvent> iterator = ((Collection<StreamEvent>) storeEvents).iterator(); iterator.hasNext(); ) {
                    StreamEvent storeEvent = iterator.next();
                    deletingEvent.setEvent(storeEventPosition, storeEvent);
                    if ((Boolean) expressionExecutor.execute(deletingEvent)) {
                        iterator.remove();
                    }
                }
            } finally {
                deletingEvent.setEvent(storeEventPosition, null);
            }
        }
    }
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) Collection(java.util.Collection) StateEvent(io.siddhi.core.event.state.StateEvent)

Example 99 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class EventChunkOperator method find.

@Override
public StreamEvent find(StateEvent matchingEvent, Object storeEvents, StreamEventCloner storeEventCloner) {
    ComplexEventChunk<StreamEvent> storeEventChunk = (ComplexEventChunk<StreamEvent>) storeEvents;
    ComplexEventChunk<StreamEvent> returnEventChunk = new ComplexEventChunk<>();
    storeEventChunk.reset();
    while (storeEventChunk.hasNext()) {
        StreamEvent storeEvent = storeEventChunk.next();
        matchingEvent.setEvent(storeEventPosition, storeEvent);
        if ((Boolean) expressionExecutor.execute(matchingEvent)) {
            returnEventChunk.add(storeEventCloner.copyStreamEvent(storeEvent));
        }
        matchingEvent.setEvent(storeEventPosition, null);
    }
    return returnEventChunk.getFirst();
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent)

Example 100 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class TestStoreContainingInMemoryTable method find.

@Override
protected RecordIterator<Object[]> find(Map<String, Object> findConditionParameterMap, CompiledCondition compiledCondition) throws ConnectionUnavailableException {
    if (findMatchingEvent == null) {
        findMatchingEvent = new StateEvent(1, 0);
    }
    try {
        StreamEvent outEvent = inMemoryTable.find(compiledCondition, findMatchingEvent);
        List<Object[]> objects = new LinkedList<>();
        if (outEvent != null) {
            while (outEvent.hasNext()) {
                objects.add(outEvent.getOutputData());
                outEvent = outEvent.getNext();
            }
            objects.add(outEvent.getOutputData());
        }
        return new TestStoreWithCacheIterator(objects.iterator());
    } finally {
        findMatchingEvent = null;
    }
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) StateEvent(io.siddhi.core.event.state.StateEvent) LinkedList(java.util.LinkedList)

Aggregations

StreamEvent (io.siddhi.core.event.stream.StreamEvent)203 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)92 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)74 StateEvent (io.siddhi.core.event.state.StateEvent)52 ComplexEvent (io.siddhi.core.event.ComplexEvent)33 ArrayList (java.util.ArrayList)27 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)26 Map (java.util.Map)24 Event (io.siddhi.core.event.Event)23 Test (org.testng.annotations.Test)23 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)20 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)18 Attribute (io.siddhi.query.api.definition.Attribute)17 HashMap (java.util.HashMap)17 Operation (io.siddhi.core.event.stream.Operation)12 LinkedList (java.util.LinkedList)12 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)11 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)10 HashSet (java.util.HashSet)10 StreamEventConverter (io.siddhi.core.event.stream.converter.StreamEventConverter)8