13bdd0a1b34b3ba944a39e22752986fef8304dea
[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_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_NEWEST_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 notmuch_messages_t *
76 notmuch_query_search_messages (notmuch_query_t *query,
77                                int first,
78                                int max_messages)
79 {
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;
84
85     message_list = _notmuch_message_list_create (query);
86     if (unlikely (message_list == NULL))
87         return NULL;
88
89     try {
90         Xapian::Enquire enquire (*notmuch->xapian_db);
91         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
92                                                    _find_prefix ("type"),
93                                                    "mail"));
94         Xapian::Query string_query, final_query;
95         Xapian::MSet mset;
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);
102
103         if (strcmp (query_string, "") == 0) {
104             final_query = mail_query;
105         } else {
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);
110         }
111
112         switch (query->sort) {
113         case NOTMUCH_SORT_OLDEST_FIRST:
114             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
115             break;
116         case NOTMUCH_SORT_NEWEST_FIRST:
117             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
118             break;
119         case NOTMUCH_SORT_MESSAGE_ID:
120             enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
121             break;
122         }
123
124 #if DEBUG_QUERY
125         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
126 #endif
127
128         enquire.set_query (final_query);
129
130         if (max_messages == -1)
131             max_messages = notmuch->xapian_db->get_doccount ();
132         mset = enquire.get_mset (first, max_messages);
133
134         for (i = mset.begin (); i != mset.end (); i++) {
135             notmuch_message_t *message;
136             notmuch_private_status_t status;
137
138             message = _notmuch_message_create (message_list, notmuch,
139                                                *i, &status);
140             if (message == NULL)
141             {
142                 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
143                     INTERNAL_ERROR ("A message iterator contains a "
144                                     "non-existent document ID.\n");
145                 break;
146             }
147
148             _notmuch_message_list_add_message (message_list, message);
149         }
150
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;
156     }
157
158     return _notmuch_messages_create (message_list);
159 }
160
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. */
165 static int
166 _notmuch_threads_destructor (notmuch_threads_t *threads)
167 {
168     g_ptr_array_free (threads->threads, TRUE);
169
170     return 0;
171 }
172
173 notmuch_threads_t *
174 notmuch_query_search_threads (notmuch_query_t *query)
175 {
176     notmuch_threads_t *threads;
177     notmuch_thread_t *thread;
178     const char *thread_id;
179     notmuch_messages_t *messages;
180     notmuch_message_t *message;
181     GHashTable *seen;
182
183     threads = talloc (query, notmuch_threads_t);
184     if (threads == NULL)
185         return NULL;
186
187     threads->notmuch = query->notmuch;
188     threads->threads = g_ptr_array_new ();
189     threads->index = 0;
190
191     talloc_set_destructor (threads, _notmuch_threads_destructor);
192
193     seen = g_hash_table_new_full (g_str_hash, g_str_equal,
194                                   free, NULL);
195
196     for (messages = notmuch_query_search_messages (query, 0, -1);
197          notmuch_messages_has_more (messages);
198          notmuch_messages_advance (messages))
199     {
200         message = notmuch_messages_get (messages);
201
202         thread_id = notmuch_message_get_thread_id (message);
203
204         if (! g_hash_table_lookup_extended (seen,
205                                             thread_id, NULL,
206                                             (void **) &thread))
207         {
208             thread = _notmuch_thread_create (query, query->notmuch,
209                                              thread_id,
210                                              query->query_string);
211             g_ptr_array_add (threads->threads, thread);
212
213             g_hash_table_insert (seen, xstrdup (thread_id), thread);
214         }
215
216         notmuch_message_destroy (message);
217     }
218
219     g_hash_table_unref (seen);
220
221     return threads;
222 }
223
224 void
225 notmuch_query_destroy (notmuch_query_t *query)
226 {
227     talloc_free (query);
228 }
229
230 notmuch_bool_t
231 notmuch_threads_has_more (notmuch_threads_t *threads)
232 {
233     return (threads->index < threads->threads->len);
234 }
235
236 notmuch_thread_t *
237 notmuch_threads_get (notmuch_threads_t *threads)
238 {
239     if (! notmuch_threads_has_more (threads))
240         return NULL;
241
242     return (notmuch_thread_t *) g_ptr_array_index (threads->threads,
243                                                    threads->index);
244 }
245
246 void
247 notmuch_threads_advance (notmuch_threads_t *threads)
248 {
249     threads->index++;
250 }
251
252 void
253 notmuch_threads_destroy (notmuch_threads_t *threads)
254 {
255     talloc_free (threads);
256 }
257
258 unsigned
259 notmuch_query_count_messages (notmuch_query_t *query)
260 {
261     notmuch_database_t *notmuch = query->notmuch;
262     const char *query_string = query->query_string;
263     Xapian::doccount count;
264
265     try {
266         Xapian::Enquire enquire (*notmuch->xapian_db);
267         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
268                                                    _find_prefix ("type"),
269                                                    "mail"));
270         Xapian::Query string_query, final_query;
271         Xapian::MSet mset;
272         unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
273                               Xapian::QueryParser::FLAG_PHRASE |
274                               Xapian::QueryParser::FLAG_LOVEHATE |
275                               Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
276                               Xapian::QueryParser::FLAG_WILDCARD |
277                               Xapian::QueryParser::FLAG_PURE_NOT);
278
279         if (strcmp (query_string, "") == 0) {
280             final_query = mail_query;
281         } else {
282             string_query = notmuch->query_parser->
283                 parse_query (query_string, flags);
284             final_query = Xapian::Query (Xapian::Query::OP_AND,
285                                          mail_query, string_query);
286         }
287
288         enquire.set_weighting_scheme(Xapian::BoolWeight());
289         enquire.set_docid_order(Xapian::Enquire::ASCENDING);
290
291 #if DEBUG_QUERY
292         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
293 #endif
294
295         enquire.set_query (final_query);
296
297         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
298
299         count = mset.get_matches_estimated();
300
301     } catch (const Xapian::Error &error) {
302         fprintf (stderr, "A Xapian exception occurred: %s\n",
303                  error.get_msg().c_str());
304         fprintf (stderr, "Query string was: %s\n", query->query_string);
305     }
306
307     return count;
308 }