mirror of https://github.com/djcb/mu.git
lib: xapian-db/config: more tests
...and xapian-db gets a small API update, update store as well.
This commit is contained in:
parent
11003000e8
commit
655a6b0499
|
@ -157,4 +157,11 @@ test('test-scanner',
|
|||
dependencies: [glib_dep, config_h_dep,
|
||||
lib_mu_utils_dep]))
|
||||
|
||||
test('test-xapian-db',
|
||||
executable('test-xapian-db', 'mu-xapian-db.cc',
|
||||
install: false,
|
||||
cpp_args: ['-DBUILD_TESTS'],
|
||||
dependencies: [lib_mu_dep, config_h_dep]))
|
||||
|
||||
|
||||
subdir('tests')
|
||||
|
|
|
@ -77,6 +77,29 @@ test_basic()
|
|||
const auto rmd = conf_db.get<Id::RootMaildir>();
|
||||
assert_equal(rmd, "/home/djcb/Maildir");
|
||||
}
|
||||
|
||||
{
|
||||
g_assert_true(Config::property<Id::BatchSize>().default_val == "50000");
|
||||
g_assert_cmpuint(conf_db.get<Id::BatchSize>(),==,50000);
|
||||
|
||||
assert_valid_result(conf_db.set<Id::BatchSize>(123456));
|
||||
g_assert_cmpuint(conf_db.get<Id::BatchSize>(),==,123456);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
MemDb db2;
|
||||
Config conf_db2{db2};
|
||||
|
||||
g_assert_cmpuint(conf_db2.get<Id::BatchSize>(),==,50000);
|
||||
g_assert_true(conf_db2.get<Id::RootMaildir>().empty());
|
||||
|
||||
// BatchSize is configurable; RootMaildir is not.
|
||||
conf_db2.import_configurable(conf_db);
|
||||
|
||||
g_assert_cmpuint(conf_db2.get<Id::BatchSize>(),==,123456);
|
||||
g_assert_true(conf_db2.get<Id::RootMaildir>().empty());
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
@ -165,29 +165,30 @@ struct Store::Private {
|
|||
Result<Store::Id>
|
||||
Store::Private::add_message_unlocked(Message& msg)
|
||||
{
|
||||
auto docid{xapian_db_.add_document(msg.document().xapian_document())};
|
||||
mu_debug("added message @ {}; docid = {}", msg.path(), docid);
|
||||
auto&& docid{xapian_db_.add_document(msg.document().xapian_document())};
|
||||
if (docid)
|
||||
mu_debug("added message @ {}; docid = {}", msg.path(), *docid);
|
||||
|
||||
return Ok(std::move(docid));
|
||||
return docid;
|
||||
}
|
||||
|
||||
|
||||
Result<Store::Id>
|
||||
Store::Private::update_message_unlocked(Message& msg, Store::Id docid)
|
||||
{
|
||||
xapian_db_.replace_document(docid, msg.document().xapian_document());
|
||||
mu_debug("updated message @ {}; docid = {}", msg.path(), docid);
|
||||
auto&& res{xapian_db_.replace_document(docid, msg.document().xapian_document())};
|
||||
if (res)
|
||||
mu_debug("updated message @ {}; docid = {}", msg.path(), *res);
|
||||
|
||||
return Ok(std::move(docid));
|
||||
return res;
|
||||
}
|
||||
|
||||
Result<Store::Id>
|
||||
Store::Private::update_message_unlocked(Message& msg, const std::string& path_to_replace)
|
||||
{
|
||||
auto id = xapian_db_.replace_document(
|
||||
return xapian_db_.replace_document(
|
||||
field_from_id(Field::Id::Path).xapian_term(path_to_replace),
|
||||
msg.document().xapian_document());
|
||||
return Ok(std::move(id));
|
||||
}
|
||||
|
||||
Option<Message>
|
||||
|
|
|
@ -90,8 +90,10 @@ make_db(const std::string& db_path, Flavor flavor)
|
|||
return Xapian::WritableDatabase(db_path, Xapian::DB_OPEN);
|
||||
case Flavor::CreateOverwrite:
|
||||
return Xapian::WritableDatabase(db_path, Xapian::DB_CREATE_OR_OVERWRITE);
|
||||
/* LCOV_EXCL_START*/
|
||||
default:
|
||||
throw std::logic_error("unknown flavor");
|
||||
/* LCOV_EXCL_STOP*/
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,3 +106,43 @@ XapianDb::XapianDb(const std::string& db_path, Flavor flavor) :
|
|||
|
||||
mu_debug("created {} / {}", flavor, *this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef BUILD_TESTS
|
||||
/*
|
||||
* Tests.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "utils/mu-test-utils.hh"
|
||||
#include "config.h"
|
||||
#include "mu-store.hh"
|
||||
|
||||
static void
|
||||
test_errors()
|
||||
{
|
||||
allow_warnings();
|
||||
|
||||
TempDir tdir;
|
||||
auto store = Store::make_new(tdir.path(), MU_TESTMAILDIR2);
|
||||
assert_valid_result(store);
|
||||
g_assert_true(store->empty());
|
||||
|
||||
XapianDb xdb(tdir.path(), Flavor::ReadOnly);
|
||||
g_assert_true(xdb.read_only());
|
||||
|
||||
g_assert_false(!!xdb.delete_document("Boo"));
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
{
|
||||
mu_test_init(&argc, &argv);
|
||||
|
||||
g_test_add_func("/xapian-db/errors", test_errors);
|
||||
|
||||
return g_test_run();
|
||||
}
|
||||
#endif /*BUILD_TESTS*/
|
||||
|
|
|
@ -307,13 +307,13 @@ public:
|
|||
*
|
||||
* @return new docid or 0
|
||||
*/
|
||||
Xapian::docid add_document(const Xapian::Document& doc) {
|
||||
return xapian_try([&]{
|
||||
Result<Xapian::docid> add_document(const Xapian::Document& doc) {
|
||||
return xapian_try_result([&]{
|
||||
DB_LOCKED;
|
||||
auto&& id= wdb().add_document(doc);
|
||||
auto&& id{wdb().add_document(doc)};
|
||||
set_timestamp(MetadataIface::last_change_key);
|
||||
return id;
|
||||
}, 0);
|
||||
return Ok(std::move(id));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -323,21 +323,24 @@ public:
|
|||
* @param id docid
|
||||
* @param doc replacement document
|
||||
*
|
||||
* @return new docid or 0
|
||||
* @return new docid or an error
|
||||
*/
|
||||
Xapian::docid replace_document(const std::string& term, const Xapian::Document& doc) {
|
||||
return xapian_try([&]{
|
||||
Result<Xapian::docid>
|
||||
replace_document(const std::string& term, const Xapian::Document& doc) {
|
||||
return xapian_try_result([&]{
|
||||
DB_LOCKED;
|
||||
auto&& id= wdb().replace_document(term, doc);
|
||||
auto&& id{wdb().replace_document(term, doc)};
|
||||
set_timestamp(MetadataIface::last_change_key);
|
||||
return id;
|
||||
}, 0);
|
||||
return Ok(std::move(id));
|
||||
});
|
||||
}
|
||||
void replace_document(const Xapian::docid id, const Xapian::Document& doc) {
|
||||
xapian_try([&]{
|
||||
Result<Xapian::docid>
|
||||
replace_document(Xapian::docid id, const Xapian::Document& doc) {
|
||||
return xapian_try_result([&]{
|
||||
DB_LOCKED;
|
||||
wdb().replace_document(id, doc);
|
||||
set_timestamp(MetadataIface::last_change_key);
|
||||
return Ok(std::move(id));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -345,20 +348,23 @@ public:
|
|||
* Delete document(s) for the given term or id
|
||||
*
|
||||
* @param term a term
|
||||
*
|
||||
* @return Ok or Error
|
||||
*/
|
||||
void delete_document(const std::string& term) {
|
||||
xapian_try([&]{
|
||||
Result<void> delete_document(const std::string& term) {
|
||||
return xapian_try_result([&]{
|
||||
DB_LOCKED;
|
||||
wdb().delete_document(term);
|
||||
set_timestamp(MetadataIface::last_change_key);
|
||||
return Ok();
|
||||
});
|
||||
}
|
||||
|
||||
void delete_document(Xapian::docid id) {
|
||||
xapian_try([&]{
|
||||
Result<void> delete_document(Xapian::docid id) {
|
||||
return xapian_try_result([&]{
|
||||
DB_LOCKED;
|
||||
wdb().delete_document(id);
|
||||
set_timestamp(MetadataIface::last_change_key);
|
||||
return Ok();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -382,15 +388,26 @@ public:
|
|||
* Start a transaction
|
||||
*
|
||||
* @param flushed
|
||||
*
|
||||
* @return Ok or Error
|
||||
*/
|
||||
void begin_transaction(bool flushed=true) {
|
||||
xapian_try([&]{ DB_LOCKED; return wdb().begin_transaction(flushed);});
|
||||
Result<void> begin_transaction(bool flushed=true) {
|
||||
return xapian_try_result([&]{
|
||||
DB_LOCKED;
|
||||
wdb().begin_transaction(flushed);
|
||||
return Ok();
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Commit a transaction
|
||||
*/
|
||||
void commit_transaction() {
|
||||
xapian_try([&]{ DB_LOCKED; return wdb().commit_transaction();});
|
||||
*
|
||||
* @return Ok or Error*/
|
||||
Result<void> commit_transaction() {
|
||||
return xapian_try_result([&]{
|
||||
DB_LOCKED;
|
||||
wdb().commit_transaction();
|
||||
return Ok();
|
||||
});
|
||||
}
|
||||
|
||||
using DbType = std::variant<Xapian::Database, Xapian::WritableDatabase>;
|
||||
|
|
Loading…
Reference in New Issue