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;
32 typedef struct _notmuch_mset_messages {
33 notmuch_messages_t base;
34 notmuch_database_t *notmuch;
35 Xapian::MSetIterator iterator;
36 Xapian::MSetIterator iterator_end;
37 } notmuch_mset_messages_t;
39 struct _notmuch_doc_id_set {
44 #define DOCIDSET_WORD(bit) ((bit) / sizeof (unsigned int))
45 #define DOCIDSET_BIT(bit) ((bit) % sizeof (unsigned int))
47 struct _notmuch_threads {
48 notmuch_query_t *query;
50 /* The ordered list of doc ids matched by the query. */
52 /* Our iterator's current position in doc_ids. */
53 unsigned int doc_id_pos;
54 /* The set of matched docid's that have not been assigned to a
55 * thread. Initially, this contains every docid in doc_ids. */
56 notmuch_doc_id_set_t match_set;
60 notmuch_query_create (notmuch_database_t *notmuch,
61 const char *query_string)
63 notmuch_query_t *query;
66 fprintf (stderr, "Query string is:\n%s\n", query_string);
69 query = talloc (NULL, notmuch_query_t);
70 if (unlikely (query == NULL))
73 query->notmuch = notmuch;
75 query->query_string = talloc_strdup (query, query_string);
77 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
83 notmuch_query_get_query_string (notmuch_query_t *query)
85 return query->query_string;
89 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
95 notmuch_query_get_sort (notmuch_query_t *query)
100 /* We end up having to call the destructors explicitly because we had
101 * to use "placement new" in order to initialize C++ objects within a
102 * block that we allocated with talloc. So C++ is making talloc
103 * slightly less simple to use, (we wouldn't need
104 * talloc_set_destructor at all otherwise).
107 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
109 messages->iterator.~MSetIterator ();
110 messages->iterator_end.~MSetIterator ();
116 notmuch_query_search_messages (notmuch_query_t *query)
118 notmuch_database_t *notmuch = query->notmuch;
119 const char *query_string = query->query_string;
120 notmuch_mset_messages_t *messages;
122 messages = talloc (query, notmuch_mset_messages_t);
123 if (unlikely (messages == NULL))
128 messages->base.is_of_list_type = FALSE;
129 messages->base.iterator = NULL;
130 messages->notmuch = notmuch;
131 new (&messages->iterator) Xapian::MSetIterator ();
132 new (&messages->iterator_end) Xapian::MSetIterator ();
134 talloc_set_destructor (messages, _notmuch_messages_destructor);
136 Xapian::Enquire enquire (*notmuch->xapian_db);
137 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
138 _find_prefix ("type"),
140 Xapian::Query string_query, final_query;
142 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
143 Xapian::QueryParser::FLAG_PHRASE |
144 Xapian::QueryParser::FLAG_LOVEHATE |
145 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
146 Xapian::QueryParser::FLAG_WILDCARD |
147 Xapian::QueryParser::FLAG_PURE_NOT);
149 if (strcmp (query_string, "") == 0 ||
150 strcmp (query_string, "*") == 0)
152 final_query = mail_query;
154 string_query = notmuch->query_parser->
155 parse_query (query_string, flags);
156 final_query = Xapian::Query (Xapian::Query::OP_AND,
157 mail_query, string_query);
160 enquire.set_weighting_scheme (Xapian::BoolWeight());
162 switch (query->sort) {
163 case NOTMUCH_SORT_OLDEST_FIRST:
164 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
166 case NOTMUCH_SORT_NEWEST_FIRST:
167 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
169 case NOTMUCH_SORT_MESSAGE_ID:
170 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
172 case NOTMUCH_SORT_UNSORTED:
177 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
180 enquire.set_query (final_query);
182 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
184 messages->iterator = mset.begin ();
185 messages->iterator_end = mset.end ();
187 return &messages->base;
189 } catch (const Xapian::Error &error) {
190 fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
191 error.get_msg().c_str());
192 fprintf (stderr, "Query string was: %s\n", query->query_string);
193 notmuch->exception_reported = TRUE;
194 talloc_free (messages);
200 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
202 notmuch_mset_messages_t *mset_messages;
204 mset_messages = (notmuch_mset_messages_t *) messages;
206 return (mset_messages->iterator != mset_messages->iterator_end);
210 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
212 notmuch_mset_messages_t *mset_messages;
214 mset_messages = (notmuch_mset_messages_t *) messages;
216 if (! _notmuch_mset_messages_valid (&mset_messages->base))
219 return *mset_messages->iterator;
223 _notmuch_mset_messages_get (notmuch_messages_t *messages)
225 notmuch_message_t *message;
226 Xapian::docid doc_id;
227 notmuch_private_status_t status;
228 notmuch_mset_messages_t *mset_messages;
230 mset_messages = (notmuch_mset_messages_t *) messages;
232 if (! _notmuch_mset_messages_valid (&mset_messages->base))
235 doc_id = *mset_messages->iterator;
237 message = _notmuch_message_create (mset_messages,
238 mset_messages->notmuch, doc_id,
241 if (message == NULL &&
242 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
244 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
251 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
253 notmuch_mset_messages_t *mset_messages;
255 mset_messages = (notmuch_mset_messages_t *) messages;
257 mset_messages->iterator++;
260 static notmuch_bool_t
261 _notmuch_doc_id_set_init (void *ctx,
262 notmuch_doc_id_set_t *doc_ids,
265 unsigned int max = 0;
266 unsigned int *bitmap;
268 for (unsigned int i = 0; i < arr->len; i++)
269 max = MAX(max, g_array_index (arr, unsigned int, i));
270 bitmap = talloc_zero_array (ctx, unsigned int, 1 + max / sizeof (*bitmap));
275 doc_ids->bitmap = bitmap;
276 doc_ids->bound = max + 1;
278 for (unsigned int i = 0; i < arr->len; i++) {
279 unsigned int doc_id = g_array_index (arr, unsigned int, i);
280 bitmap[DOCIDSET_WORD(doc_id)] |= 1 << DOCIDSET_BIT(doc_id);
287 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
290 if (doc_id >= doc_ids->bound)
292 return doc_ids->bitmap[DOCIDSET_WORD(doc_id)] & (1 << DOCIDSET_BIT(doc_id));
296 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
299 if (doc_id < doc_ids->bound)
300 doc_ids->bitmap[DOCIDSET_WORD(doc_id)] &= ~(1 << DOCIDSET_BIT(doc_id));
303 /* Glib objects force use to use a talloc destructor as well, (but not
304 * nearly as ugly as the for messages due to C++ objects). At
305 * this point, I'd really like to have some talloc-friendly
306 * equivalents for the few pieces of glib that I'm using. */
308 _notmuch_threads_destructor (notmuch_threads_t *threads)
310 if (threads->doc_ids)
311 g_array_unref (threads->doc_ids);
317 notmuch_query_search_threads (notmuch_query_t *query)
319 notmuch_threads_t *threads;
320 notmuch_messages_t *messages;
322 threads = talloc (query, notmuch_threads_t);
325 threads->doc_ids = NULL;
326 talloc_set_destructor (threads, _notmuch_threads_destructor);
328 threads->query = query;
330 messages = notmuch_query_search_messages (query);
331 if (messages == NULL) {
332 talloc_free (threads);
336 threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
337 while (notmuch_messages_valid (messages)) {
338 unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
339 g_array_append_val (threads->doc_ids, doc_id);
340 notmuch_messages_move_to_next (messages);
342 threads->doc_id_pos = 0;
344 talloc_free (messages);
346 if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
348 talloc_free (threads);
356 notmuch_query_destroy (notmuch_query_t *query)
362 notmuch_threads_valid (notmuch_threads_t *threads)
366 while (threads->doc_id_pos < threads->doc_ids->len) {
367 doc_id = g_array_index (threads->doc_ids, unsigned int,
368 threads->doc_id_pos);
369 if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
372 threads->doc_id_pos++;
375 return threads->doc_id_pos < threads->doc_ids->len;
379 notmuch_threads_get (notmuch_threads_t *threads)
383 if (! notmuch_threads_valid (threads))
386 doc_id = g_array_index (threads->doc_ids, unsigned int,
387 threads->doc_id_pos);
388 return _notmuch_thread_create (threads->query,
389 threads->query->notmuch,
392 threads->query->sort);
396 notmuch_threads_move_to_next (notmuch_threads_t *threads)
398 threads->doc_id_pos++;
402 notmuch_threads_destroy (notmuch_threads_t *threads)
404 talloc_free (threads);
408 notmuch_query_count_messages (notmuch_query_t *query)
410 notmuch_database_t *notmuch = query->notmuch;
411 const char *query_string = query->query_string;
412 Xapian::doccount count = 0;
415 Xapian::Enquire enquire (*notmuch->xapian_db);
416 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
417 _find_prefix ("type"),
419 Xapian::Query string_query, final_query;
421 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
422 Xapian::QueryParser::FLAG_PHRASE |
423 Xapian::QueryParser::FLAG_LOVEHATE |
424 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
425 Xapian::QueryParser::FLAG_WILDCARD |
426 Xapian::QueryParser::FLAG_PURE_NOT);
428 if (strcmp (query_string, "") == 0 ||
429 strcmp (query_string, "*") == 0)
431 final_query = mail_query;
433 string_query = notmuch->query_parser->
434 parse_query (query_string, flags);
435 final_query = Xapian::Query (Xapian::Query::OP_AND,
436 mail_query, string_query);
439 enquire.set_weighting_scheme(Xapian::BoolWeight());
440 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
443 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
446 enquire.set_query (final_query);
448 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
450 count = mset.get_matches_estimated();
452 } catch (const Xapian::Error &error) {
453 fprintf (stderr, "A Xapian exception occurred: %s\n",
454 error.get_msg().c_str());
455 fprintf (stderr, "Query string was: %s\n", query->query_string);