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