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 does not match messages with the excluded tags
126 * registered with the query. Any tags that explicitly appear in
127 * xquery will not be excluded. */
129 _notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)
131 for (notmuch_string_node_t *term = query->exclude_terms->head; term;
133 Xapian::TermIterator it = xquery.get_terms_begin ();
134 Xapian::TermIterator end = xquery.get_terms_end ();
135 for (; it != end; it++) {
136 if ((*it).compare (term->string) == 0)
140 xquery = Xapian::Query (Xapian::Query::OP_AND_NOT,
141 xquery, Xapian::Query (term->string));
147 notmuch_query_search_messages (notmuch_query_t *query)
149 notmuch_database_t *notmuch = query->notmuch;
150 const char *query_string = query->query_string;
151 notmuch_mset_messages_t *messages;
153 messages = talloc (query, notmuch_mset_messages_t);
154 if (unlikely (messages == NULL))
159 messages->base.is_of_list_type = FALSE;
160 messages->base.iterator = NULL;
161 messages->notmuch = notmuch;
162 new (&messages->iterator) Xapian::MSetIterator ();
163 new (&messages->iterator_end) Xapian::MSetIterator ();
165 talloc_set_destructor (messages, _notmuch_messages_destructor);
167 Xapian::Enquire enquire (*notmuch->xapian_db);
168 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
169 _find_prefix ("type"),
171 Xapian::Query string_query, final_query;
173 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
174 Xapian::QueryParser::FLAG_PHRASE |
175 Xapian::QueryParser::FLAG_LOVEHATE |
176 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
177 Xapian::QueryParser::FLAG_WILDCARD |
178 Xapian::QueryParser::FLAG_PURE_NOT);
180 if (strcmp (query_string, "") == 0 ||
181 strcmp (query_string, "*") == 0)
183 final_query = mail_query;
185 string_query = notmuch->query_parser->
186 parse_query (query_string, flags);
187 final_query = Xapian::Query (Xapian::Query::OP_AND,
188 mail_query, string_query);
191 final_query = _notmuch_exclude_tags (query, final_query);
193 enquire.set_weighting_scheme (Xapian::BoolWeight());
195 switch (query->sort) {
196 case NOTMUCH_SORT_OLDEST_FIRST:
197 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
199 case NOTMUCH_SORT_NEWEST_FIRST:
200 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
202 case NOTMUCH_SORT_MESSAGE_ID:
203 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
205 case NOTMUCH_SORT_UNSORTED:
210 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
213 enquire.set_query (final_query);
215 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
217 messages->iterator = mset.begin ();
218 messages->iterator_end = mset.end ();
220 return &messages->base;
222 } catch (const Xapian::Error &error) {
223 fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
224 error.get_msg().c_str());
225 fprintf (stderr, "Query string was: %s\n", query->query_string);
226 notmuch->exception_reported = TRUE;
227 talloc_free (messages);
233 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
235 notmuch_mset_messages_t *mset_messages;
237 mset_messages = (notmuch_mset_messages_t *) messages;
239 return (mset_messages->iterator != mset_messages->iterator_end);
243 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
245 notmuch_mset_messages_t *mset_messages;
247 mset_messages = (notmuch_mset_messages_t *) messages;
249 if (! _notmuch_mset_messages_valid (&mset_messages->base))
252 return *mset_messages->iterator;
256 _notmuch_mset_messages_get (notmuch_messages_t *messages)
258 notmuch_message_t *message;
259 Xapian::docid doc_id;
260 notmuch_private_status_t status;
261 notmuch_mset_messages_t *mset_messages;
263 mset_messages = (notmuch_mset_messages_t *) messages;
265 if (! _notmuch_mset_messages_valid (&mset_messages->base))
268 doc_id = *mset_messages->iterator;
270 message = _notmuch_message_create (mset_messages,
271 mset_messages->notmuch, doc_id,
274 if (message == NULL &&
275 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
277 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
284 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
286 notmuch_mset_messages_t *mset_messages;
288 mset_messages = (notmuch_mset_messages_t *) messages;
290 mset_messages->iterator++;
293 static notmuch_bool_t
294 _notmuch_doc_id_set_init (void *ctx,
295 notmuch_doc_id_set_t *doc_ids,
298 unsigned int max = 0;
299 unsigned int *bitmap;
301 for (unsigned int i = 0; i < arr->len; i++)
302 max = MAX(max, g_array_index (arr, unsigned int, i));
303 bitmap = talloc_zero_array (ctx, unsigned int, 1 + max / sizeof (*bitmap));
308 doc_ids->bitmap = bitmap;
309 doc_ids->bound = max + 1;
311 for (unsigned int i = 0; i < arr->len; i++) {
312 unsigned int doc_id = g_array_index (arr, unsigned int, i);
313 bitmap[DOCIDSET_WORD(doc_id)] |= 1 << DOCIDSET_BIT(doc_id);
320 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
323 if (doc_id >= doc_ids->bound)
325 return doc_ids->bitmap[DOCIDSET_WORD(doc_id)] & (1 << DOCIDSET_BIT(doc_id));
329 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
332 if (doc_id < doc_ids->bound)
333 doc_ids->bitmap[DOCIDSET_WORD(doc_id)] &= ~(1 << DOCIDSET_BIT(doc_id));
336 /* Glib objects force use to use a talloc destructor as well, (but not
337 * nearly as ugly as the for messages due to C++ objects). At
338 * this point, I'd really like to have some talloc-friendly
339 * equivalents for the few pieces of glib that I'm using. */
341 _notmuch_threads_destructor (notmuch_threads_t *threads)
343 if (threads->doc_ids)
344 g_array_unref (threads->doc_ids);
350 notmuch_query_search_threads (notmuch_query_t *query)
352 notmuch_threads_t *threads;
353 notmuch_messages_t *messages;
355 threads = talloc (query, notmuch_threads_t);
358 threads->doc_ids = NULL;
359 talloc_set_destructor (threads, _notmuch_threads_destructor);
361 threads->query = query;
363 messages = notmuch_query_search_messages (query);
364 if (messages == NULL) {
365 talloc_free (threads);
369 threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
370 while (notmuch_messages_valid (messages)) {
371 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
372 g_array_append_val (threads->doc_ids, doc_id);
373 notmuch_messages_move_to_next (messages);
375 threads->doc_id_pos = 0;
377 talloc_free (messages);
379 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
381 talloc_free (threads);
389 notmuch_query_destroy (notmuch_query_t *query)
395 notmuch_threads_valid (notmuch_threads_t *threads)
399 while (threads->doc_id_pos < threads->doc_ids->len) {
400 doc_id = g_array_index (threads->doc_ids, unsigned int,
401 threads->doc_id_pos);
402 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
405 threads->doc_id_pos++;
408 return threads->doc_id_pos < threads->doc_ids->len;
412 notmuch_threads_get (notmuch_threads_t *threads)
416 if (! notmuch_threads_valid (threads))
419 doc_id = g_array_index (threads->doc_ids, unsigned int,
420 threads->doc_id_pos);
421 return _notmuch_thread_create (threads->query,
422 threads->query->notmuch,
425 threads->query->sort);
429 notmuch_threads_move_to_next (notmuch_threads_t *threads)
431 threads->doc_id_pos++;
435 notmuch_threads_destroy (notmuch_threads_t *threads)
437 talloc_free (threads);
441 notmuch_query_count_messages (notmuch_query_t *query)
443 notmuch_database_t *notmuch = query->notmuch;
444 const char *query_string = query->query_string;
445 Xapian::doccount count = 0;
448 Xapian::Enquire enquire (*notmuch->xapian_db);
449 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
450 _find_prefix ("type"),
452 Xapian::Query string_query, final_query;
454 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
455 Xapian::QueryParser::FLAG_PHRASE |
456 Xapian::QueryParser::FLAG_LOVEHATE |
457 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
458 Xapian::QueryParser::FLAG_WILDCARD |
459 Xapian::QueryParser::FLAG_PURE_NOT);
461 if (strcmp (query_string, "") == 0 ||
462 strcmp (query_string, "*") == 0)
464 final_query = mail_query;
466 string_query = notmuch->query_parser->
467 parse_query (query_string, flags);
468 final_query = Xapian::Query (Xapian::Query::OP_AND,
469 mail_query, string_query);
472 final_query = _notmuch_exclude_tags (query, final_query);
474 enquire.set_weighting_scheme(Xapian::BoolWeight());
475 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
478 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
481 enquire.set_query (final_query);
483 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
485 count = mset.get_matches_estimated();
487 } catch (const Xapian::Error &error) {
488 fprintf (stderr, "A Xapian exception occurred: %s\n",
489 error.get_msg().c_str());
490 fprintf (stderr, "Query string was: %s\n", query->query_string);
497 notmuch_query_count_threads (notmuch_query_t *query)
499 notmuch_messages_t *messages;
505 query->sort = NOTMUCH_SORT_UNSORTED;
506 messages = notmuch_query_search_messages (query);
508 if (messages == NULL)
511 hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
513 talloc_free (messages);
517 while (notmuch_messages_valid (messages)) {
518 notmuch_message_t *message = notmuch_messages_get (messages);
519 const char *thread_id = notmuch_message_get_thread_id (message);
520 char *thread_id_copy = talloc_strdup (messages, thread_id);
521 if (unlikely (thread_id_copy == NULL)) {
522 notmuch_message_destroy (message);
526 g_hash_table_insert (hash, thread_id_copy, NULL);
527 notmuch_message_destroy (message);
528 notmuch_messages_move_to_next (messages);
531 count = g_hash_table_size (hash);
534 g_hash_table_unref (hash);
535 talloc_free (messages);