Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 1B31A431FC0 for ; Thu, 2 Oct 2014 12:19:21 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.3 X-Spam-Level: X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id h5OeBDRD08fY for ; Thu, 2 Oct 2014 12:19:17 -0700 (PDT) Received: from dmz-mailsec-scanner-5.mit.edu (dmz-mailsec-scanner-5.mit.edu [18.7.68.34]) by olra.theworths.org (Postfix) with ESMTP id 1D9B1431FB6 for ; Thu, 2 Oct 2014 12:19:17 -0700 (PDT) X-AuditID: 12074422-f79436d000000c21-28-542da534ddd0 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) (using TLS with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP id AF.23.03105.435AD245; Thu, 2 Oct 2014 15:19:16 -0400 (EDT) Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s92JJEoM022339; Thu, 2 Oct 2014 15:19:14 -0400 Received: from drake.dyndns.org (26-4-182.dynamic.csail.mit.edu [18.26.4.182]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s92JJBXw006342 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Thu, 2 Oct 2014 15:19:13 -0400 Received: from amthrax by drake.dyndns.org with local (Exim 4.84) (envelope-from ) id 1XZluB-0000uZ-Kh; Thu, 02 Oct 2014 15:19:11 -0400 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v4] lib: Simplify close and codify aborting atomic section Date: Thu, 2 Oct 2014 15:19:08 -0400 Message-Id: <1412277548-3445-1-git-send-email-aclements@csail.mit.edu> X-Mailer: git-send-email 2.1.0 In-Reply-To: <87mw9ee0vf.fsf@csail.mit.edu> References: <87mw9ee0vf.fsf@csail.mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrGIsWRmVeSWpSXmKPExsUixG6nrmuyVDfEYHO/msWN1m5Gi+s3ZzJb XFlznt2B2ePZqlvMHlsOvWf22P39J0sAcxSXTUpqTmZZapG+XQJXxuXWXywFR5QqHtxex9jA +Fi6i5GTQ0LARKJh2WpWCFtM4sK99WwgtpDAbCaJ5b8ruxi5gOwNjBKvJ61ihUgcZpKYuIYT IrGEUWLBz+csIAk2AX2JFWsngRWJCEhL7Lw7G8xmFvCWmP11NTuILSzgJbGt7QIziM0ioCpx bcsLMJtXwE1i644j7BBXyEls2P2fEcTmFNCWWPN1FjPEYi2J1s4+9gmM/AsYGVYxyqbkVunm JmbmFKcm6xYnJ+blpRbpmurlZpbopaaUbmIEB5eL0g7GnweVDjEKcDAq8fBmNOiECLEmlhVX 5h5ilORgUhLl/bFQN0SILyk/pTIjsTgjvqg0J7X4EKMEB7OSCK9wN1CONyWxsiq1KB8mJc3B oiTOu+kHX4iQQHpiSWp2ampBahFMVoaDQ0mCd/d8oEbBotT01Iq0zJwShDQTByfIcB6g4THT QYYXFyTmFmemQ+RPMSpKifNyzgNKCIAkMkrz4Hph0f+KURzoFWHeaJB2HmDigOt+BTSYCWjw O3uwwSWJCCmpBkbJ4sTEXucTLmc7J+k+Kyn0r//oucd6Unvrli0am3//X/F4tvhpyZiTcXKZ bpHrSq6rrYgW/JuiwuKh0+q2233ZTGvvd7/+/r/xX6wpILom3TL7vsn0cDfvC3Xnvk/MVeQw u9oc1N/UdVXhy6qXszccOVijoJjvI2Xmtl59zaJOu84AbcYLE5RYijMSDbWYi4oTAUuMhoPZ AgAA X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Oct 2014 19:19:21 -0000 From: Austin Clements In Xapian, closing a database implicitly aborts any outstanding transaction and commits changes. For historical reasons, notmuch_database_close had grown to almost, but not quite duplicate this behavior. Before closing the database, it would explicitly (and unnecessarily) commit it. However, if there was an outstanding transaction (ie atomic section), commit would throw a Xapian exception, which notmuch_database_close would unnecessarily print to stderr, even though notmuch_database_close would ultimately abort the transaction anyway when it called close. This patch simplifies notmuch_database_close to explicitly abort any outstanding transaction and then just call Database::close. This works for both read-only and read/write databases, takes care of committing changes, unifies the exception handling path, and codifies aborting outstanding transactions. This is currently the only way to abort an atomic section (and may remain so, since it would be difficult to roll back things we may have cached from rolled-back modifications). --- lib/database.cc | 32 +++++++++++++++++--------------- lib/notmuch.h | 9 ++++++++- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/database.cc b/lib/database.cc index a3a7cd3..a47a71d 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -903,28 +903,30 @@ notmuch_database_close (notmuch_database_t *notmuch) { notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; - try { - if (notmuch->xapian_db != NULL && - notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE) - (static_cast (notmuch->xapian_db))->flush (); - } catch (const Xapian::Error &error) { - status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; - if (! notmuch->exception_reported) { - fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n", - error.get_msg().c_str()); - } - } - /* Many Xapian objects (and thus notmuch objects) hold references to * the database, so merely deleting the database may not suffice to * close it. Thus, we explicitly close it here. */ if (notmuch->xapian_db != NULL) { try { + /* If there's an outstanding transaction, it's unclear if + * closing the Xapian database commits everything up to + * that transaction, or may discard committed (but + * unflushed) transactions. To be certain, explicitly + * cancel any outstanding transaction before closing. */ + if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE && + notmuch->atomic_nesting) + (static_cast (notmuch->xapian_db)) + ->cancel_transaction (); + + /* Close the database. This implicitly flushes + * outstanding changes. */ notmuch->xapian_db->close(); } catch (const Xapian::Error &error) { - /* don't clobber previous error status */ - if (status == NOTMUCH_STATUS_SUCCESS) - status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; + status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; + if (! notmuch->exception_reported) { + fprintf (stderr, "Error: A Xapian exception occurred closing database: %s\n", + error.get_msg().c_str()); + } } } diff --git a/lib/notmuch.h b/lib/notmuch.h index fe2340b..dae0416 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -281,7 +281,7 @@ notmuch_database_open (const char *path, notmuch_database_t **database); /** - * Close the given notmuch database. + * Commit changes and close the given notmuch database. * * After notmuch_database_close has been called, calls to other * functions on objects derived from this database may either behave @@ -292,6 +292,13 @@ notmuch_database_open (const char *path, * notmuch_database_close can be called multiple times. Later calls * have no effect. * + * For writable databases, notmuch_database_close commits all changes + * to disk before closing the database. If the caller is currently in + * an atomic section (there was a notmuch_database_begin_atomic + * without a matching notmuch_database_end_atomic), this will discard + * changes made in that atomic section (but still commit changes made + * prior to entering the atomic section). + * * Return value: * * NOTMUCH_STATUS_SUCCESS: Successfully closed the database. -- 2.1.0