From 12dc28e3e9c7ca87914f0cf154ecdcf314b39abf Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 31 Jan 2010 12:14:25 +0200 Subject: [PATCH] * update database up-to-date and/or empty checks --- src/mu-query-xapian.cc | 5 +++++ src/mu-util-xapian.cc | 37 ++++++++++++++++++++++++++++--------- src/mu-util-xapian.h | 10 ++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/mu-query-xapian.cc b/src/mu-query-xapian.cc index 7c85c670..78d671f0 100644 --- a/src/mu-query-xapian.cc +++ b/src/mu-query-xapian.cc @@ -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); diff --git a/src/mu-util-xapian.cc b/src/mu-util-xapian.cc index dc30a484..f4b93795 100644 --- a/src/mu-util-xapian.cc +++ b/src/mu-util-xapian.cc @@ -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() ? "" : 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) { diff --git a/src/mu-util-xapian.h b/src/mu-util-xapian.h index 7fda1e7e..4dfbbc4e 100644 --- a/src/mu-util-xapian.h +++ b/src/mu-util-xapian.h @@ -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 *