Search in sources :

Example 1 with MapScanResult

use of org.redisson.client.protocol.decoder.MapScanResult in project redisson by redisson.

the class CommandAsyncService method handleReference.

private <R, V> void handleReference(RPromise<R> mainPromise, R res) {
    if (res instanceof List) {
        List<Object> r = (List<Object>) res;
        for (int i = 0; i < r.size(); i++) {
            if (r.get(i) instanceof RedissonReference) {
                try {
                    r.set(i, redisson != null ? RedissonObjectFactory.fromReference(redisson, (RedissonReference) r.get(i)) : RedissonObjectFactory.fromReference(redissonReactive, (RedissonReference) r.get(i)));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            } else if (r.get(i) instanceof ScoredEntry && ((ScoredEntry) r.get(i)).getValue() instanceof RedissonReference) {
                try {
                    ScoredEntry<?> se = ((ScoredEntry<?>) r.get(i));
                    se = new ScoredEntry(se.getScore(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue()));
                    r.set(i, se);
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            }
        }
        mainPromise.trySuccess(res);
    } else if (res instanceof ListScanResult) {
        List<ScanObjectEntry> r = ((ListScanResult) res).getValues();
        for (int i = 0; i < r.size(); i++) {
            Object obj = r.get(i);
            if (!(obj instanceof ScanObjectEntry)) {
                break;
            }
            ScanObjectEntry e = r.get(i);
            if (e.getObj() instanceof RedissonReference) {
                try {
                    r.set(i, new ScanObjectEntry(e.getBuf(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getObj())));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            } else if (e.getObj() instanceof ScoredEntry && ((ScoredEntry<?>) e.getObj()).getValue() instanceof RedissonReference) {
                try {
                    ScoredEntry<?> se = ((ScoredEntry<?>) e.getObj());
                    se = new ScoredEntry(se.getScore(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) se.getValue()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) se.getValue()));
                    r.set(i, new ScanObjectEntry(e.getBuf(), se));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            }
        }
        mainPromise.trySuccess(res);
    } else if (res instanceof MapScanResult) {
        Map<ScanObjectEntry, ScanObjectEntry> map = ((MapScanResult) res).getMap();
        HashMap<ScanObjectEntry, ScanObjectEntry> toAdd = null;
        for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : map.entrySet()) {
            if (e.getValue().getObj() instanceof RedissonReference) {
                try {
                    e.setValue(new ScanObjectEntry(e.getValue().getBuf(), redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getValue().getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getValue().getObj())));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            }
            if (e.getKey().getObj() instanceof RedissonReference) {
                if (toAdd == null) {
                    toAdd = new HashMap<ScanObjectEntry, ScanObjectEntry>();
                }
                toAdd.put(e.getKey(), e.getValue());
            }
        }
        if (toAdd != null) {
            for (Map.Entry<ScanObjectEntry, ScanObjectEntry> e : toAdd.entrySet()) {
                try {
                    map.put(new ScanObjectEntry(e.getValue().getBuf(), (redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) e.getKey().getObj()) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) e.getKey().getObj()))), map.remove(e.getKey()));
                } catch (Exception exception) {
                //skip and carry on to next one.
                }
            }
        }
        mainPromise.trySuccess(res);
    } else if (res instanceof RedissonReference) {
        try {
            mainPromise.trySuccess(redisson != null ? RedissonObjectFactory.<R>fromReference(redisson, (RedissonReference) res) : RedissonObjectFactory.<R>fromReference(redissonReactive, (RedissonReference) res));
        } catch (Exception exception) {
            //fallback
            mainPromise.trySuccess(res);
        }
    } else {
        mainPromise.trySuccess(res);
    }
}
Also used : ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) RedissonReference(org.redisson.RedissonReference) MapScanResult(org.redisson.client.protocol.decoder.MapScanResult) ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) RedisAskException(org.redisson.client.RedisAskException) RedisLoadingException(org.redisson.client.RedisLoadingException) RedisTimeoutException(org.redisson.client.RedisTimeoutException) RedisException(org.redisson.client.RedisException) RedisMovedException(org.redisson.client.RedisMovedException) WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) RedisTryAgainException(org.redisson.client.RedisTryAgainException) RedissonShutdownException(org.redisson.RedissonShutdownException) ScoredEntry(org.redisson.client.protocol.ScoredEntry) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with MapScanResult

use of org.redisson.client.protocol.decoder.MapScanResult 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)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 MapScanResult (org.redisson.client.protocol.decoder.MapScanResult)2 ScanObjectEntry (org.redisson.client.protocol.decoder.ScanObjectEntry)2 ByteBuf (io.netty.buffer.ByteBuf)1 InetSocketAddress (java.net.InetSocketAddress)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Subscriber (org.reactivestreams.Subscriber)1 Subscription (org.reactivestreams.Subscription)1 RedissonReference (org.redisson.RedissonReference)1 RedissonShutdownException (org.redisson.RedissonShutdownException)1 RedisAskException (org.redisson.client.RedisAskException)1 RedisException (org.redisson.client.RedisException)1 RedisLoadingException (org.redisson.client.RedisLoadingException)1 RedisMovedException (org.redisson.client.RedisMovedException)1 RedisTimeoutException (org.redisson.client.RedisTimeoutException)1 RedisTryAgainException (org.redisson.client.RedisTryAgainException)1