From 775eb3f7159b35dc338c3b2c50cf6cbad3b31f1b Mon Sep 17 00:00:00 2001 From: djcb Date: Sun, 21 Feb 2016 19:48:21 +0200 Subject: [PATCH] mu: write contacts-cache a bit sooner Write the changes to the cache file after any indexing operation, so `mu cfind` gets new contacts a bit sooner. --- lib/mu-contacts.c | 36 +++++++++++++++++------------------- lib/mu-contacts.h | 11 ++++++++++- lib/mu-store-priv.hh | 4 +++- lib/mu-store-write.cc | 13 +++++++++---- lib/mu-store.h | 3 ++- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/lib/mu-contacts.c b/lib/mu-contacts.c index b0d8e8b9..48646ac3 100644 --- a/lib/mu-contacts.c +++ b/lib/mu-contacts.c @@ -1,6 +1,6 @@ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -** Copyright (C) 2008-2013 Dirk-Jan C. Binnema +** Copyright (C) 2008-2016 Dirk-Jan C. Binnema ** ** 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 @@ -280,8 +280,6 @@ clear_str (char* str) } - - gboolean mu_contacts_add (MuContacts *self, const char *addr, const char *name, gboolean personal, time_t tstamp) @@ -304,7 +302,7 @@ mu_contacts_add (MuContacts *self, const char *addr, const char *name, tstamp, 1); g_hash_table_insert (self->_hash, g_strdup(group), cinfo); } else { - /* if the contact is ever user in a personal way, it's + /* if the contact is ever used in a personal way, it's * personal */ if (personal) cinfo->_personal = TRUE; @@ -421,21 +419,23 @@ each_keyval (const char *group, ContactInfo *cinfo, MuContacts *self) (int)cinfo->_freq); } -static gboolean -serialize_cache (MuContacts *self) +gboolean +mu_contacts_serialize (MuContacts *self) { - gchar *data; - gsize len; - gboolean rv; + gchar *data; + gsize len; + gboolean rv; + + g_return_val_if_fail (self, FALSE); g_hash_table_foreach (self->_hash, (GHFunc)each_keyval, self); /* Note: err arg is unused */ - data = g_key_file_to_data (self->_ccache, &len, NULL); + data = g_key_file_to_data (self->_ccache, &len, NULL); if (len) { - GError *err; + GError *err; err = NULL; - rv = g_file_set_contents (self->_path, data, len, &err); + rv = g_file_set_contents (self->_path, data, len, &err); if (!rv) { g_warning ("failed to serialize cache to %s: %s", self->_path, err->message); @@ -454,12 +454,11 @@ mu_contacts_destroy (MuContacts *self) if (!self) return; - if (self->_ccache && self->_dirty) { - serialize_cache (self); + if (self->_ccache && self->_dirty && + mu_contacts_serialize (self)) MU_WRITE_LOG("serialized contacts cache %s", self->_path); - } - + if (self->_ccache) g_key_file_free (self->_ccache); @@ -471,9 +470,8 @@ mu_contacts_destroy (MuContacts *self) g_free (self); } - -/* note, we will *own* the name, email we get, and we'll free them in - * the end... */ +/* note, we will *own* the name, email we get, and we'll free them in the + * end... */ static ContactInfo * contact_info_new (char *email, char *name, gboolean personal, time_t tstamp, unsigned freq) diff --git a/lib/mu-contacts.h b/lib/mu-contacts.h index ba282afb..83090b69 100644 --- a/lib/mu-contacts.h +++ b/lib/mu-contacts.h @@ -111,7 +111,7 @@ typedef void (*MuContactsForeachFunc) (const char *email, const char *name, * call a function for either each contact, or each contact satisfying * a regular expression, * - * @param contacts contacts object + * @param self contacts object * @param func callback function to be called for each * @param user_data user data to pass to the callback * @param pattern a regular expression which matches either the e-mail @@ -124,6 +124,15 @@ typedef void (*MuContactsForeachFunc) (const char *email, const char *name, gboolean mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func, gpointer user_data, const char* pattern, size_t *num); +/** + * serialize the contacts to the contacts cache file + * + * @param self contacts object + * + * @return TRUE if the function succeeded, FALSE otherwise + * */ +gboolean mu_contacts_serialize (MuContacts *self); + G_END_DECLS #endif /*__MU_CONTACTS_H__*/ diff --git a/lib/mu-store-priv.hh b/lib/mu-store-priv.hh index d9e36e68..b0a96916 100644 --- a/lib/mu-store-priv.hh +++ b/lib/mu-store-priv.hh @@ -1,6 +1,6 @@ /* -*-mode: c++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8-*- */ /* -** Copyright (C) 2011-2012 +** Copyright (C) 2011-2016 ** ** 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 @@ -148,6 +148,8 @@ public: g_warning ("ref count != 0"); mu_contacts_destroy (_contacts); + _contacts = NULL; + if (!_read_only) mu_store_flush (this); diff --git a/lib/mu-store-write.cc b/lib/mu-store-write.cc index 0a3bfbfe..85731775 100644 --- a/lib/mu-store-write.cc +++ b/lib/mu-store-write.cc @@ -1,6 +1,6 @@ /* -*-mode: c++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8-*- */ /* -** Copyright (C) 2008-2013 Dirk-Jan C. Binnema +** Copyright (C) 2008-2016 Dirk-Jan C. Binnema ** ** 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 @@ -195,8 +195,11 @@ mu_store_flush (MuStore *store) if (store->in_transaction()) store->commit_transaction (); store->db_writable()->commit (); - + } MU_XAPIAN_CATCH_BLOCK; + + if (store->contacts()) + mu_contacts_serialize (store->contacts()); } @@ -669,9 +672,11 @@ each_contact_check_if_personal (MuMsgContact *contact, MsgDoc *msgdoc) return; for (cur = msgdoc->_my_addresses; cur; cur = g_slist_next (cur)) { - if (g_ascii_strcasecmp (contact->address, - (const char*)cur->data) == 0) + if (g_ascii_strcasecmp ( + contact->address, (const char*)cur->data) == 0) { msgdoc->_personal = TRUE; + break; + } } } diff --git a/lib/mu-store.h b/lib/mu-store.h index 21022822..e6a40a5f 100644 --- a/lib/mu-store.h +++ b/lib/mu-store.h @@ -168,7 +168,8 @@ const char* mu_store_version (const MuStore *store); /** - * try to flush/commit all outstanding work + * try to flush/commit all outstanding work to the database and the contacts + * cache. * * @param store a valid xapian store */