From: adam Date: Tue, 6 Nov 2012 15:41:22 +0000 (+0700) Subject: binding of ejdb index api X-Git-Tag: v1.2.12~642^2~11 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c5f844d2c83f8600c5422c413738b59ca18dbe48;p=platform%2Fupstream%2Fejdb.git binding of ejdb index api --- diff --git a/README.md b/README.md index f1b5749..b5736a9 100644 --- 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. +----------------------------------- ### 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. +----------------------------------- + + +### sync(cb) +Synchronize entire EJDB database with disk. + +__Arguments__ + + * {Function} cb Callback args: (error) + +----------------------------------- + + +### 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) + + ----------------------------------- + + +### 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) + +----------------------------------- + + +### 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) + +----------------------------------- + + +### 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) + +----------------------------------- + + +### 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 ================================== diff --git a/node/ejdb.js b/node/ejdb.js index 21389e5..da69e6b 100644 --- a/node/ejdb.js +++ b/node/ejdb.js @@ -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) diff --git a/node/ejdb_native.cc b/node/ejdb_native.cc index ceb57e6..e5cda4f 100644 --- a/node/ejdb_native.cc +++ b/node/ejdb_native.cc @@ -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 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 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 argv[1]; + if (task->cmd_ret != 0) { + argv[0] = Exception::Error(String::New(task->cmd_ret_msg.c_str())); + } else { + argv[0] = Local::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()); diff --git a/node/tests/t2.js b/node/tests/t2.js index b4f554f..d95b26d 100644 --- a/node/tests/t2.js +++ b/node/tests/t2.js @@ -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); diff --git a/tcejdb/ejdb.c b/tcejdb/ejdb.c index fee183d..75590bf 100644 --- a/tcejdb/ejdb.c +++ b/tcejdb/ejdb.c @@ -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)) { diff --git a/tcejdb/ejdb.h b/tcejdb/ejdb.h index 56d44d6..d076d9f 100644 --- a/tcejdb/ejdb.h +++ b/tcejdb/ejdb.h @@ -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);