2
* <one line to give the library's name and an idea of what it does.>
3
* Copyright (C) 2014 Vishesh Handa <me@vhanda.in>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
#include "wildcardpostingsource.h"
25
using namespace Baloo;
27
WildcardPostingSource::WildcardPostingSource(const QString& word, const QString& prefix)
30
m_prefix = prefix.toUtf8();
33
f.replace(QLatin1Char('?'), QLatin1Char('.'));
34
f.replace(QStringLiteral("*"), QStringLiteral(".*"));
35
f = QLatin1String("^") + f + QLatin1String("$");
36
m_regex = QRegularExpression(f);
39
void WildcardPostingSource::init(const Xapian::Database& db)
42
m_iter = db.postlist_begin("");
43
m_end = db.postlist_end("");
46
// FIXME: Maybe we want to do a query for all documents with the prefix
47
// and then just use that as a matching set?
50
bool WildcardPostingSource::isMatch(uint docid)
52
Xapian::Document doc = m_db.get_document(docid);
53
auto tit = doc.termlist_begin();
54
tit.skip_to(m_prefix.constData());
57
if (tit == doc.termlist_end())
60
std::string str = *tit;
61
QByteArray data = QByteArray::fromRawData(str.c_str(), str.length());
63
if (!data.startsWith(m_prefix)) {
67
QString s = QString::fromUtf8(data.mid(m_prefix.length()));
68
if (m_regex.match(s).hasMatch()) {
78
void WildcardPostingSource::next(Xapian::weight)
81
// This has been done so that we do not skip the first element
82
// as the PostingSource is supposed to start one before the first element
83
// whereas Xapian::Database::postlist_begin gives us the first element
92
if (m_iter == m_end) {
95
} while (!isMatch(*m_iter));
98
bool WildcardPostingSource::at_end() const
100
return m_iter == m_end;
103
Xapian::docid WildcardPostingSource::get_docid() const
111
Xapian::doccount WildcardPostingSource::get_termfreq_min() const
116
Xapian::doccount WildcardPostingSource::get_termfreq_est() const
118
return m_db.get_doccount();
121
Xapian::doccount WildcardPostingSource::get_termfreq_max() const
123
return m_db.get_doccount();