Search in sources :

Example 1 with RScoredSortedSet

use of org.redisson.api.RScoredSortedSet in project redisson by redisson.

the class LiveObjectSearch method traverseOr.

private Set<Object> traverseOr(ORCondition condition, NamingScheme namingScheme, Class<?> entityClass) {
    Set<Object> allIds = new HashSet<Object>();
    List<String> eqNames = new ArrayList<String>();
    Map<RScoredSortedSet<Object>, Number> ltNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> leNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> gtNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> geNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> eqNumericNames = new HashMap<>();
    for (Condition cond : condition.getConditions()) {
        if (cond instanceof EQCondition) {
            EQCondition eqc = (EQCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, eqc.getName());
            if (eqc.getValue() instanceof Number) {
                RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
                eqNumericNames.put(values, (Number) eqc.getValue());
            } else {
                RSetMultimap<Object, Object> map = new RedissonSetMultimap<>(namingScheme.getCodec(), commandExecutor, indexName);
                RSet<Object> values = map.get(eqc.getValue());
                eqNames.add(((RedissonObject) values).getRawName());
            }
        }
        if (cond instanceof GTCondition) {
            GTCondition gtc = (GTCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, gtc.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            gtNumericNames.put(values, gtc.getValue());
        }
        if (cond instanceof GECondition) {
            GECondition gec = (GECondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, gec.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            geNumericNames.put(values, gec.getValue());
        }
        if (cond instanceof LTCondition) {
            LTCondition ltc = (LTCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, ltc.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            ltNumericNames.put(values, ltc.getValue());
        }
        if (cond instanceof LECondition) {
            LECondition lec = (LECondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, lec.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            leNumericNames.put(values, lec.getValue());
        }
        if (cond instanceof ANDCondition) {
            Collection<Object> ids = traverseAnd((ANDCondition) cond, namingScheme, entityClass);
            allIds.addAll(ids);
        }
    }
    if (!eqNames.isEmpty()) {
        RSet<Object> set = new RedissonSet<>(commandExecutor, eqNames.get(0), null);
        allIds.addAll(set.readUnion(eqNames.toArray(new String[eqNames.size()])));
    }
    for (Entry<RScoredSortedSet<Object>, Number> e : eqNumericNames.entrySet()) {
        Collection<Object> ids = e.getKey().valueRange(e.getValue().doubleValue(), true, e.getValue().doubleValue(), true);
        allIds.addAll(ids);
    }
    for (Entry<RScoredSortedSet<Object>, Number> e : gtNumericNames.entrySet()) {
        Collection<Object> ids = e.getKey().valueRange(e.getValue().doubleValue(), false, Double.POSITIVE_INFINITY, false);
        allIds.addAll(ids);
    }
    for (Entry<RScoredSortedSet<Object>, Number> e : geNumericNames.entrySet()) {
        Collection<Object> ids = e.getKey().valueRange(e.getValue().doubleValue(), true, Double.POSITIVE_INFINITY, false);
        allIds.addAll(ids);
    }
    for (Entry<RScoredSortedSet<Object>, Number> e : ltNumericNames.entrySet()) {
        Collection<Object> ids = e.getKey().valueRange(Double.NEGATIVE_INFINITY, false, e.getValue().doubleValue(), false);
        allIds.addAll(ids);
    }
    for (Entry<RScoredSortedSet<Object>, Number> e : leNumericNames.entrySet()) {
        Collection<Object> ids = e.getKey().valueRange(Double.NEGATIVE_INFINITY, false, e.getValue().doubleValue(), true);
        allIds.addAll(ids);
    }
    return allIds;
}
Also used : RedissonSet(org.redisson.RedissonSet) RScoredSortedSet(org.redisson.api.RScoredSortedSet) Condition(org.redisson.api.condition.Condition) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RedissonSetMultimap(org.redisson.RedissonSetMultimap) RedissonObject(org.redisson.RedissonObject)

Example 2 with RScoredSortedSet

use of org.redisson.api.RScoredSortedSet in project redisson by redisson.

the class LiveObjectSearch method find.

public Set<Object> find(Class<?> entityClass, Condition condition) {
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass);
    if (condition instanceof EQCondition) {
        EQCondition c = (EQCondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        if (c.getValue() instanceof Number) {
            RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            double v = ((Number) c.getValue()).doubleValue();
            Collection<Object> gtIds = set.valueRange(v, true, v, true);
            return new HashSet<>(gtIds);
        } else {
            RSetMultimap<Object, Object> map = new RedissonSetMultimap<>(namingScheme.getCodec(), commandExecutor, indexName);
            return map.getAll(c.getValue());
        }
    } else if (condition instanceof GTCondition) {
        GTCondition c = (GTCondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
        Collection<Object> gtIds = set.valueRange(c.getValue().doubleValue(), false, Double.POSITIVE_INFINITY, false);
        return new HashSet<>(gtIds);
    } else if (condition instanceof GECondition) {
        GECondition c = (GECondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
        Collection<Object> gtIds = set.valueRange(c.getValue().doubleValue(), true, Double.POSITIVE_INFINITY, false);
        return new HashSet<>(gtIds);
    } else if (condition instanceof LTCondition) {
        LTCondition c = (LTCondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
        Collection<Object> gtIds = set.valueRange(Double.NEGATIVE_INFINITY, false, c.getValue().doubleValue(), false);
        return new HashSet<>(gtIds);
    } else if (condition instanceof LECondition) {
        LECondition c = (LECondition) condition;
        String indexName = namingScheme.getIndexName(entityClass, c.getName());
        RScoredSortedSet<Object> set = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
        Collection<Object> gtIds = set.valueRange(Double.NEGATIVE_INFINITY, false, c.getValue().doubleValue(), true);
        return new HashSet<>(gtIds);
    } else if (condition instanceof ORCondition) {
        return traverseOr((ORCondition) condition, namingScheme, entityClass);
    } else if (condition instanceof ANDCondition) {
        return traverseAnd((ANDCondition) condition, namingScheme, entityClass);
    }
    throw new IllegalArgumentException();
}
Also used : RScoredSortedSet(org.redisson.api.RScoredSortedSet) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RedissonSetMultimap(org.redisson.RedissonSetMultimap) RedissonObject(org.redisson.RedissonObject)

Example 3 with RScoredSortedSet

use of org.redisson.api.RScoredSortedSet in project redisson by redisson.

the class RedissonRateLimiterTest method testRateValue.

@Test
public void testRateValue() throws InterruptedException {
    RRateLimiter rateLimiter = redisson.getRateLimiter("test1");
    int rate = 10_000;
    rateLimiter.setRate(RateType.OVERALL, rate, 10_000, RateIntervalUnit.MILLISECONDS);
    ExecutorService e = Executors.newFixedThreadPool(200);
    for (int i = 0; i < 200; i++) {
        e.execute(() -> {
            while (true) {
                rateLimiter.acquire();
            }
        });
    }
    RScoredSortedSet<Object> sortedSet = redisson.getScoredSortedSet("{test1}:permits");
    List<Integer> sizes = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        sizes.add(sortedSet.size());
        Thread.sleep(1000);
    }
    assertThat(sizes.stream().filter(s -> s == rate).count()).isGreaterThan(16);
    e.shutdownNow();
}
Also used : RScoredSortedSet(org.redisson.api.RScoredSortedSet) RateIntervalUnit(org.redisson.api.RateIntervalUnit) RateType(org.redisson.api.RateType) java.util.concurrent(java.util.concurrent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Duration(java.time.Duration) Assertions(org.junit.jupiter.api.Assertions) RRateLimiter(org.redisson.api.RRateLimiter) Queue(java.util.Queue) ArrayList(java.util.ArrayList) RRateLimiter(org.redisson.api.RRateLimiter) Test(org.junit.jupiter.api.Test)

Example 4 with RScoredSortedSet

use of org.redisson.api.RScoredSortedSet in project redisson by redisson.

the class LiveObjectSearch method traverseAnd.

private Set<Object> traverseAnd(ANDCondition condition, NamingScheme namingScheme, Class<?> entityClass) {
    Set<Object> allIds = new HashSet<Object>();
    List<String> eqNames = new ArrayList<String>();
    Map<RScoredSortedSet<Object>, Number> gtNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> geNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> ltNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> leNumericNames = new HashMap<>();
    Map<RScoredSortedSet<Object>, Number> eqNumericNames = new HashMap<>();
    for (Condition cond : condition.getConditions()) {
        if (cond instanceof EQCondition) {
            EQCondition eqc = (EQCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, eqc.getName());
            if (eqc.getValue() instanceof Number) {
                RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
                eqNumericNames.put(values, (Number) eqc.getValue());
            } else {
                RSetMultimap<Object, Object> map = new RedissonSetMultimap<>(namingScheme.getCodec(), commandExecutor, indexName);
                RSet<Object> values = map.get(eqc.getValue());
                eqNames.add(((RedissonObject) values).getRawName());
            }
        }
        if (cond instanceof LTCondition) {
            LTCondition ltc = (LTCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, ltc.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            ltNumericNames.put(values, ltc.getValue());
        }
        if (cond instanceof LECondition) {
            LECondition lec = (LECondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, lec.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            leNumericNames.put(values, lec.getValue());
        }
        if (cond instanceof GECondition) {
            GECondition gec = (GECondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, gec.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            geNumericNames.put(values, gec.getValue());
        }
        if (cond instanceof GTCondition) {
            GTCondition gtc = (GTCondition) cond;
            String indexName = namingScheme.getIndexName(entityClass, gtc.getName());
            RScoredSortedSet<Object> values = new RedissonScoredSortedSet<>(namingScheme.getCodec(), commandExecutor, indexName, null);
            gtNumericNames.put(values, gtc.getValue());
        }
        if (cond instanceof ORCondition) {
            Collection<Object> ids = traverseOr((ORCondition) cond, namingScheme, entityClass);
            if (ids.isEmpty()) {
                return Collections.emptySet();
            }
            if (!allIds.isEmpty()) {
                allIds.retainAll(ids);
            } else {
                allIds.addAll(ids);
            }
            if (allIds.isEmpty()) {
                return Collections.emptySet();
            }
        }
    }
    if (!eqNames.isEmpty()) {
        RSet<Object> set = new RedissonSet<>(commandExecutor, eqNames.get(0), null);
        Set<Object> intersect = set.readIntersection(eqNames.toArray(new String[eqNames.size()]));
        if (!allIds.isEmpty()) {
            allIds.retainAll(intersect);
            if (allIds.isEmpty()) {
                return Collections.emptySet();
            }
        } else {
            allIds.addAll(intersect);
        }
        if (allIds.isEmpty()) {
            return allIds;
        }
    }
    if (!checkValueRange(allIds, eqNumericNames, (r, v) -> {
        return r.valueRange(v.doubleValue(), true, v.doubleValue(), true);
    })) {
        return Collections.emptySet();
    }
    if (!checkValueRange(allIds, gtNumericNames, (r, v) -> {
        return r.valueRange(v.doubleValue(), false, Double.POSITIVE_INFINITY, false);
    })) {
        return Collections.emptySet();
    }
    if (!checkValueRange(allIds, geNumericNames, (r, v) -> {
        return r.valueRange(v.doubleValue(), true, Double.POSITIVE_INFINITY, false);
    })) {
        return Collections.emptySet();
    }
    if (!checkValueRange(allIds, ltNumericNames, (r, v) -> {
        return r.valueRange(Double.NEGATIVE_INFINITY, false, v.doubleValue(), false);
    })) {
        return Collections.emptySet();
    }
    if (!checkValueRange(allIds, leNumericNames, (r, v) -> {
        return r.valueRange(Double.NEGATIVE_INFINITY, false, v.doubleValue(), true);
    })) {
        return Collections.emptySet();
    }
    return allIds;
}
Also used : RedissonSet(org.redisson.RedissonSet) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RScoredSortedSet(org.redisson.api.RScoredSortedSet) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) java.util(java.util) BiFunction(java.util.function.BiFunction) RSet(org.redisson.api.RSet) RSetMultimap(org.redisson.api.RSetMultimap) org.redisson.liveobject.condition(org.redisson.liveobject.condition) Condition(org.redisson.api.condition.Condition) RedissonSetMultimap(org.redisson.RedissonSetMultimap) RedissonObject(org.redisson.RedissonObject) Entry(java.util.Map.Entry) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) RedissonSet(org.redisson.RedissonSet) RScoredSortedSet(org.redisson.api.RScoredSortedSet) Condition(org.redisson.api.condition.Condition) RedissonScoredSortedSet(org.redisson.RedissonScoredSortedSet) RedissonSetMultimap(org.redisson.RedissonSetMultimap) RedissonObject(org.redisson.RedissonObject)

Aggregations

RScoredSortedSet (org.redisson.api.RScoredSortedSet)4 RedissonObject (org.redisson.RedissonObject)3 RedissonScoredSortedSet (org.redisson.RedissonScoredSortedSet)3 RedissonSetMultimap (org.redisson.RedissonSetMultimap)3 RedissonSet (org.redisson.RedissonSet)2 Condition (org.redisson.api.condition.Condition)2 NamingScheme (org.redisson.liveobject.resolver.NamingScheme)2 Duration (java.time.Duration)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Queue (java.util.Queue)1 java.util.concurrent (java.util.concurrent)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 BiFunction (java.util.function.BiFunction)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions (org.junit.jupiter.api.Assertions)1 Test (org.junit.jupiter.api.Test)1 RRateLimiter (org.redisson.api.RRateLimiter)1