]> git.notmuchmail.org Git - notmuch/blob - lib/regexp-fields.cc
lib: catch Xapian exceptions in n_m_get_tags
[notmuch] / lib / regexp-fields.cc
1 /* regexp-fields.cc - field processor glue for regex supporting fields
2  *
3  * This file is part of notmuch.
4  *
5  * Copyright © 2015 Austin Clements
6  * Copyright © 2016 David Bremner
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see https://www.gnu.org/licenses/ .
20  *
21  * Author: Austin Clements <aclements@csail.mit.edu>
22  *                David Bremner <david@tethera.net>
23  */
24
25 #include "regexp-fields.h"
26 #include "notmuch-private.h"
27 #include "database-private.h"
28
29 static void
30 compile_regex (regex_t &regexp, const char *str)
31 {
32     int err = regcomp (&regexp, str, REG_EXTENDED | REG_NOSUB);
33
34     if (err != 0) {
35         size_t len = regerror (err, &regexp, NULL, 0);
36         char *buffer = new char[len];
37         std::string msg = "Regexp error: ";
38         (void) regerror (err, &regexp, buffer, len);
39         msg.append (buffer, len);
40         delete[] buffer;
41
42         throw Xapian::QueryParserError (msg);
43     }
44 }
45
46 RegexpPostingSource::RegexpPostingSource (Xapian::valueno slot, const std::string &regexp)
47     : slot_ (slot)
48 {
49     compile_regex (regexp_, regexp.c_str ());
50 }
51
52 RegexpPostingSource::~RegexpPostingSource ()
53 {
54     regfree (&regexp_);
55 }
56
57 void
58 RegexpPostingSource::init (const Xapian::Database &db)
59 {
60     db_ = db;
61     it_ = db_.valuestream_begin (slot_);
62     end_ = db.valuestream_end (slot_);
63     started_ = false;
64 }
65
66 Xapian::doccount
67 RegexpPostingSource::get_termfreq_min () const
68 {
69     return 0;
70 }
71
72 Xapian::doccount
73 RegexpPostingSource::get_termfreq_est () const
74 {
75     return get_termfreq_max () / 2;
76 }
77
78 Xapian::doccount
79 RegexpPostingSource::get_termfreq_max () const
80 {
81     return db_.get_value_freq (slot_);
82 }
83
84 Xapian::docid
85 RegexpPostingSource::get_docid () const
86 {
87     return it_.get_docid ();
88 }
89
90 bool
91 RegexpPostingSource::at_end () const
92 {
93     return it_ == end_;
94 }
95
96 void
97 RegexpPostingSource::next (unused (double min_wt))
98 {
99     if (started_ && ! at_end ())
100         ++it_;
101     started_ = true;
102
103     for (; ! at_end (); ++it_) {
104         std::string value = *it_;
105         if (regexec (&regexp_, value.c_str (), 0, NULL, 0) == 0)
106             break;
107     }
108 }
109
110 void
111 RegexpPostingSource::skip_to (Xapian::docid did, unused (double min_wt))
112 {
113     started_ = true;
114     it_.skip_to (did);
115     for (; ! at_end (); ++it_) {
116         std::string value = *it_;
117         if (regexec (&regexp_, value.c_str (), 0, NULL, 0) == 0)
118             break;
119     }
120 }
121
122 bool
123 RegexpPostingSource::check (Xapian::docid did, unused (double min_wt))
124 {
125     started_ = true;
126     if (! it_.check (did) || at_end ())
127         return false;
128     return (regexec (&regexp_, (*it_).c_str (), 0, NULL, 0) == 0);
129 }
130
131 static inline Xapian::valueno
132 _find_slot (std::string prefix)
133 {
134     if (prefix == "from")
135         return NOTMUCH_VALUE_FROM;
136     else if (prefix == "subject")
137         return NOTMUCH_VALUE_SUBJECT;
138     else if (prefix == "mid")
139         return NOTMUCH_VALUE_MESSAGE_ID;
140     else
141         return Xapian::BAD_VALUENO;
142 }
143
144 RegexpFieldProcessor::RegexpFieldProcessor (std::string prefix,
145                                             notmuch_field_flag_t options_,
146                                             Xapian::QueryParser &parser_,
147                                             notmuch_database_t *notmuch_)
148     : slot (_find_slot (prefix)),
149     term_prefix (_find_prefix (prefix.c_str ())),
150     options (options_),
151     parser (parser_),
152     notmuch (notmuch_)
153 {
154 };
155
156 Xapian::Query
157 RegexpFieldProcessor::operator() (const std::string & str)
158 {
159     if (str.empty ()) {
160         if (options & NOTMUCH_FIELD_PROBABILISTIC) {
161             return Xapian::Query (Xapian::Query::OP_AND_NOT,
162                                   Xapian::Query::MatchAll,
163                                   Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix));
164         } else {
165             return Xapian::Query (term_prefix);
166         }
167     }
168
169     if (str.at (0) == '/') {
170         if (str.length () > 1 && str.at (str.size () - 1) == '/') {
171             std::string regexp_str = str.substr (1, str.size () - 2);
172             if (slot != Xapian::BAD_VALUENO) {
173                 RegexpPostingSource *postings = new RegexpPostingSource (slot, regexp_str);
174                 return Xapian::Query (postings->release ());
175             } else {
176                 std::vector<std::string> terms;
177                 regex_t regexp;
178
179                 compile_regex (regexp, regexp_str.c_str ());
180                 for (Xapian::TermIterator it = notmuch->xapian_db->allterms_begin (term_prefix);
181                      it != notmuch->xapian_db->allterms_end (); ++it) {
182                     if (regexec (&regexp, (*it).c_str () + term_prefix.size (),
183                                  0, NULL, 0) == 0)
184                         terms.push_back (*it);
185                 }
186                 return Xapian::Query (Xapian::Query::OP_OR, terms.begin (), terms.end ());
187             }
188         } else {
189             throw Xapian::QueryParserError ("unmatched regex delimiter in '" + str + "'");
190         }
191     } else {
192         if (options & NOTMUCH_FIELD_PROBABILISTIC) {
193             /* TODO replace this with a nicer API level triggering of
194              * phrase parsing, when possible */
195             std::string query_str;
196
197             if (*str.rbegin () != '*' || str.find (' ') != std::string::npos)
198                 query_str = '"' + str + '"';
199             else
200                 query_str = str;
201
202             return parser.parse_query (query_str, NOTMUCH_QUERY_PARSER_FLAGS, term_prefix);
203         } else {
204             /* Boolean prefix */
205             std::string term = term_prefix + str;
206             return Xapian::Query (term);
207         }
208     }
209 }