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 */
30 struct _notmuch_query {
31 notmuch_database_t *notmuch;
32 const char *query_string;
34 notmuch_string_list_t *exclude_terms;
35 notmuch_exclude_t omit_excluded;
37 notmuch_query_syntax_t syntax;
38 Xapian::Query xapian_query;
39 std::set<std::string> terms;
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;
49 struct _notmuch_doc_id_set {
50 unsigned char *bitmap;
54 #define DOCIDSET_WORD(bit) ((bit) / CHAR_BIT)
55 #define DOCIDSET_BIT(bit) ((bit) % CHAR_BIT)
57 struct _notmuch_threads {
58 notmuch_query_t *query;
60 /* The ordered list of doc ids matched by the query. */
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;
69 /* We need this in the message functions so forward declare. */
71 _notmuch_doc_id_set_init (void *ctx,
72 notmuch_doc_id_set_t *doc_ids,
78 char *env = getenv ("NOTMUCH_DEBUG_QUERY");
80 return (env && strcmp (env, "") != 0);
83 /* Explicit destructor call for placement new */
85 _notmuch_query_destructor (notmuch_query_t *query)
87 query->xapian_query.~Query();
88 query->terms.~set<std::string>();
92 static notmuch_query_t *
93 _notmuch_query_constructor (notmuch_database_t *notmuch,
94 const char *query_string)
96 notmuch_query_t *query;
99 fprintf (stderr, "Query string is:\n%s\n", query_string);
101 query = talloc (notmuch, notmuch_query_t);
102 if (unlikely (query == NULL))
105 new (&query->xapian_query) Xapian::Query ();
106 new (&query->terms) std::set<std::string> ();
107 query->parsed = false;
109 talloc_set_destructor (query, _notmuch_query_destructor);
111 query->notmuch = notmuch;
114 query->query_string = talloc_strdup (query, query_string);
116 query->query_string = NULL;
118 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
120 query->exclude_terms = _notmuch_string_list_create (query);
122 query->omit_excluded = NOTMUCH_EXCLUDE_TRUE;
128 notmuch_query_create (notmuch_database_t *notmuch,
129 const char *query_string)
132 notmuch_query_t *query;
133 notmuch_status_t status;
135 status = notmuch_query_create_with_syntax (notmuch, query_string,
136 NOTMUCH_QUERY_SYNTAX_XAPIAN,
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)
151 notmuch_query_t *query;
154 return NOTMUCH_STATUS_NULL_POINTER;
156 query = _notmuch_query_constructor (notmuch, query_string);
158 return NOTMUCH_STATUS_OUT_OF_MEMORY;
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;
165 query->syntax = syntax;
169 return NOTMUCH_STATUS_SUCCESS;
172 static notmuch_status_t
173 _notmuch_query_ensure_parsed_xapian (notmuch_query_t *query)
176 query->xapian_query =
177 query->notmuch->query_parser->
178 parse_query (query->query_string, NOTMUCH_QUERY_PARSER_FLAGS);
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.
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);
189 query->parsed = true;
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;
202 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
204 return NOTMUCH_STATUS_SUCCESS;
207 static notmuch_status_t
208 _notmuch_query_ensure_parsed_sexpr (notmuch_query_t *query)
211 return NOTMUCH_STATUS_SUCCESS;
213 query->xapian_query = Xapian::Query::MatchAll;
214 return NOTMUCH_STATUS_SUCCESS;
217 static notmuch_status_t
218 _notmuch_query_ensure_parsed (notmuch_query_t *query)
221 return NOTMUCH_STATUS_SUCCESS;
224 if (query->syntax == NOTMUCH_QUERY_SYNTAX_SEXP)
225 return _notmuch_query_ensure_parsed_sexpr (query);
228 return _notmuch_query_ensure_parsed_xapian (query);
232 notmuch_query_get_query_string (const notmuch_query_t *query)
234 return query->query_string;
238 notmuch_query_set_omit_excluded (notmuch_query_t *query,
239 notmuch_exclude_t omit_excluded)
241 query->omit_excluded = omit_excluded;
245 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
251 notmuch_query_get_sort (const notmuch_query_t *query)
257 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
259 notmuch_status_t status;
262 status = _notmuch_query_ensure_parsed (query);
266 term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
267 if (query->terms.count (term) != 0)
268 return NOTMUCH_STATUS_IGNORED;
270 _notmuch_string_list_append (query->exclude_terms, term);
271 return NOTMUCH_STATUS_SUCCESS;
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).
281 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
283 messages->iterator.~MSetIterator ();
284 messages->iterator_end.~MSetIterator ();
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.*/
293 _notmuch_exclude_tags (notmuch_query_t *query)
295 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
297 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
299 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
300 exclude_query, Xapian::Query (term->string));
302 return exclude_query;
307 notmuch_query_search_messages_st (notmuch_query_t *query,
308 notmuch_messages_t **out)
310 return notmuch_query_search_messages (query, out);
314 notmuch_query_search_messages (notmuch_query_t *query,
315 notmuch_messages_t **out)
317 return _notmuch_query_search_documents (query, "mail", out);
321 _notmuch_query_search_documents (notmuch_query_t *query,
323 notmuch_messages_t **out)
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;
330 status = _notmuch_query_ensure_parsed (query);
334 messages = talloc (query, notmuch_mset_messages_t);
335 if (unlikely (messages == NULL))
336 return NOTMUCH_STATUS_OUT_OF_MEMORY;
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 ();
346 talloc_set_destructor (messages, _notmuch_messages_destructor);
348 Xapian::Enquire enquire (*notmuch->xapian_db);
349 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
350 _find_prefix ("type"),
352 Xapian::Query final_query, exclude_query;
354 Xapian::MSetIterator iterator;
356 if (strcmp (query_string, "") == 0 ||
357 strcmp (query_string, "*") == 0) {
358 final_query = mail_query;
360 final_query = Xapian::Query (Xapian::Query::OP_AND,
361 mail_query, query->xapian_query);
363 messages->base.excluded_doc_ids = NULL;
365 if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
366 exclude_query = _notmuch_exclude_tags (query);
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);
376 enquire.set_weighting_scheme (Xapian::BoolWeight ());
377 enquire.set_query (exclude_query);
379 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
381 GArray *excluded_doc_ids = g_array_new (false, false, sizeof (unsigned int));
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);
387 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
388 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
390 g_array_unref (excluded_doc_ids);
395 enquire.set_weighting_scheme (Xapian::BoolWeight ());
397 switch (query->sort) {
398 case NOTMUCH_SORT_OLDEST_FIRST:
399 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, false);
401 case NOTMUCH_SORT_NEWEST_FIRST:
402 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, true);
404 case NOTMUCH_SORT_MESSAGE_ID:
405 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, false);
407 case NOTMUCH_SORT_UNSORTED:
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 ());
418 enquire.set_query (final_query);
420 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
422 messages->iterator = mset.begin ();
423 messages->iterator_end = mset.end ();
425 *out = &messages->base;
426 return NOTMUCH_STATUS_SUCCESS;
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);
436 notmuch->exception_reported = true;
437 talloc_free (messages);
438 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
443 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
445 notmuch_mset_messages_t *mset_messages;
447 mset_messages = (notmuch_mset_messages_t *) messages;
449 return (mset_messages->iterator != mset_messages->iterator_end);
453 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
455 notmuch_mset_messages_t *mset_messages;
457 mset_messages = (notmuch_mset_messages_t *) messages;
459 if (! _notmuch_mset_messages_valid (&mset_messages->base))
462 return *mset_messages->iterator;
466 _notmuch_mset_messages_get (notmuch_messages_t *messages)
468 notmuch_message_t *message;
469 Xapian::docid doc_id;
470 notmuch_private_status_t status;
471 notmuch_mset_messages_t *mset_messages;
473 mset_messages = (notmuch_mset_messages_t *) messages;
475 if (! _notmuch_mset_messages_valid (&mset_messages->base))
478 doc_id = *mset_messages->iterator;
480 message = _notmuch_message_create (mset_messages,
481 mset_messages->notmuch, doc_id,
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");
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);
497 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
499 notmuch_mset_messages_t *mset_messages;
501 mset_messages = (notmuch_mset_messages_t *) messages;
503 mset_messages->iterator++;
507 _notmuch_doc_id_set_init (void *ctx,
508 notmuch_doc_id_set_t *doc_ids,
511 unsigned int max = 0;
512 unsigned char *bitmap;
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);
521 doc_ids->bitmap = bitmap;
522 doc_ids->bound = max + 1;
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);
533 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
536 if (doc_id >= doc_ids->bound)
538 return doc_ids->bitmap[DOCIDSET_WORD (doc_id)] & (1 << DOCIDSET_BIT (doc_id));
542 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
545 if (doc_id < doc_ids->bound)
546 doc_ids->bitmap[DOCIDSET_WORD (doc_id)] &= ~(1 << DOCIDSET_BIT (doc_id));
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. */
554 _notmuch_threads_destructor (notmuch_threads_t *threads)
556 if (threads->doc_ids)
557 g_array_unref (threads->doc_ids);
563 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
565 return notmuch_query_search_threads (query, out);
569 notmuch_query_search_threads (notmuch_query_t *query,
570 notmuch_threads_t **out)
572 notmuch_threads_t *threads;
573 notmuch_messages_t *messages;
574 notmuch_status_t status;
576 threads = talloc (query, notmuch_threads_t);
578 return NOTMUCH_STATUS_OUT_OF_MEMORY;
579 threads->doc_ids = NULL;
580 talloc_set_destructor (threads, _notmuch_threads_destructor);
582 threads->query = query;
584 status = notmuch_query_search_messages (query, &messages);
586 talloc_free (threads);
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);
596 threads->doc_id_pos = 0;
598 talloc_free (messages);
600 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
602 talloc_free (threads);
603 return NOTMUCH_STATUS_OUT_OF_MEMORY;
607 return NOTMUCH_STATUS_SUCCESS;
611 notmuch_query_destroy (notmuch_query_t *query)
617 notmuch_threads_valid (notmuch_threads_t *threads)
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))
630 threads->doc_id_pos++;
633 return threads->doc_id_pos < threads->doc_ids->len;
637 notmuch_threads_get (notmuch_threads_t *threads)
641 if (! notmuch_threads_valid (threads))
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,
650 threads->query->exclude_terms,
651 threads->query->omit_excluded,
652 threads->query->sort);
656 notmuch_threads_move_to_next (notmuch_threads_t *threads)
658 threads->doc_id_pos++;
662 notmuch_threads_destroy (notmuch_threads_t *threads)
664 talloc_free (threads);
668 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
670 return notmuch_query_count_messages (query, count_out);
674 notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)
676 return _notmuch_query_count_documents (query, "mail", count_out);
680 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
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;
687 status = _notmuch_query_ensure_parsed (query);
692 Xapian::Enquire enquire (*notmuch->xapian_db);
693 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
694 _find_prefix ("type"),
696 Xapian::Query final_query, exclude_query;
699 if (strcmp (query_string, "") == 0 ||
700 strcmp (query_string, "*") == 0) {
701 final_query = mail_query;
703 final_query = Xapian::Query (Xapian::Query::OP_AND,
704 mail_query, query->xapian_query);
707 exclude_query = _notmuch_exclude_tags (query);
709 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
710 final_query, exclude_query);
712 enquire.set_weighting_scheme (Xapian::BoolWeight ());
713 enquire.set_docid_order (Xapian::Enquire::ASCENDING);
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 ());
722 enquire.set_query (final_query);
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.
729 mset = enquire.get_mset (0, 1,
730 notmuch->xapian_db->get_doccount ());
732 count = mset.get_matches_estimated ();
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;
745 return NOTMUCH_STATUS_SUCCESS;
749 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
751 return notmuch_query_count_threads (query, count);
755 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
757 notmuch_messages_t *messages;
760 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
763 query->sort = NOTMUCH_SORT_UNSORTED;
764 ret = notmuch_query_search_messages (query, &messages);
768 if (messages == NULL)
769 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
771 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
773 talloc_free (messages);
774 return NOTMUCH_STATUS_OUT_OF_MEMORY;
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;
786 g_hash_table_insert (hash, thread_id_copy, NULL);
787 notmuch_message_destroy (message);
788 notmuch_messages_move_to_next (messages);
791 *count = g_hash_table_size (hash);
794 g_hash_table_unref (hash);
795 talloc_free (messages);
801 notmuch_query_get_database (const notmuch_query_t *query)
803 return query->notmuch;