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:
Christoph Strobl
2017-05-18 13:33:10 +02:00
parent a5a4c6d8c4
commit 25af5b5f79
7 changed files with 66 additions and 77 deletions

View File

@@ -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> collation;
private Collation collation;
/**
* 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 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> 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}. <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}.
* @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<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
*/
public void setCollation(Collation collation) {
this.collation = Optional.ofNullable(collation);
public Optional<Boolean> getCapped() {
return Optional.ofNullable(capped);
}
/**
* Get the {@link Collation} settings.
*
* @return
* @return {@link Optional#empty()} if not set.
* @since 2.0
*/
public Optional<Collation> getCollation() {
return collation;
return Optional.ofNullable(collation);
}
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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);

View File

@@ -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();

View File

@@ -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();