4

I am integrating an application using ReactiveMongo with a legacy application.

As, I must maintain legacy application interfaces at some point I must block and/or transform my code into the specified interface types. I have that code distilled down to the example below.

Is there a better way than, getChunks, to consume all of an Enumerator with the output type being a List? What is the standard practice?

implicit def legacyAdapter[TInput,TResult]
  (block: Future[Enumerator[TInput]])
  (implicit translator : (TInput => TResult), 
            executionContext:ExecutionContext,
            timeOut : Duration): List[TResult] = {

  val iter = Iteratee.getChunks[TResult]
  val exhaustFuture = block.flatMap{
    enumy => { enumy.map(i => translator(i) ).run(iter) }
  }

  val r  = Await.result(exhaustFuture , timeOut)
  r
}
Yuriy
  • 2,772
  • 15
  • 22
bearrito
  • 2,217
  • 1
  • 25
  • 36

2 Answers2

2

Iteratee.getChunks is the only utility offered by playframework that build a list by consuming all chuncks of an enumerator, you can of course do the same thing using Iteratee.fold but you will reinvent the wheel as Iteratee.getChunks uses Iteratee.fold.

faissalb
  • 1,739
  • 1
  • 12
  • 14
0

We found that exposing the Collect method of http://reactivemongo.org/releases/0.10/api/index.html#reactivemongo.api.Cursor to be more performant than collecting the chunks of the Enumerator. So in a sense we solved our problem by changing the problem.

That did mean an API change for our data layer but the performance improvements allowed for it.

bearrito
  • 2,217
  • 1
  • 25
  • 36