store: rework to allow for 'init'

Rework the error handling / exception for read-only / writable and new
database.
This commit is contained in:
Dirk-Jan C. Binnema
2020-02-06 20:24:38 +02:00
parent f51846eefc
commit aa10da0a63
5 changed files with 35 additions and 74 deletions

View File

@ -280,8 +280,7 @@ check_path (const char *path)
g_return_val_if_fail (path, FALSE);
if (!g_path_is_absolute (path)) {
g_warning ("%s: not an absolute path: %s",
__func__, path);
g_warning ("%s: not an absolute path: '%s'", __func__, path);
return FALSE;
}

View File

@ -25,6 +25,7 @@
#include <xapian.h>
#include <unordered_map>
#include <atomic>
#include <iostream>
#include "mu-store.hh"
#include "utils/mu-str.h"
@ -206,22 +207,17 @@ get_uid_term (const char* path)
Store::Store (const std::string& path, bool readonly):
priv_{std::make_unique<Private>(path, readonly)}
{
if (ExpectedSchemaVersion == schema_version())
return; // All is good; nothing further to do
g_warning ("expected schema-version %s, but got %s",
ExpectedSchemaVersion, schema_version().c_str());
if (readonly) // this requires user-action.
if (ExpectedSchemaVersion != schema_version())
throw Mu::Error(Error::Code::SchemaMismatch,
"database needs reindexing");
"expected schema-version %s, but got %s",
ExpectedSchemaVersion, schema_version().c_str());
g_debug ("upgrading database");
const auto addresses{personal_addresses()};
const auto root_mdir{root_maildir()};
// g_debug ("upgrading database to schema-version %s", ExpectedSchemaVersion);
// const auto addresses{personal_addresses()};
// const auto root_mdir{root_maildir()};
priv_.reset();
priv_ = std::make_unique<Private> (path, root_mdir, addresses);
// priv_.reset();
// priv_ = std::make_unique<Private> (path, root_mdir, addresses);
}
Store::Store (const std::string& path, const std::string& maildir,
@ -479,12 +475,7 @@ mu_store_new_readable (const char* xpath, GError **err)
return reinterpret_cast<MuStore*>(new Store (xpath));
} catch (const Mu::Error& me) {
if (me.code() == Mu::Error::Code::SchemaMismatch) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_SCHEMA_MISMATCH,
"read-only database @ %s schema version does not match",
xpath);
return NULL;
}
g_warning ("failed to open database: %s", me.what());
} catch (const Xapian::Error& dbe) {
g_warning ("failed to open database @ %s: %s", xpath,
dbe.get_error_string() ? dbe.get_error_string() : "something went wrong");
@ -503,29 +494,31 @@ mu_store_new_writable (const char* xpath, GError **err)
g_debug ("opening database at %s (writable)", xpath);
try {
try {
return reinterpret_cast<MuStore*>(new Store (xpath, false/*!readonly*/));
} catch (const Mu::Error& me) {
if (me.code() == Mu::Error::Code::SchemaMismatch)
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_NEEDS_REINDEX,
"read/write database @ %s needs (re)indexing", xpath);
else
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN,
"error opening database @ %s: %s", xpath, me.what());
} catch (const Xapian::DatabaseOpeningError& dbe) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_NEEDS_REINDEX,
"failed to open database @ %s", xpath);
if (me.code() == Mu::Error::Code::SchemaMismatch) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_SCHEMA_MISMATCH,
"%s", me.what());
return NULL;
}
} catch (const Xapian::DatabaseLockError& dle) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_CANNOT_GET_WRITELOCK,
"database @ %s is write-locked already", xpath);
} catch (...) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN,
"error opening database @ %s", xpath);
"database @ %s is write-locked", xpath);
return NULL;
} catch (const Xapian::Error& dbe) {
g_warning ("failed to open database @ %s: %s", xpath,
dbe.get_error_string() ? dbe.get_error_string() : "something went wrong");
}
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_CANNOT_OPEN,
"failed to open database @ %s", xpath);
return NULL;
}
MuStore*
mu_store_new_create (const char* xpath, const char *root_maildir,
const char **personal_addresses, GError **err)

View File

@ -564,6 +564,14 @@ gboolean mu_store_database_is_locked (const gchar *xpath);
MuMsg* mu_store_get_msg (const MuStore *self, unsigned docid, GError **err)
G_GNUC_WARN_UNUSED_RESULT;
/**
* Print some information about the store
*
* @param store a store
* @param nocolor whether to _not_ show color
*/
void mu_store_print_info (const MuStore *store, gboolean nocolor);
G_END_DECLS

View File

@ -444,34 +444,6 @@ Mu::size_to_string (const std::string& val, bool is_first)
return str;
}
std::string
Mu::quoted (const std::string& str)
{
std::string res{"\""};
for (auto&& c : str) {
if (c == '\\' || c == '\"')
res += '\\';
res += c;
}
return res + '"';
}
// std::string
// Mu::quoted (const char* str)
// {
// if (!str)
// return str;
// char *s{g_strescape(str, NULL)};
// auto res = format("\"%s\"", s ? s : "");
// g_free(s);
// return res;
// }
void
Mu::assert_equal(const std::string& s1, const std::string& s2)
{

View File

@ -94,17 +94,6 @@ std::string format (const char *frm, ...) __attribute__((format(printf, 1, 2)));
std::string format (const char *frm, va_list args) __attribute__((format(printf, 1, 0)));
/**
* Quote a string -- put in "" and escape any special characters by putting '\'
* in front of them.
*
* @param str
*
* @return
*/
std::string quoted (const std::string& str);
/**
* Convert an date to the corresponding time expressed as a string with a
* 10-digit time_t