Search in sources :

Example 66 with Attribute

use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class CacheTable method initCacheTable.

public void initCacheTable(TableDefinition cacheTableDefinition, ConfigReader configReader, SiddhiAppContext siddhiAppContext, RecordTableHandler recordTableHandler, boolean cacheExpiryEnabled, int maxSize, String cachePolicy) {
    this.maxSize = maxSize;
    this.cacheExpiryEnabled = cacheExpiryEnabled;
    this.siddhiAppContext = siddhiAppContext;
    addRequiredFieldsToCacheTableDefinition(cacheTableDefinition, cacheExpiryEnabled);
    // initialize cache table
    MetaStreamEvent cacheTableMetaStreamEvent = new MetaStreamEvent();
    cacheTableMetaStreamEvent.addInputDefinition(cacheTableDefinition);
    for (Attribute attribute : cacheTableDefinition.getAttributeList()) {
        cacheTableMetaStreamEvent.addOutputData(attribute);
    }
    StreamEventFactory cacheTableStreamEventFactory = new StreamEventFactory(cacheTableMetaStreamEvent);
    StreamEventCloner cacheTableStreamEventCloner = new StreamEventCloner(cacheTableMetaStreamEvent, cacheTableStreamEventFactory);
    super.initTable(cacheTableDefinition, cacheTableStreamEventFactory, cacheTableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
}
Also used : Attribute(io.siddhi.query.api.definition.Attribute) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 67 with Attribute

use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class DistinctCountIncrementalAttributeAggregator method init.

@Override
public void init(String attributeName, Attribute.Type attributeType) {
    Attribute set;
    Expression setInitialValue;
    // distinct-count is not supported for object types.
    if (attributeType.equals(Attribute.Type.FLOAT) || attributeType.equals(Attribute.Type.DOUBLE) || attributeType.equals(Attribute.Type.INT) || attributeType.equals(Attribute.Type.LONG) || attributeType.equals(Attribute.Type.STRING) || attributeType.equals(Attribute.Type.BOOL)) {
        set = new Attribute("AGG_SET_".concat(attributeName), Attribute.Type.OBJECT);
        setInitialValue = Expression.function("createSet", Expression.variable(attributeName));
    } else {
        throw new SiddhiAppRuntimeException("Distinct count aggregation cannot be executed on attribute type " + attributeType.toString());
    }
    this.baseAttributes = new Attribute[] { set };
    this.baseAttributesInitialValues = new Expression[] { setInitialValue };
}
Also used : ReturnAttribute(io.siddhi.annotation.ReturnAttribute) Attribute(io.siddhi.query.api.definition.Attribute) Expression(io.siddhi.query.api.expression.Expression) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException)

Example 68 with Attribute

use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class SumIncrementalAttributeAggregator method init.

@Override
public void init(String attributeName, Attribute.Type attributeType) {
    Attribute sum;
    Expression sumInitialValue;
    if (attributeName == null) {
        throw new SiddhiAppCreationException("Sum incremental attribute aggregation cannot be executed " + "when no parameters are given");
    }
    if (attributeType.equals(Attribute.Type.FLOAT) || attributeType.equals(Attribute.Type.DOUBLE)) {
        sum = new Attribute("AGG_SUM_".concat(attributeName), Attribute.Type.DOUBLE);
        sumInitialValue = Expression.function("convert", Expression.variable(attributeName), Expression.value("double"));
        returnType = Attribute.Type.DOUBLE;
    } else if (attributeType.equals(Attribute.Type.INT) || attributeType.equals(Attribute.Type.LONG)) {
        sum = new Attribute("AGG_SUM_".concat(attributeName), Attribute.Type.LONG);
        sumInitialValue = Expression.function("convert", Expression.variable(attributeName), Expression.value("long"));
        returnType = Attribute.Type.LONG;
    } else {
        throw new SiddhiAppRuntimeException("Sum aggregation cannot be executed on attribute type " + attributeType.toString());
    }
    this.baseAttributes = new Attribute[] { sum };
    // Original attribute names
    this.baseAttributesInitialValues = new Expression[] { sumInitialValue };
// used for initial values, since those would be executed using original meta
}
Also used : ReturnAttribute(io.siddhi.annotation.ReturnAttribute) Attribute(io.siddhi.query.api.definition.Attribute) Expression(io.siddhi.query.api.expression.Expression) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException)

Example 69 with Attribute

use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class AbstractStreamProcessor method initProcessor.

public void initProcessor(MetaStreamEvent metaStreamEvent, ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, boolean findToBeExecuted, boolean groupBy, SiddhiElement siddhiElement, SiddhiQueryContext siddhiQueryContext) {
    this.metaStreamEvent = metaStreamEvent;
    this.siddhiQueryContext = siddhiQueryContext;
    try {
        this.inputDefinition = metaStreamEvent.getLastInputDefinition();
        this.attributeExpressionExecutors = attributeExpressionExecutors;
        this.attributeExpressionLength = attributeExpressionExecutors.length;
        InputParameterValidator.validateExpressionExecutors(this, attributeExpressionExecutors);
        StateFactory<S> stateFactory = init(metaStreamEvent, metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, streamEventClonerHolder, outputExpectsExpiredEvents, findToBeExecuted, siddhiQueryContext);
        this.additionalAttributes = getReturnAttributes();
        this.stateHolder = siddhiQueryContext.generateStateHolder(this.getClass().getName(), groupBy, stateFactory);
        siddhiQueryContext.getSiddhiAppContext().addEternalReferencedHolder(this);
        if (additionalAttributes.size() > 0) {
            StreamDefinition outputDefinition = StreamDefinition.id(inputDefinition.getId());
            outputDefinition.setQueryContextStartIndex(siddhiElement.getQueryContextStartIndex());
            outputDefinition.setQueryContextEndIndex(siddhiElement.getQueryContextEndIndex());
            for (Attribute attribute : inputDefinition.getAttributeList()) {
                outputDefinition.attribute(attribute.getName(), attribute.getType());
            }
            for (Attribute attribute : additionalAttributes) {
                outputDefinition.attribute(attribute.getName(), attribute.getType());
            }
            metaStreamEvent.addInputDefinition(outputDefinition);
        }
    } catch (Throwable t) {
        throw new SiddhiAppCreationException(t.getMessage(), t, siddhiElement.getQueryContextStartIndex(), siddhiElement.getQueryContextEndIndex(), siddhiQueryContext.getSiddhiAppContext());
    }
}
Also used : StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) Attribute(io.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException)

Example 70 with Attribute

use of io.siddhi.query.api.definition.Attribute in project siddhi by wso2.

the class SnapshotableEventQueueTestCase method incrementalPersistenceTest4.

@Test
public void incrementalPersistenceTest4() throws InterruptedException, IOException, ClassNotFoundException {
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addOutputData(new Attribute("symbol", Attribute.Type.STRING));
    metaStreamEvent.addOutputData(new Attribute("price", Attribute.Type.FLOAT));
    metaStreamEvent.addOutputData(new Attribute("volume", Attribute.Type.LONG));
    StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, new StreamEventFactory(metaStreamEvent));
    SnapshotableStreamEventQueue snapshotableStreamEventQueue = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
    StreamEvent streamEvent = new StreamEvent(metaStreamEvent.getBeforeWindowData().size(), metaStreamEvent.getOnAfterWindowData().size(), metaStreamEvent.getOutputData().size());
    streamEvent.setOutputData(new Object[] { "IBM", 500.6f, 1 });
    for (int i = 0; i < 10; i++) {
        streamEvent.getOutputData()[2] = i;
        snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
    }
    HashMap<Long, String> snapshots = new HashMap<>();
    Snapshot snapshot1 = snapshotableStreamEventQueue.getSnapshot();
    StreamEvent streamEvents = (StreamEvent) snapshot1.getState();
    Assert.assertTrue(streamEvents != null);
    snapshots.put(3L, toString(snapshot1));
    snapshotableStreamEventQueue.poll();
    snapshotableStreamEventQueue.poll();
    snapshotableStreamEventQueue.next();
    snapshotableStreamEventQueue.next();
    snapshotableStreamEventQueue.next();
    for (int i = 7; i < 10; i++) {
        snapshotableStreamEventQueue.poll();
    }
    Snapshot snapshot2 = snapshotableStreamEventQueue.getSnapshot();
    ArrayList<Operation> operationLog = (ArrayList<Operation>) snapshot2.getState();
    Assert.assertTrue(operationLog != null);
    snapshots.put(4L, toString(snapshot2));
    for (int i = 10; i < 15; i++) {
        streamEvent.getOutputData()[2] = i;
        snapshotableStreamEventQueue.add(streamEventCloner.copyStreamEvent(streamEvent));
    }
    Snapshot snapshot3 = snapshotableStreamEventQueue.getSnapshot();
    operationLog = (ArrayList<Operation>) snapshot3.getState();
    Assert.assertTrue(operationLog != null);
    snapshots.put(5L, toString(snapshot3));
    SnapshotableStreamEventQueue snapshotableStreamEventQueue2 = new SnapshotableStreamEventQueue(new StreamEventClonerHolder(streamEventCloner));
    SnapshotStateList snapshotStateList = new SnapshotStateList();
    for (Map.Entry<Long, String> entry : snapshots.entrySet()) {
        snapshotStateList.putSnapshotState(entry.getKey(), (Snapshot) fromString(entry.getValue()));
    }
    snapshotableStreamEventQueue2.restore(snapshotStateList);
    Assert.assertEquals(snapshotableStreamEventQueue, snapshotableStreamEventQueue2);
}
Also used : Attribute(io.siddhi.query.api.definition.Attribute) HashMap(java.util.HashMap) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) ArrayList(java.util.ArrayList) Operation(io.siddhi.core.event.stream.Operation) StreamEventClonerHolder(io.siddhi.core.event.stream.holder.StreamEventClonerHolder) Snapshot(io.siddhi.core.util.snapshot.state.Snapshot) SnapshotStateList(io.siddhi.core.util.snapshot.state.SnapshotStateList) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) HashMap(java.util.HashMap) Map(java.util.Map) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) SnapshotableStreamEventQueue(io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue) Test(org.testng.annotations.Test)

Aggregations

Attribute (io.siddhi.query.api.definition.Attribute)86 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)43 ArrayList (java.util.ArrayList)29 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)27 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)23 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)23 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)22 StreamEvent (io.siddhi.core.event.stream.StreamEvent)21 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)19 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)18 Variable (io.siddhi.query.api.expression.Variable)18 Expression (io.siddhi.query.api.expression.Expression)17 Test (org.testng.annotations.Test)17 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)15 HashMap (java.util.HashMap)15 StreamEventCloner (io.siddhi.core.event.stream.StreamEventCloner)14 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)14 TableDefinition (io.siddhi.query.api.definition.TableDefinition)13 Map (java.util.Map)13 Annotation (io.siddhi.query.api.annotation.Annotation)12