[PATCH v2] lib: Simplify close and codify aborting atomic section
authorAustin Clements <aclements@csail.mit.edu>
Wed, 24 Sep 2014 21:20:23 +0000 (17:20 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:04:45 +0000 (10:04 -0800)
c1/0a91964dd6f927d7ac9dedfa7a099b3b9a5eca [new file with mode: 0644]

diff --git a/c1/0a91964dd6f927d7ac9dedfa7a099b3b9a5eca b/c1/0a91964dd6f927d7ac9dedfa7a099b3b9a5eca
new file mode 100644 (file)
index 0000000..285fced
--- /dev/null
@@ -0,0 +1,159 @@
+Return-Path: <aclements@csail.mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 41067431FBC\r
+       for <notmuch@notmuchmail.org>; Wed, 24 Sep 2014 14:20:44 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id WtR9-lQ9g+c8 for <notmuch@notmuchmail.org>;\r
+       Wed, 24 Sep 2014 14:20:38 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (dmz-mailsec-scanner-4.mit.edu\r
+       [18.9.25.15])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id B56AB431FAF\r
+       for <notmuch@notmuchmail.org>; Wed, 24 Sep 2014 14:20:38 -0700 (PDT)\r
+X-AuditID: 1209190f-f79aa6d000005b45-a9-542335a538f7\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+       (using TLS with cipher AES256-SHA (256/256 bits))\r
+       (Client did not present a certificate)\r
+       by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 63.9B.23365.5A533245; Wed, 24 Sep 2014 17:20:38 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+       by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s8OLKYuY021561; \r
+       Wed, 24 Sep 2014 17:20:34 -0400\r
+Received: from drake.dyndns.org (q-1.n.148.220.37.qore.nl [37.220.148.94] (may\r
+       be forged)) (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s8OLKUxX018469\r
+       (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+       Wed, 24 Sep 2014 17:20:32 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+       (envelope-from <aclements@csail.mit.edu>)\r
+       id 1XWtzB-00029S-TY; Wed, 24 Sep 2014 17:20:29 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2] lib: Simplify close and codify aborting atomic section\r
+Date: Wed, 24 Sep 2014 17:20:23 -0400\r
+Message-Id: <1411593623-8234-1-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1411400615-20934-1-git-send-email-amdragon@mit.edu>\r
+References: <1411400615-20934-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFrrMIsWRmVeSWpSXmKPExsUixG6norvMVDnEYHI7s8WN1m5Gi+s3ZzJb\r
+       XFlznt2B2ePZqlvMHlsOvWf22P39J0sAcxSXTUpqTmZZapG+XQJXxoUb/SwFx6QqDq/5ydzA\r
+       uEO0i5GTQ0LARGL7+lWMELaYxIV769m6GLk4hARmM0n8efKIBcLZyCixqnESI4RzgUli1rRv\r
+       LCAtQgJLGCX2/rMGsdkE9CVWrJ3ECmKLCEhL7Lw7G8xmFvCWmP11NTuILSzgJXH8zhYmEJtF\r
+       QFXiZM8xsBpeATeJK39fQJ0hJ7Fh938wm1PAUeLexJmsELscJH5Pf8k+gZF/ASPDKkbZlNwq\r
+       3dzEzJzi1GTd4uTEvLzUIl0TvdzMEr3UlNJNjKDw4pTk38H47aDSIUYBDkYlHt4J4kohQqyJ\r
+       ZcWVuYcYJTmYlER51+orhwjxJeWnVGYkFmfEF5XmpBYfYpTgYFYS4VX5CFTOm5JYWZValA+T\r
+       kuZgURLn3fSDL0RIID2xJDU7NbUgtQgmK8PBoSTBe9QEaKhgUWp6akVaZk4JQpqJgxNkOA/Q\r
+       8NMgNbzFBYm5xZnpEPlTjLoc6zq/9TMJseTl56VKifMeBCkSACnKKM2DmwNLC68YxYHeEuat\r
+       BKniAaYUuEmvgJYwAS25f1weZElJIkJKqoHRh7tw+vb/4U4BsnMLYuZ+NpTdytJ68Zx2YPpK\r
+       vcPX5aSEwq7ECnbvrLHcrr7pV6Vs7cpgObHFQS0/Jh+cViB731Hbj9ltVZyV5KlivjSJJPXX\r
+       /MVTd/jIK89tYmba2f7ee7W4VL21FvOvXrd337pnf3vy7rjqoZ/ZU5yOu9nKZyya83/RgV1K\r
+       LMUZiYZazEXFiQDvLj9h5gIAAA==\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 24 Sep 2014 21:20:44 -0000\r
+\r
+From: Austin Clements <amdragon@mit.edu>\r
+\r
+In Xapian, closing a database implicitly aborts any outstanding\r
+transaction and commits changes.  For historical reasons,\r
+notmuch_database_close had grown to almost, but not quite duplicate\r
+this behavior.  Before closing the database, it would explicitly (and\r
+unnecessarily) commit it.  However, if there was an outstanding\r
+transaction (ie atomic section), commit would throw a Xapian\r
+exception, which notmuch_database_close would unnecessarily print to\r
+stderr, even though notmuch_database_close would ultimately abort the\r
+transaction anyway when it called close.\r
+\r
+This patch simplifies notmuch_database_close to just call\r
+Database::close.  This works for both read-only and read/write\r
+databases, takes care of committing changes, unifies the exception\r
+handling path, and codifies aborting outstanding transactions.  This\r
+is currently the only way to abort an atomic section (and may remain\r
+so, since it would be difficult to roll back things we may have cached\r
+from rolled-back modifications).\r
+---\r
+ lib/database.cc | 23 +++++++----------------\r
+ lib/notmuch.h   |  5 +++++\r
+ 2 files changed, 12 insertions(+), 16 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index a3a7cd3..1f7ff2a 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -903,28 +903,19 @@ notmuch_database_close (notmuch_database_t *notmuch)\r
+ {\r
+     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
\r
+-    try {\r
+-      if (notmuch->xapian_db != NULL &&\r
+-          notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)\r
+-          (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();\r
+-    } catch (const Xapian::Error &error) {\r
+-      status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+-      if (! notmuch->exception_reported) {\r
+-          fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n",\r
+-                   error.get_msg().c_str());\r
+-      }\r
+-    }\r
+-\r
+     /* Many Xapian objects (and thus notmuch objects) hold references to\r
+      * the database, so merely deleting the database may not suffice to\r
+-     * close it.  Thus, we explicitly close it here. */\r
++     * close it.  Thus, we explicitly close it here.  This will\r
++     * implicitly abort any outstanding transaction and commit changes. */\r
+     if (notmuch->xapian_db != NULL) {\r
+       try {\r
+           notmuch->xapian_db->close();\r
+       } catch (const Xapian::Error &error) {\r
+-          /* don't clobber previous error status */\r
+-          if (status == NOTMUCH_STATUS_SUCCESS)\r
+-              status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++          status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++          if (! notmuch->exception_reported) {\r
++              fprintf (stderr, "Error: A Xapian exception occurred closing database: %s\n",\r
++                       error.get_msg().c_str());\r
++          }\r
+       }\r
+     }\r
\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index fe2340b..5c40c67 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -292,6 +292,11 @@ notmuch_database_open (const char *path,\r
+  * notmuch_database_close can be called multiple times.  Later calls\r
+  * have no effect.\r
+  *\r
++ * If the caller is currently in an atomic section (there was a\r
++ * notmuch_database_begin_atomic without a matching\r
++ * notmuch_database_end_atomic), this will abort the atomic section,\r
++ * discarding any modifications made in the atomic section.\r
++ *\r
+  * Return value:\r
+  *\r
+  * NOTMUCH_STATUS_SUCCESS: Successfully closed the database.\r
+-- \r
+2.1.0\r
+\r