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