1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
| | /* query-fp.cc - "query:" field processor glue
*
* This file is part of notmuch.
*
* Copyright © 2015 Austin Clements
* Copyright © 2016 David Bremner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/ .
*
* Author: Austin Clements <aclements@csail.mit.edu>
* David Bremner <david@tethera.net>
*/
#include "regexp-fields.h"
#ifdef HAVE_XAPIAN_FIELD_PROCESSOR
RegexpPostingSource::RegexpPostingSource (Xapian::valueno slot, const std::string ®exp)
: slot_ (slot)
{
int r = regcomp (®exp_, regexp.c_str (), REG_EXTENDED | REG_NOSUB);
if (r != 0)
/* XXX Report a query syntax error using regerror */
throw "regcomp failed";
}
RegexpPostingSource::~RegexpPostingSource ()
{
regfree (®exp_);
}
void
RegexpPostingSource::init (const Xapian::Database &db)
{
db_ = db;
it_ = db_.valuestream_begin (slot_);
end_ = db.valuestream_end (slot_);
started_ = false;
}
Xapian::doccount
RegexpPostingSource::get_termfreq_min () const
{
return 0;
}
Xapian::doccount
RegexpPostingSource::get_termfreq_est () const
{
return get_termfreq_max () / 2;
}
Xapian::doccount
RegexpPostingSource::get_termfreq_max () const
{
return db_.get_value_freq (slot_);
}
Xapian::docid
RegexpPostingSource::get_docid () const
{
return it_.get_docid ();
}
bool
RegexpPostingSource::at_end () const
{
return it_ == end_;
}
void
RegexpPostingSource::next (unused (double min_wt))
{
if (started_ && ! at_end ())
++it_;
started_ = true;
for (; ! at_end (); ++it_) {
std::string value = *it_;
if (regexec (®exp_, value.c_str (), 0, NULL, 0) == 0)
break;
}
}
Xapian::Query
RegexpFieldProcessor::operator() (const std::string & str)
{
postings = new RegexpPostingSource (slot, str);
return Xapian::Query (postings);
}
#endif
|