Browse Source

Polishing.

Reformat code. Make getAnnotatedHint non-nullable.

See #3230
Original pull request: #4339
pull/4373/head
Mark Paluch 3 years ago
parent
commit
89c6099a3c
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Hint.java
  2. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java
  3. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractReactiveMongoQuery.java
  4. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AggregationUtils.java
  5. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java
  6. 7
      src/main/asciidoc/reference/mongo-repositories.adoc

12
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Hint.java

@ -26,7 +26,7 @@ import org.springframework.core.annotation.AliasFor; @@ -26,7 +26,7 @@ import org.springframework.core.annotation.AliasFor;
/**
* Annotation to declare index hints for repository query, update and aggregate operations. The index is specified by
* its name.
*
*
* @author Christoph Strobl
* @since 4.1
*/
@ -35,12 +35,18 @@ import org.springframework.core.annotation.AliasFor; @@ -35,12 +35,18 @@ import org.springframework.core.annotation.AliasFor;
@Documented
public @interface Hint {
/**
* The name of the index to use. In case of an {@literal aggregation} the index is evaluated against the initial
* collection or view.
*
* @return the index name.
*/
String value() default "";
/**
* The name of the index to use. In case of an {@literal aggregation} the index is evaluated against the initial
* collection or view. Specify the index either by the index name.
*
* collection or view.
*
* @return the index name.
*/
@AliasFor("value")

3
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

@ -235,9 +235,10 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { @@ -235,9 +235,10 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
*/
Query applyHintIfPresent(Query query) {
if(!method.hasAnnotatedHint()) {
if (!method.hasAnnotatedHint()) {
return query;
}
return query.withHint(method.getAnnotatedHint());
}

3
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractReactiveMongoQuery.java

@ -279,9 +279,10 @@ public abstract class AbstractReactiveMongoQuery implements RepositoryQuery { @@ -279,9 +279,10 @@ public abstract class AbstractReactiveMongoQuery implements RepositoryQuery {
*/
Query applyHintIfPresent(Query query) {
if(!method.hasAnnotatedHint()) {
if (!method.hasAnnotatedHint()) {
return query;
}
return query.withHint(method.getAnnotatedHint());
}

3
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AggregationUtils.java

@ -111,9 +111,10 @@ abstract class AggregationUtils { @@ -111,9 +111,10 @@ abstract class AggregationUtils {
*/
static AggregationOptions.Builder applyHint(AggregationOptions.Builder builder, MongoQueryMethod queryMethod) {
if(!queryMethod.hasAnnotatedHint()) {
if (!queryMethod.hasAnnotatedHint()) {
return builder;
}
return builder.hint(queryMethod.getAnnotatedHint());
}

11
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

@ -368,20 +368,19 @@ public class MongoQueryMethod extends QueryMethod { @@ -368,20 +368,19 @@ public class MongoQueryMethod extends QueryMethod {
* @since 4.1
*/
public boolean hasAnnotatedHint() {
return StringUtils.hasText(getAnnotatedHint());
return doFindAnnotation(Hint.class).map(Hint::indexName).filter(StringUtils::hasText).isPresent();
}
/**
* Returns the aggregation pipeline declared via a {@link Hint} annotation.
*
* @return the index name (might be empty) or {@literal null} if not present.
* @return the index name (might be empty).
* @throws IllegalStateException if the method is not annotated with {@link Hint}
* @since 4.1
*/
@Nullable
public String getAnnotatedHint() {
Optional<Hint> hint = doFindAnnotation(Hint.class);
return hint.map(Hint::indexName).orElse(null);
return doFindAnnotation(Hint.class).map(Hint::indexName).orElseThrow(() -> new IllegalStateException(
"Expected to find @Hint annotation but did not; Make sure to check hasAnnotatedHint() before."));
}
private Optional<String[]> findAnnotatedAggregation() {

7
src/main/asciidoc/reference/mongo-repositories.adoc

@ -306,14 +306,15 @@ The `@Hint` annotation allows to override MongoDB's default index selection and @@ -306,14 +306,15 @@ The `@Hint` annotation allows to override MongoDB's default index selection and
====
[source,java]
----
@Hint("lastname-idx") <1>
@Hint("lastname-idx") <1>
List<Person> findByLastname(String lastname);
@Query(value = "{ 'firstname' : ?0 }", hint="firstname-idx") <2>
@Query(value = "{ 'firstname' : ?0 }", hint = "firstname-idx") <2>
List<Person> findByFirstname(String firstname);
----
<1> Use the index with name `lastname-idx`.
<2> The `@Query` annotation defines the `hint` alias which is equivalent to explicitly adding the `@Hint` annotation.
<2> The `@Query` annotation defines the `hint` alias which is equivalent to adding the `@Hint` annotation.
====
[[mongodb.repositories.queries.update]]

Loading…
Cancel
Save