Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From e412c574bb581b003736591bea4d197e086d32cf Mon Sep 17 00:00:00 2001
- From: Juan Uys <opyate@gmail.com>
- Date: Tue, 28 Aug 2012 21:54:25 +0100
- Subject: [PATCH] capped collection support
- ---
- .../scala/se/radley/plugin/salat/SalatPlugin.scala | 26 +++++++++++++++++++-
- .../scala/se/radley/plugin/salat/package.scala | 11 ++++++++
- 2 files changed, 36 insertions(+), 1 deletions(-)
- diff --git a/src/main/scala/se/radley/plugin/salat/SalatPlugin.scala b/src/main/scala/se/radley/plugin/salat/SalatPlugin.scala
- index 17048b2..c177998 100644
- --- a/src/main/scala/se/radley/plugin/salat/SalatPlugin.scala
- +++ b/src/main/scala/se/radley/plugin/salat/SalatPlugin.scala
- @@ -5,6 +5,7 @@ import play.api.mvc._
- import play.api.Play.current
- import com.mongodb.casbah.{WriteConcern, MongoCollection, MongoConnection, MongoURI}
- import com.mongodb.{MongoException, ServerAddress}
- +import com.mongodb.casbah.commons.MongoDBObject
- class SalatPlugin(app: Application) extends Plugin {
- @@ -35,8 +36,20 @@ class SalatPlugin(app: Application) extends Plugin {
- }
- def collection(name: String) = connection(db)(name)
- +
- + def collection(name: String, pairs: Map[String, Any]): MongoCollection = {
- + val coll = if (connection(db).collectionExists(name)) {
- + connection(db)(name)
- + } else {
- + import com.mongodb.casbah.Implicits.mongoCollAsScala
- + connection(db).createCollection(name, MongoDBObject(pairs.toList)).asScala
- + }
- + coll
- + }
- def apply(name: String) = collection(name)
- +
- + def apply(name: String, pairs: Map[String, Any]) = collection(name, pairs)
- override def toString() = {
- (if (user.isDefined) user.get + "@" else "") +
- @@ -126,7 +139,7 @@ class SalatPlugin(app: Application) extends Plugin {
- def source(source: String): MongoSource = {
- sources.get(source).getOrElse(throw configuration.reportError("mongodb." + source, source + " doesn't exist"))
- }
- -
- +
- /**
- * Returns MongoCollection that has been configured in application.conf
- * @param collectionName The MongoDB collection name
- @@ -134,4 +147,15 @@ class SalatPlugin(app: Application) extends Plugin {
- * @return A MongoCollection
- */
- def collection(collectionName:String, sourceName:String = "default"): MongoCollection = source(sourceName)(collectionName)
- +
- + /**
- + * Returns MongoCollection that has been configured in application.conf with extra options
- + * @param collectionName The MongoDB collection name
- + * @param options the options with which to create the collection
- + * @param sourceName The source name ex. default
- + * @return A MongoCollection
- + */
- + def collectionWithOptions(collectionName:String, options: Map[String, Any], sourceName:String = "default"): MongoCollection = {
- + source(sourceName)(collectionName, options)
- + }
- }
- diff --git a/src/main/scala/se/radley/plugin/salat/package.scala b/src/main/scala/se/radley/plugin/salat/package.scala
- index c2a66a0..49e1ae3 100644
- --- a/src/main/scala/se/radley/plugin/salat/package.scala
- +++ b/src/main/scala/se/radley/plugin/salat/package.scala
- @@ -15,5 +15,16 @@ package object salat {
- def mongoCollection(collectionName: String, sourceName:String = "default")(implicit app: Application): MongoCollection = {
- app.plugin[SalatPlugin].map(_.collection(collectionName, sourceName)).getOrElse(throw PlayException("SalatPlugin is not registered.", "You need to register the plugin with \"500:se.radley.plugin.salat.SalatPlugin\" in conf/play.plugins"))
- }
- +
- + /**
- + * get the underlying salat MongoCollection as a capped collection
- + * @param collectionName The MongoDB collection name
- + * @param cappedAt the size of the capped collection
- + * @param sourceName The configured source name
- + * @return MongoCollection
- + */
- + def mongoCappedCollection(collectionName: String, cappedAt: Int, sourceName:String = "default")(implicit app: Application): MongoCollection = {
- + app.plugin[SalatPlugin].map(_.collectionWithOptions(collectionName, Map("capped" -> true, "size" -> cappedAt), sourceName)).getOrElse(throw PlayException("SalatPlugin is not registered.", "You need to register the plugin with \"500:se.radley.plugin.salat.SalatPlugin\" in conf/play.plugins"))
- + }
- }
- --
- 1.7.7.5 (Apple Git-26)
Add Comment
Please, Sign In to add comment