Refine contribution #4351

- Rename methods to match operation names
- Update Javadocs
- Fix code formatting

Issue #4149
This commit is contained in:
Mahmoud Ben Hassine
2023-08-22 15:48:07 +02:00
parent 34e3e0e152
commit 83b2a2adf9
2 changed files with 44 additions and 22 deletions

View File

@@ -58,8 +58,29 @@ import org.springframework.util.StringUtils;
*/
public class MongoItemWriter<T> implements ItemWriter<T>, InitializingBean {
/**
* Operation mode of the item writer.
*
* @since 5.1
*/
public enum Mode {
INSERT, UPSERT, REMOVE;
/**
* Insert items into the target collection using
* {@link BulkOperations#insert(Object)}.
*/
INSERT,
/**
* Insert or update items into the target collection using
* {@link BulkOperations#replaceOne(Query, Object, FindAndReplaceOptions)}.
*/
UPSERT,
/**
* Remove items from the target collection using
* {@link BulkOperations#remove(Query)}.
*/
REMOVE;
}
private static final String ID_KEY = "_id";
@@ -79,19 +100,22 @@ public class MongoItemWriter<T> implements ItemWriter<T>, InitializingBean {
/**
* Indicates if the items being passed to the writer are to be saved or removed from
* the data store. If set to false (default), the items will be saved. If set to true,
* the items will be removed.
* the data store. If set to false (default), the items will be saved or update using
* {@link Mode#UPSERT}. If set to true, then items will be removed.
* @param delete removal indicator
* @deprecated use {@link MongoItemWriter#setMode(Mode)}
* @deprecated use {@link MongoItemWriter#setMode(Mode)} instead. Scheduled for
* removal in v5.3 or later.
*/
@Deprecated
@Deprecated(since = "5.1", forRemoval = true)
public void setDelete(boolean delete) {
this.mode = (delete) ? Mode.REMOVE : Mode.UPSERT;
}
/**
* Set the operating {@link Mode} to be applied by this writer.
* Set the operating {@link Mode} to be applied by this writer. Defaults to
* {@link Mode#UPSERT}.
* @param mode the mode to be used.
* @since 5.1
*/
public void setMode(final Mode mode) {
this.mode = mode;
@@ -148,20 +172,14 @@ public class MongoItemWriter<T> implements ItemWriter<T>, InitializingBean {
protected void doWrite(Chunk<? extends T> chunk) {
if (!CollectionUtils.isEmpty(chunk.getItems())) {
switch (this.mode) {
case INSERT:
save(chunk);
break;
case REMOVE:
delete(chunk);
break;
default:
saveOrUpdate(chunk);
break;
case INSERT -> insert(chunk);
case REMOVE -> remove(chunk);
default -> upsert(chunk);
}
}
}
private void save(final Chunk<? extends T> chunk) {
private void insert(final Chunk<? extends T> chunk) {
final BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, chunk.getItems().get(0));
final MongoConverter mongoConverter = this.template.getConverter();
for (final Object item : chunk) {
@@ -172,7 +190,7 @@ public class MongoItemWriter<T> implements ItemWriter<T>, InitializingBean {
bulkOperations.execute();
}
private void delete(Chunk<? extends T> chunk) {
private void remove(Chunk<? extends T> chunk) {
BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, chunk.getItems().get(0));
MongoConverter mongoConverter = this.template.getConverter();
for (Object item : chunk) {
@@ -187,7 +205,7 @@ public class MongoItemWriter<T> implements ItemWriter<T>, InitializingBean {
bulkOperations.execute();
}
private void saveOrUpdate(Chunk<? extends T> chunk) {
private void upsert(Chunk<? extends T> chunk) {
BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, chunk.getItems().get(0));
MongoConverter mongoConverter = this.template.getConverter();
FindAndReplaceOptions upsert = new FindAndReplaceOptions().upsert();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import org.springframework.util.Assert;
* A builder implementation for the {@link MongoItemWriter}
*
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
* @since 4.0
* @see MongoItemWriter
*/
@@ -43,9 +44,10 @@ public class MongoItemWriterBuilder<T> {
* @param delete removal indicator
* @return The current instance of the builder
* @see MongoItemWriter#setDelete(boolean)
* @deprecated use {@link MongoItemWriterBuilder#mode(Mode)}
* @deprecated Use {@link MongoItemWriterBuilder#mode(Mode)} instead. Scheduled for
* removal in v5.3 or later.
*/
@Deprecated
@Deprecated(since = "5.1", forRemoval = true)
public MongoItemWriterBuilder<T> delete(boolean delete) {
this.mode = (delete) ? Mode.REMOVE : Mode.UPSERT;
@@ -53,10 +55,12 @@ public class MongoItemWriterBuilder<T> {
}
/**
* Set the operating {@link Mode} to be applied by this writer.
* Set the operating {@link Mode} to be applied by this writer. Defaults to
* {@link Mode#UPSERT}.
* @param mode the mode to be used.
* @return The current instance of the builder
* @see MongoItemWriter#setMode(Mode)
* @since 5.1
*/
public MongoItemWriterBuilder<T> mode(final Mode mode) {
this.mode = mode;