]> git.notmuchmail.org Git - notmuch/blob - lib/regexp-fields.cc
Merge branch 'release'
[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 #if HAVE_XAPIAN_FIELD_PROCESSOR
30 static void
31 compile_regex (regex_t &regexp, const char *str)
32 {
33     int err = regcomp (&regexp, str, REG_EXTENDED | REG_NOSUB);
34
35     if (err != 0) {
36         size_t len = regerror (err, &regexp, NULL, 0);
37         char *buffer = new char[len];
38         std::string msg;
39         (void) regerror (err, &regexp, buffer, len);
40         msg.assign (buffer, len);
41         delete[] buffer;
42
43         throw Xapian::QueryParserError (msg);
44     }
45 }
46
47 RegexpPostingSource::RegexpPostingSource (Xapian::valueno slot, const std::string &regexp)
48     : slot_ (slot)
49 {
50     compile_regex (regexp_, regexp.c_str ());
51 }
52
53 RegexpPostingSource::~RegexpPostingSource ()
54 {
55     regfree (&regexp_);
56 }
57
58 void
59 RegexpPostingSource::init (const Xapian::Database &db)
60 {
61     db_ = db;
62     it_ = db_.valuestream_begin (slot_);
63     end_ = db.valuestream_end (slot_);
64     started_ = false;
65 }
66
67 Xapian::doccount
68 RegexpPostingSource::get_termfreq_min () const
69 {
70     return 0;
71 }
72
73 Xapian::doccount
74 RegexpPostingSource::get_termfreq_est () const
75 {
76     return get_termfreq_max () / 2;
77 }
78
79 Xapian::doccount
80 RegexpPostingSource::get_termfreq_max () const
81 {
82     return db_.get_value_freq (slot_);
83 }
84
85 Xapian::docid
86 RegexpPostingSource::get_docid () const
87 {
88     return it_.get_docid ();
89 }
90
91 bool
92 RegexpPostingSource::at_end () const
93 {
94     return it_ == end_;
95 }
96
97 void
98 RegexpPostingSource::next (unused (double min_wt))
99 {
100     if (started_ && ! at_end ())
101         ++it_;
102     started_ = true;
103
104     for (; ! at_end (); ++it_) {
105         std::string value = *it_;
106         if (regexec (&regexp_, value.c_str (), 0, NULL, 0) == 0)
107             break;
108     }
109 }
110
111 void
112 RegexpPostingSource::skip_to (Xapian::docid did, unused (double min_wt))
113 {
114     started_ = true;
115     it_.skip_to (did);
116     for (; ! at_end (); ++it_) {
117         std::string value = *it_;
118         if (regexec (&regexp_, value.c_str (), 0, NULL, 0) == 0)
119             break;
120     }
121 }
122
123 bool
124 RegexpPostingSource::check (Xapian::docid did, unused (double min_wt))
125 {
126     started_ = true;
127     if (!it_.check (did) || at_end ())
128         return false;
129     return (regexec (&regexp_, (*it_).c_str (), 0, NULL, 0) == 0);
130 }
131
132 static inline Xapian::valueno _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
139         throw Xapian::QueryParserError ("unsupported regexp field '" + prefix + "'");
140 }
141
142 RegexpFieldProcessor::RegexpFieldProcessor (std::string prefix, Xapian::QueryParser &parser_, notmuch_database_t *notmuch_)
143         : slot (_find_slot (prefix)), term_prefix (_find_prefix (prefix.c_str ())),
144           parser (parser_), notmuch (notmuch_)
145 {
146 };
147
148 Xapian::Query
149 RegexpFieldProcessor::operator() (const std::string & str)
150 {
151     if (str.size () == 0)
152         return Xapian::Query(Xapian::Query::OP_AND_NOT,
153                              Xapian::Query::MatchAll,
154                              Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix));
155
156     if (str.at (0) == '/') {
157         if (str.at (str.size () - 1) == '/'){
158             RegexpPostingSource *postings = new RegexpPostingSource (slot, str.substr(1,str.size () - 2));
159             return Xapian::Query (postings->release ());
160         } else {
161             throw Xapian::QueryParserError ("unmatched regex delimiter in '" + str + "'");
162         }
163     } else {
164         /* TODO replace this with a nicer API level triggering of
165          * phrase parsing, when possible */
166         std::string query_str;
167
168         if (str.find (' ') != std::string::npos)
169             query_str = '"' + str + '"';
170         else
171             query_str = str;
172
173         return parser.parse_query (query_str, NOTMUCH_QUERY_PARSER_FLAGS, term_prefix);
174     }
175 }
176 #endif