MongoDB configuration, connecting, test
authorChristos KK Loverdos <loverdos@gmail.com>
Fri, 25 Nov 2011 12:16:13 +0000 (14:16 +0200)
committerChristos KK Loverdos <loverdos@gmail.com>
Fri, 25 Nov 2011 12:16:13 +0000 (14:16 +0200)
logic/pom.xml
logic/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBCollection.scala [new file with mode: 0644]
logic/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBConnection.scala
logic/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBStore.scala
logic/src/main/scala/gr/grnet/aquarium/store/mongodb/confmodel/MongoDBCollectionModel.scala [new file with mode: 0644]
logic/src/main/scala/gr/grnet/aquarium/store/mongodb/confmodel/MongoDBConnectionModel.scala [moved from logic/src/main/scala/gr/grnet/aquarium/store/mongodb/confmodel/MongoDBConfigurationModel.scala with 96% similarity]
logic/src/main/scala/gr/grnet/aquarium/store/mongodb/confmodel/ServerAddressConfigurationModel.scala
logic/src/main/scala/gr/grnet/aquarium/util/xstream/XStreamHelpers.scala
logic/src/test/resources/mongodb/aquarium-message-store.xml
logic/src/test/resources/mongodb/local-message-store.xml
logic/src/test/scala/gr/grnet/aquarium/store/mongodb/MongoDBStoreTest.scala

index dcd928c..654b51c 100644 (file)
 
     <!-- Official MongoDB scala driver -->
     <!-- For issues with the driver see: https://jira.mongodb.org/browse/SCALA -->
-    <dependency>
-      <groupId>com.mongodb.casbah</groupId>
-      <artifactId>casbah-core_2.9.1</artifactId>
-      <version>2.1.5-1</version>
-    </dependency>
+    <!--<dependency>-->
+      <!--<groupId>com.mongodb.casbah</groupId>-->
+      <!--<artifactId>casbah-core_2.9.1</artifactId>-->
+      <!--<version>2.1.5-1</version>-->
+    <!--</dependency>-->
   </dependencies>
 </project>
diff --git a/logic/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBCollection.scala b/logic/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBCollection.scala
new file mode 100644 (file)
index 0000000..58398cf
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.aquarium.store
+package mongodb
+
+import confmodel.MongoDBCollectionModel
+import com.mongodb.{WriteConcern, BasicDBObject}
+import gr.grnet.aquarium.util.Loggable
+
+/**
+ * 
+ * @author Christos KK Loverdos <loverdos@gmail.com>.
+ */
+class MongoDBCollection(private[mongodb] val owner: MongoDBConnection, confModel: MongoDBCollectionModel) extends MessageStore with Loggable {
+  def name = confModel.name
+  
+  private[mongodb] lazy val (_mongoDB, _mongoCollection) = {
+    val db   = owner._mongo.getDB(confModel.mongoDBName)
+    val coll = db.getCollection(confModel.mongoCollectionName)
+    (db, coll)
+  }
+
+  def storeString(message: String) = {
+    val obj = new BasicDBObject("event", message)
+    val writeResult = _mongoCollection.insert(obj, WriteConcern.valueOf(confModel.writeConcern))
+    logger.debug("Wrote message %s and got result %s".format(message, writeResult))
+    null
+  }
+}
\ No newline at end of file
index 8267fbb..af6fc2a 100644 (file)
 package gr.grnet.aquarium.store
 package mongodb
 
-import confmodel.MongoDBConfigurationModel
-import com.mongodb.casbah.MongoConnection
-import com.mongodb.{WriteConcern, ServerAddress}
+import confmodel.MongoDBConnectionModel
 import gr.grnet.aquarium.util.Loggable
+import scala.collection.JavaConversions._
+import com.mongodb.{Mongo, WriteConcern, ServerAddress}
 
 /**
  *
  * @author Christos KK Loverdos <loverdos@gmail.com>.
  */
-class MongoDBConnection(val confModel: MongoDBConfigurationModel) extends Loggable {
+class MongoDBConnection(val confModel: MongoDBConnectionModel) extends Loggable {
 
-  private[mongodb] lazy val _mongoConnection = {
+  private[mongodb] lazy val _mongo = {
     val hosts = confModel.hosts
-    val serverAddresses = hosts.map(sacm => new ServerAddress(sacm.host, sacm.port))
 
-    val mongo = MongoConnection(serverAddresses)
-    logger.info("Created MongoDB connection %s for hosts %s".format(mongo, confModel.hosts.map(h => "%s:%s".format(h.host, h.port)).mkString(", ")))
-
-    if(confModel.slaveOK) {
-      mongo.slaveOk()
-      logger.info("Set slaveOK for MongoDB connection %s".format(mongo))
+    val mongo = if(hosts.size == 1) {
+      val sacm = hosts.head
+      new Mongo(sacm.host, sacm.port)
+    } else {
+      val serverAddresses = hosts.map(sacm => new ServerAddress(sacm.host, sacm.port))
+      new Mongo(serverAddresses)
     }
-    val writeConcern = WriteConcern.valueOf(confModel.writeConcern)
-    mongo.setWriteConcern(writeConcern)
-    logger.info("Set WriteConcern %s for MongoDB connection %s".format(confModel.writeConcern, mongo))
 
     mongo
   }
+
+
+  private lazy val _collections = confModel.collections.map(new MongoDBCollection(this, _))
+  def collections: List[MongoDBCollection] = _collections
+
+  def findCollection(name: String): Option[MongoDBCollection] = collections.find(_.name == name)
 }
 
 object MongoDBConnection {
@@ -78,9 +80,11 @@ object MongoDBConnection {
   
   object DBNames {
     val test = "test"
+    val aquarium = "aquarium"
   }
 
   object CollectionNames {
-      val test = "test"
-    }
+    val test = "test"
+    val events = "events"
+  }
 }
\ No newline at end of file
index afcf9a4..e532fc5 100644 (file)
@@ -47,7 +47,7 @@ import com.ckkloverdos.maybe.NoVal
  */
 class MongoDBStore(connection: MongoDBConnection) extends MessageStore {
   def storeString(message: String) = {
-    val mongo = connection._mongoConnection
+    val mongo = connection._mongo
 
     // FIXME: implement
     NoVal
diff --git a/logic/src/main/scala/gr/grnet/aquarium/store/mongodb/confmodel/MongoDBCollectionModel.scala b/logic/src/main/scala/gr/grnet/aquarium/store/mongodb/confmodel/MongoDBCollectionModel.scala
new file mode 100644 (file)
index 0000000..6af2cc7
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.aquarium.store.mongodb
+package confmodel
+
+/**
+ * 
+ * @author Christos KK Loverdos <loverdos@gmail.com>.
+ */
+case class MongoDBCollectionModel(name: String,  mongoDBName: String, mongoCollectionName: String, writeConcern: String)
\ No newline at end of file
@@ -36,7 +36,6 @@
 package gr.grnet.aquarium.store.mongodb
 package confmodel
 
-import com.mongodb.ServerAddress
 
 ////////////////////////////////////////////////////////////////////////////
 // The WriteConcerns are as follows:
@@ -54,7 +53,6 @@ import com.mongodb.ServerAddress
  * 
  * @author Christos KK Loverdos <loverdos@gmail.com>.
  */
-case class MongoDBConfigurationModel(
+case class MongoDBConnectionModel(
     hosts: List[ServerAddressConfigurationModel],
-    slaveOK: Boolean,
-    writeConcern: String)
\ No newline at end of file
+    collections: List[MongoDBCollectionModel])
\ No newline at end of file
index 4fd1c34..59da36c 100644 (file)
@@ -68,7 +68,8 @@ object XStreamHelpers {
     prepareXStreamAlias[RabbitMQConsumerModel](xs)
 
     // MongoDB
-    prepareXStreamAlias[MongoDBConfigurationModel](xs)
+    prepareXStreamAlias[MongoDBConnectionModel](xs)
+    prepareXStreamAlias[MongoDBCollectionModel](xs)
     prepareXStreamAlias[ServerAddressConfigurationModel](xs)
 
     xs.alias("List", classOf[::[_]])
index a50a83a..5e46089 100644 (file)
@@ -1,10 +1,18 @@
-<MongoDBConfigurationModel>
-  <slaveOK>true</slaveOK>
-  <writeConcern>SAFE</writeConcern>
+<MongoDBConnectionModel>
   <hosts class="List">
     <ServerAddressConfigurationModel>
       <host>aquarium.dev.grnet.gr</host>
       <port>27017</port>
     </ServerAddressConfigurationModel>
   </hosts>
-</MongoDBConfigurationModel>
+
+  <collections class="List">
+    <MongoDBCollectionModel>
+      <name>events</name>
+
+      <mongoDBName>aquarium</mongoDBName>
+      <mongoCollectionName>events</mongoCollectionName>
+      <writeConcern>SAFE</writeConcern>
+    </MongoDBCollectionModel>
+  </collections>
+</MongoDBConnectionModel>
index c4875fd..3909908 100644 (file)
@@ -1,10 +1,18 @@
-<MongoDBConfigurationModel>
-  <slaveOK>true</slaveOK>
-  <writeConcern>SAFE</writeConcern>
+<MongoDBConnectionModel>
   <hosts class="List">
     <ServerAddressConfigurationModel>
       <host>localhost</host>
       <port>27017</port>
     </ServerAddressConfigurationModel>
   </hosts>
-</MongoDBConfigurationModel>
+
+  <collections class="List">
+    <MongoDBCollectionModel>
+      <name>events</name>
+
+      <mongoDBName>aquarium</mongoDBName>
+      <mongoCollectionName>events</mongoCollectionName>
+      <writeConcern>SAFE</writeConcern>
+    </MongoDBCollectionModel>
+  </collections>
+</MongoDBConnectionModel>
index c5bd57a..ac3c08f 100644 (file)
@@ -37,7 +37,7 @@ package gr.grnet.aquarium
 package store
 package mongodb
 
-import confmodel.{ServerAddressConfigurationModel, MongoDBConfigurationModel}
+import confmodel.{ServerAddressConfigurationModel, MongoDBConnectionModel}
 import org.junit.Test
 import org.junit.Assert._
 import org.junit.Assume.assumeTrue
@@ -45,9 +45,10 @@ import org.junit.Assume.assumeTrue
 import com.ckkloverdos.resource.DefaultResourceContext
 import MongoDBConnection.{RCFolders, PropFiles, DBNames, CollectionNames}
 import gr.grnet.aquarium.util.xstream.XStreamHelpers
-import com.mongodb.casbah.commons.MongoDBObject
 import com.ckkloverdos.sys.SysProp
 import util.Loggable
+import com.mongodb.{BasicDBObject, DBObject}
+import com.ckkloverdos.maybe.Failed
 
 /**
  * 
@@ -59,17 +60,17 @@ class MongoDBStoreTest extends Loggable {
   val xs = XStreamHelpers.newXStream
 
   lazy val MongoDBPropFile = {
-    val filename = SysProp(PropertyNames.MongoDBConfFile).value.getOr(PropFiles.local_message_store)
+    val filename = SysProp(PropertyNames.MongoDBConfFile).value.getOr(PropFiles.aquarium_message_store)
     logger.debug("Using mongodb configuration from %s".format(filename))
     filename
   }
 
-  private def _getTestConf: String = {
-    val address1 = ServerAddressConfigurationModel("aquarium.dev.grnet.gr", 27017)
-    val model = new MongoDBConfigurationModel(List(address1), true, "SAFE")
-    val xml = xs.toXML(model)
-    xml
-  }
+//  private def _getTestConf: String = {
+//    val address1 = ServerAddressConfigurationModel("aquarium.dev.grnet.gr", 27017)
+//    val model = new MongoDBConnectionModel(List(address1), true, )
+//    val xml = xs.toXML(model)
+//    xml
+//  }
 
   @Test
   def testConfigurationExists: Unit = {
@@ -80,21 +81,31 @@ class MongoDBStoreTest extends Loggable {
   def testConnection: Unit = {
     assumeTrue(LogicTestsAssumptions.EnableMongoDBTests)
 
+    assertTrue(mongodbRC.getLocalResource(MongoDBPropFile).isJust)
     for {
       confResource <- mongodbRC.getLocalResource(MongoDBPropFile)
     } {
-      val xs = XStreamHelpers.newXStream
-      logger.debug("Reading mongodb configuration from %s".format(confResource.url))
-      logger.debug("mongodb configuration is:\n%s".format(confResource.stringContent.getOr("")))
-      val maybeModel = XStreamHelpers.parseType[MongoDBConfigurationModel](confResource, xs)
+      val maybeModel = XStreamHelpers.parseType[MongoDBConnectionModel](confResource, xs)
+      maybeModel match {
+        case Failed(e, m) => throw e
+        case _ =>
+      }
       assertTrue(maybeModel.isJust)
-      val obj = MongoDBObject("1" -> "one", "2" -> "two")
-      logger.debug("Inserting %s into mongodb".format(obj))
+
       for(model <- maybeModel) {
+        logger.debug("Reading mongodb configuration from %s".format(confResource.url))
+        logger.debug("mongodb configuration is:\n%s".format(confResource.stringContent.getOr("")))
+        val obj = new BasicDBObject("1", 1)
+        logger.debug("Inserting %s into mongodb".format(obj))
         val mongo = new MongoDBConnection(model)
-        val db = mongo._mongoConnection(DBNames.test)
-        val collection = db.apply(CollectionNames.test)
-        collection.insert(obj)
+        val store: Option[MessageStore] = mongo.findCollection("events")
+
+        store match {
+          case Some(store) =>
+            store.storeString("{a: 1}")
+          case None =>
+            logger.warn("No store found")
+        }
       }
     }
   }