+ return thread_id;
+ }
+
+ /* Message has not been seen yet.
+ *
+ * We may have seen a reference to it already, in which case, we
+ * can return the thread ID stored in the metadata. Otherwise, we
+ * generate a new thread ID and store it there.
+ */
+ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+ metadata_key = _get_metadata_thread_id_key (ctx, message_id);
+ thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
+
+ if (thread_id_string.empty()) {
+ thread_id = _notmuch_database_generate_thread_id (notmuch);
+ db->set_metadata (metadata_key, thread_id);
+ } else {
+ thread_id = thread_id_string.c_str();
+ }
+
+ talloc_free (metadata_key);
+
+ return thread_id;