我有一个根对象,它有一个嵌套的对象列表。我需要访问该嵌套的对象列表,对它们执行一些操作,然后将结果保存回去。
我似乎让自己绊倒了嵌套的flatMaps
。
我想出的最好的方法是:
public Flux<Face> scanSingleVideo(@PathVariable final String id) {
// Reactive MongoDB Find by ID
return videoRepository.findById(id)
// A single video is returned, the video has an ArrayList of thumbnails
.flatMapMany(video -> Flux.fromIterable(video.getThumbnails()))
// Each thumbnail has a 'path' value which is just a string
.map(Thumbnail::getPath)
// Passing the path into a function which does some business work. The function returns a Flux of faces
.flatMap(faceService::detectFaces);
// Q: How can I get these new objects into an array on the Thumbnail, then save the video object with the updated thumbnail?
}
但是,即使这是正确的方向,我现在仍然坚持。对于上下文,这是我正在使用的两个域
视频对象
public class Video {
// {..}
List<Thumbnail> thumbnails = new ArrayList<>();
}
缩略图对象
public class Thumbnail {
// {..}
String path;
Set<Face> faces = new HashSet<>();
}
我第一次尝试接近是因为它在Thumbnail
上设置了新对象,但是非常丑陋,感觉像我在某个地方出错或错过了可以使用的关键方法。但是,这确实起作用。
public Mono<Video> scanSingleVideo(@PathVariable final String id) {
return videoRepository.findById(id)
.flatMap(video -> {
video.getThumbnails().forEach(thumbnail -> faceService
.detectFaces(thumbnail.getPath())
.flatMap(face -> {
thumbnail.getFaces().add(face);
return Mono.just(thumbnail);
}).flatMap(t -> {
video.getThumbnails().add(t);
return videoRepository.save(video);
}).subscribe());
return videoRepository.save(video);
});
}
我不喜欢嵌套订阅,但是我不确定其他方法。
编辑:对于遇到此问题的任何人,您都可以稍微清理一下我的“工作”尝试,但我仍然认为这是不正确的。
public Mono<Video> scanSingleVideo(@PathVariable final String id) {
return videoRepository.findById(id)
.flatMap(video -> {
video.getThumbnails().forEach(thumbnail -> faceService
.detectFaces(thumbnail.getPath())
.flatMap(face -> getPublisher(video, thumbnail, face))
.distinct()
.subscribe());
return videoRepository.save(video);
});
}
private Publisher<? extends Video> getPublisher(Video video, Thumbnail thumbnail, Face face) {
thumbnail.getFaces().add(face);
video.getThumbnails().add(thumbnail);
return videoRepository.save(video);
}