Skip to content

Instantly share code, notes, and snippets.

@mathieuancelin
Created October 27, 2013 22:07
Show Gist options
  • Save mathieuancelin/7188439 to your computer and use it in GitHub Desktop.
Save mathieuancelin/7188439 to your computer and use it in GitHub Desktop.
Kind of capped collection on top of Couchbase (without fixed size)
package controllers
import play.api.mvc.{Controller, Action}
import org.ancelin.play2.couchbase.Couchbase
import org.ancelin.play2.couchbase.CouchbaseRWImplicits.jsObjectToDocumentWriter
import org.ancelin.play2.couchbase.CouchbaseRWImplicits.documentAsJsValuetReader
import play.api.libs.json._
import play.api.libs.EventSource
import play.api.Play.current
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import java.util.UUID
object Application extends Controller {
val bucket = Couchbase.bucket("default")
def insert(name: String) = Action.async {
bucket.set[JsObject](UUID.randomUUID().toString, Json.obj(
"index" -> System.currentTimeMillis(),
"name" -> name
)).map(_ => Ok("done"))
}
def capped = Action {
Ok.feed(
bucket.cappedQuery[JsValue](Couchbase.docName("capped"), "by_index", {e => (e \ "index").as[Long]}) &> EventSource()
).as("text/event-stream")
}
}
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET /capped controllers.Application.capped
GET /insert controllers.Application.insert(name)
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
{
"name":"capped",
"views":{
"by_index": {
"map":"function(doc, meta) {if(doc.index) {emit(doc.index, null);}}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment