Search in sources :

Example 1 with Stream

use of reactor.rx.Stream in project redisson by redisson.

the class RedissonMapReactiveIterator method stream.

public Publisher<M> stream() {
    return new Stream<M>() {

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

                private Map<ByteBuf, ByteBuf> firstValues;

                private long iterPos = 0;

                private InetSocketAddress client;

                private long currentIndex;

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

                private Map<ByteBuf, ByteBuf> convert(Map<ScanObjectEntry, ScanObjectEntry> map) {
                    Map<ByteBuf, ByteBuf> result = new HashMap<ByteBuf, ByteBuf>(map.size());
                    for (Entry<ScanObjectEntry, ScanObjectEntry> entry : map.entrySet()) {
                        result.put(entry.getKey().getBuf(), entry.getValue().getBuf());
                    }
                    return result;
                }

                protected void nextValues() {
                    final ReactiveSubscription<M> m = this;
                    map.scanIteratorReactive(client, iterPos).subscribe(new Subscriber<MapScanResult<ScanObjectEntry, ScanObjectEntry>>() {

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

                        @Override
                        public void onNext(MapScanResult<ScanObjectEntry, ScanObjectEntry> res) {
                            client = res.getRedisClient();
                            if (iterPos == 0 && firstValues == null) {
                                firstValues = convert(res.getMap());
                            } else if (convert(res.getMap()).equals(firstValues)) {
                                m.onComplete();
                                currentIndex = 0;
                                return;
                            }
                            iterPos = res.getPos();
                            for (Entry<ScanObjectEntry, ScanObjectEntry> entry : res.getMap().entrySet()) {
                                M val = getValue(entry);
                                m.onNext(val);
                                currentIndex--;
                                if (currentIndex == 0) {
                                    m.onComplete();
                                    return;
                                }
                            }
                        }

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

                        @Override
                        public void onComplete() {
                            if (currentIndex == 0) {
                                return;
                            }
                            nextValues();
                        }
                    });
                }
            });
        }
    };
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MapScanResult(org.redisson.client.protocol.decoder.MapScanResult) ByteBuf(io.netty.buffer.ByteBuf) ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) Entry(java.util.Map.Entry) Subscriber(org.reactivestreams.Subscriber) Stream(reactor.rx.Stream) ReactiveSubscription(reactor.rx.subscription.ReactiveSubscription) ReactiveSubscription(reactor.rx.subscription.ReactiveSubscription) Subscription(org.reactivestreams.Subscription) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 2 with Stream

use of reactor.rx.Stream 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)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2 Subscriber (org.reactivestreams.Subscriber)2 Subscription (org.reactivestreams.Subscription)2 Stream (reactor.rx.Stream)2 ReactiveSubscription (reactor.rx.subscription.ReactiveSubscription)2 ByteBuf (io.netty.buffer.ByteBuf)1 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ListScanResult (org.redisson.client.protocol.decoder.ListScanResult)1 MapScanResult (org.redisson.client.protocol.decoder.MapScanResult)1 ScanObjectEntry (org.redisson.client.protocol.decoder.ScanObjectEntry)1