lib: rename contacts into contacts-cache

Plus dependents.
This commit is contained in:
Dirk-Jan C. Binnema
2022-02-19 22:08:54 +02:00
parent 39c45abc38
commit a82bd77d09
10 changed files with 137 additions and 138 deletions

View File

@ -61,8 +61,8 @@ noinst_LTLIBRARIES= \
libmu_la_SOURCES= \ libmu_la_SOURCES= \
mu-bookmarks.cc \ mu-bookmarks.cc \
mu-bookmarks.hh \ mu-bookmarks.hh \
mu-contacts.cc \ mu-contacts-cache.cc \
mu-contacts.hh \ mu-contacts-cache.hh \
mu-data.hh \ mu-data.hh \
mu-parser.cc \ mu-parser.cc \
mu-parser.hh \ mu-parser.hh \

View File

@ -23,8 +23,8 @@ lib_mu=static_library(
[ [
'mu-bookmarks.cc', 'mu-bookmarks.cc',
'mu-bookmarks.hh', 'mu-bookmarks.hh',
'mu-contacts.cc', 'mu-contacts-cache.cc',
'mu-contacts.hh', 'mu-contacts-cache.hh',
'mu-data.hh', 'mu-data.hh',
'mu-parser.cc', 'mu-parser.cc',
'mu-parser.hh', 'mu-parser.hh',

View File

@ -17,7 +17,7 @@
** **
*/ */
#include "mu-contacts.hh" #include "mu-contacts-cache.hh"
#include <mutex> #include <mutex>
#include <unordered_map> #include <unordered_map>
@ -105,7 +105,7 @@ using ContactUMap = std::unordered_map<const std::string, ContactInfo, EmailHash
// using ContactUSet = std::unordered_set<ContactInfo, ContactInfoHash, ContactInfoEqual>; // using ContactUSet = std::unordered_set<ContactInfo, ContactInfoHash, ContactInfoEqual>;
using ContactSet = std::set<std::reference_wrapper<const ContactInfo>, ContactInfoLessThan>; using ContactSet = std::set<std::reference_wrapper<const ContactInfo>, ContactInfoLessThan>;
struct Contacts::Private { struct ContactsCache::Private {
Private(const std::string& serialized, const StringVec& personal) Private(const std::string& serialized, const StringVec& personal)
: contacts_{deserialize(serialized)}, dirty_{0} : contacts_{deserialize(serialized)}, dirty_{0}
{ {
@ -128,7 +128,7 @@ struct Contacts::Private {
constexpr auto Separator = "\xff"; // Invalid in UTF-8 constexpr auto Separator = "\xff"; // Invalid in UTF-8
void void
Contacts::Private::make_personal(const StringVec& personal) ContactsCache::Private::make_personal(const StringVec& personal)
{ {
for (auto&& p : personal) { for (auto&& p : personal) {
if (p.empty()) if (p.empty())
@ -154,7 +154,7 @@ Contacts::Private::make_personal(const StringVec& personal)
} }
ContactUMap ContactUMap
Contacts::Private::deserialize(const std::string& serialized) const ContactsCache::Private::deserialize(const std::string& serialized) const
{ {
ContactUMap contacts; ContactUMap contacts;
std::stringstream ss{serialized, std::ios_base::in}; std::stringstream ss{serialized, std::ios_base::in};
@ -180,14 +180,14 @@ Contacts::Private::deserialize(const std::string& serialized) const
return contacts; return contacts;
} }
Contacts::Contacts(const std::string& serialized, const StringVec& personal) ContactsCache::ContactsCache(const std::string& serialized, const StringVec& personal)
: priv_{std::make_unique<Private>(serialized, personal)} : priv_{std::make_unique<Private>(serialized, personal)}
{ {
} }
Contacts::~Contacts() = default; ContactsCache::~ContactsCache() = default;
std::string std::string
Contacts::serialize() const ContactsCache::serialize() const
{ {
std::lock_guard<std::mutex> l_{priv_->mtx_}; std::lock_guard<std::mutex> l_{priv_->mtx_};
std::string s; std::string s;
@ -219,13 +219,13 @@ Contacts::serialize() const
} }
bool bool
Contacts::dirty() const ContactsCache::dirty() const
{ {
return priv_->dirty_; return priv_->dirty_;
} }
const ContactInfo const ContactInfo
Contacts::add(ContactInfo&& ci) ContactsCache::add(ContactInfo&& ci)
{ {
std::lock_guard<std::mutex> l_{priv_->mtx_}; std::lock_guard<std::mutex> l_{priv_->mtx_};
@ -261,7 +261,7 @@ Contacts::add(ContactInfo&& ci)
} }
const ContactInfo* const ContactInfo*
Contacts::_find(const std::string& email) const ContactsCache::_find(const std::string& email) const
{ {
std::lock_guard<std::mutex> l_{priv_->mtx_}; std::lock_guard<std::mutex> l_{priv_->mtx_};
@ -273,7 +273,7 @@ Contacts::_find(const std::string& email) const
} }
void void
Contacts::clear() ContactsCache::clear()
{ {
std::lock_guard<std::mutex> l_{priv_->mtx_}; std::lock_guard<std::mutex> l_{priv_->mtx_};
@ -283,7 +283,7 @@ Contacts::clear()
} }
std::size_t std::size_t
Contacts::size() const ContactsCache::size() const
{ {
std::lock_guard<std::mutex> l_{priv_->mtx_}; std::lock_guard<std::mutex> l_{priv_->mtx_};
@ -291,7 +291,7 @@ Contacts::size() const
} }
void void
Contacts::for_each(const EachContactFunc& each_contact) const ContactsCache::for_each(const EachContactFunc& each_contact) const
{ {
std::lock_guard<std::mutex> l_{priv_->mtx_}; std::lock_guard<std::mutex> l_{priv_->mtx_};
@ -308,7 +308,7 @@ Contacts::for_each(const EachContactFunc& each_contact) const
} }
bool bool
Contacts::is_personal(const std::string& addr) const ContactsCache::is_personal(const std::string& addr) const
{ {
for (auto&& p : priv_->personal_plain_) for (auto&& p : priv_->personal_plain_)
if (g_ascii_strcasecmp(addr.c_str(), p.c_str()) == 0) if (g_ascii_strcasecmp(addr.c_str(), p.c_str()) == 0)
@ -334,7 +334,7 @@ Contacts::is_personal(const std::string& addr) const
static void static void
test_mu_contacts_01() test_mu_contacts_01()
{ {
Mu::Contacts contacts(""); Mu::ContactsCache contacts("");
g_assert_true(contacts.empty()); g_assert_true(contacts.empty());
g_assert_cmpuint(contacts.size(), ==, 0); g_assert_cmpuint(contacts.size(), ==, 0);
@ -385,7 +385,7 @@ static void
test_mu_contacts_02() test_mu_contacts_02()
{ {
Mu::StringVec personal = {"foo@example.com", "bar@cuux.org", "/bar-.*@fnorb.f./"}; Mu::StringVec personal = {"foo@example.com", "bar@cuux.org", "/bar-.*@fnorb.f./"};
Mu::Contacts contacts{"", personal}; Mu::ContactsCache contacts{"", personal};
g_assert_true(contacts.is_personal("foo@example.com")); g_assert_true(contacts.is_personal("foo@example.com"));
g_assert_true(contacts.is_personal("Bar@CuuX.orG")); g_assert_true(contacts.is_personal("Bar@CuuX.orG"));

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2020-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** 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 ** under the terms of the GNU General Public License as published by the
@ -17,8 +17,8 @@
** **
*/ */
#ifndef __MU_CONTACTS_HH__ #ifndef __MU_CONTACTS_CACHE_HH__
#define __MU_CONTACTS_HH__ #define __MU_CONTACTS_CACHE_HH__
#include <glib.h> #include <glib.h>
#include <time.h> #include <time.h>
@ -62,22 +62,21 @@ struct ContactInfo {
int64_t tstamp{}; /**< Time-stamp, as per g_get_monotonic_time */ int64_t tstamp{}; /**< Time-stamp, as per g_get_monotonic_time */
}; };
/// All contacts class ContactsCache {
class Contacts {
public: public:
/** /**
* Construct a new contacts objects * Construct a new ContactsCache object
* *
* @param serialized serialized contacts * @param serialized serialized contacts
* @param personal personal addresses * @param personal personal addresses
*/ */
Contacts(const std::string& serialized = "", const StringVec& personal = {}); ContactsCache(const std::string& serialized = "", const StringVec& personal = {});
/** /**
* DTOR * DTOR
* *
*/ */
~Contacts(); ~ContactsCache();
/** /**
* Add a contact * Add a contact
@ -166,4 +165,4 @@ private:
} // namespace Mu } // namespace Mu
#endif /* __MU_CONTACTS_HH__ */ #endif /* __MU_CONTACTS_CACHE_HH__ */

View File

@ -42,7 +42,6 @@
#include "index/mu-indexer.hh" #include "index/mu-indexer.hh"
#include "mu-store.hh" #include "mu-store.hh"
#include "mu-msg-part.hh" #include "mu-msg-part.hh"
#include "mu-contacts.hh"
#include "utils/mu-str.h" #include "utils/mu-str.h"
#include "utils/mu-utils.hh" #include "utils/mu-utils.hh"
@ -523,7 +522,7 @@ Server::Private::contacts_handler(const Parameters& params)
auto rank{0}; auto rank{0};
Sexp::List contacts; Sexp::List contacts;
store().contacts().for_each([&](const ContactInfo& ci) { store().contacts_cache().for_each([&](const ContactInfo& ci) {
rank++; rank++;
/* since the last time we got some contacts */ /* since the last time we got some contacts */

View File

@ -118,7 +118,7 @@ struct Store::Private {
: 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)},
properties_{make_properties(path)}, contacts_{db().get_metadata(ContactsKey), properties_{make_properties(path)}, contacts_cache_{db().get_metadata(ContactsKey),
properties_.personal_addresses} properties_.personal_addresses}
{ {
} }
@ -129,7 +129,7 @@ struct Store::Private {
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)},
properties_{init_metadata(conf, path, root_maildir, personal_addresses)}, properties_{init_metadata(conf, path, root_maildir, personal_addresses)},
contacts_{"", properties_.personal_addresses} contacts_cache_{"", properties_.personal_addresses}
{ {
} }
@ -138,7 +138,7 @@ struct Store::Private {
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)},
properties_{init_metadata(conf, "", root_maildir, personal_addresses)}, properties_{init_metadata(conf, "", root_maildir, personal_addresses)},
contacts_{"", properties_.personal_addresses} contacts_cache_{"", properties_.personal_addresses}
{ {
} }
@ -210,10 +210,10 @@ struct Store::Private {
return; // not supported or not in transaction return; // not supported or not in transaction
if (force || transaction_size_ >= properties_.batch_size) { if (force || transaction_size_ >= properties_.batch_size) {
if (contacts_.dirty()) { if (contacts_cache_.dirty()) {
xapian_try([&] { xapian_try([&] {
writable_db().set_metadata(ContactsKey, writable_db().set_metadata(ContactsKey,
contacts_.serialize()); contacts_cache_.serialize());
}); });
} }
g_debug("committing transaction (n=%zu,%zu)", g_debug("committing transaction (n=%zu,%zu)",
@ -310,7 +310,7 @@ struct Store::Private {
std::unique_ptr<Xapian::Database> db_; std::unique_ptr<Xapian::Database> db_;
const Store::Properties properties_; const Store::Properties properties_;
Contacts contacts_; ContactsCache contacts_cache_;
std::unique_ptr<Indexer> indexer_; std::unique_ptr<Indexer> indexer_;
size_t transaction_size_{}; size_t transaction_size_{};
@ -365,10 +365,10 @@ Store::properties() const
return priv_->properties_; return priv_->properties_;
} }
const Contacts& const ContactsCache&
Store::contacts() const Store::contacts_cache() const
{ {
return priv_->contacts_; return priv_->contacts_cache_;
} }
const Xapian::Database& const Xapian::Database&
@ -980,7 +980,7 @@ xapian_pfx(const MessageContact& contact)
static void static void
add_contacts_terms_values(Xapian::Document& doc, MuMsg *msg, add_contacts_terms_values(Xapian::Document& doc, MuMsg *msg,
Contacts& contacts_store) ContactsCache& contacts_cache)
{ {
Xapian::TermGenerator termgen; Xapian::TermGenerator termgen;
termgen.set_document(doc); termgen.set_document(doc);
@ -1007,11 +1007,11 @@ add_contacts_terms_values(Xapian::Document& doc, MuMsg *msg,
termgen.index_text_without_positions(contact.email, 1, pfx); termgen.index_text_without_positions(contact.email, 1, pfx);
/* and add to the contact store.*/ /* and add to the contact store.*/
contacts_store.add(ContactInfo{ contacts_cache.add(ContactInfo{
contact.display_name(), contact.display_name(),
contact.email, contact.email,
contact.name, contact.name,
contacts_store.is_personal(contact.email), contacts_cache.is_personal(contact.email),
contact.message_date}); contact.message_date});
} }
} }
@ -1022,7 +1022,7 @@ Store::Private::new_doc_from_message(MuMsg* msg)
Xapian::Document doc; Xapian::Document doc;
MsgDoc docinfo = {&doc, msg, this, 0, NULL}; MsgDoc docinfo = {&doc, msg, this, 0, NULL};
add_contacts_terms_values(doc, msg, contacts_); add_contacts_terms_values(doc, msg, contacts_cache_);
mu_msg_field_foreach((MuMsgFieldForeachFunc)add_terms_values, &docinfo); mu_msg_field_foreach((MuMsgFieldForeachFunc)add_terms_values, &docinfo);
// g_printerr ("\n--%s\n--\n", doc.serialise().c_str()); // g_printerr ("\n--%s\n--\n", doc.serialise().c_str());

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** 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 ** under the terms of the GNU General Public License as published by the
@ -27,7 +27,7 @@
#include <mutex> #include <mutex>
#include <ctime> #include <ctime>
#include "mu-contacts.hh" #include "mu-contacts-cache.hh"
#include <xapian.h> #include <xapian.h>
#include <utils/mu-utils.hh> #include <utils/mu-utils.hh>
@ -111,11 +111,11 @@ public:
*/ */
const Properties& properties() const; const Properties& properties() const;
/** /**
* Get the Contacts object for this store * Get the ContactsCache object for this store
* *
* @return the Contacts object * @return the Contacts object
*/ */
const Contacts& contacts() const; const ContactsCache& contacts_cache() const;
/** /**
* Get the underlying Xapian database for this store. * Get the underlying Xapian database for this store.

View File

@ -55,9 +55,9 @@ test('test_threads',
install: false, install: false,
cpp_args: ['-DBUILD_TESTS'], cpp_args: ['-DBUILD_TESTS'],
dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep])) dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
test('test_contacts', test('test_contacts-cache',
executable('test-contacts', executable('test-contacts',
'../mu-contacts.cc', '../mu-contacts-cache.cc',
install: false, install: false,
cpp_args: ['-DBUILD_TESTS'], cpp_args: ['-DBUILD_TESTS'],
dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep])) dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
@ -67,7 +67,8 @@ test('test_message_contact',
'../mu-message-contact.cc', '../mu-message-contact.cc',
install: false, install: false,
cpp_args: ['-DBUILD_TESTS'], cpp_args: ['-DBUILD_TESTS'],
dependencies: [glib_dep, gmime_dep, lib_mu_dep, lib_test_mu_common_dep])) dependencies: [glib_dep, gmime_dep, lib_mu_dep,
lib_test_mu_common_dep]))
test('test_parser', test('test_parser',
executable('test-parser', executable('test-parser',

View File

@ -26,7 +26,7 @@
#include <ctype.h> #include <ctype.h>
#include "mu-cmd.hh" #include "mu-cmd.hh"
#include "mu-contacts.hh" #include "mu-contacts-cache.hh"
#include "mu-runtime.hh" #include "mu-runtime.hh"
#include "utils/mu-util.h" #include "utils/mu-util.h"
@ -363,7 +363,7 @@ run_cmd_cfind(const Mu::Store& store,
print_header(format); print_header(format);
store.contacts().for_each([&](const auto& ci) { each_contact(ci, ecdata); }); store.contacts_cache().for_each([&](const auto& ci) { each_contact(ci, ecdata); });
g_hash_table_unref(ecdata.nicks); g_hash_table_unref(ecdata.nicks);

View File

@ -32,7 +32,7 @@
#include "mu-msg-part.hh" #include "mu-msg-part.hh"
#include "mu-cmd.hh" #include "mu-cmd.hh"
#include "mu-maildir.hh" #include "mu-maildir.hh"
#include "mu-contacts.hh" #include "mu-contacts-cache.hh"
#include "mu-runtime.hh" #include "mu-runtime.hh"
#include "mu-message-flags.hh" #include "mu-message-flags.hh"