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 http://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_bool_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 (NULL, 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 = TRUE;
101 notmuch_query_get_query_string (notmuch_query_t *query)
103 return query->query_string;
107 notmuch_query_set_omit_excluded (notmuch_query_t *query, notmuch_bool_t omit_excluded)
109 query->omit_excluded = omit_excluded;
113 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
119 notmuch_query_get_sort (notmuch_query_t *query)
125 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
127 char *term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
128 _notmuch_string_list_append (query->exclude_terms, term);
131 /* We end up having to call the destructors explicitly because we had
132 * to use "placement new" in order to initialize C++ objects within a
133 * block that we allocated with talloc. So C++ is making talloc
134 * slightly less simple to use, (we wouldn't need
135 * talloc_set_destructor at all otherwise).
138 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
140 messages->iterator.~MSetIterator ();
141 messages->iterator_end.~MSetIterator ();
146 /* Return a query that matches messages with the excluded tags
147 * registered with query. Any tags that explicitly appear in xquery
148 * will not be excluded, and will be removed from the list of exclude
149 * tags. The caller of this function has to combine the returned
150 * query appropriately.*/
152 _notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)
154 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
156 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
158 Xapian::TermIterator it = xquery.get_terms_begin ();
159 Xapian::TermIterator end = xquery.get_terms_end ();
160 for (; it != end; it++) {
161 if ((*it).compare (term->string) == 0)
165 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
166 exclude_query, Xapian::Query (term->string));
168 term->string = talloc_strdup (query, "");
170 return exclude_query;
174 notmuch_query_search_messages (notmuch_query_t *query)
176 notmuch_database_t *notmuch = query->notmuch;
177 const char *query_string = query->query_string;
178 notmuch_mset_messages_t *messages;
180 messages = talloc (query, notmuch_mset_messages_t);
181 if (unlikely (messages == NULL))
186 messages->base.is_of_list_type = FALSE;
187 messages->base.iterator = NULL;
188 messages->notmuch = notmuch;
189 new (&messages->iterator) Xapian::MSetIterator ();
190 new (&messages->iterator_end) Xapian::MSetIterator ();
192 talloc_set_destructor (messages, _notmuch_messages_destructor);
194 Xapian::Enquire enquire (*notmuch->xapian_db);
195 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
196 _find_prefix ("type"),
198 Xapian::Query string_query, final_query, exclude_query;
200 Xapian::MSetIterator iterator;
201 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
202 Xapian::QueryParser::FLAG_PHRASE |
203 Xapian::QueryParser::FLAG_LOVEHATE |
204 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
205 Xapian::QueryParser::FLAG_WILDCARD |
206 Xapian::QueryParser::FLAG_PURE_NOT);
208 if (strcmp (query_string, "") == 0 ||
209 strcmp (query_string, "*") == 0)
211 final_query = mail_query;
213 string_query = notmuch->query_parser->
214 parse_query (query_string, flags);
215 final_query = Xapian::Query (Xapian::Query::OP_AND,
216 mail_query, string_query);
218 messages->base.excluded_doc_ids = NULL;
220 if (query->exclude_terms) {
221 exclude_query = _notmuch_exclude_tags (query, final_query);
223 if (query->omit_excluded)
224 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
225 final_query, exclude_query);
227 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
228 exclude_query, final_query);
230 enquire.set_weighting_scheme (Xapian::BoolWeight());
231 enquire.set_query (exclude_query);
233 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
235 GArray *excluded_doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
237 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
238 unsigned int doc_id = *iterator;
239 g_array_append_val (excluded_doc_ids, doc_id);
241 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
242 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
244 g_array_unref (excluded_doc_ids);
249 enquire.set_weighting_scheme (Xapian::BoolWeight());
251 switch (query->sort) {
252 case NOTMUCH_SORT_OLDEST_FIRST:
253 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
255 case NOTMUCH_SORT_NEWEST_FIRST:
256 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
258 case NOTMUCH_SORT_MESSAGE_ID:
259 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
261 case NOTMUCH_SORT_UNSORTED:
265 if (_debug_query ()) {
266 fprintf (stderr, "Exclude query is:\n%s\n",
267 exclude_query.get_description ().c_str ());
268 fprintf (stderr, "Final query is:\n%s\n",
269 final_query.get_description ().c_str ());
272 enquire.set_query (final_query);
274 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
276 messages->iterator = mset.begin ();
277 messages->iterator_end = mset.end ();
279 return &messages->base;
281 } catch (const Xapian::Error &error) {
282 fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
283 error.get_msg().c_str());
284 fprintf (stderr, "Query string was: %s\n", query->query_string);
285 notmuch->exception_reported = TRUE;
286 talloc_free (messages);
292 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
294 notmuch_mset_messages_t *mset_messages;
296 mset_messages = (notmuch_mset_messages_t *) messages;
298 return (mset_messages->iterator != mset_messages->iterator_end);
302 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
304 notmuch_mset_messages_t *mset_messages;
306 mset_messages = (notmuch_mset_messages_t *) messages;
308 if (! _notmuch_mset_messages_valid (&mset_messages->base))
311 return *mset_messages->iterator;
315 _notmuch_mset_messages_get (notmuch_messages_t *messages)
317 notmuch_message_t *message;
318 Xapian::docid doc_id;
319 notmuch_private_status_t status;
320 notmuch_mset_messages_t *mset_messages;
322 mset_messages = (notmuch_mset_messages_t *) messages;
324 if (! _notmuch_mset_messages_valid (&mset_messages->base))
327 doc_id = *mset_messages->iterator;
329 message = _notmuch_message_create (mset_messages,
330 mset_messages->notmuch, doc_id,
333 if (message == NULL &&
334 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
336 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
339 if (messages->excluded_doc_ids &&
340 _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
341 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
347 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
349 notmuch_mset_messages_t *mset_messages;
351 mset_messages = (notmuch_mset_messages_t *) messages;
353 mset_messages->iterator++;
356 static notmuch_bool_t
357 _notmuch_doc_id_set_init (void *ctx,
358 notmuch_doc_id_set_t *doc_ids,
361 unsigned int max = 0;
362 unsigned char *bitmap;
364 for (unsigned int i = 0; i < arr->len; i++)
365 max = MAX(max, g_array_index (arr, unsigned int, i));
366 bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD(max) + 1);
371 doc_ids->bitmap = bitmap;
372 doc_ids->bound = max + 1;
374 for (unsigned int i = 0; i < arr->len; i++) {
375 unsigned int doc_id = g_array_index (arr, unsigned int, i);
376 bitmap[DOCIDSET_WORD(doc_id)] |= 1 << DOCIDSET_BIT(doc_id);
383 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
386 if (doc_id >= doc_ids->bound)
388 return doc_ids->bitmap[DOCIDSET_WORD(doc_id)] & (1 << DOCIDSET_BIT(doc_id));
392 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
395 if (doc_id < doc_ids->bound)
396 doc_ids->bitmap[DOCIDSET_WORD(doc_id)] &= ~(1 << DOCIDSET_BIT(doc_id));
399 /* Glib objects force use to use a talloc destructor as well, (but not
400 * nearly as ugly as the for messages due to C++ objects). At
401 * this point, I'd really like to have some talloc-friendly
402 * equivalents for the few pieces of glib that I'm using. */
404 _notmuch_threads_destructor (notmuch_threads_t *threads)
406 if (threads->doc_ids)
407 g_array_unref (threads->doc_ids);
413 notmuch_query_search_threads (notmuch_query_t *query)
415 notmuch_threads_t *threads;
416 notmuch_messages_t *messages;
418 threads = talloc (query, notmuch_threads_t);
421 threads->doc_ids = NULL;
422 talloc_set_destructor (threads, _notmuch_threads_destructor);
424 threads->query = query;
426 messages = notmuch_query_search_messages (query);
427 if (messages == NULL) {
428 talloc_free (threads);
432 threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
433 while (notmuch_messages_valid (messages)) {
434 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
435 g_array_append_val (threads->doc_ids, doc_id);
436 notmuch_messages_move_to_next (messages);
438 threads->doc_id_pos = 0;
440 talloc_free (messages);
442 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
444 talloc_free (threads);
452 notmuch_query_destroy (notmuch_query_t *query)
458 notmuch_threads_valid (notmuch_threads_t *threads)
462 while (threads->doc_id_pos < threads->doc_ids->len) {
463 doc_id = g_array_index (threads->doc_ids, unsigned int,
464 threads->doc_id_pos);
465 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
468 threads->doc_id_pos++;
471 return threads->doc_id_pos < threads->doc_ids->len;
475 notmuch_threads_get (notmuch_threads_t *threads)
479 if (! notmuch_threads_valid (threads))
482 doc_id = g_array_index (threads->doc_ids, unsigned int,
483 threads->doc_id_pos);
484 return _notmuch_thread_create (threads->query,
485 threads->query->notmuch,
488 threads->query->exclude_terms,
489 threads->query->sort);
493 notmuch_threads_move_to_next (notmuch_threads_t *threads)
495 threads->doc_id_pos++;
499 notmuch_threads_destroy (notmuch_threads_t *threads)
501 talloc_free (threads);
505 notmuch_query_count_messages (notmuch_query_t *query)
507 notmuch_database_t *notmuch = query->notmuch;
508 const char *query_string = query->query_string;
509 Xapian::doccount count = 0;
512 Xapian::Enquire enquire (*notmuch->xapian_db);
513 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
514 _find_prefix ("type"),
516 Xapian::Query string_query, final_query, exclude_query;
518 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
519 Xapian::QueryParser::FLAG_PHRASE |
520 Xapian::QueryParser::FLAG_LOVEHATE |
521 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
522 Xapian::QueryParser::FLAG_WILDCARD |
523 Xapian::QueryParser::FLAG_PURE_NOT);
525 if (strcmp (query_string, "") == 0 ||
526 strcmp (query_string, "*") == 0)
528 final_query = mail_query;
530 string_query = notmuch->query_parser->
531 parse_query (query_string, flags);
532 final_query = Xapian::Query (Xapian::Query::OP_AND,
533 mail_query, string_query);
536 exclude_query = _notmuch_exclude_tags (query, final_query);
538 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
539 final_query, exclude_query);
541 enquire.set_weighting_scheme(Xapian::BoolWeight());
542 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
544 if (_debug_query ()) {
545 fprintf (stderr, "Exclude query is:\n%s\n",
546 exclude_query.get_description ().c_str ());
547 fprintf (stderr, "Final query is:\n%s\n",
548 final_query.get_description ().c_str ());
551 enquire.set_query (final_query);
553 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
555 count = mset.get_matches_estimated();
557 } catch (const Xapian::Error &error) {
558 fprintf (stderr, "A Xapian exception occurred: %s\n",
559 error.get_msg().c_str());
560 fprintf (stderr, "Query string was: %s\n", query->query_string);
567 notmuch_query_count_threads (notmuch_query_t *query)
569 notmuch_messages_t *messages;
575 query->sort = NOTMUCH_SORT_UNSORTED;
576 messages = notmuch_query_search_messages (query);
578 if (messages == NULL)
581 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
583 talloc_free (messages);
587 while (notmuch_messages_valid (messages)) {
588 notmuch_message_t *message = notmuch_messages_get (messages);
589 const char *thread_id = notmuch_message_get_thread_id (message);
590 char *thread_id_copy = talloc_strdup (messages, thread_id);
591 if (unlikely (thread_id_copy == NULL)) {
592 notmuch_message_destroy (message);
596 g_hash_table_insert (hash, thread_id_copy, NULL);
597 notmuch_message_destroy (message);
598 notmuch_messages_move_to_next (messages);
601 count = g_hash_table_size (hash);
604 g_hash_table_unref (hash);
605 talloc_free (messages);