Polishing.
Reformat code. Make getAnnotatedHint non-nullable. See #3230 Original pull request: #4339
This commit is contained in:
@@ -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;
|
||||
@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")
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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]]
|
||||
|
||||
Reference in New Issue
Block a user