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_messages;
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 {
46 #define DOCIDSET_WORD(bit) ((bit) / sizeof (unsigned int))
47 #define DOCIDSET_BIT(bit) ((bit) % sizeof (unsigned int))
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,
68 notmuch_query_create (notmuch_database_t *notmuch,
69 const char *query_string)
71 notmuch_query_t *query;
74 fprintf (stderr, "Query string is:\n%s\n", query_string);
77 query = talloc (NULL, notmuch_query_t);
78 if (unlikely (query == NULL))
81 query->notmuch = notmuch;
83 query->query_string = talloc_strdup (query, query_string);
85 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
87 query->exclude_terms = _notmuch_string_list_create (query);
89 query->omit_excluded_messages = FALSE;
95 notmuch_query_get_query_string (notmuch_query_t *query)
97 return query->query_string;
101 notmuch_query_set_omit_excluded_messages (notmuch_query_t *query, notmuch_bool_t omit)
103 query->omit_excluded_messages = omit;
107 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
113 notmuch_query_get_sort (notmuch_query_t *query)
119 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
121 char *term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
122 _notmuch_string_list_append (query->exclude_terms, term);
125 /* We end up having to call the destructors explicitly because we had
126 * to use "placement new" in order to initialize C++ objects within a
127 * block that we allocated with talloc. So C++ is making talloc
128 * slightly less simple to use, (we wouldn't need
129 * talloc_set_destructor at all otherwise).
132 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
134 messages->iterator.~MSetIterator ();
135 messages->iterator_end.~MSetIterator ();
140 /* Return a query that matches messages with the excluded tags
141 * registered with query. Any tags that explicitly appear in xquery
142 * will not be excluded, and will be removed from the list of exclude
143 * tags. The caller of this function has to combine the returned
144 * query appropriately.*/
146 _notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)
148 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
150 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
152 Xapian::TermIterator it = xquery.get_terms_begin ();
153 Xapian::TermIterator end = xquery.get_terms_end ();
154 for (; it != end; it++) {
155 if ((*it).compare (term->string) == 0)
159 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
160 exclude_query, Xapian::Query (term->string));
162 term->string = talloc_strdup (query, "");
164 return exclude_query;
168 notmuch_query_search_messages (notmuch_query_t *query)
170 notmuch_database_t *notmuch = query->notmuch;
171 const char *query_string = query->query_string;
172 notmuch_mset_messages_t *messages;
174 messages = talloc (query, notmuch_mset_messages_t);
175 if (unlikely (messages == NULL))
180 messages->base.is_of_list_type = FALSE;
181 messages->base.iterator = NULL;
182 messages->notmuch = notmuch;
183 new (&messages->iterator) Xapian::MSetIterator ();
184 new (&messages->iterator_end) Xapian::MSetIterator ();
186 talloc_set_destructor (messages, _notmuch_messages_destructor);
188 Xapian::Enquire enquire (*notmuch->xapian_db);
189 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
190 _find_prefix ("type"),
192 Xapian::Query string_query, final_query, exclude_query;
194 Xapian::MSetIterator iterator;
195 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
196 Xapian::QueryParser::FLAG_PHRASE |
197 Xapian::QueryParser::FLAG_LOVEHATE |
198 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
199 Xapian::QueryParser::FLAG_WILDCARD |
200 Xapian::QueryParser::FLAG_PURE_NOT);
202 if (strcmp (query_string, "") == 0 ||
203 strcmp (query_string, "*") == 0)
205 final_query = mail_query;
207 string_query = notmuch->query_parser->
208 parse_query (query_string, flags);
209 final_query = Xapian::Query (Xapian::Query::OP_AND,
210 mail_query, string_query);
212 messages->base.excluded_doc_ids = NULL;
214 if (query->exclude_terms) {
215 exclude_query = _notmuch_exclude_tags (query, final_query);
216 exclude_query = Xapian::Query (Xapian::Query::OP_AND,
217 exclude_query, final_query);
219 if (query->omit_excluded_messages)
220 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
221 final_query, exclude_query);
223 enquire.set_weighting_scheme (Xapian::BoolWeight());
224 enquire.set_query (exclude_query);
226 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
228 GArray *excluded_doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
230 for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
231 unsigned int doc_id = *iterator;
232 g_array_append_val (excluded_doc_ids, doc_id);
234 messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
235 _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
237 g_array_unref (excluded_doc_ids);
242 enquire.set_weighting_scheme (Xapian::BoolWeight());
244 switch (query->sort) {
245 case NOTMUCH_SORT_OLDEST_FIRST:
246 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
248 case NOTMUCH_SORT_NEWEST_FIRST:
249 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
251 case NOTMUCH_SORT_MESSAGE_ID:
252 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
254 case NOTMUCH_SORT_UNSORTED:
259 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
262 enquire.set_query (final_query);
264 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
266 messages->iterator = mset.begin ();
267 messages->iterator_end = mset.end ();
269 return &messages->base;
271 } catch (const Xapian::Error &error) {
272 fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
273 error.get_msg().c_str());
274 fprintf (stderr, "Query string was: %s\n", query->query_string);
275 notmuch->exception_reported = TRUE;
276 talloc_free (messages);
282 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
284 notmuch_mset_messages_t *mset_messages;
286 mset_messages = (notmuch_mset_messages_t *) messages;
288 return (mset_messages->iterator != mset_messages->iterator_end);
292 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
294 notmuch_mset_messages_t *mset_messages;
296 mset_messages = (notmuch_mset_messages_t *) messages;
298 if (! _notmuch_mset_messages_valid (&mset_messages->base))
301 return *mset_messages->iterator;
305 _notmuch_mset_messages_get (notmuch_messages_t *messages)
307 notmuch_message_t *message;
308 Xapian::docid doc_id;
309 notmuch_private_status_t status;
310 notmuch_mset_messages_t *mset_messages;
312 mset_messages = (notmuch_mset_messages_t *) messages;
314 if (! _notmuch_mset_messages_valid (&mset_messages->base))
317 doc_id = *mset_messages->iterator;
319 message = _notmuch_message_create (mset_messages,
320 mset_messages->notmuch, doc_id,
323 if (message == NULL &&
324 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
326 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
329 if (messages->excluded_doc_ids &&
330 _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
331 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
337 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
339 notmuch_mset_messages_t *mset_messages;
341 mset_messages = (notmuch_mset_messages_t *) messages;
343 mset_messages->iterator++;
346 static notmuch_bool_t
347 _notmuch_doc_id_set_init (void *ctx,
348 notmuch_doc_id_set_t *doc_ids,
351 unsigned int max = 0;
352 unsigned int *bitmap;
354 for (unsigned int i = 0; i < arr->len; i++)
355 max = MAX(max, g_array_index (arr, unsigned int, i));
356 bitmap = talloc_zero_array (ctx, unsigned int, 1 + max / sizeof (*bitmap));
361 doc_ids->bitmap = bitmap;
362 doc_ids->bound = max + 1;
364 for (unsigned int i = 0; i < arr->len; i++) {
365 unsigned int doc_id = g_array_index (arr, unsigned int, i);
366 bitmap[DOCIDSET_WORD(doc_id)] |= 1 << DOCIDSET_BIT(doc_id);
373 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
376 if (doc_id >= doc_ids->bound)
378 return doc_ids->bitmap[DOCIDSET_WORD(doc_id)] & (1 << DOCIDSET_BIT(doc_id));
382 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
385 if (doc_id < doc_ids->bound)
386 doc_ids->bitmap[DOCIDSET_WORD(doc_id)] &= ~(1 << DOCIDSET_BIT(doc_id));
389 /* Glib objects force use to use a talloc destructor as well, (but not
390 * nearly as ugly as the for messages due to C++ objects). At
391 * this point, I'd really like to have some talloc-friendly
392 * equivalents for the few pieces of glib that I'm using. */
394 _notmuch_threads_destructor (notmuch_threads_t *threads)
396 if (threads->doc_ids)
397 g_array_unref (threads->doc_ids);
403 notmuch_query_search_threads (notmuch_query_t *query)
405 notmuch_threads_t *threads;
406 notmuch_messages_t *messages;
408 threads = talloc (query, notmuch_threads_t);
411 threads->doc_ids = NULL;
412 talloc_set_destructor (threads, _notmuch_threads_destructor);
414 threads->query = query;
416 messages = notmuch_query_search_messages (query);
417 if (messages == NULL) {
418 talloc_free (threads);
422 threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
423 while (notmuch_messages_valid (messages)) {
424 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
425 g_array_append_val (threads->doc_ids, doc_id);
426 notmuch_messages_move_to_next (messages);
428 threads->doc_id_pos = 0;
430 talloc_free (messages);
432 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
434 talloc_free (threads);
442 notmuch_query_destroy (notmuch_query_t *query)
448 notmuch_threads_valid (notmuch_threads_t *threads)
452 while (threads->doc_id_pos < threads->doc_ids->len) {
453 doc_id = g_array_index (threads->doc_ids, unsigned int,
454 threads->doc_id_pos);
455 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
458 threads->doc_id_pos++;
461 return threads->doc_id_pos < threads->doc_ids->len;
465 notmuch_threads_get (notmuch_threads_t *threads)
469 if (! notmuch_threads_valid (threads))
472 doc_id = g_array_index (threads->doc_ids, unsigned int,
473 threads->doc_id_pos);
474 return _notmuch_thread_create (threads->query,
475 threads->query->notmuch,
478 threads->query->exclude_terms,
479 threads->query->sort);
483 notmuch_threads_move_to_next (notmuch_threads_t *threads)
485 threads->doc_id_pos++;
489 notmuch_threads_destroy (notmuch_threads_t *threads)
491 talloc_free (threads);
495 notmuch_query_count_messages (notmuch_query_t *query)
497 notmuch_database_t *notmuch = query->notmuch;
498 const char *query_string = query->query_string;
499 Xapian::doccount count = 0;
502 Xapian::Enquire enquire (*notmuch->xapian_db);
503 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
504 _find_prefix ("type"),
506 Xapian::Query string_query, final_query, exclude_query;
508 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
509 Xapian::QueryParser::FLAG_PHRASE |
510 Xapian::QueryParser::FLAG_LOVEHATE |
511 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
512 Xapian::QueryParser::FLAG_WILDCARD |
513 Xapian::QueryParser::FLAG_PURE_NOT);
515 if (strcmp (query_string, "") == 0 ||
516 strcmp (query_string, "*") == 0)
518 final_query = mail_query;
520 string_query = notmuch->query_parser->
521 parse_query (query_string, flags);
522 final_query = Xapian::Query (Xapian::Query::OP_AND,
523 mail_query, string_query);
526 exclude_query = _notmuch_exclude_tags (query, final_query);
528 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
529 final_query, exclude_query);
531 enquire.set_weighting_scheme(Xapian::BoolWeight());
532 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
535 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
538 enquire.set_query (final_query);
540 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
542 count = mset.get_matches_estimated();
544 } catch (const Xapian::Error &error) {
545 fprintf (stderr, "A Xapian exception occurred: %s\n",
546 error.get_msg().c_str());
547 fprintf (stderr, "Query string was: %s\n", query->query_string);
554 notmuch_query_count_threads (notmuch_query_t *query)
556 notmuch_messages_t *messages;
562 query->sort = NOTMUCH_SORT_UNSORTED;
563 messages = notmuch_query_search_messages (query);
565 if (messages == NULL)
568 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
570 talloc_free (messages);
574 while (notmuch_messages_valid (messages)) {
575 notmuch_message_t *message = notmuch_messages_get (messages);
576 const char *thread_id = notmuch_message_get_thread_id (message);
577 char *thread_id_copy = talloc_strdup (messages, thread_id);
578 if (unlikely (thread_id_copy == NULL)) {
579 notmuch_message_destroy (message);
583 g_hash_table_insert (hash, thread_id_copy, NULL);
584 notmuch_message_destroy (message);
585 notmuch_messages_move_to_next (messages);
588 count = g_hash_table_size (hash);
591 g_hash_table_unref (hash);
592 talloc_free (messages);