From 83b2a2adf92a6b336ae14679e92032a02644019e Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 22 Aug 2023 15:48:07 +0200 Subject: [PATCH] Refine contribution #4351 - Rename methods to match operation names - Update Javadocs - Fix code formatting Issue #4149 --- .../batch/item/data/MongoItemWriter.java | 54 ++++++++++++------- .../data/builder/MongoItemWriterBuilder.java | 12 +++-- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java index 0592b3565..81d003b51 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemWriter.java @@ -58,8 +58,29 @@ import org.springframework.util.StringUtils; */ public class MongoItemWriter implements ItemWriter, 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 implements ItemWriter, 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 implements ItemWriter, InitializingBean { protected void doWrite(Chunk 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 chunk) { + private void insert(final Chunk 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 implements ItemWriter, InitializingBean { bulkOperations.execute(); } - private void delete(Chunk chunk) { + private void remove(Chunk 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 implements ItemWriter, InitializingBean { bulkOperations.execute(); } - private void saveOrUpdate(Chunk chunk) { + private void upsert(Chunk chunk) { BulkOperations bulkOperations = initBulkOperations(BulkMode.ORDERED, chunk.getItems().get(0)); MongoConverter mongoConverter = this.template.getConverter(); FindAndReplaceOptions upsert = new FindAndReplaceOptions().upsert(); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilder.java index 3e080fecd..4df60a7d4 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemWriterBuilder.java @@ -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 { * @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 delete(boolean delete) { this.mode = (delete) ? Mode.REMOVE : Mode.UPSERT; @@ -53,10 +55,12 @@ public class MongoItemWriterBuilder { } /** - * 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 mode(final Mode mode) { this.mode = mode;