1 /* query.cc - Support for searching a notmuch database
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
24 #include <glib.h> /* GHashTable, GPtrArray */
26 struct _notmuch_query {
27 notmuch_database_t *notmuch;
28 const char *query_string;
30 notmuch_string_list_t *exclude_terms;
31 notmuch_exclude_t omit_excluded;
33 notmuch_query_syntax_t syntax;
34 Xapian::Query xapian_query;
35 std::set<std::string> terms;
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;
45 struct _notmuch_doc_id_set {
46 unsigned char *bitmap;
50 #define DOCIDSET_WORD(bit) ((bit) / CHAR_BIT)
51 #define DOCIDSET_BIT(bit) ((bit) % CHAR_BIT)
53 struct _notmuch_threads {
54 notmuch_query_t *query;
56 /* The ordered list of doc ids matched by the query. */
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;
65 /* We need this in the message functions so forward declare. */
67 _notmuch_doc_id_set_init (void *ctx,
68 notmuch_doc_id_set_t *doc_ids,
74 char *env = getenv ("NOTMUCH_DEBUG_QUERY");
76 return (env && strcmp (env, "") != 0);
79 /* Explicit destructor call for placement new */
81 _notmuch_query_destructor (notmuch_query_t *query)
83 query->xapian_query.~Query();
84 query->terms.~set<std::string>();
88 static notmuch_query_t *
89 _notmuch_query_constructor (notmuch_database_t *notmuch,
90 const char *query_string)
92 notmuch_query_t *query;
95 fprintf (stderr, "Query string is:\n%s\n", query_string);
97 query = talloc (notmuch, notmuch_query_t);
98 if (unlikely (query == NULL))
101 new (&query->xapian_query) Xapian::Query ();
102 new (&query->terms) std::set<std::string> ();
103 query->parsed = false;
105 talloc_set_destructor (query, _notmuch_query_destructor);
107 query->notmuch = notmuch;
110 query->query_string = talloc_strdup (query, query_string);
112 query->query_string = NULL;
114 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
116 query->exclude_terms = _notmuch_string_list_create (query);
118 query->omit_excluded = NOTMUCH_EXCLUDE_TRUE;
124 notmuch_query_create (notmuch_database_t *notmuch,
125 const char *query_string)
128 notmuch_query_t *query;
129 notmuch_status_t status;
131 status = notmuch_query_create_with_syntax (notmuch, query_string,
132 NOTMUCH_QUERY_SYNTAX_XAPIAN,
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)
147 notmuch_query_t *query;
150 return NOTMUCH_STATUS_NULL_POINTER;
152 query = _notmuch_query_constructor (notmuch, query_string);
154 return NOTMUCH_STATUS_OUT_OF_MEMORY;
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;
161 query->syntax = syntax;
165 return NOTMUCH_STATUS_SUCCESS;
168 static notmuch_status_t
169 _notmuch_query_ensure_parsed_xapian (notmuch_query_t *query)
172 query->xapian_query =
173 query->notmuch->query_parser->
174 parse_query (query->query_string, NOTMUCH_QUERY_PARSER_FLAGS);
176 /* Xapian doesn't support skip_to on terms from a query since
177 * they are unordered, so cache a copy of all terms in
178 * something searchable.
181 for (Xapian::TermIterator t = query->xapian_query.get_terms_begin ();
182 t != query->xapian_query.get_terms_end (); ++t)
183 query->terms.insert (*t);
185 query->parsed = true;
187 } catch (const Xapian::Error &error) {
188 if (! query->notmuch->exception_reported) {
189 _notmuch_database_log (query->notmuch,
190 "A Xapian exception occurred parsing query: %s\n",
191 error.get_msg ().c_str ());
192 _notmuch_database_log_append (query->notmuch,
193 "Query string was: %s\n",
194 query->query_string);
195 query->notmuch->exception_reported = true;
198 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
200 return NOTMUCH_STATUS_SUCCESS;
203 static notmuch_status_t
204 _notmuch_query_ensure_parsed_sexpr (notmuch_query_t *query)
207 return NOTMUCH_STATUS_SUCCESS;
209 return _notmuch_sexp_string_to_xapian_query (query->notmuch, query->query_string,
210 query->xapian_query);
213 static notmuch_status_t
214 _notmuch_query_ensure_parsed (notmuch_query_t *query)
217 return NOTMUCH_STATUS_SUCCESS;
220 if (query->syntax == NOTMUCH_QUERY_SYNTAX_SEXP)
221 return _notmuch_query_ensure_parsed_sexpr (query);
224 return _notmuch_query_ensure_parsed_xapian (query);
228 notmuch_query_get_query_string (const notmuch_query_t *query)
230 return query->query_string;
234 notmuch_query_set_omit_excluded (notmuch_query_t *query,
235 notmuch_exclude_t omit_excluded)
237 query->omit_excluded = omit_excluded;
241 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
247 notmuch_query_get_sort (const notmuch_query_t *query)
253 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
255 notmuch_status_t status;
258 status = _notmuch_query_ensure_parsed (query);
262 term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
263 if (query->terms.count (term) != 0)
264 return NOTMUCH_STATUS_IGNORED;
266 _notmuch_string_list_append (query->exclude_terms, term);
267 return NOTMUCH_STATUS_SUCCESS;
270 /* We end up having to call the destructors explicitly because we had
271 * to use "placement new" in order to initialize C++ objects within a
272 * block that we allocated with talloc. So C++ is making talloc
273 * slightly less simple to use, (we wouldn't need
274 * talloc_set_destructor at all otherwise).
277 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
279 messages->iterator.~MSetIterator ();
280 messages->iterator_end.~MSetIterator ();
285 /* Return a query that matches messages with the excluded tags
286 * registered with query. The caller of this function has to combine the returned
287 * query appropriately.*/
289 _notmuch_exclude_tags (notmuch_query_t *query)
291 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
293 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
295 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
296 exclude_query, Xapian::Query (term->string));
298 return exclude_query;
303 notmuch_query_search_messages_st (notmuch_query_t *query,
304 notmuch_messages_t **out)
306 return notmuch_query_search_messages (query, out);
310 notmuch_query_search_messages (notmuch_query_t *query,
311 notmuch_messages_t **out)
313 return _notmuch_query_search_documents (query, "mail", out);
317 _notmuch_query_search_documents (notmuch_query_t *query,
319 notmuch_messages_t **out)
321 notmuch_database_t *notmuch = query->notmuch;
322 const char *query_string = query->query_string;
323 notmuch_mset_messages_t *messages;
324 notmuch_status_t status;
326 status = _notmuch_query_ensure_parsed (query);
330 messages = talloc (query, notmuch_mset_messages_t);
331 if (unlikely (messages == NULL))
332 return NOTMUCH_STATUS_OUT_OF_MEMORY;
336 messages->base.is_of_list_type = false;
337 messages->base.iterator = NULL;
338 messages->notmuch = notmuch;
339 new (&messages->iterator) Xapian::MSetIterator ();
340 new (&messages->iterator_end) Xapian::MSetIterator ();
342 talloc_set_destructor (messages, _notmuch_messages_destructor);
344 Xapian::Enquire enquire (*notmuch->xapian_db);
345 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
346 _find_prefix ("type"),
348 Xapian::Query final_query, exclude_query;
350 Xapian::MSetIterator iterator;
352 if (strcmp (query_string, "") == 0 ||
353 strcmp (query_string, "*") == 0) {
354 final_query = mail_query;
356 final_query = Xapian::Query (Xapian::Query::OP_AND,
357 mail_query, query->xapian_query);
359 messages->base.excluded_doc_ids = NULL;
361 if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
362 exclude_query = _notmuch_exclude_tags (query);
364 if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
365 query->omit_excluded == NOTMUCH_EXCLUDE_ALL) {
366 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
367 final_query, exclude_query);
368 } else { /* NOTMUCH_EXCLUDE_FLAG */
369 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
370 exclude_query, final_query);
372 enquire.set_weighting_scheme (Xapian::BoolWeight ());
373 enquire.set_query (exclude_query);
375 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
377 GArray *excluded_doc_ids = g_array_new (false, false, sizeof (unsigned int));
379 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
380 unsigned int doc_id = *iterator;
381 g_array_append_val (excluded_doc_ids, doc_id);
383 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
384 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
386 g_array_unref (excluded_doc_ids);
391 enquire.set_weighting_scheme (Xapian::BoolWeight ());
393 switch (query->sort) {
394 case NOTMUCH_SORT_OLDEST_FIRST:
395 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, false);
397 case NOTMUCH_SORT_NEWEST_FIRST:
398 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, true);
400 case NOTMUCH_SORT_MESSAGE_ID:
401 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, false);
403 case NOTMUCH_SORT_UNSORTED:
407 if (_debug_query ()) {
408 fprintf (stderr, "Exclude query is:\n%s\n",
409 exclude_query.get_description ().c_str ());
410 fprintf (stderr, "Final query is:\n%s\n",
411 final_query.get_description ().c_str ());
414 enquire.set_query (final_query);
416 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
418 messages->iterator = mset.begin ();
419 messages->iterator_end = mset.end ();
421 *out = &messages->base;
422 return NOTMUCH_STATUS_SUCCESS;
424 } catch (const Xapian::Error &error) {
425 _notmuch_database_log (notmuch,
426 "A Xapian exception occurred performing query: %s\n",
427 error.get_msg ().c_str ());
428 _notmuch_database_log_append (notmuch,
429 "Query string was: %s\n",
430 query->query_string);
432 notmuch->exception_reported = true;
433 talloc_free (messages);
434 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
439 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
441 notmuch_mset_messages_t *mset_messages;
443 mset_messages = (notmuch_mset_messages_t *) messages;
445 return (mset_messages->iterator != mset_messages->iterator_end);
449 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
451 notmuch_mset_messages_t *mset_messages;
453 mset_messages = (notmuch_mset_messages_t *) messages;
455 if (! _notmuch_mset_messages_valid (&mset_messages->base))
458 return *mset_messages->iterator;
462 _notmuch_mset_messages_get (notmuch_messages_t *messages)
464 notmuch_message_t *message;
465 Xapian::docid doc_id;
466 notmuch_private_status_t status;
467 notmuch_mset_messages_t *mset_messages;
469 mset_messages = (notmuch_mset_messages_t *) messages;
471 if (! _notmuch_mset_messages_valid (&mset_messages->base))
474 doc_id = *mset_messages->iterator;
476 message = _notmuch_message_create (mset_messages,
477 mset_messages->notmuch, doc_id,
480 if (message == NULL &&
481 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
482 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
485 if (messages->excluded_doc_ids &&
486 _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
487 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
493 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
495 notmuch_mset_messages_t *mset_messages;
497 mset_messages = (notmuch_mset_messages_t *) messages;
499 mset_messages->iterator++;
503 _notmuch_doc_id_set_init (void *ctx,
504 notmuch_doc_id_set_t *doc_ids,
507 unsigned int max = 0;
508 unsigned char *bitmap;
510 for (unsigned int i = 0; i < arr->len; i++)
511 max = MAX (max, g_array_index (arr, unsigned int, i));
512 bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD (max) + 1);
517 doc_ids->bitmap = bitmap;
518 doc_ids->bound = max + 1;
520 for (unsigned int i = 0; i < arr->len; i++) {
521 unsigned int doc_id = g_array_index (arr, unsigned int, i);
522 bitmap[DOCIDSET_WORD (doc_id)] |= 1 << DOCIDSET_BIT (doc_id);
529 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
532 if (doc_id >= doc_ids->bound)
534 return doc_ids->bitmap[DOCIDSET_WORD (doc_id)] & (1 << DOCIDSET_BIT (doc_id));
538 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
541 if (doc_id < doc_ids->bound)
542 doc_ids->bitmap[DOCIDSET_WORD (doc_id)] &= ~(1 << DOCIDSET_BIT (doc_id));
545 /* Glib objects force use to use a talloc destructor as well, (but not
546 * nearly as ugly as the for messages due to C++ objects). At
547 * this point, I'd really like to have some talloc-friendly
548 * equivalents for the few pieces of glib that I'm using. */
550 _notmuch_threads_destructor (notmuch_threads_t *threads)
552 if (threads->doc_ids)
553 g_array_unref (threads->doc_ids);
559 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
561 return notmuch_query_search_threads (query, out);
565 notmuch_query_search_threads (notmuch_query_t *query,
566 notmuch_threads_t **out)
568 notmuch_threads_t *threads;
569 notmuch_messages_t *messages;
570 notmuch_status_t status;
572 threads = talloc (query, notmuch_threads_t);
574 return NOTMUCH_STATUS_OUT_OF_MEMORY;
575 threads->doc_ids = NULL;
576 talloc_set_destructor (threads, _notmuch_threads_destructor);
578 threads->query = query;
580 status = notmuch_query_search_messages (query, &messages);
582 talloc_free (threads);
586 threads->doc_ids = g_array_new (false, false, sizeof (unsigned int));
587 while (notmuch_messages_valid (messages)) {
588 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
589 g_array_append_val (threads->doc_ids, doc_id);
590 notmuch_messages_move_to_next (messages);
592 threads->doc_id_pos = 0;
594 talloc_free (messages);
596 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
598 talloc_free (threads);
599 return NOTMUCH_STATUS_OUT_OF_MEMORY;
603 return NOTMUCH_STATUS_SUCCESS;
607 notmuch_query_destroy (notmuch_query_t *query)
613 notmuch_threads_valid (notmuch_threads_t *threads)
620 while (threads->doc_id_pos < threads->doc_ids->len) {
621 doc_id = g_array_index (threads->doc_ids, unsigned int,
622 threads->doc_id_pos);
623 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
626 threads->doc_id_pos++;
629 return threads->doc_id_pos < threads->doc_ids->len;
633 notmuch_threads_get (notmuch_threads_t *threads)
637 if (! notmuch_threads_valid (threads))
640 doc_id = g_array_index (threads->doc_ids, unsigned int,
641 threads->doc_id_pos);
642 return _notmuch_thread_create (threads->query,
643 threads->query->notmuch,
646 threads->query->exclude_terms,
647 threads->query->omit_excluded,
648 threads->query->sort);
652 notmuch_threads_move_to_next (notmuch_threads_t *threads)
654 threads->doc_id_pos++;
658 notmuch_threads_destroy (notmuch_threads_t *threads)
660 talloc_free (threads);
664 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
666 return notmuch_query_count_messages (query, count_out);
670 notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)
672 return _notmuch_query_count_documents (query, "mail", count_out);
676 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
678 notmuch_database_t *notmuch = query->notmuch;
679 const char *query_string = query->query_string;
680 Xapian::doccount count = 0;
681 notmuch_status_t status;
683 status = _notmuch_query_ensure_parsed (query);
688 Xapian::Enquire enquire (*notmuch->xapian_db);
689 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
690 _find_prefix ("type"),
692 Xapian::Query final_query, exclude_query;
695 if (strcmp (query_string, "") == 0 ||
696 strcmp (query_string, "*") == 0) {
697 final_query = mail_query;
699 final_query = Xapian::Query (Xapian::Query::OP_AND,
700 mail_query, query->xapian_query);
703 exclude_query = _notmuch_exclude_tags (query);
705 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
706 final_query, exclude_query);
708 enquire.set_weighting_scheme (Xapian::BoolWeight ());
709 enquire.set_docid_order (Xapian::Enquire::ASCENDING);
711 if (_debug_query ()) {
712 fprintf (stderr, "Exclude query is:\n%s\n",
713 exclude_query.get_description ().c_str ());
714 fprintf (stderr, "Final query is:\n%s\n",
715 final_query.get_description ().c_str ());
718 enquire.set_query (final_query);
721 * Set the checkatleast parameter to the number of documents
722 * in the database to make get_matches_estimated() exact.
723 * Set the max parameter to 1 to avoid fetching documents we will discard.
725 mset = enquire.get_mset (0, 1,
726 notmuch->xapian_db->get_doccount ());
728 count = mset.get_matches_estimated ();
730 } catch (const Xapian::Error &error) {
731 _notmuch_database_log (notmuch,
732 "A Xapian exception occurred performing query: %s\n",
733 error.get_msg ().c_str ());
734 _notmuch_database_log_append (notmuch,
735 "Query string was: %s\n",
736 query->query_string);
737 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
741 return NOTMUCH_STATUS_SUCCESS;
745 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
747 return notmuch_query_count_threads (query, count);
751 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
753 notmuch_messages_t *messages;
756 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
759 query->sort = NOTMUCH_SORT_UNSORTED;
760 ret = notmuch_query_search_messages (query, &messages);
764 if (messages == NULL)
765 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
767 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
769 talloc_free (messages);
770 return NOTMUCH_STATUS_OUT_OF_MEMORY;
773 while (notmuch_messages_valid (messages)) {
774 notmuch_message_t *message = notmuch_messages_get (messages);
775 const char *thread_id = notmuch_message_get_thread_id (message);
776 char *thread_id_copy = talloc_strdup (messages, thread_id);
777 if (unlikely (thread_id_copy == NULL)) {
778 notmuch_message_destroy (message);
779 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
782 g_hash_table_insert (hash, thread_id_copy, NULL);
783 notmuch_message_destroy (message);
784 notmuch_messages_move_to_next (messages);
787 *count = g_hash_table_size (hash);
790 g_hash_table_unref (hash);
791 talloc_free (messages);
797 notmuch_query_get_database (const notmuch_query_t *query)
799 return query->notmuch;