From f0e27b4a03aa6dadefd0cd7cfc5dc23b69ec9982 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Tue, 24 Sep 2019 12:53:18 +0300 Subject: [PATCH] refactoring --- .../redisson/liveobject/LiveObjectSearch.java | 103 +++++++----------- 1 file changed, 41 insertions(+), 62 deletions(-) diff --git a/redisson/src/main/java/org/redisson/liveobject/LiveObjectSearch.java b/redisson/src/main/java/org/redisson/liveobject/LiveObjectSearch.java index 2536af918..6cc68b3bc 100644 --- a/redisson/src/main/java/org/redisson/liveobject/LiveObjectSearch.java +++ b/redisson/src/main/java/org/redisson/liveobject/LiveObjectSearch.java @@ -60,8 +60,6 @@ public class LiveObjectSearch { private Set traverseAnd(ANDCondition condition, NamingScheme namingScheme, Class entityClass) { Set allIds = new HashSet(); - RSet firstEqSet = null; - List eqNames = new ArrayList(); Map, Number> gtNumericNames = new HashMap<>(); @@ -81,11 +79,7 @@ public class LiveObjectSearch { } else { RSetMultimap map = redisson.getSetMultimap(indexName, namingScheme.getCodec()); RSet values = map.get(eqc.getValue()); - if (firstEqSet == null) { - firstEqSet = values; - } else { - eqNames.add(values.getName()); - } + eqNames.add(values.getName()); } } if (cond instanceof LTCondition) { @@ -122,24 +116,23 @@ public class LiveObjectSearch { if (ids.isEmpty()) { return Collections.emptySet(); } - allIds.addAll(ids); + allIds.retainAll(ids); + if (allIds.isEmpty()) { + return Collections.emptySet(); + } } } - if (firstEqSet != null) { - if (eqNames.isEmpty()) { - if (!allIds.isEmpty()) { - allIds.retainAll(firstEqSet.readAll()); - } else { - allIds.addAll(firstEqSet.readAll()); + if (!eqNames.isEmpty()) { + RSet set = redisson.getSet(eqNames.get(0)); + Set intersect = set.readIntersection(eqNames.toArray(new String[eqNames.size()])); + if (!allIds.isEmpty()) { + allIds.retainAll(intersect); + if (allIds.isEmpty()) { + return Collections.emptySet(); } } else { - Set intersect = firstEqSet.readIntersection(eqNames.toArray(new String[eqNames.size()])); - if (!allIds.isEmpty()) { - allIds.retainAll(intersect); - } else { - allIds.addAll(intersect); - } + allIds.addAll(intersect); } } @@ -178,25 +171,22 @@ public class LiveObjectSearch { private boolean checkValueRange(Set allIds, Map, Number> numericNames, BiFunction, Number, Collection> func) { - if (!numericNames.isEmpty()) { - Set gtAllIds = new HashSet<>(); - boolean firstFill = false; - for (Entry, Number> e : numericNames.entrySet()) { - Collection gtIds = func.apply(e.getKey(), e.getValue()); - if (gtIds.isEmpty()) { - return false; - } - if (!firstFill) { - gtAllIds.addAll(gtIds); - firstFill = true; - } else { - gtAllIds.retainAll(gtIds); - } + if (numericNames.isEmpty()) { + return true; + } + + for (Entry, Number> e : numericNames.entrySet()) { + Collection gtIds = func.apply(e.getKey(), e.getValue()); + if (gtIds.isEmpty()) { + return false; } if (!allIds.isEmpty()) { - allIds.retainAll(gtAllIds); + allIds.retainAll(gtIds); + if (allIds.isEmpty()) { + return false; + } } else { - allIds.addAll(gtAllIds); + allIds.addAll(gtIds); } } return true; @@ -205,7 +195,6 @@ public class LiveObjectSearch { private Set traverseOr(ORCondition condition, NamingScheme namingScheme, Class entityClass) { Set allIds = new HashSet(); - RSet firstEqSet = null; List eqNames = new ArrayList(); Map, Number> ltNumericNames = new HashMap<>(); @@ -225,11 +214,7 @@ public class LiveObjectSearch { } else { RSetMultimap map = redisson.getSetMultimap(indexName, namingScheme.getCodec()); RSet values = map.get(eqc.getValue()); - if (firstEqSet == null) { - firstEqSet = values; - } else { - eqNames.add(values.getName()); - } + eqNames.add(values.getName()); } } if (cond instanceof GTCondition) { @@ -265,12 +250,10 @@ public class LiveObjectSearch { allIds.addAll(ids); } } - if (firstEqSet != null) { - if (eqNames.isEmpty()) { - allIds.addAll(firstEqSet.readAll()); - } else { - allIds.addAll(firstEqSet.readUnion(eqNames.toArray(new String[eqNames.size()]))); - } + + if (!eqNames.isEmpty()) { + RSet set = redisson.getSet(eqNames.get(0)); + allIds.addAll(set.readUnion(eqNames.toArray(new String[eqNames.size()]))); } for (Entry, Number> e : eqNumericNames.entrySet()) { @@ -304,7 +287,6 @@ public class LiveObjectSearch { public Set find(Class entityClass, Condition condition) { NamingScheme namingScheme = objectBuilder.getNamingScheme(entityClass); - Set ids = Collections.emptySet(); if (condition instanceof EQCondition) { EQCondition c = (EQCondition) condition; String indexName = namingScheme.getIndexName(entityClass, c.getName()); @@ -313,45 +295,42 @@ public class LiveObjectSearch { RScoredSortedSet set = redisson.getScoredSortedSet(indexName, namingScheme.getCodec()); double v = ((Number) c.getValue()).doubleValue(); Collection gtIds = set.valueRange(v, true, v, true); - ids = new HashSet<>(gtIds); + return new HashSet<>(gtIds); } else { RSetMultimap map = redisson.getSetMultimap(indexName, namingScheme.getCodec()); - ids = map.getAll(c.getValue()); + return map.getAll(c.getValue()); } } else if (condition instanceof GTCondition) { GTCondition c = (GTCondition) condition; String indexName = namingScheme.getIndexName(entityClass, c.getName()); RScoredSortedSet set = redisson.getScoredSortedSet(indexName, namingScheme.getCodec()); Collection gtIds = set.valueRange(c.getValue().doubleValue(), false, Double.POSITIVE_INFINITY, false); - ids = new HashSet<>(gtIds); + return new HashSet<>(gtIds); } else if (condition instanceof GECondition) { GECondition c = (GECondition) condition; String indexName = namingScheme.getIndexName(entityClass, c.getName()); RScoredSortedSet set = redisson.getScoredSortedSet(indexName, namingScheme.getCodec()); Collection gtIds = set.valueRange(c.getValue().doubleValue(), true, Double.POSITIVE_INFINITY, false); - ids = new HashSet<>(gtIds); + return new HashSet<>(gtIds); } else if (condition instanceof LTCondition) { LTCondition c = (LTCondition) condition; String indexName = namingScheme.getIndexName(entityClass, c.getName()); RScoredSortedSet set = redisson.getScoredSortedSet(indexName, namingScheme.getCodec()); Collection gtIds = set.valueRange(Double.NEGATIVE_INFINITY, false, c.getValue().doubleValue(), false); - ids = new HashSet<>(gtIds); + return new HashSet<>(gtIds); } else if (condition instanceof LECondition) { LECondition c = (LECondition) condition; String indexName = namingScheme.getIndexName(entityClass, c.getName()); RScoredSortedSet set = redisson.getScoredSortedSet(indexName, namingScheme.getCodec()); Collection gtIds = set.valueRange(Double.NEGATIVE_INFINITY, false, c.getValue().doubleValue(), true); - ids = new HashSet<>(gtIds); + return new HashSet<>(gtIds); } else if (condition instanceof ORCondition) { - ids = traverseOr((ORCondition) condition, namingScheme, entityClass); + return traverseOr((ORCondition) condition, namingScheme, entityClass); } else if (condition instanceof ANDCondition) { - ids = traverseAnd((ANDCondition) condition, namingScheme, entityClass); - } - - if (ids.isEmpty()) { - return Collections.emptySet(); + return traverseAnd((ANDCondition) condition, namingScheme, entityClass); } - return ids; + + throw new IllegalArgumentException(); } -- GitLab