]> git.notmuchmail.org Git - notmuch/blob - lib/query.cc
b7d4d3527d5a8b7062cb98b8e5342b6908c92525
[notmuch] / lib / query.cc
1 /* query.cc - Support for searching a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <glib.h> /* GHashTable, GPtrArray */
25
26 struct _notmuch_query {
27     notmuch_database_t *notmuch;
28     const char *query_string;
29     notmuch_sort_t sort;
30     notmuch_string_list_t *exclude_terms;
31     notmuch_exclude_t omit_excluded;
32     notmuch_bool_t parsed;
33     Xapian::Query xapian_query;
34     std::set<std::string> terms;
35 };
36
37 typedef struct _notmuch_mset_messages {
38     notmuch_messages_t base;
39     notmuch_database_t *notmuch;
40     Xapian::MSetIterator iterator;
41     Xapian::MSetIterator iterator_end;
42 } notmuch_mset_messages_t;
43
44 struct _notmuch_doc_id_set {
45     unsigned char *bitmap;
46     unsigned int bound;
47 };
48
49 #define DOCIDSET_WORD(bit) ((bit) / CHAR_BIT)
50 #define DOCIDSET_BIT(bit) ((bit) % CHAR_BIT)
51
52 struct visible _notmuch_threads {
53     notmuch_query_t *query;
54
55     /* The ordered list of doc ids matched by the query. */
56     GArray *doc_ids;
57     /* Our iterator's current position in doc_ids. */
58     unsigned int doc_id_pos;
59     /* The set of matched docid's that have not been assigned to a
60      * thread. Initially, this contains every docid in doc_ids. */
61     notmuch_doc_id_set_t match_set;
62 };
63
64 /* We need this in the message functions so forward declare. */
65 static notmuch_bool_t
66 _notmuch_doc_id_set_init (void *ctx,
67                           notmuch_doc_id_set_t *doc_ids,
68                           GArray *arr);
69
70 static notmuch_bool_t
71 _debug_query (void)
72 {
73     char *env = getenv ("NOTMUCH_DEBUG_QUERY");
74     return (env && strcmp (env, "") != 0);
75 }
76
77 /* Explicit destructor call for placement new */
78 static int
79 _notmuch_query_destructor (notmuch_query_t *query) {
80     query->xapian_query.~Query();
81     query->terms.~set<std::string>();
82     return 0;
83 }
84
85 notmuch_query_t *
86 notmuch_query_create (notmuch_database_t *notmuch,
87                       const char *query_string)
88 {
89     notmuch_query_t *query;
90
91     if (_debug_query ())
92         fprintf (stderr, "Query string is:\n%s\n", query_string);
93
94     query = talloc (notmuch, notmuch_query_t);
95     if (unlikely (query == NULL))
96         return NULL;
97
98     new (&query->xapian_query) Xapian::Query ();
99     new (&query->terms) std::set<std::string> ();
100     query->parsed = FALSE;
101
102     talloc_set_destructor (query, _notmuch_query_destructor);
103
104     query->notmuch = notmuch;
105
106     query->query_string = talloc_strdup (query, query_string);
107
108     query->sort = NOTMUCH_SORT_NEWEST_FIRST;
109
110     query->exclude_terms = _notmuch_string_list_create (query);
111
112     query->omit_excluded = NOTMUCH_EXCLUDE_TRUE;
113
114     return query;
115 }
116
117 static notmuch_status_t
118 _notmuch_query_ensure_parsed (notmuch_query_t *query)
119 {
120     if (query->parsed)
121         return NOTMUCH_STATUS_SUCCESS;
122
123     try {
124         query->xapian_query =
125             query->notmuch->query_parser->
126                 parse_query (query->query_string, NOTMUCH_QUERY_PARSER_FLAGS);
127
128        /* Xapian doesn't support skip_to on terms from a query since
129         *  they are unordered, so cache a copy of all terms in
130         *  something searchable.
131         */
132
133         for (Xapian::TermIterator t = query->xapian_query.get_terms_begin ();
134              t != query->xapian_query.get_terms_end (); ++t)
135             query->terms.insert (*t);
136
137         query->parsed = TRUE;
138
139     } catch (const Xapian::Error &error) {
140         if (!query->notmuch->exception_reported) {
141             _notmuch_database_log (query->notmuch,
142                                    "A Xapian exception occurred parsing query: %s\n",
143                                    error.get_msg ().c_str ());
144             _notmuch_database_log_append (query->notmuch,
145                                           "Query string was: %s\n",
146                                           query->query_string);
147             query->notmuch->exception_reported = TRUE;
148         }
149
150         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
151     }
152     return NOTMUCH_STATUS_SUCCESS;
153 }
154
155 const char *
156 notmuch_query_get_query_string (const notmuch_query_t *query)
157 {
158     return query->query_string;
159 }
160
161 void
162 notmuch_query_set_omit_excluded (notmuch_query_t *query,
163                                  notmuch_exclude_t omit_excluded)
164 {
165     query->omit_excluded = omit_excluded;
166 }
167
168 void
169 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
170 {
171     query->sort = sort;
172 }
173
174 notmuch_sort_t
175 notmuch_query_get_sort (const notmuch_query_t *query)
176 {
177     return query->sort;
178 }
179
180 void
181 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
182 {
183     notmuch_status_t status;
184     char *term;
185
186     status = _notmuch_query_ensure_parsed (query);
187     /* The following is not ideal error handling, but to avoid
188      * breaking the ABI, we can live with it for now. In particular at
189      * least in the notmuch CLI, any syntax error in the query is
190      * caught in a later call to _notmuch_query_ensure_parsed with a
191      * better error path.
192      *
193      * TODO: add status return to this function.
194      */
195     if (status)
196         return;
197
198     term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
199     if (query->terms.count(term) != 0)
200         return; /* XXX report ignoring exclude? */
201
202     _notmuch_string_list_append (query->exclude_terms, term);
203 }
204
205 /* We end up having to call the destructors explicitly because we had
206  * to use "placement new" in order to initialize C++ objects within a
207  * block that we allocated with talloc. So C++ is making talloc
208  * slightly less simple to use, (we wouldn't need
209  * talloc_set_destructor at all otherwise).
210  */
211 static int
212 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
213 {
214     messages->iterator.~MSetIterator ();
215     messages->iterator_end.~MSetIterator ();
216
217     return 0;
218 }
219
220 /* Return a query that matches messages with the excluded tags
221  * registered with query. The caller of this function has to combine the returned
222  * query appropriately.*/
223 static Xapian::Query
224 _notmuch_exclude_tags (notmuch_query_t *query)
225 {
226     Xapian::Query exclude_query = Xapian::Query::MatchNothing;
227
228     for (notmuch_string_node_t *term = query->exclude_terms->head; term;
229          term = term->next) {
230         exclude_query = Xapian::Query (Xapian::Query::OP_OR,
231                                        exclude_query, Xapian::Query (term->string));
232     }
233     return exclude_query;
234 }
235
236
237 notmuch_status_t
238 notmuch_query_search_messages_st (notmuch_query_t *query,
239                                   notmuch_messages_t **out)
240 {
241     return notmuch_query_search_messages (query, out);
242 }
243
244 notmuch_status_t
245 notmuch_query_search_messages (notmuch_query_t *query,
246                                   notmuch_messages_t **out)
247 {
248     return _notmuch_query_search_documents (query, "mail", out);
249 }
250
251 notmuch_status_t
252 _notmuch_query_search_documents (notmuch_query_t *query,
253                                  const char *type,
254                                  notmuch_messages_t **out)
255 {
256     notmuch_database_t *notmuch = query->notmuch;
257     const char *query_string = query->query_string;
258     notmuch_mset_messages_t *messages;
259     notmuch_status_t status;
260
261     status = _notmuch_query_ensure_parsed (query);
262     if (status)
263         return status;
264
265     messages = talloc (query, notmuch_mset_messages_t);
266     if (unlikely (messages == NULL))
267         return NOTMUCH_STATUS_OUT_OF_MEMORY;
268
269     try {
270
271         messages->base.is_of_list_type = FALSE;
272         messages->base.iterator = NULL;
273         messages->notmuch = notmuch;
274         new (&messages->iterator) Xapian::MSetIterator ();
275         new (&messages->iterator_end) Xapian::MSetIterator ();
276
277         talloc_set_destructor (messages, _notmuch_messages_destructor);
278
279         Xapian::Enquire enquire (*notmuch->xapian_db);
280         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
281                                                    _find_prefix ("type"),
282                                                    type));
283         Xapian::Query final_query, exclude_query;
284         Xapian::MSet mset;
285         Xapian::MSetIterator iterator;
286
287         if (strcmp (query_string, "") == 0 ||
288             strcmp (query_string, "*") == 0)
289         {
290             final_query = mail_query;
291         } else {
292             final_query = Xapian::Query (Xapian::Query::OP_AND,
293                                          mail_query, query->xapian_query);
294         }
295         messages->base.excluded_doc_ids = NULL;
296
297         if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
298             exclude_query = _notmuch_exclude_tags (query);
299
300             if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
301                 query->omit_excluded == NOTMUCH_EXCLUDE_ALL)
302             {
303                 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
304                                              final_query, exclude_query);
305             } else { /* NOTMUCH_EXCLUDE_FLAG */
306                 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
307                                            exclude_query, final_query);
308
309                 enquire.set_weighting_scheme (Xapian::BoolWeight());
310                 enquire.set_query (exclude_query);
311
312                 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
313
314                 GArray *excluded_doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
315
316                 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
317                     unsigned int doc_id = *iterator;
318                     g_array_append_val (excluded_doc_ids, doc_id);
319                 }
320                 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
321                 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
322                                           excluded_doc_ids);
323                 g_array_unref (excluded_doc_ids);
324             }
325         }
326
327
328         enquire.set_weighting_scheme (Xapian::BoolWeight());
329
330         switch (query->sort) {
331         case NOTMUCH_SORT_OLDEST_FIRST:
332             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
333             break;
334         case NOTMUCH_SORT_NEWEST_FIRST:
335             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
336             break;
337         case NOTMUCH_SORT_MESSAGE_ID:
338             enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
339             break;
340         case NOTMUCH_SORT_UNSORTED:
341             break;
342         }
343
344         if (_debug_query ()) {
345             fprintf (stderr, "Exclude query is:\n%s\n",
346                      exclude_query.get_description ().c_str ());
347             fprintf (stderr, "Final query is:\n%s\n",
348                      final_query.get_description ().c_str ());
349         }
350
351         enquire.set_query (final_query);
352
353         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
354
355         messages->iterator = mset.begin ();
356         messages->iterator_end = mset.end ();
357
358         *out = &messages->base;
359         return NOTMUCH_STATUS_SUCCESS;
360
361     } catch (const Xapian::Error &error) {
362         _notmuch_database_log (notmuch,
363                                "A Xapian exception occurred performing query: %s\n",
364                                error.get_msg().c_str());
365         _notmuch_database_log_append (notmuch,
366                                "Query string was: %s\n",
367                                query->query_string);
368
369         notmuch->exception_reported = TRUE;
370         talloc_free (messages);
371         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
372     }
373 }
374
375 notmuch_bool_t
376 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
377 {
378     notmuch_mset_messages_t *mset_messages;
379
380     mset_messages = (notmuch_mset_messages_t *) messages;
381
382     return (mset_messages->iterator != mset_messages->iterator_end);
383 }
384
385 static Xapian::docid
386 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
387 {
388     notmuch_mset_messages_t *mset_messages;
389
390     mset_messages = (notmuch_mset_messages_t *) messages;
391
392     if (! _notmuch_mset_messages_valid (&mset_messages->base))
393         return 0;
394
395     return *mset_messages->iterator;
396 }
397
398 notmuch_message_t *
399 _notmuch_mset_messages_get (notmuch_messages_t *messages)
400 {
401     notmuch_message_t *message;
402     Xapian::docid doc_id;
403     notmuch_private_status_t status;
404     notmuch_mset_messages_t *mset_messages;
405
406     mset_messages = (notmuch_mset_messages_t *) messages;
407
408     if (! _notmuch_mset_messages_valid (&mset_messages->base))
409         return NULL;
410
411     doc_id = *mset_messages->iterator;
412
413     message = _notmuch_message_create (mset_messages,
414                                        mset_messages->notmuch, doc_id,
415                                        &status);
416
417     if (message == NULL &&
418        status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
419     {
420         INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
421     }
422
423     if (messages->excluded_doc_ids &&
424         _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
425         notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
426
427     return message;
428 }
429
430 void
431 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
432 {
433     notmuch_mset_messages_t *mset_messages;
434
435     mset_messages = (notmuch_mset_messages_t *) messages;
436
437     mset_messages->iterator++;
438 }
439
440 static notmuch_bool_t
441 _notmuch_doc_id_set_init (void *ctx,
442                           notmuch_doc_id_set_t *doc_ids,
443                           GArray *arr)
444 {
445     unsigned int max = 0;
446     unsigned char *bitmap;
447
448     for (unsigned int i = 0; i < arr->len; i++)
449         max = MAX(max, g_array_index (arr, unsigned int, i));
450     bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD(max) + 1);
451
452     if (bitmap == NULL)
453         return FALSE;
454
455     doc_ids->bitmap = bitmap;
456     doc_ids->bound = max + 1;
457
458     for (unsigned int i = 0; i < arr->len; i++) {
459         unsigned int doc_id = g_array_index (arr, unsigned int, i);
460         bitmap[DOCIDSET_WORD(doc_id)] |= 1 << DOCIDSET_BIT(doc_id);
461     }
462
463     return TRUE;
464 }
465
466 notmuch_bool_t
467 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
468                               unsigned int doc_id)
469 {
470     if (doc_id >= doc_ids->bound)
471         return FALSE;
472     return doc_ids->bitmap[DOCIDSET_WORD(doc_id)] & (1 << DOCIDSET_BIT(doc_id));
473 }
474
475 void
476 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
477                             unsigned int doc_id)
478 {
479     if (doc_id < doc_ids->bound)
480         doc_ids->bitmap[DOCIDSET_WORD(doc_id)] &= ~(1 << DOCIDSET_BIT(doc_id));
481 }
482
483 /* Glib objects force use to use a talloc destructor as well, (but not
484  * nearly as ugly as the for messages due to C++ objects). At
485  * this point, I'd really like to have some talloc-friendly
486  * equivalents for the few pieces of glib that I'm using. */
487 static int
488 _notmuch_threads_destructor (notmuch_threads_t *threads)
489 {
490     if (threads->doc_ids)
491         g_array_unref (threads->doc_ids);
492
493     return 0;
494 }
495
496 notmuch_status_t
497 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
498 {
499     return notmuch_query_search_threads(query, out);
500 }
501
502 notmuch_status_t
503 notmuch_query_search_threads (notmuch_query_t *query,
504                               notmuch_threads_t **out)
505 {
506     notmuch_threads_t *threads;
507     notmuch_messages_t *messages;
508     notmuch_status_t status;
509
510     threads = talloc (query, notmuch_threads_t);
511     if (threads == NULL)
512         return NOTMUCH_STATUS_OUT_OF_MEMORY;
513     threads->doc_ids = NULL;
514     talloc_set_destructor (threads, _notmuch_threads_destructor);
515
516     threads->query = query;
517
518     status = notmuch_query_search_messages (query, &messages);
519     if (status) {
520         talloc_free (threads);
521         return status;
522     }
523
524     threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
525     while (notmuch_messages_valid (messages)) {
526         unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
527         g_array_append_val (threads->doc_ids, doc_id);
528         notmuch_messages_move_to_next (messages);
529     }
530     threads->doc_id_pos = 0;
531
532     talloc_free (messages);
533
534     if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
535                                     threads->doc_ids)) {
536         talloc_free (threads);
537         return NOTMUCH_STATUS_OUT_OF_MEMORY;
538     }
539
540     *out = threads;
541     return NOTMUCH_STATUS_SUCCESS;
542 }
543
544 void
545 notmuch_query_destroy (notmuch_query_t *query)
546 {
547     talloc_free (query);
548 }
549
550 notmuch_bool_t
551 notmuch_threads_valid (notmuch_threads_t *threads)
552 {
553     unsigned int doc_id;
554
555     if (! threads)
556         return FALSE;
557
558     while (threads->doc_id_pos < threads->doc_ids->len) {
559         doc_id = g_array_index (threads->doc_ids, unsigned int,
560                                 threads->doc_id_pos);
561         if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
562             break;
563
564         threads->doc_id_pos++;
565     }
566
567     return threads->doc_id_pos < threads->doc_ids->len;
568 }
569
570 notmuch_thread_t *
571 notmuch_threads_get (notmuch_threads_t *threads)
572 {
573     unsigned int doc_id;
574
575     if (! notmuch_threads_valid (threads))
576         return NULL;
577
578     doc_id = g_array_index (threads->doc_ids, unsigned int,
579                             threads->doc_id_pos);
580     return _notmuch_thread_create (threads->query,
581                                    threads->query->notmuch,
582                                    doc_id,
583                                    &threads->match_set,
584                                    threads->query->exclude_terms,
585                                    threads->query->omit_excluded,
586                                    threads->query->sort);
587 }
588
589 void
590 notmuch_threads_move_to_next (notmuch_threads_t *threads)
591 {
592     threads->doc_id_pos++;
593 }
594
595 void
596 notmuch_threads_destroy (notmuch_threads_t *threads)
597 {
598     talloc_free (threads);
599 }
600
601 notmuch_status_t
602 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
603 {
604     return notmuch_query_count_messages (query, count_out);
605 }
606
607 notmuch_status_t
608 notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)
609 {
610     return _notmuch_query_count_documents (query, "mail", count_out);
611 }
612
613 notmuch_status_t
614 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
615 {
616     notmuch_database_t *notmuch = query->notmuch;
617     const char *query_string = query->query_string;
618     Xapian::doccount count = 0;
619     notmuch_status_t status;
620
621     status = _notmuch_query_ensure_parsed (query);
622     if (status)
623         return status;
624
625     try {
626         Xapian::Enquire enquire (*notmuch->xapian_db);
627         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
628                                                    _find_prefix ("type"),
629                                                    type));
630         Xapian::Query final_query, exclude_query;
631         Xapian::MSet mset;
632
633         if (strcmp (query_string, "") == 0 ||
634             strcmp (query_string, "*") == 0)
635         {
636             final_query = mail_query;
637         } else {
638             final_query = Xapian::Query (Xapian::Query::OP_AND,
639                                          mail_query, query->xapian_query);
640         }
641
642         exclude_query = _notmuch_exclude_tags (query);
643
644         final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
645                                          final_query, exclude_query);
646
647         enquire.set_weighting_scheme(Xapian::BoolWeight());
648         enquire.set_docid_order(Xapian::Enquire::ASCENDING);
649
650         if (_debug_query ()) {
651             fprintf (stderr, "Exclude query is:\n%s\n",
652                      exclude_query.get_description ().c_str ());
653             fprintf (stderr, "Final query is:\n%s\n",
654                      final_query.get_description ().c_str ());
655         }
656
657         enquire.set_query (final_query);
658
659         /*
660          * Set the checkatleast parameter to the number of documents
661          * in the database to make get_matches_estimated() exact.
662          * Set the max parameter to 0 to avoid fetching documents we will discard.
663          */
664         mset = enquire.get_mset (0, 0,
665                                  notmuch->xapian_db->get_doccount ());
666
667         count = mset.get_matches_estimated();
668
669     } catch (const Xapian::Error &error) {
670         _notmuch_database_log (notmuch,
671                                "A Xapian exception occurred performing query: %s\n",
672                                error.get_msg().c_str());
673         _notmuch_database_log_append (notmuch,
674                                       "Query string was: %s\n",
675                                       query->query_string);
676         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
677     }
678
679     *count_out = count;
680     return NOTMUCH_STATUS_SUCCESS;
681 }
682
683 notmuch_status_t
684 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
685 {
686     return notmuch_query_count_threads (query, count);
687 }
688
689 notmuch_status_t
690 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
691 {
692     notmuch_messages_t *messages;
693     GHashTable *hash;
694     notmuch_sort_t sort;
695     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
696
697     sort = query->sort;
698     query->sort = NOTMUCH_SORT_UNSORTED;
699     ret = notmuch_query_search_messages (query, &messages);
700     if (ret)
701         return ret;
702     query->sort = sort;
703     if (messages == NULL)
704         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
705
706     hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
707     if (hash == NULL) {
708         talloc_free (messages);
709         return NOTMUCH_STATUS_OUT_OF_MEMORY;
710     }
711
712     while (notmuch_messages_valid (messages)) {
713         notmuch_message_t *message = notmuch_messages_get (messages);
714         const char *thread_id = notmuch_message_get_thread_id (message);
715         char *thread_id_copy = talloc_strdup (messages, thread_id);
716         if (unlikely (thread_id_copy == NULL)) {
717             notmuch_message_destroy (message);
718             ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
719             goto DONE;
720         }
721         g_hash_table_insert (hash, thread_id_copy, NULL);
722         notmuch_message_destroy (message);
723         notmuch_messages_move_to_next (messages);
724     }
725
726     *count = g_hash_table_size (hash);
727
728   DONE:
729     g_hash_table_unref (hash);
730     talloc_free (messages);
731
732     return ret;
733 }
734
735 notmuch_database_t *
736 notmuch_query_get_database (const notmuch_query_t *query)
737 {
738     return query->notmuch;
739 }