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"
23 #include "xapian-extra.h"
25 #include <glib.h> /* GHashTable, GPtrArray */
27 struct _notmuch_query {
28 notmuch_database_t *notmuch;
29 const char *query_string;
31 notmuch_string_list_t *exclude_terms;
32 notmuch_exclude_t omit_excluded;
34 notmuch_query_syntax_t syntax;
35 Xapian::Query xapian_query;
36 std::set<std::string> terms;
39 typedef struct _notmuch_mset_messages {
40 notmuch_messages_t base;
41 notmuch_database_t *notmuch;
42 Xapian::MSetIterator iterator;
43 Xapian::MSetIterator iterator_end;
44 } notmuch_mset_messages_t;
46 struct _notmuch_doc_id_set {
47 unsigned char *bitmap;
51 #define DOCIDSET_WORD(bit) ((bit) / CHAR_BIT)
52 #define DOCIDSET_BIT(bit) ((bit) % CHAR_BIT)
54 struct _notmuch_threads {
55 notmuch_query_t *query;
57 /* The ordered list of doc ids matched by the query. */
59 /* Our iterator's current position in doc_ids. */
60 unsigned int doc_id_pos;
61 /* The set of matched docid's that have not been assigned to a
62 * thread. Initially, this contains every docid in doc_ids. */
63 notmuch_doc_id_set_t match_set;
66 /* We need this in the message functions so forward declare. */
68 _notmuch_doc_id_set_init (void *ctx,
69 notmuch_doc_id_set_t *doc_ids,
75 char *env = getenv ("NOTMUCH_DEBUG_QUERY");
77 return (env && strcmp (env, "") != 0);
80 /* Explicit destructor call for placement new */
82 _notmuch_query_destructor (notmuch_query_t *query)
84 query->xapian_query.~Query();
85 query->terms.~set<std::string>();
89 static notmuch_query_t *
90 _notmuch_query_constructor (notmuch_database_t *notmuch,
91 const char *query_string)
93 notmuch_query_t *query;
96 fprintf (stderr, "Query string is:\n%s\n", query_string);
98 query = talloc (notmuch, notmuch_query_t);
99 if (unlikely (query == NULL))
102 new (&query->xapian_query) Xapian::Query ();
103 new (&query->terms) std::set<std::string> ();
104 query->parsed = false;
106 talloc_set_destructor (query, _notmuch_query_destructor);
108 query->notmuch = notmuch;
111 query->query_string = talloc_strdup (query, query_string);
113 query->query_string = NULL;
115 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
117 query->exclude_terms = _notmuch_string_list_create (query);
119 query->omit_excluded = NOTMUCH_EXCLUDE_TRUE;
125 notmuch_query_create (notmuch_database_t *notmuch,
126 const char *query_string)
129 notmuch_query_t *query;
130 notmuch_status_t status;
132 status = notmuch_query_create_with_syntax (notmuch, query_string,
133 NOTMUCH_QUERY_SYNTAX_XAPIAN,
142 notmuch_query_create_with_syntax (notmuch_database_t *notmuch,
143 const char *query_string,
144 notmuch_query_syntax_t syntax,
145 notmuch_query_t **output)
148 notmuch_query_t *query;
151 return NOTMUCH_STATUS_NULL_POINTER;
153 query = _notmuch_query_constructor (notmuch, query_string);
155 return NOTMUCH_STATUS_OUT_OF_MEMORY;
157 if (syntax == NOTMUCH_QUERY_SYNTAX_SEXP && ! HAVE_SFSEXP) {
158 _notmuch_database_log (notmuch, "sexp query parser not available");
159 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
162 query->syntax = syntax;
166 return NOTMUCH_STATUS_SUCCESS;
170 _notmuch_query_cache_terms (notmuch_query_t *query)
172 /* Xapian doesn't support skip_to on terms from a query since
173 * they are unordered, so cache a copy of all terms in
174 * something searchable.
177 for (Xapian::TermIterator t = query->xapian_query.get_terms_begin ();
178 t != query->xapian_query.get_terms_end (); ++t)
179 query->terms.insert (*t);
183 _notmuch_query_string_to_xapian_query (notmuch_database_t *notmuch,
184 std::string query_string,
185 Xapian::Query &output,
189 if (query_string == "" || query_string == "*") {
190 output = xapian_query_match_all ();
193 notmuch->query_parser->
194 parse_query (query_string, NOTMUCH_QUERY_PARSER_FLAGS);
196 } catch (const Xapian::Error &error) {
197 if (! notmuch->exception_reported) {
198 _notmuch_database_log (notmuch,
199 "A Xapian exception occurred parsing query: %s\n",
200 error.get_msg ().c_str ());
201 _notmuch_database_log_append (notmuch,
202 "Query string was: %s\n",
203 query_string.c_str ());
204 notmuch->exception_reported = true;
207 msg = error.get_msg ();
208 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
210 return NOTMUCH_STATUS_SUCCESS;
213 static notmuch_status_t
214 _notmuch_query_ensure_parsed_xapian (notmuch_query_t *query)
216 notmuch_status_t status;
217 std::string msg; /* ignored */
219 status = _notmuch_query_string_to_xapian_query (query->notmuch, query->query_string,
220 query->xapian_query, msg);
224 query->parsed = true;
226 _notmuch_query_cache_terms (query);
228 return NOTMUCH_STATUS_SUCCESS;
232 static notmuch_status_t
233 _notmuch_query_ensure_parsed_sexpr (notmuch_query_t *query)
235 notmuch_status_t status;
238 return NOTMUCH_STATUS_SUCCESS;
240 status = _notmuch_sexp_string_to_xapian_query (query->notmuch, query->query_string,
241 query->xapian_query);
245 _notmuch_query_cache_terms (query);
246 return NOTMUCH_STATUS_SUCCESS;
250 static notmuch_status_t
251 _notmuch_query_ensure_parsed (notmuch_query_t *query)
254 return NOTMUCH_STATUS_SUCCESS;
257 if (query->syntax == NOTMUCH_QUERY_SYNTAX_SEXP)
258 return _notmuch_query_ensure_parsed_sexpr (query);
261 return _notmuch_query_ensure_parsed_xapian (query);
265 notmuch_query_get_query_string (const notmuch_query_t *query)
267 return query->query_string;
271 notmuch_query_set_omit_excluded (notmuch_query_t *query,
272 notmuch_exclude_t omit_excluded)
274 query->omit_excluded = omit_excluded;
278 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
284 notmuch_query_get_sort (const notmuch_query_t *query)
290 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
292 notmuch_status_t status;
295 status = _notmuch_query_ensure_parsed (query);
299 term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
300 if (query->terms.count (term) != 0)
301 return NOTMUCH_STATUS_IGNORED;
303 _notmuch_string_list_append (query->exclude_terms, term);
304 return NOTMUCH_STATUS_SUCCESS;
307 /* We end up having to call the destructors explicitly because we had
308 * to use "placement new" in order to initialize C++ objects within a
309 * block that we allocated with talloc. So C++ is making talloc
310 * slightly less simple to use, (we wouldn't need
311 * talloc_set_destructor at all otherwise).
314 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
316 messages->iterator.~MSetIterator ();
317 messages->iterator_end.~MSetIterator ();
322 /* Return a query that matches messages with the excluded tags
323 * registered with query. The caller of this function has to combine the returned
324 * query appropriately.*/
326 _notmuch_exclude_tags (notmuch_query_t *query)
328 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
330 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
332 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
333 exclude_query, Xapian::Query (term->string));
335 return exclude_query;
340 notmuch_query_search_messages_st (notmuch_query_t *query,
341 notmuch_messages_t **out)
343 return notmuch_query_search_messages (query, out);
347 notmuch_query_search_messages (notmuch_query_t *query,
348 notmuch_messages_t **out)
350 return _notmuch_query_search_documents (query, "mail", out);
354 _notmuch_query_search_documents (notmuch_query_t *query,
356 notmuch_messages_t **out)
358 notmuch_database_t *notmuch = query->notmuch;
359 notmuch_mset_messages_t *messages;
360 notmuch_status_t status;
362 status = _notmuch_query_ensure_parsed (query);
366 messages = talloc (query, notmuch_mset_messages_t);
367 if (unlikely (messages == NULL))
368 return NOTMUCH_STATUS_OUT_OF_MEMORY;
372 messages->base.is_of_list_type = false;
373 messages->base.iterator = NULL;
374 messages->notmuch = notmuch;
375 new (&messages->iterator) Xapian::MSetIterator ();
376 new (&messages->iterator_end) Xapian::MSetIterator ();
378 talloc_set_destructor (messages, _notmuch_messages_destructor);
380 Xapian::Enquire enquire (*notmuch->xapian_db);
381 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
382 _find_prefix ("type"),
384 Xapian::Query final_query, exclude_query;
386 Xapian::MSetIterator iterator;
388 final_query = Xapian::Query (Xapian::Query::OP_AND,
389 mail_query, query->xapian_query);
391 messages->base.excluded_doc_ids = NULL;
393 if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
394 exclude_query = _notmuch_exclude_tags (query);
396 if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
397 query->omit_excluded == NOTMUCH_EXCLUDE_ALL) {
398 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
399 final_query, exclude_query);
400 } else { /* NOTMUCH_EXCLUDE_FLAG */
401 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
402 exclude_query, final_query);
404 enquire.set_weighting_scheme (Xapian::BoolWeight ());
405 enquire.set_query (exclude_query);
407 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
409 GArray *excluded_doc_ids = g_array_new (false, false, sizeof (unsigned int));
411 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
412 unsigned int doc_id = *iterator;
413 g_array_append_val (excluded_doc_ids, doc_id);
415 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
416 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
418 g_array_unref (excluded_doc_ids);
423 enquire.set_weighting_scheme (Xapian::BoolWeight ());
425 switch (query->sort) {
426 case NOTMUCH_SORT_OLDEST_FIRST:
427 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, false);
429 case NOTMUCH_SORT_NEWEST_FIRST:
430 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, true);
432 case NOTMUCH_SORT_MESSAGE_ID:
433 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, false);
435 case NOTMUCH_SORT_UNSORTED:
439 if (_debug_query ()) {
440 fprintf (stderr, "Exclude query is:\n%s\n",
441 exclude_query.get_description ().c_str ());
442 fprintf (stderr, "Final query is:\n%s\n",
443 final_query.get_description ().c_str ());
446 enquire.set_query (final_query);
448 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
450 messages->iterator = mset.begin ();
451 messages->iterator_end = mset.end ();
453 *out = &messages->base;
454 return NOTMUCH_STATUS_SUCCESS;
456 } catch (const Xapian::Error &error) {
457 _notmuch_database_log (notmuch,
458 "A Xapian exception occurred performing query: %s\n",
459 error.get_msg ().c_str ());
460 _notmuch_database_log_append (notmuch,
461 "Query string was: %s\n",
462 query->query_string);
464 notmuch->exception_reported = true;
465 talloc_free (messages);
466 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
471 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
473 notmuch_mset_messages_t *mset_messages;
475 mset_messages = (notmuch_mset_messages_t *) messages;
477 return (mset_messages->iterator != mset_messages->iterator_end);
481 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
483 notmuch_mset_messages_t *mset_messages;
485 mset_messages = (notmuch_mset_messages_t *) messages;
487 if (! _notmuch_mset_messages_valid (&mset_messages->base))
490 return *mset_messages->iterator;
494 _notmuch_mset_messages_get (notmuch_messages_t *messages)
496 notmuch_message_t *message;
497 Xapian::docid doc_id;
498 notmuch_private_status_t status;
499 notmuch_mset_messages_t *mset_messages;
501 mset_messages = (notmuch_mset_messages_t *) messages;
503 if (! _notmuch_mset_messages_valid (&mset_messages->base))
506 doc_id = *mset_messages->iterator;
508 message = _notmuch_message_create (mset_messages,
509 mset_messages->notmuch, doc_id,
512 if (message == NULL &&
513 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
514 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
517 if (messages->excluded_doc_ids &&
518 _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
519 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
525 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
527 notmuch_mset_messages_t *mset_messages;
529 mset_messages = (notmuch_mset_messages_t *) messages;
531 mset_messages->iterator++;
535 _notmuch_doc_id_set_init (void *ctx,
536 notmuch_doc_id_set_t *doc_ids,
539 unsigned int max = 0;
540 unsigned char *bitmap;
542 for (unsigned int i = 0; i < arr->len; i++)
543 max = MAX (max, g_array_index (arr, unsigned int, i));
544 bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD (max) + 1);
549 doc_ids->bitmap = bitmap;
550 doc_ids->bound = max + 1;
552 for (unsigned int i = 0; i < arr->len; i++) {
553 unsigned int doc_id = g_array_index (arr, unsigned int, i);
554 bitmap[DOCIDSET_WORD (doc_id)] |= 1 << DOCIDSET_BIT (doc_id);
561 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
564 if (doc_id >= doc_ids->bound)
566 return doc_ids->bitmap[DOCIDSET_WORD (doc_id)] & (1 << DOCIDSET_BIT (doc_id));
570 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
573 if (doc_id < doc_ids->bound)
574 doc_ids->bitmap[DOCIDSET_WORD (doc_id)] &= ~(1 << DOCIDSET_BIT (doc_id));
577 /* Glib objects force use to use a talloc destructor as well, (but not
578 * nearly as ugly as the for messages due to C++ objects). At
579 * this point, I'd really like to have some talloc-friendly
580 * equivalents for the few pieces of glib that I'm using. */
582 _notmuch_threads_destructor (notmuch_threads_t *threads)
584 if (threads->doc_ids)
585 g_array_unref (threads->doc_ids);
591 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
593 return notmuch_query_search_threads (query, out);
597 notmuch_query_search_threads (notmuch_query_t *query,
598 notmuch_threads_t **out)
600 notmuch_threads_t *threads;
601 notmuch_messages_t *messages;
602 notmuch_status_t status;
604 threads = talloc (query, notmuch_threads_t);
606 return NOTMUCH_STATUS_OUT_OF_MEMORY;
607 threads->doc_ids = NULL;
608 talloc_set_destructor (threads, _notmuch_threads_destructor);
610 threads->query = query;
612 status = notmuch_query_search_messages (query, &messages);
614 talloc_free (threads);
618 threads->doc_ids = g_array_new (false, false, sizeof (unsigned int));
619 while (notmuch_messages_valid (messages)) {
620 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
621 g_array_append_val (threads->doc_ids, doc_id);
622 notmuch_messages_move_to_next (messages);
624 threads->doc_id_pos = 0;
626 talloc_free (messages);
628 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
630 talloc_free (threads);
631 return NOTMUCH_STATUS_OUT_OF_MEMORY;
635 return NOTMUCH_STATUS_SUCCESS;
639 notmuch_query_destroy (notmuch_query_t *query)
645 notmuch_threads_valid (notmuch_threads_t *threads)
652 while (threads->doc_id_pos < threads->doc_ids->len) {
653 doc_id = g_array_index (threads->doc_ids, unsigned int,
654 threads->doc_id_pos);
655 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
658 threads->doc_id_pos++;
661 return threads->doc_id_pos < threads->doc_ids->len;
665 notmuch_threads_get (notmuch_threads_t *threads)
669 if (! notmuch_threads_valid (threads))
672 doc_id = g_array_index (threads->doc_ids, unsigned int,
673 threads->doc_id_pos);
674 return _notmuch_thread_create (threads->query,
675 threads->query->notmuch,
678 threads->query->exclude_terms,
679 threads->query->omit_excluded,
680 threads->query->sort);
684 notmuch_threads_move_to_next (notmuch_threads_t *threads)
686 threads->doc_id_pos++;
690 notmuch_threads_destroy (notmuch_threads_t *threads)
692 talloc_free (threads);
696 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
698 return notmuch_query_count_messages (query, count_out);
702 notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)
704 return _notmuch_query_count_documents (query, "mail", count_out);
708 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
710 notmuch_database_t *notmuch = query->notmuch;
711 Xapian::doccount count = 0;
712 notmuch_status_t status;
714 status = _notmuch_query_ensure_parsed (query);
719 Xapian::Enquire enquire (*notmuch->xapian_db);
720 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
721 _find_prefix ("type"),
723 Xapian::Query final_query, exclude_query;
726 final_query = Xapian::Query (Xapian::Query::OP_AND,
727 mail_query, query->xapian_query);
729 exclude_query = _notmuch_exclude_tags (query);
731 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
732 final_query, exclude_query);
734 enquire.set_weighting_scheme (Xapian::BoolWeight ());
735 enquire.set_docid_order (Xapian::Enquire::ASCENDING);
737 if (_debug_query ()) {
738 fprintf (stderr, "Exclude query is:\n%s\n",
739 exclude_query.get_description ().c_str ());
740 fprintf (stderr, "Final query is:\n%s\n",
741 final_query.get_description ().c_str ());
744 enquire.set_query (final_query);
747 * Set the checkatleast parameter to the number of documents
748 * in the database to make get_matches_estimated() exact.
749 * Set the max parameter to 1 to avoid fetching documents we will discard.
751 mset = enquire.get_mset (0, 1,
752 notmuch->xapian_db->get_doccount ());
754 count = mset.get_matches_estimated ();
756 } catch (const Xapian::Error &error) {
757 _notmuch_database_log (notmuch,
758 "A Xapian exception occurred performing query: %s\n",
759 error.get_msg ().c_str ());
760 _notmuch_database_log_append (notmuch,
761 "Query string was: %s\n",
762 query->query_string);
763 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
767 return NOTMUCH_STATUS_SUCCESS;
771 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
773 return notmuch_query_count_threads (query, count);
777 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
779 notmuch_messages_t *messages;
782 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
785 query->sort = NOTMUCH_SORT_UNSORTED;
786 ret = notmuch_query_search_messages (query, &messages);
790 if (messages == NULL)
791 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
793 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
795 talloc_free (messages);
796 return NOTMUCH_STATUS_OUT_OF_MEMORY;
799 while (notmuch_messages_valid (messages)) {
800 notmuch_message_t *message = notmuch_messages_get (messages);
801 const char *thread_id = notmuch_message_get_thread_id (message);
802 char *thread_id_copy = talloc_strdup (messages, thread_id);
803 if (unlikely (thread_id_copy == NULL)) {
804 notmuch_message_destroy (message);
805 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
808 g_hash_table_insert (hash, thread_id_copy, NULL);
809 notmuch_message_destroy (message);
810 notmuch_messages_move_to_next (messages);
813 *count = g_hash_table_size (hash);
816 g_hash_table_unref (hash);
817 talloc_free (messages);
823 notmuch_query_get_database (const notmuch_query_t *query)
825 return query->notmuch;
829 _notmuch_query_expand (notmuch_database_t *notmuch, const char *field, Xapian::Query subquery,
830 Xapian::Query &output, std::string &msg)
832 std::set<std::string> terms;
833 const std::string term_prefix = _find_prefix (field);
835 if (_debug_query ()) {
836 fprintf (stderr, "Expanding subquery:\n%s\n",
837 subquery.get_description ().c_str ());
841 Xapian::Enquire enquire (*notmuch->xapian_db);
844 enquire.set_weighting_scheme (Xapian::BoolWeight ());
845 enquire.set_query (subquery);
847 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
849 for (Xapian::MSetIterator iterator = mset.begin (); iterator != mset.end (); iterator++) {
850 Xapian::docid doc_id = *iterator;
851 Xapian::Document doc = notmuch->xapian_db->get_document (doc_id);
852 Xapian::TermIterator i = doc.termlist_begin ();
854 for (i.skip_to (term_prefix);
855 i != doc.termlist_end () && ((*i).rfind (term_prefix, 0) == 0); i++) {
859 output = Xapian::Query (Xapian::Query::OP_OR, terms.begin (), terms.end ());
860 if (_debug_query ()) {
861 fprintf (stderr, "Expanded query:\n%s\n",
862 subquery.get_description ().c_str ());
865 } catch (const Xapian::Error &error) {
866 _notmuch_database_log (notmuch,
867 "A Xapian exception occurred expanding query: %s\n",
868 error.get_msg ().c_str ());
869 msg = error.get_msg ();
870 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
873 return NOTMUCH_STATUS_SUCCESS;