Skip to content

Commit 25af5b5

Browse files
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: spring-projects#462.
1 parent a5a4c6d commit 25af5b5

File tree

7 files changed

+62
-73
lines changed

7 files changed

+62
-73
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java

+36-46
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
*/
2929
public class CollectionOptions {
3030

31-
private Integer maxDocuments;
32-
private Integer size;
31+
private Long maxDocuments;
32+
private Long size;
3333
private Boolean capped;
34-
private Optional<Collation> collation;
34+
private Collation collation;
3535

3636
/**
3737
* Constructs a new <code>CollectionOptions</code> instance.
@@ -40,23 +40,21 @@ public class CollectionOptions {
4040
* @param maxDocuments the maximum number of documents in the collection.
4141
* @param capped true to created a "capped" collection (fixed size with auto-FIFO behavior based on insertion order),
4242
* false otherwise.
43+
* @deprecated since 2.0 please use {@link CollectionOptions#empty()} as entry point.
4344
*/
44-
public CollectionOptions(Integer size, Integer maxDocuments, Boolean capped) {
45-
this(size, maxDocuments, capped, Optional.empty());
45+
@Deprecated
46+
public CollectionOptions(Long size, Long maxDocuments, Boolean capped) {
47+
this(size, maxDocuments, capped, null);
4648
}
4749

48-
private CollectionOptions(Integer size, Integer maxDocuments, Boolean capped, Optional<Collation> collation) {
50+
private CollectionOptions(Long size, Long maxDocuments, Boolean capped, Collation collation) {
4951

5052
this.maxDocuments = maxDocuments;
5153
this.size = size;
5254
this.capped = capped;
5355
this.collation = collation;
5456
}
5557

56-
private CollectionOptions() {
57-
this.collation = Optional.empty();
58-
}
59-
6058
/**
6159
* Create new {@link CollectionOptions} by just providing the {@link Collation} to use.
6260
*
@@ -68,9 +66,7 @@ public static CollectionOptions just(Collation collation) {
6866

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

71-
CollectionOptions options = new CollectionOptions();
72-
options.setCollation(collation);
73-
return options;
69+
return new CollectionOptions(null, null, null, collation);
7470
}
7571

7672
/**
@@ -80,17 +76,17 @@ public static CollectionOptions just(Collation collation) {
8076
* @since 2.0
8177
*/
8278
public static CollectionOptions empty() {
83-
return new CollectionOptions();
79+
return new CollectionOptions(null, null, null, null);
8480
}
8581

8682
/**
87-
* Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}.
83+
* Create new {@link CollectionOptions} with already given settings and capped set to {@literal true}. <br />
84+
* <strong>NOTE</strong> Using capped collections requires defining {@link #size(int)}.
8885
*
89-
* @param size the collection size in bytes, this data space is preallocated.
9086
* @return new {@link CollectionOptions}.
9187
* @since 2.0
9288
*/
93-
public CollectionOptions capped(int size) {
89+
public CollectionOptions capped() {
9490
return new CollectionOptions(size, maxDocuments, true, collation);
9591
}
9692

@@ -101,7 +97,7 @@ public CollectionOptions capped(int size) {
10197
* @return new {@link CollectionOptions}.
10298
* @since 2.0
10399
*/
104-
public CollectionOptions maxDocuments(Integer maxDocuments) {
100+
public CollectionOptions maxDocuments(long maxDocuments) {
105101
return new CollectionOptions(size, maxDocuments, capped, collation);
106102
}
107103

@@ -112,7 +108,7 @@ public CollectionOptions maxDocuments(Integer maxDocuments) {
112108
* @return new {@link CollectionOptions}.
113109
* @since 2.0
114110
*/
115-
public CollectionOptions size(int size) {
111+
public CollectionOptions size(long size) {
116112
return new CollectionOptions(size, maxDocuments, capped, collation);
117113
}
118114

@@ -124,50 +120,44 @@ public CollectionOptions size(int size) {
124120
* @since 2.0
125121
*/
126122
public CollectionOptions collation(Collation collation) {
127-
return new CollectionOptions(size, maxDocuments, capped, Optional.ofNullable(collation));
128-
}
129-
130-
public Integer getMaxDocuments() {
131-
return maxDocuments;
132-
}
133-
134-
public void setMaxDocuments(Integer maxDocuments) {
135-
this.maxDocuments = maxDocuments;
136-
}
137-
138-
public Integer getSize() {
139-
return size;
140-
}
141-
142-
public void setSize(Integer size) {
143-
this.size = size;
123+
return new CollectionOptions(size, maxDocuments, capped, collation);
144124
}
145125

146-
public Boolean getCapped() {
147-
return capped;
126+
/**
127+
* Get the max number of documents the collection should be limited to.
128+
*
129+
* @return {@link Optional#empty()} if not set.
130+
*/
131+
public Optional<Long> getMaxDocuments() {
132+
return Optional.ofNullable(maxDocuments);
148133
}
149134

150-
public void setCapped(Boolean capped) {
151-
this.capped = capped;
135+
/**
136+
* Get the {@literal size} in bytes the collection should be limited to.
137+
*
138+
* @return {@link Optional#empty()} if not set.
139+
*/
140+
public Optional<Long> getSize() {
141+
return Optional.ofNullable(size);
152142
}
153143

154144
/**
155-
* Set {@link Collation} options.
145+
* Get if the collection should be capped.
156146
*
157-
* @param collation
147+
* @return {@link Optional#empty()} if not set.
158148
* @since 2.0
159149
*/
160-
public void setCollation(Collation collation) {
161-
this.collation = Optional.ofNullable(collation);
150+
public Optional<Boolean> getCapped() {
151+
return Optional.ofNullable(capped);
162152
}
163153

164154
/**
165155
* Get the {@link Collation} settings.
166156
*
167-
* @return
157+
* @return {@link Optional#empty()} if not set.
168158
* @since 2.0
169159
*/
170160
public Optional<Collation> getCollation() {
171-
return collation;
161+
return Optional.ofNullable(collation);
172162
}
173163
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@
2020
import static org.springframework.data.util.Optionals.*;
2121

2222
import java.io.IOException;
23-
import java.util.*;
23+
import java.util.ArrayList;
24+
import java.util.Collection;
25+
import java.util.Collections;
26+
import java.util.HashMap;
27+
import java.util.HashSet;
28+
import java.util.Iterator;
29+
import java.util.LinkedHashSet;
30+
import java.util.List;
31+
import java.util.Map;
2432
import java.util.Map.Entry;
33+
import java.util.Optional;
34+
import java.util.Scanner;
35+
import java.util.Set;
2536
import java.util.concurrent.TimeUnit;
2637

2738
import org.bson.Document;
@@ -1951,16 +1962,10 @@ protected Document convertToDocument(CollectionOptions collectionOptions) {
19511962

19521963
Document document = new Document();
19531964
if (collectionOptions != null) {
1954-
if (collectionOptions.getCapped() != null) {
1955-
document.put("capped", collectionOptions.getCapped().booleanValue());
1956-
}
1957-
if (collectionOptions.getSize() != null) {
1958-
document.put("size", collectionOptions.getSize().intValue());
1959-
}
1960-
if (collectionOptions.getMaxDocuments() != null) {
1961-
document.put("max", collectionOptions.getMaxDocuments().intValue());
1962-
}
19631965

1966+
collectionOptions.getCapped().ifPresent(val -> document.put("capped", val));
1967+
collectionOptions.getSize().ifPresent(val -> document.put("size", val));
1968+
collectionOptions.getMaxDocuments().ifPresent(val -> document.put("max", val));
19641969
collectionOptions.getCollation().ifPresent(val -> document.append("collation", val.toDocument()));
19651970
}
19661971
return document;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -1626,17 +1626,9 @@ protected CreateCollectionOptions convertToCreateCollectionOptions(CollectionOpt
16261626
CreateCollectionOptions result = new CreateCollectionOptions();
16271627
if (collectionOptions != null) {
16281628

1629-
if (collectionOptions.getCapped() != null) {
1630-
result = result.capped(collectionOptions.getCapped());
1631-
}
1632-
1633-
if (collectionOptions.getSize() != null) {
1634-
result = result.sizeInBytes(collectionOptions.getSize());
1635-
}
1636-
1637-
if (collectionOptions.getMaxDocuments() != null) {
1638-
result = result.maxDocuments(collectionOptions.getMaxDocuments());
1639-
}
1629+
collectionOptions.getCapped().ifPresent(result::capped);
1630+
collectionOptions.getSize().ifPresent(result::sizeInBytes);
1631+
collectionOptions.getMaxDocuments().ifPresent(result::maxDocuments);
16401632
}
16411633
return result;
16421634
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*
4747
* @author Oliver Gierke
4848
* @author Thomas Darimont
49+
* @author Christoph Strobl
4950
*/
5051
@RunWith(MockitoJUnitRunner.class)
5152
public abstract class MongoOperationsUnitTests {
@@ -137,7 +138,7 @@ public void convertsExceptionForCreateCollection2() {
137138
new Execution() {
138139
@Override
139140
public void doWith(MongoOperations operations) {
140-
operations.createCollection("foo", new CollectionOptions(1, 1, true));
141+
operations.createCollection("foo", CollectionOptions.empty().size(1).maxDocuments(1).capped());
141142
}
142143
}.assertDataAccessException();
143144
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public void rejectsDuplicateIdInInsertAll() {
334334
@Test // DATAMONGO-1687
335335
public void createCappedCollection() {
336336

337-
template.createCollection(Person.class, CollectionOptions.empty().capped(1000).maxDocuments(1000));
337+
template.createCollection(Person.class, CollectionOptions.empty().capped().size(1000).maxDocuments(1000));
338338

339339
org.bson.Document collectionOptions = getCollectionInfo(template.getCollectionName(Person.class)).get("options",
340340
org.bson.Document.class);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ public void tailStreamsData() throws InterruptedException {
803803

804804
StepVerifier.create(template.dropCollection("capped")
805805
.then(template.createCollection("capped", //
806-
new CollectionOptions(1000, 10, true)))
806+
CollectionOptions.empty().size(1000).maxDocuments(10).capped()))
807807
.then(template.insert(new Document("random", Math.random()).append("key", "value"), //
808808
"capped")))
809809
.expectNextCount(1).verifyComplete();
@@ -825,7 +825,7 @@ public void tailStreamsDataUntilCancellation() throws InterruptedException {
825825

826826
StepVerifier.create(template.dropCollection("capped")
827827
.then(template.createCollection("capped", //
828-
new CollectionOptions(1000, 10, true)))
828+
CollectionOptions.empty().size(1000).maxDocuments(10).capped()))
829829
.then(template.insert(new Document("random", Math.random()).append("key", "value"), //
830830
"capped")))
831831
.expectNextCount(1).verifyComplete();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ReactiveMongoRepositoryTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* Test for {@link ReactiveMongoRepository} query methods.
6464
*
6565
* @author Mark Paluch
66+
* @author Christoph Strobl
6667
*/
6768
@RunWith(SpringJUnit4ClassRunner.class)
6869
@ContextConfiguration("classpath:reactive-infrastructure.xml")
@@ -175,7 +176,7 @@ public void shouldUseTailableCursor() throws Exception {
175176
StepVerifier
176177
.create(template.dropCollection(Capped.class) //
177178
.then(template.createCollection(Capped.class, //
178-
new CollectionOptions(1000, 100, true)))) //
179+
CollectionOptions.empty().size(1000).maxDocuments(100).capped()))) //
179180
.expectNextCount(1) //
180181
.verifyComplete();
181182

@@ -200,7 +201,7 @@ public void shouldUseTailableCursorWithProjection() throws Exception {
200201
StepVerifier
201202
.create(template.dropCollection(Capped.class) //
202203
.then(template.createCollection(Capped.class, //
203-
new CollectionOptions(1000, 100, true)))) //
204+
CollectionOptions.empty().size(1000).maxDocuments(100).capped()))) //
204205
.expectNextCount(1) //
205206
.verifyComplete();
206207

0 commit comments

Comments
 (0)