Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Tweak documentation wording. Refine constructor nullability rules. Fix javadoc references.

Original Pull Request: #4503
  • Loading branch information
mp911de authored and christophstrobl committed Oct 12, 2023
1 parent 715231e commit 74b07e5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
/**
* The {@link Aggregation} annotation can be used to annotate a {@link org.springframework.data.repository.Repository}
* query method so that it runs the {@link Aggregation#pipeline()} on invocation.
* <br />
* <p>
* Pipeline stages are mapped against the {@link org.springframework.data.repository.Repository} domain type to consider
* {@link org.springframework.data.mongodb.core.mapping.Field field} mappings and may contain simple placeholders
* {@code ?0} as well as {@link org.springframework.expression.spel.standard.SpelExpression SpelExpressions}.
* <br />
* <p>
* Query method {@link org.springframework.data.domain.Sort} and {@link org.springframework.data.domain.Pageable}
* arguments are applied at the end of the pipeline or can be defined manually as part of it.
*
Expand Down Expand Up @@ -130,8 +130,8 @@
String collation() default "";

/**
* The mode of the read preference to use. <br />
* {@code @Aggregation(pipeline = { ... }, readPreference = "secondary")} can be used as shortcut for:
* The mode of the read preference to use. This attribute ({@code @Aggregation(pipeline = { ... }, readPreference =
* "secondary")}) is an alias for:
*
* <pre class="code">
* &#64;@Aggregation(pipeline = { ... })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@
boolean delete() default false;

/**
* Defines a default sort order for the given query.<br />
* <strong>NOTE:</strong> The so set defaults can be altered / overwritten using an explicit
* {@link org.springframework.data.domain.Sort} argument of the query method.
* Defines a default sort order for the given query. <strong>NOTE:</strong> The so set defaults can be altered /
* overwritten using an explicit {@link org.springframework.data.domain.Sort} argument of the query method.
*
* <pre>
* <code>
Expand Down Expand Up @@ -134,8 +133,7 @@
String collation() default "";

/**
* The name of the index to use. <br />
* {@code @Query(value = "...", hint = "lastname-idx")} can be used as shortcut for:
* The name of the index to use. {@code @Query(value = "...", hint = "lastname-idx")} can be used as shortcut for:
*
* <pre class="code">
* &#64;Query(...)
Expand All @@ -151,8 +149,8 @@
String hint() default "";

/**
* The mode of the read preference to use. <br />
* {@code @Query(value = "...", readPreference = "secondary")} can be used as shortcut for:
* The mode of the read preference to use. This attribute
* ({@code @Query(value = "...", readPreference = "secondary")}) is an alias for:
*
* <pre class="code">
* &#64;Query(...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@
* @author Jorge Rodríguez
* @author Christoph Strobl
* @since 4.2
* @see com.mongodb.ReadPreference
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Documented
public @interface ReadPreference {

/**
* Configure read preference mode
* @return read preference mode
* Configure the read preference mode.
*
* @return read preference mode.
*/
String value() default "";

}

Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ public org.springframework.data.mongodb.core.query.Meta getQueryMetaAttributes()
}

/**
* Check if the query method is decorated with an non empty {@link Query#sort()}.
* Check if the query method is decorated with a non-empty {@link Query#sort()}.
*
* @return true if method annotated with {@link Query} having an non empty sort attribute.
* @return true if method annotated with {@link Query} having a non-empty sort attribute.
* @since 2.1
*/
public boolean hasAnnotatedSort() {
Expand All @@ -316,9 +316,8 @@ public String getAnnotatedSort() {
"Expected to find @Query annotation but did not; Make sure to check hasAnnotatedSort() before."));
}


/**
* Check if the query method is decorated with an non empty {@link ReadPreference}.
* Check if the query method is decorated with a non-empty {@link ReadPreference}.
*
* @return true if method annotated with {@link Query} or {@link Aggregation} having a non-empty collation attribute.
* @since 4.2
Expand All @@ -332,18 +331,20 @@ public boolean hasAnnotatedReadPreference() {
*
* @return the name of the {@link ReadPreference()}.
* @throws IllegalStateException if method not annotated with {@link Query}. Make sure to check
* {@link #hasAnnotatedQuery()} first.
* {@link #hasAnnotatedReadPreference()} first.
* @since 4.2
*/
public String getAnnotatedReadPreference() {

return doFindReadPreferenceAnnotation().map(ReadPreference::value).orElseThrow(() -> new IllegalStateException(
"Expected to find @ReadPreference annotation but did not; Make sure to check hasAnnotatedReadPreference() before."));
"Expected to find @ReadPreference annotation but did not; Make sure to check hasAnnotatedReadPreference() before."));
}

/**
* Get {@link com.mongodb.ReadPreference#getName() name} from query. First check if the method is annotated. If not, check if the class is annotated.
* So if the method and the class are annotated with @ReadPreference, the method annotation takes precedence.
* Get {@link com.mongodb.ReadPreference#getName() name} from query. First check if the method is annotated. If not,
* check if the class is annotated. So if the method and the class are annotated with @ReadPreference, the method
* annotation takes precedence.
*
* @return the {@link ReadPreference}
* @since 4.2
*/
Expand All @@ -352,7 +353,7 @@ private Optional<ReadPreference> doFindReadPreferenceAnnotation() {
}

/**
* Check if the query method is decorated with an non empty {@link Query#collation()} or or
* Check if the query method is decorated with a non-empty {@link Query#collation()} or
* {@link Aggregation#collation()}.
*
* @return true if method annotated with {@link Query} or {@link Aggregation} having a non-empty collation attribute.
Expand All @@ -365,7 +366,7 @@ public boolean hasAnnotatedCollation() {
/**
* Get the collation value extracted from the {@link Query} or {@link Aggregation} annotation.
*
* @return the {@link Query#collation()} or or {@link Aggregation#collation()} value.
* @return the {@link Query#collation()} or {@link Aggregation#collation()} value.
* @throws IllegalStateException if method not annotated with {@link Query} or {@link Aggregation}. Make sure to check
* {@link #hasAnnotatedQuery()} first.
* @since 2.2
Expand Down Expand Up @@ -443,9 +444,10 @@ private <A extends Annotation> Optional<A> doFindAnnotation(Class<A> annotationT
}

@SuppressWarnings("unchecked")
private <A extends Annotation> Optional<A> doFindAnnotationInClass(Class<A> annotationType) {
private <A extends Annotation> Optional<A> doFindAnnotationInClass(Class<A> annotationType) {

Optional<Annotation> mergedAnnotation = Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), annotationType));
Optional<Annotation> mergedAnnotation = Optional
.ofNullable(AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), annotationType));
annotationCache.put(annotationType, mergedAnnotation);

return (Optional<A>) mergedAnnotation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,22 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.mongodb.ReadPreference;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Window;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Window;
import org.springframework.data.mongodb.core.ExecutableFindOperation;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.ReadPreferenceAware;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.ReadPreference;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.support.PageableExecutionUtils;
Expand All @@ -67,45 +66,51 @@
*/
public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {

private final @Nullable RepositoryMetadata repositoryMetadata;
private final MongoEntityInformation<T, ID> entityInformation;
private final MongoOperations mongoOperations;
private final Lazy<ReadPreference> readPreference;
private final Lazy<com.mongodb.ReadPreference> readPreference;

/**
* Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
*
* @param metadata must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
*/
public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
this(null, metadata, mongoOperations);
this(null, metadata, mongoOperations, null);
}

/**
* Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
*
* @param repositoryMetadata
* @param repositoryMetadata must not be {@literal null}.
* @param metadata must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
* @since 4.2
*/
public SimpleMongoRepository(@Nullable RepositoryMetadata repositoryMetadata, MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
public SimpleMongoRepository(RepositoryMetadata repositoryMetadata, MongoEntityInformation<T, ID> metadata,
MongoOperations mongoOperations) {
this(repositoryMetadata, metadata, mongoOperations, null);
}

private SimpleMongoRepository(@Nullable RepositoryMetadata repositoryMetadata, MongoEntityInformation<T, ID> metadata,
MongoOperations mongoOperations, @Nullable Object marker) {

Assert.notNull(metadata, "MongoEntityInformation must not be null");
Assert.notNull(mongoOperations, "MongoOperations must not be null");

this.repositoryMetadata = repositoryMetadata;
this.entityInformation = metadata;
this.mongoOperations = mongoOperations;

this.readPreference = repositoryMetadata == null ? Lazy.empty() : Lazy.of(() -> {
org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils.findMergedAnnotation(repositoryMetadata.getRepositoryInterface(), org.springframework.data.mongodb.repository.ReadPreference.class);
if (preference == null) {
return null;
}
return ReadPreference.valueOf(preference.value());
}
);
this.readPreference = repositoryMetadata == null ? Lazy.empty() : Lazy.of(() -> {
ReadPreference preference = AnnotatedElementUtils
.findMergedAnnotation(repositoryMetadata.getRepositoryInterface(), ReadPreference.class);

if (preference == null) {
return null;
}
return com.mongodb.ReadPreference.valueOf(preference.value());
});
}

// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

import static org.springframework.data.mongodb.core.query.Criteria.*;

import com.mongodb.ReadPreference;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand All @@ -35,23 +29,29 @@
import java.util.stream.Collectors;

import org.reactivestreams.Publisher;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Window;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Window;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.ReactiveFindOperation;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.data.mongodb.repository.ReadPreference;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import com.mongodb.client.result.DeleteResult;
Expand All @@ -69,42 +69,48 @@
*/
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {

private final @Nullable RepositoryMetadata repositoryMetadata;
private final MongoEntityInformation<T, ID> entityInformation;
private final ReactiveMongoOperations mongoOperations;
private final Lazy<ReadPreference> readPreference;
private final Lazy<com.mongodb.ReadPreference> readPreference;

public SimpleReactiveMongoRepository(MongoEntityInformation<T, ID> entityInformation,
ReactiveMongoOperations mongoOperations) {
this(null, entityInformation, mongoOperations);
this(null, entityInformation, mongoOperations, null);
}

/**
* Creates a new {@link SimpleReactiveMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
* Creates a new {@link SimpleReactiveMongoRepository} for the given {@link MongoEntityInformation} and
* {@link MongoTemplate}.
*
* @param repositoryMetadata
* @param repositoryMetadata must not be {@literal null}.
* @param entityInformation must not be {@literal null}.
* @param mongoOperations must not be {@literal null}.
* @since 4.2
*/
public SimpleReactiveMongoRepository(@Nullable RepositoryMetadata repositoryMetadata, MongoEntityInformation<T, ID> entityInformation,
ReactiveMongoOperations mongoOperations) {
public SimpleReactiveMongoRepository(RepositoryMetadata repositoryMetadata,
MongoEntityInformation<T, ID> entityInformation, ReactiveMongoOperations mongoOperations) {
this(repositoryMetadata, entityInformation, mongoOperations, null);
}

private SimpleReactiveMongoRepository(@Nullable RepositoryMetadata repositoryMetadata,
MongoEntityInformation<T, ID> entityInformation, ReactiveMongoOperations mongoOperations,
@Nullable Object marker) {

Assert.notNull(entityInformation, "EntityInformation must not be null");
Assert.notNull(mongoOperations, "MongoOperations must not be null");

this.repositoryMetadata = repositoryMetadata;
this.entityInformation = entityInformation;
this.mongoOperations = mongoOperations;

this.readPreference = repositoryMetadata == null ? Lazy.empty() : Lazy.of(() -> {
org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils.findMergedAnnotation(repositoryMetadata.getRepositoryInterface(), org.springframework.data.mongodb.repository.ReadPreference.class);
if (preference == null) {
return null;
}
return ReadPreference.valueOf(preference.value());
}
);
this.readPreference = repositoryMetadata == null ? Lazy.empty() : Lazy.of(() -> {

ReadPreference preference = AnnotatedElementUtils
.findMergedAnnotation(repositoryMetadata.getRepositoryInterface(), ReadPreference.class);
if (preference == null) {
return null;
}
return com.mongodb.ReadPreference.valueOf(preference.value());
});
}

// -------------------------------------------------------------------------
Expand Down

0 comments on commit 74b07e5

Please sign in to comment.