在Reactor Mono中获取地图总和

问题描述

如何将Mono / Flux的所有结果相加

我有一些反应式代码,其中我返回一个Flux并链接其他函数,我将用一些代码来解释它。

return commentService.getAllCommentByIssueId(issueId)
                .map {
                    taggingService.tagging(it.content).map {
                        //Get the count of the returned set and add it up to 
                        //all the other returned map results
                        //So the result should be like
                        //tags in first comment 2,tags in second comment 3
                        //so it should return 5
                    }

所以我返回一个通量,它是评论通量。我想将此查询的所有注释映射到一个函数,在该函数中我扫描每个注释的内容以获取标记,该函数由taggingservice.tagging实现,该函数返回一个Mono<MutableSet<UUID>>,因此它返回了所有被标记的UUID。 一个问题的注释数为x,我想总结所有注释中的所有标签。

我要做的是计算问题的每个注释的所有标签,并将其返回到由StatsModel(issueId,numberOfTagsInComments)组成的StatsModel中。

我现在将向您展示标记功能:

(我测试了该功能,它正在工作)

fun tagging(text:String) : Mono<MutableSet<UUID>> {
        val words = text.split( " ")
                .filter { it.startsWith("@")}
        val matches : MutableSet<UUID> = mutableSetOf()

        return userRepository.findAll().collectList()
                .map { userList ->
                    for(word in words){
                        userList.map {user ->
                            if(user.username == word.substring(1)) {
                                matches.add(user.id!!)
                            }
                        }
                    }
                    matches
                }
    }

还有我的CommentRepository,我正在使用此存储库的第一个函数。

@Repository
interface CommentRepository: ReactiveCrudRepository<CommentModel,UUID> {
    fun findAllByIssueId(issueId: UUID): Flux<CommentModel>
    fun findAllByUserId(userId: UUID): Flux<CommentModel>
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)