Search in sources :

Example 6 with Continuation

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();
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Continuation(com.oath.cyclops.types.futurestream.Continuation) Signal(com.oath.cyclops.async.adapters.Signal)

Example 7 with Continuation

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());
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 8 with Continuation

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();
    });
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) PrintStream(java.io.PrintStream)

Example 9 with Continuation

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();
    });
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) PrintStream(java.io.PrintStream)

Example 10 with Continuation

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];
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) SimpleReactProcessingException(com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException)

Aggregations

Continuation (com.oath.cyclops.types.futurestream.Continuation)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ClosedQueueException (com.oath.cyclops.async.adapters.Queue.ClosedQueueException)3 Subscription (org.reactivestreams.Subscription)3 Signal (com.oath.cyclops.async.adapters.Signal)2 Counter (com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter)2 PrintStream (java.io.PrintStream)2 Publisher (org.reactivestreams.Publisher)2 com.oath.cyclops.async (com.oath.cyclops.async)1 com.oath.cyclops.async.adapters (com.oath.cyclops.async.adapters)1 Queue (com.oath.cyclops.async.adapters.Queue)1 Topic (com.oath.cyclops.async.adapters.Topic)1 DataWitness.reactiveSeq (com.oath.cyclops.hkt.DataWitness.reactiveSeq)1 Higher (com.oath.cyclops.hkt.Higher)1 FastFuture (com.oath.cyclops.internal.react.async.future.FastFuture)1 SimpleReactProcessingException (com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException)1 OneShotStreamX (com.oath.cyclops.internal.stream.OneShotStreamX)1 com.oath.cyclops.internal.stream.spliterators (com.oath.cyclops.internal.stream.spliterators)1 ReversingDoubleArraySpliterator (com.oath.cyclops.internal.stream.spliterators.doubles.ReversingDoubleArraySpliterator)1 ReversingIntArraySpliterator (com.oath.cyclops.internal.stream.spliterators.ints.ReversingIntArraySpliterator)1