]> git.notmuchmail.org Git - notmuch/blob - query.cc
c669fb9110a6fed7347ae454ddafa12fac3edbaa
[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 const char *NOTMUCH_QUERY_ALL = "";
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_results {
35     notmuch_database_t *notmuch;
36     Xapian::PostingIterator iterator;
37     Xapian::PostingIterator iterator_end;
38 };
39
40 notmuch_query_t *
41 notmuch_query_create (notmuch_database_t *notmuch,
42                       const char *query_string)
43 {
44     notmuch_query_t *query;
45
46     query = talloc (NULL, notmuch_query_t);
47     if (unlikely (query == NULL))
48         return NULL;
49
50     query->notmuch = notmuch;
51
52     /* Special-case NOTMUCH_QUERY_ALL so we see it and not a copy. */
53     if (query_string == NOTMUCH_QUERY_ALL)
54         query->query_string = query_string;
55     else
56         query->query_string = talloc_strdup (query, query_string);
57
58     query->sort = NOTMUCH_SORT_DATE_OLDEST_FIRST;
59
60     return query;
61 }
62
63 void
64 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
65 {
66     query->sort = sort;
67 }
68
69 /* We end up having to call the destructors explicitly because we had
70  * to use "placement new" in order to initialize C++ objects within a
71  * block that we allocated with talloc. So C++ is making talloc
72  * slightly less simple to use, (we wouldn't need
73  * talloc_set_destructor at all otherwise).
74  */
75 static int
76 _notmuch_results_destructor (notmuch_results_t *results)
77 {
78     results->iterator.~PostingIterator ();
79     results->iterator_end.~PostingIterator ();
80
81     return 0;
82 }
83
84 notmuch_results_t *
85 notmuch_query_search (notmuch_query_t *query)
86 {
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         if (query->query_string != NOTMUCH_QUERY_ALL) {
95             fprintf (stderr, "Error: Arbitrary search strings are not supported yet. Come back soon!\n");
96             exit (1);
97         }
98
99         results->notmuch = query->notmuch;
100         new (&results->iterator) Xapian::PostingIterator ();
101         new (&results->iterator_end) Xapian::PostingIterator ();
102
103         talloc_set_destructor (results, _notmuch_results_destructor);
104
105         results->iterator = query->notmuch->xapian_db->postlist_begin ("");
106         results->iterator_end = query->notmuch->xapian_db->postlist_end ("");
107
108     } catch (const Xapian::Error &error) {
109         fprintf (stderr, "A Xapian exception occurred: %s\n",
110                  error.get_msg().c_str());
111     }
112
113     return results;
114 }
115
116 void
117 notmuch_query_destroy (notmuch_query_t *query)
118 {
119     talloc_free (query);
120 }
121
122 notmuch_bool_t
123 notmuch_results_has_more (notmuch_results_t *results)
124 {
125     return (results->iterator != results->iterator_end);
126 }
127
128 notmuch_message_t *
129 notmuch_results_get (notmuch_results_t *results)
130 {
131     Xapian::docid doc_id;
132
133     doc_id = *results->iterator;
134
135     return _notmuch_message_create (results,
136                                     results->notmuch, doc_id);
137 }
138
139 void
140 notmuch_results_advance (notmuch_results_t *results)
141 {
142     results->iterator++;
143 }