use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class SpliteratorBasedStream method changes.
public ReactiveSeq<T> changes() {
return ReactiveSeq.defer(() -> {
com.oath.cyclops.async.adapters.Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build();
Spliterator<T> copy = copy();
Continuation[] contRef = { null };
Signal<T> signal = new Signal<T>(null, queue);
AtomicBoolean wip = new AtomicBoolean(false);
Continuation cont = new Continuation(() -> {
if (wip.compareAndSet(false, true)) {
if (!copy.tryAdvance(signal::set)) {
signal.close();
return Continuation.empty();
}
wip.set(false);
}
return contRef[0];
});
contRef[0] = cont;
queue.addContinuation(cont);
return signal.getDiscrete().stream();
});
}
use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class ReactiveSeq method foldParallel.
default <R> R foldParallel(Function<? super Stream<T>, ? extends R> fn) {
Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build().withTimeout(1);
AtomicReference<Continuation> ref = new AtomicReference<>(null);
Continuation cont = new Continuation(() -> {
if (ref.get() == null && ref.compareAndSet(null, Continuation.empty())) {
try {
// use the first consuming thread to tell this Stream onto the Queue
this.spliterator().forEachRemaining(queue::offer);
} finally {
queue.close();
}
}
return Continuation.empty();
});
;
queue.addContinuation(cont);
return fn.apply(queue.jdkStream().parallel());
}
use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class ReactiveSeq method parallel.
default <R> ReactiveSeq<R> parallel(ForkJoinPool fj, Function<? super Stream<T>, ? extends Stream<? extends R>> fn) {
return defer(() -> {
Queue<R> queue = QueueFactories.<R>unboundedNonBlockingQueue().build();
ReactiveSeq<? extends Iterator<? extends R>> stream = ReactiveSeq.<Stream<? extends R>>generate(() -> foldParallel(fj, fn)).take(1).map(s -> s.iterator());
Iterator[] it = { null };
Continuation[] store = { null };
Continuation cont = new Continuation(() -> {
if (it[0] == null)
it[0] = stream.asFunction().apply(0l);
Iterator<R> local = it[0];
try {
if (!local.hasNext()) {
queue.close();
return Continuation.empty();
} else {
queue.offer(local.next());
}
} catch (Throwable t) {
queue.close();
throw ExceptionSoftener.throwSoftenedException(t);
}
return store[0];
});
;
store[0] = cont;
queue.addContinuation(cont);
return queue.stream();
});
}
use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class ReactiveSeq method parallel.
default <R> ReactiveSeq<R> parallel(Function<? super Stream<T>, ? extends Stream<? extends R>> fn) {
return defer(() -> {
Queue<R> queue = QueueFactories.<R>unboundedNonBlockingQueue().build();
ReactiveSeq<Iterator<? extends R>> stream = ReactiveSeq.<Stream<? extends R>>generate(() -> foldParallel(fn)).take(1).map(s -> s.iterator());
Iterator[] it = { null };
Continuation[] store = { null };
Continuation cont = new Continuation(() -> {
if (it[0] == null)
it[0] = stream.asFunction().apply(0l);
Iterator<R> local = it[0];
try {
if (!local.hasNext()) {
queue.close();
return Continuation.empty();
} else {
queue.offer(local.next());
}
} catch (Throwable t) {
queue.close();
throw ExceptionSoftener.throwSoftenedException(t);
}
return store[0];
});
;
store[0] = cont;
queue.addContinuation(cont);
return queue.stream();
});
}
use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class Runner method runContinuations.
public Continuation runContinuations(final LazyStreamWrapper lastActive, final EmptyCollector collector, boolean blocking) {
final Iterator<FastFuture> it = lastActive.injectFutures().iterator();
final Continuation[] cont = new Continuation[1];
final Continuation finish = new Continuation(() -> {
collector.afterResults(() -> {
runnable.run();
throw new ClosedQueueException();
});
return Continuation.empty();
});
final Continuation blockingFinish = new Continuation(() -> {
collector.getResults();
runnable.run();
throw new ClosedQueueException();
});
final Continuation finishNoCollect = new Continuation(() -> {
runnable.run();
throw new ClosedQueueException();
});
cont[0] = new Continuation(() -> {
try {
if (it.hasNext()) {
final FastFuture f = it.next();
// if FastFuture has been filtered out, we need to move to the next one instead
handleFilter(cont, f);
collector.accept(f);
}
if (it.hasNext())
return cont[0];
else {
return blocking ? blockingFinish.proceed() : finish.proceed();
}
} catch (final SimpleReactProcessingException e) {
} catch (final java.util.concurrent.CompletionException e) {
} catch (final Throwable e) {
collector.getSafeJoin().apply(FastFuture.failedFuture(e));
}
return finishNoCollect;
});
return cont[0];
}
Aggregations