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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
/*
** Copyright (C) 2024 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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 3, 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.
**
*/
#ifndef MU_XAPIAN_DB_HH__
#define MU_XAPIAN_DB_HH__
#include <variant>
#include <memory>
#include <string>
#include <mutex>
#include <thread>
#include <functional>
#include <unordered_map>
#include <glib.h>
#include <utils/mu-result.hh>
#include <utils/mu-utils.hh>
/*
* starting with 1.4.6, Xapian supports C++ move semantics,
* but only with XAPIAN_MOVE_SEMANTICS defined. We require
* at lest 1.4.22 so we can define it.
*/
#ifndef XAPIAN_MOVE_SEMANTICS
#define XAPIAN_MOVE_SEMANTICS
#endif /*XAPIAN_MOVE_SEMANTICS*/
#include <xapian.h>
namespace Mu {
// LCOV_EXCL_START
// avoid exception-handling boilerplate.
template <typename Func> void
xapian_try(Func&& func) noexcept
try {
func();
} catch (const Mu::Error& me) {
mu_critical("{}: mu error '{}'", __func__, me.what());
} catch (const Xapian::Error& xerr) {
mu_critical("{}: xapian error '{}'", __func__, xerr.get_msg());
} catch (const std::runtime_error& re) {
mu_critical("{}: runtime error: {}", __func__, re.what());
} catch (const std::exception& e) {
mu_critical("{}: caught std::exception: {}", __func__, e.what());
} catch (...) {
mu_critical("{}: caught exception", __func__);
}
template <typename Func, typename Default = std::invoke_result<Func>> auto
xapian_try(Func&& func, Default&& def) noexcept -> std::decay_t<decltype(func())>
try {
return func();
} catch (const Mu::Error& me) {
mu_critical("{}: mu error '{}'", __func__, me.what());
return static_cast<Default>(def);
} catch (const Xapian::DocNotFoundError& xerr) {
return static_cast<Default>(def);
} catch (const Xapian::Error& xerr) {
mu_warning("{}: xapian error '{}'", __func__, xerr.get_msg());
return static_cast<Default>(def);
} catch (const std::runtime_error& re) {
mu_critical("{}: runtime error: {}", __func__, re.what());
return static_cast<Default>(def);
} catch (const std::exception& e) {
mu_critical("{}: caught std::exception: {}", __func__, e.what());
return static_cast<Default>(def);
} catch (...) {
mu_critical("{}: caught exception", __func__);
return static_cast<Default>(def);
}
template <typename Func> auto
xapian_try_result(Func&& func) noexcept -> std::decay_t<decltype(func())>
try {
return func();
} catch (const Mu::Error& me) {
return Err(std::move(me));
} catch (const Xapian::DatabaseNotFoundError& nferr) {
return Err(Error{Error::Code::Xapian, "failed to open database"}.
add_hint("Try (re)creating using `mu init'"));
} catch (const Xapian::DatabaseLockError& dlerr) {
return Err(Error{Error::Code::StoreLock, "database locked"}.
add_hint("Perhaps mu is already running?"));
} catch (const Xapian::DatabaseCorruptError& dcerr) {
return Err(Error{Error::Code::Xapian, "failed to read database"}.
add_hint("Try (re)creating using `mu init'"));
} catch (const Xapian::DocNotFoundError& dnferr) {
return Err(Error{Error::Code::Xapian, "message not found in database"}.
add_hint("Try reopening the database"));
} catch (const Xapian::Error& xerr) {
return Err(Error::Code::Xapian, "{}", xerr.get_msg());
} catch (const std::runtime_error& re) {
return Err(Error::Code::Internal, "runtime error: {}", re.what());
} catch (const std::exception& e) {
return Err(Error::Code::Internal, "caught std::exception: {}", e.what());
} catch (...) {
return Err(Error::Code::Internal, "caught exception");
}
// LCOV_EXCL_STOP
/// abstract base
struct MetadataIface {
virtual ~MetadataIface(){}
virtual void set_metadata(const std::string& name, const std::string& val) = 0;
virtual std::string metadata(const std::string& name) const = 0;
virtual bool read_only() const = 0;
using each_func = std::function<void(const std::string&, const std::string&)>;
virtual void for_each(each_func&& func) const =0;
/*
* These are special: handled on the Xapian db level
* rather than Config
*/
static inline constexpr std::string_view created_key = "created";
static inline constexpr std::string_view last_change_key = "last-change";
};
/// In-memory db
struct MemDb final: public MetadataIface {
/**
* Create a new memdb
*
* @param readonly read-only? (for testing)
*/
explicit MemDb(bool readonly=false):read_only_{readonly} {}
/**
* Set some metadata
*
* @param name key name
* @param val value
*/
void set_metadata(const std::string& name, const std::string& val) override {
map_.erase(name);
map_[name] = val;
}
/**
* Get metadata for given key, empty if not found
*
* @param name key name
*
* @return string
*/
std::string metadata(const std::string& name) const override {
if (auto&& it = map_.find(name); it != map_.end())
return it->second;
else
return {};
}
/**
* Is this db read-only?
*
* @return true or false
*/
bool read_only() const override { return read_only_; }
/**
* Invoke function for each key/value pair. Do not call
* @this from each_func().
*
* @param func a function
*/
void for_each(MetadataIface::each_func&& func) const override {
for (const auto& [key, value] : map_)
func(key, value);
}
private:
std::unordered_map<std::string, std::string> map_;
const bool read_only_;
};
/**
* Fairly thin wrapper around Xapian::Database and Xapian::WritableDatabase
*/
class XapianDb final: public MetadataIface {
public:
/**
* Type of database to create.
*
*/
enum struct Flavor {
ReadOnly, /**< Read-only database */
Open, /**< Open existing read-write */
CreateOverwrite, /**< Create new or overwrite existing */
};
/**
* XapianDb CTOR. This may throw.
*
* @param db_path path to the database
* @param flavor kind of database
*/
XapianDb(const std::string& db_path, Flavor flavor);
/**
* DTOR
*/
~XapianDb() override {
// shouldn't use read_only() here, since that's virtual.
if (std::holds_alternative<Xapian::WritableDatabase>(db_))
request_commit(true/*force*/);
mu_debug("closing db");
}
/**
* Reinitialize from inner-config. Needed after CreateOverwrite.
*
* This is bit of a hack, needed since we cannot setup the config
* before we have a database.
*/
void reinit();
/**
* Is the database read-only?
*
* @return true or false
*/
bool read_only() const override;
/**
* Path to the database; empty for in-memory databases
*
* @return path to database
*/
const std::string& path() const {
return path_;
}
/**
* Get a description of the Xapian database
*
* @return description
*/
const std::string description() const {
return db().get_description();
}
/**
* Get the number of documents (messages) in the database
*
* @return number
*/
size_t size() const noexcept {
return xapian_try([this]{
return db().get_doccount(); }, 0);
}
/**
* Is the base empty?
*
* @return true or false
*/
size_t empty() const noexcept { return size() == 0; }
/**
* Get a database enquire object for queries.
*
* @return an enquire object
*/
Xapian::Enquire enquire() const {
return Xapian::Enquire(db());
}
/**
* Get a document from the database if there is one
*
* @param id id of the document
*
* @return the document or an error
*/
Result<Xapian::Document> document(Xapian::docid id) const {
return xapian_try_result([&]{
return Ok(db().get_document(id)); });
}
/**
* Get metadata for the given key
*
* @param key key (non-empty)
*
* @return the value or empty
*/
std::string metadata(const std::string& key) const override {
return xapian_try([&]{
return db().get_metadata(key);}, "");
}
/**
* Set metadata for the given key
*
* @param key key (non-empty)
* @param val new value for key
*/
void set_metadata(const std::string& key, const std::string& val) override {
xapian_try([&] { wdb().set_metadata(key, val);
maybe_commit();});
}
/**
* Invoke function for each key/value pair. This is called with the lock
* held, so do not call functions on @this is each_func().
*
* @param each_func a function
*/
//using each_func = MetadataIface::each_func;
void for_each(MetadataIface::each_func&& func) const override {
xapian_try([&]{
for (auto&& it = db().metadata_keys_begin();
it != db().metadata_keys_end(); ++it)
func(*it, db().get_metadata(*it));
});
}
/**
* Does the given term exist in the database?
*
* @param term some term
*
* @return true or false
*/
bool term_exists(const std::string& term) const {
return xapian_try([&]{
return db().term_exists(term);}, false);
}
/**
* Add a new document to the database
*
* @param doc a document (message)
*
* @return new docid or 0
*/
Result<Xapian::docid> add_document(const Xapian::Document& doc) {
return xapian_try_result([&]{
auto&& id{wdb().add_document(doc)};
set_timestamp(MetadataIface::last_change_key);
maybe_commit();
return Ok(std::move(id));
});
}
/**
* Replace document in database
*
* @param term unique term
* @param id docid
* @param doc replacement document
*
* @return new docid or an error
*/
Result<Xapian::docid>
replace_document(const std::string& term,
const Xapian::Document& doc) {
return xapian_try_result([&]{
auto&& id{wdb().replace_document(term, doc)};
set_timestamp(MetadataIface::last_change_key);
maybe_commit();
return Ok(std::move(id));
});
}
Result<Xapian::docid>
replace_document(Xapian::docid id,
const Xapian::Document& doc) {
return xapian_try_result([&]{
wdb().replace_document(id, doc);
set_timestamp(MetadataIface::last_change_key);
maybe_commit();
return Ok(std::move(id));
});
}
/**
* Delete document(s) for the given term or id
*
* @param term a term
*
* @return Ok or Error
*/
Result<void> delete_document(const std::string& term) {
return xapian_try_result([&]{
wdb().delete_document(term);
set_timestamp(MetadataIface::last_change_key);
maybe_commit();
return Ok();
});
}
Result<void> delete_document(Xapian::docid id) {
return xapian_try_result([&]{
wdb().delete_document(id);
set_timestamp(MetadataIface::last_change_key);
maybe_commit();
return Ok();
});
}
template<typename Func>
size_t all_terms(const std::string& prefix, Func&& func) const {
size_t n{};
for (auto it = db().allterms_begin(prefix); it != db().allterms_end(prefix); ++it) {
if (!func(*it))
break;
++n;
}
return n;
}
/**
* Requests a transaction to be started; this is only
* a request, which may not be granted.
*
* If you're already in a transaction but that transaction
* was started in another thread, that transaction will be
* committed before starting a new one.
*
* Otherwise, start a transaction if you're not already in one.
*
* @return A result; either true if a transaction was started; false
* otherwise, or an error.
*/
Result<bool> request_transaction() {
return xapian_try_result([this]() {
auto& db = wdb();
if (in_transaction())
return Ok(false); // nothing to
db.begin_transaction();
mu_debug("begin transaction");
in_transaction_ = true;
return Ok(true);
});
}
/**
* Explicitly request the Xapian DB to be committed to disk
*
* @param force whether to force-commit
*/
void request_commit(bool force = false) { request_commit(wdb(), force); }
void maybe_commit() { request_commit(false); }
/**
* Are we inside a transaction?
*
* @return true or false
*/
bool in_transaction() const { return in_transaction_; }
using DbType = std::variant<Xapian::Database, Xapian::WritableDatabase>;
private:
/**
* To be called with DB_LOCKED held.
*/
void request_commit(Xapian::WritableDatabase& db, bool force) {
if ((++changes_ < batch_size_) && !force)
return;
xapian_try([&]{
mu_debug("committing {} change(s); transaction={}; "
"forced={}", changes_,
in_transaction() ? "yes" : "no",
force ? "yes" : "no");
if (in_transaction()) {
db.commit_transaction();
in_transaction_ = {};
}
db.commit();
changes_ = 0;
});
}
void set_timestamp(const std::string_view key);
/**
* Get a reference to the underlying database
*
* @return db database reference
*/
const Xapian::Database& db() const;
/**
* Get a reference to the underlying writable database. It is
* an error to call this on a read-only database.
*
* @return db writable database reference
*/
Xapian::WritableDatabase& wdb();
std::string path_;
DbType db_;
size_t changes_{};
bool in_transaction_{};
size_t batch_size_;
};
constexpr std::string_view
format_as(XapianDb::Flavor flavor)
{
switch(flavor) {
case XapianDb::Flavor::CreateOverwrite:
return "create-overwrite";
case XapianDb::Flavor::Open:
return "open";
case XapianDb::Flavor::ReadOnly:
return "read-only";
default:
return "??";
}
}
static inline std::string
format_as(const XapianDb& db)
{
return mu_format("{} @ {}", db.description(), db.path());
}
} // namespace Mu
#endif /* MU_XAPIAN_DB_HH__ */
|