Scala 带有Akka Streams自定义源的ReactiveMongo

Scala 带有Akka Streams自定义源的ReactiveMongo,scala,akka-stream,reactivemongo,Scala,Akka Stream,Reactivemongo,我正在使用reactivemongo-akka流,并试图转换AkkaStreamCursor.documentSource。我现在看到两个问题: 声明此操作返回源[T,未使用]但集合。查找(查询)。游标[BSONDocument]。documentsource()返回源[BSONDocument,未来[State]。有没有办法避免State对象 假设我使用Future[State]我希望获得类的源代码,如下所示 case class Inner(foo: String, baz: Int) c

我正在使用reactivemongo-akka流,并试图转换
AkkaStreamCursor.documentSource
。我现在看到两个问题:

  • 声明此操作返回
    源[T,未使用]
    集合。查找(查询)。游标[BSONDocument]。documentsource()
    返回
    源[BSONDocument,未来[State]
    。有没有办法避免State对象

  • 假设我使用
    Future[State]
    我希望获得类的源代码,如下所示

    case class Inner(foo: String, baz: Int)
    
    case class Outer(bar: Inner)
    
    //
    implicit object InnerReader extends BSONDocumentReader[Inner]//defined
    
    val getCollection: Future[BSONCollection] = connection.database("db").map(_.collection("things")
    
    def stream()(implicit m: Materializer): Source[Outer, Future[State]] = {
     getCollection.map(_.find().cursor[Inner]().documentSource()).map(_.via(Flow[Inner].map(in => Outer(in))))
    
  • 但这并没有返回我可以处理的
    Future[Source[Outer,Future[State]]
    ,而是返回
    Future[Source[internal,Future[State].#Repr[Outer]]


    如何将bson阅读器用于此库?

    根据cchantep的建议,我需要将
    fromFuture
    flatMapConcat一起使用:

    def stream()(implicit m: Materializer): Source[Outer, NotUsed] = {
     val foo = getCollection.map(x => col2Source(x))
    
     fix(foo).via(flowmap(map = Outer(_)))
    }
    def col2Source(col: BSONCollection): Source[Inner, Future[State]] = {
     val cursor: AkkaStreamCursor[Inner] = 
       col.find(BSONDocument.empty).cursor[Inner]()
    
     cursor.documentSource()
    }
    
    def flowmap[In, Out](
     map: (In) => Out
    ): Flow[In, Out, NotUsed] = Flow[In].map(e => map(e))
    
    def fix[Out, Mat](futureSource: Future[Source[Out, Mat]]): Source[Out, NotUsed] = {
     Source.fromFuture(futureSource).flatMapConcat(identity)
    }
    

    链接文档中的第一个示例使用
    Future[State]
    。如果你不关心物质化状态,那么就不要使用它。那么BSON读者就没有责任处理异步问题。要扁平化未来之源,你可以使用flatMapConcat和fromFuture,或者从Akka 2.5.1 source.fromFutureGraph。你所说的BSON处理异步问题是什么意思?我只想强调一下eam进入定制案例类…我不太了解
    #Repr[Outer]
    的类型签名。您是否尝试过建议的
    flatMapConcat
    fromFutureGraph
    解决方案?(这样的
    Repr
    依赖类型在Akka Stream中很常见)。