|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.bson.Document; |
| 26 | + |
| 27 | +import org.springframework.data.geo.Point; |
| 28 | +import org.springframework.lang.Nullable; |
| 29 | +import org.springframework.util.ObjectUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * Value object representing a count query. Count queries using {@code $near} or {@code $nearSphere} require a rewrite |
| 33 | + * to {@code $geoWithin}. |
| 34 | + * |
| 35 | + * @author Christoph Strobl |
| 36 | + * @author Mark Paluch |
| 37 | + * @since 2.3 |
| 38 | + */ |
| 39 | +class CountQuery { |
| 40 | + |
| 41 | + private Document source; |
| 42 | + |
| 43 | + private CountQuery(Document source) { |
| 44 | + this.source = source; |
| 45 | + } |
| 46 | + |
| 47 | + public static CountQuery of(Document source) { |
| 48 | + return new CountQuery(source); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the query {@link Document} that can be used with {@code countDocuments()}. Potentially rewrites the query |
| 53 | + * to be usable with {@code countDocuments()}. |
| 54 | + * |
| 55 | + * @return the query {@link Document} that can be used with {@code countDocuments()}. |
| 56 | + */ |
| 57 | + public Document toQueryDocument() { |
| 58 | + |
| 59 | + if (!requiresRewrite(source)) { |
| 60 | + return source; |
| 61 | + } |
| 62 | + |
| 63 | + Document target = new Document(); |
| 64 | + |
| 65 | + for (Map.Entry<String, Object> entry : source.entrySet()) { |
| 66 | + |
| 67 | + if (entry.getValue() instanceof Document && requiresRewrite(entry.getValue())) { |
| 68 | + |
| 69 | + Document theValue = (Document) entry.getValue(); |
| 70 | + target.putAll(createGeoWithin(entry.getKey(), theValue, source.get("$and"))); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if (entry.getValue() instanceof Collection && requiresRewrite(entry.getValue())) { |
| 75 | + |
| 76 | + Collection<?> source = (Collection<?>) entry.getValue(); |
| 77 | + |
| 78 | + target.put(entry.getKey(), rewriteCollection(source)); |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if ("$and".equals(entry.getKey()) && target.containsKey("$and")) { |
| 83 | + // Expect $and to be processed with Document and createGeoWithin. |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + target.put(entry.getKey(), entry.getValue()); |
| 88 | + } |
| 89 | + |
| 90 | + return target; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param valueToInspect |
| 95 | + * @return {@code true} if the enclosing element needs to be rewritten. |
| 96 | + */ |
| 97 | + private boolean requiresRewrite(Object valueToInspect) { |
| 98 | + |
| 99 | + if (valueToInspect instanceof Document) { |
| 100 | + return requiresRewrite((Document) valueToInspect); |
| 101 | + } |
| 102 | + |
| 103 | + if (valueToInspect instanceof Collection) { |
| 104 | + return requiresRewrite((Collection) valueToInspect); |
| 105 | + } |
| 106 | + |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + private boolean requiresRewrite(Collection<?> collection) { |
| 111 | + |
| 112 | + for (Object o : collection) { |
| 113 | + if (o instanceof Document && requiresRewrite((Document) o)) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + private boolean requiresRewrite(Document document) { |
| 122 | + |
| 123 | + if (containsNear(document)) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + for (Object entry : document.values()) { |
| 128 | + |
| 129 | + if (requiresRewrite(entry)) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + private Collection<Object> rewriteCollection(Collection<?> source) { |
| 138 | + |
| 139 | + Collection<Object> rewrittenCollection = new ArrayList<>(source.size()); |
| 140 | + |
| 141 | + for (Object item : source) { |
| 142 | + if (item instanceof Document && requiresRewrite(item)) { |
| 143 | + rewrittenCollection.add(CountQuery.of((Document) item).toQueryDocument()); |
| 144 | + } else { |
| 145 | + rewrittenCollection.add(item); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return rewrittenCollection; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Rewrite the near query for field {@code key} to {@code $geoWithin}. |
| 154 | + * |
| 155 | + * @param key the queried field. |
| 156 | + * @param source source {@link Document}. |
| 157 | + * @param $and potentially existing {@code $and} condition. |
| 158 | + * @return the rewritten query {@link Document}. |
| 159 | + */ |
| 160 | + private static Document createGeoWithin(String key, Document source, @Nullable Object $and) { |
| 161 | + |
| 162 | + boolean spheric = source.containsKey("$nearSphere"); |
| 163 | + Object $near = spheric ? source.get("$nearSphere") : source.get("$near"); |
| 164 | + |
| 165 | + Number maxDistance = source.containsKey("$maxDistance") ? (Number) source.get("$maxDistance") : Double.MAX_VALUE; |
| 166 | + List<Object> $centerMax = Arrays.asList(toCenterCoordinates($near), maxDistance); |
| 167 | + Document $geoWithinMax = new Document("$geoWithin", |
| 168 | + new Document(spheric ? "$centerSphere" : "$center", $centerMax)); |
| 169 | + |
| 170 | + if (!containsNearWithMinDistance(source)) { |
| 171 | + return new Document(key, $geoWithinMax); |
| 172 | + } |
| 173 | + |
| 174 | + Number minDistance = (Number) source.get("$minDistance"); |
| 175 | + List<Object> $centerMin = Arrays.asList(toCenterCoordinates($near), minDistance); |
| 176 | + Document $geoWithinMin = new Document("$geoWithin", |
| 177 | + new Document(spheric ? "$centerSphere" : "$center", $centerMin)); |
| 178 | + |
| 179 | + List<Document> criteria = new ArrayList<>(); |
| 180 | + |
| 181 | + if ($and != null) { |
| 182 | + if ($and instanceof Collection) { |
| 183 | + criteria.addAll((Collection) $and); |
| 184 | + } else { |
| 185 | + throw new IllegalArgumentException( |
| 186 | + "Cannot rewrite query as it contains an '$and' element that is not a Collection!: Offending element: " |
| 187 | + + $and); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + criteria.add(new Document("$nor", Collections.singletonList(new Document(key, $geoWithinMin)))); |
| 192 | + criteria.add(new Document(key, $geoWithinMax)); |
| 193 | + return new Document("$and", criteria); |
| 194 | + } |
| 195 | + |
| 196 | + private static boolean containsNear(Document source) { |
| 197 | + return source.containsKey("$near") || source.containsKey("$nearSphere"); |
| 198 | + } |
| 199 | + |
| 200 | + private static boolean containsNearWithMinDistance(Document source) { |
| 201 | + |
| 202 | + if (!containsNear(source)) { |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + return source.containsKey("$minDistance"); |
| 207 | + } |
| 208 | + |
| 209 | + private static Object toCenterCoordinates(Object value) { |
| 210 | + |
| 211 | + if (ObjectUtils.isArray(value)) { |
| 212 | + return value; |
| 213 | + } |
| 214 | + |
| 215 | + if (value instanceof Point) { |
| 216 | + return Arrays.asList(((Point) value).getX(), ((Point) value).getY()); |
| 217 | + } |
| 218 | + |
| 219 | + if (value instanceof Document && ((Document) value).containsKey("x")) { |
| 220 | + |
| 221 | + Document point = (Document) value; |
| 222 | + return Arrays.asList(point.get("x"), point.get("y")); |
| 223 | + } |
| 224 | + |
| 225 | + return value; |
| 226 | + } |
| 227 | +} |
0 commit comments