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