Skip to content

Commit

Permalink
xapian-db: improve errors, fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
djcb committed Aug 21, 2024
1 parent c58eacc commit 407c6ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
9 changes: 1 addition & 8 deletions lib/mu-xapian-db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ XapianDb::read_only() const
return !std::holds_alternative<Xapian::WritableDatabase>(db_);
}

const std::string&
XapianDb::path() const
{
return path_;
}

void
XapianDb::set_timestamp(const std::string_view key)
{
Expand Down Expand Up @@ -102,8 +96,7 @@ make_db(const std::string& db_path, Flavor flavor)
XapianDb::XapianDb(const std::string& db_path, Flavor flavor):
path_(make_path(db_path, flavor)),
db_(make_db(path_, flavor)),
batch_size_{Config(*this).get<Config::Id::BatchSize>()} // default
{
batch_size_{Config(*this).get<Config::Id::BatchSize>()/*default*/} {
if (flavor == Flavor::CreateOverwrite)
set_timestamp(MetadataIface::created_key);

Expand Down
25 changes: 18 additions & 7 deletions lib/mu-xapian-db.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ 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) {
Expand All @@ -64,6 +66,9 @@ 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) {
Expand All @@ -84,6 +89,9 @@ 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'"));
Expand Down Expand Up @@ -128,13 +136,13 @@ struct MetadataIface {


/// In-memory db
struct MemDb: public MetadataIface {
struct MemDb final: public MetadataIface {
/**
* Create a new memdb
*
* @param readonly read-only? (for testing)
*/
MemDb(bool readonly=false):read_only_{readonly} {}
explicit MemDb(bool readonly=false):read_only_{readonly} {}

/**
* Set some metadata
Expand Down Expand Up @@ -188,7 +196,7 @@ private:
/**
* Fairly thin wrapper around Xapian::Database and Xapian::WritableDatabase
*/
class XapianDb: public MetadataIface {
class XapianDb final: public MetadataIface {
public:
/**
* Type of database to create.
Expand All @@ -211,8 +219,9 @@ public:
/**
* DTOR
*/
~XapianDb() {
if (!read_only())
~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");
}
Expand All @@ -237,7 +246,9 @@ public:
*
* @return path to database
*/
const std::string& path() const;
const std::string& path() const {
return path_;
}

/**
* Get a description of the Xapian database
Expand Down Expand Up @@ -500,7 +511,7 @@ private:
Xapian::WritableDatabase& wdb();

std::string path_;
DbType db_;
DbType db_;
size_t changes_{};
bool in_transaction_{};
size_t batch_size_;
Expand Down

0 comments on commit 407c6ed

Please sign in to comment.