~ubuntu-branches/debian/sid/baloo-kf5/sid

« back to all changes in this revision

Viewing changes to src/file/search/wildcardpostingsource.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-07-10 21:13:07 UTC
  • Revision ID: package-import@ubuntu.com-20140710211307-iku0qs6vlplgn06m
Tags: upstream-5.0.0b
ImportĀ upstreamĀ versionĀ 5.0.0b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include "wildcardpostingsource.h"
 
22
 
 
23
#include <QDebug>
 
24
 
 
25
using namespace Baloo;
 
26
 
 
27
WildcardPostingSource::WildcardPostingSource(const QString& word, const QString& prefix)
 
28
{
 
29
    m_word = word;
 
30
    m_prefix = prefix.toUtf8();
 
31
 
 
32
    QString f = word;
 
33
    f.replace(QLatin1Char('?'), QLatin1Char('.'));
 
34
    f.replace(QStringLiteral("*"), QStringLiteral(".*"));
 
35
    f = QLatin1String("^") + f + QLatin1String("$");
 
36
    m_regex = QRegularExpression(f);
 
37
}
 
38
 
 
39
void WildcardPostingSource::init(const Xapian::Database& db)
 
40
{
 
41
    m_db = db;
 
42
    m_iter = db.postlist_begin("");
 
43
    m_end = db.postlist_end("");
 
44
    m_first = true;
 
45
 
 
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?
 
48
}
 
49
 
 
50
bool WildcardPostingSource::isMatch(uint docid)
 
51
{
 
52
    Xapian::Document doc = m_db.get_document(docid);
 
53
    auto tit = doc.termlist_begin();
 
54
    tit.skip_to(m_prefix.constData());
 
55
 
 
56
    while (1) {
 
57
        if (tit == doc.termlist_end())
 
58
            break;
 
59
 
 
60
        std::string str = *tit;
 
61
        QByteArray data = QByteArray::fromRawData(str.c_str(), str.length());
 
62
 
 
63
        if (!data.startsWith(m_prefix)) {
 
64
            break;
 
65
        }
 
66
 
 
67
        QString s = QString::fromUtf8(data.mid(m_prefix.length()));
 
68
        if (m_regex.match(s).hasMatch()) {
 
69
            return true;
 
70
        }
 
71
 
 
72
        tit++;
 
73
    }
 
74
 
 
75
    return false;
 
76
}
 
77
 
 
78
void WildcardPostingSource::next(Xapian::weight)
 
79
{
 
80
    do {
 
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
 
84
        //
 
85
        if (m_first) {
 
86
            m_first = false;
 
87
        }
 
88
        else {
 
89
            m_iter++;
 
90
        }
 
91
 
 
92
        if (m_iter == m_end) {
 
93
            return;
 
94
        }
 
95
    } while (!isMatch(*m_iter));
 
96
}
 
97
 
 
98
bool WildcardPostingSource::at_end() const
 
99
{
 
100
    return m_iter == m_end;
 
101
}
 
102
 
 
103
Xapian::docid WildcardPostingSource::get_docid() const
 
104
{
 
105
    return *m_iter;
 
106
}
 
107
 
 
108
//
 
109
// Term Frequiencies
 
110
//
 
111
Xapian::doccount WildcardPostingSource::get_termfreq_min() const
 
112
{
 
113
    return 0;
 
114
}
 
115
 
 
116
Xapian::doccount WildcardPostingSource::get_termfreq_est() const
 
117
{
 
118
    return m_db.get_doccount();
 
119
}
 
120
 
 
121
Xapian::doccount WildcardPostingSource::get_termfreq_max() const
 
122
{
 
123
    return m_db.get_doccount();
 
124
}