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,
|
dependencies: [glib_dep, config_h_dep,
|
||||||
lib_mu_utils_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')
|
subdir('tests')
|
||||||
|
|
|
@ -77,6 +77,29 @@ test_basic()
|
||||||
const auto rmd = conf_db.get<Id::RootMaildir>();
|
const auto rmd = conf_db.get<Id::RootMaildir>();
|
||||||
assert_equal(rmd, "/home/djcb/Maildir");
|
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
|
static void
|
||||||
|
|
|
@ -165,29 +165,30 @@ struct Store::Private {
|
||||||
Result<Store::Id>
|
Result<Store::Id>
|
||||||
Store::Private::add_message_unlocked(Message& msg)
|
Store::Private::add_message_unlocked(Message& msg)
|
||||||
{
|
{
|
||||||
auto docid{xapian_db_.add_document(msg.document().xapian_document())};
|
auto&& docid{xapian_db_.add_document(msg.document().xapian_document())};
|
||||||
mu_debug("added message @ {}; docid = {}", msg.path(), docid);
|
if (docid)
|
||||||
|
mu_debug("added message @ {}; docid = {}", msg.path(), *docid);
|
||||||
|
|
||||||
return Ok(std::move(docid));
|
return docid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Result<Store::Id>
|
Result<Store::Id>
|
||||||
Store::Private::update_message_unlocked(Message& msg, Store::Id docid)
|
Store::Private::update_message_unlocked(Message& msg, Store::Id docid)
|
||||||
{
|
{
|
||||||
xapian_db_.replace_document(docid, msg.document().xapian_document());
|
auto&& res{xapian_db_.replace_document(docid, msg.document().xapian_document())};
|
||||||
mu_debug("updated message @ {}; docid = {}", msg.path(), docid);
|
if (res)
|
||||||
|
mu_debug("updated message @ {}; docid = {}", msg.path(), *res);
|
||||||
|
|
||||||
return Ok(std::move(docid));
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<Store::Id>
|
Result<Store::Id>
|
||||||
Store::Private::update_message_unlocked(Message& msg, const std::string& path_to_replace)
|
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),
|
field_from_id(Field::Id::Path).xapian_term(path_to_replace),
|
||||||
msg.document().xapian_document());
|
msg.document().xapian_document());
|
||||||
return Ok(std::move(id));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Option<Message>
|
Option<Message>
|
||||||
|
|
|
@ -90,8 +90,10 @@ make_db(const std::string& db_path, Flavor flavor)
|
||||||
return Xapian::WritableDatabase(db_path, Xapian::DB_OPEN);
|
return Xapian::WritableDatabase(db_path, Xapian::DB_OPEN);
|
||||||
case Flavor::CreateOverwrite:
|
case Flavor::CreateOverwrite:
|
||||||
return Xapian::WritableDatabase(db_path, Xapian::DB_CREATE_OR_OVERWRITE);
|
return Xapian::WritableDatabase(db_path, Xapian::DB_CREATE_OR_OVERWRITE);
|
||||||
|
/* LCOV_EXCL_START*/
|
||||||
default:
|
default:
|
||||||
throw std::logic_error("unknown flavor");
|
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);
|
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
|
* @return new docid or 0
|
||||||
*/
|
*/
|
||||||
Xapian::docid add_document(const Xapian::Document& doc) {
|
Result<Xapian::docid> add_document(const Xapian::Document& doc) {
|
||||||
return xapian_try([&]{
|
return xapian_try_result([&]{
|
||||||
DB_LOCKED;
|
DB_LOCKED;
|
||||||
auto&& id= wdb().add_document(doc);
|
auto&& id{wdb().add_document(doc)};
|
||||||
set_timestamp(MetadataIface::last_change_key);
|
set_timestamp(MetadataIface::last_change_key);
|
||||||
return id;
|
return Ok(std::move(id));
|
||||||
}, 0);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -323,21 +323,24 @@ public:
|
||||||
* @param id docid
|
* @param id docid
|
||||||
* @param doc replacement document
|
* @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) {
|
Result<Xapian::docid>
|
||||||
return xapian_try([&]{
|
replace_document(const std::string& term, const Xapian::Document& doc) {
|
||||||
|
return xapian_try_result([&]{
|
||||||
DB_LOCKED;
|
DB_LOCKED;
|
||||||
auto&& id= wdb().replace_document(term, doc);
|
auto&& id{wdb().replace_document(term, doc)};
|
||||||
set_timestamp(MetadataIface::last_change_key);
|
set_timestamp(MetadataIface::last_change_key);
|
||||||
return id;
|
return Ok(std::move(id));
|
||||||
}, 0);
|
});
|
||||||
}
|
}
|
||||||
void replace_document(const Xapian::docid id, const Xapian::Document& doc) {
|
Result<Xapian::docid>
|
||||||
xapian_try([&]{
|
replace_document(Xapian::docid id, const Xapian::Document& doc) {
|
||||||
|
return xapian_try_result([&]{
|
||||||
DB_LOCKED;
|
DB_LOCKED;
|
||||||
wdb().replace_document(id, doc);
|
wdb().replace_document(id, doc);
|
||||||
set_timestamp(MetadataIface::last_change_key);
|
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
|
* Delete document(s) for the given term or id
|
||||||
*
|
*
|
||||||
* @param term a term
|
* @param term a term
|
||||||
|
*
|
||||||
|
* @return Ok or Error
|
||||||
*/
|
*/
|
||||||
void delete_document(const std::string& term) {
|
Result<void> delete_document(const std::string& term) {
|
||||||
xapian_try([&]{
|
return xapian_try_result([&]{
|
||||||
DB_LOCKED;
|
DB_LOCKED;
|
||||||
wdb().delete_document(term);
|
wdb().delete_document(term);
|
||||||
set_timestamp(MetadataIface::last_change_key);
|
set_timestamp(MetadataIface::last_change_key);
|
||||||
|
return Ok();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Result<void> delete_document(Xapian::docid id) {
|
||||||
void delete_document(Xapian::docid id) {
|
return xapian_try_result([&]{
|
||||||
xapian_try([&]{
|
|
||||||
DB_LOCKED;
|
DB_LOCKED;
|
||||||
wdb().delete_document(id);
|
wdb().delete_document(id);
|
||||||
set_timestamp(MetadataIface::last_change_key);
|
set_timestamp(MetadataIface::last_change_key);
|
||||||
|
return Ok();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -382,15 +388,26 @@ public:
|
||||||
* Start a transaction
|
* Start a transaction
|
||||||
*
|
*
|
||||||
* @param flushed
|
* @param flushed
|
||||||
|
*
|
||||||
|
* @return Ok or Error
|
||||||
*/
|
*/
|
||||||
void begin_transaction(bool flushed=true) {
|
Result<void> begin_transaction(bool flushed=true) {
|
||||||
xapian_try([&]{ DB_LOCKED; return wdb().begin_transaction(flushed);});
|
return xapian_try_result([&]{
|
||||||
|
DB_LOCKED;
|
||||||
|
wdb().begin_transaction(flushed);
|
||||||
|
return Ok();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Commit a transaction
|
* Commit a transaction
|
||||||
*/
|
*
|
||||||
void commit_transaction() {
|
* @return Ok or Error*/
|
||||||
xapian_try([&]{ DB_LOCKED; return wdb().commit_transaction();});
|
Result<void> commit_transaction() {
|
||||||
|
return xapian_try_result([&]{
|
||||||
|
DB_LOCKED;
|
||||||
|
wdb().commit_transaction();
|
||||||
|
return Ok();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
using DbType = std::variant<Xapian::Database, Xapian::WritableDatabase>;
|
using DbType = std::variant<Xapian::Database, Xapian::WritableDatabase>;
|
||||||
|
|
Loading…
Reference in New Issue