Search in sources :

Example 1 with ScoredEntry

use of org.redisson.client.protocol.ScoredEntry 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 ScoredEntry

use of org.redisson.client.protocol.ScoredEntry in project gora by apache.

the class RedisStore method runQuery.

private Collection<K> runQuery(Query<K, T> query) {
    Collection<K> range;
    if (isNumericKey()) {
        RScoredSortedSet<Object> index = redisInstance.getScoredSortedSet(generateIndexKey());
        Collection<ScoredEntry<Object>> rangeResponse;
        int limit = query.getLimit() > -1 ? (int) query.getLimit() : Integer.MAX_VALUE;
        if (query.getStartKey() != null && query.getEndKey() != null) {
            rangeResponse = index.entryRange(obtainDoubleValue(query.getStartKey()), true, obtainDoubleValue(query.getEndKey()), true, 0, limit);
        } else if (query.getStartKey() != null && query.getEndKey() == null) {
            rangeResponse = index.entryRange(obtainDoubleValue(query.getStartKey()), true, Double.MAX_VALUE, true, 0, limit);
        } else if (query.getStartKey() == null && query.getEndKey() != null) {
            rangeResponse = index.entryRange(Double.MIN_VALUE, true, obtainDoubleValue(query.getEndKey()), true, 0, limit);
        } else {
            rangeResponse = index.entryRange(Double.MIN_VALUE, true, Double.MAX_VALUE, true, 0, limit);
        }
        range = new ArrayList<>();
        for (ScoredEntry<Object> indexVal : rangeResponse) {
            range.add((K) indexVal.getValue());
        }
    } else {
        RLexSortedSet index = redisInstance.getLexSortedSet(generateIndexKey());
        Collection<String> rangeResponse;
        int limit = query.getLimit() > -1 ? (int) query.getLimit() : Integer.MAX_VALUE;
        if (query.getStartKey() != null && query.getEndKey() != null) {
            rangeResponse = index.range(query.getStartKey().toString(), true, query.getEndKey().toString(), true, 0, limit);
        } else if (query.getStartKey() != null && query.getEndKey() == null) {
            rangeResponse = index.rangeTail(query.getStartKey().toString(), true, 0, limit);
        } else if (query.getStartKey() == null && query.getEndKey() != null) {
            rangeResponse = index.rangeHead(query.getEndKey().toString(), true, 0, limit);
        } else {
            rangeResponse = index.stream().limit(limit).collect(Collectors.toList());
        }
        range = new ArrayList<>();
        for (String indexVal : rangeResponse) {
            range.add((K) indexVal);
        }
    }
    return range;
}
Also used : ScoredEntry(org.redisson.client.protocol.ScoredEntry) RLexSortedSet(org.redisson.api.RLexSortedSet)

Example 3 with ScoredEntry

use of org.redisson.client.protocol.ScoredEntry in project redisson by redisson.

the class RedissonObjectBuilder method tryHandleReference0.

private Object tryHandleReference0(Object o, ReferenceType type) throws ReflectiveOperationException {
    if (o instanceof RedissonReference) {
        return fromReference((RedissonReference) o, type);
    } else if (o instanceof ScoredEntry && ((ScoredEntry) o).getValue() instanceof RedissonReference) {
        ScoredEntry<?> se = (ScoredEntry<?>) o;
        return new ScoredEntry(se.getScore(), fromReference((RedissonReference) se.getValue(), type));
    } else if (o instanceof Map.Entry) {
        Map.Entry old = (Map.Entry) o;
        Object key = tryHandleReference0(old.getKey(), type);
        Object value = tryHandleReference0(old.getValue(), type);
        if (value != old.getValue() || key != old.getKey()) {
            return new AbstractMap.SimpleEntry(key, value);
        }
    }
    return o;
}
Also used : Entry(java.util.Map.Entry) ScoredEntry(org.redisson.client.protocol.ScoredEntry) Entry(java.util.Map.Entry) ScoredEntry(org.redisson.client.protocol.ScoredEntry) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

ScoredEntry (org.redisson.client.protocol.ScoredEntry)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 RedissonReference (org.redisson.RedissonReference)1 RedissonShutdownException (org.redisson.RedissonShutdownException)1 RLexSortedSet (org.redisson.api.RLexSortedSet)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 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)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