Search in sources :

Example 41 with Tuple2

use of cyclops.data.tuple.Tuple2 in project cyclops by aol.

the class ZippingLatestOperator method subscribeAll.

@Override
public void subscribeAll(Consumer<? super R> onNext, Consumer<? super Throwable> onError, Runnable onCompleteDs) {
    AtomicBoolean active = new AtomicBoolean(false);
    ManyToOneConcurrentArrayQueue<R> data = new ManyToOneConcurrentArrayQueue<R>(1024);
    AtomicReference<Tuple2<T1, T2>> nextValue = new AtomicReference<>(Tuple.tuple((T1) UNSET, (T2) UNSET));
    // left & right compelte can be merged into single integer
    AtomicBoolean leftComplete = new AtomicBoolean(false);
    AtomicBoolean rightComplete = new AtomicBoolean(false);
    left.subscribeAll(e -> {
        try {
            boolean set = false;
            while (!set) {
                Tuple2<T1, T2> local = nextValue.get();
                Tuple2<T1, T2> updated = local.map1(__ -> (T1) e);
                set = nextValue.compareAndSet(local, updated);
                if (set) {
                    if (updated._2() != UNSET) {
                        if (active.compareAndSet(false, true)) {
                            data.drain(onNext::accept);
                            onNext.accept(applyFn(updated));
                            active.set(false);
                        } else {
                            while (!data.offer(applyFn(updated))) {
                            }
                        }
                    }
                }
            }
        } catch (Throwable t) {
            onError.accept(t);
        }
    }, e -> {
        onError.accept(e);
    }, () -> {
        while (!active.compareAndSet(false, true)) {
        }
        data.drain(onNext::accept);
        if (rightComplete.get()) {
            onCompleteDs.run();
        }
        leftComplete.set(true);
        active.set(false);
    });
    right.subscribeAll(e -> {
        try {
            boolean set = false;
            while (!set) {
                Tuple2<T1, T2> local = nextValue.get();
                Tuple2<T1, T2> updated = local.map2(__ -> (T2) e);
                set = nextValue.compareAndSet(local, updated);
                if (set) {
                    if (updated._1() != UNSET) {
                        if (active.compareAndSet(false, true)) {
                            data.drain(onNext::accept);
                            onNext.accept(applyFn(updated));
                            active.set(false);
                        } else {
                            while (!data.offer(applyFn(updated))) {
                            }
                        }
                    }
                }
            }
        } catch (Throwable t) {
            onError.accept(t);
        }
    }, e -> {
        onError.accept(e);
    }, () -> {
        while (!active.compareAndSet(false, true)) {
        }
        data.drain(onNext::accept);
        if (leftComplete.get()) {
            onCompleteDs.run();
        }
        rightComplete.set(true);
        active.set(false);
    });
}
Also used : ManyToOneConcurrentArrayQueue(org.agrona.concurrent.ManyToOneConcurrentArrayQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(cyclops.data.tuple.Tuple2)

Example 42 with Tuple2

use of cyclops.data.tuple.Tuple2 in project cyclops by aol.

the class SpoutsZipTest method pairWiseIncremental.

@Test
public void pairWiseIncremental() {
    AtomicBoolean data = new AtomicBoolean(false);
    AtomicReference<Vector<Tuple2<Integer, String>>> values = new AtomicReference<>(Vector.empty());
    AtomicBoolean complete = new AtomicBoolean(false);
    AtomicReference<Throwable> error = new AtomicReference<Throwable>(null);
    Subscription sub = Spouts.of(1).zip(Spouts.of("test")).zip(Spouts.of("test2")).map(t -> Tuple.tuple(t._1()._1(), t._1()._2() + t._2())).forEach(0, n -> {
        data.set(true);
        values.updateAndGet(v -> v.plus(n));
    }, e -> {
        error.set(e);
    }, () -> {
        complete.set(true);
    });
    assertFalse(data.get());
    assertFalse(complete.get());
    assertNull(error.get());
    assertThat(values.get(), equalTo(Vector.empty()));
    sub.request(10l);
    assertTrue(data.get());
    assertTrue(complete.get());
    assertNull(error.get());
    assertThat(values.get(), equalTo(Vector.of(Tuple.tuple(1, "testtest2"))));
}
Also used : Tuple4(cyclops.data.tuple.Tuple4) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) Arrays(java.util.Arrays) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) StepVerifier(reactor.test.StepVerifier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Vector(cyclops.data.Vector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Seq(cyclops.data.Seq) Tuple(cyclops.data.tuple.Tuple) Matchers.contains(org.hamcrest.Matchers.contains) Subscription(org.reactivestreams.Subscription) EmitterProcessor(reactor.core.publisher.EmitterProcessor) Matchers.equalTo(org.hamcrest.Matchers.equalTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert(org.junit.Assert) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Vector(cyclops.data.Vector) Test(org.junit.Test)

Example 43 with Tuple2

use of cyclops.data.tuple.Tuple2 in project cyclops by aol.

the class SimpleReactStream method zip.

/**
 * Zip two Streams, zipping against the underlying futures of both Streams
 * Placeholders (Futures) will be populated immediately in the new zipped Stream and results
 * will be populated asyncrhonously
 *
 * @param other  Another FutureStream to zip Futures with
 * @return New Sequence of CompletableFutures
 */
default <R> SimpleReactStream<Tuple2<U, R>> zip(final SimpleReactStream<R> other) {
    final ReactiveSeq seq = ReactiveSeq.fromStream(getLastActive().stream()).zip(ReactiveSeq.fromStream(other.getLastActive().stream()));
    final ReactiveSeq<Tuple2<CompletableFuture<U>, CompletableFuture<R>>> withType = seq;
    final SimpleReactStream futureStream = fromStreamOfFutures((Stream) withType.map(t -> CompletableFuture.allOf(t._1(), t._2()).thenApply(v -> Tuple.tuple(t._1().join(), t._2().join()))));
    return futureStream;
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Blocker(com.oath.cyclops.react.collectors.lazy.Blocker) Tuple2(cyclops.data.tuple.Tuple2) java.util(java.util) ReactiveConvertableSequence(com.oath.cyclops.ReactiveConvertableSequence) LazyReact(cyclops.futurestream.LazyReact) FutureStream(cyclops.futurestream.FutureStream) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Tuple(cyclops.data.tuple.Tuple) FilteredExecutionPathException(com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) StreamSupport(java.util.stream.StreamSupport) Collector(java.util.stream.Collector) EagerStreamWrapper(com.oath.cyclops.internal.react.stream.EagerStreamWrapper) ThrowsSoftened(com.oath.cyclops.util.ThrowsSoftened) Continueable(com.oath.cyclops.react.async.subscription.Continueable) Streams(cyclops.companion.Streams) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) ReactiveSeq(cyclops.reactive.ReactiveSeq) StageWithResults(com.oath.cyclops.react.StageWithResults) Queue(com.oath.cyclops.async.adapters.Queue) Stream(java.util.stream.Stream) Status(com.oath.cyclops.react.Status) SimpleReact(cyclops.futurestream.SimpleReact) QueueFactory(com.oath.cyclops.async.adapters.QueueFactory) Tuple2(cyclops.data.tuple.Tuple2) ReactiveSeq(cyclops.reactive.ReactiveSeq)

Aggregations

Tuple2 (cyclops.data.tuple.Tuple2)43 Test (org.junit.Test)35 ReactiveSeq (cyclops.reactive.ReactiveSeq)23 Stream (java.util.stream.Stream)17 FutureStream (cyclops.futurestream.FutureStream)16 Tuple (cyclops.data.tuple.Tuple)10 BaseSeqTest (cyclops.futurestream.react.base.BaseSeqTest)9 Collectors (java.util.stream.Collectors)7 FlowableReactiveSeq (cyclops.reactive.FlowableReactiveSeq)6 FluxReactiveSeq (cyclops.reactive.FluxReactiveSeq)6 java.util (java.util)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Queue (com.oath.cyclops.async.adapters.Queue)5 Vector (cyclops.data.Vector)5 Tuple3 (cyclops.data.tuple.Tuple3)5 LazyReact (cyclops.futurestream.LazyReact)5 Streams (cyclops.companion.Streams)4 Executor (java.util.concurrent.Executor)4 Function (java.util.function.Function)4