Skip to content

DATAMONGO-1687 DATAMONGO-1693 - Initialize collation in CollectionOptions eagerly. #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1687-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1687-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1687-SNAPSHOT</version>
</dependency>

<!-- reactive -->
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1687-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1687-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAMONGO-1687-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
*/
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.
Expand All @@ -40,21 +40,21 @@ 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;
this.capped = capped;
this.collation = collation;
}

private CollectionOptions() {}

/**
* Create new {@link CollectionOptions} by just providing the {@link Collation} to use.
*
Expand All @@ -66,9 +66,7 @@ public static CollectionOptions just(Collation collation) {

Assert.notNull(collation, "Collation must not be null!");

CollectionOptions options = new CollectionOptions();
options.setCollation(collation);
return options;
return new CollectionOptions(null, null, null, collation);
}

/**
Expand All @@ -78,17 +76,17 @@ public static CollectionOptions just(Collation collation) {
* @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);
}

Expand All @@ -99,7 +97,7 @@ public CollectionOptions capped(int size) {
* @return new {@link CollectionOptions}.
* @since 2.0
*/
public CollectionOptions maxDocuments(Integer maxDocuments) {
public CollectionOptions maxDocuments(long maxDocuments) {
return new CollectionOptions(size, maxDocuments, capped, collation);
}

Expand All @@ -110,7 +108,7 @@ public CollectionOptions maxDocuments(Integer maxDocuments) {
* @return new {@link CollectionOptions}.
* @since 2.0
*/
public CollectionOptions size(int size) {
public CollectionOptions size(long size) {
return new CollectionOptions(size, maxDocuments, capped, collation);
}

Expand All @@ -122,50 +120,44 @@ public CollectionOptions size(int size) {
* @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;
return new CollectionOptions(size, maxDocuments, capped, collation);
}

public Boolean getCapped() {
return capped;
/**
* Get the max number of documents the collection should be limited to.
*
* @return {@link Optional#empty()} if not set.
*/
public Optional<Long> getMaxDocuments() {
return Optional.ofNullable(maxDocuments);
}

public void setCapped(Boolean capped) {
this.capped = capped;
/**
* 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);
}

/**
* Set {@link Collation} options.
* Get if the collection should be capped.
*
* @param collation
* @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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,19 @@
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;
Expand Down Expand Up @@ -1951,16 +1962,10 @@ protected Document convertToDocument(CollectionOptions collectionOptions) {

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1626,17 +1626,10 @@ protected CreateCollectionOptions convertToCreateCollectionOptions(CollectionOpt
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);
collectionOptions.getCollation().map(Collation::toMongoCollation).ifPresent(result::collation);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)
public abstract class MongoOperationsUnitTests {
Expand Down Expand Up @@ -137,7 +138,7 @@ public void convertsExceptionForCreateCollection2() {
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ public class MongoTemplateTests {

private static final org.springframework.data.util.Version TWO_DOT_FOUR = org.springframework.data.util.Version
.parse("2.4");
private static final org.springframework.data.util.Version TWO_DOT_EIGHT = org.springframework.data.util.Version
.parse("2.8");
private static final org.springframework.data.util.Version THREE_DOT_FOUR = org.springframework.data.util.Version
.parse("3.4");

Expand Down Expand Up @@ -333,6 +331,26 @@ public void rejectsDuplicateIdInInsertAll() {
template.insertAll(records);
}

@Test // DATAMONGO-1687
public void createCappedCollection() {

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);
assertThat(collectionOptions.get("capped"), is(true));
}

private org.bson.Document getCollectionInfo(String collectionName) {

return template.execute(db -> {

org.bson.Document result = db.runCommand(new org.bson.Document().append("listCollections", 1).append("filter",
new org.bson.Document("name", collectionName)));
return (org.bson.Document) result.get("cursor", org.bson.Document.class).get("firstBatch", List.class).get(0);
});
}

@Test
@SuppressWarnings("deprecation")
public void testEnsureIndex() throws Exception {
Expand Down
Loading