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

https://github.com/spring-projects/spring-data-mongodb · Java · 185 lines · 119 code · 44 blank · 22 comment · 2 complexity · ed6661c12f5899620699e35176d14878 MD5 · raw file

  1. /*
  2. * Copyright 2017-2022 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.data.mongodb.core;
  17. import reactor.core.publisher.Mono;
  18. import org.springframework.data.mongodb.core.query.Query;
  19. import org.springframework.data.mongodb.core.query.UpdateDefinition;
  20. import org.springframework.lang.Nullable;
  21. import org.springframework.util.Assert;
  22. import org.springframework.util.StringUtils;
  23. import com.mongodb.client.result.UpdateResult;
  24. /**
  25. * Implementation of {@link ReactiveUpdateOperation}.
  26. *
  27. * @author Mark Paluch
  28. * @author Christoph Strobl
  29. * @since 2.0
  30. */
  31. class ReactiveUpdateOperationSupport implements ReactiveUpdateOperation {
  32. private static final Query ALL_QUERY = new Query();
  33. private final ReactiveMongoTemplate template;
  34. ReactiveUpdateOperationSupport(ReactiveMongoTemplate template) {
  35. this.template = template;
  36. }
  37. @Override
  38. public <T> ReactiveUpdate<T> update(Class<T> domainType) {
  39. Assert.notNull(domainType, "DomainType must not be null");
  40. return new ReactiveUpdateSupport<>(template, domainType, ALL_QUERY, null, null, null, null, null, domainType);
  41. }
  42. static class ReactiveUpdateSupport<T>
  43. implements ReactiveUpdate<T>, UpdateWithCollection<T>, UpdateWithQuery<T>, TerminatingUpdate<T>,
  44. FindAndReplaceWithOptions<T>, FindAndReplaceWithProjection<T>, TerminatingFindAndReplace<T> {
  45. private final ReactiveMongoTemplate template;
  46. private final Class<?> domainType;
  47. private final Query query;
  48. private final org.springframework.data.mongodb.core.query.UpdateDefinition update;
  49. @Nullable private final String collection;
  50. @Nullable private final FindAndModifyOptions findAndModifyOptions;
  51. @Nullable private final FindAndReplaceOptions findAndReplaceOptions;
  52. @Nullable private final Object replacement;
  53. private final Class<T> targetType;
  54. ReactiveUpdateSupport(ReactiveMongoTemplate template, Class<?> domainType, Query query, UpdateDefinition update,
  55. String collection, FindAndModifyOptions findAndModifyOptions, FindAndReplaceOptions findAndReplaceOptions,
  56. Object replacement, Class<T> targetType) {
  57. this.template = template;
  58. this.domainType = domainType;
  59. this.query = query;
  60. this.update = update;
  61. this.collection = collection;
  62. this.findAndModifyOptions = findAndModifyOptions;
  63. this.findAndReplaceOptions = findAndReplaceOptions;
  64. this.replacement = replacement;
  65. this.targetType = targetType;
  66. }
  67. @Override
  68. public TerminatingUpdate<T> apply(org.springframework.data.mongodb.core.query.UpdateDefinition update) {
  69. Assert.notNull(update, "Update must not be null");
  70. return new ReactiveUpdateSupport<>(template, domainType, query, update, collection, findAndModifyOptions,
  71. findAndReplaceOptions, replacement, targetType);
  72. }
  73. @Override
  74. public UpdateWithQuery<T> inCollection(String collection) {
  75. Assert.hasText(collection, "Collection must not be null nor empty");
  76. return new ReactiveUpdateSupport<>(template, domainType, query, update, collection, findAndModifyOptions,
  77. findAndReplaceOptions, replacement, targetType);
  78. }
  79. @Override
  80. public Mono<UpdateResult> first() {
  81. return doUpdate(false, false);
  82. }
  83. @Override
  84. public Mono<UpdateResult> upsert() {
  85. return doUpdate(true, true);
  86. }
  87. @Override
  88. public Mono<T> findAndModify() {
  89. String collectionName = getCollectionName();
  90. return template.findAndModify(query, update,
  91. findAndModifyOptions != null ? findAndModifyOptions : FindAndModifyOptions.none(), targetType,
  92. collectionName);
  93. }
  94. @Override
  95. public Mono<T> findAndReplace() {
  96. return template.findAndReplace(query, replacement,
  97. findAndReplaceOptions != null ? findAndReplaceOptions : FindAndReplaceOptions.none(), (Class) domainType,
  98. getCollectionName(), targetType);
  99. }
  100. @Override
  101. public UpdateWithUpdate<T> matching(Query query) {
  102. Assert.notNull(query, "Query must not be null");
  103. return new ReactiveUpdateSupport<>(template, domainType, query, update, collection, findAndModifyOptions,
  104. findAndReplaceOptions, replacement, targetType);
  105. }
  106. @Override
  107. public Mono<UpdateResult> all() {
  108. return doUpdate(true, false);
  109. }
  110. @Override
  111. public TerminatingFindAndModify<T> withOptions(FindAndModifyOptions options) {
  112. Assert.notNull(options, "Options must not be null");
  113. return new ReactiveUpdateSupport<>(template, domainType, query, update, collection, options,
  114. findAndReplaceOptions, replacement, targetType);
  115. }
  116. @Override
  117. public FindAndReplaceWithProjection<T> replaceWith(T replacement) {
  118. Assert.notNull(replacement, "Replacement must not be null");
  119. return new ReactiveUpdateSupport<>(template, domainType, query, update, collection, findAndModifyOptions,
  120. findAndReplaceOptions, replacement, targetType);
  121. }
  122. @Override
  123. public FindAndReplaceWithProjection<T> withOptions(FindAndReplaceOptions options) {
  124. Assert.notNull(options, "Options must not be null");
  125. return new ReactiveUpdateSupport<>(template, domainType, query, update, collection, findAndModifyOptions, options,
  126. replacement, targetType);
  127. }
  128. @Override
  129. public <R> FindAndReplaceWithOptions<R> as(Class<R> resultType) {
  130. Assert.notNull(resultType, "ResultType must not be null");
  131. return new ReactiveUpdateSupport<>(template, domainType, query, update, collection, findAndModifyOptions,
  132. findAndReplaceOptions, replacement, resultType);
  133. }
  134. private Mono<UpdateResult> doUpdate(boolean multi, boolean upsert) {
  135. return template.doUpdate(getCollectionName(), query, update, domainType, upsert, multi);
  136. }
  137. private String getCollectionName() {
  138. return StringUtils.hasText(collection) ? collection : template.getCollectionName(domainType);
  139. }
  140. }
  141. }