1 /* query.cc - Support for searching a notmuch database
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
26 const char *NOTMUCH_QUERY_ALL = "";
28 struct _notmuch_query {
29 notmuch_database_t *notmuch;
30 const char *query_string;
34 struct _notmuch_results {
35 notmuch_database_t *notmuch;
36 Xapian::PostingIterator iterator;
37 Xapian::PostingIterator iterator_end;
41 notmuch_query_create (notmuch_database_t *notmuch,
42 const char *query_string)
44 notmuch_query_t *query;
46 query = talloc (NULL, notmuch_query_t);
47 if (unlikely (query == NULL))
50 query->notmuch = notmuch;
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;
56 query->query_string = talloc_strdup (query, query_string);
58 query->sort = NOTMUCH_SORT_DATE_OLDEST_FIRST;
64 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
70 _notmuch_results_destroy (notmuch_results_t *results)
72 results->iterator.~PostingIterator ();
73 results->iterator_end.~PostingIterator ();
79 notmuch_query_search (notmuch_query_t *query)
81 notmuch_results_t *results;
83 results = talloc (query, notmuch_results_t);
84 if (unlikely (results == NULL))
88 if (query->query_string != NOTMUCH_QUERY_ALL) {
89 fprintf (stderr, "Error: Arbitrary search strings are not supported yet. Come back soon!\n");
93 results->notmuch = query->notmuch;
94 new (&results->iterator) Xapian::PostingIterator ();
95 new (&results->iterator_end) Xapian::PostingIterator ();
97 talloc_set_destructor (results, _notmuch_results_destroy);
99 results->iterator = query->notmuch->xapian_db->postlist_begin ("");
100 results->iterator_end = query->notmuch->xapian_db->postlist_end ("");
102 } catch (const Xapian::Error &error) {
103 fprintf (stderr, "A Xapian exception occurred: %s\n",
104 error.get_msg().c_str());
111 notmuch_query_destroy (notmuch_query_t *query)
117 notmuch_results_has_more (notmuch_results_t *results)
119 return (results->iterator != results->iterator_end);
123 notmuch_results_get (notmuch_results_t *results)
125 Xapian::docid doc_id;
127 doc_id = *results->iterator;
129 return _notmuch_message_create (results,
130 results->notmuch, doc_id);
134 notmuch_results_advance (notmuch_results_t *results)