DATAMONGO-1687 - Polishing.
CollectionOptions is now immutable and returns Optional#empty for values not set. Some minor changes to JavaDoc and required updates for tests involved. Original Pull Request: #462.
This commit is contained in:
@@ -28,10 +28,10 @@ import org.springframework.util.Assert;
|
|||||||
*/
|
*/
|
||||||
public class CollectionOptions {
|
public class CollectionOptions {
|
||||||
|
|
||||||
private Integer maxDocuments;
|
private Long maxDocuments;
|
||||||
private Integer size;
|
private Long size;
|
||||||
private Boolean capped;
|
private Boolean capped;
|
||||||
private Optional<Collation> collation;
|
private Collation collation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new <code>CollectionOptions</code> instance.
|
* Constructs a new <code>CollectionOptions</code> instance.
|
||||||
@@ -40,12 +40,14 @@ public class CollectionOptions {
|
|||||||
* @param maxDocuments the maximum number of documents in the collection.
|
* @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),
|
* @param capped true to created a "capped" collection (fixed size with auto-FIFO behavior based on insertion order),
|
||||||
* false otherwise.
|
* false otherwise.
|
||||||
|
* @deprecated since 2.0 please use {@link CollectionOptions#empty()} as entry point.
|
||||||
*/
|
*/
|
||||||
public CollectionOptions(Integer size, Integer maxDocuments, Boolean capped) {
|
@Deprecated
|
||||||
this(size, maxDocuments, capped, Optional.empty());
|
public CollectionOptions(Long size, Long maxDocuments, Boolean capped) {
|
||||||
|
this(size, maxDocuments, capped, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CollectionOptions(Integer size, Integer maxDocuments, Boolean capped, Optional<Collation> collation) {
|
private CollectionOptions(Long size, Long maxDocuments, Boolean capped, Collation collation) {
|
||||||
|
|
||||||
this.maxDocuments = maxDocuments;
|
this.maxDocuments = maxDocuments;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
@@ -53,10 +55,6 @@ public class CollectionOptions {
|
|||||||
this.collation = collation;
|
this.collation = collation;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CollectionOptions() {
|
|
||||||
this.collation = Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new {@link CollectionOptions} by just providing the {@link Collation} to use.
|
* 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!");
|
Assert.notNull(collation, "Collation must not be null!");
|
||||||
|
|
||||||
CollectionOptions options = new CollectionOptions();
|
return new CollectionOptions(null, null, null, collation);
|
||||||
options.setCollation(collation);
|
|
||||||
return options;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,17 +76,17 @@ public class CollectionOptions {
|
|||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public static CollectionOptions empty() {
|
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}. <br />
|
||||||
|
* <strong>NOTE</strong> Using capped collections requires defining {@link #size(int)}.
|
||||||
*
|
*
|
||||||
* @param size the collection size in bytes, this data space is preallocated.
|
|
||||||
* @return new {@link CollectionOptions}.
|
* @return new {@link CollectionOptions}.
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public CollectionOptions capped(int size) {
|
public CollectionOptions capped() {
|
||||||
return new CollectionOptions(size, maxDocuments, true, collation);
|
return new CollectionOptions(size, maxDocuments, true, collation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +97,7 @@ public class CollectionOptions {
|
|||||||
* @return new {@link CollectionOptions}.
|
* @return new {@link CollectionOptions}.
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public CollectionOptions maxDocuments(Integer maxDocuments) {
|
public CollectionOptions maxDocuments(long maxDocuments) {
|
||||||
return new CollectionOptions(size, maxDocuments, capped, collation);
|
return new CollectionOptions(size, maxDocuments, capped, collation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +108,7 @@ public class CollectionOptions {
|
|||||||
* @return new {@link CollectionOptions}.
|
* @return new {@link CollectionOptions}.
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public CollectionOptions size(int size) {
|
public CollectionOptions size(long size) {
|
||||||
return new CollectionOptions(size, maxDocuments, capped, collation);
|
return new CollectionOptions(size, maxDocuments, capped, collation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,50 +120,44 @@ public class CollectionOptions {
|
|||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public CollectionOptions collation(Collation collation) {
|
public CollectionOptions collation(Collation collation) {
|
||||||
return new CollectionOptions(size, maxDocuments, capped, Optional.ofNullable(collation));
|
return new CollectionOptions(size, maxDocuments, capped, 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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<Long> 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<Long> getSize() {
|
||||||
|
return Optional.ofNullable(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get if the collection should be capped.
|
||||||
|
*
|
||||||
|
* @return {@link Optional#empty()} if not set.
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public void setCollation(Collation collation) {
|
public Optional<Boolean> getCapped() {
|
||||||
this.collation = Optional.ofNullable(collation);
|
return Optional.ofNullable(capped);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the {@link Collation} settings.
|
* Get the {@link Collation} settings.
|
||||||
*
|
*
|
||||||
* @return
|
* @return {@link Optional#empty()} if not set.
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public Optional<Collation> getCollation() {
|
public Optional<Collation> getCollation() {
|
||||||
return collation;
|
return Optional.ofNullable(collation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,19 @@ import static org.springframework.data.mongodb.core.query.SerializationUtils.*;
|
|||||||
import static org.springframework.data.util.Optionals.*;
|
import static org.springframework.data.util.Optionals.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
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.Map.Entry;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
@@ -1951,16 +1962,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
|||||||
|
|
||||||
Document document = new Document();
|
Document document = new Document();
|
||||||
if (collectionOptions != null) {
|
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()));
|
collectionOptions.getCollation().ifPresent(val -> document.append("collation", val.toDocument()));
|
||||||
}
|
}
|
||||||
return document;
|
return document;
|
||||||
|
|||||||
@@ -1626,17 +1626,9 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
|||||||
CreateCollectionOptions result = new CreateCollectionOptions();
|
CreateCollectionOptions result = new CreateCollectionOptions();
|
||||||
if (collectionOptions != null) {
|
if (collectionOptions != null) {
|
||||||
|
|
||||||
if (collectionOptions.getCapped() != null) {
|
collectionOptions.getCapped().ifPresent(result::capped);
|
||||||
result = result.capped(collectionOptions.getCapped());
|
collectionOptions.getSize().ifPresent(result::sizeInBytes);
|
||||||
}
|
collectionOptions.getMaxDocuments().ifPresent(result::maxDocuments);
|
||||||
|
|
||||||
if (collectionOptions.getSize() != null) {
|
|
||||||
result = result.sizeInBytes(collectionOptions.getSize());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (collectionOptions.getMaxDocuments() != null) {
|
|
||||||
result = result.maxDocuments(collectionOptions.getMaxDocuments());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ import com.mongodb.DBRef;
|
|||||||
*
|
*
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
* @author Thomas Darimont
|
* @author Thomas Darimont
|
||||||
|
* @author Christoph Strobl
|
||||||
*/
|
*/
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public abstract class MongoOperationsUnitTests {
|
public abstract class MongoOperationsUnitTests {
|
||||||
@@ -137,7 +138,7 @@ public abstract class MongoOperationsUnitTests {
|
|||||||
new Execution() {
|
new Execution() {
|
||||||
@Override
|
@Override
|
||||||
public void doWith(MongoOperations operations) {
|
public void doWith(MongoOperations operations) {
|
||||||
operations.createCollection("foo", new CollectionOptions(1, 1, true));
|
operations.createCollection("foo", CollectionOptions.empty().size(1).maxDocuments(1).capped());
|
||||||
}
|
}
|
||||||
}.assertDataAccessException();
|
}.assertDataAccessException();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -334,7 +334,7 @@ public class MongoTemplateTests {
|
|||||||
@Test // DATAMONGO-1687
|
@Test // DATAMONGO-1687
|
||||||
public void createCappedCollection() {
|
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 collectionOptions = getCollectionInfo(template.getCollectionName(Person.class)).get("options",
|
||||||
org.bson.Document.class);
|
org.bson.Document.class);
|
||||||
|
|||||||
@@ -803,7 +803,7 @@ public class ReactiveMongoTemplateTests {
|
|||||||
|
|
||||||
StepVerifier.create(template.dropCollection("capped")
|
StepVerifier.create(template.dropCollection("capped")
|
||||||
.then(template.createCollection("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"), //
|
.then(template.insert(new Document("random", Math.random()).append("key", "value"), //
|
||||||
"capped")))
|
"capped")))
|
||||||
.expectNextCount(1).verifyComplete();
|
.expectNextCount(1).verifyComplete();
|
||||||
@@ -825,7 +825,7 @@ public class ReactiveMongoTemplateTests {
|
|||||||
|
|
||||||
StepVerifier.create(template.dropCollection("capped")
|
StepVerifier.create(template.dropCollection("capped")
|
||||||
.then(template.createCollection("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"), //
|
.then(template.insert(new Document("random", Math.random()).append("key", "value"), //
|
||||||
"capped")))
|
"capped")))
|
||||||
.expectNextCount(1).verifyComplete();
|
.expectNextCount(1).verifyComplete();
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ import org.springframework.util.ClassUtils;
|
|||||||
* Test for {@link ReactiveMongoRepository} query methods.
|
* Test for {@link ReactiveMongoRepository} query methods.
|
||||||
*
|
*
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
|
* @author Christoph Strobl
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:reactive-infrastructure.xml")
|
@ContextConfiguration("classpath:reactive-infrastructure.xml")
|
||||||
@@ -175,7 +176,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
|
|||||||
StepVerifier
|
StepVerifier
|
||||||
.create(template.dropCollection(Capped.class) //
|
.create(template.dropCollection(Capped.class) //
|
||||||
.then(template.createCollection(Capped.class, //
|
.then(template.createCollection(Capped.class, //
|
||||||
new CollectionOptions(1000, 100, true)))) //
|
CollectionOptions.empty().size(1000).maxDocuments(100).capped()))) //
|
||||||
.expectNextCount(1) //
|
.expectNextCount(1) //
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
|
|
||||||
@@ -200,7 +201,7 @@ public class ReactiveMongoRepositoryTests implements BeanClassLoaderAware, BeanF
|
|||||||
StepVerifier
|
StepVerifier
|
||||||
.create(template.dropCollection(Capped.class) //
|
.create(template.dropCollection(Capped.class) //
|
||||||
.then(template.createCollection(Capped.class, //
|
.then(template.createCollection(Capped.class, //
|
||||||
new CollectionOptions(1000, 100, true)))) //
|
CollectionOptions.empty().size(1000).maxDocuments(100).capped()))) //
|
||||||
.expectNextCount(1) //
|
.expectNextCount(1) //
|
||||||
.verifyComplete();
|
.verifyComplete();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user