Do some effectful composition instead of try/catch
authorChristos KK Loverdos <loverdos@gmail.com>
Tue, 17 Jan 2012 13:13:55 +0000 (15:13 +0200)
committerChristos KK Loverdos <loverdos@gmail.com>
Tue, 17 Jan 2012 13:13:55 +0000 (15:13 +0200)
src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBStore.scala

index b69d3dc..f3528b9 100644 (file)
@@ -294,6 +294,10 @@ class MongoDBStore(
 }
 
 object MongoDBStore {
+  object JsonNames {
+    final val _id = "_id"
+  }
+
   /**
    * Collection holding the [[gr.grnet.aquarium.logic.events.ResourceEvent]]s.
    *
@@ -397,25 +401,26 @@ object MongoDBStore {
                   collection: DBCollection,
                   idName: String,
                   idValueProvider: (A) => String,
-                  serializer: (A) => DBObject) : Maybe[RecordID] = Maybe {
-    // Store
-    val dbObj = serializer apply any
-    val writeResult = collection insert dbObj
-    writeResult.getLastError().throwOnError()
-
-    // Get back to retrieve unique id
-    val cursor = collection.find(new BasicDBObject(idName, idValueProvider(any)))
+                  serializer: (A) => DBObject) : Maybe[RecordID] = {
+    import com.ckkloverdos.maybe.effect
 
-    try {
-      // TODO: better way to get _id?
-      if(cursor.hasNext)
-        RecordID(cursor.next().get(ResourceJsonNames._id).toString)
-      else
-        throw new StoreException("Could not store %s to %s".format(any, collection))
-    } finally {
-      cursor.close()
+    Maybe {
+      val dbObj = serializer apply any
+      val writeResult = collection insert dbObj
+      writeResult.getLastError().throwOnError()
+
+      // Get back to retrieve unique id
+      val cursor = collection.find(new BasicDBObject(idName, idValueProvider(any)))
+      cursor
+    } flatMap { cursor ⇒
+      effect {
+        if(cursor.hasNext)
+          RecordID(cursor.next().get(JsonNames._id).toString)
+        else
+          throw new StoreException("Could not store %s to %s".format(any, collection))
+      } {} { cursor.close() }
     }
- }
+  }
 
   def jsonSupportToDBObject(any: JsonSupport): DBObject = {
     JSON.parse(any.toJson) match {