Created
October 27, 2013 22:07
-
-
Save mathieuancelin/7188439 to your computer and use it in GitHub Desktop.
Kind of capped collection on top of Couchbase (without fixed size)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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