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_threads {
40 notmuch_query_t *query;
42 notmuch_messages_t *messages;
44 /* This thread ID is our iterator state. */
45 const char *thread_id;
49 notmuch_query_create (notmuch_database_t *notmuch,
50 const char *query_string)
52 notmuch_query_t *query;
55 fprintf (stderr, "Query string is:\n%s\n", query_string);
58 query = talloc (NULL, notmuch_query_t);
59 if (unlikely (query == NULL))
62 query->notmuch = notmuch;
64 query->query_string = talloc_strdup (query, query_string);
66 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
72 notmuch_query_get_query_string (notmuch_query_t *query)
74 return query->query_string;
78 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
84 notmuch_query_get_sort (notmuch_query_t *query)
89 /* We end up having to call the destructors explicitly because we had
90 * to use "placement new" in order to initialize C++ objects within a
91 * block that we allocated with talloc. So C++ is making talloc
92 * slightly less simple to use, (we wouldn't need
93 * talloc_set_destructor at all otherwise).
96 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
98 messages->iterator.~MSetIterator ();
99 messages->iterator_end.~MSetIterator ();
105 notmuch_query_search_messages (notmuch_query_t *query)
107 notmuch_database_t *notmuch = query->notmuch;
108 const char *query_string = query->query_string;
109 notmuch_mset_messages_t *messages;
111 messages = talloc (query, notmuch_mset_messages_t);
112 if (unlikely (messages == NULL))
117 messages->base.is_of_list_type = FALSE;
118 messages->base.iterator = NULL;
119 messages->notmuch = notmuch;
120 new (&messages->iterator) Xapian::MSetIterator ();
121 new (&messages->iterator_end) Xapian::MSetIterator ();
123 talloc_set_destructor (messages, _notmuch_messages_destructor);
125 Xapian::Enquire enquire (*notmuch->xapian_db);
126 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
127 _find_prefix ("type"),
129 Xapian::Query string_query, final_query;
131 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
132 Xapian::QueryParser::FLAG_PHRASE |
133 Xapian::QueryParser::FLAG_LOVEHATE |
134 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
135 Xapian::QueryParser::FLAG_WILDCARD |
136 Xapian::QueryParser::FLAG_PURE_NOT);
138 if (strcmp (query_string, "") == 0 ||
139 strcmp (query_string, "*") == 0)
141 final_query = mail_query;
143 string_query = notmuch->query_parser->
144 parse_query (query_string, flags);
145 final_query = Xapian::Query (Xapian::Query::OP_AND,
146 mail_query, string_query);
149 enquire.set_weighting_scheme (Xapian::BoolWeight());
151 switch (query->sort) {
152 case NOTMUCH_SORT_OLDEST_FIRST:
153 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
155 case NOTMUCH_SORT_NEWEST_FIRST:
156 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
158 case NOTMUCH_SORT_MESSAGE_ID:
159 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
161 case NOTMUCH_SORT_UNSORTED:
166 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
169 enquire.set_query (final_query);
171 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
173 messages->iterator = mset.begin ();
174 messages->iterator_end = mset.end ();
176 return &messages->base;
178 } catch (const Xapian::Error &error) {
179 fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
180 error.get_msg().c_str());
181 fprintf (stderr, "Query string was: %s\n", query->query_string);
182 notmuch->exception_reported = TRUE;
183 talloc_free (messages);
189 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
191 notmuch_mset_messages_t *mset_messages;
193 mset_messages = (notmuch_mset_messages_t *) messages;
195 return (mset_messages->iterator != mset_messages->iterator_end);
199 _notmuch_mset_messages_get (notmuch_messages_t *messages)
201 notmuch_message_t *message;
202 Xapian::docid doc_id;
203 notmuch_private_status_t status;
204 notmuch_mset_messages_t *mset_messages;
206 mset_messages = (notmuch_mset_messages_t *) messages;
208 if (! _notmuch_mset_messages_valid (&mset_messages->base))
211 doc_id = *mset_messages->iterator;
213 message = _notmuch_message_create (mset_messages,
214 mset_messages->notmuch, doc_id,
217 if (message == NULL &&
218 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
220 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
227 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
229 notmuch_mset_messages_t *mset_messages;
231 mset_messages = (notmuch_mset_messages_t *) messages;
233 mset_messages->iterator++;
236 /* Glib objects force use to use a talloc destructor as well, (but not
237 * nearly as ugly as the for messages due to C++ objects). At
238 * this point, I'd really like to have some talloc-friendly
239 * equivalents for the few pieces of glib that I'm using. */
241 _notmuch_threads_destructor (notmuch_threads_t *threads)
243 g_hash_table_unref (threads->threads);
249 notmuch_query_search_threads (notmuch_query_t *query)
251 notmuch_threads_t *threads;
253 threads = talloc (query, notmuch_threads_t);
257 threads->query = query;
258 threads->threads = g_hash_table_new_full (g_str_hash, g_str_equal,
261 threads->messages = notmuch_query_search_messages (query);
262 if (threads->messages == NULL) {
263 talloc_free (threads);
267 threads->thread_id = NULL;
269 talloc_set_destructor (threads, _notmuch_threads_destructor);
275 notmuch_query_destroy (notmuch_query_t *query)
281 notmuch_threads_valid (notmuch_threads_t *threads)
283 notmuch_message_t *message;
285 if (threads->thread_id)
288 while (notmuch_messages_valid (threads->messages))
290 message = notmuch_messages_get (threads->messages);
292 threads->thread_id = notmuch_message_get_thread_id (message);
294 if (! g_hash_table_lookup_extended (threads->threads,
298 g_hash_table_insert (threads->threads,
299 xstrdup (threads->thread_id), NULL);
300 notmuch_messages_move_to_next (threads->messages);
304 notmuch_messages_move_to_next (threads->messages);
307 threads->thread_id = NULL;
312 notmuch_threads_get (notmuch_threads_t *threads)
314 if (! notmuch_threads_valid (threads))
317 return _notmuch_thread_create (threads->query,
318 threads->query->notmuch,
320 threads->query->query_string,
321 threads->query->sort);
325 notmuch_threads_move_to_next (notmuch_threads_t *threads)
327 threads->thread_id = NULL;
331 notmuch_threads_destroy (notmuch_threads_t *threads)
333 talloc_free (threads);
337 notmuch_query_count_messages (notmuch_query_t *query)
339 notmuch_database_t *notmuch = query->notmuch;
340 const char *query_string = query->query_string;
341 Xapian::doccount count = 0;
344 Xapian::Enquire enquire (*notmuch->xapian_db);
345 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
346 _find_prefix ("type"),
348 Xapian::Query string_query, final_query;
350 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
351 Xapian::QueryParser::FLAG_PHRASE |
352 Xapian::QueryParser::FLAG_LOVEHATE |
353 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
354 Xapian::QueryParser::FLAG_WILDCARD |
355 Xapian::QueryParser::FLAG_PURE_NOT);
357 if (strcmp (query_string, "") == 0 ||
358 strcmp (query_string, "*") == 0)
360 final_query = mail_query;
362 string_query = notmuch->query_parser->
363 parse_query (query_string, flags);
364 final_query = Xapian::Query (Xapian::Query::OP_AND,
365 mail_query, string_query);
368 enquire.set_weighting_scheme(Xapian::BoolWeight());
369 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
372 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
375 enquire.set_query (final_query);
377 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
379 count = mset.get_matches_estimated();
381 } catch (const Xapian::Error &error) {
382 fprintf (stderr, "A Xapian exception occurred: %s\n",
383 error.get_msg().c_str());
384 fprintf (stderr, "Query string was: %s\n", query->query_string);