clean up m0 release; fix a few bugs, get us the rest of the way to m0 gloda-milestone-0
authorAndrew Sutherland <asutherland@asutherland.org>
Sat, 14 Jun 2008 17:25:04 -0400
changeset 822 93bb9e91d8cd4caa82602289230c380e10ceb3b1
parent 821 b06e0519d9c92477f9908671968163d6f334cb65
child 823 a3ff3e9b9edd130cebdf4ac48c7155265589a5d5
push idunknown
push userunknown
push dateunknown
clean up m0 release; fix a few bugs, get us the rest of the way to m0
modules/datamodel.js
modules/datastore.js
modules/gloda.js
modules/indexer.js
--- a/modules/datamodel.js
+++ b/modules/datamodel.js
@@ -59,17 +59,18 @@ function GlodaConversation (aDatastore, 
 GlodaConversation.prototype = {
   get id() { return this._id; },
   get subject() { return this._subject; },
   get oldestMessageDate() { return this._oldestMessageDate; },
   get newestMessageDate() { return this._newestMessageDate; },
   
   get messages() {
     if (this._messages == null) {
-      this._messages = this._datastore.getMessagesByConversationID(this._id);
+      this._messages = this._datastore.getMessagesByConversationID(this._id,
+                                                                   false);
     }
     return this._messages;
   }
 };
 
 
 function GlodaMessage (aDatastore, aID, aFolderID, aFolderURI, aMessageKey,
                        aConversationID, aConversation, aParentID,
@@ -93,26 +94,31 @@ function GlodaMessage (aDatastore, aID, 
 GlodaMessage.prototype = {
   get id() { return this._id; },
   get folderID() { return this._folderID; },
   get messageKey() { return this._messageKey; },
   get conversationID() { return this._conversationID; },
   // conversation is special
   get parentID() { return this._parentID; },
   get headerMessageID() { return this._headerMessageID; },
-  get bodySnippet() { return this._boddySnippet; },
+  get bodySnippet() { return this._bodySnippet; },
   
   get conversation() {
     if (this._conversation == null) {
       this._conversation = this._datastore.getConversationByID(
         this._conversationID);
     }
     return this._conversation;
   },
   
+  set messageKey(aMessageKey) { this._messageKey = aMessageKey; },
+  set folderURI(aFolderURI) {
+    this._folderID = this._datastore._mapFolderURI(aFolderURI);
+  },
+  
   /**
    * Return the underlying nsIMsgDBHdr from the folder storage for this, or
    *  null if the message does not exist for one reason or another.
    */
   get folderMessage() {
     if (this._folderMessage != null)
       return this._folderMessage;
     if (this._folderURI == null || this._messageKey == null)
--- a/modules/datastore.js
+++ b/modules/datastore.js
@@ -386,17 +386,16 @@ let GlodaDatastore = {
     let ims = this._insertMessageStatement;
     ims.params.folderID = folderID;
     ims.params.messageKey = aMessageKey;
     ims.params.conversationID = aConversationID;
     ims.params.parentID = aParentID;
     ims.params.headerMessageID = aHeaderMessageID;
     ims.params.bodySnippet = aBodySnippet;
 
-
     try {
        ims.execute();
     }
     catch(ex) {
        throw("error executing statement... " +
              this.dbConnection.lastError + ": " +
              this.dbConnection.lastErrorString + " - " + ex);
     }
@@ -410,28 +409,26 @@ let GlodaDatastore = {
   
   get _updateMessageStatement() {
     let statement = this._createStatement(
       "UPDATE messages SET folderID = :folderID, \
                            messageKey = :messageKey, \
                            conversationID = :conversationID, \
                            parentID = :parentID, \
                            headerMessageID = :headerMessageID, \
-                           bodySnippet = :boddySnippet \
+                           bodySnippet = :bodySnippet \
               WHERE id = :id");
     this.__defineGetter__("_updateMessageStatement", function() statement);
-    return this._insertMessageStatement; 
+    return this._updateMessageStatement; 
   }, 
   
   updateMessage: function glodaDBUpdateMessage(aMessage) {
-    let folderID = this._mapFolderURI(aFolderURI);
-    
     let ums = this._updateMessageStatement;
-    ums.params.id = aMessage.id
-    ums.params.folderID = aMessage.folderID
+    ums.params.id = aMessage.id;
+    ums.params.folderID = aMessage.folderID;
     ums.params.messageKey = aMessage.messageKey;
     ums.params.conversationID = aMessage.conversationID;
     ums.params.parentID = aMessage.parentID;
     ums.params.headerMessageID = aMessage.headerMessageID;
     ums.params.bodySnippet = aMessage.bodySnippet;
     
     ums.execute();
   },
@@ -460,16 +457,20 @@ let GlodaDatastore = {
       this._mapFolderURI(aFolderURI);
     this._selectMessageByLocationStatement.params.messageKey = aMessageKey;
     
     let message = null;
     if (this._selectMessageByLocationStatement.step())
       message = this._messageFromRow(this._selectMessageByLocationStatement.row);
     this._selectMessageByLocationStatement.reset();
     
+    if (message == null)
+      this._log.error("Error locating message with key=" + aMessageKey +
+                      " and URI " + aFolderURI);
+    
     return message;
   },
   
   getMessagesByMessageID: function glodaDBGetMessagesByMessageID(aMessageIDs) {
     let msgIDToIndex = {};
     let results = [];
     for (let iID=0; iID < aMessageIDs.length; ++iID) {
       let msgID = aMessageIDs[iID];
@@ -489,28 +490,48 @@ let GlodaDatastore = {
     while (statement.step()) {
       results[msgIDToIndex[statement.row["headerMessageID"]]] =
         this._messageFromRow(statement.row);
     }
     statement.reset();
     
     return results;
   },
+  
+  // could probably do with an optimized version of this...
+  getMessageByMessageID: function glodaDBGetMessageByMessageID(aMessageID) {
+    var ids = [aMessageID];
+    var messages = this.getMessagesByMessageID(ids);
+    return messages.pop();
+  },
 
   get _selectMessagesByConversationIDStatement() {
     let statement = this._createStatement(
       "SELECT * FROM messages WHERE conversationID = :conversationID");
     this.__defineGetter__("_selectMessagesByConversationIDStatement",
       function() statement);
     return this._selectMessagesByConversationIDStatement;
   },
 
+  get _selectMessagesByConversationIDNoGhostsStatement() {
+    let statement = this._createStatement(
+      "SELECT * FROM messages WHERE conversationID = :conversationID AND \
+                                    folderID IS NOT NULL");
+    this.__defineGetter__("_selectMessagesByConversationIDNoGhostsStatement",
+      function() statement);
+    return this._selectMessagesByConversationIDNoGhostsStatement;
+  },
+
   getMessagesByConversationID: function glodaDBGetMessagesByConversationID(
-        aConversationID) {
-    let statement = this._selectMessagesByConversationIDStatement;
+        aConversationID, aIncludeGhosts) {
+    let statement;
+    if (aIncludeGhosts)
+      statement = this._selectMessagesByConversationIDStatement;
+    else
+      statement = this._selectMessagesByConversationIDNoGhostsStatement;
     statement.params.conversationID = aConversationID; 
     
     let messages = [];
     while (statement.step()) {
       messages.push(this._messageFromRow(statement.row));
     }
     statement.reset();
     
--- a/modules/gloda.js
+++ b/modules/gloda.js
@@ -65,16 +65,24 @@ let Gloda = {
     dapp.level = Log4Moz.Level.All;
     root.addAppender(dapp);
     
     this._log = Log4Moz.Service.getLogger("Gloda.NS");
     this._log.info("Logging Initialized");
   },
   
   getMessageForHeader: function glodaNSGetMessageForHeader(aMsgHdr) {
-    return GlodaDatastore.getMessageFromLocation(aMsgHdr.folder.URI,
-                                                 aMsgHdr.messageKey);
+    let message = GlodaDatastore.getMessageFromLocation(aMsgHdr.folder.URI,
+                                                        aMsgHdr.messageKey);
+    if (message == null) {
+      message = GlodaDatastore.getMessageByMessageID(aMsgHdr.messageId);
+      this._log.warn("Fell back to locating message by id; actual message " +
+                     "key is: " + aMsgHdr.messageKey + " database key: " +
+                     message.messageKey);
+    }
+    
+    return message;
   },
   
 };
 
 Gloda._init();
 GlodaDatastore._init();
\ No newline at end of file
--- a/modules/indexer.js
+++ b/modules/indexer.js
@@ -199,19 +199,23 @@ let GlodaIndexer = {
     references.push(aMsgHdr.messageId);
     // (ancestors have a direct correspondence to the message id)
     let ancestors = this._datastore.getMessagesByMessageID(references);
     // pull our current message lookup results off
     references.pop();
     let curMsg = ancestors.pop();
     
     if (curMsg != null) {
-      this._log.warn("Attempting to re-index message: " + aMsgHdr.messageId
+      // we already know about the guy, which means he was either previously
+      // a ghost or he is a duplicate...
+      if (curMsg.messageKey != null) {
+        this._log.warn("Attempting to re-index message: " + aMsgHdr.messageId
                         + " (" + aMsgHdr.subject + ")");
-      return; 
+        return;
+      } 
     }
     
     let conversationID = null;
     
     // (walk from closest to furthest ancestor)
     for (let iAncestor=ancestors.length-1; iAncestor >= 0; --iAncestor) {
       let ancestor = ancestors[iAncestor];
       
@@ -251,28 +255,35 @@ let GlodaIndexer = {
         ancestor = this._datastore.createMessage(null, null, // no folder loc
                                                  conversationID,
                                                  lastAncestorId,
                                                  references[iAncestor],
                                                  null); // no snippet
         ancestors[iAncestor] = ancestor;
       }
       else if (ancestor.parentID == null) {
-        ancestor.parentID = lastAncestorId;
+        ancestor._parentID = lastAncestorId;
         this._datastore.updateMessage(ancestor);
       }
       
       lastAncestorId = ancestor.id;
     }
     // now all our ancestors exist, though they may be ghost-like...
     
-    this._log.debug("creating message with: " + aMsgHdr.folder.URI +
-                    ", " + conversationID +
-                    ", " + lastAncestorId + ", " + aMsgHdr.messageId +
-                    ", null.");
-    curMsg = this._datastore.createMessage(aMsgHdr.folder.URI,
-                                           aMsgHdr.messageKey,                
-                                           conversationID,
-                                           lastAncestorId,
-                                           aMsgHdr.messageId,
-                                           null); // no snippet
+    if (curMsg == null) {
+      this._log.debug("creating message with: " + aMsgHdr.folder.URI +
+                      ", " + conversationID +
+                      ", " + lastAncestorId + ", " + aMsgHdr.messageId +
+                      ", null.");
+      curMsg = this._datastore.createMessage(aMsgHdr.folder.URI,
+                                             aMsgHdr.messageKey,                
+                                             conversationID,
+                                             lastAncestorId,
+                                             aMsgHdr.messageId,
+                                             null); // no snippet
+     }
+     else {
+        curMsg.folderURI = aMsgHdr.folder.URI;
+        curMsg.messageKey = aMsgHdr.messageKey;
+        this._datastore.updateMessage(curMsg);
+     }
   },
 };