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;
169 _notmuch_query_cache_terms (notmuch_query_t *query)
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.
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);
182 _notmuch_query_string_to_xapian_query (notmuch_database_t *notmuch,
183 std::string query_string,
184 Xapian::Query &output,
188 if (query_string == "" || query_string == "*") {
189 output = Xapian::Query::MatchAll;
192 notmuch->query_parser->
193 parse_query (query_string, NOTMUCH_QUERY_PARSER_FLAGS);
195 } catch (const Xapian::Error &error) {
196 if (! notmuch->exception_reported) {
197 _notmuch_database_log (notmuch,
198 "A Xapian exception occurred parsing query: %s\n",
199 error.get_msg ().c_str ());
200 _notmuch_database_log_append (notmuch,
201 "Query string was: %s\n",
202 query_string.c_str ());
203 notmuch->exception_reported = true;
206 msg = error.get_msg ();
207 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
209 return NOTMUCH_STATUS_SUCCESS;
212 static notmuch_status_t
213 _notmuch_query_ensure_parsed_xapian (notmuch_query_t *query)
215 notmuch_status_t status;
216 std::string msg; /* ignored */
218 status = _notmuch_query_string_to_xapian_query (query->notmuch, query->query_string,
219 query->xapian_query, msg);
223 query->parsed = true;
225 _notmuch_query_cache_terms (query);
227 return NOTMUCH_STATUS_SUCCESS;
231 static notmuch_status_t
232 _notmuch_query_ensure_parsed_sexpr (notmuch_query_t *query)
234 notmuch_status_t status;
237 return NOTMUCH_STATUS_SUCCESS;
239 status = _notmuch_sexp_string_to_xapian_query (query->notmuch, query->query_string,
240 query->xapian_query);
244 _notmuch_query_cache_terms (query);
245 return NOTMUCH_STATUS_SUCCESS;
249 static notmuch_status_t
250 _notmuch_query_ensure_parsed (notmuch_query_t *query)
253 return NOTMUCH_STATUS_SUCCESS;
256 if (query->syntax == NOTMUCH_QUERY_SYNTAX_SEXP)
257 return _notmuch_query_ensure_parsed_sexpr (query);
260 return _notmuch_query_ensure_parsed_xapian (query);
264 notmuch_query_get_query_string (const notmuch_query_t *query)
266 return query->query_string;
270 notmuch_query_set_omit_excluded (notmuch_query_t *query,
271 notmuch_exclude_t omit_excluded)
273 query->omit_excluded = omit_excluded;
277 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
283 notmuch_query_get_sort (const notmuch_query_t *query)
289 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
291 notmuch_status_t status;
294 status = _notmuch_query_ensure_parsed (query);
298 term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
299 if (query->terms.count (term) != 0)
300 return NOTMUCH_STATUS_IGNORED;
302 _notmuch_string_list_append (query->exclude_terms, term);
303 return NOTMUCH_STATUS_SUCCESS;
306 /* We end up having to call the destructors explicitly because we had
307 * to use "placement new" in order to initialize C++ objects within a
308 * block that we allocated with talloc. So C++ is making talloc
309 * slightly less simple to use, (we wouldn't need
310 * talloc_set_destructor at all otherwise).
313 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
315 messages->iterator.~MSetIterator ();
316 messages->iterator_end.~MSetIterator ();
321 /* Return a query that matches messages with the excluded tags
322 * registered with query. The caller of this function has to combine the returned
323 * query appropriately.*/
325 _notmuch_exclude_tags (notmuch_query_t *query)
327 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
329 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
331 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
332 exclude_query, Xapian::Query (term->string));
334 return exclude_query;
339 notmuch_query_search_messages_st (notmuch_query_t *query,
340 notmuch_messages_t **out)
342 return notmuch_query_search_messages (query, out);
346 notmuch_query_search_messages (notmuch_query_t *query,
347 notmuch_messages_t **out)
349 return _notmuch_query_search_documents (query, "mail", out);
353 _notmuch_query_search_documents (notmuch_query_t *query,
355 notmuch_messages_t **out)
357 notmuch_database_t *notmuch = query->notmuch;
358 notmuch_mset_messages_t *messages;
359 notmuch_status_t status;
361 status = _notmuch_query_ensure_parsed (query);
365 messages = talloc (query, notmuch_mset_messages_t);
366 if (unlikely (messages == NULL))
367 return NOTMUCH_STATUS_OUT_OF_MEMORY;
371 messages->base.is_of_list_type = false;
372 messages->base.iterator = NULL;
373 messages->notmuch = notmuch;
374 new (&messages->iterator) Xapian::MSetIterator ();
375 new (&messages->iterator_end) Xapian::MSetIterator ();
377 talloc_set_destructor (messages, _notmuch_messages_destructor);
379 Xapian::Enquire enquire (*notmuch->xapian_db);
380 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
381 _find_prefix ("type"),
383 Xapian::Query final_query, exclude_query;
385 Xapian::MSetIterator iterator;
387 final_query = Xapian::Query (Xapian::Query::OP_AND,
388 mail_query, query->xapian_query);
390 messages->base.excluded_doc_ids = NULL;
392 if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
393 exclude_query = _notmuch_exclude_tags (query);
395 if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
396 query->omit_excluded == NOTMUCH_EXCLUDE_ALL) {
397 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
398 final_query, exclude_query);
399 } else { /* NOTMUCH_EXCLUDE_FLAG */
400 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
401 exclude_query, final_query);
403 enquire.set_weighting_scheme (Xapian::BoolWeight ());
404 enquire.set_query (exclude_query);
406 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
408 GArray *excluded_doc_ids = g_array_new (false, false, sizeof (unsigned int));
410 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
411 unsigned int doc_id = *iterator;
412 g_array_append_val (excluded_doc_ids, doc_id);
414 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
415 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
417 g_array_unref (excluded_doc_ids);
422 enquire.set_weighting_scheme (Xapian::BoolWeight ());
424 switch (query->sort) {
425 case NOTMUCH_SORT_OLDEST_FIRST:
426 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, false);
428 case NOTMUCH_SORT_NEWEST_FIRST:
429 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, true);
431 case NOTMUCH_SORT_MESSAGE_ID:
432 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, false);
434 case NOTMUCH_SORT_UNSORTED:
438 if (_debug_query ()) {
439 fprintf (stderr, "Exclude query is:\n%s\n",
440 exclude_query.get_description ().c_str ());
441 fprintf (stderr, "Final query is:\n%s\n",
442 final_query.get_description ().c_str ());
445 enquire.set_query (final_query);
447 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
449 messages->iterator = mset.begin ();
450 messages->iterator_end = mset.end ();
452 *out = &messages->base;
453 return NOTMUCH_STATUS_SUCCESS;
455 } catch (const Xapian::Error &error) {
456 _notmuch_database_log (notmuch,
457 "A Xapian exception occurred performing query: %s\n",
458 error.get_msg ().c_str ());
459 _notmuch_database_log_append (notmuch,
460 "Query string was: %s\n",
461 query->query_string);
463 notmuch->exception_reported = true;
464 talloc_free (messages);
465 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
470 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
472 notmuch_mset_messages_t *mset_messages;
474 mset_messages = (notmuch_mset_messages_t *) messages;
476 return (mset_messages->iterator != mset_messages->iterator_end);
480 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
482 notmuch_mset_messages_t *mset_messages;
484 mset_messages = (notmuch_mset_messages_t *) messages;
486 if (! _notmuch_mset_messages_valid (&mset_messages->base))
489 return *mset_messages->iterator;
493 _notmuch_mset_messages_get (notmuch_messages_t *messages)
495 notmuch_message_t *message;
496 Xapian::docid doc_id;
497 notmuch_private_status_t status;
498 notmuch_mset_messages_t *mset_messages;
500 mset_messages = (notmuch_mset_messages_t *) messages;
502 if (! _notmuch_mset_messages_valid (&mset_messages->base))
505 doc_id = *mset_messages->iterator;
507 message = _notmuch_message_create (mset_messages,
508 mset_messages->notmuch, doc_id,
511 if (message == NULL &&
512 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
513 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
516 if (messages->excluded_doc_ids &&
517 _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
518 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
524 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
526 notmuch_mset_messages_t *mset_messages;
528 mset_messages = (notmuch_mset_messages_t *) messages;
530 mset_messages->iterator++;
534 _notmuch_doc_id_set_init (void *ctx,
535 notmuch_doc_id_set_t *doc_ids,
538 unsigned int max = 0;
539 unsigned char *bitmap;
541 for (unsigned int i = 0; i < arr->len; i++)
542 max = MAX (max, g_array_index (arr, unsigned int, i));
543 bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD (max) + 1);
548 doc_ids->bitmap = bitmap;
549 doc_ids->bound = max + 1;
551 for (unsigned int i = 0; i < arr->len; i++) {
552 unsigned int doc_id = g_array_index (arr, unsigned int, i);
553 bitmap[DOCIDSET_WORD (doc_id)] |= 1 << DOCIDSET_BIT (doc_id);
560 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
563 if (doc_id >= doc_ids->bound)
565 return doc_ids->bitmap[DOCIDSET_WORD (doc_id)] & (1 << DOCIDSET_BIT (doc_id));
569 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
572 if (doc_id < doc_ids->bound)
573 doc_ids->bitmap[DOCIDSET_WORD (doc_id)] &= ~(1 << DOCIDSET_BIT (doc_id));
576 /* Glib objects force use to use a talloc destructor as well, (but not
577 * nearly as ugly as the for messages due to C++ objects). At
578 * this point, I'd really like to have some talloc-friendly
579 * equivalents for the few pieces of glib that I'm using. */
581 _notmuch_threads_destructor (notmuch_threads_t *threads)
583 if (threads->doc_ids)
584 g_array_unref (threads->doc_ids);
590 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
592 return notmuch_query_search_threads (query, out);
596 notmuch_query_search_threads (notmuch_query_t *query,
597 notmuch_threads_t **out)
599 notmuch_threads_t *threads;
600 notmuch_messages_t *messages;
601 notmuch_status_t status;
603 threads = talloc (query, notmuch_threads_t);
605 return NOTMUCH_STATUS_OUT_OF_MEMORY;
606 threads->doc_ids = NULL;
607 talloc_set_destructor (threads, _notmuch_threads_destructor);
609 threads->query = query;
611 status = notmuch_query_search_messages (query, &messages);
613 talloc_free (threads);
617 threads->doc_ids = g_array_new (false, false, sizeof (unsigned int));
618 while (notmuch_messages_valid (messages)) {
619 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
620 g_array_append_val (threads->doc_ids, doc_id);
621 notmuch_messages_move_to_next (messages);
623 threads->doc_id_pos = 0;
625 talloc_free (messages);
627 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
629 talloc_free (threads);
630 return NOTMUCH_STATUS_OUT_OF_MEMORY;
634 return NOTMUCH_STATUS_SUCCESS;
638 notmuch_query_destroy (notmuch_query_t *query)
644 notmuch_threads_valid (notmuch_threads_t *threads)
651 while (threads->doc_id_pos < threads->doc_ids->len) {
652 doc_id = g_array_index (threads->doc_ids, unsigned int,
653 threads->doc_id_pos);
654 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
657 threads->doc_id_pos++;
660 return threads->doc_id_pos < threads->doc_ids->len;
664 notmuch_threads_get (notmuch_threads_t *threads)
668 if (! notmuch_threads_valid (threads))
671 doc_id = g_array_index (threads->doc_ids, unsigned int,
672 threads->doc_id_pos);
673 return _notmuch_thread_create (threads->query,
674 threads->query->notmuch,
677 threads->query->exclude_terms,
678 threads->query->omit_excluded,
679 threads->query->sort);
683 notmuch_threads_move_to_next (notmuch_threads_t *threads)
685 threads->doc_id_pos++;
689 notmuch_threads_destroy (notmuch_threads_t *threads)
691 talloc_free (threads);
695 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
697 return notmuch_query_count_messages (query, count_out);
701 notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)
703 return _notmuch_query_count_documents (query, "mail", count_out);
707 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
709 notmuch_database_t *notmuch = query->notmuch;
710 Xapian::doccount count = 0;
711 notmuch_status_t status;
713 status = _notmuch_query_ensure_parsed (query);
718 Xapian::Enquire enquire (*notmuch->xapian_db);
719 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
720 _find_prefix ("type"),
722 Xapian::Query final_query, exclude_query;
725 final_query = Xapian::Query (Xapian::Query::OP_AND,
726 mail_query, query->xapian_query);
728 exclude_query = _notmuch_exclude_tags (query);
730 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
731 final_query, exclude_query);
733 enquire.set_weighting_scheme (Xapian::BoolWeight ());
734 enquire.set_docid_order (Xapian::Enquire::ASCENDING);
736 if (_debug_query ()) {
737 fprintf (stderr, "Exclude query is:\n%s\n",
738 exclude_query.get_description ().c_str ());
739 fprintf (stderr, "Final query is:\n%s\n",
740 final_query.get_description ().c_str ());
743 enquire.set_query (final_query);
746 * Set the checkatleast parameter to the number of documents
747 * in the database to make get_matches_estimated() exact.
748 * Set the max parameter to 1 to avoid fetching documents we will discard.
750 mset = enquire.get_mset (0, 1,
751 notmuch->xapian_db->get_doccount ());
753 count = mset.get_matches_estimated ();
755 } catch (const Xapian::Error &error) {
756 _notmuch_database_log (notmuch,
757 "A Xapian exception occurred performing query: %s\n",
758 error.get_msg ().c_str ());
759 _notmuch_database_log_append (notmuch,
760 "Query string was: %s\n",
761 query->query_string);
762 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
766 return NOTMUCH_STATUS_SUCCESS;
770 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
772 return notmuch_query_count_threads (query, count);
776 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
778 notmuch_messages_t *messages;
781 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
784 query->sort = NOTMUCH_SORT_UNSORTED;
785 ret = notmuch_query_search_messages (query, &messages);
789 if (messages == NULL)
790 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
792 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
794 talloc_free (messages);
795 return NOTMUCH_STATUS_OUT_OF_MEMORY;
798 while (notmuch_messages_valid (messages)) {
799 notmuch_message_t *message = notmuch_messages_get (messages);
800 const char *thread_id = notmuch_message_get_thread_id (message);
801 char *thread_id_copy = talloc_strdup (messages, thread_id);
802 if (unlikely (thread_id_copy == NULL)) {
803 notmuch_message_destroy (message);
804 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
807 g_hash_table_insert (hash, thread_id_copy, NULL);
808 notmuch_message_destroy (message);
809 notmuch_messages_move_to_next (messages);
812 *count = g_hash_table_size (hash);
815 g_hash_table_unref (hash);
816 talloc_free (messages);
822 notmuch_query_get_database (const notmuch_query_t *query)
824 return query->notmuch;
828 _notmuch_query_expand (notmuch_database_t *notmuch, const char *field, Xapian::Query subquery,
829 Xapian::Query &output, std::string &msg)
831 std::set<std::string> terms;
832 const std::string term_prefix = _find_prefix (field);
834 if (_debug_query ()) {
835 fprintf (stderr, "Expanding subquery:\n%s\n",
836 subquery.get_description ().c_str ());
840 Xapian::Enquire enquire (*notmuch->xapian_db);
843 enquire.set_weighting_scheme (Xapian::BoolWeight ());
844 enquire.set_query (subquery);
846 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
848 for (Xapian::MSetIterator iterator = mset.begin (); iterator != mset.end (); iterator++) {
849 Xapian::docid doc_id = *iterator;
850 Xapian::Document doc = notmuch->xapian_db->get_document (doc_id);
851 Xapian::TermIterator i = doc.termlist_begin ();
853 for (i.skip_to (term_prefix);
854 i != doc.termlist_end () && ((*i).rfind (term_prefix, 0) == 0); i++) {
858 output = Xapian::Query (Xapian::Query::OP_OR, terms.begin (), terms.end ());
859 if (_debug_query ()) {
860 fprintf (stderr, "Expanded query:\n%s\n",
861 subquery.get_description ().c_str ());
864 } catch (const Xapian::Error &error) {
865 _notmuch_database_log (notmuch,
866 "A Xapian exception occurred expanding query: %s\n",
867 error.get_msg ().c_str ());
868 msg = error.get_msg ();
869 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
872 return NOTMUCH_STATUS_SUCCESS;