Search in sources :

Example 16 with Subscription

use of org.reactivestreams.Subscription in project redisson by redisson.

the class SetReactiveIterator method subscribe.

@Override
public void subscribe(final Subscriber<? super V> t) {
    t.onSubscribe(new ReactiveSubscription<V>(this, t) {

        private List<ByteBuf> firstValues;

        private List<ByteBuf> lastValues;

        private long nextIterPos;

        private InetSocketAddress client;

        private boolean finished;

        @Override
        protected void onRequest(long n) {
            nextValues();
        }

        private void handle(List<ScanObjectEntry> vals) {
            for (ScanObjectEntry val : vals) {
                onNext((V) val.getObj());
            }
        }

        protected void nextValues() {
            final ReactiveSubscription<V> m = this;
            scanIteratorReactive(client, nextIterPos).subscribe(new Subscriber<ListScanResult<ScanObjectEntry>>() {

                @Override
                public void onSubscribe(Subscription s) {
                    s.request(Long.MAX_VALUE);
                }

                @Override
                public void onNext(ListScanResult<ScanObjectEntry> res) {
                    if (finished) {
                        free(firstValues);
                        free(lastValues);
                        client = null;
                        firstValues = null;
                        lastValues = null;
                        nextIterPos = 0;
                        return;
                    }
                    long prevIterPos = nextIterPos;
                    if (lastValues != null) {
                        free(lastValues);
                    }
                    lastValues = convert(res.getValues());
                    client = res.getRedisClient();
                    if (nextIterPos == 0 && firstValues == null) {
                        firstValues = lastValues;
                        lastValues = null;
                        if (firstValues.isEmpty()) {
                            client = null;
                            firstValues = null;
                            nextIterPos = 0;
                            prevIterPos = -1;
                        }
                    } else {
                        if (firstValues.isEmpty()) {
                            firstValues = lastValues;
                            lastValues = null;
                            if (firstValues.isEmpty()) {
                                if (res.getPos() == 0) {
                                    finished = true;
                                    m.onComplete();
                                    return;
                                }
                            }
                        } else if (lastValues.removeAll(firstValues)) {
                            free(firstValues);
                            free(lastValues);
                            client = null;
                            firstValues = null;
                            lastValues = null;
                            nextIterPos = 0;
                            prevIterPos = -1;
                            finished = true;
                            m.onComplete();
                            return;
                        }
                    }
                    handle(res.getValues());
                    nextIterPos = res.getPos();
                    if (prevIterPos == nextIterPos) {
                        finished = true;
                        m.onComplete();
                    }
                }

                @Override
                public void onError(Throwable error) {
                    m.onError(error);
                }

                @Override
                public void onComplete() {
                    if (finished) {
                        return;
                    }
                    nextValues();
                }
            });
        }
    });
}
Also used : ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) Subscriber(org.reactivestreams.Subscriber) ReactiveSubscription(reactor.rx.subscription.ReactiveSubscription) ReactiveSubscription(reactor.rx.subscription.ReactiveSubscription) Subscription(org.reactivestreams.Subscription)

Example 17 with Subscription

use of org.reactivestreams.Subscription in project redisson by redisson.

the class RedissonKeysReactive method createKeysIterator.

private Publisher<String> createKeysIterator(final MasterSlaveEntry entry, final String pattern) {
    return new Stream<String>() {

        @Override
        public void subscribe(final Subscriber<? super String> t) {
            t.onSubscribe(new ReactiveSubscription<String>(this, t) {

                private List<String> firstValues;

                private long nextIterPos;

                private InetSocketAddress client;

                private long currentIndex;

                @Override
                protected void onRequest(final long n) {
                    currentIndex = n;
                    nextValues();
                }

                protected void nextValues() {
                    final ReactiveSubscription<String> m = this;
                    scanIterator(entry, nextIterPos, pattern).subscribe(new Subscriber<ListScanResult<String>>() {

                        @Override
                        public void onSubscribe(Subscription s) {
                            s.request(Long.MAX_VALUE);
                        }

                        @Override
                        public void onNext(ListScanResult<String> res) {
                            client = res.getRedisClient();
                            long prevIterPos = nextIterPos;
                            if (nextIterPos == 0 && firstValues == null) {
                                firstValues = res.getValues();
                            } else if (res.getValues().equals(firstValues)) {
                                m.onComplete();
                                currentIndex = 0;
                                return;
                            }
                            nextIterPos = res.getPos();
                            if (prevIterPos == nextIterPos) {
                                nextIterPos = -1;
                            }
                            for (String val : res.getValues()) {
                                m.onNext(val);
                                currentIndex--;
                                if (currentIndex == 0) {
                                    m.onComplete();
                                    return;
                                }
                            }
                            if (nextIterPos == -1) {
                                m.onComplete();
                                currentIndex = 0;
                            }
                        }

                        @Override
                        public void onError(Throwable error) {
                            m.onError(error);
                        }

                        @Override
                        public void onComplete() {
                            if (currentIndex == 0) {
                                return;
                            }
                            nextValues();
                        }
                    });
                }
            });
        }
    };
}
Also used : ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) Subscriber(org.reactivestreams.Subscriber) InetSocketAddress(java.net.InetSocketAddress) Stream(reactor.rx.Stream) ReactiveSubscription(reactor.rx.subscription.ReactiveSubscription) ReactiveSubscription(reactor.rx.subscription.ReactiveSubscription) Subscription(org.reactivestreams.Subscription)

Example 18 with Subscription

use of org.reactivestreams.Subscription in project ratpack by ratpack.

the class MapPublisher method subscribe.

@Override
public void subscribe(final Subscriber<? super O> outSubscriber) {
    input.subscribe(new Subscriber<I>() {

        private Subscription subscription;

        private final AtomicBoolean done = new AtomicBoolean();

        @Override
        public void onSubscribe(Subscription subscription) {
            this.subscription = subscription;
            outSubscriber.onSubscribe(this.subscription);
        }

        @Override
        public void onNext(I in) {
            O out;
            try {
                out = function.apply(in);
            } catch (Throwable throwable) {
                subscription.cancel();
                onError(throwable);
                return;
            }
            if (!done.get()) {
                outSubscriber.onNext(out);
            }
        }

        @Override
        public void onError(Throwable t) {
            if (done.compareAndSet(false, true)) {
                outSubscriber.onError(t);
            }
        }

        @Override
        public void onComplete() {
            if (done.compareAndSet(false, true)) {
                outSubscriber.onComplete();
            }
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(org.reactivestreams.Subscription)

Example 19 with Subscription

use of org.reactivestreams.Subscription in project cyclops by aol.

the class BaseSequentialTest method subscribe3ErrorOnComplete.

@Test
public void subscribe3ErrorOnComplete() throws InterruptedException {
    List<Integer> result = new ArrayList<>();
    AtomicBoolean onComplete = new AtomicBoolean(false);
    Subscription s = of(1, 2, 3).forEachSubscribe(i -> result.add(i), e -> e.printStackTrace(), () -> onComplete.set(true));
    assertThat(onComplete.get(), Matchers.equalTo(false));
    s.request(4l);
    assertThat(onComplete.get(), Matchers.equalTo(true));
    assertThat(result.size(), Matchers.equalTo(3));
    assertThat(result, hasItems(1, 2, 3));
    assertThat(onComplete.get(), Matchers.equalTo(true));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 20 with Subscription

use of org.reactivestreams.Subscription in project cyclops by aol.

the class BaseSequentialTest method subscribeError.

@Test
public void subscribeError() throws InterruptedException {
    List<Integer> result = new ArrayList<>();
    Subscription s = of(1, 2, 3).forEachSubscribe(i -> result.add(i), e -> e.printStackTrace());
    s.request(1l);
    assertThat(result.size(), Matchers.equalTo(1));
    s.request(1l);
    assertThat(result.size(), Matchers.equalTo(2));
    s.request(1l);
    assertThat(result.size(), Matchers.equalTo(3));
    assertThat(result, hasItems(1, 2, 3));
}
Also used : Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Aggregations

Subscription (org.reactivestreams.Subscription)627 Test (org.junit.Test)506 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)158 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)131 ArrayList (java.util.ArrayList)117 AtomicReference (java.util.concurrent.atomic.AtomicReference)105 List (java.util.List)80 FluxOperatorTest (reactor.test.publisher.FluxOperatorTest)74 Subscriber (org.reactivestreams.Subscriber)68 AtomicLong (java.util.concurrent.atomic.AtomicLong)56 Assert (org.junit.Assert)56 Arrays (java.util.Arrays)43 BaseSequentialTest (com.oath.cyclops.streams.BaseSequentialTest)42 Vector (cyclops.data.Vector)39 ReactiveSeq (cyclops.reactive.ReactiveSeq)39 Executor (java.util.concurrent.Executor)38 Spouts (cyclops.reactive.Spouts)36 Arrays.asList (java.util.Arrays.asList)36 Executors (java.util.concurrent.Executors)35 StepVerifier (reactor.test.StepVerifier)34