Use BulkOperations API for Remove Operations in MongoItemWriter

Issue #3737
This commit is contained in:
Parikshit Dutta
2020-07-15 19:16:16 +05:30
committed by Mahmoud Ben Hassine
parent e7ea16c9bc
commit 6c5cb2b13d
3 changed files with 157 additions and 81 deletions

View File

@@ -143,27 +143,22 @@ public class MongoItemWriter<T> implements ItemWriter<T>, InitializingBean {
}
private void delete(List<? extends T> items) {
if (StringUtils.hasText(this.collection)) {
for (Object item : items) {
this.template.remove(item, this.collection);
}
}
else {
for (Object item : items) {
this.template.remove(item);
BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, items.get(0));
MongoConverter mongoConverter = this.template.getConverter();
for (Object item : items) {
Document document = new Document();
mongoConverter.write(item, document);
Object objectId = document.get(ID_KEY);
if (objectId != null) {
Query query = new Query().addCriteria(Criteria.where(ID_KEY).is(objectId));
bulkOperations.remove(query);
}
}
bulkOperations.execute();
}
private void saveOrUpdate(List<? extends T> items) {
BulkOperations bulkOperations;
BulkMode bulkMode = BulkMode.ORDERED;
if (StringUtils.hasText(this.collection)) {
bulkOperations = this.template.bulkOps(bulkMode, this.collection);
}
else {
bulkOperations = this.template.bulkOps(bulkMode, ClassUtils.getUserClass(items.get(0)));
}
BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, items.get(0));
MongoConverter mongoConverter = this.template.getConverter();
FindAndReplaceOptions upsert = new FindAndReplaceOptions().upsert();
for (Object item : items) {
@@ -176,6 +171,17 @@ public class MongoItemWriter<T> implements ItemWriter<T>, InitializingBean {
bulkOperations.execute();
}
private BulkOperations initBulkOperations(BulkMode bulkMode, Object item) {
BulkOperations bulkOperations;
if (StringUtils.hasText(this.collection)) {
bulkOperations = this.template.bulkOps(bulkMode, this.collection);
}
else {
bulkOperations = this.template.bulkOps(bulkMode, ClassUtils.getUserClass(item));
}
return bulkOperations;
}
private boolean transactionActive() {
return TransactionSynchronizationManager.isActualTransactionActive();
}