Skip to content

Commit 1a5de2e

Browse files
committed
DATAMONGO-2331 - Polishing.
Remove deprecated methods accepting Update in favor of methods accepting UpdateDefinition in Template APIs. Hide AggregationUpdate constructor. Tweak Javadoc and reference documentation. Original pull request: spring-projects#789.
1 parent cc07a1b commit 1a5de2e

16 files changed

+257
-710
lines changed

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

+4-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Optional;
1919

20+
import org.springframework.data.mongodb.core.aggregation.AggregationUpdate;
2021
import org.springframework.data.mongodb.core.query.Query;
2122
import org.springframework.data.mongodb.core.query.Update;
2223
import org.springframework.data.mongodb.core.query.UpdateDefinition;
@@ -157,22 +158,12 @@ interface UpdateWithUpdate<T> {
157158
* @param update must not be {@literal null}.
158159
* @return new instance of {@link TerminatingUpdate}.
159160
* @throws IllegalArgumentException if update is {@literal null}.
161+
* @since 2.3
162+
* @see Update
163+
* @see AggregationUpdate
160164
*/
161165
TerminatingUpdate<T> apply(UpdateDefinition update);
162166

163-
/**
164-
* Set the {@link Update} to be applied.
165-
*
166-
* @param update must not be {@literal null}.
167-
* @return new instance of {@link TerminatingUpdate}.
168-
* @throws IllegalArgumentException if update is {@literal null}.
169-
* @deprecated since 2.3 in favor of {@link #apply(UpdateDefinition)}.
170-
*/
171-
@Deprecated
172-
default TerminatingUpdate<T> apply(Update update) {
173-
return apply((UpdateDefinition) update);
174-
}
175-
176167
/**
177168
* Specify {@code replacement} object.
178169
*

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

+55-274
Large diffs are not rendered by default.

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

+55-61
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public <T> CloseableIterator<T> stream(Query query, Class<T> entityType, String
437437
}
438438

439439
@SuppressWarnings("ConstantConditions")
440-
protected <T> CloseableIterator<T> doStream(Query query, final Class<?> entityType, String collectionName,
440+
protected <T> CloseableIterator<T> doStream(Query query, Class<?> entityType, String collectionName,
441441
Class<T> returnType) {
442442

443443
Assert.notNull(query, "Query must not be null!");
@@ -471,7 +471,7 @@ public String getCollectionName(Class<?> entityClass) {
471471
*/
472472
@Override
473473
@SuppressWarnings("ConstantConditions")
474-
public Document executeCommand(final String jsonCommand) {
474+
public Document executeCommand(String jsonCommand) {
475475

476476
Assert.hasText(jsonCommand, "JsonCommand must not be null nor empty!");
477477

@@ -484,7 +484,7 @@ public Document executeCommand(final String jsonCommand) {
484484
*/
485485
@Override
486486
@SuppressWarnings("ConstantConditions")
487-
public Document executeCommand(final Document command) {
487+
public Document executeCommand(Document command) {
488488

489489
Assert.notNull(command, "Command must not be null!");
490490

@@ -655,7 +655,7 @@ public <T> MongoCollection<Document> createCollection(Class<T> entityClass,
655655
* (non-Javadoc)
656656
* @see org.springframework.data.mongodb.core.MongoOperations#createCollection(java.lang.String)
657657
*/
658-
public MongoCollection<Document> createCollection(final String collectionName) {
658+
public MongoCollection<Document> createCollection(String collectionName) {
659659

660660
Assert.notNull(collectionName, "CollectionName must not be null!");
661661

@@ -666,8 +666,8 @@ public MongoCollection<Document> createCollection(final String collectionName) {
666666
* (non-Javadoc)
667667
* @see org.springframework.data.mongodb.core.MongoOperations#createCollection(java.lang.String, org.springframework.data.mongodb.core.CollectionOptions)
668668
*/
669-
public MongoCollection<Document> createCollection(final String collectionName,
670-
final @Nullable CollectionOptions collectionOptions) {
669+
public MongoCollection<Document> createCollection(String collectionName,
670+
@Nullable CollectionOptions collectionOptions) {
671671

672672
Assert.notNull(collectionName, "CollectionName must not be null!");
673673
return doCreateCollection(collectionName, convertToDocument(collectionOptions));
@@ -678,7 +678,7 @@ public MongoCollection<Document> createCollection(final String collectionName,
678678
* @see org.springframework.data.mongodb.core.MongoOperations#getCollection(java.lang.String)
679679
*/
680680
@SuppressWarnings("ConstantConditions")
681-
public MongoCollection<Document> getCollection(final String collectionName) {
681+
public MongoCollection<Document> getCollection(String collectionName) {
682682

683683
Assert.notNull(collectionName, "CollectionName must not be null!");
684684

@@ -698,7 +698,7 @@ public <T> boolean collectionExists(Class<T> entityClass) {
698698
* @see org.springframework.data.mongodb.core.ExecutableInsertOperation#getCollection(java.lang.String)
699699
*/
700700
@SuppressWarnings("ConstantConditions")
701-
public boolean collectionExists(final String collectionName) {
701+
public boolean collectionExists(String collectionName) {
702702

703703
Assert.notNull(collectionName, "CollectionName must not be null!");
704704

@@ -729,15 +729,13 @@ public void dropCollection(String collectionName) {
729729

730730
Assert.notNull(collectionName, "CollectionName must not be null!");
731731

732-
execute(collectionName, new CollectionCallback<Void>() {
733-
public Void doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
734-
collection.drop();
735-
if (LOGGER.isDebugEnabled()) {
736-
LOGGER.debug("Dropped collection [{}]",
737-
collection.getNamespace() != null ? collection.getNamespace().getCollectionName() : collectionName);
738-
}
739-
return null;
732+
execute(collectionName, (CollectionCallback<Void>) collection -> {
733+
collection.drop();
734+
if (LOGGER.isDebugEnabled()) {
735+
LOGGER.debug("Dropped collection [{}]",
736+
collection.getNamespace() != null ? collection.getNamespace().getCollectionName() : collectionName);
740737
}
738+
return null;
741739
});
742740
}
743741

@@ -1154,7 +1152,7 @@ public long count(Query query, Class<?> entityClass) {
11541152
}
11551153

11561154
@Override
1157-
public long count(final Query query, String collectionName) {
1155+
public long count(Query query, String collectionName) {
11581156
return count(query, null, collectionName);
11591157
}
11601158

@@ -1484,7 +1482,7 @@ protected <T> T doSave(String collectionName, T objectToSave, MongoWriter<T> wri
14841482
}
14851483

14861484
@SuppressWarnings("ConstantConditions")
1487-
protected Object insertDocument(final String collectionName, final Document document, final Class<?> entityClass) {
1485+
protected Object insertDocument(String collectionName, Document document, Class<?> entityClass) {
14881486

14891487
if (LOGGER.isDebugEnabled()) {
14901488
LOGGER.debug("Inserting Document containing fields: {} in collection: {}", document.keySet(), collectionName);
@@ -1505,7 +1503,7 @@ protected Object insertDocument(final String collectionName, final Document docu
15051503
});
15061504
}
15071505

1508-
protected List<Object> insertDocumentList(final String collectionName, final List<Document> documents) {
1506+
protected List<Object> insertDocumentList(String collectionName, List<Document> documents) {
15091507

15101508
if (documents.isEmpty()) {
15111509
return Collections.emptyList();
@@ -1533,34 +1531,32 @@ protected List<Object> insertDocumentList(final String collectionName, final Lis
15331531
return MappedDocument.toIds(documents);
15341532
}
15351533

1536-
protected Object saveDocument(final String collectionName, final Document dbDoc, final Class<?> entityClass) {
1534+
protected Object saveDocument(String collectionName, Document dbDoc, Class<?> entityClass) {
15371535

15381536
if (LOGGER.isDebugEnabled()) {
15391537
LOGGER.debug("Saving Document containing fields: {}", dbDoc.keySet());
15401538
}
15411539

1542-
return execute(collectionName, new CollectionCallback<Object>() {
1543-
public Object doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
1544-
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass,
1545-
dbDoc, null);
1546-
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
1540+
return execute(collectionName, collection -> {
1541+
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass,
1542+
dbDoc, null);
1543+
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
15471544

1548-
MappedDocument mapped = MappedDocument.of(dbDoc);
1545+
MappedDocument mapped = MappedDocument.of(dbDoc);
15491546

1550-
if (!mapped.hasId()) {
1551-
if (writeConcernToUse == null) {
1552-
collection.insertOne(dbDoc);
1553-
} else {
1554-
collection.withWriteConcern(writeConcernToUse).insertOne(dbDoc);
1555-
}
1556-
} else if (writeConcernToUse == null) {
1557-
collection.replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true));
1547+
if (!mapped.hasId()) {
1548+
if (writeConcernToUse == null) {
1549+
collection.insertOne(dbDoc);
15581550
} else {
1559-
collection.withWriteConcern(writeConcernToUse).replaceOne(mapped.getIdFilter(), dbDoc,
1560-
new ReplaceOptions().upsert(true));
1551+
collection.withWriteConcern(writeConcernToUse).insertOne(dbDoc);
15611552
}
1562-
return mapped.getId();
1553+
} else if (writeConcernToUse == null) {
1554+
collection.replaceOne(mapped.getIdFilter(), dbDoc, new ReplaceOptions().upsert(true));
1555+
} else {
1556+
collection.withWriteConcern(writeConcernToUse).replaceOne(mapped.getIdFilter(), dbDoc,
1557+
new ReplaceOptions().upsert(true));
15631558
}
1559+
return mapped.getId();
15641560
});
15651561
}
15661562

@@ -1588,7 +1584,7 @@ public UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> e
15881584
}
15891585

15901586
@Override
1591-
public UpdateResult updateFirst(final Query query, final UpdateDefinition update, final String collectionName) {
1587+
public UpdateResult updateFirst(Query query, UpdateDefinition update, String collectionName) {
15921588
return doUpdate(collectionName, query, update, null, false, false);
15931589
}
15941590

@@ -1606,22 +1602,21 @@ public UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> e
16061602
}
16071603

16081604
@Override
1609-
public UpdateResult updateMulti(final Query query, final UpdateDefinition update, String collectionName) {
1605+
public UpdateResult updateMulti(Query query, UpdateDefinition update, String collectionName) {
16101606
return doUpdate(collectionName, query, update, null, false, true);
16111607
}
16121608

16131609
@Override
1614-
public UpdateResult updateMulti(final Query query, final UpdateDefinition update, Class<?> entityClass,
1615-
String collectionName) {
1610+
public UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName) {
16161611

16171612
Assert.notNull(entityClass, "EntityClass must not be null!");
16181613

16191614
return doUpdate(collectionName, query, update, entityClass, false, true);
16201615
}
16211616

16221617
@SuppressWarnings("ConstantConditions")
1623-
protected UpdateResult doUpdate(final String collectionName, final Query query, final UpdateDefinition update,
1624-
@Nullable final Class<?> entityClass, final boolean upsert, final boolean multi) {
1618+
protected UpdateResult doUpdate(String collectionName, Query query, UpdateDefinition update,
1619+
@Nullable Class<?> entityClass, boolean upsert, boolean multi) {
16251620

16261621
Assert.notNull(collectionName, "CollectionName must not be null!");
16271622
Assert.notNull(query, "Query must not be null!");
@@ -1659,22 +1654,23 @@ protected UpdateResult doUpdate(final String collectionName, final Query query,
16591654
? new RelaxedTypeBasedAggregationOperationContext(entityClass, mappingContext, queryMapper)
16601655
: Aggregation.DEFAULT_CONTEXT;
16611656

1662-
AggregationUpdate aUppdate = ((AggregationUpdate) update);
1663-
List<Document> pipeline = new AggregationUtil(queryMapper, mappingContext).createPipeline(aUppdate, context);
1657+
List<Document> pipeline = new AggregationUtil(queryMapper, mappingContext)
1658+
.createPipeline((AggregationUpdate) update, context);
16641659

16651660
return execute(collectionName, collection -> {
16661661

1662+
if (LOGGER.isDebugEnabled()) {
1663+
LOGGER.debug("Calling update using query: {} and update: {} in collection: {}",
1664+
serializeToJsonSafely(queryObj), serializeToJsonSafely(pipeline), collectionName);
1665+
}
1666+
16671667
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName,
16681668
entityClass, update.getUpdateObject(), queryObj);
16691669
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
16701670

16711671
collection = writeConcernToUse != null ? collection.withWriteConcern(writeConcernToUse) : collection;
16721672

1673-
if (multi) {
1674-
return collection.updateMany(queryObj, pipeline, opts);
1675-
}
1676-
1677-
return collection.updateOne(queryObj, pipeline, opts);
1673+
return multi ? collection.updateMany(queryObj, pipeline, opts) : collection.updateOne(queryObj, pipeline, opts);
16781674
});
16791675
}
16801676

@@ -1707,11 +1703,8 @@ protected UpdateResult doUpdate(final String collectionName, final Query query,
17071703

17081704
return collection.replaceOne(queryObj, updateObj, replaceOptions);
17091705
} else {
1710-
if (multi) {
1711-
return collection.updateMany(queryObj, updateObj, opts);
1712-
} else {
1713-
return collection.updateOne(queryObj, updateObj, opts);
1714-
}
1706+
return multi ? collection.updateMany(queryObj, updateObj, opts)
1707+
: collection.updateOne(queryObj, updateObj, opts);
17151708
}
17161709
});
17171710
}
@@ -1764,14 +1757,14 @@ public DeleteResult remove(Query query, Class<?> entityClass, String collectionN
17641757
}
17651758

17661759
@SuppressWarnings("ConstantConditions")
1767-
protected <T> DeleteResult doRemove(final String collectionName, final Query query,
1768-
@Nullable final Class<T> entityClass, boolean multi) {
1760+
protected <T> DeleteResult doRemove(String collectionName, Query query, @Nullable Class<T> entityClass,
1761+
boolean multi) {
17691762

17701763
Assert.notNull(query, "Query must not be null!");
17711764
Assert.hasText(collectionName, "Collection name must not be null or empty!");
17721765

1773-
final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
1774-
final Document queryObject = queryMapper.getMappedObject(query.getQueryObject(), entity);
1766+
MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
1767+
Document queryObject = queryMapper.getMappedObject(query.getQueryObject(), entity);
17751768

17761769
return execute(collectionName, collection -> {
17771770

@@ -2693,7 +2686,7 @@ protected <T> T doFindAndModify(String collectionName, Document query, Document
26932686

26942687
Document mappedQuery = queryMapper.getMappedObject(query, entity);
26952688

2696-
Object mappedUpdate = new Document();
2689+
Object mappedUpdate;
26972690
if (update instanceof AggregationUpdate) {
26982691

26992692
AggregationOperationContext context = entityClass != null
@@ -3114,7 +3107,8 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
31143107
} else if (update instanceof List) {
31153108
return collection.findOneAndUpdate(query, (List<Document>) update, opts);
31163109
}
3117-
throw new IllegalArgumentException("doh - that does not work");
3110+
3111+
throw new IllegalArgumentException(String.format("Using %s is not supported in findOneAndUpdate", update));
31183112
}
31193113
}
31203114

0 commit comments

Comments
 (0)