]> git.cworth.org Git - notmuch/blob - lib/query.cc
tests: disable CRL checks from gpgsm
[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 notmuch_query_t *
88 notmuch_query_create (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 static notmuch_status_t
120 _notmuch_query_ensure_parsed (notmuch_query_t *query)
121 {
122     if (query->parsed)
123         return NOTMUCH_STATUS_SUCCESS;
124
125     try {
126         query->xapian_query =
127             query->notmuch->query_parser->
128             parse_query (query->query_string, NOTMUCH_QUERY_PARSER_FLAGS);
129
130         /* Xapian doesn't support skip_to on terms from a query since
131          *  they are unordered, so cache a copy of all terms in
132          *  something searchable.
133          */
134
135         for (Xapian::TermIterator t = query->xapian_query.get_terms_begin ();
136              t != query->xapian_query.get_terms_end (); ++t)
137             query->terms.insert (*t);
138
139         query->parsed = true;
140
141     } catch (const Xapian::Error &error) {
142         if (! query->notmuch->exception_reported) {
143             _notmuch_database_log (query->notmuch,
144                                    "A Xapian exception occurred parsing query: %s\n",
145                                    error.get_msg ().c_str ());
146             _notmuch_database_log_append (query->notmuch,
147                                           "Query string was: %s\n",
148                                           query->query_string);
149             query->notmuch->exception_reported = true;
150         }
151
152         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
153     }
154     return NOTMUCH_STATUS_SUCCESS;
155 }
156
157 const char *
158 notmuch_query_get_query_string (const notmuch_query_t *query)
159 {
160     return query->query_string;
161 }
162
163 void
164 notmuch_query_set_omit_excluded (notmuch_query_t *query,
165                                  notmuch_exclude_t omit_excluded)
166 {
167     query->omit_excluded = omit_excluded;
168 }
169
170 void
171 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
172 {
173     query->sort = sort;
174 }
175
176 notmuch_sort_t
177 notmuch_query_get_sort (const notmuch_query_t *query)
178 {
179     return query->sort;
180 }
181
182 notmuch_status_t
183 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
184 {
185     notmuch_status_t status;
186     char *term;
187
188     status = _notmuch_query_ensure_parsed (query);
189     if (status)
190         return status;
191
192     term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
193     if (query->terms.count (term) != 0)
194         return NOTMUCH_STATUS_IGNORED;
195
196     _notmuch_string_list_append (query->exclude_terms, term);
197     return NOTMUCH_STATUS_SUCCESS;
198 }
199
200 /* We end up having to call the destructors explicitly because we had
201  * to use "placement new" in order to initialize C++ objects within a
202  * block that we allocated with talloc. So C++ is making talloc
203  * slightly less simple to use, (we wouldn't need
204  * talloc_set_destructor at all otherwise).
205  */
206 static int
207 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
208 {
209     messages->iterator.~MSetIterator ();
210     messages->iterator_end.~MSetIterator ();
211
212     return 0;
213 }
214
215 /* Return a query that matches messages with the excluded tags
216  * registered with query. The caller of this function has to combine the returned
217  * query appropriately.*/
218 static Xapian::Query
219 _notmuch_exclude_tags (notmuch_query_t *query)
220 {
221     Xapian::Query exclude_query = Xapian::Query::MatchNothing;
222
223     for (notmuch_string_node_t *term = query->exclude_terms->head; term;
224          term = term->next) {
225         exclude_query = Xapian::Query (Xapian::Query::OP_OR,
226                                        exclude_query, Xapian::Query (term->string));
227     }
228     return exclude_query;
229 }
230
231
232 notmuch_status_t
233 notmuch_query_search_messages_st (notmuch_query_t *query,
234                                   notmuch_messages_t **out)
235 {
236     return notmuch_query_search_messages (query, out);
237 }
238
239 notmuch_status_t
240 notmuch_query_search_messages (notmuch_query_t *query,
241                                notmuch_messages_t **out)
242 {
243     return _notmuch_query_search_documents (query, "mail", out);
244 }
245
246 notmuch_status_t
247 _notmuch_query_search_documents (notmuch_query_t *query,
248                                  const char *type,
249                                  notmuch_messages_t **out)
250 {
251     notmuch_database_t *notmuch = query->notmuch;
252     const char *query_string = query->query_string;
253     notmuch_mset_messages_t *messages;
254     notmuch_status_t status;
255
256     status = _notmuch_query_ensure_parsed (query);
257     if (status)
258         return status;
259
260     messages = talloc (query, notmuch_mset_messages_t);
261     if (unlikely (messages == NULL))
262         return NOTMUCH_STATUS_OUT_OF_MEMORY;
263
264     try {
265
266         messages->base.is_of_list_type = false;
267         messages->base.iterator = NULL;
268         messages->notmuch = notmuch;
269         new (&messages->iterator) Xapian::MSetIterator ();
270         new (&messages->iterator_end) Xapian::MSetIterator ();
271
272         talloc_set_destructor (messages, _notmuch_messages_destructor);
273
274         Xapian::Enquire enquire (*notmuch->xapian_db);
275         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
276                                                    _find_prefix ("type"),
277                                                    type));
278         Xapian::Query final_query, exclude_query;
279         Xapian::MSet mset;
280         Xapian::MSetIterator iterator;
281
282         if (strcmp (query_string, "") == 0 ||
283             strcmp (query_string, "*") == 0) {
284             final_query = mail_query;
285         } else {
286             final_query = Xapian::Query (Xapian::Query::OP_AND,
287                                          mail_query, query->xapian_query);
288         }
289         messages->base.excluded_doc_ids = NULL;
290
291         if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
292             exclude_query = _notmuch_exclude_tags (query);
293
294             if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
295                 query->omit_excluded == NOTMUCH_EXCLUDE_ALL) {
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         INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
413     }
414
415     if (messages->excluded_doc_ids &&
416         _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
417         notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
418
419     return message;
420 }
421
422 void
423 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
424 {
425     notmuch_mset_messages_t *mset_messages;
426
427     mset_messages = (notmuch_mset_messages_t *) messages;
428
429     mset_messages->iterator++;
430 }
431
432 static bool
433 _notmuch_doc_id_set_init (void *ctx,
434                           notmuch_doc_id_set_t *doc_ids,
435                           GArray *arr)
436 {
437     unsigned int max = 0;
438     unsigned char *bitmap;
439
440     for (unsigned int i = 0; i < arr->len; i++)
441         max = MAX (max, g_array_index (arr, unsigned int, i));
442     bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD (max) + 1);
443
444     if (bitmap == NULL)
445         return false;
446
447     doc_ids->bitmap = bitmap;
448     doc_ids->bound = max + 1;
449
450     for (unsigned int i = 0; i < arr->len; i++) {
451         unsigned int doc_id = g_array_index (arr, unsigned int, i);
452         bitmap[DOCIDSET_WORD (doc_id)] |= 1 << DOCIDSET_BIT (doc_id);
453     }
454
455     return true;
456 }
457
458 bool
459 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
460                               unsigned int doc_id)
461 {
462     if (doc_id >= doc_ids->bound)
463         return false;
464     return doc_ids->bitmap[DOCIDSET_WORD (doc_id)] & (1 << DOCIDSET_BIT (doc_id));
465 }
466
467 void
468 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
469                             unsigned int doc_id)
470 {
471     if (doc_id < doc_ids->bound)
472         doc_ids->bitmap[DOCIDSET_WORD (doc_id)] &= ~(1 << DOCIDSET_BIT (doc_id));
473 }
474
475 /* Glib objects force use to use a talloc destructor as well, (but not
476  * nearly as ugly as the for messages due to C++ objects). At
477  * this point, I'd really like to have some talloc-friendly
478  * equivalents for the few pieces of glib that I'm using. */
479 static int
480 _notmuch_threads_destructor (notmuch_threads_t *threads)
481 {
482     if (threads->doc_ids)
483         g_array_unref (threads->doc_ids);
484
485     return 0;
486 }
487
488 notmuch_status_t
489 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
490 {
491     return notmuch_query_search_threads (query, out);
492 }
493
494 notmuch_status_t
495 notmuch_query_search_threads (notmuch_query_t *query,
496                               notmuch_threads_t **out)
497 {
498     notmuch_threads_t *threads;
499     notmuch_messages_t *messages;
500     notmuch_status_t status;
501
502     threads = talloc (query, notmuch_threads_t);
503     if (threads == NULL)
504         return NOTMUCH_STATUS_OUT_OF_MEMORY;
505     threads->doc_ids = NULL;
506     talloc_set_destructor (threads, _notmuch_threads_destructor);
507
508     threads->query = query;
509
510     status = notmuch_query_search_messages (query, &messages);
511     if (status) {
512         talloc_free (threads);
513         return status;
514     }
515
516     threads->doc_ids = g_array_new (false, false, sizeof (unsigned int));
517     while (notmuch_messages_valid (messages)) {
518         unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
519         g_array_append_val (threads->doc_ids, doc_id);
520         notmuch_messages_move_to_next (messages);
521     }
522     threads->doc_id_pos = 0;
523
524     talloc_free (messages);
525
526     if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
527                                     threads->doc_ids)) {
528         talloc_free (threads);
529         return NOTMUCH_STATUS_OUT_OF_MEMORY;
530     }
531
532     *out = threads;
533     return NOTMUCH_STATUS_SUCCESS;
534 }
535
536 void
537 notmuch_query_destroy (notmuch_query_t *query)
538 {
539     talloc_free (query);
540 }
541
542 notmuch_bool_t
543 notmuch_threads_valid (notmuch_threads_t *threads)
544 {
545     unsigned int doc_id;
546
547     if (! threads)
548         return false;
549
550     while (threads->doc_id_pos < threads->doc_ids->len) {
551         doc_id = g_array_index (threads->doc_ids, unsigned int,
552                                 threads->doc_id_pos);
553         if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
554             break;
555
556         threads->doc_id_pos++;
557     }
558
559     return threads->doc_id_pos < threads->doc_ids->len;
560 }
561
562 notmuch_thread_t *
563 notmuch_threads_get (notmuch_threads_t *threads)
564 {
565     unsigned int doc_id;
566
567     if (! notmuch_threads_valid (threads))
568         return NULL;
569
570     doc_id = g_array_index (threads->doc_ids, unsigned int,
571                             threads->doc_id_pos);
572     return _notmuch_thread_create (threads->query,
573                                    threads->query->notmuch,
574                                    doc_id,
575                                    &threads->match_set,
576                                    threads->query->exclude_terms,
577                                    threads->query->omit_excluded,
578                                    threads->query->sort);
579 }
580
581 void
582 notmuch_threads_move_to_next (notmuch_threads_t *threads)
583 {
584     threads->doc_id_pos++;
585 }
586
587 void
588 notmuch_threads_destroy (notmuch_threads_t *threads)
589 {
590     talloc_free (threads);
591 }
592
593 notmuch_status_t
594 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
595 {
596     return notmuch_query_count_messages (query, count_out);
597 }
598
599 notmuch_status_t
600 notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)
601 {
602     return _notmuch_query_count_documents (query, "mail", count_out);
603 }
604
605 notmuch_status_t
606 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
607 {
608     notmuch_database_t *notmuch = query->notmuch;
609     const char *query_string = query->query_string;
610     Xapian::doccount count = 0;
611     notmuch_status_t status;
612
613     status = _notmuch_query_ensure_parsed (query);
614     if (status)
615         return status;
616
617     try {
618         Xapian::Enquire enquire (*notmuch->xapian_db);
619         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
620                                                    _find_prefix ("type"),
621                                                    type));
622         Xapian::Query final_query, exclude_query;
623         Xapian::MSet mset;
624
625         if (strcmp (query_string, "") == 0 ||
626             strcmp (query_string, "*") == 0) {
627             final_query = mail_query;
628         } else {
629             final_query = Xapian::Query (Xapian::Query::OP_AND,
630                                          mail_query, query->xapian_query);
631         }
632
633         exclude_query = _notmuch_exclude_tags (query);
634
635         final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
636                                      final_query, exclude_query);
637
638         enquire.set_weighting_scheme (Xapian::BoolWeight ());
639         enquire.set_docid_order (Xapian::Enquire::ASCENDING);
640
641         if (_debug_query ()) {
642             fprintf (stderr, "Exclude query is:\n%s\n",
643                      exclude_query.get_description ().c_str ());
644             fprintf (stderr, "Final query is:\n%s\n",
645                      final_query.get_description ().c_str ());
646         }
647
648         enquire.set_query (final_query);
649
650         /*
651          * Set the checkatleast parameter to the number of documents
652          * in the database to make get_matches_estimated() exact.
653          * Set the max parameter to 1 to avoid fetching documents we will discard.
654          */
655         mset = enquire.get_mset (0, 1,
656                                  notmuch->xapian_db->get_doccount ());
657
658         count = mset.get_matches_estimated ();
659
660     } catch (const Xapian::Error &error) {
661         _notmuch_database_log (notmuch,
662                                "A Xapian exception occurred performing query: %s\n",
663                                error.get_msg ().c_str ());
664         _notmuch_database_log_append (notmuch,
665                                       "Query string was: %s\n",
666                                       query->query_string);
667         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
668     }
669
670     *count_out = count;
671     return NOTMUCH_STATUS_SUCCESS;
672 }
673
674 notmuch_status_t
675 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
676 {
677     return notmuch_query_count_threads (query, count);
678 }
679
680 notmuch_status_t
681 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
682 {
683     notmuch_messages_t *messages;
684     GHashTable *hash;
685     notmuch_sort_t sort;
686     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
687
688     sort = query->sort;
689     query->sort = NOTMUCH_SORT_UNSORTED;
690     ret = notmuch_query_search_messages (query, &messages);
691     if (ret)
692         return ret;
693     query->sort = sort;
694     if (messages == NULL)
695         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
696
697     hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
698     if (hash == NULL) {
699         talloc_free (messages);
700         return NOTMUCH_STATUS_OUT_OF_MEMORY;
701     }
702
703     while (notmuch_messages_valid (messages)) {
704         notmuch_message_t *message = notmuch_messages_get (messages);
705         const char *thread_id = notmuch_message_get_thread_id (message);
706         char *thread_id_copy = talloc_strdup (messages, thread_id);
707         if (unlikely (thread_id_copy == NULL)) {
708             notmuch_message_destroy (message);
709             ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
710             goto DONE;
711         }
712         g_hash_table_insert (hash, thread_id_copy, NULL);
713         notmuch_message_destroy (message);
714         notmuch_messages_move_to_next (messages);
715     }
716
717     *count = g_hash_table_size (hash);
718
719   DONE:
720     g_hash_table_unref (hash);
721     talloc_free (messages);
722
723     return ret;
724 }
725
726 notmuch_database_t *
727 notmuch_query_get_database (const notmuch_query_t *query)
728 {
729     return query->notmuch;
730 }