2009-11-25 21:55:06 +01:00
|
|
|
/*
|
2022-01-15 14:12:38 +01:00
|
|
|
** Copyright (C) 2021-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-25 21:55:06 +01:00
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
#include <chrono>
|
2020-11-03 08:58:59 +01:00
|
|
|
#include <memory>
|
2019-07-28 13:12:06 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <array>
|
|
|
|
#include <cstdlib>
|
2020-06-27 10:47:34 +02:00
|
|
|
#include <stdexcept>
|
2022-02-13 14:22:09 +01:00
|
|
|
#include <string>
|
2019-07-28 13:12:06 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <atomic>
|
2020-06-27 10:47:34 +02:00
|
|
|
#include <type_traits>
|
2020-02-06 19:24:38 +01:00
|
|
|
#include <iostream>
|
2020-02-29 13:40:22 +01:00
|
|
|
#include <cstring>
|
2020-06-27 10:47:34 +02:00
|
|
|
|
2022-02-03 21:51:02 +01:00
|
|
|
#include <vector>
|
2020-06-27 10:47:34 +02:00
|
|
|
#include <xapian.h>
|
|
|
|
|
2022-04-22 07:05:08 +02:00
|
|
|
#include "mu-maildir.hh"
|
2019-07-28 13:12:06 +02:00
|
|
|
#include "mu-store.hh"
|
2022-01-30 13:28:30 +01:00
|
|
|
#include "mu-query.hh"
|
2019-12-30 21:28:53 +01:00
|
|
|
#include "utils/mu-error.hh"
|
|
|
|
|
2019-12-16 21:41:17 +01:00
|
|
|
#include "utils/mu-utils.hh"
|
2023-01-14 16:11:36 +01:00
|
|
|
#include <utils/mu-utils-file.hh>
|
2021-11-03 11:30:37 +01:00
|
|
|
#include "utils/mu-xapian-utils.hh"
|
2011-08-29 22:35:12 +02:00
|
|
|
|
2019-07-28 13:12:06 +02:00
|
|
|
using namespace Mu;
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
static_assert(std::is_same<Store::Id, Xapian::docid>::value, "wrong type for Store::Id");
|
2020-06-27 10:47:34 +02:00
|
|
|
|
2022-05-10 07:16:47 +02:00
|
|
|
// Properties
|
2019-07-28 13:12:06 +02:00
|
|
|
constexpr auto SchemaVersionKey = "schema-version";
|
2020-01-30 23:20:34 +01:00
|
|
|
constexpr auto RootMaildirKey = "maildir"; // XXX: make this 'root-maildir'
|
2019-07-28 13:12:06 +02:00
|
|
|
constexpr auto ContactsKey = "contacts";
|
|
|
|
constexpr auto PersonalAddressesKey = "personal-addresses";
|
|
|
|
constexpr auto CreatedKey = "created";
|
2020-06-27 10:47:34 +02:00
|
|
|
constexpr auto BatchSizeKey = "batch-size";
|
|
|
|
constexpr auto DefaultBatchSize = 250'000U;
|
2019-07-28 13:12:06 +02:00
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
constexpr auto MaxMessageSizeKey = "max-message-size";
|
|
|
|
constexpr auto DefaultMaxMessageSize = 100'000'000U;
|
|
|
|
constexpr auto ExpectedSchemaVersion = MU_STORE_SCHEMA_VERSION;
|
2020-03-02 22:19:34 +01:00
|
|
|
|
2022-05-10 07:16:47 +02:00
|
|
|
// Stats.
|
|
|
|
constexpr auto ChangedKey = "changed";
|
|
|
|
constexpr auto IndexedKey = "indexed";
|
|
|
|
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
tstamp_to_string(::time_t t)
|
|
|
|
{
|
|
|
|
char buf[17];
|
|
|
|
::snprintf(buf, sizeof(buf), "%" PRIx64, static_cast<int64_t>(t));
|
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ::time_t
|
|
|
|
string_to_tstamp(const std::string& str)
|
|
|
|
{
|
|
|
|
return static_cast<::time_t>(::strtoll(str.c_str(), {}, 16));
|
|
|
|
}
|
2022-02-16 21:06:00 +01:00
|
|
|
|
2019-07-28 13:12:06 +02:00
|
|
|
struct Store::Private {
|
2022-04-22 07:05:08 +02:00
|
|
|
enum struct XapianOpts { ReadOnly, Open, CreateOverwrite };
|
2020-06-27 10:47:34 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
Private(const std::string& path, bool readonly)
|
2022-05-05 00:27:08 +02:00
|
|
|
: read_only_{readonly}, db_{make_xapian_db(path,
|
|
|
|
read_only_ ? XapianOpts::ReadOnly
|
|
|
|
: XapianOpts::Open)},
|
|
|
|
properties_{make_properties(path)},
|
|
|
|
contacts_cache_{db().get_metadata(ContactsKey),
|
2022-04-30 00:12:29 +02:00
|
|
|
properties_.personal_addresses} {
|
|
|
|
}
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-02-13 13:52:41 +01:00
|
|
|
Private(const std::string& path,
|
2022-02-19 18:00:47 +01:00
|
|
|
const std::string& root_maildir,
|
|
|
|
const StringVec& personal_addresses,
|
|
|
|
const Store::Config& conf)
|
2021-10-20 11:18:15 +02:00
|
|
|
: read_only_{false}, db_{make_xapian_db(path, XapianOpts::CreateOverwrite)},
|
2022-02-13 13:52:41 +01:00
|
|
|
properties_{init_metadata(conf, path, root_maildir, personal_addresses)},
|
2022-04-30 00:12:29 +02:00
|
|
|
contacts_cache_{"", properties_.personal_addresses} {
|
|
|
|
}
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-05-18 17:07:19 +02:00
|
|
|
~Private() try {
|
|
|
|
|
2022-02-13 13:52:41 +01:00
|
|
|
g_debug("closing store @ %s", properties_.database_path.c_str());
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!read_only_) {
|
2021-11-09 21:43:11 +01:00
|
|
|
transaction_maybe_commit(true /*force*/);
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2022-02-07 21:34:49 +01:00
|
|
|
} catch (...) {
|
|
|
|
g_critical("caught exception in store dtor");
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2020-11-03 08:58:59 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
std::unique_ptr<Xapian::Database> make_xapian_db(const std::string db_path, XapianOpts opts)
|
|
|
|
try {
|
2022-05-05 00:27:08 +02:00
|
|
|
/* we do our own flushing, set Xapian's internal one as the
|
|
|
|
* backstop*/
|
2022-05-12 07:50:20 +02:00
|
|
|
g_setenv("XAPIAN_FLUSH_THRESHOLD", "500000", 1);
|
2022-05-05 00:27:08 +02:00
|
|
|
|
2023-01-06 18:48:40 +01:00
|
|
|
if (g_mkdir_with_parents(db_path.c_str(), 0700) != 0)
|
|
|
|
throw Mu::Error(Error::Code::Internal,
|
|
|
|
"failed to create database dir %s: %s",
|
|
|
|
db_path.c_str(), ::strerror(errno));
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
switch (opts) {
|
2022-05-05 00:27:08 +02:00
|
|
|
case XapianOpts::ReadOnly:
|
|
|
|
return std::make_unique<Xapian::Database>(db_path);
|
2021-10-20 11:18:15 +02:00
|
|
|
case XapianOpts::Open:
|
|
|
|
return std::make_unique<Xapian::WritableDatabase>(db_path, Xapian::DB_OPEN);
|
|
|
|
case XapianOpts::CreateOverwrite:
|
|
|
|
return std::make_unique<Xapian::WritableDatabase>(
|
2022-05-05 00:27:08 +02:00
|
|
|
db_path,
|
2021-10-20 11:18:15 +02:00
|
|
|
Xapian::DB_CREATE_OR_OVERWRITE);
|
2022-05-05 00:27:08 +02:00
|
|
|
default:
|
|
|
|
throw std::logic_error("invalid xapian options");
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
2022-05-12 07:50:20 +02:00
|
|
|
} catch (const Xapian::DatabaseLockError& xde) {
|
|
|
|
throw Mu::Error(Error::Code::StoreLock,
|
2022-05-15 12:40:44 +02:00
|
|
|
"%s", xde.get_msg().c_str());
|
2021-10-20 11:18:15 +02:00
|
|
|
} catch (const Xapian::DatabaseError& xde) {
|
|
|
|
throw Mu::Error(Error::Code::Store,
|
2022-05-15 12:40:44 +02:00
|
|
|
"%s", xde.get_msg().c_str());
|
2023-01-06 18:48:40 +01:00
|
|
|
} catch (const Mu::Error& me) {
|
|
|
|
throw;
|
2021-10-20 11:18:15 +02:00
|
|
|
} catch (...) {
|
|
|
|
throw Mu::Error(Error::Code::Internal,
|
2022-02-19 18:00:47 +01:00
|
|
|
"something went wrong when opening store @ %s",
|
|
|
|
db_path.c_str());
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Xapian::Database& db() const { return *db_.get(); }
|
|
|
|
|
|
|
|
Xapian::WritableDatabase& writable_db()
|
|
|
|
{
|
|
|
|
if (read_only_)
|
|
|
|
throw Mu::Error(Error::Code::AccessDenied, "database is read-only");
|
|
|
|
return dynamic_cast<Xapian::WritableDatabase&>(*db_.get());
|
|
|
|
}
|
|
|
|
|
2021-11-09 21:43:11 +01:00
|
|
|
// If not started yet, start a transaction. Otherwise, just update the transaction size.
|
|
|
|
void transaction_inc() noexcept
|
2021-10-20 11:18:15 +02:00
|
|
|
{
|
2021-11-09 21:43:11 +01:00
|
|
|
if (transaction_size_ == 0) {
|
|
|
|
g_debug("starting transaction");
|
|
|
|
xapian_try([this] { writable_db().begin_transaction(); });
|
|
|
|
}
|
|
|
|
++transaction_size_;
|
2021-10-18 11:22:26 +02:00
|
|
|
}
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2021-11-09 21:43:11 +01:00
|
|
|
// Opportunistically commit a transaction if the transaction size
|
|
|
|
// filled up a batch, or with force.
|
2022-05-05 00:27:08 +02:00
|
|
|
void transaction_maybe_commit(bool force = false) noexcept {
|
2022-02-13 13:52:41 +01:00
|
|
|
if (force || transaction_size_ >= properties_.batch_size) {
|
2022-02-19 21:08:54 +01:00
|
|
|
if (contacts_cache_.dirty()) {
|
2022-01-15 14:12:38 +01:00
|
|
|
xapian_try([&] {
|
|
|
|
writable_db().set_metadata(ContactsKey,
|
2022-02-19 21:08:54 +01:00
|
|
|
contacts_cache_.serialize());
|
2022-01-15 14:12:38 +01:00
|
|
|
});
|
|
|
|
}
|
2022-05-18 19:16:48 +02:00
|
|
|
|
|
|
|
if (indexer_) { // save last index time.
|
|
|
|
if (auto&& t{indexer_->completed()}; t != 0)
|
|
|
|
writable_db().set_metadata(
|
|
|
|
IndexedKey, tstamp_to_string(t));
|
|
|
|
}
|
|
|
|
|
2022-04-22 07:05:08 +02:00
|
|
|
if (transaction_size_ == 0)
|
|
|
|
return; // nothing more to do here.
|
|
|
|
|
2022-02-03 21:51:02 +01:00
|
|
|
g_debug("committing transaction (n=%zu,%zu)",
|
2022-02-19 18:00:47 +01:00
|
|
|
transaction_size_, metadata_cache_.size());
|
2021-11-09 21:43:11 +01:00
|
|
|
xapian_try([this] {
|
2021-10-18 11:22:26 +02:00
|
|
|
writable_db().commit_transaction();
|
2022-02-13 14:22:09 +01:00
|
|
|
for (auto&& mdata : metadata_cache_)
|
2022-02-03 21:51:02 +01:00
|
|
|
writable_db().set_metadata(mdata.first, mdata.second);
|
2021-11-09 21:43:11 +01:00
|
|
|
transaction_size_ = 0;
|
|
|
|
});
|
|
|
|
}
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2020-06-27 10:47:34 +02:00
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
time_t metadata_time_t(const std::string& key) const {
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto ts = db().get_metadata(key);
|
|
|
|
return (time_t)atoll(db().get_metadata(key).c_str());
|
|
|
|
}
|
2020-06-27 10:47:34 +02:00
|
|
|
|
2022-02-13 13:52:41 +01:00
|
|
|
Store::Properties make_properties(const std::string& db_path)
|
2021-10-20 11:18:15 +02:00
|
|
|
{
|
2022-02-13 13:52:41 +01:00
|
|
|
Store::Properties props;
|
|
|
|
|
|
|
|
props.database_path = db_path;
|
|
|
|
props.schema_version = db().get_metadata(SchemaVersionKey);
|
2022-05-10 07:16:47 +02:00
|
|
|
props.created = string_to_tstamp(db().get_metadata(CreatedKey));
|
2022-02-13 13:52:41 +01:00
|
|
|
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.root_maildir = db().get_metadata(RootMaildirKey);
|
|
|
|
props.personal_addresses = Mu::split(db().get_metadata(PersonalAddressesKey), ",");
|
|
|
|
|
|
|
|
return props;
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2020-06-27 10:47:34 +02:00
|
|
|
|
2022-02-13 13:52:41 +01:00
|
|
|
Store::Properties init_metadata(const Store::Config& conf,
|
2022-04-28 21:59:03 +02:00
|
|
|
const std::string& path,
|
|
|
|
const std::string& root_maildir,
|
2022-05-05 00:27:08 +02:00
|
|
|
const StringVec& personal_addresses) {
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
writable_db().set_metadata(SchemaVersionKey, ExpectedSchemaVersion);
|
2022-05-10 07:16:47 +02:00
|
|
|
writable_db().set_metadata(CreatedKey, tstamp_to_string(::time({})));
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
const size_t batch_size = conf.batch_size ? conf.batch_size : DefaultBatchSize;
|
|
|
|
writable_db().set_metadata(BatchSizeKey, Mu::format("%zu", batch_size));
|
|
|
|
const size_t max_msg_size = conf.max_message_size ? conf.max_message_size
|
2022-02-19 18:00:47 +01:00
|
|
|
: DefaultMaxMessageSize;
|
2021-10-20 11:18:15 +02:00
|
|
|
writable_db().set_metadata(MaxMessageSizeKey, Mu::format("%zu", max_msg_size));
|
|
|
|
|
2022-04-28 21:59:03 +02:00
|
|
|
writable_db().set_metadata(RootMaildirKey, canonicalize_filename(root_maildir, {}));
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
std::string addrs;
|
|
|
|
for (const auto& addr : personal_addresses) { // _very_ minimal check.
|
|
|
|
if (addr.find(",") != std::string::npos)
|
|
|
|
throw Mu::Error(Error::Code::InvalidArgument,
|
2022-02-19 18:00:47 +01:00
|
|
|
"e-mail address '%s' contains comma",
|
|
|
|
addr.c_str());
|
2021-10-20 11:18:15 +02:00
|
|
|
addrs += (addrs.empty() ? "" : ",") + addr;
|
|
|
|
}
|
|
|
|
writable_db().set_metadata(PersonalAddressesKey, addrs);
|
|
|
|
|
2022-02-13 13:52:41 +01:00
|
|
|
return make_properties(path);
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2020-03-02 22:19:34 +01:00
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
Option<Message> find_message_unlocked(Store::Id docid) const;
|
|
|
|
Result<Store::Id> update_message_unlocked(Message& msg, Store::Id docid);
|
|
|
|
Result<Store::Id> update_message_unlocked(Message& msg, const std::string& old_path);
|
2022-12-03 15:42:19 +01:00
|
|
|
Result<Message> move_message_unlocked(Message&& msg,
|
|
|
|
Option<const std::string&> target_mdir,
|
|
|
|
Option<Flags> new_flags,
|
|
|
|
MoveOptions opts);
|
2022-05-05 00:27:08 +02:00
|
|
|
|
2022-02-03 21:51:02 +01:00
|
|
|
/* metadata to write as part of a transaction commit */
|
2022-02-13 14:22:09 +01:00
|
|
|
std::unordered_map<std::string, std::string> metadata_cache_;
|
2022-02-03 21:51:02 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
const bool read_only_{};
|
|
|
|
std::unique_ptr<Xapian::Database> db_;
|
2020-11-03 08:58:59 +01:00
|
|
|
|
2022-02-13 13:52:41 +01:00
|
|
|
const Store::Properties properties_;
|
2022-02-19 21:08:54 +01:00
|
|
|
ContactsCache contacts_cache_;
|
2021-10-20 11:18:15 +02:00
|
|
|
std::unique_ptr<Indexer> indexer_;
|
2019-07-28 13:12:06 +02:00
|
|
|
|
2021-11-09 21:43:11 +01:00
|
|
|
size_t transaction_size_{};
|
|
|
|
std::mutex lock_;
|
2019-07-28 13:12:06 +02:00
|
|
|
};
|
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
Result<Store::Id>
|
|
|
|
Store::Private::update_message_unlocked(Message& msg, Store::Id docid)
|
|
|
|
{
|
|
|
|
return xapian_try_result([&]{
|
|
|
|
writable_db().replace_document(docid, msg.document().xapian_document());
|
|
|
|
g_debug("updated message @ %s; docid = %u", msg.path().c_str(), docid);
|
2022-11-07 17:35:25 +01:00
|
|
|
//g_info("%s", msg.sexp().to_string().c_str());
|
2022-05-10 07:16:47 +02:00
|
|
|
writable_db().set_metadata(ChangedKey, tstamp_to_string(::time({})));
|
2022-05-05 00:27:08 +02:00
|
|
|
return Ok(std::move(docid));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<Store::Id>
|
|
|
|
Store::Private::update_message_unlocked(Message& msg, const std::string& path_to_replace)
|
|
|
|
{
|
|
|
|
return xapian_try_result([&]{
|
|
|
|
auto id = writable_db().replace_document(
|
|
|
|
field_from_id(Field::Id::Path).xapian_term(path_to_replace),
|
|
|
|
msg.document().xapian_document());
|
|
|
|
|
2022-05-10 07:16:47 +02:00
|
|
|
writable_db().set_metadata(ChangedKey, tstamp_to_string(::time({})));
|
2022-05-05 00:27:08 +02:00
|
|
|
return Ok(std::move(id));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Option<Message>
|
|
|
|
Store::Private::find_message_unlocked(Store::Id docid) const
|
|
|
|
{
|
|
|
|
return xapian_try([&]()->Option<Message> {
|
|
|
|
auto res = Message::make_from_document(db().get_document(docid));
|
|
|
|
if (res)
|
|
|
|
return Some(std::move(res.value()));
|
|
|
|
else
|
|
|
|
return Nothing;
|
|
|
|
}, Nothing);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-09 19:58:35 +02:00
|
|
|
Store::Store(const std::string& path, Store::Options opts)
|
|
|
|
: priv_{std::make_unique<Private>(path, none_of(opts & Store::Options::Writable))}
|
2019-07-28 13:12:06 +02:00
|
|
|
{
|
2022-09-28 21:38:06 +02:00
|
|
|
if (none_of(opts & Store::Options::Writable) &&
|
|
|
|
any_of(opts & Store::Options::ReInit))
|
|
|
|
throw Mu::Error(Error::Code::InvalidArgument,
|
|
|
|
"Options::ReInit requires Options::Writable");
|
|
|
|
|
|
|
|
if (any_of(opts & Store::Options::ReInit)) {
|
|
|
|
/* user wants to re-initialize an existing store */
|
|
|
|
Config conf{};
|
|
|
|
conf.batch_size = properties().batch_size;
|
|
|
|
conf.max_message_size = properties().max_message_size;
|
|
|
|
const auto root_maildir{properties().root_maildir};
|
|
|
|
const auto addrs{properties().personal_addresses};
|
|
|
|
/* close the old one */
|
|
|
|
this->priv_.reset();
|
|
|
|
/* and create a new one. */
|
|
|
|
Store new_store(path, root_maildir, addrs, conf);
|
|
|
|
this->priv_ = std::move(new_store.priv_);
|
2022-05-09 19:58:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 21:38:06 +02:00
|
|
|
/* otherwise, the schema version should match. */
|
2022-05-09 19:58:35 +02:00
|
|
|
if (properties().schema_version != ExpectedSchemaVersion)
|
|
|
|
throw Mu::Error(Error::Code::SchemaMismatch,
|
2022-09-28 21:38:06 +02:00
|
|
|
"expected schema-version %s, but got %s",
|
|
|
|
ExpectedSchemaVersion,
|
|
|
|
properties().schema_version.c_str());
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
Store::Store(const std::string& path,
|
2022-02-19 18:00:47 +01:00
|
|
|
const std::string& maildir,
|
|
|
|
const StringVec& personal_addresses,
|
|
|
|
const Store::Config& conf)
|
2021-10-20 11:18:15 +02:00
|
|
|
: priv_{std::make_unique<Private>(path, maildir, personal_addresses, conf)}
|
|
|
|
{
|
|
|
|
}
|
2021-02-16 18:32:15 +01:00
|
|
|
|
2022-10-26 20:51:53 +02:00
|
|
|
Store::Store(Store&& other)
|
|
|
|
{
|
|
|
|
priv_ = std::move(other.priv_);
|
|
|
|
priv_->indexer_.reset();
|
|
|
|
}
|
2022-05-09 19:58:35 +02:00
|
|
|
|
2019-07-28 13:12:06 +02:00
|
|
|
Store::~Store() = default;
|
|
|
|
|
2022-02-13 13:52:41 +01:00
|
|
|
const Store::Properties&
|
|
|
|
Store::properties() const
|
2019-07-28 13:12:06 +02:00
|
|
|
{
|
2022-02-13 13:52:41 +01:00
|
|
|
return priv_->properties_;
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2022-05-10 07:16:47 +02:00
|
|
|
Store::Statistics
|
|
|
|
Store::statistics() const
|
|
|
|
{
|
|
|
|
Statistics stats{};
|
|
|
|
|
|
|
|
stats.size = size();
|
|
|
|
stats.last_change = string_to_tstamp(priv_->db().get_metadata(ChangedKey));
|
|
|
|
stats.last_index = string_to_tstamp(priv_->db().get_metadata(IndexedKey));
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-19 21:08:54 +01:00
|
|
|
const ContactsCache&
|
|
|
|
Store::contacts_cache() const
|
2019-07-28 13:12:06 +02:00
|
|
|
{
|
2022-02-19 21:08:54 +01:00
|
|
|
return priv_->contacts_cache_;
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 08:58:59 +01:00
|
|
|
const Xapian::Database&
|
|
|
|
Store::database() const
|
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
return priv_->db();
|
2020-11-03 08:58:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
Indexer&
|
|
|
|
Store::indexer()
|
2019-07-28 13:12:06 +02:00
|
|
|
{
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2019-07-28 13:12:06 +02:00
|
|
|
|
2022-02-13 13:52:41 +01:00
|
|
|
if (properties().read_only)
|
2021-10-20 11:18:15 +02:00
|
|
|
throw Error{Error::Code::Store, "no indexer for read-only store"};
|
|
|
|
else if (!priv_->indexer_)
|
|
|
|
priv_->indexer_ = std::make_unique<Indexer>(*this);
|
2019-07-28 13:12:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return *priv_->indexer_.get();
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t
|
|
|
|
Store::size() const
|
|
|
|
{
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2021-10-20 11:18:15 +02:00
|
|
|
return priv_->db().get_doccount();
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Store::empty() const
|
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
return size() == 0;
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:59:03 +02:00
|
|
|
Result<Store::Id>
|
|
|
|
Store::add_message(const std::string& path, bool use_transaction)
|
2020-01-30 23:20:34 +01:00
|
|
|
{
|
2022-04-28 21:59:03 +02:00
|
|
|
if (auto msg{Message::make_from_path(path)}; !msg)
|
|
|
|
return Err(msg.error());
|
|
|
|
else
|
|
|
|
return add_message(msg.value(), use_transaction);
|
2020-01-30 23:20:34 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 07:05:08 +02:00
|
|
|
Result<Store::Id>
|
2022-04-28 21:59:03 +02:00
|
|
|
Store::add_message(Message& msg, bool use_transaction)
|
2020-01-30 23:20:34 +01:00
|
|
|
{
|
2022-05-05 00:27:08 +02:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
|
|
|
|
2022-05-09 18:34:07 +02:00
|
|
|
const auto mdir{maildir_from_path(msg.path(),
|
2022-04-28 21:59:03 +02:00
|
|
|
properties().root_maildir)};
|
|
|
|
if (!mdir)
|
|
|
|
return Err(mdir.error());
|
|
|
|
|
|
|
|
if (auto&& res = msg.set_maildir(mdir.value()); !res)
|
|
|
|
return Err(res.error());
|
2022-05-01 10:18:57 +02:00
|
|
|
/* add contacts from this message to cache; this cache
|
|
|
|
* also determines whether those contacts are _personal_, i.e. match
|
|
|
|
* our personal addresses.
|
|
|
|
*
|
|
|
|
* if a message has any personal contacts, mark it as personal; do
|
|
|
|
* this by updating the message flags.
|
|
|
|
*/
|
|
|
|
bool is_personal{};
|
|
|
|
priv_->contacts_cache_.add(msg.all_contacts(), is_personal);
|
|
|
|
if (is_personal)
|
|
|
|
msg.set_flags(msg.flags() | Flags::Personal);
|
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
if (use_transaction)
|
|
|
|
priv_->transaction_inc();
|
2022-04-28 21:59:03 +02:00
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
auto res = priv_->update_message_unlocked(msg, msg.path());
|
|
|
|
if (!res)
|
|
|
|
return Err(res.error());
|
2021-11-09 21:43:11 +01:00
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
if (use_transaction) /* commit if batch is full */
|
|
|
|
priv_->transaction_maybe_commit();
|
2020-01-30 23:20:34 +01:00
|
|
|
|
2022-05-30 19:31:23 +02:00
|
|
|
g_debug("added %smessage @ %s; docid = %u",
|
|
|
|
is_personal ? "personal " : "", msg.path().c_str(), *res);
|
2020-06-10 16:41:00 +02:00
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
return res;
|
2020-01-30 23:20:34 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:59:03 +02:00
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
Result<Store::Id>
|
|
|
|
Store::update_message(Message& msg, Store::Id docid)
|
2020-06-10 08:04:47 +02:00
|
|
|
{
|
2022-05-01 00:13:17 +02:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
return priv_->update_message_unlocked(msg, docid);
|
2020-06-10 08:04:47 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 23:20:34 +01:00
|
|
|
bool
|
2021-10-20 11:18:15 +02:00
|
|
|
Store::remove_message(const std::string& path)
|
2020-01-30 23:20:34 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
return xapian_try(
|
|
|
|
[&] {
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2022-04-22 07:05:08 +02:00
|
|
|
const auto term{field_from_id(Field::Id::Path).xapian_term(path)};
|
2021-10-20 11:18:15 +02:00
|
|
|
priv_->writable_db().delete_document(term);
|
2022-05-10 07:16:47 +02:00
|
|
|
priv_->writable_db().set_metadata(
|
|
|
|
ChangedKey, tstamp_to_string(::time({})));
|
2021-10-20 11:18:15 +02:00
|
|
|
g_debug("deleted message @ %s from store", path.c_str());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
false);
|
2020-01-30 23:20:34 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
void
|
2021-10-20 11:18:15 +02:00
|
|
|
Store::remove_messages(const std::vector<Store::Id>& ids)
|
2020-06-27 10:47:34 +02:00
|
|
|
{
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2021-11-09 21:43:11 +01:00
|
|
|
|
|
|
|
priv_->transaction_inc();
|
2021-10-20 16:18:51 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
xapian_try([&] {
|
|
|
|
for (auto&& id : ids) {
|
|
|
|
priv_->writable_db().delete_document(id);
|
|
|
|
}
|
2022-05-10 07:16:47 +02:00
|
|
|
priv_->writable_db().set_metadata(
|
|
|
|
ChangedKey, tstamp_to_string(::time({})));
|
2021-10-20 11:18:15 +02:00
|
|
|
});
|
2021-10-20 16:18:51 +02:00
|
|
|
|
2021-11-09 21:43:11 +01:00
|
|
|
priv_->transaction_maybe_commit(true /*force*/);
|
2020-06-27 10:47:34 +02:00
|
|
|
}
|
2020-01-30 23:20:34 +01:00
|
|
|
|
2022-02-13 14:22:09 +01:00
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
Option<Message>
|
|
|
|
Store::find_message(Store::Id docid) const
|
|
|
|
{
|
|
|
|
std::lock_guard guard{priv_->lock_};
|
|
|
|
|
|
|
|
return priv_->find_message_unlocked(docid);
|
|
|
|
}
|
|
|
|
|
2022-12-03 15:42:19 +01:00
|
|
|
/**
|
|
|
|
* Move a message in store and filesystem.
|
|
|
|
*
|
|
|
|
* Lock is assumed taken already
|
|
|
|
*
|
|
|
|
* @param id message id
|
|
|
|
* @param target_mdir target_midr (or Nothing for current)
|
|
|
|
* @param new_flags new flags (or Notthing)
|
2023-01-14 17:07:39 +01:00
|
|
|
* @param opts move_optionss
|
2022-12-03 15:42:19 +01:00
|
|
|
*
|
|
|
|
* @return the Message after the moving, or an Error
|
|
|
|
*/
|
2022-04-22 07:05:08 +02:00
|
|
|
Result<Message>
|
2022-12-03 15:42:19 +01:00
|
|
|
Store::Private::move_message_unlocked(Message&& msg,
|
|
|
|
Option<const std::string&> target_mdir,
|
|
|
|
Option<Flags> new_flags,
|
|
|
|
MoveOptions opts)
|
2022-04-22 07:05:08 +02:00
|
|
|
{
|
2022-12-03 15:42:19 +01:00
|
|
|
const auto old_path = msg.path();
|
|
|
|
const auto target_flags = new_flags.value_or(msg.flags());
|
|
|
|
const auto target_maildir = target_mdir.value_or(msg.maildir());
|
2022-04-22 07:05:08 +02:00
|
|
|
|
|
|
|
/* 1. first determine the file system path of the target */
|
|
|
|
const auto target_path =
|
2022-12-03 15:42:19 +01:00
|
|
|
maildir_determine_target(msg.path(), properties_.root_maildir,
|
|
|
|
target_maildir, target_flags,
|
|
|
|
any_of(opts & MoveOptions::ChangeName));
|
2022-04-22 07:05:08 +02:00
|
|
|
if (!target_path)
|
|
|
|
return Err(target_path.error());
|
|
|
|
|
|
|
|
/* 2. let's move it */
|
2022-12-03 15:42:19 +01:00
|
|
|
if (const auto res = maildir_move_message(msg.path(), target_path.value()); !res)
|
2022-05-05 00:27:08 +02:00
|
|
|
return Err(res.error());
|
2022-04-22 07:05:08 +02:00
|
|
|
|
|
|
|
/* 3. file move worked, now update the message with the new info.*/
|
2022-12-03 15:42:19 +01:00
|
|
|
if (auto&& res = msg.update_after_move(
|
2022-05-05 00:27:08 +02:00
|
|
|
target_path.value(), target_maildir, target_flags); !res)
|
|
|
|
return Err(res.error());
|
2022-04-22 07:05:08 +02:00
|
|
|
|
|
|
|
/* 4. update message worked; re-store it */
|
2022-12-03 15:42:19 +01:00
|
|
|
if (auto&& res = update_message_unlocked(msg, old_path); !res)
|
2022-05-05 00:27:08 +02:00
|
|
|
return Err(res.error());
|
2022-04-22 07:05:08 +02:00
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
/* 6. Profit! */
|
2022-12-03 15:42:19 +01:00
|
|
|
return Ok(std::move(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* get a vec of all messages with the given message id */
|
|
|
|
static Store::IdMessageVec
|
|
|
|
messages_with_msgid(const Store& store, const std::string& msgid, size_t max=100)
|
|
|
|
{
|
|
|
|
if (msgid.size() > MaxTermLength) {
|
|
|
|
g_warning("invalid message-id '%s'", msgid.c_str());
|
|
|
|
return {};
|
|
|
|
} else if (msgid.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto xprefix{field_from_id(Field::Id::MessageId).shortcut};
|
|
|
|
/*XXX this is a bit dodgy */
|
|
|
|
auto tmp{g_ascii_strdown(msgid.c_str(), -1)};
|
|
|
|
auto expr{g_strdup_printf("%c:%s", xprefix, tmp)};
|
|
|
|
g_free(tmp);
|
|
|
|
|
|
|
|
const auto res{store.run_query(expr, {}, QueryFlags::None, max)};
|
|
|
|
g_free(expr);
|
|
|
|
if (!res) {
|
|
|
|
g_warning("failed to run message-id-query: %s", res.error().what());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (res->empty()) {
|
|
|
|
g_warning("could not find message(s) for msgid %s", msgid.c_str());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Store::IdMessageVec imvec;
|
|
|
|
for (auto&& mi : *res)
|
|
|
|
imvec.emplace_back(std::make_pair(mi.doc_id(), mi.message().value()));
|
|
|
|
|
|
|
|
return imvec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Flags
|
|
|
|
filter_dup_flags(Flags old_flags, Flags new_flags)
|
|
|
|
{
|
|
|
|
new_flags = flags_keep_unmutable(old_flags, new_flags, Flags::Draft);
|
|
|
|
new_flags = flags_keep_unmutable(old_flags, new_flags, Flags::Flagged);
|
|
|
|
new_flags = flags_keep_unmutable(old_flags, new_flags, Flags::Trashed);
|
|
|
|
|
|
|
|
return new_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<Store::IdMessageVec>
|
|
|
|
Store::move_message(Store::Id id,
|
|
|
|
Option<const std::string&> target_mdir,
|
|
|
|
Option<Flags> new_flags,
|
|
|
|
MoveOptions opts)
|
|
|
|
{
|
|
|
|
std::lock_guard guard{priv_->lock_};
|
|
|
|
|
|
|
|
auto msg{priv_->find_message_unlocked(id)};
|
|
|
|
if (!msg)
|
|
|
|
return Err(Error::Code::Store, "cannot find message <%u>", id);
|
|
|
|
|
|
|
|
auto res{priv_->move_message_unlocked(std::move(*msg), target_mdir, new_flags, opts)};
|
|
|
|
if (!res)
|
|
|
|
return Err(res.error());
|
|
|
|
|
|
|
|
IdMessageVec imvec;
|
|
|
|
imvec.emplace_back(std::make_pair(id, std::move(*res)));
|
|
|
|
if (none_of(opts & Store::MoveOptions::DupFlags) || !new_flags)
|
|
|
|
return Ok(std::move(imvec));
|
|
|
|
|
|
|
|
/* handle the dupflags case; i.e. apply (a subset of) the flags to
|
|
|
|
* all messages with the same message-id as well */
|
|
|
|
for (auto&& [docid, msg]: messages_with_msgid(*this, imvec.at(0).second.message_id())) {
|
|
|
|
|
|
|
|
if (docid == id)
|
|
|
|
continue; // already
|
|
|
|
|
|
|
|
/* For now, don't change Draft/Flagged/Trashed */
|
|
|
|
Flags dup_flags = filter_dup_flags(msg.flags(), *new_flags);
|
|
|
|
|
|
|
|
/* use the updated new_flags and default MoveOptions (so we don't recurse, nor do we
|
|
|
|
* change the base-name of moved messages) */
|
|
|
|
auto dup_res = priv_->move_message_unlocked(std::move(msg), Nothing,
|
|
|
|
dup_flags,
|
|
|
|
Store::MoveOptions::None);
|
|
|
|
// just log a warning if it fails, but continue.
|
|
|
|
if (dup_res)
|
|
|
|
imvec.emplace_back(docid, std::move(*dup_res));
|
|
|
|
else
|
|
|
|
g_warning("failed to move dup: %s", dup_res.error().what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(std::move(imvec));
|
2022-04-22 07:05:08 +02:00
|
|
|
}
|
|
|
|
|
2022-02-13 14:22:09 +01:00
|
|
|
std::string
|
|
|
|
Store::metadata(const std::string& key) const
|
2019-07-28 13:12:06 +02:00
|
|
|
{
|
2022-02-13 14:22:09 +01:00
|
|
|
// get metadata either from the (uncommitted) cache or from the store.
|
|
|
|
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2019-07-28 13:12:06 +02:00
|
|
|
|
2022-02-13 14:22:09 +01:00
|
|
|
const auto it = priv_->metadata_cache_.find(key);
|
|
|
|
if (it != priv_->metadata_cache_.end())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return xapian_try([&] {
|
|
|
|
return priv_->db().get_metadata(key);
|
|
|
|
}, "");
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-02-13 14:22:09 +01:00
|
|
|
Store::set_metadata(const std::string& key, const std::string& val)
|
2019-07-28 13:12:06 +02:00
|
|
|
{
|
2022-02-13 14:22:09 +01:00
|
|
|
// get metadata either from the (uncommitted) cache or from the store.
|
|
|
|
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2019-07-28 13:12:06 +02:00
|
|
|
|
2022-02-13 14:22:09 +01:00
|
|
|
priv_->metadata_cache_.erase(key);
|
|
|
|
priv_->metadata_cache_.emplace(key, val);
|
|
|
|
}
|
2022-01-30 13:28:30 +01:00
|
|
|
|
2022-02-13 14:22:09 +01:00
|
|
|
|
|
|
|
time_t
|
|
|
|
Store::dirstamp(const std::string& path) const
|
|
|
|
{
|
|
|
|
constexpr auto epoch = static_cast<time_t>(0);
|
|
|
|
const auto ts{metadata(path)};
|
|
|
|
if (ts.empty())
|
|
|
|
return epoch;
|
|
|
|
else
|
|
|
|
return static_cast<time_t>(strtoll(ts.c_str(), NULL, 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Store::set_dirstamp(const std::string& path, time_t tstamp)
|
|
|
|
{
|
|
|
|
std::array<char, 2 * sizeof(tstamp) + 1> data{};
|
2022-01-30 13:28:30 +01:00
|
|
|
const auto len = static_cast<size_t>(
|
|
|
|
g_snprintf(data.data(), data.size(), "%zx", tstamp));
|
2019-07-28 13:12:06 +02:00
|
|
|
|
2022-02-13 14:22:09 +01:00
|
|
|
set_metadata(path, std::string{data.data(), len});
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 23:20:34 +01:00
|
|
|
bool
|
2021-10-20 11:18:15 +02:00
|
|
|
Store::contains_message(const std::string& path) const
|
2020-01-30 23:20:34 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
return xapian_try(
|
|
|
|
[&] {
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2022-04-22 07:05:08 +02:00
|
|
|
const auto term{field_from_id(Field::Id::Path).xapian_term(path)};
|
2021-10-20 11:18:15 +02:00
|
|
|
return priv_->db().term_exists(term);
|
|
|
|
},
|
|
|
|
false);
|
2020-01-30 23:20:34 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 10:47:34 +02:00
|
|
|
std::size_t
|
2021-10-20 11:18:15 +02:00
|
|
|
Store::for_each_message_path(Store::ForEachMessageFunc msg_func) const
|
2019-07-28 13:12:06 +02:00
|
|
|
{
|
2021-10-18 11:22:26 +02:00
|
|
|
size_t n{};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
xapian_try([&] {
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2021-10-18 11:22:26 +02:00
|
|
|
Xapian::Enquire enq{priv_->db()};
|
2022-01-30 13:28:30 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
enq.set_query(Xapian::Query::MatchAll);
|
|
|
|
enq.set_cutoff(0, 0);
|
2019-07-28 13:12:06 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
Xapian::MSet matches(enq.get_mset(0, priv_->db().get_doccount()));
|
2022-03-20 13:12:41 +01:00
|
|
|
constexpr auto path_no{field_from_id(Field::Id::Path).value_no()};
|
2021-10-20 11:18:15 +02:00
|
|
|
for (auto&& it = matches.begin(); it != matches.end(); ++it, ++n)
|
2022-03-04 23:29:21 +01:00
|
|
|
if (!msg_func(*it, it.get_document().get_value(path_no)))
|
2021-10-20 11:18:15 +02:00
|
|
|
break;
|
|
|
|
});
|
2020-06-27 10:47:34 +02:00
|
|
|
|
2021-10-18 11:22:26 +02:00
|
|
|
return n;
|
2019-07-28 13:12:06 +02:00
|
|
|
}
|
2011-08-29 22:35:12 +02:00
|
|
|
|
2021-11-09 21:43:11 +01:00
|
|
|
void
|
|
|
|
Store::commit()
|
|
|
|
{
|
2022-01-30 13:28:30 +01:00
|
|
|
std::lock_guard guard{priv_->lock_};
|
2021-11-09 21:43:11 +01:00
|
|
|
priv_->transaction_maybe_commit(true /*force*/);
|
|
|
|
}
|
|
|
|
|
2022-05-10 07:16:47 +02:00
|
|
|
|
2020-11-03 08:58:59 +01:00
|
|
|
std::size_t
|
2022-03-20 13:12:41 +01:00
|
|
|
Store::for_each_term(Field::Id field_id, Store::ForEachTermFunc func) const
|
2020-11-03 08:58:59 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
size_t n{};
|
|
|
|
|
|
|
|
xapian_try([&] {
|
2022-02-05 07:44:43 +01:00
|
|
|
/*
|
|
|
|
* Do _not_ take a lock; this is only called from
|
2022-03-04 23:29:21 +01:00
|
|
|
* the message parser which already has the lock
|
|
|
|
*/
|
2021-10-20 11:18:15 +02:00
|
|
|
std::vector<std::string> terms;
|
2022-03-20 13:12:41 +01:00
|
|
|
const auto prefix{field_from_id(field_id).xapian_term()};
|
2022-03-04 23:29:21 +01:00
|
|
|
for (auto it = priv_->db().allterms_begin(prefix);
|
|
|
|
it != priv_->db().allterms_end(prefix); ++it) {
|
2022-05-05 00:27:08 +02:00
|
|
|
++n;
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!func(*it))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return n;
|
2020-11-03 08:58:59 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:45:04 +01:00
|
|
|
std::mutex&
|
|
|
|
Store::lock() const
|
|
|
|
{
|
|
|
|
return priv_->lock_;
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:59:03 +02:00
|
|
|
Result<QueryResults>
|
2022-03-04 23:29:21 +01:00
|
|
|
Store::run_query(const std::string& expr,
|
2022-04-28 21:59:03 +02:00
|
|
|
Field::Id sortfield_id,
|
2022-02-19 18:00:47 +01:00
|
|
|
QueryFlags flags, size_t maxnum) const
|
2022-01-30 13:28:30 +01:00
|
|
|
{
|
2022-04-28 21:59:03 +02:00
|
|
|
return Query{*this}.run(expr, sortfield_id, flags, maxnum);
|
2022-01-30 13:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
Store::count_query(const std::string& expr) const
|
|
|
|
{
|
|
|
|
return xapian_try([&] {
|
|
|
|
std::lock_guard guard{priv_->lock_};
|
|
|
|
Query q{*this};
|
2022-03-04 23:29:21 +01:00
|
|
|
return q.count(expr); }, 0);
|
2022-01-30 13:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Store::parse_query(const std::string& expr, bool xapian) const
|
|
|
|
{
|
|
|
|
return xapian_try([&] {
|
|
|
|
std::lock_guard guard{priv_->lock_};
|
|
|
|
Query q{*this};
|
|
|
|
|
|
|
|
return q.parse(expr, xapian);
|
|
|
|
},
|
2022-02-19 18:00:47 +01:00
|
|
|
std::string{});
|
2022-01-30 13:28:30 +01:00
|
|
|
}
|