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

com.kolibrifx.plovercrest.server.streams.folds.GenericFoldSetup Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2017, KolibriFX AS. Licensed under the Apache License, version 2.0.
 */

package com.kolibrifx.plovercrest.server.streams.folds;

import java.util.List;
import java.util.function.Supplier;
import com.kolibrifx.lancebill.folds.GenericFold;
import com.kolibrifx.plovercrest.server.internal.folds.FoldCallback;
import com.kolibrifx.plovercrest.server.internal.folds.FoldReaderInputs;
import com.kolibrifx.plovercrest.server.internal.folds.FoldReaderSetup;
import com.kolibrifx.plovercrest.server.internal.folds.FoldWriter;
import com.kolibrifx.plovercrest.server.streams.Stream;

public class GenericFoldSetup implements FoldSetup {
    private Supplier> foldSupplier;

    public GenericFoldSetup(final GenericFold fold) {
        this(new Supplier>() {
            @Override
            public GenericFold get() {
                return fold;
            }
        });
    }

    public GenericFoldSetup(final Supplier> foldSupplier) {
        this.foldSupplier = foldSupplier;
    }

    @Override
    public FoldReaderSetup setupReader(final long timestamp, final FoldWriter foldOutput,
                                          final List> inputStreams) {
        final FoldReaderInputs inputs = new FoldReaderInputs<>();
        long adjustedTimestamp = timestamp;
        for (final Stream input : inputStreams) {
            final long ts = input.seekPreviousTimestamp(timestamp);
            adjustedTimestamp = Math.max(adjustedTimestamp, ts);
        }
        inputs.addInputs(inputStreams, adjustedTimestamp);
        final GenericFold fold = foldSupplier.get();
        return new FoldReaderSetup() {
            @Override
            public FoldCallback getFoldCallback() {
                return new TakePreviousFoldCallbackAdapter<>(timestamp, foldOutput, fold);
            }

            @Override
            public FoldReaderInputs getInputs() {
                return inputs;
            }
        };
    }

    @Override
    public long getLastOutputTimestamp(final List> inputStreams,
                                       final CombinatorStrategy combinatorStrategy) {
        if (combinatorStrategy instanceof LiveCombinatorStrategy) {
            long result = Long.MIN_VALUE;
            for (final Stream stream : inputStreams) {
                result = Math.max(result, stream.getLastTimestamp());
            }
            return result;
        }
        if (combinatorStrategy instanceof HistoricalCombinatorStrategy) {
            long result = Long.MAX_VALUE;
            for (final Stream stream : inputStreams) {
                result = Math.min(result, stream.getLastTimestamp());
            }
            return result;
        }
        throw new IllegalStateException("Unknown combinator strategy " + combinatorStrategy);
    }
}