use of org.springframework.data.mongodb.core.convert.MongoWriter in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method doInsertBatch.
protected <T> Flux<T> doInsertBatch(String collectionName, Collection<? extends T> batchToSave, MongoWriter<Object> writer) {
Assert.notNull(writer, "MongoWriter must not be null!");
Mono<List<Tuple2<AdaptibleEntity<T>, Document>>> prepareDocuments = Flux.fromIterable(batchToSave).flatMap(uninitialized -> {
BeforeConvertEvent<T> event = new BeforeConvertEvent<>(uninitialized, collectionName);
T toConvert = maybeEmitEvent(event).getSource();
return maybeCallBeforeConvert(toConvert, collectionName).flatMap(it -> {
AdaptibleEntity<T> entity = operations.forEntity(it, mongoConverter.getConversionService());
entity.assertUpdateableIdIfNotSet();
T initialized = entity.initializeVersionProperty();
Document dbDoc = entity.toMappedDocument(writer).getDocument();
maybeEmitEvent(new BeforeSaveEvent<>(initialized, dbDoc, collectionName));
return maybeCallBeforeSave(initialized, dbDoc, collectionName).thenReturn(Tuples.of(entity, dbDoc));
});
}).collectList();
Flux<Tuple2<AdaptibleEntity<T>, Document>> insertDocuments = prepareDocuments.flatMapMany(tuples -> {
List<Document> documents = tuples.stream().map(Tuple2::getT2).collect(Collectors.toList());
return insertDocumentList(collectionName, documents).thenMany(Flux.fromIterable(tuples));
});
return insertDocuments.flatMap(tuple -> {
Document document = tuple.getT2();
Object id = MappedDocument.of(document).getId();
T saved = tuple.getT1().populateIdIfNecessary(id);
maybeEmitEvent(new AfterSaveEvent<>(saved, document, collectionName));
return maybeCallAfterSave(saved, document, collectionName);
});
}
Aggregations