From 37dae78ab8d0ce7adddb67e3d0f9472aa06bcbde Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 10 Apr 2024 16:20:28 +0200 Subject: [PATCH] Polishing. Add Override annotations and comment why we optimize. See #4612 Original pull request: #4613 --- .../data/mongodb/core/convert/ReferenceLoader.java | 6 +++--- .../data/mongodb/core/convert/ReferenceLookupDelegate.java | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLoader.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLoader.java index cf7444e566..5128fe5154 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLoader.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLoader.java @@ -130,7 +130,7 @@ public Iterable apply(MongoCollection collection) { /** * @return a {@link DocumentReferenceQuery} that will not match any documents. - * @since 4.3 + * @since 4.2.5 */ static DocumentReferenceQuery forNoResult() { return NoResultsFilter.INSTANCE; @@ -139,8 +139,8 @@ static DocumentReferenceQuery forNoResult() { /** * A dedicated {@link DocumentReferenceQuery} that will not match any documents. - * - * @since 4.3 + * + * @since 4.2.5 */ enum NoResultsFilter implements DocumentReferenceQuery { INSTANCE; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLookupDelegate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLookupDelegate.java index 3cd5f0e980..1bcf4b1c00 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLookupDelegate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLookupDelegate.java @@ -240,7 +240,8 @@ ValueProvider valueProviderFor(Object source) { EvaluationContext evaluationContextFor(MongoPersistentProperty property, Object source, SpELContext spELContext) { - Object target = source instanceof DocumentReferenceSource documentReferenceSource ? documentReferenceSource.getTargetSource() + Object target = source instanceof DocumentReferenceSource documentReferenceSource + ? documentReferenceSource.getTargetSource() : source; if (target == null) { @@ -285,6 +286,7 @@ DocumentReferenceQuery computeFilter(MongoPersistentProperty property, Object so Collection objects = (Collection) value; + // optimization: bypass query if the collection pointing to the references is empty if (objects.isEmpty()) { return DocumentReferenceQuery.forNoResult(); } @@ -301,7 +303,7 @@ DocumentReferenceQuery computeFilter(MongoPersistentProperty property, Object so if (property.isMap() && value instanceof Map) { - if(ObjectUtils.isEmpty(value)) { + if (ObjectUtils.isEmpty(value)) { return DocumentReferenceQuery.forNoResult(); } @@ -461,6 +463,7 @@ public Iterable restoreOrder(Iterable documents) { return target.stream().sorted((o1, o2) -> compareAgainstReferenceIndex(ors, o1, o2)).collect(Collectors.toList()); } + @Override public Document getQuery() { return query; }