use of io.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.
the class ExpressionBuilder method buildStreamVariableExecutor.
private void buildStreamVariableExecutor(Variable variable, int streamEventChainIndex, ExpressionVisitor expressionVisitor, Attribute.Type type) {
String id = variable.getAttributeName();
if (variable.getStreamId() != null) {
id = variable.getStreamId() + "." + id;
}
expressionVisitor.beginVisitStreamVariable(id, variable.getStreamId(), variable.getAttributeName(), type);
if (!variableExpressionExecutorMap.containsKey(id)) {
ExpressionExecutor variableExpressionExecutor = ExpressionParser.parseExpression(variable, matchingMetaInfoHolder.getMetaStateEvent(), streamEventChainIndex, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
variableExpressionExecutorMap.put(id, variableExpressionExecutor);
}
expressionVisitor.endVisitStreamVariable(id, variable.getStreamId(), variable.getAttributeName(), type);
}
use of io.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.
the class AbstractRecordTable method compileUpdateSet.
public CompiledUpdateSet compileUpdateSet(UpdateSet updateSet, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
RecordTableCompiledUpdateSet recordTableCompiledUpdateSet = new RecordTableCompiledUpdateSet();
Map<String, ExpressionExecutor> parentExecutorMap = new HashMap<>();
for (UpdateSet.SetAttribute setAttribute : updateSet.getSetAttributeList()) {
ExpressionExecutor inMemoryAssignmentExecutor = ExpressionParser.parseExpression(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
ExpressionBuilder expressionBuilder = new ExpressionBuilder(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, null, inMemoryAssignmentExecutor, siddhiQueryContext);
CompiledExpression compiledExpression = compileSetAttribute(expressionBuilder);
recordTableCompiledUpdateSet.put(setAttribute.getTableVariable().getAttributeName(), compiledExpression);
Map<String, ExpressionExecutor> expressionExecutorMap = expressionBuilder.getVariableExpressionExecutorMap();
parentExecutorMap.putAll(expressionExecutorMap);
}
recordTableCompiledUpdateSet.setExpressionExecutorMap(parentExecutorMap);
return recordTableCompiledUpdateSet;
}
use of io.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.
the class QueryParserHelper method updateVariablePosition.
public static void updateVariablePosition(MetaComplexEvent metaComplexEvent, List<VariableExpressionExecutor> variableExpressionExecutorList) {
for (VariableExpressionExecutor variableExpressionExecutor : variableExpressionExecutorList) {
int streamEventChainIndex = variableExpressionExecutor.getPosition()[STREAM_EVENT_CHAIN_INDEX];
if (streamEventChainIndex == HAVING_STATE) {
if (metaComplexEvent instanceof MetaStreamEvent) {
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = OUTPUT_DATA_INDEX;
} else {
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = STATE_OUTPUT_DATA_INDEX;
}
variableExpressionExecutor.getPosition()[STREAM_EVENT_CHAIN_INDEX] = UNKNOWN_STATE;
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_INDEX_IN_TYPE] = metaComplexEvent.getOutputStreamDefinition().getAttributeList().indexOf(variableExpressionExecutor.getAttribute());
continue;
} else if (metaComplexEvent instanceof MetaStreamEvent && streamEventChainIndex >= 1) {
// VariableExpressionExecutor on Event table
continue;
} else if (metaComplexEvent instanceof MetaStateEvent && streamEventChainIndex >= ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents().length) {
// for VariableExpressionExecutor on Event table
continue;
}
MetaStreamEvent metaStreamEvent;
if (metaComplexEvent instanceof MetaStreamEvent) {
metaStreamEvent = (MetaStreamEvent) metaComplexEvent;
} else {
metaStreamEvent = ((MetaStateEvent) metaComplexEvent).getMetaStreamEvent(streamEventChainIndex);
}
if (metaStreamEvent.getOutputData().contains(variableExpressionExecutor.getAttribute())) {
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = OUTPUT_DATA_INDEX;
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_INDEX_IN_TYPE] = metaStreamEvent.getOutputData().indexOf(variableExpressionExecutor.getAttribute());
} else if (metaStreamEvent.getOnAfterWindowData().contains(variableExpressionExecutor.getAttribute())) {
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = ON_AFTER_WINDOW_DATA_INDEX;
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_INDEX_IN_TYPE] = metaStreamEvent.getOnAfterWindowData().indexOf(variableExpressionExecutor.getAttribute());
} else if (metaStreamEvent.getBeforeWindowData().contains(variableExpressionExecutor.getAttribute())) {
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = BEFORE_WINDOW_DATA_INDEX;
variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_INDEX_IN_TYPE] = metaStreamEvent.getBeforeWindowData().indexOf(variableExpressionExecutor.getAttribute());
}
}
}
use of io.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.
the class AbstractQueryableRecordTable method compileSelection.
public CompiledSelection compileSelection(Selector selector, List<Attribute> expectedOutputAttributes, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
selectorForTestOnDemandQuery = selector;
siddhiQueryContextForTestOnDemandQuery = siddhiQueryContext;
matchingMetaInfoHolderForTestOnDemandQuery = matchingMetaInfoHolder;
List<OutputAttribute> outputAttributes = selector.getSelectionList();
if (outputAttributes.size() == 0) {
MetaStreamEvent metaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(matchingMetaInfoHolder.getStoreEventIndex());
List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
}
}
List<SelectAttributeBuilder> selectAttributeBuilders = new ArrayList<>(outputAttributes.size());
for (OutputAttribute outputAttribute : outputAttributes) {
ExpressionBuilder expressionBuilder = new ExpressionBuilder(outputAttribute.getExpression(), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
selectAttributeBuilders.add(new SelectAttributeBuilder(expressionBuilder, outputAttribute.getRename()));
}
MatchingMetaInfoHolder metaInfoHolderAfterSelect = new MatchingMetaInfoHolder(matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getMatchingStreamEventIndex(), matchingMetaInfoHolder.getStoreEventIndex(), matchingMetaInfoHolder.getMatchingStreamDefinition(), matchingMetaInfoHolder.getMatchingStreamDefinition(), matchingMetaInfoHolder.getCurrentState());
List<ExpressionBuilder> groupByExpressionBuilders = null;
if (selector.getGroupByList().size() != 0) {
groupByExpressionBuilders = new ArrayList<>(outputAttributes.size());
for (Variable variable : selector.getGroupByList()) {
groupByExpressionBuilders.add(new ExpressionBuilder(variable, metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext));
}
}
ExpressionBuilder havingExpressionBuilder = null;
if (selector.getHavingExpression() != null) {
havingExpressionBuilder = new ExpressionBuilder(selector.getHavingExpression(), metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
}
List<OrderByAttributeBuilder> orderByAttributeBuilders = null;
if (selector.getOrderByList().size() != 0) {
orderByAttributeBuilders = new ArrayList<>(selector.getOrderByList().size());
for (OrderByAttribute orderByAttribute : selector.getOrderByList()) {
ExpressionBuilder expressionBuilder = new ExpressionBuilder(orderByAttribute.getVariable(), metaInfoHolderAfterSelect, variableExpressionExecutors, tableMap, null, null, siddhiQueryContext);
orderByAttributeBuilders.add(new OrderByAttributeBuilder(expressionBuilder, orderByAttribute.getOrder()));
}
}
Long limit = null;
Long offset = null;
if (selector.getLimit() != null) {
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), metaInfoHolderAfterSelect.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
limit = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
if (limit < 0) {
throw new SiddhiAppCreationException("'limit' cannot have negative value, but found '" + limit + "'", selector, siddhiQueryContext.getSiddhiAppContext());
}
}
if (selector.getOffset() != null) {
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getOffset(), metaInfoHolderAfterSelect.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
offset = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
if (offset < 0) {
throw new SiddhiAppCreationException("'offset' cannot have negative value, but found '" + offset + "'", selector, siddhiQueryContext.getSiddhiAppContext());
}
}
CompiledSelection compiledSelection = compileSelection(selectAttributeBuilders, groupByExpressionBuilders, havingExpressionBuilder, orderByAttributeBuilders, limit, offset);
Map<String, ExpressionExecutor> expressionExecutorMap = new HashMap<>();
if (selectAttributeBuilders.size() != 0) {
for (SelectAttributeBuilder selectAttributeBuilder : selectAttributeBuilders) {
expressionExecutorMap.putAll(selectAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
}
}
if (groupByExpressionBuilders != null && groupByExpressionBuilders.size() != 0) {
for (ExpressionBuilder groupByExpressionBuilder : groupByExpressionBuilders) {
expressionExecutorMap.putAll(groupByExpressionBuilder.getVariableExpressionExecutorMap());
}
}
if (havingExpressionBuilder != null) {
expressionExecutorMap.putAll(havingExpressionBuilder.getVariableExpressionExecutorMap());
}
if (orderByAttributeBuilders != null && orderByAttributeBuilders.size() != 0) {
for (OrderByAttributeBuilder orderByAttributeBuilder : orderByAttributeBuilders) {
expressionExecutorMap.putAll(orderByAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
}
}
if (cacheEnabled) {
CompiledSelectionWithCache compiledSelectionWithCache;
MetaStateEvent metaStateEventForCacheSelection = matchingMetaInfoHolder.getMetaStateEvent().clone();
ReturnStream returnStream = new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS);
int metaPosition = SiddhiConstants.UNKNOWN_STATE;
List<VariableExpressionExecutor> variableExpressionExecutorsForQuerySelector = new ArrayList<>();
QuerySelector querySelector = SelectorParser.parse(selector, returnStream, metaStateEventForCacheSelection, tableMap, variableExpressionExecutorsForQuerySelector, metaPosition, ProcessingMode.BATCH, false, siddhiQueryContext);
if (matchingMetaInfoHolder.getMetaStateEvent().getOutputDataAttributes().size() == 0) {
for (MetaStateEventAttribute outputDataAttribute : metaStateEventForCacheSelection.getOutputDataAttributes()) {
matchingMetaInfoHolder.getMetaStateEvent().addOutputDataAllowingDuplicate(outputDataAttribute);
}
}
QueryParserHelper.updateVariablePosition(metaStateEventForCacheSelection, variableExpressionExecutorsForQuerySelector);
querySelector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(metaStateEventForCacheSelection));
RecordStoreCompiledSelection recordStoreCompiledSelection = new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
compiledSelectionWithCache = new CompiledSelectionWithCache(recordStoreCompiledSelection, querySelector, metaStateEventForCacheSelection, matchingMetaInfoHolder.getStoreEventIndex(), variableExpressionExecutorsForQuerySelector);
return compiledSelectionWithCache;
} else {
return new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
}
}
use of io.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.
the class AbstractQueryableRecordTable method initCache.
@Override
public void initCache(TableDefinition tableDefinition, SiddhiAppContext siddhiAppContext, StreamEventCloner storeEventCloner, ConfigReader configReader) {
String[] annotationNames = { ANNOTATION_STORE, ANNOTATION_CACHE };
Annotation cacheTableAnnotation = getAnnotation(annotationNames, tableDefinition.getAnnotations());
if (cacheTableAnnotation != null) {
cacheEnabled = true;
maxCacheSize = Integer.parseInt(cacheTableAnnotation.getElement(CACHE_TABLE_SIZE));
TableDefinition cacheTableDefinition = TableDefinition.id(tableDefinition.getId());
for (Attribute attribute : tableDefinition.getAttributeList()) {
cacheTableDefinition.attribute(attribute.getName(), attribute.getType());
}
for (Annotation annotation : tableDefinition.getAnnotations()) {
if (!annotation.getName().equalsIgnoreCase("Store")) {
cacheTableDefinition.annotation(annotation);
}
}
String cachePolicy = cacheTableAnnotation.getElement(ANNOTATION_CACHE_POLICY);
if (cachePolicy == null || cachePolicy.equalsIgnoreCase("FIFO")) {
cachePolicy = "FIFO";
cacheTable = new CacheTableFIFO();
} else if (cachePolicy.equalsIgnoreCase("LRU")) {
cacheTable = new CacheTableLRU();
} else if (cachePolicy.equalsIgnoreCase("LFU")) {
cacheTable = new CacheTableLFU();
} else {
throw new SiddhiAppCreationException(siddhiAppContext.getName() + " : Cache policy can only be one " + "of FIFO, LRU, and LFU but given as " + cachePolicy);
}
// check if cache expiry enabled and initialize relevant parameters
if (cacheTableAnnotation.getElement(ANNOTATION_CACHE_RETENTION_PERIOD) != null) {
cacheExpiryEnabled = true;
retentionPeriod = Expression.Time.timeToLong(cacheTableAnnotation.getElement(ANNOTATION_CACHE_RETENTION_PERIOD));
if (cacheTableAnnotation.getElement(ANNOTATION_CACHE_PURGE_INTERVAL) == null) {
purgeInterval = retentionPeriod;
} else {
purgeInterval = Expression.Time.timeToLong(cacheTableAnnotation.getElement(ANNOTATION_CACHE_PURGE_INTERVAL));
}
storeSizeCheckInterval = purgeInterval * 5;
} else {
storeSizeCheckInterval = 10000;
}
((CacheTable) cacheTable).initCacheTable(cacheTableDefinition, configReader, siddhiAppContext, recordTableHandler, cacheExpiryEnabled, maxCacheSize, cachePolicy);
// creating objects needed to load cache
SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext, CACHE_QUERY_NAME + tableDefinition.getId());
MatchingMetaInfoHolder matchingMetaInfoHolder = generateMatchingMetaInfoHolderForCacheTable(tableDefinition);
OnDemandQuery onDemandQuery = OnDemandQuery.query().from(InputStore.store(tableDefinition.getId())).select(Selector.selector().limit(Expression.value((maxCacheSize + 1))));
List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
compiledConditionForCaching = compileCondition(Expression.value(true), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
List<Attribute> expectedOutputAttributes = buildExpectedOutputAttributes(onDemandQuery, tableMap, SiddhiConstants.UNKNOWN_STATE, matchingMetaInfoHolder, siddhiQueryContext);
compiledSelectionForCaching = compileSelection(onDemandQuery.getSelector(), expectedOutputAttributes, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
outputAttributesForCaching = expectedOutputAttributes.toArray(new Attribute[0]);
QueryParserHelper.reduceMetaComplexEvent(matchingMetaInfoHolder.getMetaStateEvent());
QueryParserHelper.updateVariablePosition(matchingMetaInfoHolder.getMetaStateEvent(), variableExpressionExecutors);
compiledSelectionForSelectAll = generateCSForSelectAll();
}
}
Aggregations