message/document: update sexp on the fly

Keep the sexp for the document up to date during scan / change, instead of
having a separate step.
This commit is contained in:
Dirk-Jan C. Binnema
2022-05-05 01:22:14 +03:00
parent 5da066a59e
commit 85fed37870
8 changed files with 364 additions and 329 deletions

View File

@ -27,6 +27,7 @@
#include <charconv>
#include <cinttypes>
#include <string>
#include <utils/mu-utils.hh>
@ -48,12 +49,13 @@ add_search_term(Xapian::Document& doc, const Field& field, const std::string& va
termgen.index_text(utf8_flatten(val),1,field.xapian_term());
} else
throw std::logic_error("not a search term");
}
if (field.id == Field::Id::Tags)
for (auto tag = doc.termlist_begin(); tag != doc.termlist_end(); ++tag)
if ((*tag)[0] == 'X')
g_message("%u: %s", doc.get_docid(), (*tag).c_str());
static std::string
make_prop_name(const Field& field)
{
return ":" + std::string(field.name);
}
void
@ -66,6 +68,10 @@ Document::add(Field::Id id, const std::string& val)
if (field.is_searchable())
add_search_term(xdoc_, field, val);
if (field.include_in_sexp())
sexp_list().add_prop(make_prop_name(field),
Sexp::make_string(std::move(val)));
}
void
@ -82,6 +88,14 @@ Document::add(Field::Id id, const std::vector<std::string>& vals)
std::for_each(vals.begin(), vals.end(),
[&](const auto& val) {
add_search_term(xdoc_, field, val); });
if (field.include_in_sexp()) {
Sexp::List elms;
for(auto&& val: vals)
elms.add(Sexp::make_string(val));
sexp_list().add_prop(make_prop_name(field),
Sexp::make_list(std::move(elms)));
}
}
@ -91,6 +105,24 @@ Document::string_vec_value(Field::Id field_id) const noexcept
return Mu::split(string_value(field_id), SepaChar1);
}
static Sexp
make_contacts_sexp(const Contacts& contacts)
{
Sexp::List clist;
seq_for_each(contacts, [&](auto&& c) {
if (!c.name.empty())
clist.add(Sexp::make_prop_list(
":name", Sexp::make_string(c.name),
":email", Sexp::make_string(c.email)));
else
clist.add(Sexp::make_prop_list(
":email", Sexp::make_string(c.email)));
});
return Sexp::make_list(std::move(clist));
}
void
Document::add(Field::Id id, const Contacts& contacts)
{
@ -122,6 +154,11 @@ Document::add(Field::Id id, const Contacts& contacts)
if (!cvec.empty())
xdoc_.add_value(field.value_no(), join(cvec, SepaChar1));
if (field.include_in_sexp())
sexp_list().add_prop(make_prop_name(field),
make_contacts_sexp(contacts));
}
Contacts
@ -152,6 +189,26 @@ Document::contacts_value(Field::Id id) const noexcept
return contacts;
}
void
Document::add_extra_contacts(const std::string& propname, const Contacts& contacts)
{
if (!contacts.empty())
sexp_list().add_prop(std::string{propname},
make_contacts_sexp(contacts));
}
static Sexp
make_emacs_time_sexp(::time_t t)
{
Sexp::List dlist;
dlist.add(Sexp::make_number(static_cast<unsigned>(t >> 16)));
dlist.add(Sexp::make_number(static_cast<unsigned>(t & 0xffff)));
dlist.add(Sexp::make_number(0));
return Sexp::make_list(std::move(dlist));
}
void
Document::add(Field::Id id, int64_t val)
{
@ -166,6 +223,15 @@ Document::add(Field::Id id, int64_t val)
if (field.is_value())
xdoc_.add_value(field.value_no(), to_lexnum(val));
if (field.include_in_sexp()) {
if (field.is_time_t())
sexp_list().add_prop(make_prop_name(field),
make_emacs_time_sexp(val));
else
sexp_list().add_prop(make_prop_name(field),
Sexp::make_number(val));
}
}
int64_t
@ -184,8 +250,11 @@ Document::add(Priority prio)
xdoc_.add_value(field.value_no(), std::string(1, to_char(prio)));
xdoc_.add_boolean_term(field.xapian_term(to_char(prio)));
}
if (field.include_in_sexp())
sexp_list().add_prop(make_prop_name(field),
Sexp::make_symbol_sv(priority_name(prio)));
}
Priority
Document::priority_value() const noexcept
@ -198,19 +267,51 @@ void
Document::add(Flags flags)
{
constexpr auto field{field_from_id(Field::Id::Flags)};
const auto old_flags{flags_value()};
Sexp::List flaglist;
xdoc_.add_value(field.value_no(), to_lexnum(static_cast<int64_t>(flags)));
flag_infos_for_each([&](auto&& flag_info) {
auto term=[&](){return field.xapian_term(flag_info.shortcut_lower());};
if (any_of(flag_info.flag & flags))
if (any_of(flag_info.flag & flags)) {
xdoc_.add_boolean_term(term());
else if(any_of(flag_info.flag & old_flags)) {
/* field can be _updated_, so clear out any removed flags */
xdoc_.remove_term(term());
flaglist.add(Sexp::make_symbol_sv(flag_info.name));
}
});
if (field.include_in_sexp())
sexp_list().add_prop(make_prop_name(field),
Sexp::make_list(std::move(flaglist)));
}
Sexp::List&
Document::sexp_list()
{
/* perhaps we need get the sexp_ from the document first? */
if (sexp_list_.empty()) {
const auto str{xdoc_.get_data()};
if (!str.empty()) {
Sexp sexp{Sexp::make_parse(str)};
sexp_list_ = sexp.list();
}
}
return sexp_list_;
}
std::string
Document::cached_sexp() const
{
return xdoc_.get_data();
}
void
Document::update_cached_sexp(void)
{
if (sexp_list_.empty())
return; /* nothing to do; i.e. the exisiting sexp is still up to
* date */
xdoc_.set_data(Sexp::make_list(Sexp::List{sexp_list()}).to_sexp_string());
}
Flags
@ -219,6 +320,39 @@ Document::flags_value() const noexcept
return static_cast<Flags>(integer_value(Field::Id::Flags));
}
void
Document::remove(Field::Id field_id)
{
const auto field{field_from_id(field_id)};
const auto pfx{field.xapian_prefix()};
xapian_try([&]{
if (auto&& val{xdoc_.get_value(field.value_no())}; !val.empty()) {
g_debug("removing value<%u>: '%s'", field.value_no(),
val.c_str());
xdoc_.remove_value(field.value_no());
}
std::vector<std::string> kill_list;
for (auto&& it = xdoc_.termlist_begin();
it != xdoc_.termlist_end(); ++it) {
const auto term{*it};
if (!term.empty() && term.at(0) == pfx)
kill_list.emplace_back(term);
}
for (auto&& term: kill_list) {
g_debug("removing term '%s'", term.c_str());
try {
xdoc_.remove_term(term);
} catch(const Xapian::InvalidArgumentError& xe) {
g_critical("failed to remove '%s'", term.c_str());
}
}
});
}
#ifdef BUILD_TESTS