From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 4LkbCX8SHl9/GgAA0tVLHw (envelope-from ) for ; Sun, 26 Jul 2020 23:32:15 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id eok1BX8SHl8YLgAA1q6Kng (envelope-from ) for ; Sun, 26 Jul 2020 23:32:15 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id A87B59404E1 for ; Sun, 26 Jul 2020 23:32:14 +0000 (UTC) Received: from [144.217.243.247] (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 89BC127C14; Sun, 26 Jul 2020 19:31:55 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 7FFD61F9C9 for ; Sun, 26 Jul 2020 19:31:48 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id 7767A60AB8; Sun, 26 Jul 2020 19:31:48 -0400 (EDT) Received: (nullmailer pid 1638993 invoked by uid 1000); Sun, 26 Jul 2020 23:31:43 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 3/3] lib: replace use of static_cast for writable databases Date: Sun, 26 Jul 2020 20:31:36 -0300 Message-Id: <20200726233136.1636807-4-david@tethera.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200726233136.1636807-1-david@tethera.net> References: <20200726233136.1636807-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: ASK57UP3FJDNVTWTUOKCPM47LENYGPBE X-Message-ID-Hash: ASK57UP3FJDNVTWTUOKCPM47LENYGPBE X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Spam-Score: 1.53 X-TUID: qrxfK3LVicYS 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 | 13 +++---------- lib/config.cc | 4 +--- lib/database-private.h | 2 +- lib/database.cc | 21 ++++++++++++--------- lib/directory.cc | 18 +++++++----------- lib/message.cc | 10 +++------- 6 files changed, 27 insertions(+), 41 deletions(-) diff --git a/lib/add-message.cc b/lib/add-message.cc index 9dd4b697..485debad 100644 --- a/lib/add-message.cc +++ b/lib/add-message.cc @@ -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]; - Xapian::WritableDatabase *db; - - db = static_cast (notmuch->xapian_db); 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; } @@ -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. */ - db = static_cast (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); @@ -370,13 +367,9 @@ _consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch, if (stored_id.empty ()) { return NULL; } else { - Xapian::WritableDatabase *db; - - db = static_cast (notmuch->xapian_db); - /* 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 ()); } diff --git a/lib/config.cc b/lib/config.cc index 20036471..dae0ff0e 100644 --- a/lib/config.cc +++ b/lib/config.cc @@ -45,15 +45,13 @@ notmuch_database_set_config (notmuch_database_t *notmuch, const char *value) { notmuch_status_t status; - Xapian::WritableDatabase *db; status = _notmuch_database_ensure_writable (notmuch); if (status) return status; try { - db = static_cast (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; diff --git a/lib/database-private.h b/lib/database-private.h index d2c25313..041602cd 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -189,11 +189,11 @@ struct _notmuch_database { 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; + 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). */ diff --git a/lib/database.cc b/lib/database.cc index 08278235..75189685 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -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) { - 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): @@ -976,7 +979,7 @@ notmuch_database_open_verbose (const char *path, strip_trailing (notmuch->path, '/'); - notmuch->mode = mode; + notmuch->writable_xapian_db = NULL; 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) { - 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); } @@ -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) - (static_cast (notmuch->xapian_db)) - ->cancel_transaction (); + notmuch->writable_xapian_db->cancel_transaction (); /* Close the database. This implicitly flushes * outstanding changes. */ @@ -1454,7 +1457,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, if (status) return status; - db = static_cast (notmuch->xapian_db); + db = notmuch->writable_xapian_db; 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 { - (static_cast (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 ()); @@ -1736,7 +1739,7 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch) notmuch->atomic_nesting > 1) goto DONE; - db = static_cast (notmuch->xapian_db); + db = notmuch->writable_xapian_db; try { db->commit_transaction (); diff --git a/lib/directory.cc b/lib/directory.cc index 9ad66e82..79ceea31 100644 --- a/lib/directory.cc +++ b/lib/directory.cc @@ -112,7 +112,6 @@ _notmuch_directory_find_or_create (notmuch_database_t *notmuch, 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; @@ -189,10 +188,10 @@ _notmuch_directory_find_or_create (notmuch_database_t *notmuch, directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP, Xapian::sortable_serialise (0)); - db = static_cast (notmuch->xapian_db); - 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); } @@ -226,20 +225,18 @@ notmuch_directory_set_mtime (notmuch_directory_t *directory, 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; - db = static_cast (notmuch->xapian_db); - 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; @@ -309,15 +306,14 @@ notmuch_status_t 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 { - db = static_cast (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", diff --git a/lib/message.cc b/lib/message.cc index d23e64ab..fca99082 100644 --- a/lib/message.cc +++ b/lib/message.cc @@ -1322,8 +1322,6 @@ _notmuch_message_upgrade_last_mod (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; @@ -1342,8 +1340,8 @@ _notmuch_message_sync (notmuch_message_t *message) _notmuch_database_new_revision ( message->notmuch))); - db = static_cast (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; } @@ -1353,7 +1351,6 @@ notmuch_status_t _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; @@ -1370,8 +1367,7 @@ _notmuch_message_delete (notmuch_message_t *message) if (status) return status; - db = static_cast (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); -- 2.27.0