Created
August 28, 2012 20:58
-
-
Save opyate/3504304 to your computer and use it in GitHub Desktop.
Rough and ready capped collection support for play-salat v 9265a2643fdae2b2b79d535515336f9eb3b00f3f
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment