use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.
the class AbstractQueryableRecordTable method query.
@Override
public StreamEvent query(StateEvent matchingEvent, CompiledCondition compiledCondition, CompiledSelection compiledSelection, Attribute[] outputAttributes) throws ConnectionUnavailableException {
findMatchingEvent = matchingEvent;
updateStoreTableSize();
// handle condition type convs
ComplexEventChunk<StreamEvent> streamEventComplexEventChunk = new ComplexEventChunk<>();
RecordStoreCompiledCondition recordStoreCompiledCondition;
RecordStoreCompiledSelection recordStoreCompiledSelection;
CompiledConditionWithCache compiledConditionWithCache = null;
CompiledSelectionWithCache compiledSelectionWithCache = null;
StreamEvent cacheResults;
if (cacheEnabled) {
RecordStoreCompiledCondition compiledConditionTemp = (RecordStoreCompiledCondition) compiledCondition;
compiledConditionWithCache = (CompiledConditionWithCache) compiledConditionTemp.getCompiledCondition();
recordStoreCompiledCondition = new RecordStoreCompiledCondition(compiledConditionTemp.variableExpressionExecutorMap, compiledConditionWithCache.getStoreCompileCondition(), compiledConditionTemp.getSiddhiQueryContext());
compiledSelectionWithCache = (CompiledSelectionWithCache) compiledSelection;
recordStoreCompiledSelection = compiledSelectionWithCache.recordStoreCompiledSelection;
} else {
recordStoreCompiledSelection = ((RecordStoreCompiledSelection) compiledSelection);
recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
}
Map<String, Object> parameterMap = new HashMap<>();
for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
}
for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledSelection.variableExpressionExecutorMap.entrySet()) {
parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
}
Iterator<Object[]> records;
if (cacheEnabled) {
readWriteLock.writeLock().lock();
try {
// when store is smaller than max cache size
if (storeTableSize <= maxCacheSize && !queryStoreWithoutCheckingCache.get()) {
// return results from cache
cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
if (log.isDebugEnabled()) {
log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store table size is smaller than max cache. Sending results from cache");
}
if (cacheResults == null) {
return null;
}
return executeSelectorOnCacheResults(compiledSelectionWithCache, cacheResults, matchingEvent.getStreamEvent(0));
} else {
// when store is bigger than max cache size
if (log.isDebugEnabled() && !queryStoreWithoutCheckingCache.get()) {
log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store table size is bigger than cache.");
}
if (compiledConditionWithCache.isRouteToCache() && !queryStoreWithoutCheckingCache.get()) {
if (log.isDebugEnabled()) {
log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache constraints satisfied. Checking cache");
}
// if query conrains all primary keys and has == only for them
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 executeSelectorOnCacheResults(compiledSelectionWithCache, cacheResults, matchingEvent.getStreamEvent(0));
}
if (log.isDebugEnabled()) {
log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache miss. Loading from store");
}
// read all fields of missed entry from store
Iterator<Object[]> recordsFromSelectAll;
if (recordTableHandler != null) {
recordsFromSelectAll = recordTableHandler.query(matchingEvent.getTimestamp(), parameterMap, recordStoreCompiledCondition.getCompiledCondition(), compiledSelectionForSelectAll, outputAttributes);
} else {
recordsFromSelectAll = query(parameterMap, recordStoreCompiledCondition.getCompiledCondition(), compiledSelectionForSelectAll, outputAttributes);
}
if (recordsFromSelectAll == null || !recordsFromSelectAll.hasNext()) {
if (log.isDebugEnabled()) {
log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store also miss. sending null");
}
return null;
}
Object[] recordSelectAll = recordsFromSelectAll.next();
StreamEvent streamEvent = storeEventPool.newInstance();
streamEvent.setOutputData(new Object[outputAttributes.length]);
System.arraycopy(recordSelectAll, 0, streamEvent.getOutputData(), 0, recordSelectAll.length);
if (cacheTable.size() == maxCacheSize) {
((CacheTable) cacheTable).deleteOneEntryUsingCachePolicy();
}
((CacheTable) cacheTable).addStreamEventUptoMaxSize(streamEvent);
if (log.isDebugEnabled()) {
log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": sending results from cache after loading from store");
}
cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
return executeSelectorOnCacheResults(compiledSelectionWithCache, cacheResults, matchingEvent.getStreamEvent(0));
}
}
} finally {
readWriteLock.writeLock().unlock();
}
}
if (log.isDebugEnabled() && !queryStoreWithoutCheckingCache.get()) {
log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": sending results from store");
}
// query conditions are not satisfied check from store/ cache not enabled
if (recordTableHandler != null) {
records = recordTableHandler.query(matchingEvent.getTimestamp(), parameterMap, recordStoreCompiledCondition.getCompiledCondition(), recordStoreCompiledSelection.compiledSelection, outputAttributes);
} else {
records = query(parameterMap, recordStoreCompiledCondition.getCompiledCondition(), recordStoreCompiledSelection.compiledSelection, outputAttributes);
}
addStreamEventToChunk(outputAttributes, streamEventComplexEventChunk, records);
return streamEventComplexEventChunk.getFirst();
}
use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.
the class InMemoryTable method reduceEventsForUpdateOrInsert.
protected ComplexEventChunk<StreamEvent> reduceEventsForUpdateOrInsert(AddingStreamEventExtractor addingStreamEventExtractor, InMemoryCompiledCondition inMemoryCompiledCondition, InMemoryCompiledUpdateSet compiledUpdateSet, ComplexEventChunk<StateEvent> failedEvents) {
ComplexEventChunk<StreamEvent> toInsertEventChunk = new ComplexEventChunk<>();
failedEvents.reset();
while (failedEvents.hasNext()) {
StateEvent failedEvent = failedEvents.next();
boolean updated = false;
toInsertEventChunk.reset();
while (toInsertEventChunk.hasNext()) {
StreamEvent toInsertEvent = toInsertEventChunk.next();
failedEvent.setEvent(inMemoryCompiledCondition.getStoreEventIndex(), toInsertEvent);
if ((Boolean) inMemoryCompiledCondition.getUpdateOrInsertExpressionExecutor().execute(failedEvent)) {
for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
toInsertEvent.setOutputData(entry.getValue().execute(failedEvent), entry.getKey());
}
updated = true;
}
}
if (!updated) {
toInsertEventChunk.add(addingStreamEventExtractor.getAddingStreamEvent(failedEvent));
}
}
return toInsertEventChunk;
}
use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.
the class AbstractRecordTable method find.
@Override
public StreamEvent find(CompiledCondition compiledCondition, StateEvent matchingEvent) throws ConnectionUnavailableException {
RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
Map<String, Object> findConditionParameterMap = new HashMap<>();
for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
findConditionParameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
}
Iterator<Object[]> records;
if (recordTableHandler != null) {
records = recordTableHandler.find(matchingEvent.getTimestamp(), findConditionParameterMap, recordStoreCompiledCondition.getCompiledCondition());
} else {
records = find(findConditionParameterMap, recordStoreCompiledCondition.getCompiledCondition());
}
ComplexEventChunk<StreamEvent> streamEventComplexEventChunk = new ComplexEventChunk<>();
if (records != null) {
while (records.hasNext()) {
Object[] record = records.next();
StreamEvent streamEvent = storeEventPool.newInstance();
System.arraycopy(record, 0, streamEvent.getOutputData(), 0, record.length);
streamEventComplexEventChunk.add(streamEvent);
}
}
return streamEventComplexEventChunk.getFirst();
}
use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.
the class NonCollectionExecutor method find.
public StreamEvent find(StateEvent matchingEvent, IndexedEventHolder indexedEventHolder, StreamEventCloner storeEventCloner) {
if ((Boolean) expressionExecutor.execute(matchingEvent)) {
ComplexEventChunk<StreamEvent> returnEventChunk = new ComplexEventChunk<StreamEvent>();
Collection<StreamEvent> storeEvents = indexedEventHolder.getAllEvents();
for (StreamEvent storeEvent : storeEvents) {
if (storeEventCloner != null) {
returnEventChunk.add(storeEventCloner.copyStreamEvent(storeEvent));
} else {
returnEventChunk.add(storeEvent);
}
}
return returnEventChunk.getFirst();
} else {
return null;
}
}
use of io.siddhi.core.event.ComplexEventChunk in project siddhi by wso2.
the class CollectionOperator method find.
@Override
public StreamEvent find(StateEvent matchingEvent, Object storeEvents, StreamEventCloner storeEventCloner) {
ComplexEventChunk<StreamEvent> returnEventChunk = new ComplexEventChunk<>();
for (StreamEvent storeEvent : (Collection<StreamEvent>) storeEvents) {
matchingEvent.setEvent(storeEventPosition, storeEvent);
if ((Boolean) expressionExecutor.execute(matchingEvent)) {
returnEventChunk.add(storeEventCloner.copyStreamEvent(storeEvent));
}
matchingEvent.setEvent(storeEventPosition, null);
}
return returnEventChunk.getFirst();
}
Aggregations