mirror of https://github.com/djcb/mu.git
lib: xapian-db/store: simplify
No need for "pimpl" in xapian-db; keep it simple.
This commit is contained in:
parent
a32b924692
commit
0b4f7c4cbe
|
@ -65,8 +65,8 @@ remove_slash(const std::string& str)
|
||||||
struct Store::Private {
|
struct Store::Private {
|
||||||
|
|
||||||
Private(const std::string& path, bool readonly):
|
Private(const std::string& path, bool readonly):
|
||||||
xapian_db_{make_db(path, readonly ? XapianDb::Flavor::ReadOnly
|
xapian_db_{XapianDb(path, readonly ? XapianDb::Flavor::ReadOnly
|
||||||
: XapianDb::Flavor::Open)},
|
: XapianDb::Flavor::Open)},
|
||||||
config_{xapian_db_},
|
config_{xapian_db_},
|
||||||
contacts_cache_{config_},
|
contacts_cache_{config_},
|
||||||
root_maildir_{remove_slash(config_.get<Config::Id::RootMaildir>())}
|
root_maildir_{remove_slash(config_.get<Config::Id::RootMaildir>())}
|
||||||
|
@ -74,7 +74,7 @@ struct Store::Private {
|
||||||
|
|
||||||
Private(const std::string& path, const std::string& root_maildir,
|
Private(const std::string& path, const std::string& root_maildir,
|
||||||
Option<const Config&> conf):
|
Option<const Config&> conf):
|
||||||
xapian_db_{make_db(path, XapianDb::Flavor::CreateOverwrite)},
|
xapian_db_{XapianDb(path, XapianDb::Flavor::CreateOverwrite)},
|
||||||
config_{make_config(xapian_db_, root_maildir, conf)},
|
config_{make_config(xapian_db_, root_maildir, conf)},
|
||||||
contacts_cache_{config_},
|
contacts_cache_{config_},
|
||||||
root_maildir_{remove_slash(config_.get<Config::Id::RootMaildir>())}
|
root_maildir_{remove_slash(config_.get<Config::Id::RootMaildir>())}
|
||||||
|
@ -118,13 +118,6 @@ struct Store::Private {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
XapianDb make_db(const std::string& path, XapianDb::Flavor flavor) {
|
|
||||||
if (auto&& res{XapianDb::make(path, flavor)}; res)
|
|
||||||
return std::move(res.value());
|
|
||||||
else
|
|
||||||
throw res.error();
|
|
||||||
}
|
|
||||||
|
|
||||||
Config make_config(XapianDb& xapian_db, const std::string& root_maildir,
|
Config make_config(XapianDb& xapian_db, const std::string& root_maildir,
|
||||||
Option<const Config&> conf) {
|
Option<const Config&> conf) {
|
||||||
|
|
||||||
|
|
|
@ -26,38 +26,13 @@
|
||||||
|
|
||||||
using namespace Mu;
|
using namespace Mu;
|
||||||
|
|
||||||
struct XapianDb::Private {
|
|
||||||
using DbType = std::variant<Xapian::Database, Xapian::WritableDatabase>;
|
|
||||||
|
|
||||||
Private(Xapian::Database&& db, const std::string& path):
|
|
||||||
db_{std::move(db)}, path_{path} {}
|
|
||||||
Private(Xapian::WritableDatabase&& wdb, const std::string& path):
|
|
||||||
db_{std::move(wdb)}, path_{path} {}
|
|
||||||
|
|
||||||
DbType db_;
|
|
||||||
const std::string path_;
|
|
||||||
mutable std::mutex lock_;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
XapianDb::XapianDb(Xapian::Database&& db, const std::string& path):
|
|
||||||
priv_{std::make_unique<Private>(std::move(db), path)}
|
|
||||||
{}
|
|
||||||
XapianDb::XapianDb(Xapian::WritableDatabase&& wdb, const std::string& path):
|
|
||||||
priv_{std::make_unique<Private>(std::move(wdb), path)}
|
|
||||||
{}
|
|
||||||
|
|
||||||
XapianDb::XapianDb(XapianDb&& rhs) = default;
|
|
||||||
XapianDb::~XapianDb() = default;
|
|
||||||
|
|
||||||
|
|
||||||
const Xapian::Database&
|
const Xapian::Database&
|
||||||
XapianDb::db() const
|
XapianDb::db() const
|
||||||
{
|
{
|
||||||
if (std::holds_alternative<Xapian::WritableDatabase>(priv_->db_))
|
if (std::holds_alternative<Xapian::WritableDatabase>(db_))
|
||||||
return std::get<Xapian::WritableDatabase>(priv_->db_);
|
return std::get<Xapian::WritableDatabase>(db_);
|
||||||
else
|
else
|
||||||
return std::get<Xapian::Database>(priv_->db_);
|
return std::get<Xapian::Database>(db_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Xapian::WritableDatabase&
|
Xapian::WritableDatabase&
|
||||||
|
@ -65,25 +40,19 @@ XapianDb::wdb()
|
||||||
{
|
{
|
||||||
if (read_only())
|
if (read_only())
|
||||||
throw std::runtime_error("database is read-only");
|
throw std::runtime_error("database is read-only");
|
||||||
return std::get<Xapian::WritableDatabase>(priv_->db_);
|
return std::get<Xapian::WritableDatabase>(db_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
XapianDb::read_only() const
|
XapianDb::read_only() const
|
||||||
{
|
{
|
||||||
return !std::holds_alternative<Xapian::WritableDatabase>(priv_->db_);
|
return !std::holds_alternative<Xapian::WritableDatabase>(db_);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string&
|
const std::string&
|
||||||
XapianDb::path() const
|
XapianDb::path() const
|
||||||
{
|
{
|
||||||
return priv_->path_;
|
return path_;
|
||||||
}
|
|
||||||
|
|
||||||
std::mutex&
|
|
||||||
XapianDb::lock() const
|
|
||||||
{
|
|
||||||
return priv_->lock_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -92,44 +61,44 @@ XapianDb::set_timestamp(const std::string_view key)
|
||||||
wdb().set_metadata(std::string{key}, mu_format("{}", ::time({})));
|
wdb().set_metadata(std::string{key}, mu_format("{}", ::time({})));
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<XapianDb>
|
using Flavor = XapianDb::Flavor;
|
||||||
XapianDb::make(const std::string& db_path, Flavor flavor) noexcept try {
|
|
||||||
|
|
||||||
|
static std::string
|
||||||
|
make_path(const std::string& db_path, Flavor flavor)
|
||||||
|
{
|
||||||
if (flavor != Flavor::ReadOnly) {
|
if (flavor != Flavor::ReadOnly) {
|
||||||
/* we do our own flushing, set Xapian's internal one as
|
/* we do our own flushing, set Xapian's internal one as
|
||||||
* the backstop*/
|
* the backstop*/
|
||||||
g_setenv("XAPIAN_FLUSH_THRESHOLD", "500000", 1);
|
g_setenv("XAPIAN_FLUSH_THRESHOLD", "500000", 1);
|
||||||
/* create path if needed */
|
/* create path if needed */
|
||||||
if (g_mkdir_with_parents(db_path.c_str(), 0700) != 0)
|
if (g_mkdir_with_parents(db_path.c_str(), 0700) != 0)
|
||||||
return Err(Error::Code::File, "failed to create database dir {}: {}",
|
throw Error(Error::Code::File, "failed to create database dir {}: {}",
|
||||||
db_path, ::strerror(errno));
|
db_path, ::strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return db_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
static XapianDb::DbType
|
||||||
|
make_db(const std::string& db_path, Flavor flavor)
|
||||||
|
{
|
||||||
switch (flavor) {
|
switch (flavor) {
|
||||||
|
|
||||||
case Flavor::ReadOnly:
|
case Flavor::ReadOnly:
|
||||||
return Ok(XapianDb(Xapian::Database(db_path), db_path));
|
return Xapian::Database(db_path);
|
||||||
case Flavor::Open:
|
case Flavor::Open:
|
||||||
return Ok(XapianDb(Xapian::WritableDatabase(
|
return Xapian::WritableDatabase(db_path, Xapian::DB_OPEN);
|
||||||
db_path, Xapian::DB_OPEN), db_path));
|
case Flavor::CreateOverwrite:
|
||||||
case Flavor::CreateOverwrite: {
|
return Xapian::WritableDatabase(db_path, Xapian::DB_CREATE_OR_OVERWRITE);
|
||||||
auto&& xdb{XapianDb(Xapian::WritableDatabase(
|
|
||||||
db_path,
|
|
||||||
Xapian::DB_CREATE_OR_OVERWRITE), db_path)};
|
|
||||||
xdb.set_timestamp(MetadataIface::created_key);
|
|
||||||
return Ok(std::move(xdb));
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return Err(Error::Code::Internal, "invalid xapian options");
|
throw std::logic_error("unknown flavor");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (const Xapian::DatabaseLockError& xde) {
|
|
||||||
return Err(Error::Code::StoreLock, "{}", xde.get_msg());
|
XapianDb::XapianDb(const std::string& db_path, Flavor flavor) :
|
||||||
} catch (const Xapian::DatabaseError& xde) {
|
path_(make_path(db_path, flavor)),
|
||||||
return Err(Error::Code::Store, "{}", xde.get_msg());
|
db_(make_db(path_,flavor)) {
|
||||||
} catch (const Mu::Error& me) {
|
|
||||||
return Err(me);
|
if (flavor == Flavor::CreateOverwrite)
|
||||||
} catch (...) {
|
set_timestamp(MetadataIface::created_key);
|
||||||
return Err(Error::Code::Internal,
|
|
||||||
"something went wrong when opening store @ {}", db_path);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,7 +162,7 @@ private:
|
||||||
* with just the things we need + locking + exception handling
|
* with just the things we need + locking + exception handling
|
||||||
*/
|
*/
|
||||||
class XapianDb: public MetadataIface {
|
class XapianDb: public MetadataIface {
|
||||||
#define DB_LOCKED std::unique_lock l{lock()}
|
#define DB_LOCKED std::unique_lock lock__{lock_};
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Type of database to create.
|
* Type of database to create.
|
||||||
|
@ -174,15 +174,7 @@ public:
|
||||||
CreateOverwrite, /**< Create new or overwrite existing */
|
CreateOverwrite, /**< Create new or overwrite existing */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
XapianDb(const std::string& db_path, Flavor flavor);
|
||||||
* Make a new Xapian Database wrapper object
|
|
||||||
*
|
|
||||||
* @param db_path path to the database
|
|
||||||
* @param flavor type of database to created
|
|
||||||
*
|
|
||||||
* @return a result; either a XapianDb or an Error.
|
|
||||||
*/
|
|
||||||
static Result<XapianDb> make(const std::string& db_path, Flavor flavor) noexcept;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the database read-only?
|
* Is the database read-only?
|
||||||
|
@ -364,6 +356,11 @@ public:
|
||||||
xapian_try([&]{ DB_LOCKED; return wdb().commit_transaction();});
|
xapian_try([&]{ DB_LOCKED; return wdb().commit_transaction();});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using DbType = std::variant<Xapian::Database, Xapian::WritableDatabase>;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void set_timestamp(const std::string_view key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a reference to the underlying database
|
* Get a reference to the underlying database
|
||||||
*
|
*
|
||||||
|
@ -378,26 +375,10 @@ public:
|
||||||
*/
|
*/
|
||||||
Xapian::WritableDatabase& wdb();
|
Xapian::WritableDatabase& wdb();
|
||||||
|
|
||||||
/**
|
mutable std::mutex lock_;
|
||||||
* DTOR
|
std::string path_;
|
||||||
*/
|
|
||||||
~XapianDb();
|
|
||||||
|
|
||||||
/**
|
DbType db_;
|
||||||
* Move CTOR
|
|
||||||
*
|
|
||||||
* @param rhs
|
|
||||||
*/
|
|
||||||
XapianDb(XapianDb&& rhs);
|
|
||||||
|
|
||||||
private:
|
|
||||||
XapianDb(Xapian::Database&& db, const std::string& path);
|
|
||||||
XapianDb(Xapian::WritableDatabase&& wdb, const std::string& path);
|
|
||||||
void set_timestamp(const std::string_view key);
|
|
||||||
std::mutex& lock() const;
|
|
||||||
|
|
||||||
struct Private;
|
|
||||||
std::unique_ptr<Private> priv_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Mu
|
} // namespace Mu
|
||||||
|
|
Loading…
Reference in New Issue