store: rename "metadata" into "properties"

properties are the constant (for the duration) values for a store; metadata may
change, so reserve that name for that.
This commit is contained in:
Dirk-Jan C. Binnema 2022-02-13 14:52:41 +02:00
parent 23fc8bdba8
commit 3820118246
9 changed files with 67 additions and 68 deletions

View File

@ -85,9 +85,9 @@ try {
StoreSingleton = std::make_unique<Mu::Store>(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB)); StoreSingleton = std::make_unique<Mu::Store>(mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB));
g_debug("mu-guile: opened store @ %s (n=%zu); maildir: %s", g_debug("mu-guile: opened store @ %s (n=%zu); maildir: %s",
StoreSingleton->metadata().database_path.c_str(), StoreSingleton->properties().database_path.c_str(),
StoreSingleton->size(), StoreSingleton->size(),
StoreSingleton->metadata().root_maildir.c_str()); StoreSingleton->properties().root_maildir.c_str());
return TRUE; return TRUE;

View File

@ -79,15 +79,15 @@ private:
struct Indexer::Private { struct Indexer::Private {
Private(Mu::Store& store) Private(Mu::Store& store)
: store_{store}, scanner_{store_.metadata().root_maildir, : store_{store}, scanner_{store_.properties().root_maildir,
[this](auto&& path, auto&& statbuf, auto&& info) { [this](auto&& path, auto&& statbuf, auto&& info) {
return handler(path, statbuf, info); return handler(path, statbuf, info);
}}, }},
max_message_size_{store_.metadata().max_message_size} max_message_size_{store_.properties().max_message_size}
{ {
g_message("created indexer for %s -> %s (batch-size: %zu)", g_message("created indexer for %s -> %s (batch-size: %zu)",
store.metadata().root_maildir.c_str(), store.properties().root_maildir.c_str(),
store.metadata().database_path.c_str(), store.metadata().batch_size); store.properties().database_path.c_str(), store.properties().batch_size);
} }
~Private() ~Private()
@ -379,7 +379,7 @@ Indexer::~Indexer() = default;
bool bool
Indexer::start(const Indexer::Config& conf) Indexer::start(const Indexer::Config& conf)
{ {
const auto mdir{priv_->store_.metadata().root_maildir}; const auto mdir{priv_->store_.properties().root_maildir};
if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0)) { if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0)) {
g_critical("'%s' is not readable: %s", mdir.c_str(), g_strerror(errno)); g_critical("'%s' is not readable: %s", mdir.c_str(), g_strerror(errno));
return false; return false;

View File

@ -983,7 +983,7 @@ Server::Private::ping_handler(const Parameters& params)
} }
Sexp::List addrs; Sexp::List addrs;
for (auto&& addr : store().metadata().personal_addresses) for (auto&& addr : store().properties().personal_addresses)
addrs.add(Sexp::make_string(addr)); addrs.add(Sexp::make_string(addr));
Sexp::List lst; Sexp::List lst;
@ -992,8 +992,8 @@ Server::Private::ping_handler(const Parameters& params)
Sexp::List proplst; Sexp::List proplst;
proplst.add_prop(":version", Sexp::make_string(VERSION)); proplst.add_prop(":version", Sexp::make_string(VERSION));
proplst.add_prop(":personal-addresses", Sexp::make_list(std::move(addrs))); proplst.add_prop(":personal-addresses", Sexp::make_list(std::move(addrs)));
proplst.add_prop(":database-path", Sexp::make_string(store().metadata().database_path)); proplst.add_prop(":database-path", Sexp::make_string(store().properties().database_path));
proplst.add_prop(":root-maildir", Sexp::make_string(store().metadata().root_maildir)); proplst.add_prop(":root-maildir", Sexp::make_string(store().properties().root_maildir));
proplst.add_prop(":doccount", Sexp::make_number(storecount)); proplst.add_prop(":doccount", Sexp::make_number(storecount));
proplst.add_prop(":queries", Sexp::make_list(std::move(qresults))); proplst.add_prop(":queries", Sexp::make_list(std::move(qresults)));

View File

@ -52,6 +52,7 @@ constexpr auto RootMaildirKey = "maildir"; // XXX: make this 'root-maildir
constexpr auto ContactsKey = "contacts"; constexpr auto ContactsKey = "contacts";
constexpr auto PersonalAddressesKey = "personal-addresses"; constexpr auto PersonalAddressesKey = "personal-addresses";
constexpr auto CreatedKey = "created"; constexpr auto CreatedKey = "created";
constexpr auto LastIndexKey = "last-index"; /* time of last index */
constexpr auto BatchSizeKey = "batch-size"; constexpr auto BatchSizeKey = "batch-size";
constexpr auto DefaultBatchSize = 250'000U; constexpr auto DefaultBatchSize = 250'000U;
@ -88,27 +89,24 @@ add_synonym_for_flag(MuFlags flag, Xapian::WritableDatabase* db)
} }
struct Store::Private { struct Store::Private {
enum struct XapianOpts { ReadOnly, enum struct XapianOpts { ReadOnly, Open, CreateOverwrite, InMemory };
Open,
CreateOverwrite,
InMemory };
Private(const std::string& path, bool readonly) Private(const std::string& path, bool readonly)
: read_only_{readonly}, db_{make_xapian_db(path, : read_only_{readonly}, db_{make_xapian_db(path,
read_only_ ? XapianOpts::ReadOnly read_only_ ? XapianOpts::ReadOnly
: XapianOpts::Open)}, : XapianOpts::Open)},
mdata_{make_metadata(path)}, contacts_{db().get_metadata(ContactsKey), properties_{make_properties(path)}, contacts_{db().get_metadata(ContactsKey),
mdata_.personal_addresses} properties_.personal_addresses}
{ {
} }
Private(const std::string& path, Private(const std::string& path,
const std::string& root_maildir, const std::string& root_maildir,
const StringVec& personal_addresses, const StringVec& personal_addresses,
const Store::Config& conf) const Store::Config& conf)
: read_only_{false}, db_{make_xapian_db(path, XapianOpts::CreateOverwrite)}, : read_only_{false}, db_{make_xapian_db(path, XapianOpts::CreateOverwrite)},
mdata_{init_metadata(conf, path, root_maildir, personal_addresses)}, properties_{init_metadata(conf, path, root_maildir, personal_addresses)},
contacts_{"", mdata_.personal_addresses} contacts_{"", properties_.personal_addresses}
{ {
} }
@ -116,14 +114,14 @@ struct Store::Private {
const StringVec& personal_addresses, const StringVec& personal_addresses,
const Store::Config& conf) const Store::Config& conf)
: read_only_{false}, db_{make_xapian_db("", XapianOpts::InMemory)}, : read_only_{false}, db_{make_xapian_db("", XapianOpts::InMemory)},
mdata_{init_metadata(conf, "", root_maildir, personal_addresses)}, properties_{init_metadata(conf, "", root_maildir, personal_addresses)},
contacts_{"", mdata_.personal_addresses} contacts_{"", properties_.personal_addresses}
{ {
} }
~Private() ~Private()
try { try {
g_debug("closing store @ %s", mdata_.database_path.c_str()); g_debug("closing store @ %s", properties_.database_path.c_str());
if (!read_only_) { if (!read_only_) {
transaction_maybe_commit(true /*force*/); transaction_maybe_commit(true /*force*/);
} }
@ -171,7 +169,7 @@ struct Store::Private {
// If not started yet, start a transaction. Otherwise, just update the transaction size. // If not started yet, start a transaction. Otherwise, just update the transaction size.
void transaction_inc() noexcept void transaction_inc() noexcept
{ {
if (mdata_.in_memory) if (properties_.in_memory)
return; // not supported return; // not supported
if (transaction_size_ == 0) { if (transaction_size_ == 0) {
@ -185,10 +183,10 @@ struct Store::Private {
// filled up a batch, or with force. // filled up a batch, or with force.
void transaction_maybe_commit(bool force = false) noexcept void transaction_maybe_commit(bool force = false) noexcept
{ {
if (mdata_.in_memory || transaction_size_ == 0) if (properties_.in_memory || transaction_size_ == 0)
return; // not supported or not in transaction return; // not supported or not in transaction
if (force || transaction_size_ >= mdata_.batch_size) { if (force || transaction_size_ >= properties_.batch_size) {
if (contacts_.dirty()) { if (contacts_.dirty()) {
xapian_try([&] { xapian_try([&] {
writable_db().set_metadata(ContactsKey, writable_db().set_metadata(ContactsKey,
@ -227,26 +225,24 @@ struct Store::Private {
return (time_t)atoll(db().get_metadata(key).c_str()); return (time_t)atoll(db().get_metadata(key).c_str());
} }
Store::Metadata make_metadata(const std::string& db_path) Store::Properties make_properties(const std::string& db_path)
{ {
Store::Metadata mdata; Store::Properties props;
mdata.database_path = db_path; props.database_path = db_path;
mdata.schema_version = db().get_metadata(SchemaVersionKey); props.schema_version = db().get_metadata(SchemaVersionKey);
mdata.created = ::atoll(db().get_metadata(CreatedKey).c_str()); props.created = ::atoll(db().get_metadata(CreatedKey).c_str());
mdata.read_only = read_only_; props.read_only = read_only_;
props.batch_size = ::atoll(db().get_metadata(BatchSizeKey).c_str());
props.max_message_size = ::atoll(db().get_metadata(MaxMessageSizeKey).c_str());
props.in_memory = db_path.empty();
props.root_maildir = db().get_metadata(RootMaildirKey);
props.personal_addresses = Mu::split(db().get_metadata(PersonalAddressesKey), ",");
mdata.batch_size = ::atoll(db().get_metadata(BatchSizeKey).c_str()); return props;
mdata.max_message_size = ::atoll(db().get_metadata(MaxMessageSizeKey).c_str());
mdata.in_memory = db_path.empty();
mdata.root_maildir = db().get_metadata(RootMaildirKey);
mdata.personal_addresses = Mu::split(db().get_metadata(PersonalAddressesKey), ",");
return mdata;
} }
Store::Metadata init_metadata(const Store::Config& conf, Store::Properties init_metadata(const Store::Config& conf,
const std::string& path, const std::string& path,
const std::string& root_maildir, const std::string& root_maildir,
const StringVec& personal_addresses) const StringVec& personal_addresses)
@ -273,7 +269,7 @@ struct Store::Private {
} }
writable_db().set_metadata(PersonalAddressesKey, addrs); writable_db().set_metadata(PersonalAddressesKey, addrs);
return make_metadata(path); return make_properties(path);
} }
Xapian::docid add_or_update_msg(Xapian::docid docid, MuMsg* msg); Xapian::docid add_or_update_msg(Xapian::docid docid, MuMsg* msg);
@ -286,7 +282,7 @@ struct Store::Private {
const bool read_only_{}; const bool read_only_{};
std::unique_ptr<Xapian::Database> db_; std::unique_ptr<Xapian::Database> db_;
const Store::Metadata mdata_; const Store::Properties properties_;
Contacts contacts_; Contacts contacts_;
std::unique_ptr<Indexer> indexer_; std::unique_ptr<Indexer> indexer_;
@ -313,12 +309,12 @@ get_uid_term(const char* path)
Store::Store(const std::string& path, bool readonly) Store::Store(const std::string& path, bool readonly)
: priv_{std::make_unique<Private>(path, readonly)} : priv_{std::make_unique<Private>(path, readonly)}
{ {
if (metadata().schema_version != ExpectedSchemaVersion) if (properties().schema_version != ExpectedSchemaVersion)
throw Mu::Error(Error::Code::SchemaMismatch, throw Mu::Error(Error::Code::SchemaMismatch,
"expected schema-version %s, but got %s; " "expected schema-version %s, but got %s; "
"please use 'mu init'", "please use 'mu init'",
ExpectedSchemaVersion, ExpectedSchemaVersion,
metadata().schema_version.c_str()); properties().schema_version.c_str());
} }
Store::Store(const std::string& path, Store::Store(const std::string& path,
@ -336,10 +332,10 @@ Store::Store(const std::string& maildir, const StringVec& personal_addresses, co
Store::~Store() = default; Store::~Store() = default;
const Store::Metadata& const Store::Properties&
Store::metadata() const Store::properties() const
{ {
return priv_->mdata_; return priv_->properties_;
} }
const Contacts& const Contacts&
@ -365,7 +361,7 @@ Store::indexer()
{ {
std::lock_guard guard{priv_->lock_}; std::lock_guard guard{priv_->lock_};
if (metadata().read_only) if (properties().read_only)
throw Error{Error::Code::Store, "no indexer for read-only store"}; throw Error{Error::Code::Store, "no indexer for read-only store"};
else if (!priv_->indexer_) else if (!priv_->indexer_)
priv_->indexer_ = std::make_unique<Indexer>(*this); priv_->indexer_ = std::make_unique<Indexer>(*this);
@ -419,7 +415,7 @@ Store::add_message(const std::string& path, bool use_transaction)
std::lock_guard guard{priv_->lock_}; std::lock_guard guard{priv_->lock_};
GError* gerr{}; GError* gerr{};
const auto maildir{maildir_from_path(metadata().root_maildir, path)}; const auto maildir{maildir_from_path(properties().root_maildir, path)};
auto msg{mu_msg_new_from_file(path.c_str(), maildir.c_str(), &gerr)}; auto msg{mu_msg_new_from_file(path.c_str(), maildir.c_str(), &gerr)};
if (G_UNLIKELY(!msg)) if (G_UNLIKELY(!msg))
throw Error{Error::Code::Message, throw Error{Error::Code::Message,

View File

@ -86,7 +86,10 @@ public:
*/ */
~Store(); ~Store();
struct Metadata { /**
* Store properties
*/
struct Properties {
std::string database_path; /**< Full path to the Xapian database */ std::string database_path; /**< Full path to the Xapian database */
std::string schema_version; /**< Database schema version */ std::string schema_version; /**< Database schema version */
std::time_t created; /**< database creation time */ std::time_t created; /**< database creation time */
@ -102,11 +105,11 @@ public:
}; };
/** /**
* Get metadata about this store. * Get properties about this store.
* *
* @return the metadata * @return the metadata
*/ */
const Metadata& metadata() const; const Properties& properties() const;
/** /**
* Get the Contacts object for this store * Get the Contacts object for this store
* *

View File

@ -43,7 +43,7 @@ test_store_ctor_dtor()
g_assert_true(store.empty()); g_assert_true(store.empty());
g_assert_cmpuint(0, ==, store.size()); g_assert_cmpuint(0, ==, store.size());
g_assert_cmpstr(MU_STORE_SCHEMA_VERSION, ==, store.metadata().schema_version.c_str()); g_assert_cmpstr(MU_STORE_SCHEMA_VERSION, ==, store.properties().schema_version.c_str());
} }
static void static void
@ -84,7 +84,7 @@ test_store_add_count_remove_in_memory()
{ {
Mu::Store store{MuTestMaildir, {}, {}}; Mu::Store store{MuTestMaildir, {}, {}};
g_assert_true(store.metadata().in_memory); g_assert_true(store.properties().in_memory);
const auto id1 = store.add_message(MuTestMaildir + "/cur/1283599333.1840_11.cthulhu!2,"); const auto id1 = store.add_message(MuTestMaildir + "/cur/1283599333.1840_11.cthulhu!2,");

View File

@ -96,7 +96,7 @@ Mu::mu_cmd_index(Mu::Store& store, const MuConfig* opts, GError** err)
return MU_ERROR; return MU_ERROR;
} }
const auto mdir{store.metadata().root_maildir}; const auto mdir{store.properties().root_maildir};
if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0)) { if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0)) {
mu_util_g_set_error(err, mu_util_g_set_error(err,
MU_ERROR_FILE, MU_ERROR_FILE,
@ -113,8 +113,8 @@ Mu::mu_cmd_index(Mu::Store& store, const MuConfig* opts, GError** err)
std::cout << "lazily "; std::cout << "lazily ";
std::cout << "indexing maildir " << col.fg(Color::Green) std::cout << "indexing maildir " << col.fg(Color::Green)
<< store.metadata().root_maildir << col.reset() << " -> store " << store.properties().root_maildir << col.reset() << " -> store "
<< col.fg(Color::Green) << store.metadata().database_path << col.reset() << col.fg(Color::Green) << store.properties().database_path << col.reset()
<< std::endl; << std::endl;
} }

View File

@ -113,8 +113,8 @@ try {
Server server{store, output_sexp_stdout}; Server server{store, output_sexp_stdout};
g_message("created server with store @ %s; maildir @ %s; debug-mode %s", g_message("created server with store @ %s; maildir @ %s; debug-mode %s",
store.metadata().database_path.c_str(), store.properties().database_path.c_str(),
store.metadata().root_maildir.c_str(), store.properties().root_maildir.c_str(),
opts->debug ? "yes" : "no"); opts->debug ? "yes" : "no");
tty = ::isatty(::fileno(stdout)); tty = ::isatty(::fileno(stdout));

View File

@ -518,14 +518,14 @@ cmd_info(const Mu::Store& store, const MuConfig* opts, GError** err)
{ {
Mu::MaybeAnsi col{!opts->nocolor}; Mu::MaybeAnsi col{!opts->nocolor};
key_val(col, "maildir", store.metadata().root_maildir); key_val(col, "maildir", store.properties().root_maildir);
key_val(col, "database-path", store.metadata().database_path); key_val(col, "database-path", store.properties().database_path);
key_val(col, "schema-version", store.metadata().schema_version); key_val(col, "schema-version", store.properties().schema_version);
key_val(col, "max-message-size", store.metadata().max_message_size); key_val(col, "max-message-size", store.properties().max_message_size);
key_val(col, "batch-size", store.metadata().batch_size); key_val(col, "batch-size", store.properties().batch_size);
key_val(col, "messages in store", store.size()); key_val(col, "messages in store", store.size());
const auto created{store.metadata().created}; const auto created{store.properties().created};
const auto tstamp{::localtime(&created)}; const auto tstamp{::localtime(&created)};
#pragma GCC diagnostic push #pragma GCC diagnostic push
@ -536,7 +536,7 @@ cmd_info(const Mu::Store& store, const MuConfig* opts, GError** err)
key_val(col, "created", tbuf); key_val(col, "created", tbuf);
const auto addrs{store.metadata().personal_addresses}; const auto addrs{store.properties().personal_addresses};
if (addrs.empty()) if (addrs.empty())
key_val(col, "personal-address", "<none>"); key_val(col, "personal-address", "<none>");
else else