]> git.cworth.org Git - obsolete/notmuch-old/blob - lib/query.cc
Switch from random to sequential thread identifiers.
[obsolete/notmuch-old] / 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             final_query = mail_query;
130         } else {
131             string_query = notmuch->query_parser->
132                 parse_query (query_string, flags);
133             final_query = Xapian::Query (Xapian::Query::OP_AND,
134                                          mail_query, string_query);
135         }
136
137         enquire.set_weighting_scheme (Xapian::BoolWeight());
138
139         switch (query->sort) {
140         case NOTMUCH_SORT_OLDEST_FIRST:
141             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
142             break;
143         case NOTMUCH_SORT_NEWEST_FIRST:
144             enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, TRUE);
145             break;
146         case NOTMUCH_SORT_MESSAGE_ID:
147             enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
148             break;
149         }
150
151 #if DEBUG_QUERY
152         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
153 #endif
154
155         enquire.set_query (final_query);
156
157         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
158
159         messages->iterator = mset.begin ();
160         messages->iterator_end = mset.end ();
161
162     } catch (const Xapian::Error &error) {
163         fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
164                  error.get_msg().c_str());
165         fprintf (stderr, "Query string was: %s\n", query->query_string);
166         notmuch->exception_reported = TRUE;
167     }
168
169     return &messages->base;
170 }
171
172 notmuch_bool_t
173 _notmuch_mset_messages_has_more (notmuch_messages_t *messages)
174 {
175     notmuch_mset_messages_t *mset_messages;
176
177     mset_messages = (notmuch_mset_messages_t *) messages;
178
179     return (mset_messages->iterator != mset_messages->iterator_end);
180 }
181
182 notmuch_message_t *
183 _notmuch_mset_messages_get (notmuch_messages_t *messages)
184 {
185     notmuch_message_t *message;
186     Xapian::docid doc_id;
187     notmuch_private_status_t status;
188     notmuch_mset_messages_t *mset_messages;
189
190     mset_messages = (notmuch_mset_messages_t *) messages;
191
192     if (! _notmuch_mset_messages_has_more (&mset_messages->base))
193         return NULL;
194
195     doc_id = *mset_messages->iterator;
196
197     message = _notmuch_message_create (mset_messages,
198                                        mset_messages->notmuch, doc_id,
199                                        &status);
200
201     if (message == NULL &&
202        status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
203     {
204         INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
205     }
206
207     return message;
208 }
209
210 void
211 _notmuch_mset_messages_advance (notmuch_messages_t *messages)
212 {
213     notmuch_mset_messages_t *mset_messages;
214
215     mset_messages = (notmuch_mset_messages_t *) messages;
216
217     mset_messages->iterator++;
218 }
219
220 /* Glib objects force use to use a talloc destructor as well, (but not
221  * nearly as ugly as the for messages due to C++ objects). At
222  * this point, I'd really like to have some talloc-friendly
223  * equivalents for the few pieces of glib that I'm using. */
224 static int
225 _notmuch_threads_destructor (notmuch_threads_t *threads)
226 {
227     g_hash_table_unref (threads->threads);
228
229     return 0;
230 }
231
232 notmuch_threads_t *
233 notmuch_query_search_threads (notmuch_query_t *query)
234 {
235     notmuch_threads_t *threads;
236
237     threads = talloc (query, notmuch_threads_t);
238     if (threads == NULL)
239         return NULL;
240
241     threads->query = query;
242     threads->threads = g_hash_table_new_full (g_str_hash, g_str_equal,
243                                               free, NULL);
244
245     threads->messages = notmuch_query_search_messages (query);
246
247     threads->thread_id = NULL;
248
249     talloc_set_destructor (threads, _notmuch_threads_destructor);
250
251     return threads;
252 }
253
254 void
255 notmuch_query_destroy (notmuch_query_t *query)
256 {
257     talloc_free (query);
258 }
259
260 notmuch_bool_t
261 notmuch_threads_has_more (notmuch_threads_t *threads)
262 {
263     notmuch_message_t *message;
264
265     if (threads->thread_id)
266         return TRUE;
267
268     while (notmuch_messages_has_more (threads->messages))
269     {
270         message = notmuch_messages_get (threads->messages);
271
272         threads->thread_id = notmuch_message_get_thread_id (message);
273
274         if (! g_hash_table_lookup_extended (threads->threads,
275                                             threads->thread_id,
276                                             NULL, NULL))
277         {
278             g_hash_table_insert (threads->threads,
279                                  xstrdup (threads->thread_id), NULL);
280             notmuch_messages_advance (threads->messages);
281             return TRUE;
282         }
283
284         notmuch_messages_advance (threads->messages);
285     }
286
287     threads->thread_id = NULL;
288     return FALSE;
289 }
290
291 notmuch_thread_t *
292 notmuch_threads_get (notmuch_threads_t *threads)
293 {
294     if (! notmuch_threads_has_more (threads))
295         return NULL;
296
297     return _notmuch_thread_create (threads->query,
298                                    threads->query->notmuch,
299                                    threads->thread_id,
300                                    threads->query->query_string);
301 }
302
303 void
304 notmuch_threads_advance (notmuch_threads_t *threads)
305 {
306     threads->thread_id = NULL;
307 }
308
309 void
310 notmuch_threads_destroy (notmuch_threads_t *threads)
311 {
312     talloc_free (threads);
313 }
314
315 unsigned
316 notmuch_query_count_messages (notmuch_query_t *query)
317 {
318     notmuch_database_t *notmuch = query->notmuch;
319     const char *query_string = query->query_string;
320     Xapian::doccount count = 0;
321
322     try {
323         Xapian::Enquire enquire (*notmuch->xapian_db);
324         Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
325                                                    _find_prefix ("type"),
326                                                    "mail"));
327         Xapian::Query string_query, final_query;
328         Xapian::MSet mset;
329         unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
330                               Xapian::QueryParser::FLAG_PHRASE |
331                               Xapian::QueryParser::FLAG_LOVEHATE |
332                               Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
333                               Xapian::QueryParser::FLAG_WILDCARD |
334                               Xapian::QueryParser::FLAG_PURE_NOT);
335
336         if (strcmp (query_string, "") == 0) {
337             final_query = mail_query;
338         } else {
339             string_query = notmuch->query_parser->
340                 parse_query (query_string, flags);
341             final_query = Xapian::Query (Xapian::Query::OP_AND,
342                                          mail_query, string_query);
343         }
344
345         enquire.set_weighting_scheme(Xapian::BoolWeight());
346         enquire.set_docid_order(Xapian::Enquire::ASCENDING);
347
348 #if DEBUG_QUERY
349         fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
350 #endif
351
352         enquire.set_query (final_query);
353
354         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
355
356         count = mset.get_matches_estimated();
357
358     } catch (const Xapian::Error &error) {
359         fprintf (stderr, "A Xapian exception occurred: %s\n",
360                  error.get_msg().c_str());
361         fprintf (stderr, "Query string was: %s\n", query->query_string);
362     }
363
364     return count;
365 }