]> git.notmuchmail.org Git - notmuch/blob - lib/query.cc
Merge Sebastian Spaeth's python bindings into bindings/python
[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 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;
40
41 struct _notmuch_threads {
42     notmuch_query_t *query;
43     GHashTable *threads;
44     notmuch_messages_t *messages;
45
46     /* This thread ID is our iterator state. */
47     const char *thread_id;
48 };
49
50 notmuch_query_t *
51 notmuch_query_create (notmuch_database_t *notmuch,
52                       const char *query_string)
53 {
54     notmuch_query_t *query;
55
56 #ifdef DEBUG_QUERY
57     fprintf (stderr, "Query string is:\n%s\n", query_string);
58 #endif
59
60     query = talloc (NULL, notmuch_query_t);
61     if (unlikely (query == NULL))
62         return NULL;
63
64     query->notmuch = notmuch;
65
66     query->query_string = talloc_strdup (query, query_string);
67
68     query->sort = NOTMUCH_SORT_NEWEST_FIRST;
69
70     return query;
71 }
72
73 void
74 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
75 {
76     query->sort = sort;
77 }
78
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).
84  */
85 static int
86 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
87 {
88     messages->iterator.~MSetIterator ();
89     messages->iterator_end.~MSetIterator ();
90
91     return 0;
92 }
93
94 notmuch_messages_t *
95 notmuch_query_search_messages (notmuch_query_t *query)
96 {
97     notmuch_database_t *notmuch = query->notmuch;
98     const char *query_string = query->query_string;
99     notmuch_mset_messages_t *messages;
100
101     messages = talloc (query, notmuch_mset_messages_t);
102     if (unlikely (messages == NULL))
103         return NULL;
104
105     try {
106
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 ();
112
113         talloc_set_destructor (messages, _notmuch_messages_destructor);
114
115         Xapian::Enquire enquire (*notmuch->xapian_db);
116         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
117                                                    _find_prefix ("type"),
118                                                    "mail"));
119         Xapian::Query string_query, final_query;
120         Xapian::MSet mset;
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);
127
128         if (strcmp (query_string, "") == 0 ||
129             strcmp (query_string, "*") == 0)
130         {
131             final_query = mail_query;
132         } else {
133             string_query = notmuch->query_parser->
134                 parse_query (query_string, flags);
135             final_query = Xapian::Query (Xapian::Query::OP_AND,
136                                          mail_query, string_query);
137         }
138
139         enquire.set_weighting_scheme (Xapian::BoolWeight());
140
141         switch (query->sort) {
142         case NOTMUCH_SORT_OLDEST_FIRST:
143             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
144             break;
145         case NOTMUCH_SORT_NEWEST_FIRST:
146             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
147             break;
148         case NOTMUCH_SORT_MESSAGE_ID:
149             enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
150             break;
151         case NOTMUCH_SORT_UNSORTED:
152             break;
153         }
154
155 #if DEBUG_QUERY
156         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
157 #endif
158
159         enquire.set_query (final_query);
160
161         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
162
163         messages->iterator = mset.begin ();
164         messages->iterator_end = mset.end ();
165
166     } catch (const Xapian::Error &error) {
167         fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
168                  error.get_msg().c_str());
169         fprintf (stderr, "Query string was: %s\n", query->query_string);
170         notmuch->exception_reported = TRUE;
171     }
172
173     return &messages->base;
174 }
175
176 notmuch_bool_t
177 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
178 {
179     notmuch_mset_messages_t *mset_messages;
180
181     mset_messages = (notmuch_mset_messages_t *) messages;
182
183     return (mset_messages->iterator != mset_messages->iterator_end);
184 }
185
186 notmuch_message_t *
187 _notmuch_mset_messages_get (notmuch_messages_t *messages)
188 {
189     notmuch_message_t *message;
190     Xapian::docid doc_id;
191     notmuch_private_status_t status;
192     notmuch_mset_messages_t *mset_messages;
193
194     mset_messages = (notmuch_mset_messages_t *) messages;
195
196     if (! _notmuch_mset_messages_valid (&mset_messages->base))
197         return NULL;
198
199     doc_id = *mset_messages->iterator;
200
201     message = _notmuch_message_create (mset_messages,
202                                        mset_messages->notmuch, doc_id,
203                                        &status);
204
205     if (message == NULL &&
206        status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
207     {
208         INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
209     }
210
211     return message;
212 }
213
214 void
215 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
216 {
217     notmuch_mset_messages_t *mset_messages;
218
219     mset_messages = (notmuch_mset_messages_t *) messages;
220
221     mset_messages->iterator++;
222 }
223
224 /* Glib objects force use to use a talloc destructor as well, (but not
225  * nearly as ugly as the for messages due to C++ objects). At
226  * this point, I'd really like to have some talloc-friendly
227  * equivalents for the few pieces of glib that I'm using. */
228 static int
229 _notmuch_threads_destructor (notmuch_threads_t *threads)
230 {
231     g_hash_table_unref (threads->threads);
232
233     return 0;
234 }
235
236 notmuch_threads_t *
237 notmuch_query_search_threads (notmuch_query_t *query)
238 {
239     notmuch_threads_t *threads;
240
241     threads = talloc (query, notmuch_threads_t);
242     if (threads == NULL)
243         return NULL;
244
245     threads->query = query;
246     threads->threads = g_hash_table_new_full (g_str_hash, g_str_equal,
247                                               free, NULL);
248
249     threads->messages = notmuch_query_search_messages (query);
250
251     threads->thread_id = NULL;
252
253     talloc_set_destructor (threads, _notmuch_threads_destructor);
254
255     return threads;
256 }
257
258 void
259 notmuch_query_destroy (notmuch_query_t *query)
260 {
261     talloc_free (query);
262 }
263
264 notmuch_bool_t
265 notmuch_threads_valid (notmuch_threads_t *threads)
266 {
267     notmuch_message_t *message;
268
269     if (threads->thread_id)
270         return TRUE;
271
272     while (notmuch_messages_valid (threads->messages))
273     {
274         message = notmuch_messages_get (threads->messages);
275
276         threads->thread_id = notmuch_message_get_thread_id (message);
277
278         if (! g_hash_table_lookup_extended (threads->threads,
279                                             threads->thread_id,
280                                             NULL, NULL))
281         {
282             g_hash_table_insert (threads->threads,
283                                  xstrdup (threads->thread_id), NULL);
284             notmuch_messages_move_to_next (threads->messages);
285             return TRUE;
286         }
287
288         notmuch_messages_move_to_next (threads->messages);
289     }
290
291     threads->thread_id = NULL;
292     return FALSE;
293 }
294
295 notmuch_thread_t *
296 notmuch_threads_get (notmuch_threads_t *threads)
297 {
298     if (! notmuch_threads_valid (threads))
299         return NULL;
300
301     return _notmuch_thread_create (threads->query,
302                                    threads->query->notmuch,
303                                    threads->thread_id,
304                                    threads->query->query_string,
305                                    threads->query->sort);
306 }
307
308 void
309 notmuch_threads_move_to_next (notmuch_threads_t *threads)
310 {
311     threads->thread_id = NULL;
312 }
313
314 void
315 notmuch_threads_destroy (notmuch_threads_t *threads)
316 {
317     talloc_free (threads);
318 }
319
320 unsigned
321 notmuch_query_count_messages (notmuch_query_t *query)
322 {
323     notmuch_database_t *notmuch = query->notmuch;
324     const char *query_string = query->query_string;
325     Xapian::doccount count = 0;
326
327     try {
328         Xapian::Enquire enquire (*notmuch->xapian_db);
329         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
330                                                    _find_prefix ("type"),
331                                                    "mail"));
332         Xapian::Query string_query, final_query;
333         Xapian::MSet mset;
334         unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
335                               Xapian::QueryParser::FLAG_PHRASE |
336                               Xapian::QueryParser::FLAG_LOVEHATE |
337                               Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
338                               Xapian::QueryParser::FLAG_WILDCARD |
339                               Xapian::QueryParser::FLAG_PURE_NOT);
340
341         if (strcmp (query_string, "") == 0 ||
342             strcmp (query_string, "*") == 0)
343         {
344             final_query = mail_query;
345         } else {
346             string_query = notmuch->query_parser->
347                 parse_query (query_string, flags);
348             final_query = Xapian::Query (Xapian::Query::OP_AND,
349                                          mail_query, string_query);
350         }
351
352         enquire.set_weighting_scheme(Xapian::BoolWeight());
353         enquire.set_docid_order(Xapian::Enquire::ASCENDING);
354
355 #if DEBUG_QUERY
356         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
357 #endif
358
359         enquire.set_query (final_query);
360
361         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
362
363         count = mset.get_matches_estimated();
364
365     } catch (const Xapian::Error &error) {
366         fprintf (stderr, "A Xapian exception occurred: %s\n",
367                  error.get_msg().c_str());
368         fprintf (stderr, "Query string was: %s\n", query->query_string);
369     }
370
371     return count;
372 }