From 27ffe7fabbc761dd6828d409d0e1511432cc1b60 Mon Sep 17 00:00:00 2001 From: Kyaw Tun Date: Sun, 28 Jun 2015 06:44:09 +0800 Subject: [PATCH] split keysOf valuesOf countOf keysByIndex valuesByIndex --- src/ydn/db/core/exports.js | 28 ++--- src/ydn/db/core/i_operator.js | 27 +++-- src/ydn/db/core/operator.js | 198 +++++----------------------------- src/ydn/db/core/storage.js | 43 ++++---- src/ydn/db/crud/exports.js | 12 ++- src/ydn/db/crud/i_operator.js | 42 ++++++++ src/ydn/db/crud/operator.js | 71 +++++++++++- src/ydn/db/crud/storage.js | 37 +++++-- test/qunit/crud.js | 2 +- test/qunit/ver_1_iterator.js | 82 +++++++------- 10 files changed, 269 insertions(+), 273 deletions(-) diff --git a/src/ydn/db/core/exports.js b/src/ydn/db/core/exports.js index e2a207af..67735bce 100755 --- a/src/ydn/db/core/exports.js +++ b/src/ydn/db/core/exports.js @@ -12,23 +12,27 @@ goog.require('ydn.db.core.Storage'); -goog.exportProperty(ydn.db.core.Storage.prototype, 'scan', - ydn.db.core.Storage.prototype.scan); -goog.exportProperty(ydn.db.core.Storage.prototype, 'map', - ydn.db.core.Storage.prototype.map); -goog.exportProperty(ydn.db.core.Storage.prototype, 'reduce', - ydn.db.core.Storage.prototype.reduce); +goog.exportProperty(ydn.db.core.Storage.prototype, 'countOf', + ydn.db.core.Storage.prototype.countOf); +goog.exportProperty(ydn.db.core.Storage.prototype, 'keysOf', + ydn.db.core.Storage.prototype.keysOf); goog.exportProperty(ydn.db.core.Storage.prototype, 'open', ydn.db.core.Storage.prototype.open); +goog.exportProperty(ydn.db.core.Storage.prototype, 'scan', + ydn.db.core.Storage.prototype.scan); +goog.exportProperty(ydn.db.core.Storage.prototype, 'valuesOf', + ydn.db.core.Storage.prototype.valuesOf); -goog.exportProperty(ydn.db.core.DbOperator.prototype, 'scan', - ydn.db.core.DbOperator.prototype.scan); -goog.exportProperty(ydn.db.core.DbOperator.prototype, 'map', - ydn.db.core.DbOperator.prototype.map); -goog.exportProperty(ydn.db.core.DbOperator.prototype, 'reduce', - ydn.db.core.DbOperator.prototype.reduce); +goog.exportProperty(ydn.db.core.DbOperator.prototype, 'countOf', + ydn.db.core.DbOperator.prototype.countOf); +goog.exportProperty(ydn.db.core.DbOperator.prototype, 'keysOf', + ydn.db.core.DbOperator.prototype.keysOf); goog.exportProperty(ydn.db.core.DbOperator.prototype, 'open', ydn.db.core.DbOperator.prototype.open); +goog.exportProperty(ydn.db.core.DbOperator.prototype, 'scan', + ydn.db.core.DbOperator.prototype.scan); +goog.exportProperty(ydn.db.core.DbOperator.prototype, 'valuesOf', + ydn.db.core.DbOperator.prototype.valuesOf); goog.exportProperty(ydn.db.core.req.AbstractCursor.prototype, 'getKey', ydn.db.core.req.AbstractCursor.prototype.getKey); diff --git a/src/ydn/db/core/i_operator.js b/src/ydn/db/core/i_operator.js index e0348f64..fda4f988 100755 --- a/src/ydn/db/core/i_operator.js +++ b/src/ydn/db/core/i_operator.js @@ -17,22 +17,29 @@ ydn.db.core.IOperator = function() {}; /** - * Map operation. + * Iterate keys by iterator. * @param {!ydn.db.Iterator} iterator - * @param {?function(*): (*|undefined)} callback - * @return {!goog.async.Deferred} deferred. + * @return {!ydn.db.Request} result promise. */ -ydn.db.core.IOperator.prototype.map = goog.abstractMethod; +ydn.db.core.IOperator.prototype.countOf = goog.abstractMethod; /** - * Reduce operation. - * @param {!ydn.db.Iterator} iterator iterator. - * @param {function(*, *, number): *} callback callback. - * @param {*=} opt_initial initial value. - * @return {!goog.async.Deferred} deferred. + * Iterate keys by iterator. + * @param {!ydn.db.Iterator} iterator + * @param {number=} arg2 limit. + * @return {!ydn.db.Request} result promise. + */ +ydn.db.core.IOperator.prototype.keysOf = goog.abstractMethod; + + +/** + * Iterate values by iterator. + * @param {!ydn.db.Iterator} iterator iterator + * @param {number=} arg2 limit. + * @return {!ydn.db.Request} result promise. */ -ydn.db.core.IOperator.prototype.reduce = goog.abstractMethod; +ydn.db.core.IOperator.prototype.valuesOf = goog.abstractMethod; diff --git a/src/ydn/db/core/operator.js b/src/ydn/db/core/operator.js index bdee2da6..09771719 100755 --- a/src/ydn/db/core/operator.js +++ b/src/ydn/db/core/operator.js @@ -105,9 +105,8 @@ ydn.db.core.DbOperator.prototype.get = function(arg1, arg2) { /** * @param {ydn.db.Iterator} q the iterator. * @param {number=} arg2 limit. - * @param {number=} arg3 offset. */ -ydn.db.core.DbOperator.prototype.keysOf = function (q, arg2, arg3) { +ydn.db.core.DbOperator.prototype.keysOf = function (q, arg2) { /** * @type {number} @@ -123,13 +122,8 @@ ydn.db.core.DbOperator.prototype.keysOf = function (q, arg2, arg3) { throw new ydn.debug.error.ArgumentException('limit must be a number, ' + ' but ' + arg2); } - if (goog.isDef(arg3)) { - throw new ydn.debug.error.ArgumentException( - 'offset must not be specified'); - } - - goog.log.finer(this.logger, 'keysByIterator:' + q); + goog.log.finer(this.logger, 'keysOf:' + q); var df = this.tx_thread.request(ydn.db.Request.Method.KEYS_ITER, [q.getStoreName()]); df.addTxback(function () { @@ -150,37 +144,45 @@ ydn.db.core.DbOperator.prototype.keysOf = function (q, arg2, arg3) { ydn.db.core.DbOperator.prototype.keys = function(arg1, arg2, arg3, arg4, arg5, arg6, arg7) { if (arg1 instanceof ydn.db.Iterator) { - return this.keysOf(arg1, /** @type {number} */(arg2), /** @type {number} */(arg3)); + return this.keysOf(arg1, /** @type {number} */(arg2)); } else { return goog.base(this, 'keys', arg1, arg2, arg3, arg4, arg5, arg6, arg7); } }; +/** + * @inheritDoc + */ +ydn.db.core.DbOperator.prototype.countOf = function(arg1) { + + /** + * + * @type {!ydn.db.Iterator} + */ + var q = arg1; + goog.log.finer(this.logger, 'countIterator:' + q); + var df = this.tx_thread.request(ydn.db.Request.Method.COUNT, + [q.getStoreName()]); + df.addTxback(function () { + this.iterate(ydn.db.base.QueryMethod.COUNT, df, q); + }, this); + + return df; + + +}; + /** * @inheritDoc */ ydn.db.core.DbOperator.prototype.count = function(arg1, arg2, arg3, arg4) { - var me = this; if (arg1 instanceof ydn.db.Iterator) { if (goog.isDef(arg2) || goog.isDef(arg3)) { throw new ydn.debug.error.ArgumentException('too many arguments.'); } - - /** - * - * @type {!ydn.db.Iterator} - */ - var q = arg1; - goog.log.finer(this.logger, 'countIterator:' + q); - var df = this.tx_thread.request(ydn.db.Request.Method.COUNT, - [q.getStoreName()]); - df.addTxback(function() { - this.iterate(ydn.db.base.QueryMethod.COUNT, df, q); - }, this); - - return df; + return this.countOf(arg1); } else { return goog.base(this, 'count', arg1, arg2, arg3, arg4); } @@ -191,10 +193,9 @@ ydn.db.core.DbOperator.prototype.count = function(arg1, arg2, arg3, arg4) { /** * @param {!ydn.db.Iterator} q the iterator. * @param {number=} arg2 limit. - * @param {number=} arg3 offset. * @return {!ydn.db.Request} */ -ydn.db.core.DbOperator.prototype.listValues = function (q, arg2, arg3) { +ydn.db.core.DbOperator.prototype.valuesOf = function (q, arg2) { var me = this; @@ -213,10 +214,6 @@ ydn.db.core.DbOperator.prototype.listValues = function (q, arg2, arg3) { throw new ydn.debug.error.ArgumentException('limit must be a number, ' + 'but ' + arg2); } - if (goog.isDef(arg3)) { - throw new ydn.debug.error.ArgumentException( - 'offset must not be specified'); - } goog.log.finer(this.logger, 'listByIterator:' + q); var df = this.tx_thread.request(ydn.db.Request.Method.VALUES_ITER, @@ -241,7 +238,7 @@ ydn.db.core.DbOperator.prototype.values = function(arg1, arg2, arg3, arg4, var me = this; if (arg1 instanceof ydn.db.Iterator) { - return this.listValues(arg1, /** @type {number} */(arg2), /** @type {number} */(arg3)); + return this.valuesOf(arg1, /** @type {number} */(arg2)); } else { return goog.base(this, 'values', arg1, arg2, arg3, arg4, arg5, arg6); } @@ -673,145 +670,6 @@ ydn.db.core.DbOperator.prototype.open = function(callback, iter, opt_mode, }; -/** - * @inheritDoc - */ -ydn.db.core.DbOperator.prototype.map = function(iterator, callback) { - - var me = this; - var stores = iterator.stores(); - for (var store, i = 0; store = stores[i]; i++) { - if (!store) { - throw new ydn.debug.error.ArgumentException('Store "' + store + - '" not found.'); - } - } - var df = this.tx_thread.request(ydn.db.Request.Method.MAP, stores); - goog.log.finest(this.logger, 'map:' + iterator); - this.tx_thread.exec(df, function(tx, tx_no, cb) { - - var lbl = tx_no + ' iterating ' + iterator; - goog.log.finest(me.logger, lbl); - - var names = iterator.stores(); - var crs = []; - for (var ni = 0; ni < names.length; ni++) { - crs[ni] = me.getIndexExecutor().getCursor(tx, tx_no, names[ni]); - } - var cursor = iterator.load(crs); - - cursor.onFail = function(e) { - cb(e, false); - }; - /** - * - * @param {IDBKey=} opt_key effective key. - */ - cursor.onNext = function(opt_key) { - if (goog.isDefAndNotNull(opt_key)) { - var key = opt_key; - var ref; - if (iterator.isIndexIterator()) { - if (iterator.isKeyIterator()) { - ref = key; - } else { - ref = cursor.getPrimaryKey(); - } - } else { - if (iterator.isKeyIterator()) { - ref = key; - } else { - ref = cursor.getValue(); - } - } - callback(ref); - //console.log(['onNext', key, primaryKey, value, ref, adv]); - cursor.advance(1); - - } else { - cb(undefined); - callback = null; - } - }; - - }, stores, ydn.db.base.TransactionMode.READ_ONLY); - - return df; -}; - - -/** - * @inheritDoc - */ -ydn.db.core.DbOperator.prototype.reduce = function(iterator, callback, - opt_initial) { - - var me = this; - var stores = iterator.stores(); - for (var store, i = 0; store = stores[i]; i++) { - if (!store) { - throw new ydn.debug.error.ArgumentException('Store "' + store + - '" not found.'); - } - } - var df = this.tx_thread.request(ydn.db.Request.Method.REDUCE, stores); - - var previous = goog.isObject(opt_initial) ? - ydn.object.clone(opt_initial) : opt_initial; - goog.log.finer(this.logger, 'reduce:' + iterator); - this.tx_thread.exec(df, function(tx, tx_no, cb) { - - - var names = iterator.stores(); - var crs = []; - for (var ni = 0; ni < names.length; ni++) { - crs[ni] = me.getIndexExecutor().getCursor(tx, tx_no, names[ni]); - } - var cursor = iterator.load(crs); - - /** - * - * @param {!Error} e error. - */ - cursor.onFail = function(e) { - cb(e, true); - }; - var index = 0; - /** - * - * @param {IDBKey=} opt_key effective key. - */ - cursor.onNext = function(opt_key) { - if (goog.isDefAndNotNull(opt_key)) { - var current_value; - if (iterator.isIndexIterator()) { - if (iterator.isKeyIterator()) { - current_value = opt_key; - } else { - current_value = cursor.getPrimaryKey(); - } - } else { - if (iterator.isKeyIterator()) { - current_value = opt_key; - } else { - current_value = cursor.getValue(); - } - } - - //console.log([previous, current_value, index]); - previous = callback(previous, current_value, index++); - cursor.advance(1); - } else { - cb(previous); - } - }; - - }, stores, ydn.db.base.TransactionMode.READ_ONLY); - - return df; -}; - - /** * List record in a store. * @param {ydn.db.base.QueryMethod} mth keys method. diff --git a/src/ydn/db/core/storage.js b/src/ydn/db/core/storage.js index 5537ea3d..24767cc5 100755 --- a/src/ydn/db/core/storage.js +++ b/src/ydn/db/core/storage.js @@ -86,45 +86,44 @@ ydn.db.core.Storage.prototype.open = function(callback, iter, opt_mode, /** * List record in a store. - * @param {ydn.db.base.QueryMethod} mth keys method * @param {!ydn.db.Iterator} iter iterator - * @param {number=} opt_limit limit - * @param {number=} opt_offset limit * @return {!ydn.db.Request} request */ -ydn.db.core.Storage.prototype.listIter = function(mth, iter, - opt_limit, opt_offset) { - return this.getIndexOperator().listIter(mth, iter, - opt_limit, opt_offset); +ydn.db.core.Storage.prototype.countOf = function(iter) { + return this.getIndexOperator().countOf(iter); }; /** - * Cursor scan iteration. - * @param {!ydn.db.algo.AbstractSolver|function(!Array, !Array): (Array|undefined)} solver - * solver - * @param {!Array.} iterators for the cursor - * @return {!goog.async.Deferred} promise upon completion + * List record in a store. + * @param {!ydn.db.Iterator} iter iterator + * @param {number=} opt_limit limit + * @return {!ydn.db.Request} request */ -ydn.db.core.Storage.prototype.scan = function(solver, iterators) { - return this.getIndexOperator().scan(solver, iterators); +ydn.db.core.Storage.prototype.valuesOf = function(iter, opt_limit) { + return this.getIndexOperator().valuesOf(iter, opt_limit); }; /** - * @inheritDoc + * List record in a store. + * @param {!ydn.db.Iterator} iter iterator + * @param {number=} opt_limit limit + * @return {!ydn.db.Request} request */ -ydn.db.core.Storage.prototype.map = function(iterator, callback) { - return this.getIndexOperator().map(iterator, callback); +ydn.db.core.Storage.prototype.keysOf = function(iter, opt_limit) { + return this.getIndexOperator().keysOf(iter, opt_limit); }; /** - * @inheritDoc + * Cursor scan iteration. + * @param {!ydn.db.algo.AbstractSolver|function(!Array, !Array): (Array|undefined)} solver + * solver + * @param {!Array.} iterators for the cursor + * @return {!goog.async.Deferred} promise upon completion */ -ydn.db.core.Storage.prototype.reduce = function(iterator, callback, - opt_initial) { - return this.getIndexOperator().reduce(iterator, callback, opt_initial); +ydn.db.core.Storage.prototype.scan = function(solver, iterators) { + return this.getIndexOperator().scan(solver, iterators); }; -//goog.exportSymbol('ydn.db.Storage', ydn.db.core.Storage); diff --git a/src/ydn/db/crud/exports.js b/src/ydn/db/crud/exports.js index b2009090..126f9a82 100755 --- a/src/ydn/db/crud/exports.js +++ b/src/ydn/db/crud/exports.js @@ -19,10 +19,12 @@ goog.exportProperty(ydn.db.crud.Storage.prototype, 'get', ydn.db.crud.Storage.prototype.get); goog.exportProperty(ydn.db.crud.Storage.prototype, 'keys', ydn.db.crud.Storage.prototype.keys); -//goog.exportProperty(ydn.db.crud.Storage.prototype, 'load', -// ydn.db.crud.Storage.prototype.load); +goog.exportProperty(ydn.db.crud.Storage.prototype, 'keysByIndex', + ydn.db.crud.Storage.prototype.keysByIndex); goog.exportProperty(ydn.db.crud.Storage.prototype, 'values', ydn.db.crud.Storage.prototype.values); +goog.exportProperty(ydn.db.crud.Storage.prototype, 'valuesByIndex', + ydn.db.crud.Storage.prototype.valuesByIndex); goog.exportProperty(ydn.db.crud.Storage.prototype, 'put', ydn.db.crud.Storage.prototype.put); goog.exportProperty(ydn.db.crud.Storage.prototype, 'clear', @@ -38,10 +40,12 @@ goog.exportProperty(ydn.db.crud.DbOperator.prototype, 'get', ydn.db.crud.DbOperator.prototype.get); goog.exportProperty(ydn.db.crud.DbOperator.prototype, 'keys', ydn.db.crud.DbOperator.prototype.keys); -//goog.exportProperty(ydn.db.crud.Storage.prototype, 'load', -// ydn.db.crud.Storage.prototype.load); +goog.exportProperty(ydn.db.crud.DbOperator.prototype, 'keysByIndex', + ydn.db.crud.DbOperator.prototype.keysByIndex); goog.exportProperty(ydn.db.crud.DbOperator.prototype, 'values', ydn.db.crud.DbOperator.prototype.values); +goog.exportProperty(ydn.db.crud.DbOperator.prototype, 'valuesByIndex', + ydn.db.crud.DbOperator.prototype.valuesByIndex); goog.exportProperty(ydn.db.crud.DbOperator.prototype, 'put', ydn.db.crud.DbOperator.prototype.put); goog.exportProperty(ydn.db.crud.DbOperator.prototype, 'clear', diff --git a/src/ydn/db/crud/i_operator.js b/src/ydn/db/crud/i_operator.js index 8552d612..80a634d1 100755 --- a/src/ydn/db/crud/i_operator.js +++ b/src/ydn/db/crud/i_operator.js @@ -43,6 +43,18 @@ ydn.db.crud.IOperator = function() {}; ydn.db.crud.IOperator.prototype.count = goog.abstractMethod; +/** + * + * @param {string} store_name store name or names. + * @param {string} index_name store name or names. + * @param {(IDBKeyRange|ydn.db.KeyRange|ydn.db.IDBKeyRange)=} opt_key_range_index + * index name or key range. + * @param {boolean=} opt_unique count unique index key. + * @return {!ydn.db.Request} return object in deferred function. + */ +ydn.db.crud.IOperator.prototype.countByIndex = goog.abstractMethod; + + /** * Return object or objects of given key or keys. * @param {(!Object|string|!ydn.db.Key)=} opt_arg1 table name. @@ -69,6 +81,21 @@ ydn.db.crud.IOperator.prototype.get = goog.abstractMethod; ydn.db.crud.IOperator.prototype.values = goog.abstractMethod; +/** + * Return object or objects of given key or keys. + * @param {string} opt_arg1 table name. + * @param {string} opt_arg2 + * list of primary keys or key range. + * @param {(IDBKeyRange|KeyRangeJson|ydn.db.KeyRange)=} opt_arg3 limit. + * @param {number=} opt_arg4 offset. + * @param {number=} opt_unique name. + * @param {boolean=} opt_arg6 reverse. + * @param {boolean=} opt_arg7 reverse. + * @return {!ydn.db.Request} return object in deferred function. + */ +ydn.db.crud.IOperator.prototype.valuesByIndex = goog.abstractMethod; + + /** * List keys or effective keys. * @param {*|string} store_name or iterator. @@ -84,6 +111,21 @@ ydn.db.crud.IOperator.prototype.values = goog.abstractMethod; ydn.db.crud.IOperator.prototype.keys = goog.abstractMethod; +/** + * List keys or effective keys. + * @param {string} store_name or iterator. + * @param {string} opt_arg1 key range + * or index name or limit for iterator. + * @param {(IDBKeyRange|ydn.db.KeyRange|KeyRangeJson)=} opt_arg2 limit or key range. + * @param {number=} opt_arg3 offset or limit. + * @param {(number)=} opt_arg4 reverse or offset. + * @param {boolean=} opt_arg5 reverse. + * @param {boolean=} unique limit. + * @return {!ydn.db.Request} result promise. + */ +ydn.db.crud.IOperator.prototype.keysByIndex = goog.abstractMethod; + + /** * Execute ADD request either storing result to tx or callback to df. * @param {string|StoreSchema} store_name_or_schema store name or diff --git a/src/ydn/db/crud/operator.js b/src/ydn/db/crud/operator.js index 278087f2..e5754f77 100755 --- a/src/ydn/db/crud/operator.js +++ b/src/ydn/db/crud/operator.js @@ -74,6 +74,67 @@ ydn.db.crud.DbOperator.prototype.getCrudExecutor = function() { }; +/** + * + * @inheritDoc + */ +ydn.db.crud.DbOperator.prototype.countByIndex = function(store_name, index_name, + index_key_range, unique) { + var req; + var me = this; + + /** + * @type {!Array.} + */ + var store_names; + + /** + * @type {IDBKeyRange} + */ + var key_range; + + var store = this.schema.getStore(store_name); + if (!store) { + throw new ydn.debug.error.ArgumentException('store name "' + store_name + + '" not found.'); + } + if (goog.DEBUG && goog.isDef(unique) && !goog.isBoolean(unique)) { + throw new ydn.debug.error.ArgumentException('unique value "' + unique + + '" must be boolean, but found ' + typeof unique + '.'); + } + store_names = [store_name]; + + if (goog.isObject(index_key_range)) { + if (goog.DEBUG) { + var msg1 = ydn.db.KeyRange.validate(index_key_range); + if (msg1) { + throw new ydn.debug.error.ArgumentException('invalid key range: ' + + ydn.json.toShortString(index_key_range) + ' ' + msg1); + } + } + key_range = ydn.db.KeyRange.parseIDBKeyRange(index_key_range); + } else { + if (goog.DEBUG && goog.isDefAndNotNull(index_key_range)) { + throw new ydn.debug.error.ArgumentException('invalid key range: ' + + ydn.json.toShortString(index_key_range) + + ' of type ' + typeof index_key_range); + } + key_range = null; + } + + goog.log.finer(this.logger, 'countIndexKeyRange: ' + store_name + ' ' + + (index_name ? index_name : '') + ydn.json.stringify(key_range)); + req = this.tx_thread.request(ydn.db.Request.Method.COUNT, store_names); + store.hook(req, arguments); + req.addTxback(function(tx) { + this.getCrudExecutor().countKeyRange(req, store_names[0], key_range, + index_name, !!unique); + }, this); + + return req; +}; + + /** * * @inheritDoc @@ -378,7 +439,7 @@ ydn.db.crud.DbOperator.prototype.keysByKeyRange = function(store_name, opt_kr, * @param {boolean=} opt_unique * @return {!ydn.db.Request>} */ -ydn.db.crud.DbOperator.prototype.keysByIndexKeyRange = function(store_name, +ydn.db.crud.DbOperator.prototype.keysByIndex = function(store_name, index_name, opt_kr, opt_limit, opt_offset, opt_reverse, opt_unique) { var range, limit, offset, reverse, unique; @@ -423,7 +484,7 @@ ydn.db.crud.DbOperator.prototype.keysByIndexKeyRange = function(store_name, 'unique must be a boolean'); } } - goog.log.finer(this.logger, 'keysByIndexKeyRange: ' + store_name); + goog.log.finer(this.logger, 'keysByIndex: ' + store_name); var req = this.tx_thread.request(ydn.db.Request.Method.KEYS_INDEX, [store_name]); store.hook(req, arguments); @@ -499,7 +560,7 @@ ydn.db.crud.DbOperator.prototype.keys = function(opt_store_name, arg1, var req; if (goog.isString(arg1)) { // index key range - req = this.keysByIndexKeyRange(store_name, + req = this.keysByIndex(store_name, /** @type {string} */ (arg1), /** @type {ydn.db.KeyRange|IDBKeyRange} */ (arg2), /** @type {number|undefined} */ (arg3), @@ -582,7 +643,7 @@ ydn.db.crud.DbOperator.prototype.values = function(arg0, arg1, arg2, arg3, arg4, this.getCrudExecutor().listByIds(req, store_name, ids); }, this); } else if (goog.isString(arg1)) { // index name - req = this.valuesByIndexKeyRange(store_name, arg1, + req = this.valuesByIndex(store_name, arg1, /** @type {ydn.db.KeyRange|IDBKeyRange} */ (arg2), arg3, /** @type {number|undefined} */ (arg4), arg5, arg6); } else { @@ -714,7 +775,7 @@ ydn.db.crud.DbOperator.prototype.valuesByKeyRange = function(store_name, opt_kr, * @param {boolean=} opt_unique * @return {!ydn.db.Request.>} */ -ydn.db.crud.DbOperator.prototype.valuesByIndexKeyRange = function(store_name, +ydn.db.crud.DbOperator.prototype.valuesByIndex = function(store_name, index_name, opt_kr, opt_limit, opt_offset, opt_reverse, opt_unique) { var store = this.schema.getStore(store_name); var limit, offset, reverse, unique; diff --git a/src/ydn/db/crud/storage.js b/src/ydn/db/crud/storage.js index d764f006..9c73315d 100755 --- a/src/ydn/db/crud/storage.js +++ b/src/ydn/db/crud/storage.js @@ -139,6 +139,16 @@ ydn.db.crud.Storage.prototype.count = function(store_name, key_range, index, }; +/** + * + * @inheritDoc + */ +ydn.db.crud.Storage.prototype.countByIndex = function(store_name, key_range, index, + unique) { + return this.getCoreOperator().countByIndex(store_name, key_range, index, unique); +}; + + /** * @inheritDoc */ @@ -153,16 +163,18 @@ ydn.db.crud.Storage.prototype.get = function(arg1, arg2) { */ ydn.db.crud.Storage.prototype.keys = function(store_name, arg2, arg3, arg4, arg5, arg6, arg7) { - // return ydn.db.crud.DbOperator.prototype.keys.apply( - // /** @type {ydn.db.crud.DbOperator} */ (this.base_tx_queue), - // Array.prototype.slice.call(arguments)); + return this.getCoreOperator().keys(store_name, arg2, arg3, arg4, arg5, arg6, + arg7); +}; - // above trick is the same effect as follow - //return this.getCoreOperator().keys(store_name, arg2, arg3, - // arg4, arg5, arg6, arg7); - // but it preserve argument length - return this.getCoreOperator().keys(store_name, arg2, arg3, arg4, arg5, arg6, +/** + * + * @inheritDoc + */ +ydn.db.crud.Storage.prototype.keysByIndex = function(store_name, arg2, arg3, arg4, + arg5, arg6, arg7) { + return this.getCoreOperator().keysByIndex(store_name, arg2, arg3, arg4, arg5, arg6, arg7); }; @@ -176,6 +188,15 @@ ydn.db.crud.Storage.prototype.values = function(arg1, arg2, arg3, arg4, arg5, }; +/** + * @inheritDoc + */ +ydn.db.crud.Storage.prototype.valuesByIndex = function(arg1, arg2, arg3, arg4, arg5, + arg6) { + return this.getCoreOperator().valuesByIndex(arg1, arg2, arg3, arg4, arg5, arg6); +}; + + /** * List * @param {ydn.db.base.QueryMethod} type diff --git a/test/qunit/crud.js b/test/qunit/crud.js index feea236a..7cc8e124 100755 --- a/test/qunit/crud.js +++ b/test/qunit/crud.js @@ -870,7 +870,7 @@ deepEqual(x, [1, 3, 2], 'number of record'); }); - db.keys(store_inline_index, 'value', ydn.db.KeyRange.only('a'), 10, 0).always(function(x) { + db.keysByIndex(store_inline_index, 'value', ydn.db.KeyRange.only('a'), 10, 0).always(function(x) { deepEqual(x, [1, 3], 'only a'); start(); var type = db.getType(); diff --git a/test/qunit/ver_1_iterator.js b/test/qunit/ver_1_iterator.js index b1609b20..bb8511bb 100755 --- a/test/qunit/ver_1_iterator.js +++ b/test/qunit/ver_1_iterator.js @@ -161,11 +161,11 @@ var schema_1 = { var value_iter = ydn.db.IndexIterator.where(store_inline_index, 'value', '>', 1, '<=', 3); var name_iter = ydn.db.IndexIterator.where(store_inline_index, 'name', '^', 'b'); - db_r.count(value_iter).always(function (x) { + db_r.countOf(value_iter).always(function (x) { //console.log('count value') equal(x, 1, 'number of values in the range'); }); - db_r.count(name_iter).always(function (x) { + db_r.countOf(name_iter).always(function (x) { equal(x, 2, 'number of name in the range'); start(); }); @@ -390,31 +390,31 @@ var schema_1 = { asyncTest("2. Ref value by index key range", 5, function () { var q = ydn.db.IndexValueIterator.where(store_inline_index, 'value', '>=', 2, '<=', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, objs.slice(1, 3), 'closed bound'); }); q = ydn.db.IndexValueIterator.where(store_inline_index, 'value', '>=', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, objs.slice(2), 'lowerBound'); }); q = ydn.db.IndexValueIterator.where(store_inline_index, 'value', '>', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, objs.slice(3), 'open lowerBound'); }); q = ydn.db.IndexValueIterator.where(store_inline_index, 'value', '<=', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, objs.slice(0, 3), 'upperBound'); }); q = ydn.db.IndexValueIterator.where(store_inline_index, 'value', '<', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, objs.slice(0, 2), 'open upperBound'); start(); @@ -425,38 +425,38 @@ var schema_1 = { var keys = objs.map(function(x) {return x.id}); var q = ydn.db.IndexIterator.where(store_inline_index, 'value', '>=', 2, '<=', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(1, 3), 'closed bound'); }); q = ydn.db.IndexIterator.where(store_inline_index, 'value', '>=', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(2), 'lowerBound'); }); q = ydn.db.IndexIterator.where(store_inline_index, 'value', '>', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(3), 'open lowerBound'); }); q = ydn.db.IndexIterator.where(store_inline_index, 'value', '<=', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(0, 3), 'upperBound'); }); q = new ydn.db.IndexIterator(store_inline_index, 'value', null, true); - db.values(q, 3).always(function (x) { + db.valuesOf(q, 3).always(function (x) { //console.log(q) deepEqual(x, keys.slice().reverse().slice(0,3), 'reverse key range'); }); q = ydn.db.IndexIterator.where(store_inline_index, 'value', '<', 4); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(0, 2), 'open upperBound'); start(); @@ -467,25 +467,25 @@ var schema_1 = { asyncTest("4. Ref value by string index key range", 4, function () { var q = ydn.db.IndexValueIterator.where(store_inline_index, 'name', '^', 'b'); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) equal(x.length, 4, 'LIKE%'); }); q = ydn.db.IndexValueIterator.where(store_inline_index, 'name', '=', 'b'); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x.length, 1, 'equal'); }); q = ydn.db.IndexValueIterator.where(store_inline_index, 'name', '<', 'b'); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x.length, 1, '<'); }); q = ydn.db.IndexValueIterator.where(store_inline_index, 'name', '^', 'd'); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x.length, 0, 'LIKE% no result'); start(); @@ -497,7 +497,7 @@ var schema_1 = { var range = ydn.db.KeyRange.only('a'); var q = new ydn.db.IndexIterator(store_inline_index, 'tags', range); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, [objs[0].id, objs[3].id, objs[6].id], 'ref value only a'); @@ -505,21 +505,21 @@ var schema_1 = { range = ydn.db.KeyRange.only('a'); q = new ydn.db.IndexValueIterator(store_inline_index, 'tags', range); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, [objs[0], objs[3], objs[6]], 'only a'); }); q = new ydn.db.IndexIterator(store_inline_index, 'tags', range, false, true); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, [objs[0].id], 'only a unique'); }); q = new ydn.db.IndexValueIterator(store_inline_index, 'tags', range, false, true); - db.values(q).always(function (x) { + db.valuesOf(q).always(function (x) { //console.log(q) deepEqual(x, [objs[0]], 'only a unique'); start(); @@ -589,54 +589,54 @@ var schema_1 = { var key_range = ydn.db.KeyRange.bound(1, 3); var q = new ydn.db.ValueIterator(store_inline_index, key_range); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(1, 4), 'closed bound'); }); key_range = ydn.db.KeyRange.bound(1, 3); q = new ydn.db.ValueIterator(store_inline_index, key_range, true); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { var exp = keys.slice(1, 4).reverse(); deepEqual(x, exp, 'closed bound reverse'); }); key_range = ydn.db.KeyRange.bound(1, 3); q = new ydn.db.ValueIterator(store_inline_index, key_range); - db.keys(q, 1).always(function (x) { + db.keysOf(q, 1).always(function (x) { deepEqual(x, keys.slice(1, 2), 'closed bound limit'); }); key_range = ydn.db.KeyRange.bound(1, 3); q = new ydn.db.ValueIterator(store_inline_index, key_range, true); - db.keys(q, 1).always(function (x) { + db.keysOf(q, 1).always(function (x) { deepEqual(x, keys.slice(3, 4), 'closed bound reverse limit'); }); key_range = ydn.db.KeyRange.lowerBound(2); q = new ydn.db.ValueIterator(store_inline_index, key_range); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(2), 'lowerBound'); }); key_range = ydn.db.KeyRange.lowerBound(2, true); q = new ydn.db.ValueIterator(store_inline_index, key_range); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(3), 'open lowerBound'); }); key_range = ydn.db.KeyRange.upperBound(2); q = new ydn.db.ValueIterator(store_inline_index, key_range); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(0, 3), 'upperBound'); }); key_range = ydn.db.KeyRange.upperBound(2, true); q = new ydn.db.ValueIterator(store_inline_index, key_range); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(0, 2), 'open upperBound'); start(); @@ -651,31 +651,31 @@ var schema_1 = { }); expect(5); var q = ydn.db.IndexIterator.where(store_inline_index, 'value', '>=', 2, '<=', 4); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(1, 3), 'closed bound'); }); q = ydn.db.IndexIterator.where(store_inline_index, 'value', '>=', 4); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(2), 'lowerBound'); }); q = ydn.db.IndexIterator.where(store_inline_index, 'value', '>', 4); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(3), 'open lowerBound'); }); q = ydn.db.IndexIterator.where(store_inline_index, 'value', '<=', 4); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(0, 3), 'upperBound'); }); q = ydn.db.IndexIterator.where(store_inline_index, 'value', '<', 4); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, keys.slice(0, 2), 'open upperBound'); start(); @@ -688,26 +688,26 @@ var schema_1 = { var range = ydn.db.KeyRange.only('a'); var q = new ydn.db.IndexIterator(store_inline_index, 'tags', range); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, ['a', 'a', 'a'], 'only a'); }); range = ydn.db.KeyRange.only('a'); q = new ydn.db.IndexValueIterator(store_inline_index, 'tags', range); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, ['a', 'a', 'a'], 'only a'); }); q = new ydn.db.IndexIterator(store_inline_index, 'tags', range, false, true); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, ['a'], 'only a unique'); }); q = new ydn.db.IndexValueIterator(store_inline_index, 'tags', range, false, true); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, ['a'], 'only a unique'); }); @@ -719,13 +719,13 @@ var schema_1 = { result.sort(); var q = new ydn.db.IndexIterator(store_inline_index, 'tags'); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, result, 'all'); }); var q = new ydn.db.IndexIterator(store_inline_index, 'tags', null, false, true); - db.keys(q).always(function (x) { + db.keysOf(q).always(function (x) { //console.log(q) deepEqual(x, ['a', 'b', 'c', 'd', 'e', 'z'], 'all unique'); start(); @@ -748,4 +748,4 @@ QUnit.moduleDone(function(result) { QUnit.done(function(results) { reporter.report(); -}); \ No newline at end of file +});