From 5ef89b1d0f772803d11a727d30b95ee81de70e1e Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 17 Jan 2016 00:49:26 -0500 Subject: [PATCH] Speed up DB methods slightly by avoiding unnecessary yields And tidy some things up --- chrome/content/zotero/xpcom/db.js | 113 +++++++++++--------------- chrome/content/zotero/xpcom/zotero.js | 22 +++-- 2 files changed, 59 insertions(+), 76 deletions(-) diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js index 1fb8cfe098..c149572bfe 100644 --- a/chrome/content/zotero/xpcom/db.js +++ b/chrome/content/zotero/xpcom/db.js @@ -72,13 +72,12 @@ Zotero.DBConnection = function(dbName) { // Private members this._dbName = dbName; this._shutdown = false; - this._connectionAsync = null; + this._connection = null; this._transactionID = null; this._transactionDate = null; this._lastTransactionDate = null; this._transactionRollback = false; this._transactionNestingLevel = 0; - this._asyncTransactionNestingLevel = 0; this._callbacks = { begin: [], commit: [], @@ -89,7 +88,6 @@ Zotero.DBConnection = function(dbName) { } }; this._dbIsCorrupt = null - this._self = this; this._transactionPromise = null; } @@ -610,16 +608,13 @@ Zotero.DBConnection.prototype.requireTransaction = function () { * rows are Proxy objects that return values from the * underlying mozIStorageRows based on column names. */ -Zotero.DBConnection.prototype.queryAsync = function (sql, params, options) { - let conn; - let self = this; - let onRow = null; - return this._getConnectionAsync(options) - .then(function (c) { - conn = c; - [sql, params] = self.parseQueryAndParams(sql, params); +Zotero.DBConnection.prototype.queryAsync = Zotero.Promise.coroutine(function* (sql, params, options) { + try { + let onRow = null; + let conn = this._getConnection(options) || (yield this._getConnectionAsync(options)); + [sql, params] = this.parseQueryAndParams(sql, params); if (Zotero.Debug.enabled) { - self.logQuery(sql, params, options); + this.logQuery(sql, params, options); } if (options && options.onRow) { // Errors in onRow don't stop the query unless StopIteration is thrown @@ -638,11 +633,9 @@ Zotero.DBConnection.prototype.queryAsync = function (sql, params, options) { } } } - return conn.executeCached(sql, params, onRow); - }) - .then(function (rows) { + let rows = yield conn.executeCached(sql, params, onRow); // Parse out the SQL command being used - var op = sql.match(/^[^a-z]*[^ ]+/i); + let op = sql.match(/^[^a-z]*[^ ]+/i); if (op) { op = op.toString().toLowerCase(); } @@ -681,8 +674,8 @@ Zotero.DBConnection.prototype.queryAsync = function (sql, params, options) { // returning it for SELECT and REPLACE queries return; } - }) - .catch(function (e) { + } + catch (e) { if (e.errors && e.errors[0]) { var eStr = e + ""; eStr = eStr.indexOf("Error: ") == 0 ? eStr.substr(7): e; @@ -693,8 +686,8 @@ Zotero.DBConnection.prototype.queryAsync = function (sql, params, options) { else { throw e; } - }); -}; + } +}); Zotero.DBConnection.prototype.queryTx = function (sql, params, options) { @@ -711,20 +704,17 @@ Zotero.DBConnection.prototype.queryTx = function (sql, params, options) { * @param {Array|String|Integer} [params] SQL parameters to bind * @return {Promise} A promise for either the value or FALSE if no result */ -Zotero.DBConnection.prototype.valueQueryAsync = function (sql, params) { - let self = this; - return this._getConnectionAsync() - .then(function (conn) { - [sql, params] = self.parseQueryAndParams(sql, params); +Zotero.DBConnection.prototype.valueQueryAsync = Zotero.Promise.coroutine(function* (sql, params, options = {}) { + try { + let conn = this._getConnection(options) || (yield this._getConnectionAsync(options)); + [sql, params] = this.parseQueryAndParams(sql, params); if (Zotero.Debug.enabled) { - self.logQuery(sql, params); + this.logQuery(sql, params); } - return conn.executeCached(sql, params); - }) - .then(function (rows) { + let rows = yield conn.executeCached(sql, params); return rows.length ? rows[0].getResultByIndex(0) : false; - }) - .catch(function (e) { + } + catch (e) { if (e.errors && e.errors[0]) { var eStr = e + ""; eStr = eStr.indexOf("Error: ") == 0 ? eStr.substr(7): e; @@ -735,8 +725,8 @@ Zotero.DBConnection.prototype.valueQueryAsync = function (sql, params) { else { throw e; } - }); -}; + } +}); /** @@ -757,26 +747,21 @@ Zotero.DBConnection.prototype.rowQueryAsync = function (sql, params) { * @param {Array|String|Integer} [params] SQL parameters to bind * @return {Promise} A promise for an array of values in the column */ -Zotero.DBConnection.prototype.columnQueryAsync = function (sql, params) { - let conn; - let self = this; - return this._getConnectionAsync(). - then(function (c) { - conn = c; - [sql, params] = self.parseQueryAndParams(sql, params); +Zotero.DBConnection.prototype.columnQueryAsync = Zotero.Promise.coroutine(function* (sql, params, options = {}) { + try { + let conn = this._getConnection(options) || (yield this._getConnectionAsync(options)); + [sql, params] = this.parseQueryAndParams(sql, params); if (Zotero.Debug.enabled) { - self.logQuery(sql, params); + this.logQuery(sql, params); } - return conn.executeCached(sql, params); - }) - .then(function (rows) { + let rows = yield conn.executeCached(sql, params); var column = []; for (let i=0, len=rows.length; i