Browse Source

Retain order doing reactive save operations with multiple elements.

Ensure subscription order on multi document operations.

Original pull request: #4824
Closes #4804
4.3.x
Christoph Strobl 1 year ago committed by Mark Paluch
parent
commit
d0ee280f09
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
  2. 14
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

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

@ -1413,7 +1413,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati @@ -1413,7 +1413,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
});
return Flux.fromIterable(elementsByCollection.keySet())
.flatMap(collectionName -> doInsertBatch(collectionName, elementsByCollection.get(collectionName), writer));
.concatMap(collectionName -> doInsertBatch(collectionName, elementsByCollection.get(collectionName), writer));
}
protected <T> Flux<T> doInsertBatch(String collectionName, Collection<? extends T> batchToSave,

14
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

@ -112,8 +112,8 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -112,8 +112,8 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
Streamable<S> source = Streamable.of(entities);
return source.stream().allMatch(entityInformation::isNew) ? //
mongoOperations.insert(source.stream().collect(Collectors.toList()), entityInformation.getCollectionName()) : //
Flux.fromIterable(entities).flatMap(this::save);
insert(entities) :
Flux.fromIterable(entities).concatMap(this::save);
}
@Override
@ -121,7 +121,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -121,7 +121,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
Assert.notNull(entityStream, "The given Publisher of entities must not be null");
return Flux.from(entityStream).flatMapSequential(entity -> entityInformation.isNew(entity) ? //
return Flux.from(entityStream).concatMap(entity -> entityInformation.isNew(entity) ? //
mongoOperations.insert(entity, entityInformation.getCollectionName()) : //
mongoOperations.save(entity, entityInformation.getCollectionName()));
}
@ -295,7 +295,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -295,7 +295,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
Optional<ReadPreference> readPreference = getReadPreference();
return Flux.from(entityStream)//
.map(entityInformation::getRequiredId)//
.flatMap(id -> deleteById(id, readPreference))//
.concatMap(id -> deleteById(id, readPreference))//
.then();
}
@ -336,8 +336,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -336,8 +336,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
Assert.notNull(entities, "The given Iterable of entities must not be null");
Collection<S> source = toCollection(entities);
return source.isEmpty() ? Flux.empty() : mongoOperations.insertAll(source);
return source.isEmpty() ? Flux.empty() : mongoOperations.insert(source, entityInformation.getCollectionName());
}
@Override
@ -345,8 +344,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement @@ -345,8 +344,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
Assert.notNull(entities, "The given Publisher of entities must not be null");
return Flux.from(entities)
.flatMapSequential(entity -> mongoOperations.insert(entity, entityInformation.getCollectionName()));
return Flux.from(entities).concatMap(this::insert);
}
// -------------------------------------------------------------------------

Loading…
Cancel
Save