#83 - command method + memory free improvement
authoryudanov <fedwiz@academ.org>
Fri, 19 Jul 2013 08:57:29 +0000 (15:57 +0700)
committeryudanov <fedwiz@academ.org>
Fri, 19 Jul 2013 08:57:29 +0000 (15:57 +0700)
rbejdb/src/rbbson.c
rbejdb/src/rbbson.h
rbejdb/src/rbejdb.c
rbejdb/test/t2.rb

index 4f7bf6a..65fe8e6 100644 (file)
@@ -403,19 +403,19 @@ VALUE bson_to_ruby_wrapper(VALUE bsonWrap) {
     return bson_to_ruby(rbbson->bsonval);
 }
 
-VALUE bson_to_ruby_ensure_destroy_wrapper(VALUE bsonWrap, VALUE exception) {
+VALUE bson_to_ruby_ensure_del_wrapper(VALUE bsonWrap, VALUE exception) {
     RBBSON* rbbson;
     Data_Get_Struct(bsonWrap, RBBSON, rbbson);
     if (rbbson->bsonval) {
-        bson_destroy(rbbson->bsonval);
+        bson_del(rbbson->bsonval);
     }
     rbbson->bsonval = NULL;
     return Qnil;
 }
 
-VALUE bson_to_ruby_ensure_destroy(bson* bsonval) {
+VALUE bson_to_ruby_ensure_del(bson* bsonval) {
     VALUE bsonWrap = createBsonWrap(bsonval);
-    return rb_ensure(bson_to_ruby_wrapper, bsonWrap, bson_to_ruby_ensure_destroy_wrapper, bsonWrap);
+    return rb_ensure(bson_to_ruby_wrapper, bsonWrap, bson_to_ruby_ensure_del_wrapper, bsonWrap);
 }
 
 VALUE bson_oid_to_ruby(const bson_oid_t* oid) {
index e7af5d9..5820ef5 100644 (file)
@@ -23,7 +23,7 @@ extern "C" {
 
     VALUE bson_to_ruby(const bson* bsonval);
 
-    VALUE bson_to_ruby_ensure_destroy(bson* bsonval);
+    VALUE bson_to_ruby_ensure_del(bson* bsonval);
 
 
     VALUE bson_oid_to_ruby(const bson_oid_t* oid);
index b5b292f..d0be216 100644 (file)
@@ -317,11 +317,11 @@ VALUE EJDB_save(int argc, VALUE *argv, VALUE self) {
 
         bson_oid_t oid;
         if (!ejdbsavebson2(coll, bsonval, &oid, merge)) {
-            bson_destroy(bsonval);
+            bson_del(bsonval);
             raise_ejdb_error(ejdb);
         }
 
-        bson_destroy(bsonval);
+        bson_del(bsonval);
 
         VALUE roid = bson_oid_to_ruby(&oid);
         rb_ary_push(oids, roid);
@@ -370,7 +370,7 @@ VALUE EJDB_load(VALUE self, VALUE collName, VALUE rboid) {
     bson_oid_t oid = ruby_to_bson_oid(rboid);
     bson *bs = ejdbloadbson(coll, &oid);
 
-    return bs ? bson_to_ruby_ensure_destroy(bs) : nil_or_raise_ejdb_error(ejdb);
+    return bs ? bson_to_ruby_ensure_del(bs) : nil_or_raise_ejdb_error(ejdb);
 }
 
 /*
@@ -419,11 +419,11 @@ VALUE prepare_query_hints(VALUE hints) {
 
 void EJDB_remove_query_internal(RBEJDB_QUERY* rbquery) {
     if (rbquery->qbson) {
-        bson_destroy(rbquery->qbson);
+        bson_del(rbquery->qbson);
         rbquery->qbson = NULL;
     }
     if (rbquery->hintsbson) {
-        bson_destroy(rbquery->hintsbson);
+        bson_del(rbquery->hintsbson);
         rbquery->hintsbson = NULL;
     }
     if (rbquery->orarrbson) {
@@ -459,7 +459,7 @@ VALUE EJDB_find_internal(VALUE self, VALUE collName, VALUE queryWrap, VALUE q, V
         bson* orqbson;
         ruby_to_bson(orq, &orqbson, RUBY_TO_BSON_AS_QUERY);
         bson_copy(rbquery->orarrbson + (i++), orqbson);
-        bson_destroy(orqbson);
+        bson_del(orqbson);
         rbquery->orarrlng++;
     }
 
@@ -722,6 +722,63 @@ VALUE EJDB_update(int argc, VALUE* argv, VALUE self) {
     return EJDB_find(4, findargs, self);
 }
 
+/**
+ * call-seq:
+ *   ejdb.command(cmd) -> Hash
+ *
+ * Execute the ejdb database command.
+ *
+ * Supported commands:
+ *
+ *  1) Exports database collections data. See ejdbexport() method.
+ *
+ *    :export => {
+ *          :path => string,                    //Exports database collections data
+ *          :cnames => [string array]|nil,      //List of collection names to export
+ *          :mode => int|nil                    //Values: null|`JBJSONEXPORT` See ejdbexport() method
+ *    }
+ *
+ *    Command response:
+ *       {
+ *          "log" => string,        //Diagnostic log about executing this command
+ *          "error" => string|nil, //ejdb error message
+ *          "errorCode" => int|0,   //ejdb error code
+ *       }
+ *
+ *  2) Imports previously exported collections data into ejdb.
+ *
+ *    :import => {
+ *          :path => string                     //The directory path in which data resides
+ *          :cnames => [string array]|nil,      //List of collection names to import
+ *          :mode => int|nil                    //Values: null|`JBIMPORTUPDATE`|`JBIMPORTREPLACE` See ejdbimport() method
+ *     }
+ *
+ *     Command response:
+ *       {
+ *          "log" => string,        //Diagnostic log about executing this command
+ *          "error" => string|nil,  //ejdb error message
+ *          "errorCode" => int|0,    //ejdb error code
+ *       }
+ *
+ * - +cmd+ (Hash or Object) - command spec.
+ *
+ * Returns allocated command response object as hash.
+ */
+VALUE EJDB_command(VALUE self, VALUE command) {
+    EJDB* ejdb = getEJDB(self);
+
+    bson* bsoncmd;
+    ruby_to_bson(command, &bsoncmd, 0);
+
+    bson* res = ejdbcommand(ejdb, bsoncmd);
+    bson_del(bsoncmd);
+
+    if (!res) {
+        raise_ejdb_error(ejdb);
+    }
+
+    return bson_to_ruby_ensure_del(res);
+}
 
 
 VALUE EJDB_set_index_internal(VALUE self, VALUE collName, VALUE fpath, int flags) {
@@ -1320,6 +1377,8 @@ void Init_rbejdb() {
     rb_define_method(ejdbClass, "update", RUBY_METHOD_FUNC(EJDB_update), -1);
     rb_define_method(ejdbClass, "remove", RUBY_METHOD_FUNC(EJDB_remove), 2);
 
+    rb_define_method(ejdbClass, "command", RUBY_METHOD_FUNC(EJDB_command), 1);
+
     rb_define_method(ejdbClass, "drop_collection", RUBY_METHOD_FUNC(EJDB_drop_collection), -1);
     rb_define_method(ejdbClass, "ensure_collection", RUBY_METHOD_FUNC(EJDB_ensure_collection), -1);
 
index 75f6fd9..71e3f50 100644 (file)
@@ -458,7 +458,20 @@ class EJDBTestUnit < Test::Unit::TestCase
   end
 
 
-  def test_ejdbh_different_hacks
+  def test_ejdbh_command
+    assert_not_nil $jb
+    assert $jb.open?
+
+    cmdret = $jb.command(:ping => {})
+
+    assert_not_nil cmdret
+    assert_equal("pong", cmdret["log"])
+
+    puts __method__.inspect + " has passed successfull"
+  end
+
+
+  def test_ejdbi_different_hacks
     assert_nil $jb.load("monsters", "111")
     assert_nil $jb.load("parrots", "111")
     assert_nil $jb.load("parrots", "")
@@ -612,7 +625,8 @@ class EJDBTestUnit < Test::Unit::TestCase
     puts __method__.inspect + " has passed successfull"
   end
 
-  def test_ejdbi_internal_rbejdb_classes
+
+  def test_ejdbj_internal_rbejdb_classes
     assert_raise(RuntimeError) {
       EJDB.new
     }
@@ -622,7 +636,8 @@ class EJDBTestUnit < Test::Unit::TestCase
     puts __method__.inspect + " has passed successfull"
   end
 
-  def test_ejdbj_close
+
+  def test_ejdbk_close
     assert_not_nil $jb
     assert $jb.open?