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;
33 typedef struct _notmuch_mset_messages {
34 notmuch_messages_t base;
35 notmuch_database_t *notmuch;
36 Xapian::MSetIterator iterator;
37 Xapian::MSetIterator iterator_end;
38 } notmuch_mset_messages_t;
40 struct _notmuch_doc_id_set {
45 #define DOCIDSET_WORD(bit) ((bit) / sizeof (unsigned int))
46 #define DOCIDSET_BIT(bit) ((bit) % sizeof (unsigned int))
48 struct visible _notmuch_threads {
49 notmuch_query_t *query;
51 /* The ordered list of doc ids matched by the query. */
53 /* Our iterator's current position in doc_ids. */
54 unsigned int doc_id_pos;
55 /* The set of matched docid's that have not been assigned to a
56 * thread. Initially, this contains every docid in doc_ids. */
57 notmuch_doc_id_set_t match_set;
61 notmuch_query_create (notmuch_database_t *notmuch,
62 const char *query_string)
64 notmuch_query_t *query;
67 fprintf (stderr, "Query string is:\n%s\n", query_string);
70 query = talloc (NULL, notmuch_query_t);
71 if (unlikely (query == NULL))
74 query->notmuch = notmuch;
76 query->query_string = talloc_strdup (query, query_string);
78 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
80 query->exclude_terms = _notmuch_string_list_create (query);
86 notmuch_query_get_query_string (notmuch_query_t *query)
88 return query->query_string;
92 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
98 notmuch_query_get_sort (notmuch_query_t *query)
104 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
106 char *term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
107 _notmuch_string_list_append (query->exclude_terms, term);
110 /* We end up having to call the destructors explicitly because we had
111 * to use "placement new" in order to initialize C++ objects within a
112 * block that we allocated with talloc. So C++ is making talloc
113 * slightly less simple to use, (we wouldn't need
114 * talloc_set_destructor at all otherwise).
117 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
119 messages->iterator.~MSetIterator ();
120 messages->iterator_end.~MSetIterator ();
125 /* Return a query that matches messages with the excluded tags
126 * registered with query. Any tags that explicitly appear in xquery
127 * will not be excluded. The caller of this function has to combine
128 * the returned query appropriately.*/
130 _notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)
132 Xapian::Query exclude_query = Xapian::Query::MatchNothing;
134 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
136 Xapian::TermIterator it = xquery.get_terms_begin ();
137 Xapian::TermIterator end = xquery.get_terms_end ();
138 for (; it != end; it++) {
139 if ((*it).compare (term->string) == 0)
143 exclude_query = Xapian::Query (Xapian::Query::OP_OR,
144 exclude_query, Xapian::Query (term->string));
146 return exclude_query;
150 notmuch_query_search_messages (notmuch_query_t *query)
152 notmuch_database_t *notmuch = query->notmuch;
153 const char *query_string = query->query_string;
154 notmuch_mset_messages_t *messages;
156 messages = talloc (query, notmuch_mset_messages_t);
157 if (unlikely (messages == NULL))
162 messages->base.is_of_list_type = FALSE;
163 messages->base.iterator = NULL;
164 messages->notmuch = notmuch;
165 new (&messages->iterator) Xapian::MSetIterator ();
166 new (&messages->iterator_end) Xapian::MSetIterator ();
168 talloc_set_destructor (messages, _notmuch_messages_destructor);
170 Xapian::Enquire enquire (*notmuch->xapian_db);
171 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
172 _find_prefix ("type"),
174 Xapian::Query string_query, final_query, exclude_query;
176 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
177 Xapian::QueryParser::FLAG_PHRASE |
178 Xapian::QueryParser::FLAG_LOVEHATE |
179 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
180 Xapian::QueryParser::FLAG_WILDCARD |
181 Xapian::QueryParser::FLAG_PURE_NOT);
183 if (strcmp (query_string, "") == 0 ||
184 strcmp (query_string, "*") == 0)
186 final_query = mail_query;
188 string_query = notmuch->query_parser->
189 parse_query (query_string, flags);
190 final_query = Xapian::Query (Xapian::Query::OP_AND,
191 mail_query, string_query);
194 exclude_query = _notmuch_exclude_tags (query, final_query);
196 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
197 final_query, exclude_query);
199 enquire.set_weighting_scheme (Xapian::BoolWeight());
201 switch (query->sort) {
202 case NOTMUCH_SORT_OLDEST_FIRST:
203 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
205 case NOTMUCH_SORT_NEWEST_FIRST:
206 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
208 case NOTMUCH_SORT_MESSAGE_ID:
209 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
211 case NOTMUCH_SORT_UNSORTED:
216 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
219 enquire.set_query (final_query);
221 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
223 messages->iterator = mset.begin ();
224 messages->iterator_end = mset.end ();
226 return &messages->base;
228 } catch (const Xapian::Error &error) {
229 fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
230 error.get_msg().c_str());
231 fprintf (stderr, "Query string was: %s\n", query->query_string);
232 notmuch->exception_reported = TRUE;
233 talloc_free (messages);
239 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
241 notmuch_mset_messages_t *mset_messages;
243 mset_messages = (notmuch_mset_messages_t *) messages;
245 return (mset_messages->iterator != mset_messages->iterator_end);
249 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
251 notmuch_mset_messages_t *mset_messages;
253 mset_messages = (notmuch_mset_messages_t *) messages;
255 if (! _notmuch_mset_messages_valid (&mset_messages->base))
258 return *mset_messages->iterator;
262 _notmuch_mset_messages_get (notmuch_messages_t *messages)
264 notmuch_message_t *message;
265 Xapian::docid doc_id;
266 notmuch_private_status_t status;
267 notmuch_mset_messages_t *mset_messages;
269 mset_messages = (notmuch_mset_messages_t *) messages;
271 if (! _notmuch_mset_messages_valid (&mset_messages->base))
274 doc_id = *mset_messages->iterator;
276 message = _notmuch_message_create (mset_messages,
277 mset_messages->notmuch, doc_id,
280 if (message == NULL &&
281 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
283 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
290 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
292 notmuch_mset_messages_t *mset_messages;
294 mset_messages = (notmuch_mset_messages_t *) messages;
296 mset_messages->iterator++;
299 static notmuch_bool_t
300 _notmuch_doc_id_set_init (void *ctx,
301 notmuch_doc_id_set_t *doc_ids,
304 unsigned int max = 0;
305 unsigned int *bitmap;
307 for (unsigned int i = 0; i < arr->len; i++)
308 max = MAX(max, g_array_index (arr, unsigned int, i));
309 bitmap = talloc_zero_array (ctx, unsigned int, 1 + max / sizeof (*bitmap));
314 doc_ids->bitmap = bitmap;
315 doc_ids->bound = max + 1;
317 for (unsigned int i = 0; i < arr->len; i++) {
318 unsigned int doc_id = g_array_index (arr, unsigned int, i);
319 bitmap[DOCIDSET_WORD(doc_id)] |= 1 << DOCIDSET_BIT(doc_id);
326 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
329 if (doc_id >= doc_ids->bound)
331 return doc_ids->bitmap[DOCIDSET_WORD(doc_id)] & (1 << DOCIDSET_BIT(doc_id));
335 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
338 if (doc_id < doc_ids->bound)
339 doc_ids->bitmap[DOCIDSET_WORD(doc_id)] &= ~(1 << DOCIDSET_BIT(doc_id));
342 /* Glib objects force use to use a talloc destructor as well, (but not
343 * nearly as ugly as the for messages due to C++ objects). At
344 * this point, I'd really like to have some talloc-friendly
345 * equivalents for the few pieces of glib that I'm using. */
347 _notmuch_threads_destructor (notmuch_threads_t *threads)
349 if (threads->doc_ids)
350 g_array_unref (threads->doc_ids);
356 notmuch_query_search_threads (notmuch_query_t *query)
358 notmuch_threads_t *threads;
359 notmuch_messages_t *messages;
361 threads = talloc (query, notmuch_threads_t);
364 threads->doc_ids = NULL;
365 talloc_set_destructor (threads, _notmuch_threads_destructor);
367 threads->query = query;
369 messages = notmuch_query_search_messages (query);
370 if (messages == NULL) {
371 talloc_free (threads);
375 threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
376 while (notmuch_messages_valid (messages)) {
377 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
378 g_array_append_val (threads->doc_ids, doc_id);
379 notmuch_messages_move_to_next (messages);
381 threads->doc_id_pos = 0;
383 talloc_free (messages);
385 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
387 talloc_free (threads);
395 notmuch_query_destroy (notmuch_query_t *query)
401 notmuch_threads_valid (notmuch_threads_t *threads)
405 while (threads->doc_id_pos < threads->doc_ids->len) {
406 doc_id = g_array_index (threads->doc_ids, unsigned int,
407 threads->doc_id_pos);
408 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
411 threads->doc_id_pos++;
414 return threads->doc_id_pos < threads->doc_ids->len;
418 notmuch_threads_get (notmuch_threads_t *threads)
422 if (! notmuch_threads_valid (threads))
425 doc_id = g_array_index (threads->doc_ids, unsigned int,
426 threads->doc_id_pos);
427 return _notmuch_thread_create (threads->query,
428 threads->query->notmuch,
431 threads->query->sort);
435 notmuch_threads_move_to_next (notmuch_threads_t *threads)
437 threads->doc_id_pos++;
441 notmuch_threads_destroy (notmuch_threads_t *threads)
443 talloc_free (threads);
447 notmuch_query_count_messages (notmuch_query_t *query)
449 notmuch_database_t *notmuch = query->notmuch;
450 const char *query_string = query->query_string;
451 Xapian::doccount count = 0;
454 Xapian::Enquire enquire (*notmuch->xapian_db);
455 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
456 _find_prefix ("type"),
458 Xapian::Query string_query, final_query, exclude_query;
460 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
461 Xapian::QueryParser::FLAG_PHRASE |
462 Xapian::QueryParser::FLAG_LOVEHATE |
463 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
464 Xapian::QueryParser::FLAG_WILDCARD |
465 Xapian::QueryParser::FLAG_PURE_NOT);
467 if (strcmp (query_string, "") == 0 ||
468 strcmp (query_string, "*") == 0)
470 final_query = mail_query;
472 string_query = notmuch->query_parser->
473 parse_query (query_string, flags);
474 final_query = Xapian::Query (Xapian::Query::OP_AND,
475 mail_query, string_query);
478 exclude_query = _notmuch_exclude_tags (query, final_query);
480 final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
481 final_query, exclude_query);
483 enquire.set_weighting_scheme(Xapian::BoolWeight());
484 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
487 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
490 enquire.set_query (final_query);
492 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
494 count = mset.get_matches_estimated();
496 } catch (const Xapian::Error &error) {
497 fprintf (stderr, "A Xapian exception occurred: %s\n",
498 error.get_msg().c_str());
499 fprintf (stderr, "Query string was: %s\n", query->query_string);
506 notmuch_query_count_threads (notmuch_query_t *query)
508 notmuch_messages_t *messages;
514 query->sort = NOTMUCH_SORT_UNSORTED;
515 messages = notmuch_query_search_messages (query);
517 if (messages == NULL)
520 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
522 talloc_free (messages);
526 while (notmuch_messages_valid (messages)) {
527 notmuch_message_t *message = notmuch_messages_get (messages);
528 const char *thread_id = notmuch_message_get_thread_id (message);
529 char *thread_id_copy = talloc_strdup (messages, thread_id);
530 if (unlikely (thread_id_copy == NULL)) {
531 notmuch_message_destroy (message);
535 g_hash_table_insert (hash, thread_id_copy, NULL);
536 notmuch_message_destroy (message);
537 notmuch_messages_move_to_next (messages);
540 count = g_hash_table_size (hash);
543 g_hash_table_unref (hash);
544 talloc_free (messages);