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;
34 typedef struct _notmuch_mset_messages {
35 notmuch_messages_t base;
36 notmuch_database_t *notmuch;
37 Xapian::MSetIterator iterator;
38 Xapian::MSetIterator iterator_end;
39 } notmuch_mset_messages_t;
41 struct _notmuch_doc_id_set {
42 unsigned char *bitmap;
46 #define DOCIDSET_WORD(bit) ((bit) / CHAR_BIT)
47 #define DOCIDSET_BIT(bit) ((bit) % CHAR_BIT)
49 struct visible _notmuch_threads {
50 notmuch_query_t *query;
52 /* The ordered list of doc ids matched by the query. */
54 /* Our iterator's current position in doc_ids. */
55 unsigned int doc_id_pos;
56 /* The set of matched docid's that have not been assigned to a
57 * thread. Initially, this contains every docid in doc_ids. */
58 notmuch_doc_id_set_t match_set;
61 /* We need this in the message functions so forward declare. */
63 _notmuch_doc_id_set_init (void *ctx,
64 notmuch_doc_id_set_t *doc_ids,
70 char *env = getenv ("NOTMUCH_DEBUG_QUERY");
71 return (env && strcmp (env, "") != 0);
75 notmuch_query_create (notmuch_database_t *notmuch,
76 const char *query_string)
78 notmuch_query_t *query;
81 fprintf (stderr, "Query string is:\n%s\n", query_string);
83 query = talloc (notmuch, notmuch_query_t);
84 if (unlikely (query == NULL))
87 query->notmuch = notmuch;
89 query->query_string = talloc_strdup (query, query_string);
91 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
93 query->exclude_terms = _notmuch_string_list_create (query);
95 query->omit_excluded = NOTMUCH_EXCLUDE_TRUE;
101 notmuch_query_get_query_string (const notmuch_query_t *query)
103 return query->query_string;
107 notmuch_query_set_omit_excluded (notmuch_query_t *query,
108 notmuch_exclude_t omit_excluded)
110 query->omit_excluded = omit_excluded;
114 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
120 notmuch_query_get_sort (const notmuch_query_t *query)
126 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
128 char *term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
129 _notmuch_string_list_append (query->exclude_terms, term);
132 /* We end up having to call the destructors explicitly because we had
133 * to use "placement new" in order to initialize C++ objects within a
134 * block that we allocated with talloc. So C++ is making talloc
135 * slightly less simple to use, (we wouldn't need
136 * talloc_set_destructor at all otherwise).
139 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
141 messages->iterator.~MSetIterator ();
142 messages->iterator_end.~MSetIterator ();
147 /* Return a query that matches messages with the excluded tags
148 * registered with query. Any tags that explicitly appear in xquery
149 * will not be excluded, and will be removed from the list of exclude
150 * tags. The caller of this function has to combine the returned
151 * query appropriately.*/
153 _notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)
155 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
157 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
159 Xapian::TermIterator it = xquery.get_terms_begin ();
160 Xapian::TermIterator end = xquery.get_terms_end ();
161 for (; it != end; it++) {
162 if ((*it).compare (term->string) == 0)
166 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
167 exclude_query, Xapian::Query (term->string));
169 term->string = talloc_strdup (query, "");
171 return exclude_query;
175 notmuch_query_search_messages (notmuch_query_t *query)
177 notmuch_status_t status;
178 notmuch_messages_t *messages;
179 status = notmuch_query_search_messages_st (query, &messages);
187 notmuch_query_search_messages_st (notmuch_query_t *query,
188 notmuch_messages_t **out)
190 return _notmuch_query_search_documents (query, "mail", out);
194 _notmuch_query_search_documents (notmuch_query_t *query,
196 notmuch_messages_t **out)
198 notmuch_database_t *notmuch = query->notmuch;
199 const char *query_string = query->query_string;
200 notmuch_mset_messages_t *messages;
202 messages = talloc (query, notmuch_mset_messages_t);
203 if (unlikely (messages == NULL))
204 return NOTMUCH_STATUS_OUT_OF_MEMORY;
208 messages->base.is_of_list_type = FALSE;
209 messages->base.iterator = NULL;
210 messages->notmuch = notmuch;
211 new (&messages->iterator) Xapian::MSetIterator ();
212 new (&messages->iterator_end) Xapian::MSetIterator ();
214 talloc_set_destructor (messages, _notmuch_messages_destructor);
216 Xapian::Enquire enquire (*notmuch->xapian_db);
217 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
218 _find_prefix ("type"),
220 Xapian::Query string_query, final_query, exclude_query;
222 Xapian::MSetIterator iterator;
224 if (strcmp (query_string, "") == 0 ||
225 strcmp (query_string, "*") == 0)
227 final_query = mail_query;
229 string_query = notmuch->query_parser->
230 parse_query (query_string, NOTMUCH_QUERY_PARSER_FLAGS);
231 final_query = Xapian::Query (Xapian::Query::OP_AND,
232 mail_query, string_query);
234 messages->base.excluded_doc_ids = NULL;
236 if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
237 exclude_query = _notmuch_exclude_tags (query, final_query);
239 if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
240 query->omit_excluded == NOTMUCH_EXCLUDE_ALL)
242 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
243 final_query, exclude_query);
244 } else { /* NOTMUCH_EXCLUDE_FLAG */
245 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
246 exclude_query, final_query);
248 enquire.set_weighting_scheme (Xapian::BoolWeight());
249 enquire.set_query (exclude_query);
251 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
253 GArray *excluded_doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
255 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
256 unsigned int doc_id = *iterator;
257 g_array_append_val (excluded_doc_ids, doc_id);
259 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
260 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
262 g_array_unref (excluded_doc_ids);
267 enquire.set_weighting_scheme (Xapian::BoolWeight());
269 switch (query->sort) {
270 case NOTMUCH_SORT_OLDEST_FIRST:
271 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
273 case NOTMUCH_SORT_NEWEST_FIRST:
274 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
276 case NOTMUCH_SORT_MESSAGE_ID:
277 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
279 case NOTMUCH_SORT_UNSORTED:
283 if (_debug_query ()) {
284 fprintf (stderr, "Exclude query is:\n%s\n",
285 exclude_query.get_description ().c_str ());
286 fprintf (stderr, "Final query is:\n%s\n",
287 final_query.get_description ().c_str ());
290 enquire.set_query (final_query);
292 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
294 messages->iterator = mset.begin ();
295 messages->iterator_end = mset.end ();
297 *out = &messages->base;
298 return NOTMUCH_STATUS_SUCCESS;
300 } catch (const Xapian::Error &error) {
301 _notmuch_database_log (notmuch,
302 "A Xapian exception occurred performing query: %s\n",
303 error.get_msg().c_str());
304 _notmuch_database_log_append (notmuch,
305 "Query string was: %s\n",
306 query->query_string);
308 notmuch->exception_reported = TRUE;
309 talloc_free (messages);
310 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
315 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
317 notmuch_mset_messages_t *mset_messages;
319 mset_messages = (notmuch_mset_messages_t *) messages;
321 return (mset_messages->iterator != mset_messages->iterator_end);
325 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
327 notmuch_mset_messages_t *mset_messages;
329 mset_messages = (notmuch_mset_messages_t *) messages;
331 if (! _notmuch_mset_messages_valid (&mset_messages->base))
334 return *mset_messages->iterator;
338 _notmuch_mset_messages_get (notmuch_messages_t *messages)
340 notmuch_message_t *message;
341 Xapian::docid doc_id;
342 notmuch_private_status_t status;
343 notmuch_mset_messages_t *mset_messages;
345 mset_messages = (notmuch_mset_messages_t *) messages;
347 if (! _notmuch_mset_messages_valid (&mset_messages->base))
350 doc_id = *mset_messages->iterator;
352 message = _notmuch_message_create (mset_messages,
353 mset_messages->notmuch, doc_id,
356 if (message == NULL &&
357 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
359 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
362 if (messages->excluded_doc_ids &&
363 _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
364 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
370 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
372 notmuch_mset_messages_t *mset_messages;
374 mset_messages = (notmuch_mset_messages_t *) messages;
376 mset_messages->iterator++;
379 static notmuch_bool_t
380 _notmuch_doc_id_set_init (void *ctx,
381 notmuch_doc_id_set_t *doc_ids,
384 unsigned int max = 0;
385 unsigned char *bitmap;
387 for (unsigned int i = 0; i < arr->len; i++)
388 max = MAX(max, g_array_index (arr, unsigned int, i));
389 bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD(max) + 1);
394 doc_ids->bitmap = bitmap;
395 doc_ids->bound = max + 1;
397 for (unsigned int i = 0; i < arr->len; i++) {
398 unsigned int doc_id = g_array_index (arr, unsigned int, i);
399 bitmap[DOCIDSET_WORD(doc_id)] |= 1 << DOCIDSET_BIT(doc_id);
406 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
409 if (doc_id >= doc_ids->bound)
411 return doc_ids->bitmap[DOCIDSET_WORD(doc_id)] & (1 << DOCIDSET_BIT(doc_id));
415 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
418 if (doc_id < doc_ids->bound)
419 doc_ids->bitmap[DOCIDSET_WORD(doc_id)] &= ~(1 << DOCIDSET_BIT(doc_id));
422 /* Glib objects force use to use a talloc destructor as well, (but not
423 * nearly as ugly as the for messages due to C++ objects). At
424 * this point, I'd really like to have some talloc-friendly
425 * equivalents for the few pieces of glib that I'm using. */
427 _notmuch_threads_destructor (notmuch_threads_t *threads)
429 if (threads->doc_ids)
430 g_array_unref (threads->doc_ids);
437 notmuch_query_search_threads (notmuch_query_t *query)
439 notmuch_status_t status;
440 notmuch_threads_t *threads;
441 status = notmuch_query_search_threads_st (query, &threads);
449 notmuch_query_search_threads_st (notmuch_query_t *query,
450 notmuch_threads_t **out)
452 notmuch_threads_t *threads;
453 notmuch_messages_t *messages;
454 notmuch_status_t status;
456 threads = talloc (query, notmuch_threads_t);
458 return NOTMUCH_STATUS_OUT_OF_MEMORY;
459 threads->doc_ids = NULL;
460 talloc_set_destructor (threads, _notmuch_threads_destructor);
462 threads->query = query;
464 status = notmuch_query_search_messages_st (query, &messages);
466 talloc_free (threads);
470 threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
471 while (notmuch_messages_valid (messages)) {
472 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
473 g_array_append_val (threads->doc_ids, doc_id);
474 notmuch_messages_move_to_next (messages);
476 threads->doc_id_pos = 0;
478 talloc_free (messages);
480 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
482 talloc_free (threads);
483 return NOTMUCH_STATUS_OUT_OF_MEMORY;
487 return NOTMUCH_STATUS_SUCCESS;
491 notmuch_query_destroy (notmuch_query_t *query)
497 notmuch_threads_valid (notmuch_threads_t *threads)
504 while (threads->doc_id_pos < threads->doc_ids->len) {
505 doc_id = g_array_index (threads->doc_ids, unsigned int,
506 threads->doc_id_pos);
507 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
510 threads->doc_id_pos++;
513 return threads->doc_id_pos < threads->doc_ids->len;
517 notmuch_threads_get (notmuch_threads_t *threads)
521 if (! notmuch_threads_valid (threads))
524 doc_id = g_array_index (threads->doc_ids, unsigned int,
525 threads->doc_id_pos);
526 return _notmuch_thread_create (threads->query,
527 threads->query->notmuch,
530 threads->query->exclude_terms,
531 threads->query->omit_excluded,
532 threads->query->sort);
536 notmuch_threads_move_to_next (notmuch_threads_t *threads)
538 threads->doc_id_pos++;
542 notmuch_threads_destroy (notmuch_threads_t *threads)
544 talloc_free (threads);
548 notmuch_query_count_messages (notmuch_query_t *query)
550 notmuch_status_t status;
553 status = notmuch_query_count_messages_st (query, &count);
554 return status ? 0 : count;
558 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
560 return _notmuch_query_count_documents (query, "mail", count_out);
564 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
566 notmuch_database_t *notmuch = query->notmuch;
567 const char *query_string = query->query_string;
568 Xapian::doccount count = 0;
571 Xapian::Enquire enquire (*notmuch->xapian_db);
572 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
573 _find_prefix ("type"),
575 Xapian::Query string_query, final_query, exclude_query;
578 if (strcmp (query_string, "") == 0 ||
579 strcmp (query_string, "*") == 0)
581 final_query = mail_query;
583 string_query = notmuch->query_parser->
584 parse_query (query_string, NOTMUCH_QUERY_PARSER_FLAGS);
585 final_query = Xapian::Query (Xapian::Query::OP_AND,
586 mail_query, string_query);
589 exclude_query = _notmuch_exclude_tags (query, final_query);
591 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
592 final_query, exclude_query);
594 enquire.set_weighting_scheme(Xapian::BoolWeight());
595 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
597 if (_debug_query ()) {
598 fprintf (stderr, "Exclude query is:\n%s\n",
599 exclude_query.get_description ().c_str ());
600 fprintf (stderr, "Final query is:\n%s\n",
601 final_query.get_description ().c_str ());
604 enquire.set_query (final_query);
607 * Set the checkatleast parameter to the number of documents
608 * in the database to make get_matches_estimated() exact.
609 * Set the max parameter to 0 to avoid fetching documents we will discard.
611 mset = enquire.get_mset (0, 0,
612 notmuch->xapian_db->get_doccount ());
614 count = mset.get_matches_estimated();
616 } catch (const Xapian::Error &error) {
617 _notmuch_database_log (notmuch,
618 "A Xapian exception occurred performing query: %s\n",
619 error.get_msg().c_str());
620 _notmuch_database_log_append (notmuch,
621 "Query string was: %s\n",
622 query->query_string);
623 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
627 return NOTMUCH_STATUS_SUCCESS;
631 notmuch_query_count_threads (notmuch_query_t *query)
633 notmuch_status_t status;
636 status = notmuch_query_count_threads_st (query, &count);
637 return status ? 0 : count;
641 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
643 notmuch_messages_t *messages;
646 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
649 query->sort = NOTMUCH_SORT_UNSORTED;
650 ret = notmuch_query_search_messages_st (query, &messages);
654 if (messages == NULL)
655 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
657 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
659 talloc_free (messages);
660 return NOTMUCH_STATUS_OUT_OF_MEMORY;
663 while (notmuch_messages_valid (messages)) {
664 notmuch_message_t *message = notmuch_messages_get (messages);
665 const char *thread_id = notmuch_message_get_thread_id (message);
666 char *thread_id_copy = talloc_strdup (messages, thread_id);
667 if (unlikely (thread_id_copy == NULL)) {
668 notmuch_message_destroy (message);
669 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
672 g_hash_table_insert (hash, thread_id_copy, NULL);
673 notmuch_message_destroy (message);
674 notmuch_messages_move_to_next (messages);
677 *count = g_hash_table_size (hash);
680 g_hash_table_unref (hash);
681 talloc_free (messages);
687 notmuch_query_get_database (const notmuch_query_t *query)
689 return query->notmuch;