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;
32 notmuch_bool_t parsed;
33 Xapian::Query xapian_query;
34 std::set<std::string> terms;
37 typedef struct _notmuch_mset_messages {
38 notmuch_messages_t base;
39 notmuch_database_t *notmuch;
40 Xapian::MSetIterator iterator;
41 Xapian::MSetIterator iterator_end;
42 } notmuch_mset_messages_t;
44 struct _notmuch_doc_id_set {
45 unsigned char *bitmap;
49 #define DOCIDSET_WORD(bit) ((bit) / CHAR_BIT)
50 #define DOCIDSET_BIT(bit) ((bit) % CHAR_BIT)
52 struct visible _notmuch_threads {
53 notmuch_query_t *query;
55 /* The ordered list of doc ids matched by the query. */
57 /* Our iterator's current position in doc_ids. */
58 unsigned int doc_id_pos;
59 /* The set of matched docid's that have not been assigned to a
60 * thread. Initially, this contains every docid in doc_ids. */
61 notmuch_doc_id_set_t match_set;
64 /* We need this in the message functions so forward declare. */
66 _notmuch_doc_id_set_init (void *ctx,
67 notmuch_doc_id_set_t *doc_ids,
73 char *env = getenv ("NOTMUCH_DEBUG_QUERY");
74 return (env && strcmp (env, "") != 0);
77 /* Explicit destructor call for placement new */
79 _notmuch_query_destructor (notmuch_query_t *query) {
80 query->xapian_query.~Query();
81 query->terms.~set<std::string>();
86 notmuch_query_create (notmuch_database_t *notmuch,
87 const char *query_string)
89 notmuch_query_t *query;
92 fprintf (stderr, "Query string is:\n%s\n", query_string);
94 query = talloc (notmuch, notmuch_query_t);
95 if (unlikely (query == NULL))
98 new (&query->xapian_query) Xapian::Query ();
99 new (&query->terms) std::set<std::string> ();
100 query->parsed = FALSE;
102 talloc_set_destructor (query, _notmuch_query_destructor);
104 query->notmuch = notmuch;
106 query->query_string = talloc_strdup (query, query_string);
108 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
110 query->exclude_terms = _notmuch_string_list_create (query);
112 query->omit_excluded = NOTMUCH_EXCLUDE_TRUE;
117 static notmuch_status_t
118 _notmuch_query_ensure_parsed (notmuch_query_t *query)
121 return NOTMUCH_STATUS_SUCCESS;
124 query->xapian_query =
125 query->notmuch->query_parser->
126 parse_query (query->query_string, NOTMUCH_QUERY_PARSER_FLAGS);
128 /* Xapian doesn't support skip_to on terms from a query since
129 * they are unordered, so cache a copy of all terms in
130 * something searchable.
133 for (Xapian::TermIterator t = query->xapian_query.get_terms_begin ();
134 t != query->xapian_query.get_terms_end (); ++t)
135 query->terms.insert (*t);
137 query->parsed = TRUE;
139 } catch (const Xapian::Error &error) {
140 if (!query->notmuch->exception_reported) {
141 _notmuch_database_log (query->notmuch,
142 "A Xapian exception occurred parsing query: %s\n",
143 error.get_msg ().c_str ());
144 _notmuch_database_log_append (query->notmuch,
145 "Query string was: %s\n",
146 query->query_string);
147 query->notmuch->exception_reported = TRUE;
150 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
152 return NOTMUCH_STATUS_SUCCESS;
156 notmuch_query_get_query_string (const notmuch_query_t *query)
158 return query->query_string;
162 notmuch_query_set_omit_excluded (notmuch_query_t *query,
163 notmuch_exclude_t omit_excluded)
165 query->omit_excluded = omit_excluded;
169 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
175 notmuch_query_get_sort (const notmuch_query_t *query)
181 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
183 notmuch_status_t status;
186 status = _notmuch_query_ensure_parsed (query);
187 /* The following is not ideal error handling, but to avoid
188 * breaking the ABI, we can live with it for now. In particular at
189 * least in the notmuch CLI, any syntax error in the query is
190 * caught in a later call to _notmuch_query_ensure_parsed with a
193 * TODO: add status return to this function.
198 term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
199 if (query->terms.count(term) != 0)
200 return; /* XXX report ignoring exclude? */
202 _notmuch_string_list_append (query->exclude_terms, term);
205 /* We end up having to call the destructors explicitly because we had
206 * to use "placement new" in order to initialize C++ objects within a
207 * block that we allocated with talloc. So C++ is making talloc
208 * slightly less simple to use, (we wouldn't need
209 * talloc_set_destructor at all otherwise).
212 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
214 messages->iterator.~MSetIterator ();
215 messages->iterator_end.~MSetIterator ();
220 /* Return a query that matches messages with the excluded tags
221 * registered with query. The caller of this function has to combine the returned
222 * query appropriately.*/
224 _notmuch_exclude_tags (notmuch_query_t *query)
226 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
228 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
230 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
231 exclude_query, Xapian::Query (term->string));
233 return exclude_query;
238 notmuch_query_search_messages_st (notmuch_query_t *query,
239 notmuch_messages_t **out)
241 return notmuch_query_search_messages (query, out);
245 notmuch_query_search_messages (notmuch_query_t *query,
246 notmuch_messages_t **out)
248 return _notmuch_query_search_documents (query, "mail", out);
252 _notmuch_query_search_documents (notmuch_query_t *query,
254 notmuch_messages_t **out)
256 notmuch_database_t *notmuch = query->notmuch;
257 const char *query_string = query->query_string;
258 notmuch_mset_messages_t *messages;
259 notmuch_status_t status;
261 status = _notmuch_query_ensure_parsed (query);
265 messages = talloc (query, notmuch_mset_messages_t);
266 if (unlikely (messages == NULL))
267 return NOTMUCH_STATUS_OUT_OF_MEMORY;
271 messages->base.is_of_list_type = FALSE;
272 messages->base.iterator = NULL;
273 messages->notmuch = notmuch;
274 new (&messages->iterator) Xapian::MSetIterator ();
275 new (&messages->iterator_end) Xapian::MSetIterator ();
277 talloc_set_destructor (messages, _notmuch_messages_destructor);
279 Xapian::Enquire enquire (*notmuch->xapian_db);
280 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
281 _find_prefix ("type"),
283 Xapian::Query final_query, exclude_query;
285 Xapian::MSetIterator iterator;
287 if (strcmp (query_string, "") == 0 ||
288 strcmp (query_string, "*") == 0)
290 final_query = mail_query;
292 final_query = Xapian::Query (Xapian::Query::OP_AND,
293 mail_query, query->xapian_query);
295 messages->base.excluded_doc_ids = NULL;
297 if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
298 exclude_query = _notmuch_exclude_tags (query);
300 if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
301 query->omit_excluded == NOTMUCH_EXCLUDE_ALL)
303 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
304 final_query, exclude_query);
305 } else { /* NOTMUCH_EXCLUDE_FLAG */
306 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
307 exclude_query, final_query);
309 enquire.set_weighting_scheme (Xapian::BoolWeight());
310 enquire.set_query (exclude_query);
312 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
314 GArray *excluded_doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
316 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
317 unsigned int doc_id = *iterator;
318 g_array_append_val (excluded_doc_ids, doc_id);
320 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
321 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
323 g_array_unref (excluded_doc_ids);
328 enquire.set_weighting_scheme (Xapian::BoolWeight());
330 switch (query->sort) {
331 case NOTMUCH_SORT_OLDEST_FIRST:
332 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
334 case NOTMUCH_SORT_NEWEST_FIRST:
335 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
337 case NOTMUCH_SORT_MESSAGE_ID:
338 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
340 case NOTMUCH_SORT_UNSORTED:
344 if (_debug_query ()) {
345 fprintf (stderr, "Exclude query is:\n%s\n",
346 exclude_query.get_description ().c_str ());
347 fprintf (stderr, "Final query is:\n%s\n",
348 final_query.get_description ().c_str ());
351 enquire.set_query (final_query);
353 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
355 messages->iterator = mset.begin ();
356 messages->iterator_end = mset.end ();
358 *out = &messages->base;
359 return NOTMUCH_STATUS_SUCCESS;
361 } catch (const Xapian::Error &error) {
362 _notmuch_database_log (notmuch,
363 "A Xapian exception occurred performing query: %s\n",
364 error.get_msg().c_str());
365 _notmuch_database_log_append (notmuch,
366 "Query string was: %s\n",
367 query->query_string);
369 notmuch->exception_reported = TRUE;
370 talloc_free (messages);
371 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
376 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
378 notmuch_mset_messages_t *mset_messages;
380 mset_messages = (notmuch_mset_messages_t *) messages;
382 return (mset_messages->iterator != mset_messages->iterator_end);
386 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
388 notmuch_mset_messages_t *mset_messages;
390 mset_messages = (notmuch_mset_messages_t *) messages;
392 if (! _notmuch_mset_messages_valid (&mset_messages->base))
395 return *mset_messages->iterator;
399 _notmuch_mset_messages_get (notmuch_messages_t *messages)
401 notmuch_message_t *message;
402 Xapian::docid doc_id;
403 notmuch_private_status_t status;
404 notmuch_mset_messages_t *mset_messages;
406 mset_messages = (notmuch_mset_messages_t *) messages;
408 if (! _notmuch_mset_messages_valid (&mset_messages->base))
411 doc_id = *mset_messages->iterator;
413 message = _notmuch_message_create (mset_messages,
414 mset_messages->notmuch, doc_id,
417 if (message == NULL &&
418 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
420 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
423 if (messages->excluded_doc_ids &&
424 _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
425 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
431 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
433 notmuch_mset_messages_t *mset_messages;
435 mset_messages = (notmuch_mset_messages_t *) messages;
437 mset_messages->iterator++;
440 static notmuch_bool_t
441 _notmuch_doc_id_set_init (void *ctx,
442 notmuch_doc_id_set_t *doc_ids,
445 unsigned int max = 0;
446 unsigned char *bitmap;
448 for (unsigned int i = 0; i < arr->len; i++)
449 max = MAX(max, g_array_index (arr, unsigned int, i));
450 bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD(max) + 1);
455 doc_ids->bitmap = bitmap;
456 doc_ids->bound = max + 1;
458 for (unsigned int i = 0; i < arr->len; i++) {
459 unsigned int doc_id = g_array_index (arr, unsigned int, i);
460 bitmap[DOCIDSET_WORD(doc_id)] |= 1 << DOCIDSET_BIT(doc_id);
467 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
470 if (doc_id >= doc_ids->bound)
472 return doc_ids->bitmap[DOCIDSET_WORD(doc_id)] & (1 << DOCIDSET_BIT(doc_id));
476 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
479 if (doc_id < doc_ids->bound)
480 doc_ids->bitmap[DOCIDSET_WORD(doc_id)] &= ~(1 << DOCIDSET_BIT(doc_id));
483 /* Glib objects force use to use a talloc destructor as well, (but not
484 * nearly as ugly as the for messages due to C++ objects). At
485 * this point, I'd really like to have some talloc-friendly
486 * equivalents for the few pieces of glib that I'm using. */
488 _notmuch_threads_destructor (notmuch_threads_t *threads)
490 if (threads->doc_ids)
491 g_array_unref (threads->doc_ids);
497 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
499 return notmuch_query_search_threads(query, out);
503 notmuch_query_search_threads (notmuch_query_t *query,
504 notmuch_threads_t **out)
506 notmuch_threads_t *threads;
507 notmuch_messages_t *messages;
508 notmuch_status_t status;
510 threads = talloc (query, notmuch_threads_t);
512 return NOTMUCH_STATUS_OUT_OF_MEMORY;
513 threads->doc_ids = NULL;
514 talloc_set_destructor (threads, _notmuch_threads_destructor);
516 threads->query = query;
518 status = notmuch_query_search_messages (query, &messages);
520 talloc_free (threads);
524 threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
525 while (notmuch_messages_valid (messages)) {
526 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
527 g_array_append_val (threads->doc_ids, doc_id);
528 notmuch_messages_move_to_next (messages);
530 threads->doc_id_pos = 0;
532 talloc_free (messages);
534 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
536 talloc_free (threads);
537 return NOTMUCH_STATUS_OUT_OF_MEMORY;
541 return NOTMUCH_STATUS_SUCCESS;
545 notmuch_query_destroy (notmuch_query_t *query)
551 notmuch_threads_valid (notmuch_threads_t *threads)
558 while (threads->doc_id_pos < threads->doc_ids->len) {
559 doc_id = g_array_index (threads->doc_ids, unsigned int,
560 threads->doc_id_pos);
561 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
564 threads->doc_id_pos++;
567 return threads->doc_id_pos < threads->doc_ids->len;
571 notmuch_threads_get (notmuch_threads_t *threads)
575 if (! notmuch_threads_valid (threads))
578 doc_id = g_array_index (threads->doc_ids, unsigned int,
579 threads->doc_id_pos);
580 return _notmuch_thread_create (threads->query,
581 threads->query->notmuch,
584 threads->query->exclude_terms,
585 threads->query->omit_excluded,
586 threads->query->sort);
590 notmuch_threads_move_to_next (notmuch_threads_t *threads)
592 threads->doc_id_pos++;
596 notmuch_threads_destroy (notmuch_threads_t *threads)
598 talloc_free (threads);
602 notmuch_query_count_messages (notmuch_query_t *query)
604 notmuch_status_t status;
607 status = notmuch_query_count_messages_st (query, &count);
608 return status ? 0 : count;
612 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
614 return _notmuch_query_count_documents (query, "mail", count_out);
618 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
620 notmuch_database_t *notmuch = query->notmuch;
621 const char *query_string = query->query_string;
622 Xapian::doccount count = 0;
623 notmuch_status_t status;
625 status = _notmuch_query_ensure_parsed (query);
630 Xapian::Enquire enquire (*notmuch->xapian_db);
631 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
632 _find_prefix ("type"),
634 Xapian::Query final_query, exclude_query;
637 if (strcmp (query_string, "") == 0 ||
638 strcmp (query_string, "*") == 0)
640 final_query = mail_query;
642 final_query = Xapian::Query (Xapian::Query::OP_AND,
643 mail_query, query->xapian_query);
646 exclude_query = _notmuch_exclude_tags (query);
648 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
649 final_query, exclude_query);
651 enquire.set_weighting_scheme(Xapian::BoolWeight());
652 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
654 if (_debug_query ()) {
655 fprintf (stderr, "Exclude query is:\n%s\n",
656 exclude_query.get_description ().c_str ());
657 fprintf (stderr, "Final query is:\n%s\n",
658 final_query.get_description ().c_str ());
661 enquire.set_query (final_query);
664 * Set the checkatleast parameter to the number of documents
665 * in the database to make get_matches_estimated() exact.
666 * Set the max parameter to 0 to avoid fetching documents we will discard.
668 mset = enquire.get_mset (0, 0,
669 notmuch->xapian_db->get_doccount ());
671 count = mset.get_matches_estimated();
673 } catch (const Xapian::Error &error) {
674 _notmuch_database_log (notmuch,
675 "A Xapian exception occurred performing query: %s\n",
676 error.get_msg().c_str());
677 _notmuch_database_log_append (notmuch,
678 "Query string was: %s\n",
679 query->query_string);
680 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
684 return NOTMUCH_STATUS_SUCCESS;
688 notmuch_query_count_threads (notmuch_query_t *query)
690 notmuch_status_t status;
693 status = notmuch_query_count_threads_st (query, &count);
694 return status ? 0 : count;
698 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
700 notmuch_messages_t *messages;
703 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
706 query->sort = NOTMUCH_SORT_UNSORTED;
707 ret = notmuch_query_search_messages (query, &messages);
711 if (messages == NULL)
712 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
714 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
716 talloc_free (messages);
717 return NOTMUCH_STATUS_OUT_OF_MEMORY;
720 while (notmuch_messages_valid (messages)) {
721 notmuch_message_t *message = notmuch_messages_get (messages);
722 const char *thread_id = notmuch_message_get_thread_id (message);
723 char *thread_id_copy = talloc_strdup (messages, thread_id);
724 if (unlikely (thread_id_copy == NULL)) {
725 notmuch_message_destroy (message);
726 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
729 g_hash_table_insert (hash, thread_id_copy, NULL);
730 notmuch_message_destroy (message);
731 notmuch_messages_move_to_next (messages);
734 *count = g_hash_table_size (hash);
737 g_hash_table_unref (hash);
738 talloc_free (messages);
744 notmuch_query_get_database (const notmuch_query_t *query)
746 return query->notmuch;