com.kolibrifx.plovercrest.server.streams.folds.LiveCombinatorStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plovercrest-server Show documentation
Show all versions of plovercrest-server Show documentation
Plovercrest server library.
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.Collection;
import com.kolibrifx.plovercrest.server.internal.folds.FoldCallback;
import com.kolibrifx.plovercrest.server.internal.folds.PeekableInputStream;
/**
* @see com.kolibrifx.plovercrest.server.folds.FoldCombinators#live()
*/
public class LiveCombinatorStrategy implements CombinatorStrategy {
@Override
public boolean tryConsumeNext(final Collection extends PeekableInputStream extends T>> feeds,
final FoldCallback super T> cb) {
if (feeds.isEmpty()) {
return false;
}
PeekableInputStream extends T> best = null;
for (final PeekableInputStream extends T> feed : feeds) {
if (!feed.hasNext()) {
// Missing data from one table. For this strategy, we continue searching.
// This ensures that (live) updates trigger events immediately, but can cause
// out-of-order timestamps.
continue;
}
if (best == null || feed.nextTimestamp() < best.nextTimestamp()) {
best = feed;
}
}
if (best == null) {
cb.onEnd();
return false;
}
assert best.hasNext();
best.consumeNext(cb);
return true;
}
}