All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fluxtion.runtime.dataflow.groupby.GroupByTumblingWindow Maven / Gradle / Ivy

There is a newer version: 9.4.4
Show newest version
package com.fluxtion.runtime.dataflow.groupby;

import com.fluxtion.runtime.annotations.NoTriggerReference;
import com.fluxtion.runtime.annotations.OnParentUpdate;
import com.fluxtion.runtime.annotations.OnTrigger;
import com.fluxtion.runtime.annotations.builder.SepNode;
import com.fluxtion.runtime.dataflow.FlowFunction;
import com.fluxtion.runtime.dataflow.TriggeredFlowFunction;
import com.fluxtion.runtime.dataflow.aggregate.AggregateFlowFunction;
import com.fluxtion.runtime.dataflow.function.AbstractFlowFunction;
import com.fluxtion.runtime.partition.LambdaReflection.SerializableFunction;
import com.fluxtion.runtime.partition.LambdaReflection.SerializableSupplier;
import com.fluxtion.runtime.time.FixedRateTrigger;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


/**
 * @param  Input type
 * @param  Output of aggregate function
 * @param  Key type from input T
 * @param  Value type from input T, input to aggregate function
 * @param  {@link FlowFunction} input type
 * @param 
 */
public class GroupByTumblingWindow, F extends AggregateFlowFunction>
        extends AbstractFlowFunction, S>
        implements TriggeredFlowFunction> {

    @SepNode
    @NoTriggerReference
    public GroupByFlowFunctionWrapper groupByWindowedCollection;
    public FixedRateTrigger rollTrigger;

    private transient final Map mapOfValues = new HashMap<>();
    private transient final MyGroupBy results = new MyGroupBy();

    public GroupByTumblingWindow(
            S inputEventStream,
            SerializableSupplier windowFunctionSupplier,
            SerializableFunction keyFunction,
            SerializableFunction valueFunction,
            int windowSizeMillis) {
        this(inputEventStream);
        this.groupByWindowedCollection = new GroupByFlowFunctionWrapper<>(keyFunction, valueFunction, windowFunctionSupplier);
        rollTrigger = FixedRateTrigger.atMillis(windowSizeMillis);
    }

    public GroupByTumblingWindow(S inputEventStream) {
        super(inputEventStream, null);
    }

    @Override
    public GroupBy get() {
        return results;
    }

    protected void cacheWindowValue() {
        mapOfValues.clear();
        mapOfValues.putAll(groupByWindowedCollection.toMap());
    }

    protected void aggregateInputValue(S inputEventStream) {
        groupByWindowedCollection.aggregate(inputEventStream.get());
    }

    @OnParentUpdate
    public void timeTriggerFired(FixedRateTrigger rollTrigger) {
        if (rollTrigger.getTriggerCount() == 1) {
            cacheWindowValue();
        }
        publishOverrideTriggered = !overridePublishTrigger & !overrideUpdateTrigger;
        inputStreamTriggered_1 = true;
        inputStreamTriggered = true;
        groupByWindowedCollection.reset();
        if (rollTrigger.getTriggerCount() != 1) {
            cacheWindowValue();
        }
    }

    @OnParentUpdate
    public void inputUpdated(S inputEventStream) {
        aggregateInputValue(inputEventStream);
        inputStreamTriggered_1 = false;
        inputStreamTriggered = false;
    }

    @OnParentUpdate("updateTriggerNode")
    public void updateTriggerNodeUpdated(Object triggerNode) {
        super.updateTriggerNodeUpdated(triggerNode);
        cacheWindowValue();
    }

    @Override
    protected void resetOperation() {
        mapOfValues.clear();
        groupByWindowedCollection.reset();
    }

    @Override
    public boolean isStatefulFunction() {
        return true;
    }

    @OnTrigger
    public boolean triggered() {
        return fireEventUpdateNotification();
    }


    private class MyGroupBy implements GroupBy {

        @Override
        public Map toMap() {
            return mapOfValues;
        }

        @Override
        public Collection values() {
            return mapOfValues.values();
        }

        @Override
        public R lastValue() {
            return groupByWindowedCollection.lastValue();
        }

        @Override
        public KeyValue lastKeyValue() {
            return groupByWindowedCollection.lastKeyValue();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy