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

com.fluxtion.runtime.dataflow.aggregate.function.BucketedSlidingWindow Maven / Gradle / Ivy

package com.fluxtion.runtime.dataflow.aggregate.function;

import com.fluxtion.runtime.dataflow.aggregate.AggregateDoubleFlowFunction;
import com.fluxtion.runtime.dataflow.aggregate.AggregateFlowFunction;
import com.fluxtion.runtime.dataflow.aggregate.AggregateIntFlowFunction;
import com.fluxtion.runtime.dataflow.aggregate.AggregateLongFlowFunction;
import com.fluxtion.runtime.partition.LambdaReflection.SerializableSupplier;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
 * @param  Input type
 * @param  return type
 * @param  BaseSlidingWindowFunction
 */
public class BucketedSlidingWindow> {

    private final Supplier windowFunctionSupplier;
    protected final F aggregatedFunction;
    protected final F currentFunction;
    private final List buckets;
    private int writePointer;
    private boolean allBucketsFilled = false;
    private final boolean deductSupported;

    public BucketedSlidingWindow(Supplier windowFunctionSupplier, int numberOfBuckets) {
        this.windowFunctionSupplier = windowFunctionSupplier;
        aggregatedFunction = windowFunctionSupplier.get();
        currentFunction = windowFunctionSupplier.get();
        deductSupported = currentFunction.deductSupported();
        buckets = new ArrayList<>(numberOfBuckets);
        for (int i = 0; i < numberOfBuckets; i++) {
            buckets.add(windowFunctionSupplier.get());
        }
    }

    public void init() {
        aggregatedFunction.reset();
        currentFunction.reset();
        buckets.forEach(AggregateFlowFunction::reset);
    }

    public final void aggregate(T input) {
        currentFunction.aggregate(input);
    }

    public void roll() {
        roll(1);
    }

    public void roll(int windowsToRoll) {
        if (deductSupported) {
            for (int i = 0; i < windowsToRoll; i++) {
                F oldFunction = buckets.get(writePointer);
                aggregatedFunction.combine(currentFunction);
                aggregatedFunction.deduct(oldFunction);
                oldFunction.reset();
                oldFunction.combine(currentFunction);
                currentFunction.reset();
                writePointer++;
                allBucketsFilled = allBucketsFilled | writePointer == buckets.size();
                writePointer = writePointer % buckets.size();
            }
        } else {
            aggregatedFunction.reset();
            //clear and then combine
            for (int i = 0; i < windowsToRoll; i++) {
                F oldFunction = buckets.get(writePointer);
                oldFunction.reset();
                oldFunction.combine(currentFunction);
                currentFunction.reset();
                writePointer++;
                allBucketsFilled = allBucketsFilled | writePointer == buckets.size();
                writePointer = writePointer % buckets.size();
            }
            for (int i = 0; i < buckets.size(); i++) {
                aggregatedFunction.combine(buckets.get(i));
            }
        }
    }

    public boolean isAllBucketsFilled() {
        return allBucketsFilled;
    }

    public R get() {
        return aggregatedFunction.get();
    }

    public static class BucketedSlidingWindowedIntFunction>
            extends BucketedSlidingWindow {

        public BucketedSlidingWindowedIntFunction(SerializableSupplier windowFunctionSupplier, int numberOfBuckets) {
            super(windowFunctionSupplier, numberOfBuckets);
        }

        public void aggregateInt(int input) {
            currentFunction.aggregateInt(input);
        }

        public int getAsInt() {
            return aggregatedFunction.getAsInt();
        }
    }

    public static class BucketedSlidingWindowedDoubleFunction>
            extends BucketedSlidingWindow {

        public BucketedSlidingWindowedDoubleFunction(SerializableSupplier windowFunctionSupplier, int numberOfBuckets) {
            super(windowFunctionSupplier, numberOfBuckets);
        }

        public void aggregateDouble(double input) {
            currentFunction.aggregateDouble(input);
        }

        public double getAsDouble() {
            return aggregatedFunction.getAsDouble();
        }
    }

    public static class BucketedSlidingWindowedLongFunction>
            extends BucketedSlidingWindow {

        public BucketedSlidingWindowedLongFunction(SerializableSupplier windowFunctionSupplier, int numberOfBuckets) {
            super(windowFunctionSupplier, numberOfBuckets);
        }

        public void aggregateLong(long input) {
            currentFunction.aggregateLong(input);
        }

        public long getAsLong() {
            return aggregatedFunction.getAsLong();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy