Merge: lib/mongodb: minor fixes
authorJean Privat <jean@pryen.org>
Fri, 16 Oct 2015 20:16:05 +0000 (16:16 -0400)
committerJean Privat <jean@pryen.org>
Fri, 16 Oct 2015 20:16:05 +0000 (16:16 -0400)
This PR apply some fixes to the MongoDB library:
* avoids crash when `Collection::find` returns only one element
* fixes the SegFault workaround in `Collection::find`

Pull-Request: #1759
Reviewed-by: Jean Privat <jean@pryen.org>

lib/mongodb/mongodb.nit

index 1e9b9bd..624dc8a 100644 (file)
@@ -600,14 +600,12 @@ class MongoCollection
        # ~~~
        fun find(query: JsonObject): nullable JsonObject do
                assert is_alive
-               var c = native.find(query.to_bson.native)
-               assert is_alive # FIXME used to avoid segfault (so `self` isn't garbage collected to soon)
+               var q = new NativeBSON.from_json_string(query.to_json.to_cstring)
+               var c = native.find(q)
+               q.destroy
                if c == null then return null
                var cursor = new MongoCursor(c)
-               if cursor.is_ok then
-                       cursor.next
-                       return cursor.item
-               end
+               if cursor.is_ok then return cursor.item
                return null
        end