Guest User

Untitled

a guest
Dec 11th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. From e412c574bb581b003736591bea4d197e086d32cf Mon Sep 17 00:00:00 2001
  2. From: Juan Uys <opyate@gmail.com>
  3. Date: Tue, 28 Aug 2012 21:54:25 +0100
  4. Subject: [PATCH] capped collection support
  5.  
  6. ---
  7. .../scala/se/radley/plugin/salat/SalatPlugin.scala | 26 +++++++++++++++++++-
  8. .../scala/se/radley/plugin/salat/package.scala | 11 ++++++++
  9. 2 files changed, 36 insertions(+), 1 deletions(-)
  10.  
  11. diff --git a/src/main/scala/se/radley/plugin/salat/SalatPlugin.scala b/src/main/scala/se/radley/plugin/salat/SalatPlugin.scala
  12. index 17048b2..c177998 100644
  13. --- a/src/main/scala/se/radley/plugin/salat/SalatPlugin.scala
  14. +++ b/src/main/scala/se/radley/plugin/salat/SalatPlugin.scala
  15. @@ -5,6 +5,7 @@ import play.api.mvc._
  16. import play.api.Play.current
  17. import com.mongodb.casbah.{WriteConcern, MongoCollection, MongoConnection, MongoURI}
  18. import com.mongodb.{MongoException, ServerAddress}
  19. +import com.mongodb.casbah.commons.MongoDBObject
  20.  
  21. class SalatPlugin(app: Application) extends Plugin {
  22.  
  23. @@ -35,8 +36,20 @@ class SalatPlugin(app: Application) extends Plugin {
  24. }
  25.  
  26. def collection(name: String) = connection(db)(name)
  27. +
  28. + def collection(name: String, pairs: Map[String, Any]): MongoCollection = {
  29. + val coll = if (connection(db).collectionExists(name)) {
  30. + connection(db)(name)
  31. + } else {
  32. + import com.mongodb.casbah.Implicits.mongoCollAsScala
  33. + connection(db).createCollection(name, MongoDBObject(pairs.toList)).asScala
  34. + }
  35. + coll
  36. + }
  37.  
  38. def apply(name: String) = collection(name)
  39. +
  40. + def apply(name: String, pairs: Map[String, Any]) = collection(name, pairs)
  41.  
  42. override def toString() = {
  43. (if (user.isDefined) user.get + "@" else "") +
  44. @@ -126,7 +139,7 @@ class SalatPlugin(app: Application) extends Plugin {
  45. def source(source: String): MongoSource = {
  46. sources.get(source).getOrElse(throw configuration.reportError("mongodb." + source, source + " doesn't exist"))
  47. }
  48. -
  49. +
  50. /**
  51. * Returns MongoCollection that has been configured in application.conf
  52. * @param collectionName The MongoDB collection name
  53. @@ -134,4 +147,15 @@ class SalatPlugin(app: Application) extends Plugin {
  54. * @return A MongoCollection
  55. */
  56. def collection(collectionName:String, sourceName:String = "default"): MongoCollection = source(sourceName)(collectionName)
  57. +
  58. + /**
  59. + * Returns MongoCollection that has been configured in application.conf with extra options
  60. + * @param collectionName The MongoDB collection name
  61. + * @param options the options with which to create the collection
  62. + * @param sourceName The source name ex. default
  63. + * @return A MongoCollection
  64. + */
  65. + def collectionWithOptions(collectionName:String, options: Map[String, Any], sourceName:String = "default"): MongoCollection = {
  66. + source(sourceName)(collectionName, options)
  67. + }
  68. }
  69. diff --git a/src/main/scala/se/radley/plugin/salat/package.scala b/src/main/scala/se/radley/plugin/salat/package.scala
  70. index c2a66a0..49e1ae3 100644
  71. --- a/src/main/scala/se/radley/plugin/salat/package.scala
  72. +++ b/src/main/scala/se/radley/plugin/salat/package.scala
  73. @@ -15,5 +15,16 @@ package object salat {
  74. def mongoCollection(collectionName: String, sourceName:String = "default")(implicit app: Application): MongoCollection = {
  75. 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"))
  76. }
  77. +
  78. + /**
  79. + * get the underlying salat MongoCollection as a capped collection
  80. + * @param collectionName The MongoDB collection name
  81. + * @param cappedAt the size of the capped collection
  82. + * @param sourceName The configured source name
  83. + * @return MongoCollection
  84. + */
  85. + def mongoCappedCollection(collectionName: String, cappedAt: Int, sourceName:String = "default")(implicit app: Application): MongoCollection = {
  86. + 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"))
  87. + }
  88.  
  89. }
  90. --
  91. 1.7.7.5 (Apple Git-26)
Add Comment
Please, Sign In to add comment