Contenu connexe
Similaire à ReactiveMongo - non blocking and asynchronous I/O operations (20)
Plus de David Pichsenmeister (7)
ReactiveMongo - non blocking and asynchronous I/O operations
- 2. overview
● fully non-blocking
● asynchronous I/O operations
● build against Akka 2.2 & 2.3
● build against Scala 2.10 & 2.11
- 3. play! 2.3 plugin
● support to play! 2.3
● JSON <-> BSON conversion with
embedded JSON library in play!
● JSONCollection instead of
ReactiveMongo’s BSONDocument
- 4. case class Pokemon(
_id: BSONObjectID,
name: String,
category: String,
moves: List[String]
)
object Pokemon {
implicit val pokemonFormat: Format[Pokemon] =
Json.format[Pokemon]
}
- 5. object PokemonDAO {
private def collectionName: String = "pokemon"
private def db = ReactiveMongoPlugin.db
private def collection =
db[JSONCollection](collectionName)
- 6. def insert(pokemon: Pokemon): Future[LastError] = {
collection.insert(Json.toJson(pokemon))
}
def update(pokemon: Pokemon): Future[LastError] = {
collection.update(BSONDocument("_id" -> pokemon._id),
Json.toJson(pokemon))
}
def remove(pokemon: Pokemon): Future[LastError] = {
collection.remove(BSONDocument("_id" -> pokemon._id))
}
- 7. def findById(id: BSONObjectID): Future[Option[Pokemon] = {
collection.find(BSONDocument("_id" ->
id)).cursor[Pokemon].headOption
}
def findByCategory(cat: String): Future[List[Pokemon] = {
collection.find(BSONDocument("category" ->
cat)).options(QueryOpts(skipN = 0, batchSizeN
= 10)).cursor[Pokemon].collect[List]()
}
def catchEmAll(): Future[List[Pokemon] = {
collection.find(BSONDocument()).
cursor[Pokemon].collect[List]()
}
- 8. def update() = Action.async(parse.json) {
// DON’T do this at home, kids!!
val pokemon = Json.fromJson[Pokemon](request.body).get
val future: Future =PokemonDAO.update(pokemon)
future.map(lastError => {
Accepted
})
}
controller
- 9. more features
● GridFS as non-blocking, streaming
datastore
● Enumerator/Iteratee pattern to avoid
huge memory usage
- 10. val cursor = collection.find(BSONDocument()).cursor[Pokemon]
cursor.enumerate().apply(Iteratee.foreach { pokemon =>
println("found pokemon: " + pokemon.name)
})
enumerator/iteratee