binding of ejdb index api
authoradam <anton@adamansky.com>
Tue, 6 Nov 2012 15:41:22 +0000 (22:41 +0700)
committeradam <anton@adamansky.com>
Tue, 6 Nov 2012 15:41:22 +0000 (22:41 +0700)
README.md
node/ejdb.js
node/ejdb_native.cc
node/tests/t2.js
tcejdb/ejdb.c
tcejdb/ejdb.h

index f1b5749..b5736a9 100644 (file)
--- a/README.md
+++ b/README.md
@@ -120,7 +120,7 @@ __Arguments__
  * {Number} `[openMode=JBOWRITER | JBOCREAT]` Bitmast of open modes:
        - `JBOREADER` Open as a reader.
        - `JBOWRITER` Open as a writer.
-       - `JBOCREAT` Create db if it not exists
+       - `JBOCREAT` Create if db file not exists
        - `JBOTRUNC` Truncate db.
 
 ---------------------------------------
@@ -171,7 +171,7 @@ __Arguments__
 
  * {String} cname Name of collection.
  * {Boolean} `[prune=false]` If true the collection data will erased from disk.
- * {Function} `[cb]` Callback function with arguments: (error)
+ * {Function} `[cb]` Callback args: (error)
 
 ---------------------------------------
 
@@ -194,7 +194,7 @@ __Arguments__
 
  * {String} **cname** Name of collection.
  * {Array|Object} jsarr Signle JSON object or array of JSON objects to save
- * {Function} `[cb]` Callback function with arguments: (error, {Array} of OIDs for saved objects)
+ * {Function} `[cb]` Callback args: (error, {Array} of OIDs for saved objects)
 
 
 --------------------------------------
@@ -208,7 +208,7 @@ __Arguments__
 
  * {String} cname Name of collection
  * {String} oid Object identifier (OID)
- * {Function} cb  Callback function with arguments: (error, obj)
+ * {Function} cb  Callback args: (error, obj)
         `obj`:  Retrieved JSON object or NULL if it is not found.
 
 --------------------------------------
@@ -222,7 +222,7 @@ __Arguments__
 
  * {String} cname Name of collection
  * {String} oid Object identifier (OID)
- * {Function} cb  Callback function with arguments: (error)
+ * {Function} cb  Callback args: (error)
 
 
 --------------------------------------
@@ -295,7 +295,7 @@ EJDB queries inspired by MongoDB (mongodb.org) and follows same philosophy.
  * {Object} qobj Main JSON query object
  * {Array} `[orarr]` Array of additional OR query objects (joined with OR predicate).
  * {Object} `[hints]` JSON object with query hints.
- * {Function} cb Callback function with arguments: (error, cursor, count)
+ * {Function} cb Callback args: (error, cursor, count)
             `cursor`: Cursor object to traverse records
             `count`:  Total number of selected records
 
@@ -318,9 +318,10 @@ __Arguments__
  * {Object} qobj Main JSON query object
  * {Array} `[orarr]` Array of additional OR query objects (joined with OR predicate).
  * {Object} `[hints]` JSON object with query hints.
- * {Function} cb Callback function with arguments: (error, obj)
+ * {Function} cb Callback args: (error, obj)
              `obj`  Retrieved JSON object or NULL if it is not found.
 
+-----------------------------------
 
 <a name="count"/>
 ### count(cname, qobj, orarr, hints, cb)
@@ -339,9 +340,91 @@ __Arguments__
  * {Object} qobj Main JSON query object
  * {Array} `[orarr]` Array of additional OR query objects (joined with OR predicate).
  * {Object} `[hints]` JSON object with query hints.
- * {Function} cbcb Callback function with arguments: (error, count)
+ * {Function} cb Callback args: (error, count)
               `count`:  Number of matching records.
 
+-----------------------------------
+
+<a name="sync"/>
+### sync(cb)
+Synchronize entire EJDB database with disk.
+
+__Arguments__
+
+ * {Function} cb Callback args: (error)
+
+-----------------------------------
+
+<a name="dropIndexes"/>
+### dropIndexes(cname, path, cb)
+Drop indexes of all types for JSON field path.
+
+__Arguments__
+
+ * {String} cname Name of collection
+ * {String} path  JSON field path
+ * {Function} `[cb]` Optional callback function. Callback args: (error)
+
+ -----------------------------------
+
+ <a name="optimizeIndexes"/>
+### optimizeIndexes(cname, path, cb)
+Optimize indexes of all types for JSON field path.
+Performs B+ tree index file optimization.
+
+ __Arguments__
+
+  * {String} cname Name of collection
+  * {String} path  JSON field path
+  * {Function} `[cb]` Optional callback function. Callback args: (error)
+
+-----------------------------------
+
+<a name="ensureIndex"/>
+### ensureStringIndex(cname, path, cb)
+### ensureNumberIndex(cname, path, cb)
+### ensureArrayIndex(cname, path, cb)
+
+Ensure index presence of String|Number|Array type for JSON field path
+
+ __Arguments__
+
+  * {String} cname Name of collection
+  * {String} path  JSON field path
+  * {Function} `[cb]` Optional callback function. Callback args: (error)
+
+-----------------------------------
+
+<a name="rebuildIndex"/>
+### rebuildStringIndex(cname, path, cb)
+### rebuildNumberIndex(cname, path, cb)
+### rebuildArrayIndex(cname, path, cb)
+
+Rebuild index of String|Number|Array type for JSON field path
+
+ __Arguments__
+
+  * {String} cname Name of collection
+  * {String} path  JSON field path
+  * {Function} `[cb]` Optional callback function. Callback args: (error)
+
+-----------------------------------
+
+<a name="dropIndex"/>
+### dropStringIndex(cname, path, cb)
+### dropNumberIndex(cname, path, cb)
+### dropArrayIndex(cname, path, cb)
+
+Drop index of String|Number|Array type for JSON field path
+
+ __Arguments__
+
+  * {String} cname Name of collection
+  * {String} path  JSON field path
+  * {Function} `[cb]` Optional callback function. Callback args: (error)
+
+-----------------------------------
+
 
 EJDB C Library
 ==================================
index 21389e5..da69e6b 100644 (file)
@@ -393,6 +393,16 @@ const simpleErrCb = function(err) {
 };
 
 /**
+ * Synchronize entire EJDB database and
+ * all its collections with storage.
+ * @param {Function} [cb] Optional callback function. Callback args: (error)
+ */
+EJDB.prototype.sync = function(cb) {
+    if (typeof cb !== "function") cb = simpleErrCb;
+    return this._impl.sync(cb);
+};
+
+/**
  * DROP indexes of all types for JSON field path
  * @param {String} cname Name of collection
  * @param {String} path  JSON field path
@@ -405,7 +415,7 @@ EJDB.prototype.dropIndexes = function(cname, path, cb) {
 
 /**
  * OPTIMIZE indexes of all types for JSON field path.
- *  B+ tree index file optimization.
+ *  Performs B+ tree index file optimization.
  * @param {String} cname Name of collection
  * @param {String} path  JSON field path
  * @param {Function} [cb] Optional callback function. Callback args: (error)
index ceb57e6..e5cda4f 100644 (file)
@@ -429,7 +429,8 @@ namespace ejdb {
             cmdRemove = 3, //Remove BSON by oid
             cmdQuery = 4, //Query collection
             cmdRemoveColl = 5, //Remove collection
-            cmdSetIndex = 6 //Set index
+            cmdSetIndex = 6, //Set index
+            cmdSync = 7 //Sync database
         };
 
         struct BSONCmdData { //Any bson related cmd data
@@ -679,6 +680,15 @@ namespace ejdb {
             return scope.Close(args.This());
         }
 
+        static Handle<Value> s_sync(const Arguments& args) {
+            HandleScope scope;
+            REQ_FUN_ARG(0, cb);
+            NodeEJDB *njb = ObjectWrap::Unwrap< NodeEJDB > (args.This());
+            EJBTask *task = new EJBTask(cb, njb, cmdSync, NULL, NULL);
+            uv_queue_work(uv_default_loop(), &task->uv_work, s_exec_cmd_eio, s_exec_cmd_eio_after);
+            return scope.Close(args.This());
+        }
+
         static Handle<Value> s_ecode(const Arguments& args) {
             HandleScope scope;
             NodeEJDB *njb = ObjectWrap::Unwrap< NodeEJDB > (args.This());
@@ -756,6 +766,11 @@ namespace ejdb {
                 case cmdSetIndex:
                     set_index((SetIndexCmdTask*) task);
                     break;
+                case cmdSync:
+                    sync(task);
+                    break;
+                default:
+                    assert(0);
             }
         }
 
@@ -780,6 +795,36 @@ namespace ejdb {
                 case cmdSetIndex:
                     set_index_after((SetIndexCmdTask*) task);
                     break;
+                case cmdSync:
+                    sync_after(task);
+                    break;
+                default:
+                    assert(0);
+            }
+        }
+
+        void sync(EJBTask *task) {
+            if (!_check_state((EJBTask*) task)) {
+                return;
+            }
+            if (!ejdbsyncdb(m_jb)) {
+                task->cmd_ret = CMD_RET_ERROR;
+                task->cmd_ret_msg = _jb_error_msg();
+            }
+        }
+
+        void sync_after(EJBTask *task) {
+            HandleScope scope;
+            Local<Value> argv[1];
+            if (task->cmd_ret != 0) {
+                argv[0] = Exception::Error(String::New(task->cmd_ret_msg.c_str()));
+            } else {
+                argv[0] = Local<Primitive>::New(Null());
+            }
+            TryCatch try_catch;
+            task->cb->Call(Context::GetCurrent()->Global(), 1, argv);
+            if (try_catch.HasCaught()) {
+                FatalException(try_catch);
             }
         }
 
@@ -1112,6 +1157,7 @@ finish:
             NODE_SET_PROTOTYPE_METHOD(constructor_template, "removeCollection", s_rm_collection);
             NODE_SET_PROTOTYPE_METHOD(constructor_template, "isOpen", s_is_open);
             NODE_SET_PROTOTYPE_METHOD(constructor_template, "setIndex", s_set_index);
+            NODE_SET_PROTOTYPE_METHOD(constructor_template, "sync", s_sync);
 
             //Symbols
             target->Set(String::NewSymbol("NodeEJDB"), constructor_template->GetFunction());
index b4f554f..d95b26d 100644 (file)
@@ -257,6 +257,16 @@ module.exports.testRemove = function(test) {
     });
 };
 
+
+module.exports.testSync = function(test) {
+    test.ok(jb);
+    test.ok(jb.isOpen());
+    jb.sync(function(err) {
+        test.ifError(err);
+        test.done();
+    });
+};
+
 module.exports.testRemoveColls = function(test) {
     jb.removeCollection("birds", function(err) {
         test.ifError(err);
index fee183d..75590bf 100644 (file)
@@ -589,6 +589,24 @@ EJDB_EXPORT bool ejdbsyncoll(EJCOLL *jcoll) {
     return rv;
 }
 
+EJDB_EXPORT bool ejdbsyncdb(EJDB *jb) {
+    assert(jb);
+    JBENSUREOPENLOCK(jb, true, false);
+    bool rv = true;
+    EJCOLL *coll = NULL;
+    for (int i = 0; i < jb->cdbsnum; ++i) {
+        coll = jb->cdbs + i;
+        assert(coll);
+        rv = JBCLOCKMETHOD(coll, true);
+        if (!rv) break;
+        rv = tctdbsync(coll->tdb);
+        JBCUNLOCKMETHOD(coll);
+        if (!rv) break;
+    }
+    JBUNLOCKMETHOD(jb);
+    return rv;
+}
+
 EJDB_EXPORT bool ejdbtranbegin(EJCOLL *jcoll) {
     assert(jcoll);
     if (!JBISOPEN(jcoll->jb)) {
index 56d44d6..d076d9f 100644 (file)
@@ -49,7 +49,7 @@ enum { /** Database open modes */
     JBOREADER = 1 << 0, /**< Open as a reader. */
     JBOWRITER = 1 << 1, /**< Open as a writer. */
     JBOCREAT = 1 << 2, /**< Create if db file not exists. */
-    JBOTRUNC = 1 << 3, /**< Truncate db. */
+    JBOTRUNC = 1 << 3, /**< Truncate db on open. */
     JBONOLCK = 1 << 4, /**< Open without locking. */
     JBOLCKNB = 1 << 5, /**< Lock without blocking. */
     JBOTSYNC = 1 << 6 /**< Synchronize every transaction. */
@@ -308,6 +308,13 @@ EJDB_EXPORT TCLIST* ejdbqrysearch(EJCOLL *jcoll, const EJQ *q, uint32_t *count,
  */
 EJDB_EXPORT bool ejdbsyncoll(EJCOLL *jcoll);
 
+/**
+ * Synchronize entire EJDB database and
+ * all its collections with storage.
+ * @param jb Database hand
+ */
+EJDB_EXPORT bool ejdbsyncdb(EJDB *jb);
+
 /** Begin transaction for EJDB collection. */
 EJDB_EXPORT bool ejdbtranbegin(EJCOLL *coll);