diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java index 320a9d4e1..0d6b2ac39 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java @@ -28,10 +28,10 @@ import org.springframework.util.Assert; */ public class CollectionOptions { - private Integer maxDocuments; - private Integer size; + private Long maxDocuments; + private Long size; private Boolean capped; - private Optional collation; + private Collation collation; /** * Constructs a new CollectionOptions instance. @@ -40,12 +40,14 @@ public class CollectionOptions { * @param maxDocuments the maximum number of documents in the collection. * @param capped true to created a "capped" collection (fixed size with auto-FIFO behavior based on insertion order), * false otherwise. + * @deprecated since 2.0 please use {@link CollectionOptions#empty()} as entry point. */ - public CollectionOptions(Integer size, Integer maxDocuments, Boolean capped) { - this(size, maxDocuments, capped, Optional.empty()); + @Deprecated + public CollectionOptions(Long size, Long maxDocuments, Boolean capped) { + this(size, maxDocuments, capped, null); } - private CollectionOptions(Integer size, Integer maxDocuments, Boolean capped, Optional collation) { + private CollectionOptions(Long size, Long maxDocuments, Boolean capped, Collation collation) { this.maxDocuments = maxDocuments; this.size = size; @@ -53,10 +55,6 @@ public class CollectionOptions { this.collation = collation; } - private CollectionOptions() { - this.collation = Optional.empty(); - } - /** * Create new {@link CollectionOptions} by just providing the {@link Collation} to use. * @@ -68,9 +66,7 @@ public class CollectionOptions { Assert.notNull(collation, "Collation must not be null!"); - CollectionOptions options = new CollectionOptions(); - options.setCollation(collation); - return options; + return new CollectionOptions(null, null, null, collation); } /** @@ -80,17 +76,17 @@ public class CollectionOptions { * @since 2.0 */ public static CollectionOptions empty() { - return new CollectionOptions(); + return new CollectionOptions(null, null, null, null); } /** - * Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}. + * Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}.
+ * NOTE Using capped collections requires defining {@link #size(int)}. * - * @param size the collection size in bytes, this data space is preallocated. * @return new {@link CollectionOptions}. * @since 2.0 */ - public CollectionOptions capped(int size) { + public CollectionOptions capped() { return new CollectionOptions(size, maxDocuments, true, collation); } @@ -101,7 +97,7 @@ public class CollectionOptions { * @return new {@link CollectionOptions}. * @since 2.0 */ - public CollectionOptions maxDocuments(Integer maxDocuments) { + public CollectionOptions maxDocuments(long maxDocuments) { return new CollectionOptions(size, maxDocuments, capped, collation); } @@ -112,7 +108,7 @@ public class CollectionOptions { * @return new {@link CollectionOptions}. * @since 2.0 */ - public CollectionOptions size(int size) { + public CollectionOptions size(long size) { return new CollectionOptions(size, maxDocuments, capped, collation); } @@ -124,50 +120,44 @@ public class CollectionOptions { * @since 2.0 */ public CollectionOptions collation(Collation collation) { - return new CollectionOptions(size, maxDocuments, capped, Optional.ofNullable(collation)); - } - - public Integer getMaxDocuments() { - return maxDocuments; - } - - public void setMaxDocuments(Integer maxDocuments) { - this.maxDocuments = maxDocuments; - } - - public Integer getSize() { - return size; - } - - public void setSize(Integer size) { - this.size = size; - } - - public Boolean getCapped() { - return capped; - } - - public void setCapped(Boolean capped) { - this.capped = capped; + return new CollectionOptions(size, maxDocuments, capped, collation); } /** - * Set {@link Collation} options. + * Get the max number of documents the collection should be limited to. * - * @param collation + * @return {@link Optional#empty()} if not set. + */ + public Optional getMaxDocuments() { + return Optional.ofNullable(maxDocuments); + } + + /** + * Get the {@literal size} in bytes the collection should be limited to. + * + * @return {@link Optional#empty()} if not set. + */ + public Optional getSize() { + return Optional.ofNullable(size); + } + + /** + * Get if the collection should be capped. + * + * @return {@link Optional#empty()} if not set. * @since 2.0 */ - public void setCollation(Collation collation) { - this.collation = Optional.ofNullable(collation); + public Optional getCapped() { + return Optional.ofNullable(capped); } /** * Get the {@link Collation} settings. * - * @return + * @return {@link Optional#empty()} if not set. * @since 2.0 */ public Optional getCollation() { - return collation; + return Optional.ofNullable(collation); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 7b418aafa..ade633bf5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -20,8 +20,19 @@ import static org.springframework.data.mongodb.core.query.SerializationUtils.*; import static org.springframework.data.util.Optionals.*; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; +import java.util.Scanner; +import java.util.Set; import java.util.concurrent.TimeUnit; import org.bson.Document; @@ -1951,16 +1962,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, Document document = new Document(); if (collectionOptions != null) { - if (collectionOptions.getCapped() != null) { - document.put("capped", collectionOptions.getCapped().booleanValue()); - } - if (collectionOptions.getSize() != null) { - document.put("size", collectionOptions.getSize().intValue()); - } - if (collectionOptions.getMaxDocuments() != null) { - document.put("max", collectionOptions.getMaxDocuments().intValue()); - } + collectionOptions.getCapped().ifPresent(val -> document.put("capped", val)); + collectionOptions.getSize().ifPresent(val -> document.put("size", val)); + collectionOptions.getMaxDocuments().ifPresent(val -> document.put("max", val)); collectionOptions.getCollation().ifPresent(val -> document.append("collation", val.toDocument())); } return document; diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java index c658e428c..e47885b13 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java @@ -1626,17 +1626,9 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati CreateCollectionOptions result = new CreateCollectionOptions(); if (collectionOptions != null) { - if (collectionOptions.getCapped() != null) { - result = result.capped(collectionOptions.getCapped()); - } - - if (collectionOptions.getSize() != null) { - result = result.sizeInBytes(collectionOptions.getSize()); - } - - if (collectionOptions.getMaxDocuments() != null) { - result = result.maxDocuments(collectionOptions.getMaxDocuments()); - } + collectionOptions.getCapped().ifPresent(result::capped); + collectionOptions.getSize().ifPresent(result::sizeInBytes); + collectionOptions.getMaxDocuments().ifPresent(result::maxDocuments); } return result; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java index ef71988a9..97529ac2f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java @@ -46,6 +46,7 @@ import com.mongodb.DBRef; * * @author Oliver Gierke * @author Thomas Darimont + * @author Christoph Strobl */ @RunWith(MockitoJUnitRunner.class) public abstract class MongoOperationsUnitTests { @@ -137,7 +138,7 @@ public abstract class MongoOperationsUnitTests { new Execution() { @Override public void doWith(MongoOperations operations) { - operations.createCollection("foo", new CollectionOptions(1, 1, true)); + operations.createCollection("foo", CollectionOptions.empty().size(1).maxDocuments(1).capped()); } }.assertDataAccessException(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index e1465f2ea..5cb6c43df 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -334,7 +334,7 @@ public class MongoTemplateTests { @Test // DATAMONGO-1687 public void createCappedCollection() { - template.createCollection(Person.class, CollectionOptions.empty().capped(1000).maxDocuments(1000)); + template.createCollection(Person.class, CollectionOptions.empty().capped().size(1000).maxDocuments(1000)); org.bson.Document collectionOptions = getCollectionInfo(template.getCollectionName(Person.class)).get("options", org.bson.Document.class); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateTests.java index 9a6e0d06b..0ae2b6cf0 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateTests.java @@ -803,7 +803,7 @@ public class ReactiveMongoTemplateTests { StepVerifier.create(template.dropCollection("capped") .then(template.createCollection("capped", // - new CollectionOptions(1000, 10, true))) + CollectionOptions.empty().size(1000).maxDocuments(10).capped())) .then(template.insert(new Document("random", Math.random()).append("key", "value"), // "capped"))) .expectNextCount(1).verifyComplete(); @@ -825,7 +825,7 @@ public class ReactiveMongoTemplateTests { StepVerifier.create(template.dropCollection("capped") .then(template.createCollection("capped", // - new CollectionOptions(1000, 10, true))) + CollectionOptions.empty().size(1000).maxDocuments(10).capped())) .then(template.insert(new Document("random", Math.random()).append("key", "value"), // "capped"))) .expectNextCount(1).verifyComplete(); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java index f732810e3..caecbda01 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java @@ -63,6 +63,7 @@ import org.springframework.util.ClassUtils; * Test for {@link ReactiveMongoRepository} query methods. * * @author Mark Paluch + * @author Christoph Strobl */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:reactive-infrastructure.xml") @@ -175,7 +176,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF StepVerifier .create(template.dropCollection(Capped.class) // .then(template.createCollection(Capped.class, // - new CollectionOptions(1000, 100, true)))) // + CollectionOptions.empty().size(1000).maxDocuments(100).capped()))) // .expectNextCount(1) // .verifyComplete(); @@ -200,7 +201,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF StepVerifier .create(template.dropCollection(Capped.class) // .then(template.createCollection(Capped.class, // - new CollectionOptions(1000, 100, true)))) // + CollectionOptions.empty().size(1000).maxDocuments(100).capped()))) // .expectNextCount(1) // .verifyComplete();