- channel = g_io_channel_new_file (filename, "r", &error);
- if (channel == NULL) {
- fprintf (stderr, "Error: %s\n", error->message);
- exit (1);
+ value_string = doc.get_value (NOTMUCH_VALUE_THREAD);
+ value = value_string.c_str();
+ if (strlen (value))
+ g_hash_table_insert (thread_ids, strdup (value), NULL);
+}
+
+/* Return one or more thread_ids, (as a GPtrArray of strings), for the
+ * given message based on looking into the database for any messages
+ * referenced in parents, and also for any messages in the database
+ * referencing message_id.
+ *
+ * Caller should free all strings in the array and the array itself,
+ * (g_ptr_array_free) when done. */
+static GPtrArray *
+find_thread_ids (Xapian::Database db,
+ GPtrArray *parents,
+ const char *message_id)
+{
+ Xapian::PostingIterator child, children_end;
+ Xapian::Document doc;
+ GHashTable *thread_ids;
+ GList *keys, *l;
+ unsigned int i;
+ const char *parent_message_id;
+ GPtrArray *result;
+
+ thread_ids = g_hash_table_new (g_str_hash, g_str_equal);
+
+ find_messages_by_term (db, "ref", message_id, &child, &children_end);
+ for ( ; child != children_end; child++) {
+ doc = find_message_by_docid (db, *child);
+ insert_thread_id (thread_ids, doc);