Search in sources :

Example 1 with ListScanResult

use of org.redisson.client.protocol.decoder.ListScanResult 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 2 with ListScanResult

use of org.redisson.client.protocol.decoder.ListScanResult 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 3 with ListScanResult

use of org.redisson.client.protocol.decoder.ListScanResult 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 4 with ListScanResult

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

the class RedissonLiveObjectService method findIds.

@Override
public <K> Iterable<K> findIds(Class<?> entityClass, int count) {
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass);
    String pattern = namingScheme.getNamePattern(entityClass);
    RedissonKeys keys = new RedissonKeys(commandExecutor);
    RedisCommand<ListScanResult<String>> command = new RedisCommand<>("SCAN", new ListMultiDecoder2(new ListScanResultReplayDecoder(), new ObjectListReplayDecoder<Object>()), new Convertor<Object>() {

        @Override
        public Object convert(Object obj) {
            if (!(obj instanceof String)) {
                return obj;
            }
            return namingScheme.resolveId(obj.toString());
        }
    });
    return keys.getKeysByPattern(command, pattern, 0, count);
}
Also used : ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) ListMultiDecoder2(org.redisson.client.protocol.decoder.ListMultiDecoder2) RedisCommand(org.redisson.client.protocol.RedisCommand) ListScanResultReplayDecoder(org.redisson.client.protocol.decoder.ListScanResultReplayDecoder) ObjectListReplayDecoder(org.redisson.client.protocol.decoder.ObjectListReplayDecoder)

Example 5 with ListScanResult

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

the class RedissonObjectBuilder method tryHandleReference.

public Object tryHandleReference(Object o, ReferenceType type) throws ReflectiveOperationException {
    boolean hasConversion = false;
    if (o instanceof List) {
        List<Object> r = (List<Object>) o;
        for (int i = 0; i < r.size(); i++) {
            Object ref = tryHandleReference0(r.get(i), type);
            if (ref != r.get(i)) {
                r.set(i, ref);
            }
        }
        return o;
    } else if (o instanceof Set) {
        Set<Object> set;
        Set<Object> r = (Set<Object>) o;
        boolean useNewSet = o instanceof LinkedHashSet;
        try {
            set = (Set<Object>) o.getClass().getConstructor().newInstance();
        } catch (Exception exception) {
            set = new LinkedHashSet<>();
        }
        for (Object i : r) {
            Object ref = tryHandleReference0(i, type);
            // like only one element fails.
            if (useNewSet) {
                set.add(ref);
            } else {
                try {
                    r.add(ref);
                    set.add(i);
                } catch (Exception e) {
                    // r is not supporting add operation, like
                    // LinkedHashMap$LinkedEntrySet and others.
                    // fall back to use a new set.
                    useNewSet = true;
                    set.add(ref);
                }
            }
            hasConversion |= ref != i;
        }
        if (!hasConversion) {
            return o;
        } else if (useNewSet) {
            return set;
        } else if (!set.isEmpty()) {
            r.removeAll(set);
        }
        return o;
    } else if (o instanceof Map) {
        Map<Object, Object> r = (Map<Object, Object>) o;
        for (Map.Entry<Object, Object> e : r.entrySet()) {
            if (e.getKey() instanceof RedissonReference || e.getValue() instanceof RedissonReference) {
                Object key = e.getKey();
                Object value = e.getValue();
                if (e.getKey() instanceof RedissonReference) {
                    key = fromReference((RedissonReference) e.getKey(), type);
                    r.remove(e.getKey());
                }
                if (e.getValue() instanceof RedissonReference) {
                    value = fromReference((RedissonReference) e.getValue(), type);
                }
                r.put(key, value);
            }
        }
        return o;
    } else if (o instanceof ListScanResult) {
        tryHandleReference(((ListScanResult) o).getValues(), type);
        return o;
    } else if (o instanceof MapScanResult) {
        MapScanResult scanResult = (MapScanResult) o;
        Map oldMap = ((MapScanResult) o).getMap();
        Map map = (Map) tryHandleReference(oldMap, type);
        if (map != oldMap) {
            MapScanResult<Object, Object> newScanResult = new MapScanResult<Object, Object>(scanResult.getPos(), map);
            newScanResult.setRedisClient(scanResult.getRedisClient());
            return newScanResult;
        } else {
            return o;
        }
    } else {
        return tryHandleReference0(o, type);
    }
}
Also used : ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) MapScanResult(org.redisson.client.protocol.decoder.MapScanResult) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

ListScanResult (org.redisson.client.protocol.decoder.ListScanResult)5 InetSocketAddress (java.net.InetSocketAddress)2 Subscriber (org.reactivestreams.Subscriber)2 Subscription (org.reactivestreams.Subscription)2 MapScanResult (org.redisson.client.protocol.decoder.MapScanResult)2 ScanObjectEntry (org.redisson.client.protocol.decoder.ScanObjectEntry)2 ReactiveSubscription (reactor.rx.subscription.ReactiveSubscription)2 ByteBuf (io.netty.buffer.ByteBuf)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)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