* introduce --empty and --autoupgrade, and document them

This commit is contained in:
Dirk-Jan C. Binnema 2010-01-23 21:57:57 +02:00
parent 501ce008d3
commit a4bdb311ec
7 changed files with 109 additions and 32 deletions

View File

@ -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:

View File

@ -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 : "<none>");
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;

View File

@ -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 }

View File

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

View File

@ -73,6 +73,7 @@ mu_store_xapian_new (const char* xpath)
}
char*
mu_store_xapian_version (MuStoreXapian *store)
{

View File

@ -20,14 +20,25 @@
#include "config.h"
#include <xapian.h>
#include <cstring>
#include <errno.h>
#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;
}

View File

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