Akonadi Search

xapiandocument.cpp
1 /*
2  * SPDX-FileCopyrightText: 2014 Vishesh Handa <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "xapiandocument.h"
9 
10 using namespace Akonadi::Search;
11 
12 XapianDocument::XapianDocument()
13  : m_termGen(&m_doc)
14 {
15 }
16 
17 XapianDocument::XapianDocument(const Xapian::Document &doc)
18  : m_doc(doc)
19  , m_termGen(&m_doc)
20 {
21 }
22 
23 void 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 
30 void XapianDocument::addBoolTerm(int term, const QString &prefix)
31 {
32  addBoolTerm(QString::number(term), prefix);
33 }
34 
35 void 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 
42 void XapianDocument::indexText(const QString &text, const QString &prefix, int wdfInc)
43 {
44  m_termGen.indexText(text, prefix, wdfInc);
45 }
46 
47 void XapianDocument::indexText(const QString &text, int wdfInc)
48 {
49  indexText(text, QString(), wdfInc);
50 }
51 
52 Xapian::Document XapianDocument::doc() const
53 {
54  return m_doc;
55 }
56 
57 void XapianDocument::addValue(int pos, const QString &value)
58 {
59  m_doc.add_value(pos, value.toStdString());
60 }
61 
62 QString 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 }
QString number(int n, int base)
QString fromUtf8(const char *str, int size)
QByteArray fromRawData(const char *data, int size)
char at(int i) const const
Akonadi search infrastructure.
Definition: core/query.h:20
std::string toStdString() const const
QByteArray toUtf8() const const
bool startsWith(const QByteArray &ba) const const
bool removeTermStartsWith(const QByteArray &prefix)
Remove all the terms which start with the prefix prefix.
const char * constData() const const
int size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Dec 5 2023 04:11:35 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.