Bug 888784 - Make FormHistory.count use Sqlite.jsm database backend. r=mak
☠☠ backed out by 9fc7e71752fd ☠ ☠
authorMike Conley <mconley@mozilla.com>
Thu, 30 Nov 2017 14:28:13 -0500
changeset 450173 51fb50c1ea68400276c4eb715c0f76e2290c0e07
parent 450172 80a207ed79ecdfde31d6ea1c58fd576df8146c13
child 450174 10c472d10264e8fbe90f9bdc8c8706d64f75c938
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs888784
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 888784 - Make FormHistory.count use Sqlite.jsm database backend. r=mak MozReview-Commit-ID: 53gec0qQL1y
toolkit/components/satchel/FormHistory.jsm
--- a/toolkit/components/satchel/FormHistory.jsm
+++ b/toolkit/components/satchel/FormHistory.jsm
@@ -260,25 +260,16 @@ function makeQueryPredicates(aQueryData,
     return field + " = :" + field;
   }).join(delimiter);
 }
 
 /**
  * Storage statement creation and parameter binding
  */
 
-function makeCountStatement(aSearchData) {
-  let query = "SELECT COUNT(*) AS numEntries FROM moz_formhistory";
-  let queryTerms = makeQueryPredicates(aSearchData);
-  if (queryTerms) {
-    query += " WHERE " + queryTerms;
-  }
-  return dbCreateAsyncStatement(query, aSearchData);
-}
-
 function makeSearchStatement(aSearchData, aSelectTerms) {
   let query = "SELECT " + aSelectTerms.join(", ") + " FROM moz_formhistory";
   let queryTerms = makeQueryPredicates(aSearchData);
   if (queryTerms) {
     query += " WHERE " + queryTerms;
   }
 
   return dbCreateAsyncStatement(query, aSearchData);
@@ -1068,16 +1059,40 @@ this.FormHistory = {
   get db() {
     return DB.conn;
   },
 
   get enabled() {
     return Prefs.enabled;
   },
 
+  _prepareHandlers(handlers) {
+    let defaultHandlers = {
+      handleResult: NOOP,
+      handleError: NOOP,
+      handleCompletion: NOOP,
+    };
+
+    if (!handlers) {
+      return defaultHandlers;
+    }
+
+    if (handlers.handleResult) {
+      defaultHandlers.handleResult = handlers.handleResult;
+    }
+    if (handlers.handleError) {
+      defaultHandlers.handleError = handlers.handleError;
+    }
+    if (handlers.handleCompletion) {
+      defaultHandlers.handleCompletion = handlers.handleCompletion;
+    }
+
+    return defaultHandlers;
+  },
+
   search(aSelectTerms, aSearchData, aCallbacks) {
     // if no terms selected, select everything
     if (!aSelectTerms) {
       aSelectTerms = validFields;
     }
     validateSearchData(aSearchData, "Search");
 
     let stmt = makeSearchStatement(aSearchData, aSelectTerms);
@@ -1111,46 +1126,42 @@ this.FormHistory = {
           );
         }
       },
     };
 
     stmt.executeAsync(handlers);
   },
 
-  count(aSearchData, aCallbacks) {
+  count(aSearchData, aHandlers) {
     validateSearchData(aSearchData, "Count");
-    let stmt = makeCountStatement(aSearchData);
-    let handlers = {
-      handleResult(aResultSet) {
-        let row = aResultSet.getNextRow();
-        let count = row.getResultByName("numEntries");
-        if (aCallbacks && aCallbacks.handleResult) {
-          aCallbacks.handleResult(count);
-        }
-      },
+
+    let query = "SELECT COUNT(*) AS numEntries FROM moz_formhistory";
+    let queryTerms = makeQueryPredicates(aSearchData);
+    if (queryTerms) {
+      query += " WHERE " + queryTerms;
+    }
+
+    let handlers = this._prepareHandlers(aHandlers);
 
-      handleError(aError) {
-        if (aCallbacks && aCallbacks.handleError) {
-          aCallbacks.handleError(aError);
+    return new Promise((resolve, reject) => {
+      this.db.then(async conn => {
+        try {
+          let rows = await conn.executeCached(query, aSearchData);
+          let count = rows[0].getResultByName("numEntries");
+          handlers.handleResult(count);
+          handlers.handleCompletion(0);
+          resolve(count);
+        } catch (e) {
+          handlers.handleError(e);
+          handlers.handleCompletion(1);
+          reject(e);
         }
-      },
-
-      handleCompletion(aReason) {
-        if (aCallbacks && aCallbacks.handleCompletion) {
-          aCallbacks.handleCompletion(
-            aReason == Ci.mozIStorageStatementCallback.REASON_FINISHED ?
-              0 :
-              1
-          );
-        }
-      },
-    };
-
-    stmt.executeAsync(handlers);
+      });
+    });
   },
 
   update(aChanges, aCallbacks) {
     // Used to keep track of how many searches have been started. When that number
     // are finished, updateFormHistoryWrite can be called.
     let numSearches = 0;
     let completedSearches = 0;
     let searchFailed = false;