From a4bdb311ec6ed3901c7aab6ffbd65980a689d48c Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 23 Jan 2010 21:57:57 +0200 Subject: [PATCH] * introduce --empty and --autoupgrade, and document them --- man/mu.1 | 21 +++++++++++-- src/mu-cmd.c | 68 ++++++++++++++++++++++++------------------ src/mu-config.c | 4 +++ src/mu-config.h | 3 ++ src/mu-store-xapian.cc | 1 + src/mu-util-xapian.cc | 30 ++++++++++++++++++- src/mu-util-xapian.h | 14 +++++++++ 7 files changed, 109 insertions(+), 32 deletions(-) diff --git a/man/mu.1 b/man/mu.1 index 8d1c7067..00394215 100644 --- a/man/mu.1 +++ b/man/mu.1 @@ -36,8 +36,7 @@ information in a database .TP \fBfind\fR for finding messages in your database, using certain search parameters (see -below for details). You can use \fBquery\fR and \fBsearch\fR as synonyms for -\fBfind\fR. +below for details). .TP \fBmkdir\fR @@ -141,6 +140,24 @@ re-index all mails, even ones that are already in the database. \fB\-u\fR, \fB\-\-nocleanup\fR disables the database cleanup that \fBmu\fR does by default after indexing. +.TP +\fB\-y\fR, \fB\-\-empty\fR +clear all messages from the database before +indexing. This is effectively the same as removing the database. The +difference with \fB\-\-reindex\fR is that \fB\-\-empty\fR guarantees that +after the indexing has finished, there are no 'old' messages in the database +anymore, which is not true with \fB\-\-reindex\fR when indexing only a part of +messages (using \fB\-\-maildir\fR). For this reason, it's necessary to run +\fBmu index \-\-empty\fR when there is an upgrade in the database +format. \fBmu index\fR will warn you about this. + +.TP +\fB\-u\fR, \fB\-\-autoupgrade\fR +automatically use \fB\-y\fR, \fB\-\-empty\fR when \fBmu\fR notices that the +database version is not up-to-date. This option is for use in cron scripts +etc., so they won't require any user interaction, even when mu introduces a +new database vesion. + .TP .B NOTE: diff --git a/src/mu-cmd.c b/src/mu-cmd.c index 9ecfbb54..fa19cdd3 100644 --- a/src/mu-cmd.c +++ b/src/mu-cmd.c @@ -44,16 +44,13 @@ cmd_from_string (const char* cmd) return MU_CMD_INDEX; /* support some synonyms... */ - if ((strcmp (cmd, "query") == 0) || - (strcmp (cmd, "find") == 0) || - (strcmp (cmd, "search") == 0)) + if (strcmp (cmd, "find") == 0) return MU_CMD_FIND; if (strcmp (cmd, "cleanup") == 0) return MU_CMD_CLEANUP; - if ((strcmp (cmd, "mkmdir") == 0) || - (strcmp (cmd, "mkdir") == 0)) + if (strcmp (cmd, "mkdir") == 0) return MU_CMD_MKDIR; /* if ((strcmp (cmd, "help") == 0) || */ @@ -63,26 +60,12 @@ cmd_from_string (const char* cmd) return MU_CMD_UNKNOWN; } -static gboolean -database_needs_reindex (MuConfigOptions *opts) +static void +update_warning (void) { - gchar *version; - gboolean reindex; - - version = mu_util_xapian_db_version (opts->xpath); - if (!version || strcmp (version, MU_XAPIAN_DB_VERSION) != 0) { - g_warning ("expected database version %s, " - "but current version is %s", - MU_XAPIAN_DB_VERSION, - version ? version : ""); - g_message ("please run `mu index --reindex' for your full " - "maildir"); - reindex = TRUE; - } else - reindex = FALSE; - - g_free (version); - return reindex; + g_warning ("the database needs to be updated to version %s", + MU_XAPIAN_DB_VERSION); + g_message ("please run 'mu index --empty' (see the manpage)"); } @@ -338,9 +321,11 @@ cmd_find (MuConfigOptions *opts) if (!query_params_valid (opts)) return FALSE; - - if (database_needs_reindex(opts)) + + if (mu_util_xapian_db_version_up_to_date (opts->xpath)) { + update_warning (); return FALSE; + } /* first param is 'query', search params are after that */ params = (const gchar**)&opts->params[1]; @@ -408,6 +393,31 @@ index_msg_cb (MuIndexStats* stats, void *user_data) } +static gboolean +database_version_check_and_update (MuConfigOptions *opts) +{ + /* we empty the database before doing anything */ + if (opts->empty) { + opts->reindex = TRUE; + g_message ("Emptying database %s", opts->xpath); + return mu_util_xapian_clear_database (opts->xpath); + } + + if (mu_util_xapian_db_version_up_to_date (opts->xpath)) + return TRUE; /* ok, nothing to do */ + + /* ok, database is not up to date */ + if (opts->autoupgrade) { + opts->reindex = TRUE; + g_message ("Auto-upgrade: clearing old database first"); + return mu_util_xapian_clear_database (opts->xpath); + } + + update_warning (); + return FALSE; +} + + static gboolean cmd_index (MuConfigOptions *opts) { @@ -415,10 +425,10 @@ cmd_index (MuConfigOptions *opts) if (!check_index_params (opts)) return FALSE; - - if (!opts->reindex && database_needs_reindex(opts)) - return FALSE; + if (!database_version_check_and_update(opts)) + return FALSE; + mu_msg_gmime_init (); { MuIndex *midx; diff --git a/src/mu-config.c b/src/mu-config.c index 150570f9..e2e05060 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -65,6 +65,10 @@ config_options_group_index (MuConfigOptions *opts) "top of the maildir", NULL}, {"reindex", 'r', 0, G_OPTION_ARG_NONE, &opts->reindex, "index already indexed messages too", NULL}, + {"empty", 'y', 0, G_OPTION_ARG_NONE, &opts->empty, + "empty the database before indexing"}, + {"autoupgrade", 'u', 0, G_OPTION_ARG_NONE, &opts->autoupgrade, + "automatically upgrade the database with new mu versions"}, {"nocleanup", 'u', 0, G_OPTION_ARG_NONE, &opts->nocleanup, "don't clean up the database after indexing", NULL}, { NULL, 0, 0, 0, NULL, NULL, NULL } diff --git a/src/mu-config.h b/src/mu-config.h index 7ea0f264..995eb66b 100644 --- a/src/mu-config.h +++ b/src/mu-config.h @@ -45,6 +45,9 @@ struct _MuConfigOptions { char *maildir; /* where the mails are */ gboolean nocleanup; /* don't cleanup deleted mails from db */ gboolean reindex; /* re-index existing mails */ + gboolean empty; /* empty the database before indexing */ + gboolean autoupgrade; /* automatically upgrade db + * when needed */ /* options for querying */ gboolean xquery; /* give the Xapian query instead of diff --git a/src/mu-store-xapian.cc b/src/mu-store-xapian.cc index 2fafd55b..7b5905bd 100644 --- a/src/mu-store-xapian.cc +++ b/src/mu-store-xapian.cc @@ -73,6 +73,7 @@ mu_store_xapian_new (const char* xpath) } + char* mu_store_xapian_version (MuStoreXapian *store) { diff --git a/src/mu-util-xapian.cc b/src/mu-util-xapian.cc index cfc12db7..306a12eb 100644 --- a/src/mu-util-xapian.cc +++ b/src/mu-util-xapian.cc @@ -20,14 +20,25 @@ #include "config.h" #include + #include +#include + #include "mu-util.h" #include "mu-util-xapian.h" char* mu_util_xapian_db_version (const gchar *xpath) { + g_return_val_if_fail (xpath, NULL); + try { + if (!access(xpath, F_OK) == 0) { + g_warning ("cannot access %s: %s", + xpath, strerror(errno)); + return NULL; + } + Xapian::Database db (xpath); const std::string version (db.get_metadata (MU_XAPIAN_VERSION_KEY)); @@ -45,6 +56,8 @@ mu_util_xapian_db_version_up_to_date (const gchar *xpath) { gchar *version; gboolean uptodate; + + g_return_val_if_fail (xpath, FALSE); version = mu_util_xapian_db_version (xpath); if (!version) @@ -57,5 +70,20 @@ mu_util_xapian_db_version_up_to_date (const gchar *xpath) } - +gboolean +mu_util_xapian_clear_database (const gchar *xpath) +{ + g_return_val_if_fail (xpath, FALSE); + + try { + Xapian::WritableDatabase db + (xpath, Xapian::DB_CREATE_OR_OVERWRITE); + db.flush (); + + return TRUE; + + } MU_XAPIAN_CATCH_BLOCK; + + return FALSE; +} diff --git a/src/mu-util-xapian.h b/src/mu-util-xapian.h index 91488b05..67066fdc 100644 --- a/src/mu-util-xapian.h +++ b/src/mu-util-xapian.h @@ -47,6 +47,20 @@ char* mu_util_xapian_db_version (const gchar *xpath); */ gboolean mu_util_xapian_db_version_up_to_date (const gchar *xpath); + + +/** + * clear the database, ie., remove all of the contents. This is a + * destructive operation, but the database can be restored be doing a + * full scan of the maildirs. + * + * @param xpath path to the database + * + * @return TRUE if the clearing succeeded, FALSE otherwise. + */ +gboolean mu_util_xapian_clear_database (const gchar *xpath); + + G_END_DECLS #endif /*__MU_UTIL_XAPIAN_H__*/