Akonadi Search

xapiandocument.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7
8#include "xapiandocument.h"
9
10using namespace Akonadi::Search;
11
12XapianDocument::XapianDocument()
13 : m_termGen(&m_doc)
14{
15}
16
17XapianDocument::XapianDocument(const Xapian::Document &doc)
18 : m_doc(doc)
19 , m_termGen(&m_doc)
20{
21}
22
23void XapianDocument::addTerm(const QString &term, const QString &prefix)
24{
25 const QByteArray arr = prefix.toUtf8() + term.toUtf8();
26
27 m_doc.add_term(arr.constData());
28}
29
30void XapianDocument::addBoolTerm(int term, const QString &prefix)
31{
32 addBoolTerm(QString::number(term), prefix);
33}
34
35void XapianDocument::addBoolTerm(const QString &term, const QString &prefix)
36{
37 const QByteArray arr = prefix.toUtf8() + term.toUtf8();
38
39 m_doc.add_boolean_term(arr.constData());
40}
41
42void XapianDocument::indexText(const QString &text, const QString &prefix, int wdfInc)
43{
44 m_termGen.indexText(text, prefix, wdfInc);
45}
46
47void XapianDocument::indexText(const QString &text, int wdfInc)
48{
49 indexText(text, QString(), wdfInc);
50}
51
52Xapian::Document XapianDocument::doc() const
53{
54 return m_doc;
55}
56
57void XapianDocument::addValue(int pos, const QString &value)
58{
59 m_doc.add_value(pos, value.toStdString());
60}
61
62QString XapianDocument::fetchTermStartsWith(const QByteArray &term)
63{
64 try {
65 Xapian::TermIterator it = m_doc.termlist_begin();
66 it.skip_to(term.constData());
67
68 if (it == m_doc.termlist_end()) {
69 return {};
70 }
71 std::string str = *it;
72 return QString::fromUtf8(str.c_str(), str.length());
73 } catch (const Xapian::Error &) {
74 return {};
75 }
76}
77
79{
80 bool modified = false;
81
82 Xapian::TermIterator it = m_doc.termlist_begin();
83 it.skip_to(prefix.constData());
84 while (it != m_doc.termlist_end()) {
85 const std::string t = *it;
86 const QByteArray term = QByteArray::fromRawData(t.c_str(), t.size());
87 if (!term.startsWith(prefix)) {
88 break;
89 }
90
91 // The term should not just be the prefix
92 if (term.size() <= prefix.size()) {
93 break;
94 }
95
96 // The term should not contain any more upper case letters
97 if (isupper(term.at(prefix.size()))) {
98 ++it;
99 continue;
100 }
101
102 ++it;
103 m_doc.remove_term(t);
104 modified = true;
105 }
106
107 return modified;
108}
bool removeTermStartsWith(const QByteArray &prefix)
Remove all the terms which start with the prefix prefix.
Akonadi search infrastructure.
Definition core/query.h:21
char at(qsizetype i) const const
const char * constData() const const
QByteArray fromRawData(const char *data, qsizetype size)
qsizetype size() const const
bool startsWith(QByteArrayView bv) const const
QString fromUtf8(QByteArrayView str)
QString number(double n, char format, int precision)
std::string toStdString() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.