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);
}
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();
}
}
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);
}
}
}
}
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();
}
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;
}
}
Aggregations