]> git.notmuchmail.org Git - notmuch/blob - lib/query.cc
lib/query: Fix notmuch_threads_t to stream results rather than blocking.
[notmuch] / lib / query.cc
1 /* query.cc - Support for searching a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <glib.h> /* GHashTable, GPtrArray */
25
26 #include <xapian.h>
27
28 struct _notmuch_query {
29     notmuch_database_t *notmuch;
30     const char *query_string;
31     notmuch_sort_t sort;
32 };
33
34 struct _notmuch_messages {
35     notmuch_database_t *notmuch;
36     Xapian::MSetIterator iterator;
37     Xapian::MSetIterator iterator_end;
38 };
39
40 struct _notmuch_threads {
41     notmuch_query_t *query;
42     GHashTable *threads;
43     notmuch_messages_t *messages;
44
45     /* This thread ID is our iterator state. */
46     const char *thread_id;
47 };
48
49 notmuch_query_t *
50 notmuch_query_create (notmuch_database_t *notmuch,
51                       const char *query_string)
52 {
53     notmuch_query_t *query;
54
55 #ifdef DEBUG_QUERY
56     fprintf (stderr, "Query string is:\n%s\n", query_string);
57 #endif
58
59     query = talloc (NULL, notmuch_query_t);
60     if (unlikely (query == NULL))
61         return NULL;
62
63     query->notmuch = notmuch;
64
65     query->query_string = talloc_strdup (query, query_string);
66
67     query->sort = NOTMUCH_SORT_NEWEST_FIRST;
68
69     return query;
70 }
71
72 void
73 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
74 {
75     query->sort = sort;
76 }
77
78 notmuch_messages_t *
79 notmuch_query_search_messages (notmuch_query_t *query,
80                                int first,
81                                int max_messages)
82 {
83     notmuch_database_t *notmuch = query->notmuch;
84     const char *query_string = query->query_string;
85     notmuch_message_list_t *message_list;
86     Xapian::MSetIterator i;
87
88     message_list = _notmuch_message_list_create (query);
89     if (unlikely (message_list == NULL))
90         return NULL;
91
92     try {
93         Xapian::Enquire enquire (*notmuch->xapian_db);
94         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
95                                                    _find_prefix ("type"),
96                                                    "mail"));
97         Xapian::Query string_query, final_query;
98         Xapian::MSet mset;
99         unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
100                               Xapian::QueryParser::FLAG_PHRASE |
101                               Xapian::QueryParser::FLAG_LOVEHATE |
102                               Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
103                               Xapian::QueryParser::FLAG_WILDCARD |
104                               Xapian::QueryParser::FLAG_PURE_NOT);
105
106         if (strcmp (query_string, "") == 0) {
107             final_query = mail_query;
108         } else {
109             string_query = notmuch->query_parser->
110                 parse_query (query_string, flags);
111             final_query = Xapian::Query (Xapian::Query::OP_AND,
112                                          mail_query, string_query);
113         }
114
115         switch (query->sort) {
116         case NOTMUCH_SORT_OLDEST_FIRST:
117             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
118             break;
119         case NOTMUCH_SORT_NEWEST_FIRST:
120             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
121             break;
122         case NOTMUCH_SORT_MESSAGE_ID:
123             enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
124             break;
125         }
126
127 #if DEBUG_QUERY
128         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
129 #endif
130
131         enquire.set_query (final_query);
132
133         if (max_messages == -1)
134             max_messages = notmuch->xapian_db->get_doccount ();
135         mset = enquire.get_mset (first, max_messages);
136
137         for (i = mset.begin (); i != mset.end (); i++) {
138             notmuch_message_t *message;
139             notmuch_private_status_t status;
140
141             message = _notmuch_message_create (message_list, notmuch,
142                                                *i, &status);
143             if (message == NULL)
144             {
145                 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
146                     INTERNAL_ERROR ("A message iterator contains a "
147                                     "non-existent document ID.\n");
148                 break;
149             }
150
151             _notmuch_message_list_add_message (message_list, message);
152         }
153
154     } catch (const Xapian::Error &error) {
155         fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
156                  error.get_msg().c_str());
157         fprintf (stderr, "Query string was: %s\n", query->query_string);
158         notmuch->exception_reported = TRUE;
159     }
160
161     return _notmuch_messages_create (message_list);
162 }
163
164 /* Glib objects force use to use a talloc destructor as well, (but not
165  * nearly as ugly as the for messages due to C++ objects). At
166  * this point, I'd really like to have some talloc-friendly
167  * equivalents for the few pieces of glib that I'm using. */
168 static int
169 _notmuch_threads_destructor (notmuch_threads_t *threads)
170 {
171     g_hash_table_unref (threads->threads);
172
173     return 0;
174 }
175
176 notmuch_threads_t *
177 notmuch_query_search_threads (notmuch_query_t *query)
178 {
179     notmuch_threads_t *threads;
180
181     threads = talloc (query, notmuch_threads_t);
182     if (threads == NULL)
183         return NULL;
184
185     threads->query = query;
186     threads->threads = g_hash_table_new_full (g_str_hash, g_str_equal,
187                                               free, NULL);
188
189     threads->messages = notmuch_query_search_messages (query, 0, -1);
190
191     threads->thread_id = NULL;
192
193     talloc_set_destructor (threads, _notmuch_threads_destructor);
194
195     return threads;
196 }
197
198 void
199 notmuch_query_destroy (notmuch_query_t *query)
200 {
201     talloc_free (query);
202 }
203
204 notmuch_bool_t
205 notmuch_threads_has_more (notmuch_threads_t *threads)
206 {
207     notmuch_message_t *message;
208
209     if (threads->thread_id)
210         return TRUE;
211
212     while (notmuch_messages_has_more (threads->messages))
213     {
214         message = notmuch_messages_get (threads->messages);
215
216         threads->thread_id = notmuch_message_get_thread_id (message);
217
218         if (! g_hash_table_lookup_extended (threads->threads,
219                                             threads->thread_id,
220                                             NULL, NULL))
221         {
222             g_hash_table_insert (threads->threads,
223                                  xstrdup (threads->thread_id), NULL);
224             notmuch_messages_advance (threads->messages);
225             return TRUE;
226         }
227
228         notmuch_messages_advance (threads->messages);
229     }
230
231     threads->thread_id = NULL;
232     return FALSE;
233 }
234
235 notmuch_thread_t *
236 notmuch_threads_get (notmuch_threads_t *threads)
237 {
238     if (! notmuch_threads_has_more (threads))
239         return NULL;
240
241     return _notmuch_thread_create (threads->query,
242                                    threads->query->notmuch,
243                                    threads->thread_id,
244                                    threads->query->query_string);
245 }
246
247 void
248 notmuch_threads_advance (notmuch_threads_t *threads)
249 {
250     threads->thread_id = NULL;
251 }
252
253 void
254 notmuch_threads_destroy (notmuch_threads_t *threads)
255 {
256     talloc_free (threads);
257 }
258
259 unsigned
260 notmuch_query_count_messages (notmuch_query_t *query)
261 {
262     notmuch_database_t *notmuch = query->notmuch;
263     const char *query_string = query->query_string;
264     Xapian::doccount count;
265
266     try {
267         Xapian::Enquire enquire (*notmuch->xapian_db);
268         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
269                                                    _find_prefix ("type"),
270                                                    "mail"));
271         Xapian::Query string_query, final_query;
272         Xapian::MSet mset;
273         unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
274                               Xapian::QueryParser::FLAG_PHRASE |
275                               Xapian::QueryParser::FLAG_LOVEHATE |
276                               Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
277                               Xapian::QueryParser::FLAG_WILDCARD |
278                               Xapian::QueryParser::FLAG_PURE_NOT);
279
280         if (strcmp (query_string, "") == 0) {
281             final_query = mail_query;
282         } else {
283             string_query = notmuch->query_parser->
284                 parse_query (query_string, flags);
285             final_query = Xapian::Query (Xapian::Query::OP_AND,
286                                          mail_query, string_query);
287         }
288
289         enquire.set_weighting_scheme(Xapian::BoolWeight());
290         enquire.set_docid_order(Xapian::Enquire::ASCENDING);
291
292 #if DEBUG_QUERY
293         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
294 #endif
295
296         enquire.set_query (final_query);
297
298         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
299
300         count = mset.get_matches_estimated();
301
302     } catch (const Xapian::Error &error) {
303         fprintf (stderr, "A Xapian exception occurred: %s\n",
304                  error.get_msg().c_str());
305         fprintf (stderr, "Query string was: %s\n", query->query_string);
306     }
307
308     return count;
309 }