Search in sources :

Example 11 with Continuation

use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.

the class ReactiveSeq method merge.

default ReactiveSeq<T> merge(Adapter<T>... adapters) {
    return defer(() -> {
        Publisher<T>[] publishers = ReactiveSeq.of(adapters).map(a -> a.stream()).toArray(n -> new Publisher[n]);
        final Counter c = new Counter();
        c.active.set(publishers.length + 1);
        final QueueBasedSubscriber<T> init = QueueBasedSubscriber.subscriber(QueueFactories.boundedQueue(5_000), c, publishers.length);
        final Supplier<Continuation> sp = () -> {
            backpressureAware().subscribe(init);
            for (final Publisher next : publishers) {
                next.subscribe(QueueBasedSubscriber.subscriber(init.getQueue(), c, publishers.length));
            }
            init.close();
            return Continuation.empty();
        };
        final Continuation continuation = new Continuation(sp);
        init.addContinuation(continuation);
        return ReactiveSeq.fromStream(init.jdkStream());
    });
}
Also used : PersistentCollection(com.oath.cyclops.types.persistent.PersistentCollection) Continuation(com.oath.cyclops.types.futurestream.Continuation) DataWitness.reactiveSeq(com.oath.cyclops.hkt.DataWitness.reactiveSeq) HashMap(cyclops.data.HashMap) Seq(cyclops.data.Seq) Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecoverableTraversable(com.oath.cyclops.types.traversable.RecoverableTraversable) ReversingLongArraySpliterator(com.oath.cyclops.internal.stream.spliterators.longs.ReversingLongArraySpliterator) Unit(com.oath.cyclops.types.factory.Unit) PrintWriter(java.io.PrintWriter) Contains(com.oath.cyclops.types.foldable.Contains) Monoid(cyclops.function.Monoid) com.oath.cyclops.async.adapters(com.oath.cyclops.async.adapters) ReversingDoubleArraySpliterator(com.oath.cyclops.internal.stream.spliterators.doubles.ReversingDoubleArraySpliterator) com.oath.cyclops.types.stream(com.oath.cyclops.types.stream) ReversingRangeLongSpliterator(com.oath.cyclops.internal.stream.spliterators.longs.ReversingRangeLongSpliterator) Queue(com.oath.cyclops.async.adapters.Queue) ReversingRangeIntSpliterator(com.oath.cyclops.internal.stream.spliterators.ints.ReversingRangeIntSpliterator) Function3(cyclops.function.Function3) Function4(cyclops.function.Function4) com.oath.cyclops.internal.stream.spliterators(com.oath.cyclops.internal.stream.spliterators) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) java.util.function(java.util.function) QueueBasedSubscriber(com.oath.cyclops.types.reactive.QueueBasedSubscriber) Tuple4(cyclops.data.tuple.Tuple4) Higher(com.oath.cyclops.hkt.Higher) To(com.oath.cyclops.types.foldable.To) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) java.util(java.util) ReversingIntArraySpliterator(com.oath.cyclops.internal.stream.spliterators.ints.ReversingIntArraySpliterator) java.util.stream(java.util.stream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Vector(cyclops.data.Vector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Tuple(cyclops.data.tuple.Tuple) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) com.oath.cyclops.async(com.oath.cyclops.async) Subscriber(org.reactivestreams.Subscriber) ReactiveTransformable(com.oath.cyclops.types.functor.ReactiveTransformable) OnEmptySwitch(com.oath.cyclops.types.recoverable.OnEmptySwitch) PrintStream(java.io.PrintStream) cyclops.control(cyclops.control) Streams(cyclops.companion.Streams) Executor(java.util.concurrent.Executor) Publisher(org.reactivestreams.Publisher) lombok.val(lombok.val) TimeUnit(java.util.concurrent.TimeUnit) Reducer(cyclops.function.Reducer) AtomicLong(java.util.concurrent.atomic.AtomicLong) ForkJoinPool(java.util.concurrent.ForkJoinPool) OneShotStreamX(com.oath.cyclops.internal.stream.OneShotStreamX) Curry(cyclops.function.Curry) Enumeration(cyclops.data.Enumeration) Continuation(com.oath.cyclops.types.futurestream.Continuation) Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter) Publisher(org.reactivestreams.Publisher)

Example 12 with Continuation

use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.

the class ReactiveSeq method broadcast.

default Topic<T> broadcast() {
    Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build().withTimeout(1);
    Topic<T> topic = new Topic<T>(queue, QueueFactories.<T>unboundedNonBlockingQueue());
    AtomicBoolean wip = new AtomicBoolean(false);
    Spliterator<T> split = this.spliterator();
    Continuation[] ref = { null };
    Continuation cont = new Continuation(() -> {
        if (wip.compareAndSet(false, true)) {
            try {
                // use the first consuming thread to tell this Stream onto the Queue
                if (!split.tryAdvance(topic::offer)) {
                    topic.close();
                    return Continuation.empty();
                }
            } finally {
                wip.set(false);
            }
        }
        return ref[0];
    });
    ref[0] = cont;
    queue.addContinuation(cont);
    return topic;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Continuation(com.oath.cyclops.types.futurestream.Continuation)

Example 13 with Continuation

use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.

the class ReactiveSeq method mergeP.

/**
 * A potentially asynchronous merge operation where data from each publisher may arrive out of order (if publishers
 * are configured to publish asynchronously.
 * The QueueFactory parameter can be used by pull based Streams to control the maximum queued elements @see {@link QueueFactories}
 * Push based reactive-streams signal demand via their subscription.
 */
// name will be refactored to merge in the future
@Deprecated
default ReactiveSeq<T> mergeP(final QueueFactory<T> factory, final Publisher<T>... publishers) {
    return defer(() -> {
        final Counter c = new Counter();
        c.active.set(publishers.length + 1);
        final QueueBasedSubscriber<T> init = QueueBasedSubscriber.subscriber(factory, c, publishers.length);
        final Supplier<Continuation> sp = () -> {
            subscribe(init);
            for (final Publisher next : publishers) {
                next.subscribe(QueueBasedSubscriber.subscriber(init.getQueue(), c, publishers.length));
            }
            init.close();
            return Continuation.empty();
        };
        final Continuation continuation = new Continuation(sp);
        init.addContinuation(continuation);
        return ReactiveSeq.fromStream(init.jdkStream());
    });
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter) Publisher(org.reactivestreams.Publisher)

Example 14 with Continuation

use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.

the class ReactiveStreamX method unwrapStream.

@Override
public Stream<T> unwrapStream() {
    if (async == Type.NO_BACKPRESSURE) {
        Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build();
        AtomicBoolean wip = new AtomicBoolean(false);
        Continuation cont = new Continuation(() -> {
            if (wip.compareAndSet(false, true)) {
                this.source.subscribeAll(queue::offer, i -> {
                    queue.close();
                }, () -> queue.close());
            }
            return Continuation.empty();
        });
        queue.addContinuation(cont);
        return queue.stream();
    }
    return StreamSupport.stream(new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).spliterator(), false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Continuation(com.oath.cyclops.types.futurestream.Continuation)

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