]> git.notmuchmail.org Git - notmuch/commitdiff
lib: replace use of static_cast for writable databases
authorDavid Bremner <david@tethera.net>
Sun, 26 Jul 2020 23:31:36 +0000 (20:31 -0300)
committerDavid Bremner <david@tethera.net>
Tue, 28 Jul 2020 11:47:58 +0000 (08:47 -0300)
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.

lib/add-message.cc
lib/config.cc
lib/database-private.h
lib/database.cc
lib/directory.cc
lib/message.cc

index 9dd4b69771d25ff4f77886c1cfd46cb1602210dd..485debad225239211e9a6f165f24be519080a0ed 100644 (file)
@@ -43,15 +43,12 @@ _notmuch_database_generate_thread_id (notmuch_database_t *notmuch)
     /* 16 bytes (+ terminator) for hexadecimal representation of
      * a 64-bit integer. */
     static char thread_id[17];
     /* 16 bytes (+ terminator) for hexadecimal representation of
      * a 64-bit integer. */
     static char thread_id[17];
-    Xapian::WritableDatabase *db;
-
-    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
 
     notmuch->last_thread_id++;
 
     sprintf (thread_id, "%016" PRIx64, notmuch->last_thread_id);
 
 
     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
      * generate a new thread ID and store it there.
      */
      * can return the thread ID stored in the metadata. Otherwise, we
      * 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);
     thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
 
     metadata_key = _get_metadata_thread_id_key (ctx, message_id);
     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 ()) {
        return NULL;
     } else {
     if (stored_id.empty ()) {
        return NULL;
     } else {
-       Xapian::WritableDatabase *db;
-
-       db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
-
        /* Clear the metadata for this message ID. We don't need it
         * anymore. */
        /* Clear the metadata for this message ID. We don't need it
         * 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 ());
     }
index 200364715a634bb6c469c88a1d4ef2d650820e12..dae0ff0e7f92d3e70681bff9589c38a9a814f6d4 100644 (file)
@@ -45,15 +45,13 @@ notmuch_database_set_config (notmuch_database_t *notmuch,
                             const char *value)
 {
     notmuch_status_t status;
                             const char *value)
 {
     notmuch_status_t status;
-    Xapian::WritableDatabase *db;
 
     status = _notmuch_database_ensure_writable (notmuch);
     if (status)
        return status;
 
     try {
 
     status = _notmuch_database_ensure_writable (notmuch);
     if (status)
        return status;
 
     try {
-       db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
-       db->set_metadata (CONFIG_PREFIX + key, value);
+       notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
     } catch (const Xapian::Error &error) {
        status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
        notmuch->exception_reported = true;
     } catch (const Xapian::Error &error) {
        status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
        notmuch->exception_reported = true;
index d2c253136d146db6cca03f2ed2b220526ab07cfb..041602cdc6a8911b33531cfa8e0dbd72307b8242 100644 (file)
@@ -189,11 +189,11 @@ struct _notmuch_database {
 
     char *path;
 
 
     char *path;
 
-    notmuch_database_mode_t mode;
     int atomic_nesting;
     /* true if changes have been made in this atomic section */
     bool atomic_dirty;
     Xapian::Database *xapian_db;
     int atomic_nesting;
     /* true if changes have been made in this atomic section */
     bool atomic_dirty;
     Xapian::Database *xapian_db;
+    Xapian::WritableDatabase *writable_xapian_db;
     bool open;
     /* Bit mask of features used by this database.  This is a
      * bitwise-OR of NOTMUCH_FEATURE_* values (above). */
     bool open;
     /* Bit mask of features used by this database.  This is a
      * bitwise-OR of NOTMUCH_FEATURE_* values (above). */
index 082782356d851eca7472c68a6c33930594f0d658..7518968599f65fbeed8ac5285e15fb3cfb1cdc2e 100644 (file)
@@ -72,7 +72,10 @@ _log_xapian_exception (const char *where, notmuch_database_t *notmuch,  const Xa
 notmuch_database_mode_t
 _notmuch_database_mode (notmuch_database_t *notmuch)
 {
 notmuch_database_mode_t
 _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->view = 1;
     try {
     notmuch->atomic_nesting = 0;
     notmuch->view = 1;
     try {
@@ -984,8 +987,9 @@ notmuch_database_open_verbose (const char *path,
        string last_mod;
 
        if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
        string last_mod;
 
        if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
-           notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
-                                                              DB_ACTION);
+           notmuch->writable_xapian_db = new Xapian::WritableDatabase (xapian_path,
+                                                                       DB_ACTION);
+           notmuch->xapian_db = notmuch->writable_xapian_db;
        } else {
            notmuch->xapian_db = new Xapian::Database (xapian_path);
        }
        } else {
            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. */
            if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_WRITE &&
                notmuch->atomic_nesting)
             * cancel any outstanding transaction before closing. */
            if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_WRITE &&
                notmuch->atomic_nesting)
-               (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))
-               ->cancel_transaction ();
+               notmuch->writable_xapian_db->cancel_transaction ();
 
            /* Close the database.  This implicitly flushes
             * outstanding changes. */
 
            /* Close the database.  This implicitly flushes
             * outstanding changes. */
@@ -1454,7 +1457,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     if (status)
        return status;
 
     if (status)
        return status;
 
-    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+    db = notmuch->writable_xapian_db;
 
     target_features = notmuch->features | NOTMUCH_FEATURES_CURRENT;
     new_features = NOTMUCH_FEATURES_CURRENT & ~notmuch->features;
 
     target_features = notmuch->features | NOTMUCH_FEATURES_CURRENT;
     new_features = NOTMUCH_FEATURES_CURRENT & ~notmuch->features;
@@ -1711,7 +1714,7 @@ notmuch_database_begin_atomic (notmuch_database_t *notmuch)
        return NOTMUCH_STATUS_UPGRADE_REQUIRED;
 
     try {
        return NOTMUCH_STATUS_UPGRADE_REQUIRED;
 
     try {
-       (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->begin_transaction (false);
+       notmuch->writable_xapian_db->begin_transaction (false);
     } catch (const Xapian::Error &error) {
        _notmuch_database_log (notmuch, "A Xapian exception occurred beginning transaction: %s.\n",
                               error.get_msg ().c_str ());
     } catch (const Xapian::Error &error) {
        _notmuch_database_log (notmuch, "A Xapian exception occurred beginning transaction: %s.\n",
                               error.get_msg ().c_str ());
@@ -1736,7 +1739,7 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch)
        notmuch->atomic_nesting > 1)
        goto DONE;
 
        notmuch->atomic_nesting > 1)
        goto DONE;
 
-    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+    db = notmuch->writable_xapian_db;
     try {
        db->commit_transaction ();
 
     try {
        db->commit_transaction ();
 
index eaba65ab60c7bf656809e23e1c805269d274b1c5..b9c3d77f663fa3e2b4dbfb596e4e3915546375bc 100644 (file)
@@ -99,7 +99,6 @@ _notmuch_directory_find_or_create (notmuch_database_t *notmuch,
                                   notmuch_find_flags_t flags,
                                   notmuch_status_t *status_ret)
 {
                                   notmuch_find_flags_t flags,
                                   notmuch_status_t *status_ret)
 {
-    Xapian::WritableDatabase *db;
     notmuch_directory_t *directory;
     notmuch_private_status_t private_status;
     const char *db_path;
     notmuch_directory_t *directory;
     notmuch_private_status_t private_status;
     const char *db_path;
@@ -176,10 +175,10 @@ _notmuch_directory_find_or_create (notmuch_database_t *notmuch,
            directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
                                      Xapian::sortable_serialise (0));
 
            directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
                                      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)
 {
     notmuch_database_t *notmuch = directory->notmuch;
                             time_t mtime)
 {
     notmuch_database_t *notmuch = directory->notmuch;
-    Xapian::WritableDatabase *db;
     notmuch_status_t status;
 
     status = _notmuch_database_ensure_writable (notmuch);
     if (status)
        return status;
 
     notmuch_status_t status;
 
     status = _notmuch_database_ensure_writable (notmuch);
     if (status)
        return status;
 
-    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
-
     try {
        directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
                                  Xapian::sortable_serialise (mtime));
 
     try {
        directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
                                  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_status_t status;
 notmuch_directory_delete (notmuch_directory_t *directory)
 {
     notmuch_status_t status;
-    Xapian::WritableDatabase *db;
 
     status = _notmuch_database_ensure_writable (directory->notmuch);
     if (status)
        return status;
 
     try {
 
     status = _notmuch_database_ensure_writable (directory->notmuch);
     if (status)
        return status;
 
     try {
-       db = static_cast <Xapian::WritableDatabase *> (directory->notmuch->xapian_db);
-       db->delete_document (directory->document_id);
+       directory->notmuch->
+           writable_xapian_db->delete_document (directory->document_id);
     } catch (const Xapian::Error &error) {
        _notmuch_database_log (directory->notmuch,
                               "A Xapian exception occurred deleting directory entry: %s.\n",
     } catch (const Xapian::Error &error) {
        _notmuch_database_log (directory->notmuch,
                               "A Xapian exception occurred deleting directory entry: %s.\n",
index d23e64abaa06b4c1d3327f4bedd638e52dd5cf64..fca99082a8483cba035fba37f9a1bc1ebaab0b4e 100644 (file)
@@ -1322,8 +1322,6 @@ _notmuch_message_upgrade_last_mod (notmuch_message_t *message)
 void
 _notmuch_message_sync (notmuch_message_t *message)
 {
 void
 _notmuch_message_sync (notmuch_message_t *message)
 {
-    Xapian::WritableDatabase *db;
-
     if (_notmuch_database_mode (message->notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
        return;
 
     if (_notmuch_database_mode (message->notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY)
        return;
 
@@ -1342,8 +1340,8 @@ _notmuch_message_sync (notmuch_message_t *message)
                                    _notmuch_database_new_revision (
                                        message->notmuch)));
 
                                    _notmuch_database_new_revision (
                                        message->notmuch)));
 
-    db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
-    db->replace_document (message->doc_id, message->doc);
+    message->notmuch->writable_xapian_db->
+       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_status_t status;
 _notmuch_message_delete (notmuch_message_t *message)
 {
     notmuch_status_t status;
-    Xapian::WritableDatabase *db;
     const char *mid, *tid, *query_string;
     notmuch_message_t *ghost;
     notmuch_private_status_t private_status;
     const char *mid, *tid, *query_string;
     notmuch_message_t *ghost;
     notmuch_private_status_t private_status;
@@ -1370,8 +1367,7 @@ _notmuch_message_delete (notmuch_message_t *message)
     if (status)
        return status;
 
     if (status)
        return status;
 
-    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
-    db->delete_document (message->doc_id);
+    message->notmuch->writable_xapian_db->delete_document (message->doc_id);
 
     /* if this was a ghost to begin with, we are done */
     private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);
 
     /* if this was a ghost to begin with, we are done */
     private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);