Merge: Faster lookup
[nit.git] / lib / mongodb / native_mongodb.nit
index ddcf248..ba9b5e3 100644 (file)
@@ -114,12 +114,23 @@ end
 # * a 2-byte process id (Big Endian), and
 # * a 3-byte counter (Big Endian), starting with a random value.
 extern class BSONObjectId `{ bson_oid_t * `}
+
+       # Generates a new `bson_oid_t`.
+       new `{
+               bson_oid_t *self = malloc(sizeof(bson_oid_t));
+               bson_oid_init(self, NULL);
+               return self;
+       `}
+
        # Object id.
        fun id: String import NativeString.to_s_with_copy `{
                char str[25];
                bson_oid_to_string(self, str);
                return NativeString_to_s_with_copy(str);
        `}
+
+       # Destroy `self`.
+       fun destroy `{ free(self); `}
 end
 
 redef class Sys
@@ -433,6 +444,24 @@ extern class NativeMongoCollection `{ mongoc_collection_t * `}
                return NativeMongoCursor_as_nullable(cursor);
        `}
 
+       # Wrapper for `mongoc_collection_aggregate()`.
+       #
+       # This function shall execute an aggregation `pipeline` on the underlying collection.
+       #
+       # The `pipeline` parameter should contain a field named `pipeline` containing
+       # a BSON array of pipeline stages.
+       fun aggregate(pipeline: NativeBSON): nullable NativeMongoCursor import
+               NativeMongoCursor.as nullable, set_mongoc_error `{
+               bson_error_t error;
+               mongoc_cursor_t *cursor;
+               cursor = mongoc_collection_aggregate(self, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
+               if (mongoc_cursor_error(cursor, &error)) {
+                       NativeMongoCollection_set_mongoc_error(self, &error);
+                       return null_NativeMongoCursor();
+               }
+               return NativeMongoCursor_as_nullable(cursor);
+       `}
+
        # Wrapper for `mongoc_collection_stats()`.
        #
        # This function is a helper to retrieve statistics about the collection.
@@ -506,7 +535,12 @@ extern class NativeMongoCursor `{ mongoc_cursor_t* `}
        # Wrapper for `mongoc_cursor_current()`.
        #
        # Fetches the cursors current document or NULL if there has been an error.
-       fun current: NativeBSON `{ return (bson_t*) mongoc_cursor_current(self); `}
+       fun current: NativeBSON `{
+               // As said in documentation, BSON objects should not be freed manually.
+               bson_t* bson = (bson_t*) mongoc_cursor_current(self);
+               // Copy BSON so we can let the GC free it automatically.
+               return bson_copy(bson);
+       `}
 
        # Wrapper for `mongoc_cursor_next()`.
        #
@@ -519,11 +553,6 @@ extern class NativeMongoCursor `{ mongoc_cursor_t* `}
                return mongoc_cursor_next(self, &doc);
        `}
 
-       # Wrapper for `mongoc_cursor_more()`.
-       #
-       # This function shall indicate if there is more data to be read from the cursor.
-       fun more: Bool `{ return mongoc_cursor_more(self); `}
-
        # Wrapper for `mongoc_cursor_destroy()`.
        #
        # This instance should not be used beyond this point!