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 struct _notmuch_message_results {
35 notmuch_database_t *notmuch;
36 Xapian::MSetIterator iterator;
37 Xapian::MSetIterator iterator_end;
40 struct _notmuch_thread_results {
41 notmuch_database_t *notmuch;
42 GPtrArray *thread_ids;
47 notmuch_query_create (notmuch_database_t *notmuch,
48 const char *query_string)
50 notmuch_query_t *query;
53 fprintf (stderr, "Query string is:\n%s\n", query_string);
56 query = talloc (NULL, notmuch_query_t);
57 if (unlikely (query == NULL))
60 query->notmuch = notmuch;
62 query->query_string = talloc_strdup (query, query_string);
64 query->sort = NOTMUCH_SORT_DATE_OLDEST_FIRST;
70 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
75 /* We end up having to call the destructors explicitly because we had
76 * to use "placement new" in order to initialize C++ objects within a
77 * block that we allocated with talloc. So C++ is making talloc
78 * slightly less simple to use, (we wouldn't need
79 * talloc_set_destructor at all otherwise).
82 _notmuch_message_results_destructor (notmuch_message_results_t *results)
84 results->iterator.~MSetIterator ();
85 results->iterator_end.~MSetIterator ();
90 notmuch_message_results_t *
91 notmuch_query_search_messages (notmuch_query_t *query)
93 notmuch_database_t *notmuch = query->notmuch;
94 const char *query_string = query->query_string;
95 notmuch_message_results_t *results;
97 results = talloc (query, notmuch_message_results_t);
98 if (unlikely (results == NULL))
102 Xapian::Enquire enquire (*notmuch->xapian_db);
103 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
104 _find_prefix ("type"),
106 Xapian::Query string_query, final_query;
108 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
109 Xapian::QueryParser::FLAG_PHRASE |
110 Xapian::QueryParser::FLAG_LOVEHATE |
111 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
112 Xapian::QueryParser::FLAG_WILDCARD);
114 if (strcmp (query_string, "") == 0) {
115 final_query = mail_query;
117 string_query = notmuch->query_parser->
118 parse_query (query_string, flags);
119 final_query = Xapian::Query (Xapian::Query::OP_AND,
120 mail_query, string_query);
123 switch (query->sort) {
124 case NOTMUCH_SORT_DATE_OLDEST_FIRST:
125 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
127 case NOTMUCH_SORT_DATE_NEWEST_FIRST:
128 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
130 case NOTMUCH_SORT_MESSAGE_ID:
131 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
136 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
139 enquire.set_query (final_query);
141 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
143 results->notmuch = notmuch;
145 new (&results->iterator) Xapian::MSetIterator ();
146 new (&results->iterator_end) Xapian::MSetIterator ();
148 talloc_set_destructor (results, _notmuch_message_results_destructor);
150 results->iterator = mset.begin ();
151 results->iterator_end = mset.end ();
153 } catch (const Xapian::Error &error) {
154 fprintf (stderr, "A Xapian exception occurred: %s\n",
155 error.get_msg().c_str());
161 notmuch_thread_results_t *
162 notmuch_query_search_threads (notmuch_query_t *query)
164 notmuch_thread_results_t *thread_results;
165 notmuch_message_results_t *message_results;
166 notmuch_message_t *message;
167 const char *thread_id;
170 thread_results = talloc (query, notmuch_thread_results_t);
171 if (thread_results == NULL)
174 thread_results->notmuch = query->notmuch;
175 thread_results->thread_ids = g_ptr_array_new ();
176 thread_results->index = 0;
178 seen = g_hash_table_new_full (g_str_hash, g_str_equal,
181 for (message_results = notmuch_query_search_messages (query);
182 notmuch_message_results_has_more (message_results);
183 notmuch_message_results_advance (message_results))
185 message = notmuch_message_results_get (message_results);
186 thread_id = notmuch_message_get_thread_id (message);
188 if (g_hash_table_lookup_extended (seen,
189 thread_id, NULL, NULL))
194 g_hash_table_insert (seen, xstrdup (thread_id), NULL);
196 g_ptr_array_add (thread_results->thread_ids,
197 talloc_strdup (thread_results, thread_id));
200 g_hash_table_unref (seen);
202 return thread_results;
206 notmuch_query_destroy (notmuch_query_t *query)
212 notmuch_message_results_has_more (notmuch_message_results_t *results)
214 return (results->iterator != results->iterator_end);
218 notmuch_message_results_get (notmuch_message_results_t *results)
220 notmuch_message_t *message;
221 Xapian::docid doc_id;
222 notmuch_private_status_t status;
224 if (! notmuch_message_results_has_more (results))
227 doc_id = *results->iterator;
229 message = _notmuch_message_create (results,
230 results->notmuch, doc_id,
233 if (message == NULL &&
234 status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
236 INTERNAL_ERROR ("a results iterator contains a non-existent document ID.\n");
243 notmuch_message_results_advance (notmuch_message_results_t *results)
249 notmuch_message_results_destroy (notmuch_message_results_t *results)
251 talloc_free (results);
255 notmuch_thread_results_has_more (notmuch_thread_results_t *results)
257 return (results->index < results->thread_ids->len);
261 notmuch_thread_results_get (notmuch_thread_results_t *results)
263 notmuch_thread_t *thread;
264 const char *thread_id;
266 if (! notmuch_thread_results_has_more (results))
269 thread_id = (const char *) g_ptr_array_index (results->thread_ids,
272 thread = _notmuch_thread_create (results,
280 notmuch_thread_results_advance (notmuch_thread_results_t *results)
286 notmuch_thread_results_destroy (notmuch_thread_results_t *results)
288 g_ptr_array_free (results->thread_ids, TRUE);
289 talloc_free (results);