]> git.notmuchmail.org Git - notmuch/blob - query.cc
add_files: Pull one stat out of the recrusive function.
[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 <xapian.h>
25
26 struct _notmuch_query {
27     notmuch_database_t *notmuch;
28     const char *query_string;
29     notmuch_sort_t sort;
30 };
31
32 struct _notmuch_results {
33     notmuch_database_t *notmuch;
34     Xapian::MSetIterator iterator;
35     Xapian::MSetIterator iterator_end;
36 };
37
38 notmuch_query_t *
39 notmuch_query_create (notmuch_database_t *notmuch,
40                       const char *query_string)
41 {
42     notmuch_query_t *query;
43
44     query = talloc (NULL, notmuch_query_t);
45     if (unlikely (query == NULL))
46         return NULL;
47
48     query->notmuch = notmuch;
49
50     query->query_string = talloc_strdup (query, query_string);
51
52     query->sort = NOTMUCH_SORT_DATE_OLDEST_FIRST;
53
54     return query;
55 }
56
57 void
58 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
59 {
60     query->sort = sort;
61 }
62
63 /* We end up having to call the destructors explicitly because we had
64  * to use "placement new" in order to initialize C++ objects within a
65  * block that we allocated with talloc. So C++ is making talloc
66  * slightly less simple to use, (we wouldn't need
67  * talloc_set_destructor at all otherwise).
68  */
69 static int
70 _notmuch_results_destructor (notmuch_results_t *results)
71 {
72     results->iterator.~MSetIterator ();
73     results->iterator_end.~MSetIterator ();
74
75     return 0;
76 }
77
78 notmuch_results_t *
79 notmuch_query_search (notmuch_query_t *query)
80 {
81     notmuch_database_t *notmuch = query->notmuch;
82     const char *query_string = query->query_string;
83     notmuch_results_t *results;
84
85     results = talloc (query, notmuch_results_t);
86     if (unlikely (results == NULL))
87         return NULL;
88
89     try {
90         Xapian::Enquire enquire (*notmuch->xapian_db);
91         Xapian::Query mail_query ("Kmail");
92         Xapian::Query string_query, final_query;
93         Xapian::MSet mset;
94         unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN &
95                               Xapian::QueryParser::FLAG_PHRASE &
96                               Xapian::QueryParser::FLAG_LOVEHATE &
97                               Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE &
98                               Xapian::QueryParser::FLAG_WILDCARD);
99
100         if (strcmp (query_string, "") == 0) {
101             final_query = mail_query;
102         } else {
103             string_query = notmuch->query_parser->
104                 parse_query (query_string, flags);
105             final_query = Xapian::Query (Xapian::Query::OP_AND,
106                                          mail_query, string_query);
107         }
108
109         switch (query->sort) {
110         case NOTMUCH_SORT_DATE_OLDEST_FIRST:
111             enquire.set_sort_by_value (NOTMUCH_VALUE_DATE, FALSE);
112             break;
113         case NOTMUCH_SORT_DATE_NEWEST_FIRST:
114             enquire.set_sort_by_value (NOTMUCH_VALUE_DATE, TRUE);
115             break;
116         case NOTMUCH_SORT_MESSAGE_ID:
117             enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
118             break;
119         }
120
121         enquire.set_query (final_query);
122
123         mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
124
125         results->notmuch = notmuch;
126
127         new (&results->iterator) Xapian::MSetIterator ();
128         new (&results->iterator_end) Xapian::MSetIterator ();
129
130         talloc_set_destructor (results, _notmuch_results_destructor);
131
132         results->iterator = mset.begin ();
133         results->iterator_end = mset.end ();
134
135     } catch (const Xapian::Error &error) {
136         fprintf (stderr, "A Xapian exception occurred: %s\n",
137                  error.get_msg().c_str());
138     }
139
140     return results;
141 }
142
143 void
144 notmuch_query_destroy (notmuch_query_t *query)
145 {
146     talloc_free (query);
147 }
148
149 notmuch_bool_t
150 notmuch_results_has_more (notmuch_results_t *results)
151 {
152     return (results->iterator != results->iterator_end);
153 }
154
155 notmuch_message_t *
156 notmuch_results_get (notmuch_results_t *results)
157 {
158     Xapian::docid doc_id;
159
160     doc_id = *results->iterator;
161
162     return _notmuch_message_create (results,
163                                     results->notmuch, doc_id);
164 }
165
166 void
167 notmuch_results_advance (notmuch_results_t *results)
168 {
169     results->iterator++;
170 }
171
172 void
173 notmuch_results_destroy (notmuch_results_t *results)
174 {
175     talloc_free (results);
176 }