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_messages {
35 notmuch_database_t *notmuch;
36 Xapian::MSetIterator iterator;
37 Xapian::MSetIterator iterator_end;
40 struct _notmuch_threads {
41 notmuch_database_t *notmuch;
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_NEWEST_FIRST;
70 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
76 notmuch_query_search_messages (notmuch_query_t *query,
80 notmuch_database_t *notmuch = query->notmuch;
81 const char *query_string = query->query_string;
82 notmuch_message_list_t *message_list;
83 Xapian::MSetIterator i;
85 message_list = _notmuch_message_list_create (query);
86 if (unlikely (message_list == NULL))
90 Xapian::Enquire enquire (*notmuch->xapian_db);
91 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
92 _find_prefix ("type"),
94 Xapian::Query string_query, final_query;
96 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
97 Xapian::QueryParser::FLAG_PHRASE |
98 Xapian::QueryParser::FLAG_LOVEHATE |
99 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
100 Xapian::QueryParser::FLAG_WILDCARD |
101 Xapian::QueryParser::FLAG_PURE_NOT);
103 if (strcmp (query_string, "") == 0) {
104 final_query = mail_query;
106 string_query = notmuch->query_parser->
107 parse_query (query_string, flags);
108 final_query = Xapian::Query (Xapian::Query::OP_AND,
109 mail_query, string_query);
112 switch (query->sort) {
113 case NOTMUCH_SORT_OLDEST_FIRST:
114 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
116 case NOTMUCH_SORT_NEWEST_FIRST:
117 enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
119 case NOTMUCH_SORT_MESSAGE_ID:
120 enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
125 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
128 enquire.set_query (final_query);
130 if (max_messages == -1)
131 max_messages = notmuch->xapian_db->get_doccount ();
132 mset = enquire.get_mset (first, max_messages);
134 for (i = mset.begin (); i != mset.end (); i++) {
135 notmuch_message_t *message;
136 notmuch_private_status_t status;
138 message = _notmuch_message_create (message_list, notmuch,
142 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
143 INTERNAL_ERROR ("A message iterator contains a "
144 "non-existent document ID.\n");
148 _notmuch_message_list_add_message (message_list, message);
151 } catch (const Xapian::Error &error) {
152 fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
153 error.get_msg().c_str());
154 fprintf (stderr, "Query string was: %s\n", query->query_string);
155 notmuch->exception_reported = TRUE;
158 return _notmuch_messages_create (message_list);
161 /* Glib objects force use to use a talloc destructor as well, (but not
162 * nearly as ugly as the for messages due to C++ objects). At
163 * this point, I'd really like to have some talloc-friendly
164 * equivalents for the few pieces of glib that I'm using. */
166 _notmuch_threads_destructor (notmuch_threads_t *threads)
168 g_ptr_array_free (threads->threads, TRUE);
174 notmuch_query_search_threads (notmuch_query_t *query,
178 notmuch_threads_t *threads;
179 notmuch_thread_t *thread;
180 const char *thread_id;
181 notmuch_messages_t *messages;
182 notmuch_message_t *message;
184 int messages_seen = 0, threads_seen = 0;
186 threads = talloc (query, notmuch_threads_t);
190 threads->notmuch = query->notmuch;
191 threads->threads = g_ptr_array_new ();
194 talloc_set_destructor (threads, _notmuch_threads_destructor);
196 seen = g_hash_table_new_full (g_str_hash, g_str_equal,
199 while (max_threads < 0 || threads_seen < first + max_threads)
201 int messages_seen_previously = messages_seen;
203 for (messages = notmuch_query_search_messages (query,
206 notmuch_messages_has_more (messages);
207 notmuch_messages_advance (messages))
209 message = notmuch_messages_get (messages);
211 thread_id = notmuch_message_get_thread_id (message);
213 if (! g_hash_table_lookup_extended (seen,
217 if (threads_seen >= first) {
218 thread = _notmuch_thread_create (query, query->notmuch,
220 query->query_string);
221 g_ptr_array_add (threads->threads, thread);
226 g_hash_table_insert (seen, xstrdup (thread_id), thread);
231 notmuch_message_destroy (message);
235 if (max_threads >= 0 && threads_seen >= first + max_threads)
239 /* Stop if we're not seeing any more messages. */
240 if (messages_seen == messages_seen_previously)
244 g_hash_table_unref (seen);
250 notmuch_query_destroy (notmuch_query_t *query)
256 notmuch_threads_has_more (notmuch_threads_t *threads)
258 return (threads->index < threads->threads->len);
262 notmuch_threads_get (notmuch_threads_t *threads)
264 if (! notmuch_threads_has_more (threads))
267 return (notmuch_thread_t *) g_ptr_array_index (threads->threads,
272 notmuch_threads_advance (notmuch_threads_t *threads)
278 notmuch_threads_destroy (notmuch_threads_t *threads)
280 talloc_free (threads);
284 notmuch_query_count_messages (notmuch_query_t *query)
286 notmuch_database_t *notmuch = query->notmuch;
287 const char *query_string = query->query_string;
288 Xapian::doccount count;
291 Xapian::Enquire enquire (*notmuch->xapian_db);
292 Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
293 _find_prefix ("type"),
295 Xapian::Query string_query, final_query;
297 unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
298 Xapian::QueryParser::FLAG_PHRASE |
299 Xapian::QueryParser::FLAG_LOVEHATE |
300 Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
301 Xapian::QueryParser::FLAG_WILDCARD |
302 Xapian::QueryParser::FLAG_PURE_NOT);
304 if (strcmp (query_string, "") == 0) {
305 final_query = mail_query;
307 string_query = notmuch->query_parser->
308 parse_query (query_string, flags);
309 final_query = Xapian::Query (Xapian::Query::OP_AND,
310 mail_query, string_query);
313 enquire.set_weighting_scheme(Xapian::BoolWeight());
314 enquire.set_docid_order(Xapian::Enquire::ASCENDING);
317 fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
320 enquire.set_query (final_query);
322 mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
324 count = mset.get_matches_estimated();
326 } catch (const Xapian::Error &error) {
327 fprintf (stderr, "A Xapian exception occurred: %s\n",
328 error.get_msg().c_str());
329 fprintf (stderr, "Query string was: %s\n", query->query_string);