/* 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 * David Bremner */ #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