/* Given a (mostly empty) 'message' and its corresponding
* 'message_file' link it to existing threads in the database.
*
- * We first look at 'message_file' and its link-relevant headers
- * (References and In-Reply-To) for message IDs. We also look in the
- * database for existing message that reference 'message'. In either
- * case, we will assign to the current message the first thread_id
+ * The first check is in the metadata of the database to see if we
+ * have pre-allocated a thread_id in advance for this message, (which
+ * would have happened if a message was previously added that
+ * referenced this one).
+ *
+ * Second, we look at 'message_file' and its link-relevant headers
+ * (References and In-Reply-To) for message IDs.
+ *
+ * Finally, we look in the database for existing message that
+ * reference 'message'.
+ *
+ * In all cases, we assign to the current message the first thread_id
* found (through either parent or child). We will also merge any
* existing, distinct threads where this message belongs to both,
* (which is not uncommon when mesages are processed out of order).
*
- * Finally, if not thread ID has been found through parent or child,
- * we call _notmuch_message_generate_thread_id to generate a new
- * generates a new thread ID if the message doesn't connect to any
- * existing threads.
+ * Finally, if no thread ID has been found through parent or child, we
+ * call _notmuch_message_generate_thread_id to generate a new thread
+ * ID. This should only happen for new, top-level messages, (no
+ * References or In-Reply-To header in this message, and no previously
+ * added message refers to this message).
*/
static notmuch_status_t
_notmuch_database_link_message (notmuch_database_t *notmuch,
notmuch_message_file_t *message_file)
{
notmuch_status_t status;
- const char *thread_id = NULL;
- char *metadata_key = _get_metadata_thread_id_key (message,
- notmuch_message_get_message_id (message));
+ const char *message_id, *thread_id = NULL;
+ char *metadata_key;
+ string stored_id;
+
+ message_id = notmuch_message_get_message_id (message);
+ metadata_key = _get_metadata_thread_id_key (message, message_id);
+
/* Check if we have already seen related messages to this one.
* If we have then use the thread_id that we stored at that time.
*/
- string stored_id = notmuch->xapian_db->get_metadata (metadata_key);
- if (!stored_id.empty()) {
- Xapian::WritableDatabase *db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+ stored_id = notmuch->xapian_db->get_metadata (metadata_key);
+ if (! stored_id.empty()) {
+ Xapian::WritableDatabase *db;
+
+ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+
+ /* Clear the metadata for this message ID. We don't need it
+ * anymore. */
db->set_metadata (metadata_key, "");
thread_id = stored_id.c_str();
+
_notmuch_message_add_term (message, "thread", thread_id);
}
talloc_free (metadata_key);