From 056fecd6aa7a0b231b28b62c46bde4a535baf6d2 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Mon, 14 Mar 2022 09:38:59 +0200 Subject: [PATCH] store::for_each_term: use field-id We were trying to convert a field (string) to a xapian prefix back to a field (enum). That's unnecessarily complicated and worse, step 2 won't work. --- lib/mu-parser.cc | 2 +- lib/mu-store.cc | 16 ++-------------- lib/mu-store.hh | 4 ++-- 3 files changed, 5 insertions(+), 17 deletions(-) diff --git a/lib/mu-parser.cc b/lib/mu-parser.cc index 64730f1b..35254e35 100644 --- a/lib/mu-parser.cc +++ b/lib/mu-parser.cc @@ -200,7 +200,7 @@ Parser::Private::process_regex(const std::string& field_str, const auto field{message_field(*id_opt)}; const auto prefix{field.xapian_term()}; std::vector terms; - store_.for_each_term(prefix, [&](auto&& str) { + store_.for_each_term(field.id, [&](auto&& str) { if (std::regex_search(str.c_str() + 1, rx)) // avoid copy terms.emplace_back(str); return true; diff --git a/lib/mu-store.cc b/lib/mu-store.cc index dc983f92..9066efe1 100644 --- a/lib/mu-store.cc +++ b/lib/mu-store.cc @@ -604,7 +604,7 @@ Store::commit() } std::size_t -Store::for_each_term(const std::string& field_name, Store::ForEachTermFunc func) const +Store::for_each_term(Message::Field::Id field_id, Store::ForEachTermFunc func) const { size_t n{}; @@ -613,20 +613,8 @@ Store::for_each_term(const std::string& field_name, Store::ForEachTermFunc func) * Do _not_ take a lock; this is only called from * the message parser which already has the lock */ - - /* get id from name or shortcut */ - const auto id_opt = std::invoke([&]()->std::optional { - if (field_name.length() == 1) - return message_field_id(field_name[0]); - else - return message_field_id(field_name); - }); - - if (!id_opt) - return; - - const auto prefix{std::string{1, message_field(*id_opt).xapian_prefix()}}; std::vector terms; + const auto prefix{message_field(field_id).xapian_term()}; for (auto it = priv_->db().allterms_begin(prefix); it != priv_->db().allterms_end(prefix); ++it) { if (!func(*it)) diff --git a/lib/mu-store.hh b/lib/mu-store.hh index bba8e360..8ad97399 100644 --- a/lib/mu-store.hh +++ b/lib/mu-store.hh @@ -282,12 +282,12 @@ public: * takes a lock on the store, so the func should _not_ call any other * Store:: methods. * - * @param a prefix + * @param id the field id * @param func a Callable invoked for each message. * * @return the number of times func was invoked */ - size_t for_each_term(const std::string& prefix, ForEachTermFunc func) const; + size_t for_each_term(Message::Field::Id id, ForEachTermFunc func) const; /**