# Since the MongoDB notation is not JSON complient, the mongoc wrapper uses
# a JSON based notation like `{"$oid": "hash"}`.
# This is the notation returned by the `to_json` service.
-private class MongoObjectId
+class MongoObjectId
- var native: BSONObjectId
+ private var native: BSONObjectId = new BSONObjectId
+
+ private init with_native(native: BSONObjectId) do
+ self.native = native
+ end
# The unique ID as an MongoDB Object ID string.
fun id: String do return native.id
private fun last_id: nullable MongoObjectId do
var last_id = sys.last_mongoc_id
if last_id == null then return null
- return new MongoObjectId(last_id)
+ return new MongoObjectId.with_native(last_id)
end
# Set the last generated id or `null` to unset once used.
# * 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