Skip to content

Commit fcd9d2d

Browse files
DATAMONGO-1719 - Polishing.
Use empty query where possible to avoid null values and introduce non optional findAndModify alternative for imperative api. Add missing ExecutableUpdateOperation Kotlin extension. Update Javadoc and add non-Javadoc comments.
1 parent 36c77e1 commit fcd9d2d

18 files changed

+306
-80
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ interface TerminatingBulkInsert<T> {
9494
BulkWriteResult bulk(Collection<? extends T> objects);
9595
}
9696

97-
/**
98-
* @author Christoph Strobl
99-
* @since 2.0
100-
*/
101-
interface ExecutableInsert<T> extends TerminatingInsert<T>, InsertWithCollection<T>, InsertWithBulkMode<T> {}
102-
10397
/**
10498
* Collection override (optional).
10599
*
@@ -134,4 +128,10 @@ interface InsertWithBulkMode<T> extends TerminatingInsert<T> {
134128
*/
135129
TerminatingBulkInsert<T> withBulkMode(BulkMode bulkMode);
136130
}
131+
132+
/**
133+
* @author Christoph Strobl
134+
* @since 2.0
135+
*/
136+
interface ExecutableInsert<T> extends TerminatingInsert<T>, InsertWithCollection<T>, InsertWithBulkMode<T> {}
137137
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
import java.util.List;
2424

25-
import org.bson.Document;
26-
import org.springframework.data.mongodb.core.query.BasicQuery;
2725
import org.springframework.data.mongodb.core.query.Query;
2826
import org.springframework.util.Assert;
2927
import org.springframework.util.StringUtils;
@@ -39,6 +37,8 @@
3937
*/
4038
class ExecutableRemoveOperationSupport implements ExecutableRemoveOperation {
4139

40+
private static final Query ALL_QUERY = new Query();
41+
4242
private final MongoTemplate tempate;
4343

4444
/**
@@ -59,7 +59,7 @@ public <T> ExecutableRemove<T> remove(Class<T> domainType) {
5959

6060
Assert.notNull(domainType, "DomainType must not be null!");
6161

62-
return new ExecutableRemoveSupport<>(tempate, domainType, null, null);
62+
return new ExecutableRemoveSupport<>(tempate, domainType, ALL_QUERY, null);
6363
}
6464

6565
/**
@@ -96,23 +96,19 @@ public DeleteResult all() {
9696

9797
String collectionName = getCollectionName();
9898

99-
return template.doRemove(collectionName, getQuery(), domainType);
99+
return template.doRemove(collectionName, query, domainType);
100100
}
101101

102102
@Override
103103
public List<T> findAndRemove() {
104104

105105
String collectionName = getCollectionName();
106106

107-
return template.doFindAndDelete(collectionName, getQuery(), domainType);
107+
return template.doFindAndDelete(collectionName, query, domainType);
108108
}
109109

110110
private String getCollectionName() {
111111
return StringUtils.hasText(collection) ? collection : template.determineCollectionName(domainType);
112112
}
113-
114-
private Query getQuery() {
115-
return query != null ? query : new BasicQuery(new Document());
116-
}
117113
}
118114
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ public interface ExecutableUpdateOperation {
5656
*/
5757
<T> ExecutableUpdate<T> update(Class<T> domainType);
5858

59-
/**
60-
* @author Christoph Strobl
61-
* @since 2.0
62-
*/
63-
interface ExecutableUpdate<T> extends UpdateWithCollection<T>, UpdateWithQuery<T>, UpdateWithUpdate<T> {}
64-
6559
/**
6660
* Declare the {@link Update} to apply.
6761
*
@@ -145,7 +139,16 @@ interface TerminatingFindAndModify<T> {
145139
*
146140
* @return {@link Optional#empty()} if nothing found.
147141
*/
148-
Optional<T> findAndModify();
142+
default Optional<T> findAndModify() {
143+
return Optional.ofNullable(findAndModifyValue());
144+
}
145+
146+
/**
147+
* Find, modify and return the first matching document.
148+
*
149+
* @return {@literal null} if nothing found.
150+
*/
151+
T findAndModifyValue();
149152
}
150153

151154
/**
@@ -177,4 +180,10 @@ interface TerminatingUpdate<T> extends TerminatingFindAndModify<T>, FindAndModif
177180
*/
178181
UpdateResult upsert();
179182
}
183+
184+
/**
185+
* @author Christoph Strobl
186+
* @since 2.0
187+
*/
188+
interface ExecutableUpdate<T> extends UpdateWithCollection<T>, UpdateWithQuery<T>, UpdateWithUpdate<T> {}
180189
}

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
import java.util.Optional;
2424

25-
import org.bson.Document;
26-
import org.springframework.data.mongodb.core.query.BasicQuery;
2725
import org.springframework.data.mongodb.core.query.Query;
2826
import org.springframework.data.mongodb.core.query.Update;
2927
import org.springframework.util.Assert;
@@ -40,6 +38,8 @@
4038
*/
4139
class ExecutableUpdateOperationSupport implements ExecutableUpdateOperation {
4240

41+
private static final Query ALL_QUERY = new Query();
42+
4343
private final MongoTemplate template;
4444

4545
/**
@@ -59,7 +59,7 @@ public <T> ExecutableUpdate<T> update(Class<T> domainType) {
5959

6060
Assert.notNull(domainType, "DomainType must not be null!");
6161

62-
return new ExecutableUpdateSupport<>(template, domainType, null, null, null, null);
62+
return new ExecutableUpdateSupport<>(template, domainType, ALL_QUERY, null, null, null);
6363
}
6464

6565
/**
@@ -105,12 +105,8 @@ public UpdateResult upsert() {
105105
}
106106

107107
@Override
108-
public Optional<T> findAndModify() {
109-
110-
String collectionName = getCollectionName();
111-
112-
return Optional.ofNullable(template.findAndModify(query != null ? query : new BasicQuery(new Document()), update,
113-
options, domainType, collectionName));
108+
public T findAndModifyValue() {
109+
return template.findAndModify(query, update, options, domainType, getCollectionName());
114110
}
115111

116112
@Override
@@ -135,12 +131,7 @@ public TerminatingFindAndModify<T> withOptions(FindAndModifyOptions options) {
135131
}
136132

137133
private UpdateResult doUpdate(boolean multi, boolean upsert) {
138-
139-
String collectionName = getCollectionName();
140-
141-
Query query = this.query != null ? this.query : new BasicQuery(new Document());
142-
143-
return template.doUpdate(collectionName, query, update, domainType, upsert, multi);
134+
return template.doUpdate(getCollectionName(), query, update, domainType, upsert, multi);
144135
}
145136

146137
private String getCollectionName() {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* </pre>
3737
*
3838
* @author Mark Paluch
39+
* @author Christoph Strobl
3940
* @since 2.0
4041
*/
4142
public interface ReactiveAggregationOperation {
@@ -46,7 +47,7 @@ public interface ReactiveAggregationOperation {
4647
* input type for he aggregation.
4748
*
4849
* @param domainType must not be {@literal null}.
49-
* @return new instance of {@link ReactiveAggregation}.
50+
* @return new instance of {@link ReactiveAggregation}. Never {@literal null}.
5051
* @throws IllegalArgumentException if domainType is {@literal null}.
5152
*/
5253
<T> ReactiveAggregation<T> aggregateAndReturn(Class<T> domainType);
@@ -61,8 +62,8 @@ interface AggregationOperationWithCollection<T> {
6162
* Skip this step to use the default collection derived from the domain type.
6263
*
6364
* @param collection must not be {@literal null} nor {@literal empty}.
64-
* @return new instance of {@link AggregationOperationWithAggregation}.
65-
* @throws IllegalArgumentException if collection is {@literal null}.
65+
* @return new instance of {@link AggregationOperationWithAggregation}. Never {@literal null}.
66+
* @throws IllegalArgumentException if collection is {@literal null} or empty.
6667
*/
6768
AggregationOperationWithAggregation<T> inCollection(String collection);
6869
}
@@ -89,7 +90,7 @@ interface AggregationOperationWithAggregation<T> {
8990
* Set the aggregation to be used.
9091
*
9192
* @param aggregation must not be {@literal null}.
92-
* @return new instance of {@link TerminatingAggregationOperation}.
93+
* @return new instance of {@link TerminatingAggregationOperation}. Never {@literal null}.
9394
* @throws IllegalArgumentException if aggregation is {@literal null}.
9495
*/
9596
TerminatingAggregationOperation<T> by(Aggregation aggregation);

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
import org.springframework.util.StringUtils;
2828

2929
/**
30-
* Implementation of {@link ExecutableAggregationOperation} operating directly on {@link ReactiveMongoTemplate}.
30+
* Implementation of {@link ReactiveAggregationOperation} operating directly on {@link ReactiveMongoTemplate}.
3131
*
3232
* @author Mark Paluch
33+
* @autor Christoph Strobl
3334
* @since 2.0
3435
*/
3536
class ReactiveAggregationOperationSupport implements ReactiveAggregationOperation {
@@ -49,6 +50,10 @@ class ReactiveAggregationOperationSupport implements ReactiveAggregationOperatio
4950
this.template = template;
5051
}
5152

53+
/*
54+
* (non-Javadoc)
55+
* @see org.springframework.data.mongodb.core.ReactiveAggregationOperation#aggregateAndReturn(java.lang.Class)
56+
*/
5257
@Override
5358
public <T> ReactiveAggregation<T> aggregateAndReturn(Class<T> domainType) {
5459

@@ -67,6 +72,10 @@ static class ReactiveAggregationSupport<T>
6772
Aggregation aggregation;
6873
String collection;
6974

75+
/*
76+
* (non-Javadoc)
77+
* @see org.springframework.data.mongodb.core.ReactiveAggregationOperation.AggregationOperationWithCollection#inCollection(java.lang.String)
78+
*/
7079
@Override
7180
public AggregationOperationWithAggregation<T> inCollection(String collection) {
7281

@@ -75,6 +84,10 @@ public AggregationOperationWithAggregation<T> inCollection(String collection) {
7584
return new ReactiveAggregationSupport<>(template, domainType, aggregation, collection);
7685
}
7786

87+
/*
88+
* (non-Javadoc)
89+
* @see org.springframework.data.mongodb.core.ReactiveAggregationOperation.AggregationOperationWithAggregation#by(org.springframework.data.mongodb.core.Aggregation)
90+
*/
7891
@Override
7992
public TerminatingAggregationOperation<T> by(Aggregation aggregation) {
8093

@@ -83,6 +96,10 @@ public TerminatingAggregationOperation<T> by(Aggregation aggregation) {
8396
return new ReactiveAggregationSupport<>(template, domainType, aggregation, collection);
8497
}
8598

99+
/*
100+
* (non-Javadoc)
101+
* @see org.springframework.data.mongodb.core.ReactiveAggregationOperation.TerminatingAggregationOperation#all()
102+
*/
86103
@Override
87104
public Flux<T> all() {
88105
return template.aggregate(aggregation, getCollectionName(aggregation), domainType);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* </pre>
4545
*
4646
* @author Mark Paluch
47+
* @author Christoph Strobl
4748
* @since 2.0
4849
*/
4950
public interface ReactiveFindOperation {
@@ -52,7 +53,7 @@ public interface ReactiveFindOperation {
5253
* Start creating a find operation for the given {@literal domainType}.
5354
*
5455
* @param domainType must not be {@literal null}.
55-
* @return new instance of {@link ReactiveFind}.
56+
* @return new instance of {@link ReactiveFind}. Never {@literal null}.
5657
* @throws IllegalArgumentException if domainType is {@literal null}.
5758
*/
5859
<T> ReactiveFind<T> query(Class<T> domainType);
@@ -65,15 +66,15 @@ interface TerminatingFind<T> {
6566
/**
6667
* Get exactly zero or one result.
6768
*
68-
* @return {@link Mono#empty()} if no match found.
69+
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
6970
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found.
7071
*/
7172
Mono<T> one();
7273

7374
/**
7475
* Get the first or no result.
7576
*
76-
* @return {@link Mono#empty()} if no match found.
77+
* @return {@link Mono#empty()} if no match found. Never {@literal null}.
7778
*/
7879
Mono<T> first();
7980

@@ -87,14 +88,14 @@ interface TerminatingFind<T> {
8788
/**
8889
* Get the number of matching elements.
8990
*
90-
* @return total number of matching elements.
91+
* @return {@link Mono} emitting total number of matching elements. Never {@literal null}.
9192
*/
9293
Mono<Long> count();
9394

9495
/**
9596
* Check for the presence of matching elements.
9697
*
97-
* @return {@literal true} if at least one matching element exists.
98+
* @return {@link Mono} emitting {@literal true} if at least one matching element exists. Never {@literal null}.
9899
*/
99100
Mono<Boolean> exists();
100101
}

0 commit comments

Comments
 (0)