1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*
* Copyright (C) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
*
* 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 2 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, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//xapian.h needs to be included first to build
#include <xapian.h>
#include "fulltextindex.h"
#include <QFile>
#include <QDir>
#include "log.h"
#include "definitions.h"
FulltextIndex::FulltextIndex(const QByteArray &resourceInstanceIdentifier, Sink::Storage::DataStore::AccessMode accessMode)
: mName("fulltext"),
mDbPath{QFile::encodeName(Sink::resourceStorageLocation(resourceInstanceIdentifier) + '/' + "fulltext")}
{
try {
if (QDir{}.mkpath(mDbPath)) {
if (accessMode == Sink::Storage::DataStore::ReadWrite) {
mDb = new Xapian::WritableDatabase(mDbPath.toStdString(), Xapian::DB_CREATE_OR_OPEN);
} else {
mDb = new Xapian::Database(mDbPath.toStdString(), Xapian::DB_OPEN);
}
} else {
SinkError() << "Failed to open database" << mDbPath;
}
} catch (const Xapian::DatabaseError& e) {
SinkError() << "Failed to open database" << mDbPath << ":" << QString::fromStdString(e.get_msg());
}
}
FulltextIndex::~FulltextIndex()
{
delete mDb;
}
static std::string idTerm(const QByteArray &key)
{
return "Q" + key.toStdString();
}
void FulltextIndex::add(const QByteArray &key, const QString &value)
{
add(key, {{{}, value}});
}
void FulltextIndex::add(const QByteArray &key, const QList<QPair<QString, QString>> &values)
{
if (!mDb) {
return;
}
try {
Xapian::TermGenerator generator;
Xapian::Document document;
generator.set_document(document);
for (const auto &entry : values) {
if (!entry.second.isEmpty()) {
generator.index_text(entry.second.toStdString());
}
}
document.add_value(0, key.toStdString());
const auto idterm = idTerm(key);
document.add_boolean_term(idterm);
writableDatabase()->replace_document(idterm, document);
}
catch (const Xapian::Error &error) {
SinkError() << "Exception during Xapian commit_transaction:" << error.get_msg().c_str();
//FIXME we should somehow retry the transaction...
Q_ASSERT(false);
}
}
void FulltextIndex::commitTransaction()
{
if (mHasTransactionOpen) {
Q_ASSERT(mDb);
try {
writableDatabase()->commit_transaction();
mHasTransactionOpen = false;
}
catch (const Xapian::Error &error) {
SinkError() << "Exception during Xapian commit_transaction:" << error.get_msg().c_str();
//FIXME we should somehow retry the transaction...
Q_ASSERT(false);
}
}
}
void FulltextIndex::abortTransaction()
{
if (mHasTransactionOpen) {
Q_ASSERT(mDb);
try {
writableDatabase()->cancel_transaction();
mHasTransactionOpen = false;
}
catch (const Xapian::Error &error) {
SinkError() << "Exception during Xapian cancel_transaction:" << error.get_msg().c_str();
//FIXME we should somehow retry the transaction...
Q_ASSERT(false);
}
}
}
Xapian::WritableDatabase* FulltextIndex::writableDatabase()
{
Q_ASSERT(dynamic_cast<Xapian::WritableDatabase*>(mDb));
auto db = static_cast<Xapian::WritableDatabase*>(mDb);
if (!mHasTransactionOpen) {
try {
db->begin_transaction();
mHasTransactionOpen = true;
}
catch (const Xapian::Error &error) {
SinkError() << "Exception during Xapian begin_transaction:" << error.get_msg().c_str();
//FIXME we should somehow retry the transaction...
Q_ASSERT(false);
}
}
return db;
}
void FulltextIndex::remove(const QByteArray &key)
{
if (!mDb) {
return;
}
try {
writableDatabase()->delete_document(idTerm(key));
}
catch (const Xapian::Error &error) {
SinkError() << "Exception during Xapian delete_document:" << error.get_msg().c_str();
//FIXME we should somehow retry the transaction...
Q_ASSERT(false);
}
}
QVector<QByteArray> FulltextIndex::lookup(const QString &searchTerm)
{
if (!mDb) {
return {};
}
QVector<QByteArray> results;
try {
Xapian::QueryParser parser;
auto query = parser.parse_query(searchTerm.toStdString(), Xapian::QueryParser::FLAG_WILDCARD|Xapian::QueryParser::FLAG_PHRASE|Xapian::QueryParser::FLAG_BOOLEAN|Xapian::QueryParser::FLAG_LOVEHATE|Xapian::QueryParser::FLAG_PARTIAL);
Xapian::Enquire enquire(*mDb);
enquire.set_query(query);
auto limit = 1000;
Xapian::MSet mset = enquire.get_mset(0, limit);
Xapian::MSetIterator it = mset.begin();
for (;it != mset.end(); it++) {
auto doc = it.get_document();
const auto data = doc.get_value(0);
results << QByteArray{data.c_str(), int(data.length())};
}
}
catch (const Xapian::Error &error) {
// Nothing to do, move along
}
return results;
}
|