Adapt Enumerator / Iteratee to Stream

I have a Spray application where I'm using reactive-mongo to connect to my Mongo database.

val collection: BSONCollection = db("ping")
val cursor: Cursor[Ping] = collection.find(BSONDocument()).cursor[Ping]
val e: Enumerator[Ping] = cursor.enumerate()

ReactiveMongo's cursor allows me to get the data by chunks. This cursor can be transformed to a play.api.libs.iteratee.Enumerator which is in turn connected to a play.api.libs.iteratee.Iteratee where any code I want is called for each element of the collection. "Ping" is my domain entity.

With Spray it's possible to return a Stream[Ping] that will be marshalled into a JSON response. I'd like to know if it's possible to adapt the Enumerator or the Iteratee to a Stream, so I can return that.

Answers


You can use collect[Stream] instead of enumerate on the Cursor instance to do that:

val collection: BSONCollection = db("ping")
val cursor: Cursor[Ping] = collection.find(BSONDocument()).cursor[Ping]
val stream: Stream[Ping] = cursor.toList.toStream

EDIT: Used cursor.toList.toStream. This will gather all the documents matched by the query, then produce a stream.


Need Your Help

Spring Security 3.1: How To Get Security To Kick In At An Alternative Entry Point

java authentication spring-security

I'm new to Spring. I've recently learned to use Spring 3.1 to implement LDAP authentication. I was told to customize the authentication such that if a user from an IP address in the database come...

change the certificate Charles Proxy uses

ssl proxy charles pinning

I would like to implement pinning on my Android app ( https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning ). In short, it's a whitelist of Public Keys that the client will accept. ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.