]> git.notmuchmail.org Git - notmuch/blob - lib/lastmod-fp.cc
emacs: Add new option notmuch-search-hide-excluded
[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 #include "lastmod-fp.h"
25
26 notmuch_status_t
27 _notmuch_lastmod_strings_to_query (notmuch_database_t *notmuch,
28                                    const std::string &from, const std::string &to,
29                                    Xapian::Query &output, std::string &msg)
30 {
31     long from_idx = 0L, to_idx = LONG_MAX;
32     long current;
33     std::string str;
34
35     /* revision should not change, but for the avoidance of doubt,
36      * grab for both ends of range, if needed*/
37     current = notmuch_database_get_revision (notmuch, NULL);
38
39     try {
40         if (from.empty ())
41             from_idx = 0L;
42         else
43             from_idx = std::stol (from);
44     } catch (std::logic_error &e) {
45         msg = "bad 'from' revision: '" + from + "'";
46         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
47     }
48
49     if (from_idx < 0)
50         from_idx += current;
51
52     try {
53         if (EMPTY_STRING (to))
54             to_idx = LONG_MAX;
55         else
56             to_idx = std::stol (to);
57     } catch (std::logic_error &e) {
58         msg = "bad 'to' revision: '" + to + "'";
59         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
60     }
61
62     if (to_idx < 0)
63         to_idx += current;
64
65     output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, NOTMUCH_VALUE_LAST_MOD,
66                             Xapian::sortable_serialise (from_idx),
67                             Xapian::sortable_serialise (to_idx));
68     return NOTMUCH_STATUS_SUCCESS;
69 }
70
71 Xapian::Query
72 LastModRangeProcessor::operator() (const std::string &begin, const std::string &end)
73 {
74
75     Xapian::Query output;
76     std::string msg;
77
78     if (_notmuch_lastmod_strings_to_query (notmuch, begin, end, output, msg))
79         throw Xapian::QueryParserError (msg);
80
81     return output;
82 }
83