Retain order doing reactive save operations with multiple elements.

Ensure subscription order on multi document operations.

Original pull request: #4824
Closes #4804
This commit is contained in:
Christoph Strobl
2024-10-31 07:46:58 +01:00
committed by Mark Paluch
parent 6200440326
commit d0ee280f09
2 changed files with 7 additions and 9 deletions

View File

@@ -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,

View File

@@ -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
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
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
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
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);
}
// -------------------------------------------------------------------------