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