notmuch setup/new: Print progress once per second instead of after 1000 files.
[notmuch] / 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_message_results {
35     notmuch_database_t *notmuch;
36     Xapian::MSetIterator iterator;
37     Xapian::MSetIterator iterator_end;
38 };
39
40 struct _notmuch_thread_results {
41     notmuch_database_t *notmuch;
42     GPtrArray *threads;
43     unsigned int index;
44 };
45
46 notmuch_query_t *
47 notmuch_query_create (notmuch_database_t *notmuch,
48                       const char *query_string)
49 {
50     notmuch_query_t *query;
51
52 #ifdef DEBUG_QUERY
53     fprintf (stderr, "Query string is:\n%s\n", query_string);
54 #endif
55
56     query = talloc (NULL, notmuch_query_t);
57     if (unlikely (query == NULL))
58         return NULL;
59
60     query->notmuch = notmuch;
61
62     query->query_string = talloc_strdup (query, query_string);
63
64     query->sort = NOTMUCH_SORT_DATE_OLDEST_FIRST;
65
66     return query;
67 }
68
69 void
70 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
71 {
72     query->sort = sort;
73 }
74
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).
80  */
81 static int
82 _notmuch_message_results_destructor (notmuch_message_results_t *results)
83 {
84     results->iterator.~MSetIterator ();
85     results->iterator_end.~MSetIterator ();
86
87     return 0;
88 }
89
90 notmuch_message_results_t *
91 notmuch_query_search_messages (notmuch_query_t *query)
92 {
93     notmuch_database_t *notmuch = query->notmuch;
94     const char *query_string = query->query_string;
95     notmuch_message_results_t *results;
96
97     results = talloc (query, notmuch_message_results_t);
98     if (unlikely (results == NULL))
99         return NULL;
100
101     try {
102         Xapian::Enquire enquire (*notmuch->xapian_db);
103         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
104                                                    _find_prefix ("type"),
105                                                    "mail"));
106         Xapian::Query string_query, final_query;
107         Xapian::MSet mset;
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);
113
114         if (strcmp (query_string, "") == 0) {
115             final_query = mail_query;
116         } else {
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);
121         }
122
123         switch (query->sort) {
124         case NOTMUCH_SORT_DATE_OLDEST_FIRST:
125             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
126             break;
127         case NOTMUCH_SORT_DATE_NEWEST_FIRST:
128             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
129             break;
130         case NOTMUCH_SORT_MESSAGE_ID:
131             enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
132             break;
133         }
134
135 #if DEBUG_QUERY
136         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
137 #endif
138
139         enquire.set_query (final_query);
140
141         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
142
143         results->notmuch = notmuch;
144
145         new (&results->iterator) Xapian::MSetIterator ();
146         new (&results->iterator_end) Xapian::MSetIterator ();
147
148         talloc_set_destructor (results, _notmuch_message_results_destructor);
149
150         results->iterator = mset.begin ();
151         results->iterator_end = mset.end ();
152
153     } catch (const Xapian::Error &error) {
154         fprintf (stderr, "A Xapian exception occurred: %s\n",
155                  error.get_msg().c_str());
156     }
157
158     return results;
159 }
160
161 /* Glib objects force use to use a talloc destructor as well, (but not
162  * nearly as ugly as the for message_results 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. */
165 static int
166 _notmuch_thread_results_destructor (notmuch_thread_results_t *results)
167 {
168     g_ptr_array_free (results->threads, TRUE);
169
170     return 0;
171 }
172
173 notmuch_thread_results_t *
174 notmuch_query_search_threads (notmuch_query_t *query)
175 {
176     notmuch_thread_results_t *thread_results;
177     notmuch_thread_t *thread;
178     const char *thread_id;
179     notmuch_message_results_t *message_results;
180     notmuch_message_t *message;
181     notmuch_tags_t *tags;
182     const char *tag;
183     GHashTable *seen;
184
185     thread_results = talloc (query, notmuch_thread_results_t);
186     if (thread_results == NULL)
187         return NULL;
188
189     thread_results->notmuch = query->notmuch;
190     thread_results->threads = g_ptr_array_new ();
191     thread_results->index = 0;
192
193     talloc_set_destructor (thread_results, _notmuch_thread_results_destructor);
194
195     seen = g_hash_table_new_full (g_str_hash, g_str_equal,
196                                   free, NULL);
197
198     for (message_results = notmuch_query_search_messages (query);
199          notmuch_message_results_has_more (message_results);
200          notmuch_message_results_advance (message_results))
201     {
202         message = notmuch_message_results_get (message_results);
203
204         thread_id = notmuch_message_get_thread_id (message);
205
206         if (! g_hash_table_lookup_extended (seen,
207                                             thread_id, NULL,
208                                             (void **) &thread))
209         {
210             const char *subject;
211
212             thread = _notmuch_thread_create (query, query->notmuch,
213                                              thread_id);
214
215             subject = _notmuch_message_get_subject (message);
216
217             _notmuch_thread_set_subject (thread, subject);
218
219             g_hash_table_insert (seen, xstrdup (thread_id), thread);
220
221             g_ptr_array_add (thread_results->threads, thread);
222         }
223
224         for (tags = notmuch_message_get_tags (message);
225              notmuch_tags_has_more (tags);
226              notmuch_tags_advance (tags))
227         {
228             tag = notmuch_tags_get (tags);
229             _notmuch_thread_add_tag (thread, tag);
230         }
231
232         notmuch_message_destroy (message);
233     }
234
235     g_hash_table_unref (seen);
236
237     return thread_results;
238 }
239
240 void
241 notmuch_query_destroy (notmuch_query_t *query)
242 {
243     talloc_free (query);
244 }
245
246 notmuch_bool_t
247 notmuch_message_results_has_more (notmuch_message_results_t *results)
248 {
249     return (results->iterator != results->iterator_end);
250 }
251
252 notmuch_message_t *
253 notmuch_message_results_get (notmuch_message_results_t *results)
254 {
255     notmuch_message_t *message;
256     Xapian::docid doc_id;
257     notmuch_private_status_t status;
258
259     if (! notmuch_message_results_has_more (results))
260         return NULL;
261
262     doc_id = *results->iterator;
263
264     message = _notmuch_message_create (results,
265                                        results->notmuch, doc_id,
266                                        &status);
267
268     if (message == NULL &&
269         status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
270     {
271         INTERNAL_ERROR ("a results iterator contains a non-existent document ID.\n");
272     }
273
274     return message;
275 }
276
277 void
278 notmuch_message_results_advance (notmuch_message_results_t *results)
279 {
280     results->iterator++;
281 }
282
283 void
284 notmuch_message_results_destroy (notmuch_message_results_t *results)
285 {
286     talloc_free (results);
287 }
288
289 notmuch_bool_t
290 notmuch_thread_results_has_more (notmuch_thread_results_t *results)
291 {
292     return (results->index < results->threads->len);
293 }
294
295 notmuch_thread_t *
296 notmuch_thread_results_get (notmuch_thread_results_t *results)
297 {
298     if (! notmuch_thread_results_has_more (results))
299         return NULL;
300
301     return (notmuch_thread_t *) g_ptr_array_index (results->threads,
302                                                    results->index);
303 }
304
305 void
306 notmuch_thread_results_advance (notmuch_thread_results_t *results)
307 {
308     results->index++;
309 }
310
311 void
312 notmuch_thread_results_destroy (notmuch_thread_results_t *results)
313 {
314     talloc_free (results);
315 }