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.
This commit is contained in:
djcb
2016-02-21 19:48:21 +02:00
parent 2b0d75b295
commit 775eb3f715
5 changed files with 41 additions and 26 deletions

View File

@ -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 <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2016 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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)

View File

@ -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__*/

View File

@ -1,6 +1,6 @@
/* -*-mode: c++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8-*- */
/*
** Copyright (C) 2011-2012 <djcb@djcbsoftware.nl>
** Copyright (C) 2011-2016 <djcb@djcbsoftware.nl>
**
** 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);

View File

@ -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 <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2016 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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;
}
}
}

View File

@ -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
*/