|
24 | 24 | import java.util.Collections; |
25 | 25 | import java.util.List; |
26 | 26 | import java.util.Optional; |
| 27 | +import java.util.Set; |
27 | 28 | import java.util.stream.Collectors; |
28 | 29 |
|
29 | 30 | import org.bson.Document; |
|
41 | 42 | * @author Oliver Gierke |
42 | 43 | * @author Christoph Strobl |
43 | 44 | * @author Mark Paluch |
| 45 | + * @author dragonfsky |
44 | 46 | */ |
45 | 47 | public class IndexInfo { |
46 | 48 |
|
@@ -182,16 +184,80 @@ public List<IndexField> getIndexFields() { |
182 | 184 | } |
183 | 185 |
|
184 | 186 | /** |
185 | | - * Returns whether the index is covering exactly the fields given independently of the order. |
| 187 | + * Returns whether the index contains all given field keys independently of their position. Field keys are compared as |
| 188 | + * a set and therefore repeated input keys are ignored. |
186 | 189 | * |
187 | 190 | * @param keys must not be {@literal null}. |
188 | 191 | * @return |
| 192 | + * @since 5.1 |
189 | 193 | */ |
| 194 | + public boolean containsAllFields(Collection<String> keys) { |
| 195 | + |
| 196 | + Assert.notNull(keys, "Collection of keys must not be null"); |
| 197 | + |
| 198 | + return getIndexFieldKeys().containsAll(keys); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Returns whether the index contains all given field keys independently of their position. Field keys are compared as |
| 203 | + * a set and therefore repeated input keys are ignored. |
| 204 | + * |
| 205 | + * @param keys must not be {@literal null}. |
| 206 | + * @return |
| 207 | + * @deprecated since 5.1. Use {@link #containsAllFields(Collection)}, {@link #isIndexForFieldsExactly(Collection)}, or |
| 208 | + * {@link #coversFields(Collection)} to express the intended index field matching semantics. |
| 209 | + */ |
| 210 | + @Deprecated(since = "5.1") |
190 | 211 | public boolean isIndexForFields(Collection<String> keys) { |
| 212 | + return containsAllFields(keys); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Returns whether the index matches exactly the given field keys independently of their order. Field keys are compared |
| 217 | + * as a set and therefore repeated input keys are ignored. |
| 218 | + * |
| 219 | + * @param keys must not be {@literal null}. |
| 220 | + * @return |
| 221 | + * @since 5.1 |
| 222 | + */ |
| 223 | + public boolean isIndexForFieldsExactly(Collection<String> keys) { |
191 | 224 |
|
192 | 225 | Assert.notNull(keys, "Collection of keys must not be null"); |
193 | 226 |
|
194 | | - return this.indexFields.stream().map(IndexField::getKey).collect(Collectors.toSet()).containsAll(keys); |
| 227 | + Set<String> indexFieldKeys = getIndexFieldKeys(); |
| 228 | + Set<String> keysToCheck = keys.stream().collect(Collectors.toSet()); |
| 229 | + |
| 230 | + return indexFieldKeys.size() == keysToCheck.size() && indexFieldKeys.containsAll(keysToCheck); |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Returns whether the given field keys are covered by this index according to compound-index prefix field matching. |
| 235 | + * Field keys are compared as a set and therefore repeated input keys are ignored. This method matches |
| 236 | + * {@link IndexField#getKey() index field keys} only and does not evaluate special query semantics of text, geo, or |
| 237 | + * wildcard indexes. |
| 238 | + * |
| 239 | + * @param keys must not be {@literal null}. |
| 240 | + * @return |
| 241 | + * @since 5.1 |
| 242 | + */ |
| 243 | + public boolean coversFields(Collection<String> keys) { |
| 244 | + |
| 245 | + Assert.notNull(keys, "Collection of keys must not be null"); |
| 246 | + |
| 247 | + Set<String> keysToCheck = keys.stream().collect(Collectors.toSet()); |
| 248 | + |
| 249 | + if (keysToCheck.size() > indexFields.size()) { |
| 250 | + return false; |
| 251 | + } |
| 252 | + |
| 253 | + Set<String> indexFieldPrefix = indexFields.stream().limit(keysToCheck.size()).map(IndexField::getKey) |
| 254 | + .collect(Collectors.toSet()); |
| 255 | + |
| 256 | + return indexFieldPrefix.containsAll(keysToCheck); |
| 257 | + } |
| 258 | + |
| 259 | + private Set<String> getIndexFieldKeys() { |
| 260 | + return this.indexFields.stream().map(IndexField::getKey).collect(Collectors.toSet()); |
195 | 261 | } |
196 | 262 |
|
197 | 263 | public String getName() { |
|
0 commit comments