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