Bug 637576 - Part 2: Sync Form engine: cache queries. r=philikon
authorRichard Newman <rnewman@mozilla.com>
Mon, 22 Aug 2011 10:38:28 -0700
changeset 77046 904783c227c86a82f1d92a556f61bdcbf7b45d38
parent 77045 0b209890603fab79662900a3ea24c7b2f4a9b227
child 77047 fc9a011616cce0d0c77ec48f56bdbe1e1e2c2f98
child 77604 51fb40284211a0a3f8da3ea769601c87b956b2da
push id78
push userclegnitto@mozilla.com
push dateFri, 16 Dec 2011 17:32:24 +0000
treeherdermozilla-release@79d24e644fdd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersphilikon
bugs637576
milestone9.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 637576 - Part 2: Sync Form engine: cache queries. r=philikon
services/sync/modules/engines/forms.js
--- a/services/sync/modules/engines/forms.js
+++ b/services/sync/modules/engines/forms.js
@@ -63,84 +63,120 @@ Utils.deferGetSet(FormRec, "cleartext", 
 
 
 let FormWrapper = {
   _log: Log4Moz.repository.getLogger("Sync.Engine.Forms"),
 
   _getEntryCols: ["name", "value"],
   _guidCols:     ["guid"],
 
-  getAllEntries: function getAllEntries() {
-    // Sort by (lastUsed - minLast) / (maxLast - minLast) * timesUsed / maxTimes
-    let query = Svc.Form.DBConnection.createAsyncStatement(
+  _stmts: {},
+  _getStmt: function _getStmt(query) {
+    if (query in this._stmts) {
+      return this._stmts[query];
+    }
+
+    this._log.trace("Creating SQL statement: " + query);
+    let db = Svc.Form.DBConnection;
+    return this._stmts[query] = db.createAsyncStatement(query);
+  },
+
+  get _getAllEntriesStmt() {
+    const query =
       "SELECT fieldname name, value FROM moz_formhistory " +
       "ORDER BY 1.0 * (lastUsed - (SELECT lastUsed FROM moz_formhistory ORDER BY lastUsed ASC LIMIT 1)) / " +
         "((SELECT lastUsed FROM moz_formhistory ORDER BY lastUsed DESC LIMIT 1) - (SELECT lastUsed FROM moz_formhistory ORDER BY lastUsed ASC LIMIT 1)) * " +
         "timesUsed / (SELECT timesUsed FROM moz_formhistory ORDER BY timesUsed DESC LIMIT 1) DESC " +
-      "LIMIT 500");
-    return Async.querySpinningly(query, this._getEntryCols);
+      "LIMIT 500";
+    return this._getStmt(query);
+  },
+
+  get _getEntryStmt() {
+    const query = "SELECT fieldname name, value FROM moz_formhistory " +
+                  "WHERE guid = :guid";
+    return this._getStmt(query);
+  },
+
+  get _getGUIDStmt() {
+    const query = "SELECT guid FROM moz_formhistory " +
+                  "WHERE fieldname = :name AND value = :value";
+    return this._getStmt(query);
+  },
+
+  get _setGUIDStmt() {
+    const query = "UPDATE moz_formhistory SET guid = :guid " +
+                  "WHERE fieldname = :name AND value = :value";
+    return this._getStmt(query);
+  },
+
+  get _hasGUIDStmt() {
+    const query = "SELECT guid FROM moz_formhistory WHERE guid = :guid LIMIT 1";
+    return this._getStmt(query);
+  },
+
+  get _replaceGUIDStmt() {
+    const query = "UPDATE moz_formhistory SET guid = :newGUID " +
+                  "WHERE guid = :oldGUID";
+    return this._getStmt(query);
+  },
+
+  getAllEntries: function getAllEntries() {
+    return Async.querySpinningly(this._getAllEntriesStmt, this._getEntryCols);
   },
 
   getEntry: function getEntry(guid) {
-    let query = Svc.Form.DBConnection.createAsyncStatement(
-      "SELECT fieldname name, value FROM moz_formhistory WHERE guid = :guid");
-    query.params.guid = guid;
-    return Async.querySpinningly(query, this._getEntryCols)[0];
+    let stmt = this._getEntryStmt;
+    stmt.params.guid = guid;
+    return Async.querySpinningly(stmt, this._getEntryCols)[0];
   },
 
   getGUID: function getGUID(name, value) {
     // Query for the provided entry.
-    let getQuery = Svc.Form.DBConnection.createAsyncStatement(
-      "SELECT guid FROM moz_formhistory " +
-      "WHERE fieldname = :name AND value = :value");
-    getQuery.params.name = name;
-    getQuery.params.value = value;
+    let getStmt = this._getGUIDStmt;
+    getStmt.params.name = name;
+    getStmt.params.value = value;
 
     // Give the GUID if we found one.
-    let item = Async.querySpinningly(getQuery, this._guidCols)[0];
+    let item = Async.querySpinningly(getStmt, this._guidCols)[0];
 
     if (!item) {
       // Shouldn't happen, but Bug 597400...
       // Might as well just return.
       this._log.warn("GUID query returned " + item + "; turn on Trace logging for details.");
       this._log.trace("getGUID(" + JSON.stringify(name) + ", " +
                       JSON.stringify(value) + ") => " + item);
       return null;
     }
 
     if (item.guid != null) {
       return item.guid;
     }
 
     // We need to create a GUID for this entry.
-    let setQuery = Svc.Form.DBConnection.createAsyncStatement(
-      "UPDATE moz_formhistory SET guid = :guid " +
-      "WHERE fieldname = :name AND value = :value");
+    let setStmt = this._setGUIDStmt;
     let guid = Utils.makeGUID();
-    setQuery.params.guid = guid;
-    setQuery.params.name = name;
-    setQuery.params.value = value;
-    Async.querySpinningly(setQuery);
+    setStmt.params.guid = guid;
+    setStmt.params.name = name;
+    setStmt.params.value = value;
+    Async.querySpinningly(setStmt);
 
     return guid;
   },
 
   hasGUID: function hasGUID(guid) {
-    let query = Svc.Form.DBConnection.createAsyncStatement(
-      "SELECT guid FROM moz_formhistory WHERE guid = :guid LIMIT 1");
-    query.params.guid = guid;
-    return Async.querySpinningly(query, this._guidCols).length == 1;
+    let stmt = this._hasGUIDStmt;
+    stmt.params.guid = guid;
+    return Async.querySpinningly(stmt, this._guidCols).length == 1;
   },
 
   replaceGUID: function replaceGUID(oldGUID, newGUID) {
-    let query = Svc.Form.DBConnection.createAsyncStatement(
-      "UPDATE moz_formhistory SET guid = :newGUID WHERE guid = :oldGUID");
-    query.params.oldGUID = oldGUID;
-    query.params.newGUID = newGUID;
-    Async.querySpinningly(query);
+    let stmt = this._replaceGUIDStmt;
+    stmt.params.oldGUID = oldGUID;
+    stmt.params.newGUID = newGUID;
+    Async.querySpinningly(stmt);
   }
 
 };
 
 function FormEngine() {
   SyncEngine.call(this, "Forms");
 }
 FormEngine.prototype = {