+ /* If we haven't seen that message yet then check if we have already
+ * generated a dummy id for it and stored it in the metadata.
+ * If not then we generate a new thread id.
+ * This ensures that we can thread messages even when we haven't received
+ * the root (yet?)
+ */
+ if (message == NULL) {
+ Xapian::WritableDatabase *db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+ char * metadata_key = _get_metadata_thread_id_key (ctx, message_id);
+ string thread_id = notmuch->xapian_db->get_metadata(metadata_key);
+ if (thread_id.empty()) {
+ ret = _notmuch_database_generate_thread_id(notmuch);
+ db->set_metadata(metadata_key, ret);
+ } else {
+ ret = thread_id.c_str();
+ }
+ talloc_free (metadata_key);
+ goto DONE;
+ }