From 655a6b04991c506ea6d6bddfa8830e6bde1164ea Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 23 Sep 2023 09:27:46 +0300 Subject: [PATCH] lib: xapian-db/config: more tests ...and xapian-db gets a small API update, update store as well. --- lib/meson.build | 7 +++++ lib/mu-config.cc | 23 +++++++++++++++++ lib/mu-store.cc | 17 ++++++------ lib/mu-xapian-db.cc | 42 ++++++++++++++++++++++++++++++ lib/mu-xapian-db.hh | 63 ++++++++++++++++++++++++++++----------------- 5 files changed, 121 insertions(+), 31 deletions(-) diff --git a/lib/meson.build b/lib/meson.build index 857f8a69..df7c8e37 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -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') diff --git a/lib/mu-config.cc b/lib/mu-config.cc index 23930f27..6ce5c845 100644 --- a/lib/mu-config.cc +++ b/lib/mu-config.cc @@ -77,6 +77,29 @@ test_basic() const auto rmd = conf_db.get(); assert_equal(rmd, "/home/djcb/Maildir"); } + + { + g_assert_true(Config::property().default_val == "50000"); + g_assert_cmpuint(conf_db.get(),==,50000); + + assert_valid_result(conf_db.set(123456)); + g_assert_cmpuint(conf_db.get(),==,123456); + } + + + { + MemDb db2; + Config conf_db2{db2}; + + g_assert_cmpuint(conf_db2.get(),==,50000); + g_assert_true(conf_db2.get().empty()); + + // BatchSize is configurable; RootMaildir is not. + conf_db2.import_configurable(conf_db); + + g_assert_cmpuint(conf_db2.get(),==,123456); + g_assert_true(conf_db2.get().empty()); + } } static void diff --git a/lib/mu-store.cc b/lib/mu-store.cc index 8864da3a..b1fa4fb3 100644 --- a/lib/mu-store.cc +++ b/lib/mu-store.cc @@ -165,29 +165,30 @@ struct Store::Private { Result 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::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::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 diff --git a/lib/mu-xapian-db.cc b/lib/mu-xapian-db.cc index 1d5a864a..0b54150f 100644 --- a/lib/mu-xapian-db.cc +++ b/lib/mu-xapian-db.cc @@ -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*/ diff --git a/lib/mu-xapian-db.hh b/lib/mu-xapian-db.hh index f6cebf4c..6fc682be 100644 --- a/lib/mu-xapian-db.hh +++ b/lib/mu-xapian-db.hh @@ -307,13 +307,13 @@ public: * * @return new docid or 0 */ - Xapian::docid add_document(const Xapian::Document& doc) { - return xapian_try([&]{ + Result 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 + 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 + 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 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 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 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 commit_transaction() { + return xapian_try_result([&]{ + DB_LOCKED; + wdb().commit_transaction(); + return Ok(); + }); } using DbType = std::variant;