DATAMONGO-1183 - Polishing.

Tweak Javadoc and reference docs. Migrate tests to AssertJ. Remove not needed warning suppressions.

Original pull request: #750.
This commit is contained in:
Mark Paluch
2019-05-28 10:11:27 +02:00
parent 89843a1488
commit d937460351
8 changed files with 54 additions and 58 deletions

View File

@@ -21,43 +21,37 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marker interface for a property that should be used as key for a
* <a href="https://docs.mongodb.com/manual/core/index-hashed/">Hashed Index</a>. If used on a simple property the index
* uses a hashing function to compute the hash of the value of the index field. Added to a property of complex type the
* embedded document is collapsed and the hash computed for the entire object.
* Annotation for a property that should be used as key for a
* <a href="https://docs.mongodb.com/manual/core/index-hashed/">Hashed Index</a>. If used on a simple property, the
* index uses a hashing function to compute the hash of the value of the index field. Added to a property of complex
* type the embedded document is collapsed and the hash computed for the entire object.
* <p />
*
* <pre>
* <code>
*
* <pre class="code">
* &#64;Document
* public class DomainType {
*
* &#64;HashIndexed @Id String id;
* &#64;HashIndexed @Id String id;
* }
* </code>
* </pre>
*
* {@link HashIndexed} can also be used as meta {@link java.lang.annotation.Annotation} to create composed annotations.
*
* <pre>
* <code>
* {@link HashIndexed} can also be used as meta {@link java.lang.annotation.Annotation} to create composed annotations:
*
* <pre class="code">
* &#64;Indexed
* &#64;HashIndexed
* &#64;Retention(RetentionPolicy.RUNTIME)
* public @interface IndexAndHash {
*
* &#64;AliasFor(annotation = Indexed.class, attribute = "name")
* String name() default "";
* &#64;AliasFor(annotation = Indexed.class, attribute = "name")
* String name() default "";
* }
*
* &#64;Document
* public class DomainType {
*
* &#64;ComposedHashIndexed(name = "idx-name") String value;
* &#64;ComposedHashIndexed(name = "idx-name") String value;
* }
* </code>
* </pre>
*
* @author Christoph Strobl
@@ -67,5 +61,4 @@ import java.lang.annotation.Target;
@Target({ ElementType.ANNOTATION_TYPE, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface HashIndexed {
}

View File

@@ -26,7 +26,6 @@ import org.springframework.util.ObjectUtils;
* @author Oliver Gierke
* @author Christoph Strobl
*/
@SuppressWarnings("deprecation")
public final class IndexField {
enum Type {

View File

@@ -103,19 +103,20 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
}
/**
* Resolve the {@link IndexDefinition}s for given {@literal root} entity by traversing {@link MongoPersistentProperty}
* scanning for index annotations {@link Indexed}, {@link CompoundIndex} and {@link GeospatialIndex}. The given
* {@literal root} has therefore to be annotated with {@link Document}.
* Resolve the {@link IndexDefinition}s for a given {@literal root} entity by traversing
* {@link MongoPersistentProperty} scanning for index annotations {@link Indexed}, {@link CompoundIndex} and
* {@link GeospatialIndex}. The given {@literal root} has therefore to be annotated with {@link Document}.
*
* @param root must not be null.
* @return List of {@link IndexDefinitionHolder}. Will never be {@code null}.
* @throws IllegalArgumentException in case of missing {@link Document} annotation marking root entities.
*/
public List<IndexDefinitionHolder> resolveIndexForEntity(final MongoPersistentEntity<?> root) {
public List<IndexDefinitionHolder> resolveIndexForEntity(MongoPersistentEntity<?> root) {
Assert.notNull(root, "Index cannot be resolved for given 'null' entity.");
Assert.notNull(root, "MongoPersistentEntity must not be null!");
Document document = root.findAnnotation(Document.class);
Assert.notNull(document, "Given entity is not collection root.");
Assert.notNull(document, () -> String
.format("Entity %s is not a collection root. Make sure to annotate it with @Document!", root.getName()));
List<IndexDefinitionHolder> indexInformation = new ArrayList<>();
String collection = root.getCollection();
@@ -201,7 +202,6 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
}
}
@Nullable
private List<IndexDefinitionHolder> createIndexDefinitionHolderForProperty(String dotPath, String collection,
MongoPersistentProperty persistentProperty) {
@@ -328,7 +328,8 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
}
/**
* Create {@link IndexDefinition} wrapped in {@link IndexDefinitionHolder} for {@link CompoundIndexes} of given type.
* Create {@link IndexDefinition} wrapped in {@link IndexDefinitionHolder} for {@link CompoundIndexes} of a given
* type.
*
* @param dotPath The properties {@literal "dot"} path representation from its document root.
* @param fallbackCollection
@@ -410,7 +411,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
}
/**
* Creates {@link IndexDefinition} wrapped in {@link IndexDefinitionHolder} out of {@link Indexed} for given
* Creates {@link IndexDefinition} wrapped in {@link IndexDefinitionHolder} out of {@link Indexed} for a given
* {@link MongoPersistentProperty}.
*
* @param dotPath The properties {@literal "dot"} path representation from its document root.
@@ -471,7 +472,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
}
/**
* Creates {@link HashedIndex} wrapped in {@link IndexDefinitionHolder} out of {@link HashIndexed} for given
* Creates {@link HashedIndex} wrapped in {@link IndexDefinitionHolder} out of {@link HashIndexed} for a given
* {@link MongoPersistentProperty}.
*
* @param dotPath The properties {@literal "dot"} path representation from its document root.

View File

@@ -42,6 +42,8 @@ import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.IndexOptions;
/**
* Unit tests for {@link DefaultIndexOperations}.
*
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)

View File

@@ -15,10 +15,10 @@
*/
package org.springframework.data.mongodb.core.index;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.springframework.data.domain.Sort.Direction;
/**
@@ -27,7 +27,6 @@ import org.springframework.data.domain.Sort.Direction;
* @author Oliver Gierke
* @author Christoph Strobl
*/
@SuppressWarnings("deprecation")
public class IndexFieldUnitTests {
@Test
@@ -35,9 +34,9 @@ public class IndexFieldUnitTests {
IndexField field = IndexField.create("foo", Direction.ASC);
assertThat(field.getKey(), is("foo"));
assertThat(field.getDirection(), is(Direction.ASC));
assertThat(field.isGeo(), is(false));
assertThat(field.getKey()).isEqualTo("foo");
assertThat(field.getDirection()).isEqualTo(Direction.ASC);
assertThat(field.isGeo()).isFalse();
}
@Test
@@ -45,9 +44,9 @@ public class IndexFieldUnitTests {
IndexField field = IndexField.geo("foo");
assertThat(field.getKey(), is("foo"));
assertThat(field.getDirection(), is(nullValue()));
assertThat(field.isGeo(), is(true));
assertThat(field.getKey()).isEqualTo("foo");
assertThat(field.getDirection()).isNull();
assertThat(field.isGeo()).isTrue();
}
@Test
@@ -56,8 +55,8 @@ public class IndexFieldUnitTests {
IndexField first = IndexField.create("foo", Direction.ASC);
IndexField second = IndexField.create("foo", Direction.ASC);
assertThat(first, is(second));
assertThat(second, is(first));
assertThat(first).isEqualTo(second);
assertThat(second).isEqualTo(first);
}
@Test
@@ -66,13 +65,13 @@ public class IndexFieldUnitTests {
IndexField first = IndexField.geo("bar");
IndexField second = IndexField.geo("bar");
assertThat(first, is(second));
assertThat(second, is(first));
assertThat(first).isEqualTo(second);
assertThat(second).isEqualTo(first);
}
@Test // DATAMONGO-1183
public void correctTypeForHashedFields() {
assertThat(IndexField.hashed("key").isHashed(), is(true));
assertThat(IndexField.hashed("key").isHashed()).isTrue();
}
@Test // DATAMONGO-1183
@@ -81,7 +80,7 @@ public class IndexFieldUnitTests {
IndexField first = IndexField.hashed("bar");
IndexField second = IndexField.hashed("bar");
assertThat(first, is(second));
assertThat(second, is(first));
assertThat(first).isEqualTo(second);
assertThat(second).isEqualTo(first);
}
}

View File

@@ -1182,7 +1182,7 @@ public class MongoPersistentEntityIndexResolverUnitTests {
assertThat(indexDefinitions).hasSize(1);
assertThat(indexDefinitions.get(0)).satisfies(it -> {
assertThat(it.getIndexKeys()).isEqualTo(new org.bson.Document("_id", "hashed"));
assertThat(it.getIndexKeys()).hasSize(1).containsEntry("_id", "hashed");
});
}
@@ -1193,7 +1193,7 @@ public class MongoPersistentEntityIndexResolverUnitTests {
assertThat(indexDefinitions).hasSize(1);
assertThat(indexDefinitions.get(0)).satisfies(it -> {
assertThat(it.getIndexKeys()).isEqualTo(new org.bson.Document("value", "hashed"));
assertThat(it.getIndexKeys()).hasSize(1).containsEntry("value", "hashed");
});
}
@@ -1205,10 +1205,10 @@ public class MongoPersistentEntityIndexResolverUnitTests {
assertThat(indexDefinitions).hasSize(2);
assertThat(indexDefinitions.get(0)).satisfies(it -> {
assertThat(it.getIndexKeys()).isEqualTo(new org.bson.Document("value", 1));
assertThat(it.getIndexKeys()).containsEntry("value", 1);
});
assertThat(indexDefinitions.get(1)).satisfies(it -> {
assertThat(it.getIndexKeys()).isEqualTo(new org.bson.Document("value", "hashed"));
assertThat(it.getIndexKeys()).containsEntry("value", "hashed");
});
}
@@ -1220,11 +1220,11 @@ public class MongoPersistentEntityIndexResolverUnitTests {
assertThat(indexDefinitions).hasSize(2);
assertThat(indexDefinitions.get(0)).satisfies(it -> {
assertThat(it.getIndexKeys()).isEqualTo(new org.bson.Document("value", 1));
assertThat(it.getIndexKeys()).containsEntry("value", 1);
assertThat(it.getIndexOptions()).containsEntry("name", "idx-name");
});
assertThat(indexDefinitions.get(1)).satisfies(it -> {
assertThat(it.getIndexKeys()).isEqualTo(new org.bson.Document("value", "hashed"));
assertThat(it.getIndexKeys()).containsEntry("value", "hashed");
});
}

View File

@@ -19,6 +19,7 @@
* Annotation-based Collation support through `@Document` and `@Query`.
* <<mongodb.repositories.queries.aggregation, Aggregation framework>> support via repository query methods.
* Declarative reactive transactions using <<mongo.transactions.reactive-tx-manager, @Transactional>>.
* Support for <<mapping-usage-indexes.hashed-index>>.
[[new-features.2-1-0]]
== What's New in Spring Data MongoDB 2.1

View File

@@ -606,8 +606,9 @@ public class Person {
[[mapping-usage-indexes.hashed-index]]
=== Hashed Indexes
Hashed indexes allow hash based sharding within a sharded cluster. The hashed field value is used to shard collection results
in a more random distribution. For details refer to the https://docs.mongodb.com/manual/core/index-hashed/[MongoDB Documentation].
Hashed indexes allow hash based sharding within a sharded cluster.
Using hashed field values to shard collections results in a more random distribution.
For details, refer to the https://docs.mongodb.com/manual/core/index-hashed/[MongoDB Documentation].
Here's an example that creates a hashed index for `_id`:
@@ -625,7 +626,7 @@ public class DomainType {
----
====
Hashed indexes can be created next to other index definitions like shown below, in that case both indices will be created.
Hashed indexes can be created next to other index definitions like shown below, in that case both indices are created:
.Example Hashed Index Usage togehter with simple index
====
@@ -643,7 +644,7 @@ public class DomainType {
----
====
In case the above example is too verbose, a compound annotation allows to reduce the number of annotations present.
In case the example above is too verbose, a compound annotation allows to reduce the number of annotations that need to be declared on a property:
.Example Composed Hashed Index Usage
====
@@ -652,7 +653,7 @@ In case the above example is too verbose, a compound annotation allows to reduce
@Document
public class DomainType {
@IndexAndHash(name = "idx...") <1>
@IndexAndHash(name = "idx...") <1>
String value;
// ...
@@ -672,7 +673,7 @@ public @interface IndexAndHash {
[NOTE]
====
Although index creation via annotations comes in handy for many scenarios please cosider taking over more control by setting up indices manually via `IndexOperations`.
Although index creation via annotations comes in handy for many scenarios cosider taking over more control by setting up indices manually via `IndexOperations`.
[source,java]
----
@@ -684,7 +685,7 @@ mongoOperations.indexOpsFor(Jedi.class)
[[mapping-usage-indexes.text-index]]
=== Text Indexes
NOTE: The text index feature is disabled by default for mongodb v.2.4.
NOTE: The text index feature is disabled by default for MongoDB v.2.4.
Creating a text index allows accumulating several fields into a searchable full-text index. It is only possible to have one text index per collection, so all fields marked with `@TextIndexed` are combined into this index. Properties can be weighted to influence the document score for ranking results. The default language for the text index is English. To change the default language, set the `language` attribute to whichever language you want (for example,`@Document(language="spanish")`). Using a property called `language` or `@Language` lets you define a language override on a per document base. The following example shows how to created a text index and set the language to Spanish: