Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ReactiveMongo - non blocking and asynchronous I/O operations

4,525 views

Published on

ReactiveMongo is a scala driver that provides fully non-blocking and asynchronous I/O operations.

Published in: Software

ReactiveMongo - non blocking and asynchronous I/O operations

  1. 1. reactivemongo by david pichsenmeister
  2. 2. overview ● fully non-blocking ● asynchronous I/O operations ● build against Akka 2.2 & 2.3 ● build against Scala 2.10 & 2.11
  3. 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. 4. case class Pokemon( _id: BSONObjectID, name: String, category: String, moves: List[String] ) object Pokemon { implicit val pokemonFormat: Format[Pokemon] = Json.format[Pokemon] }
  5. 5. object PokemonDAO { private def collectionName: String = "pokemon" private def db = ReactiveMongoPlugin.db private def collection = db[JSONCollection](collectionName)
  6. 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. 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. 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. 9. more features ● GridFS as non-blocking, streaming datastore ● Enumerator/Iteratee pattern to avoid huge memory usage
  10. 10. val cursor = collection.find(BSONDocument()).cursor[Pokemon] cursor.enumerate().apply(Iteratee.foreach { pokemon => println("found pokemon: " + pokemon.name) }) enumerator/iteratee
  11. 11. resources http://reactivemongo.org https://github.com/ReactiveMongo/Play-ReactiveMongo http://www.pokemon.com
  12. 12. thanks! feel free to add me on: .../3x14159265

×