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 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 _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");
75 return (env && strcmp (env, "") != 0);
78 /* Explicit destructor call for placement new */
80 _notmuch_query_destructor (notmuch_query_t *query)
82 query->xapian_query.~Query();
83 query->terms.~set<std::string>();
87 static notmuch_query_t *
88 _notmuch_query_constructor (notmuch_database_t *notmuch,
89 const char *query_string)
91 notmuch_query_t *query;
94 fprintf (stderr, "Query string is:\n%s\n", query_string);
96 query = talloc (notmuch, notmuch_query_t);
97 if (unlikely (query == NULL))
100 new (&query->xapian_query) Xapian::Query ();
101 new (&query->terms) std::set<std::string> ();
102 query->parsed = false;
104 talloc_set_destructor (query, _notmuch_query_destructor);
106 query->notmuch = notmuch;
108 query->query_string = talloc_strdup (query, query_string);
110 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
112 query->exclude_terms = _notmuch_string_list_create (query);
114 query->omit_excluded = NOTMUCH_EXCLUDE_TRUE;
120 notmuch_query_create (notmuch_database_t *notmuch,
121 const char *query_string)
124 notmuch_query_t *query = _notmuch_query_constructor (notmuch, query_string);
132 static notmuch_status_t
133 _notmuch_query_ensure_parsed (notmuch_query_t *query)
136 return NOTMUCH_STATUS_SUCCESS;
139 query->xapian_query =
140 query->notmuch->query_parser->
141 parse_query (query->query_string, NOTMUCH_QUERY_PARSER_FLAGS);
143 /* Xapian doesn't support skip_to on terms from a query since
144 * they are unordered, so cache a copy of all terms in
145 * something searchable.
148 for (Xapian::TermIterator t = query->xapian_query.get_terms_begin ();
149 t != query->xapian_query.get_terms_end (); ++t)
150 query->terms.insert (*t);
152 query->parsed = true;
154 } catch (const Xapian::Error &error) {
155 if (! query->notmuch->exception_reported) {
156 _notmuch_database_log (query->notmuch,
157 "A Xapian exception occurred parsing query: %s\n",
158 error.get_msg ().c_str ());
159 _notmuch_database_log_append (query->notmuch,
160 "Query string was: %s\n",
161 query->query_string);
162 query->notmuch->exception_reported = true;
165 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
167 return NOTMUCH_STATUS_SUCCESS;
171 notmuch_query_get_query_string (const notmuch_query_t *query)
173 return query->query_string;
177 notmuch_query_set_omit_excluded (notmuch_query_t *query,
178 notmuch_exclude_t omit_excluded)
180 query->omit_excluded = omit_excluded;
184 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
190 notmuch_query_get_sort (const notmuch_query_t *query)
196 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
198 notmuch_status_t status;
201 status = _notmuch_query_ensure_parsed (query);
205 term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
206 if (query->terms.count (term) != 0)
207 return NOTMUCH_STATUS_IGNORED;
209 _notmuch_string_list_append (query->exclude_terms, term);
210 return NOTMUCH_STATUS_SUCCESS;
213 /* We end up having to call the destructors explicitly because we had
214 * to use "placement new" in order to initialize C++ objects within a
215 * block that we allocated with talloc. So C++ is making talloc
216 * slightly less simple to use, (we wouldn't need
217 * talloc_set_destructor at all otherwise).
220 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
222 messages->iterator.~MSetIterator ();
223 messages->iterator_end.~MSetIterator ();
228 /* Return a query that matches messages with the excluded tags
229 * registered with query. The caller of this function has to combine the returned
230 * query appropriately.*/
232 _notmuch_exclude_tags (notmuch_query_t *query)
234 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
236 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
238 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
239 exclude_query, Xapian::Query (term->string));
241 return exclude_query;
246 notmuch_query_search_messages_st (notmuch_query_t *query,
247 notmuch_messages_t **out)
249 return notmuch_query_search_messages (query, out);
253 notmuch_query_search_messages (notmuch_query_t *query,
254 notmuch_messages_t **out)
256 return _notmuch_query_search_documents (query, "mail", out);
260 _notmuch_query_search_documents (notmuch_query_t *query,
262 notmuch_messages_t **out)
264 notmuch_database_t *notmuch = query->notmuch;
265 const char *query_string = query->query_string;
266 notmuch_mset_messages_t *messages;
267 notmuch_status_t status;
269 status = _notmuch_query_ensure_parsed (query);
273 messages = talloc (query, notmuch_mset_messages_t);
274 if (unlikely (messages == NULL))
275 return NOTMUCH_STATUS_OUT_OF_MEMORY;
279 messages->base.is_of_list_type = false;
280 messages->base.iterator = NULL;
281 messages->notmuch = notmuch;
282 new (&messages->iterator) Xapian::MSetIterator ();
283 new (&messages->iterator_end) Xapian::MSetIterator ();
285 talloc_set_destructor (messages, _notmuch_messages_destructor);
287 Xapian::Enquire enquire (*notmuch->xapian_db);
288 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
289 _find_prefix ("type"),
291 Xapian::Query final_query, exclude_query;
293 Xapian::MSetIterator iterator;
295 if (strcmp (query_string, "") == 0 ||
296 strcmp (query_string, "*") == 0) {
297 final_query = mail_query;
299 final_query = Xapian::Query (Xapian::Query::OP_AND,
300 mail_query, query->xapian_query);
302 messages->base.excluded_doc_ids = NULL;
304 if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
305 exclude_query = _notmuch_exclude_tags (query);
307 if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
308 query->omit_excluded == NOTMUCH_EXCLUDE_ALL) {
309 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
310 final_query, exclude_query);
311 } else { /* NOTMUCH_EXCLUDE_FLAG */
312 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
313 exclude_query, final_query);
315 enquire.set_weighting_scheme (Xapian::BoolWeight ());
316 enquire.set_query (exclude_query);
318 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
320 GArray *excluded_doc_ids = g_array_new (false, false, sizeof (unsigned int));
322 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
323 unsigned int doc_id = *iterator;
324 g_array_append_val (excluded_doc_ids, doc_id);
326 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
327 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
329 g_array_unref (excluded_doc_ids);
334 enquire.set_weighting_scheme (Xapian::BoolWeight ());
336 switch (query->sort) {
337 case NOTMUCH_SORT_OLDEST_FIRST:
338 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, false);
340 case NOTMUCH_SORT_NEWEST_FIRST:
341 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, true);
343 case NOTMUCH_SORT_MESSAGE_ID:
344 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, false);
346 case NOTMUCH_SORT_UNSORTED:
350 if (_debug_query ()) {
351 fprintf (stderr, "Exclude query is:\n%s\n",
352 exclude_query.get_description ().c_str ());
353 fprintf (stderr, "Final query is:\n%s\n",
354 final_query.get_description ().c_str ());
357 enquire.set_query (final_query);
359 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
361 messages->iterator = mset.begin ();
362 messages->iterator_end = mset.end ();
364 *out = &messages->base;
365 return NOTMUCH_STATUS_SUCCESS;
367 } catch (const Xapian::Error &error) {
368 _notmuch_database_log (notmuch,
369 "A Xapian exception occurred performing query: %s\n",
370 error.get_msg ().c_str ());
371 _notmuch_database_log_append (notmuch,
372 "Query string was: %s\n",
373 query->query_string);
375 notmuch->exception_reported = true;
376 talloc_free (messages);
377 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
382 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
384 notmuch_mset_messages_t *mset_messages;
386 mset_messages = (notmuch_mset_messages_t *) messages;
388 return (mset_messages->iterator != mset_messages->iterator_end);
392 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
394 notmuch_mset_messages_t *mset_messages;
396 mset_messages = (notmuch_mset_messages_t *) messages;
398 if (! _notmuch_mset_messages_valid (&mset_messages->base))
401 return *mset_messages->iterator;
405 _notmuch_mset_messages_get (notmuch_messages_t *messages)
407 notmuch_message_t *message;
408 Xapian::docid doc_id;
409 notmuch_private_status_t status;
410 notmuch_mset_messages_t *mset_messages;
412 mset_messages = (notmuch_mset_messages_t *) messages;
414 if (! _notmuch_mset_messages_valid (&mset_messages->base))
417 doc_id = *mset_messages->iterator;
419 message = _notmuch_message_create (mset_messages,
420 mset_messages->notmuch, doc_id,
423 if (message == NULL &&
424 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
425 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
428 if (messages->excluded_doc_ids &&
429 _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
430 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
436 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
438 notmuch_mset_messages_t *mset_messages;
440 mset_messages = (notmuch_mset_messages_t *) messages;
442 mset_messages->iterator++;
446 _notmuch_doc_id_set_init (void *ctx,
447 notmuch_doc_id_set_t *doc_ids,
450 unsigned int max = 0;
451 unsigned char *bitmap;
453 for (unsigned int i = 0; i < arr->len; i++)
454 max = MAX (max, g_array_index (arr, unsigned int, i));
455 bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD (max) + 1);
460 doc_ids->bitmap = bitmap;
461 doc_ids->bound = max + 1;
463 for (unsigned int i = 0; i < arr->len; i++) {
464 unsigned int doc_id = g_array_index (arr, unsigned int, i);
465 bitmap[DOCIDSET_WORD (doc_id)] |= 1 << DOCIDSET_BIT (doc_id);
472 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
475 if (doc_id >= doc_ids->bound)
477 return doc_ids->bitmap[DOCIDSET_WORD (doc_id)] & (1 << DOCIDSET_BIT (doc_id));
481 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
484 if (doc_id < doc_ids->bound)
485 doc_ids->bitmap[DOCIDSET_WORD (doc_id)] &= ~(1 << DOCIDSET_BIT (doc_id));
488 /* Glib objects force use to use a talloc destructor as well, (but not
489 * nearly as ugly as the for messages due to C++ objects). At
490 * this point, I'd really like to have some talloc-friendly
491 * equivalents for the few pieces of glib that I'm using. */
493 _notmuch_threads_destructor (notmuch_threads_t *threads)
495 if (threads->doc_ids)
496 g_array_unref (threads->doc_ids);
502 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
504 return notmuch_query_search_threads (query, out);
508 notmuch_query_search_threads (notmuch_query_t *query,
509 notmuch_threads_t **out)
511 notmuch_threads_t *threads;
512 notmuch_messages_t *messages;
513 notmuch_status_t status;
515 threads = talloc (query, notmuch_threads_t);
517 return NOTMUCH_STATUS_OUT_OF_MEMORY;
518 threads->doc_ids = NULL;
519 talloc_set_destructor (threads, _notmuch_threads_destructor);
521 threads->query = query;
523 status = notmuch_query_search_messages (query, &messages);
525 talloc_free (threads);
529 threads->doc_ids = g_array_new (false, false, sizeof (unsigned int));
530 while (notmuch_messages_valid (messages)) {
531 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
532 g_array_append_val (threads->doc_ids, doc_id);
533 notmuch_messages_move_to_next (messages);
535 threads->doc_id_pos = 0;
537 talloc_free (messages);
539 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
541 talloc_free (threads);
542 return NOTMUCH_STATUS_OUT_OF_MEMORY;
546 return NOTMUCH_STATUS_SUCCESS;
550 notmuch_query_destroy (notmuch_query_t *query)
556 notmuch_threads_valid (notmuch_threads_t *threads)
563 while (threads->doc_id_pos < threads->doc_ids->len) {
564 doc_id = g_array_index (threads->doc_ids, unsigned int,
565 threads->doc_id_pos);
566 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
569 threads->doc_id_pos++;
572 return threads->doc_id_pos < threads->doc_ids->len;
576 notmuch_threads_get (notmuch_threads_t *threads)
580 if (! notmuch_threads_valid (threads))
583 doc_id = g_array_index (threads->doc_ids, unsigned int,
584 threads->doc_id_pos);
585 return _notmuch_thread_create (threads->query,
586 threads->query->notmuch,
589 threads->query->exclude_terms,
590 threads->query->omit_excluded,
591 threads->query->sort);
595 notmuch_threads_move_to_next (notmuch_threads_t *threads)
597 threads->doc_id_pos++;
601 notmuch_threads_destroy (notmuch_threads_t *threads)
603 talloc_free (threads);
607 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
609 return notmuch_query_count_messages (query, count_out);
613 notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)
615 return _notmuch_query_count_documents (query, "mail", count_out);
619 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
621 notmuch_database_t *notmuch = query->notmuch;
622 const char *query_string = query->query_string;
623 Xapian::doccount count = 0;
624 notmuch_status_t status;
626 status = _notmuch_query_ensure_parsed (query);
631 Xapian::Enquire enquire (*notmuch->xapian_db);
632 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
633 _find_prefix ("type"),
635 Xapian::Query final_query, exclude_query;
638 if (strcmp (query_string, "") == 0 ||
639 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 1 to avoid fetching documents we will discard.
668 mset = enquire.get_mset (0, 1,
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_st (notmuch_query_t *query, unsigned *count)
690 return notmuch_query_count_threads (query, count);
694 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
696 notmuch_messages_t *messages;
699 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
702 query->sort = NOTMUCH_SORT_UNSORTED;
703 ret = notmuch_query_search_messages (query, &messages);
707 if (messages == NULL)
708 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
710 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
712 talloc_free (messages);
713 return NOTMUCH_STATUS_OUT_OF_MEMORY;
716 while (notmuch_messages_valid (messages)) {
717 notmuch_message_t *message = notmuch_messages_get (messages);
718 const char *thread_id = notmuch_message_get_thread_id (message);
719 char *thread_id_copy = talloc_strdup (messages, thread_id);
720 if (unlikely (thread_id_copy == NULL)) {
721 notmuch_message_destroy (message);
722 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
725 g_hash_table_insert (hash, thread_id_copy, NULL);
726 notmuch_message_destroy (message);
727 notmuch_messages_move_to_next (messages);
730 *count = g_hash_table_size (hash);
733 g_hash_table_unref (hash);
734 talloc_free (messages);
740 notmuch_query_get_database (const notmuch_query_t *query)
742 return query->notmuch;