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 */
28 struct _notmuch_query {
29 notmuch_database_t *notmuch;
30 const char *query_string;
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_threads {
42 notmuch_query_t *query;
44 notmuch_messages_t *messages;
46 /* This thread ID is our iterator state. */
47 const char *thread_id;
51 notmuch_query_create (notmuch_database_t *notmuch,
52 const char *query_string)
54 notmuch_query_t *query;
57 fprintf (stderr, "Query string is:\n%s\n", query_string);
60 query = talloc (NULL, notmuch_query_t);
61 if (unlikely (query == NULL))
64 query->notmuch = notmuch;
66 query->query_string = talloc_strdup (query, query_string);
68 query->sort = NOTMUCH_SORT_NEWEST_FIRST;
74 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
79 /* We end up having to call the destructors explicitly because we had
80 * to use "placement new" in order to initialize C++ objects within a
81 * block that we allocated with talloc. So C++ is making talloc
82 * slightly less simple to use, (we wouldn't need
83 * talloc_set_destructor at all otherwise).
86 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
88 messages->iterator.~MSetIterator ();
89 messages->iterator_end.~MSetIterator ();
95 notmuch_query_search_messages (notmuch_query_t *query)
97 notmuch_database_t *notmuch = query->notmuch;
98 const char *query_string = query->query_string;
99 notmuch_mset_messages_t *messages;
101 messages = talloc (query, notmuch_mset_messages_t);
102 if (unlikely (messages == NULL))
107 messages->base.is_of_list_type = FALSE;
108 messages->base.iterator = NULL;
109 messages->notmuch = notmuch;
110 new (&messages->iterator) Xapian::MSetIterator ();
111 new (&messages->iterator_end) Xapian::MSetIterator ();
113 talloc_set_destructor (messages, _notmuch_messages_destructor);
115 Xapian::Enquire enquire (*notmuch->xapian_db);
116 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
117 _find_prefix ("type"),
119 Xapian::Query string_query, final_query;
121 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
122 Xapian::QueryParser::FLAG_PHRASE |
123 Xapian::QueryParser::FLAG_LOVEHATE |
124 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
125 Xapian::QueryParser::FLAG_WILDCARD |
126 Xapian::QueryParser::FLAG_PURE_NOT);
128 if (strcmp (query_string, "") == 0) {
129 final_query = mail_query;
131 string_query = notmuch->query_parser->
132 parse_query (query_string, flags);
133 final_query = Xapian::Query (Xapian::Query::OP_AND,
134 mail_query, string_query);
137 switch (query->sort) {
138 case NOTMUCH_SORT_OLDEST_FIRST:
139 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
141 case NOTMUCH_SORT_NEWEST_FIRST:
142 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
144 case NOTMUCH_SORT_MESSAGE_ID:
145 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
150 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
153 enquire.set_query (final_query);
155 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
157 messages->iterator = mset.begin ();
158 messages->iterator_end = mset.end ();
160 } catch (const Xapian::Error &error) {
161 fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
162 error.get_msg().c_str());
163 fprintf (stderr, "Query string was: %s\n", query->query_string);
164 notmuch->exception_reported = TRUE;
167 return &messages->base;
171 _notmuch_mset_messages_has_more (notmuch_messages_t *messages)
173 notmuch_mset_messages_t *mset_messages;
175 mset_messages = (notmuch_mset_messages_t *) messages;
177 return (mset_messages->iterator != mset_messages->iterator_end);
181 _notmuch_mset_messages_get (notmuch_messages_t *messages)
183 notmuch_message_t *message;
184 Xapian::docid doc_id;
185 notmuch_private_status_t status;
186 notmuch_mset_messages_t *mset_messages;
188 mset_messages = (notmuch_mset_messages_t *) messages;
190 if (! _notmuch_mset_messages_has_more (&mset_messages->base))
193 doc_id = *mset_messages->iterator;
195 message = _notmuch_message_create (mset_messages,
196 mset_messages->notmuch, doc_id,
199 if (message == NULL &&
200 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
202 INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
209 _notmuch_mset_messages_advance (notmuch_messages_t *messages)
211 notmuch_mset_messages_t *mset_messages;
213 mset_messages = (notmuch_mset_messages_t *) messages;
215 mset_messages->iterator++;
218 /* Glib objects force use to use a talloc destructor as well, (but not
219 * nearly as ugly as the for messages due to C++ objects). At
220 * this point, I'd really like to have some talloc-friendly
221 * equivalents for the few pieces of glib that I'm using. */
223 _notmuch_threads_destructor (notmuch_threads_t *threads)
225 g_hash_table_unref (threads->threads);
231 notmuch_query_search_threads (notmuch_query_t *query)
233 notmuch_threads_t *threads;
235 threads = talloc (query, notmuch_threads_t);
239 threads->query = query;
240 threads->threads = g_hash_table_new_full (g_str_hash, g_str_equal,
243 threads->messages = notmuch_query_search_messages (query);
245 threads->thread_id = NULL;
247 talloc_set_destructor (threads, _notmuch_threads_destructor);
253 notmuch_query_destroy (notmuch_query_t *query)
259 notmuch_threads_has_more (notmuch_threads_t *threads)
261 notmuch_message_t *message;
263 if (threads->thread_id)
266 while (notmuch_messages_has_more (threads->messages))
268 message = notmuch_messages_get (threads->messages);
270 threads->thread_id = notmuch_message_get_thread_id (message);
272 if (! g_hash_table_lookup_extended (threads->threads,
276 g_hash_table_insert (threads->threads,
277 xstrdup (threads->thread_id), NULL);
278 notmuch_messages_advance (threads->messages);
282 notmuch_messages_advance (threads->messages);
285 threads->thread_id = NULL;
290 notmuch_threads_get (notmuch_threads_t *threads)
292 if (! notmuch_threads_has_more (threads))
295 return _notmuch_thread_create (threads->query,
296 threads->query->notmuch,
298 threads->query->query_string);
302 notmuch_threads_advance (notmuch_threads_t *threads)
304 threads->thread_id = NULL;
308 notmuch_threads_destroy (notmuch_threads_t *threads)
310 talloc_free (threads);
314 notmuch_query_count_messages (notmuch_query_t *query)
316 notmuch_database_t *notmuch = query->notmuch;
317 const char *query_string = query->query_string;
318 Xapian::doccount count = 0;
321 Xapian::Enquire enquire (*notmuch->xapian_db);
322 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
323 _find_prefix ("type"),
325 Xapian::Query string_query, final_query;
327 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
328 Xapian::QueryParser::FLAG_PHRASE |
329 Xapian::QueryParser::FLAG_LOVEHATE |
330 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
331 Xapian::QueryParser::FLAG_WILDCARD |
332 Xapian::QueryParser::FLAG_PURE_NOT);
334 if (strcmp (query_string, "") == 0) {
335 final_query = mail_query;
337 string_query = notmuch->query_parser->
338 parse_query (query_string, flags);
339 final_query = Xapian::Query (Xapian::Query::OP_AND,
340 mail_query, string_query);
343 enquire.set_weighting_scheme(Xapian::BoolWeight());
344 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
347 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
350 enquire.set_query (final_query);
352 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
354 count = mset.get_matches_estimated();
356 } catch (const Xapian::Error &error) {
357 fprintf (stderr, "A Xapian exception occurred: %s\n",
358 error.get_msg().c_str());
359 fprintf (stderr, "Query string was: %s\n", query->query_string);