* update database up-to-date and/or empty checks

This commit is contained in:
Dirk-Jan C. Binnema 2010-01-31 12:14:25 +02:00
parent 143f8c007d
commit 12dc28e3e9
3 changed files with 43 additions and 9 deletions

View File

@ -137,6 +137,11 @@ mu_query_xapian_new (const char* xpath)
return NULL;
}
if (mu_util_xapian_db_is_empty (xpath)) {
g_warning ("database %s is empty; nothing to do", xpath);
return NULL;
}
if (!mu_util_xapian_db_version_up_to_date (xpath)) {
g_warning ("%s is not up-to-date, needs a full update",
xpath);

View File

@ -31,18 +31,17 @@ char*
mu_util_xapian_db_version (const gchar *xpath)
{
g_return_val_if_fail (xpath, NULL);
if (!access(xpath, F_OK) == 0) {
g_warning ("cannot access %s: %s", xpath, strerror(errno));
return NULL;
}
try {
if (!access(xpath, F_OK) == 0) {
g_warning ("cannot access %s: %s",
xpath, strerror(errno));
return NULL;
}
try {
Xapian::Database db (xpath);
const std::string version
(db.get_metadata (MU_XAPIAN_VERSION_KEY));
MU_WRITE_LOG ("database version: '%s', expected '%s'",
version.empty() ? "<none>" : version.c_str(),
MU_XAPIAN_DB_VERSION);
@ -62,7 +61,7 @@ mu_util_xapian_db_version_up_to_date (const gchar *xpath)
gboolean uptodate;
g_return_val_if_fail (xpath, FALSE);
version = mu_util_xapian_db_version (xpath);
if (!version)
return FALSE;
@ -74,6 +73,26 @@ mu_util_xapian_db_version_up_to_date (const gchar *xpath)
}
gboolean
mu_util_xapian_db_is_empty (const gchar* xpath)
{
g_return_val_if_fail (xpath, TRUE);
/* it's 'empty' (non-existant) */
if (access(xpath, F_OK) != 0 && errno == ENOENT)
return TRUE;
try {
Xapian::Database db (xpath);
return db.get_doccount() == 0 ? TRUE : FALSE;
} MU_XAPIAN_CATCH_BLOCK;
return FALSE;
}
gboolean
mu_util_xapian_clear_database (const gchar *xpath)
{

View File

@ -38,6 +38,16 @@ G_BEGIN_DECLS
gchar* mu_util_xapian_db_version (const gchar *xpath) G_GNUC_WARN_UNUSED_RESULT;
/**
* check whether the database is empty (contains 0 documents); in
* addition, a non-existing database is considered 'empty' too
*
* @param xpath path to the xapian database
*
* @return TRUE if the database is empty, FALSE otherwise
*/
gboolean mu_util_xapian_db_is_empty (const gchar *xpath);
/**
* check if the 'schema' of the current database is up-to-date
*