Skip to content

Commit 89c6099

Browse files
committed
Polishing.
Reformat code. Make getAnnotatedHint non-nullable. See #3230 Original pull request: #4339
1 parent 7b44f78 commit 89c6099

File tree

6 files changed

+24
-15
lines changed

6 files changed

+24
-15
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Annotation to declare index hints for repository query, update and aggregate operations. The index is specified by
2828
* its name.
29-
*
29+
*
3030
* @author Christoph Strobl
3131
* @since 4.1
3232
*/
@@ -35,12 +35,18 @@
3535
@Documented
3636
public @interface Hint {
3737

38+
/**
39+
* The name of the index to use. In case of an {@literal aggregation} the index is evaluated against the initial
40+
* collection or view.
41+
*
42+
* @return the index name.
43+
*/
3844
String value() default "";
3945

4046
/**
4147
* The name of the index to use. In case of an {@literal aggregation} the index is evaluated against the initial
42-
* collection or view. Specify the index either by the index name.
43-
*
48+
* collection or view.
49+
*
4450
* @return the index name.
4551
*/
4652
@AliasFor("value")

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ Query applyAnnotatedCollationIfPresent(Query query, ConvertingParameterAccessor
235235
*/
236236
Query applyHintIfPresent(Query query) {
237237

238-
if(!method.hasAnnotatedHint()) {
238+
if (!method.hasAnnotatedHint()) {
239239
return query;
240240
}
241+
241242
return query.withHint(method.getAnnotatedHint());
242243
}
243244

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,10 @@ Query applyAnnotatedCollationIfPresent(Query query, ConvertingParameterAccessor
279279
*/
280280
Query applyHintIfPresent(Query query) {
281281

282-
if(!method.hasAnnotatedHint()) {
282+
if (!method.hasAnnotatedHint()) {
283283
return query;
284284
}
285+
285286
return query.withHint(method.getAnnotatedHint());
286287
}
287288

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ static AggregationOptions.Builder applyMeta(AggregationOptions.Builder builder,
111111
*/
112112
static AggregationOptions.Builder applyHint(AggregationOptions.Builder builder, MongoQueryMethod queryMethod) {
113113

114-
if(!queryMethod.hasAnnotatedHint()) {
114+
if (!queryMethod.hasAnnotatedHint()) {
115115
return builder;
116116
}
117+
117118
return builder.hint(queryMethod.getAnnotatedHint());
118119
}
119120

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -368,20 +368,19 @@ public String[] getAnnotatedAggregation() {
368368
* @since 4.1
369369
*/
370370
public boolean hasAnnotatedHint() {
371-
return StringUtils.hasText(getAnnotatedHint());
371+
return doFindAnnotation(Hint.class).map(Hint::indexName).filter(StringUtils::hasText).isPresent();
372372
}
373373

374374
/**
375375
* Returns the aggregation pipeline declared via a {@link Hint} annotation.
376376
*
377-
* @return the index name (might be empty) or {@literal null} if not present.
377+
* @return the index name (might be empty).
378+
* @throws IllegalStateException if the method is not annotated with {@link Hint}
378379
* @since 4.1
379380
*/
380-
@Nullable
381381
public String getAnnotatedHint() {
382-
383-
Optional<Hint> hint = doFindAnnotation(Hint.class);
384-
return hint.map(Hint::indexName).orElse(null);
382+
return doFindAnnotation(Hint.class).map(Hint::indexName).orElseThrow(() -> new IllegalStateException(
383+
"Expected to find @Hint annotation but did not; Make sure to check hasAnnotatedHint() before."));
385384
}
386385

387386
private Optional<String[]> findAnnotatedAggregation() {

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,15 @@ The `@Hint` annotation allows to override MongoDB's default index selection and
306306
====
307307
[source,java]
308308
----
309-
@Hint("lastname-idx") <1>
309+
@Hint("lastname-idx") <1>
310310
List<Person> findByLastname(String lastname);
311311
312-
@Query(value = "{ 'firstname' : ?0 }", hint="firstname-idx") <2>
312+
@Query(value = "{ 'firstname' : ?0 }", hint = "firstname-idx") <2>
313313
List<Person> findByFirstname(String firstname);
314314
----
315+
315316
<1> Use the index with name `lastname-idx`.
316-
<2> The `@Query` annotation defines the `hint` alias which is equivalent to explicitly adding the `@Hint` annotation.
317+
<2> The `@Query` annotation defines the `hint` alias which is equivalent to adding the `@Hint` annotation.
317318
====
318319

319320
[[mongodb.repositories.queries.update]]

0 commit comments

Comments
 (0)