]> git.notmuchmail.org Git - notmuch/blob - lib/lastmod-fp.cc
5fdaf281ece558aa7c5f381df2cc59c973f2998b
[notmuch] / lib / lastmod-fp.cc
1 /* lastmod-fp.cc - lastmod range query glue
2  *
3  * This file is part of notmuch.
4  *
5  * Copyright © 2022 David Bremner
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see https://www.gnu.org/licenses/ .
19  *
20  * Author: David Bremner <david@tethera.net>
21  */
22
23 #include "database-private.h"
24
25 notmuch_status_t
26 _notmuch_lastmod_strings_to_query (notmuch_database_t *notmuch,
27                                    const std::string &from, const std::string &to,
28                                    Xapian::Query &output, std::string &msg)
29 {
30     long from_idx = 0L, to_idx = LONG_MAX;
31     long current;
32     std::string str;
33
34     /* revision should not change, but for the avoidance of doubt,
35      * grab for both ends of range, if needed*/
36     current = notmuch_database_get_revision (notmuch, NULL);
37
38     try {
39         if (from.empty ())
40             from_idx = 0L;
41         else
42             from_idx = std::stol (from);
43     } catch (std::logic_error &e) {
44         msg = "bad 'from' revision: '" + from + "'";
45         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
46     }
47
48     if (from_idx < 0)
49         from_idx += current;
50
51     try {
52         if (EMPTY_STRING (to))
53             to_idx = LONG_MAX;
54         else
55             to_idx = std::stol (to);
56     } catch (std::logic_error &e) {
57         msg = "bad 'to' revision: '" + to + "'";
58         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
59     }
60
61     if (to_idx < 0)
62         to_idx += current;
63
64     output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, NOTMUCH_VALUE_LAST_MOD,
65                             Xapian::sortable_serialise (from_idx),
66                             Xapian::sortable_serialise (to_idx));
67     return NOTMUCH_STATUS_SUCCESS;
68 }