lib: replace use of static_cast for writable databases

static_cast is a bit tricky to understand and error prone, so add a
second pointer to (potentially the same) Xapian database object that
we know has the right subclass.
This commit is contained in:
David Bremner 2020-07-26 20:31:36 -03:00
parent d7d4c729ab
commit a09293793f
6 changed files with 27 additions and 41 deletions

View file

@ -43,15 +43,12 @@ _notmuch_database_generate_thread_id (notmuch_database_t *notmuch)
/* 16 bytes (+ terminator) for hexadecimal representation of /* 16 bytes (+ terminator) for hexadecimal representation of
* a 64-bit integer. */ * a 64-bit integer. */
static char thread_id[17]; static char thread_id[17];
Xapian::WritableDatabase *db;
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
notmuch->last_thread_id++; notmuch->last_thread_id++;
sprintf (thread_id, "%016" PRIx64, notmuch->last_thread_id); sprintf (thread_id, "%016" PRIx64, notmuch->last_thread_id);
db->set_metadata ("last_thread_id", thread_id); notmuch->writable_xapian_db->set_metadata ("last_thread_id", thread_id);
return thread_id; return thread_id;
} }
@ -161,7 +158,7 @@ _resolve_message_id_to_thread_id_old (notmuch_database_t *notmuch,
* can return the thread ID stored in the metadata. Otherwise, we * can return the thread ID stored in the metadata. Otherwise, we
* generate a new thread ID and store it there. * generate a new thread ID and store it there.
*/ */
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db); db = notmuch->writable_xapian_db;
metadata_key = _get_metadata_thread_id_key (ctx, message_id); metadata_key = _get_metadata_thread_id_key (ctx, message_id);
thread_id_string = notmuch->xapian_db->get_metadata (metadata_key); thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
@ -370,13 +367,9 @@ _consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch,
if (stored_id.empty ()) { if (stored_id.empty ()) {
return NULL; return NULL;
} else { } else {
Xapian::WritableDatabase *db;
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
/* Clear the metadata for this message ID. We don't need it /* Clear the metadata for this message ID. We don't need it
* anymore. */ * anymore. */
db->set_metadata (metadata_key, ""); notmuch->writable_xapian_db->set_metadata (metadata_key, "");
return talloc_strdup (ctx, stored_id.c_str ()); return talloc_strdup (ctx, stored_id.c_str ());
} }

View file

@ -45,15 +45,13 @@ notmuch_database_set_config (notmuch_database_t *notmuch,
const char *value) const char *value)
{ {
notmuch_status_t status; notmuch_status_t status;
Xapian::WritableDatabase *db;
status = _notmuch_database_ensure_writable (notmuch); status = _notmuch_database_ensure_writable (notmuch);
if (status) if (status)
return status; return status;
try { try {
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db); notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
db->set_metadata (CONFIG_PREFIX + key, value);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
notmuch->exception_reported = true; notmuch->exception_reported = true;

View file

@ -189,11 +189,11 @@ struct _notmuch_database {
char *path; char *path;
notmuch_database_mode_t mode;
int atomic_nesting; int atomic_nesting;
/* true if changes have been made in this atomic section */ /* true if changes have been made in this atomic section */
bool atomic_dirty; bool atomic_dirty;
Xapian::Database *xapian_db; Xapian::Database *xapian_db;
Xapian::WritableDatabase *writable_xapian_db;
bool open; bool open;
/* Bit mask of features used by this database. This is a /* Bit mask of features used by this database. This is a
* bitwise-OR of NOTMUCH_FEATURE_* values (above). */ * bitwise-OR of NOTMUCH_FEATURE_* values (above). */

View file

@ -72,7 +72,10 @@ _log_xapian_exception (const char *where, notmuch_database_t *notmuch, const Xa
notmuch_database_mode_t notmuch_database_mode_t
_notmuch_database_mode (notmuch_database_t *notmuch) _notmuch_database_mode (notmuch_database_t *notmuch)
{ {
return notmuch->mode; if (notmuch->writable_xapian_db)
return NOTMUCH_DATABASE_MODE_READ_WRITE;
else
return NOTMUCH_DATABASE_MODE_READ_ONLY;
} }
/* Here's the current schema for our database (for NOTMUCH_DATABASE_VERSION): /* Here's the current schema for our database (for NOTMUCH_DATABASE_VERSION):
@ -976,7 +979,7 @@ notmuch_database_open_verbose (const char *path,
strip_trailing (notmuch->path, '/'); strip_trailing (notmuch->path, '/');
notmuch->mode = mode; notmuch->writable_xapian_db = NULL;
notmuch->atomic_nesting = 0; notmuch->atomic_nesting = 0;
notmuch->view = 1; notmuch->view = 1;
try { try {
@ -984,8 +987,9 @@ notmuch_database_open_verbose (const char *path,
string last_mod; string last_mod;
if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) { if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path, notmuch->writable_xapian_db = new Xapian::WritableDatabase (xapian_path,
DB_ACTION); DB_ACTION);
notmuch->xapian_db = notmuch->writable_xapian_db;
} else { } else {
notmuch->xapian_db = new Xapian::Database (xapian_path); notmuch->xapian_db = new Xapian::Database (xapian_path);
} }
@ -1115,8 +1119,7 @@ notmuch_database_close (notmuch_database_t *notmuch)
* cancel any outstanding transaction before closing. */ * cancel any outstanding transaction before closing. */
if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_WRITE && if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_WRITE &&
notmuch->atomic_nesting) notmuch->atomic_nesting)
(static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db)) notmuch->writable_xapian_db->cancel_transaction ();
->cancel_transaction ();
/* Close the database. This implicitly flushes /* Close the database. This implicitly flushes
* outstanding changes. */ * outstanding changes. */
@ -1454,7 +1457,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
if (status) if (status)
return status; return status;
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db); db = notmuch->writable_xapian_db;
target_features = notmuch->features | NOTMUCH_FEATURES_CURRENT; target_features = notmuch->features | NOTMUCH_FEATURES_CURRENT;
new_features = NOTMUCH_FEATURES_CURRENT & ~notmuch->features; new_features = NOTMUCH_FEATURES_CURRENT & ~notmuch->features;
@ -1711,7 +1714,7 @@ notmuch_database_begin_atomic (notmuch_database_t *notmuch)
return NOTMUCH_STATUS_UPGRADE_REQUIRED; return NOTMUCH_STATUS_UPGRADE_REQUIRED;
try { try {
(static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->begin_transaction (false); notmuch->writable_xapian_db->begin_transaction (false);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
_notmuch_database_log (notmuch, "A Xapian exception occurred beginning transaction: %s.\n", _notmuch_database_log (notmuch, "A Xapian exception occurred beginning transaction: %s.\n",
error.get_msg ().c_str ()); error.get_msg ().c_str ());
@ -1736,7 +1739,7 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch)
notmuch->atomic_nesting > 1) notmuch->atomic_nesting > 1)
goto DONE; goto DONE;
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db); db = notmuch->writable_xapian_db;
try { try {
db->commit_transaction (); db->commit_transaction ();

View file

@ -99,7 +99,6 @@ _notmuch_directory_find_or_create (notmuch_database_t *notmuch,
notmuch_find_flags_t flags, notmuch_find_flags_t flags,
notmuch_status_t *status_ret) notmuch_status_t *status_ret)
{ {
Xapian::WritableDatabase *db;
notmuch_directory_t *directory; notmuch_directory_t *directory;
notmuch_private_status_t private_status; notmuch_private_status_t private_status;
const char *db_path; const char *db_path;
@ -176,10 +175,10 @@ _notmuch_directory_find_or_create (notmuch_database_t *notmuch,
directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP, directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
Xapian::sortable_serialise (0)); Xapian::sortable_serialise (0));
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
directory->document_id = _notmuch_database_generate_doc_id (notmuch); directory->document_id = _notmuch_database_generate_doc_id (notmuch);
db->replace_document (directory->document_id, directory->doc); directory->notmuch->
writable_xapian_db
-> replace_document (directory->document_id, directory->doc);
talloc_free (local); talloc_free (local);
} }
@ -213,20 +212,18 @@ notmuch_directory_set_mtime (notmuch_directory_t *directory,
time_t mtime) time_t mtime)
{ {
notmuch_database_t *notmuch = directory->notmuch; notmuch_database_t *notmuch = directory->notmuch;
Xapian::WritableDatabase *db;
notmuch_status_t status; notmuch_status_t status;
status = _notmuch_database_ensure_writable (notmuch); status = _notmuch_database_ensure_writable (notmuch);
if (status) if (status)
return status; return status;
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
try { try {
directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP, directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
Xapian::sortable_serialise (mtime)); Xapian::sortable_serialise (mtime));
db->replace_document (directory->document_id, directory->doc); directory->notmuch
->writable_xapian_db->replace_document (directory->document_id, directory->doc);
directory->mtime = mtime; directory->mtime = mtime;
@ -288,15 +285,14 @@ notmuch_status_t
notmuch_directory_delete (notmuch_directory_t *directory) notmuch_directory_delete (notmuch_directory_t *directory)
{ {
notmuch_status_t status; notmuch_status_t status;
Xapian::WritableDatabase *db;
status = _notmuch_database_ensure_writable (directory->notmuch); status = _notmuch_database_ensure_writable (directory->notmuch);
if (status) if (status)
return status; return status;
try { try {
db = static_cast <Xapian::WritableDatabase *> (directory->notmuch->xapian_db); directory->notmuch->
db->delete_document (directory->document_id); writable_xapian_db->delete_document (directory->document_id);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
_notmuch_database_log (directory->notmuch, _notmuch_database_log (directory->notmuch,
"A Xapian exception occurred deleting directory entry: %s.\n", "A Xapian exception occurred deleting directory entry: %s.\n",

View file

@ -1322,8 +1322,6 @@ _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
void void
_notmuch_message_sync (notmuch_message_t *message) _notmuch_message_sync (notmuch_message_t *message)
{ {
Xapian::WritableDatabase *db;
if (_notmuch_database_mode (message->notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY) if (_notmuch_database_mode (message->notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
return; return;
@ -1342,8 +1340,8 @@ _notmuch_message_sync (notmuch_message_t *message)
_notmuch_database_new_revision ( _notmuch_database_new_revision (
message->notmuch))); message->notmuch)));
db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db); message->notmuch->writable_xapian_db->
db->replace_document (message->doc_id, message->doc); replace_document (message->doc_id, message->doc);
message->modified = false; message->modified = false;
} }
@ -1353,7 +1351,6 @@ notmuch_status_t
_notmuch_message_delete (notmuch_message_t *message) _notmuch_message_delete (notmuch_message_t *message)
{ {
notmuch_status_t status; notmuch_status_t status;
Xapian::WritableDatabase *db;
const char *mid, *tid, *query_string; const char *mid, *tid, *query_string;
notmuch_message_t *ghost; notmuch_message_t *ghost;
notmuch_private_status_t private_status; notmuch_private_status_t private_status;
@ -1370,8 +1367,7 @@ _notmuch_message_delete (notmuch_message_t *message)
if (status) if (status)
return status; return status;
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db); message->notmuch->writable_xapian_db->delete_document (message->doc_id);
db->delete_document (message->doc_id);
/* if this was a ghost to begin with, we are done */ /* if this was a ghost to begin with, we are done */
private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost); private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);