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