sync only the last 150 history items (pending some special sauce to sync high-scoring items); various sql fixes. history is syncing now, though there are bugs here and there
authorDan Mills <thunder@mozilla.com>
Wed, 17 Dec 2008 02:32:00 -0800
changeset 45107 4db296318219a4ca2511bd13d2d50ce9ecf33b5f
parent 45106 075e33ed4e6beea434f54e26b98dc9a5a9d6a324
child 45108 055dc7f76c9c8f8972a5620b6771a48db70c2594
push idunknown
push userunknown
push dateunknown
sync only the last 150 history items (pending some special sauce to sync high-scoring items); various sql fixes. history is syncing now, though there are bugs here and there
services/sync/modules/engines/history.js
--- a/services/sync/modules/engines/history.js
+++ b/services/sync/modules/engines/history.js
@@ -35,16 +35,18 @@
  * ***** END LICENSE BLOCK ***** */
 
 const EXPORTED_SYMBOLS = ['HistoryEngine'];
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cu = Components.utils;
 
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
 Cu.import("resource://weave/log4moz.js");
 Cu.import("resource://weave/util.js");
 Cu.import("resource://weave/engines.js");
 Cu.import("resource://weave/syncCores.js");
 Cu.import("resource://weave/stores.js");
 Cu.import("resource://weave/trackers.js");
 Cu.import("resource://weave/async.js");
 
@@ -131,24 +133,31 @@ HistoryStore.prototype = {
     let stmt = this._db.createStatement(
       "SELECT id FROM moz_places WHERE url = :url");
     this.__defineGetter__("_pidStm", function() stmt);
     return stmt;
   },
 
   get _urlStm() {
     let stmt = this._db.createStatement(
-      "SELECT url FROM moz_places WHERE id = ?1");
+      "SELECT url FROM moz_places WHERE id = :id");
     this.__defineGetter__("_urlStm", function() stmt);
     return stmt;
   },
 
+  get _annoAttrIdStm() {
+    let stmt = this._db.createStatement(
+      "SELECT id from moz_anno_attributes WHERE name = :name");
+    this.__defineGetter__("_annoAttrIdStm", function() stmt);
+    return stmt;
+  },
+
   get _findPidByAnnoStm() {
     let stmt = this._db.createStatement(
-      "SELECT place_id FROM moz_annos WHERE type = ?1 AND content = ?2");
+      "SELECT place_id AS id FROM moz_annos WHERE anno_attribute_id = :attr AND content = :content");
     this.__defineGetter__("_findPidByAnnoStm", function() stmt);
     return stmt;
   },
 
   create: function HistStore_create(record) {
     this._log.debug("  -> creating history entry: " + record.id);
     let uri = Utils.makeURI(record.cleartext.URI);
     let redirect = false;
@@ -195,45 +204,65 @@ HistoryStore.prototype = {
     try {
       return this._anno.getPageAnnotation(uri, "weave/guid");
     } catch (e) {
       // FIXME
       // if (e != NS_ERROR_NOT_AVAILABLE)
       // throw e;
     }
     let guid = Utils.makeGUID();
-    this._anno.setPageAnnotation(uri, "weave/guid", guid, 0, 0);
+    this._anno.setPageAnnotation(uri, "weave/guid", guid, 0, this._anno.EXPIRE_WITH_HISTORY);
     return guid;
   },
 
   // See bug 468732 for why we use SQL here
   _findURLByGUID: function HistStore__findByGUID(guid) {
-    this._findPidByAnnoStm.bindUTF8Parameter(0, "weave/guid");
-    this._findPidByAnnoStm.bindUTF8Parameter(1, guid);
-    if (this._findPidByAnnoStm.executeStep()) {
-      let placeId = this._findPidByAnnoStm.getInt32(0);
-      this._urlStm.bindInt32Parameter(0, placeId);
-      if (this._urlStm.executeStep()) {
-        return this._urlStm.getString(0);
-      }
+    try {
+      this._annoAttrIdStm.params.name = "weave/guid";
+      if (!this._annoAttrIdStm.step())
+        return null;
+      let annoId = this._annoAttrIdStm.row.id;
+
+      this._findPidByAnnoStm.params.attr = annoId;
+      this._findPidByAnnoStm.params.content = guid;
+      if (!this._findPidByAnnoStm.step())
+        return null;
+      let placeId = this._findPidByAnnoStm.row.id;
+
+      this._urlStm.params.id = placeId;
+      if (!this._urlStm.step())
+        return null;
+
+      return this._urlStm.row.url;
+    } finally {
+      this._annoAttrIdStm.reset();
+      this._findPidByAnnoStm.reset();
+      this._urlStm.reset();
     }
-    return null;
+  },
+
+  changeItemID: function HStore_changeItemID(oldID, newID) {
+    let uri = Utils.makeURI(this._findURLByGUID(oldID));
+    this._anno.setPageAnnotation(uri, "weave/guid", newID, 0, 0);
   },
 
   // XXX need a better way to query Places for all GUIDs
   getAllIDs: function BStore_getAllIDs() {
     let all = this.wrap();
     return all;
   },
 
   wrap: function HistStore_wrap() {
     let query = this._hsvc.getNewQuery(),
         options = this._hsvc.getNewQueryOptions();
 
     query.minVisits = 1;
+    options.maxResults = 150;
+    options.sortingMode = options.SORT_BY_DATE_DESCENDING;
+    options.queryType = options.QUERY_TYPE_HISTORY;
 
     let root = this._hsvc.executeQuery(query, options).root;
     root.QueryInterface(Ci.nsINavHistoryQueryResultNode);
     root.containerOpen = true;
 
     let items = {};
     for (let i = 0; i < root.childCount; i++) {
       let item = root.getChild(i);
@@ -263,16 +292,18 @@ HistoryStore.prototype = {
 
 function HistoryTracker() {
   this._init();
 }
 HistoryTracker.prototype = {
   __proto__: Tracker.prototype,
   _logName: "HistoryTracker",
 
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver]),
+
   get _hsvc() {
     let hsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
       getService(Ci.nsINavHistoryService);
     this.__defineGetter__("_hsvc", function() hsvc);
     return hsvc;
   },
 
   // FIXME: hack!
@@ -297,27 +328,27 @@ HistoryTracker.prototype = {
    */
   _upScore: function BMT__upScore() {
     if (!this.enabled)
       return;
     this._score += 1;
   },
 
   onVisit: function HT_onVisit(uri, vid, time, session, referrer, trans) {
-    this._log.trace("onVisit: " + itemId);
+    this._log.trace("onVisit: " + uri.spec);
     this.addChangedID(this._store._getGUID(uri));
     this._upScore();
   },
   onPageExpired: function HT_onPageExpired(uri, time, entry) {
-    this._log.trace("onPageExpired: " + itemId);
+    this._log.trace("onPageExpired: " + uri.spec);
     this.addChangedID(this._store._getGUID(uri)); // XXX eek ?
     this._upScore();
   },
   onDeleteURI: function HT_onDeleteURI(uri) {
-    this._log.trace("onDeleteURI: " + itemId);
+    this._log.trace("onDeleteURI: " + uri.spec);
     this.addChangedID(this._store._getGUID(uri)); // FIXME eek
     this._upScore();
   },
   onClearHistory: function HT_onClearHistory() {
     this._log.trace("onClearHistory");
     this._score += 50;
   }
 };