unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Cc: Austin Clements <amdragon@mit.edu>
Subject: [PATCH 08/17] lib: Add support for nested atomic sections.
Date: Sat, 11 Jun 2011 16:04:34 -0400	[thread overview]
Message-ID: <1307822683-848-9-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1307822683-848-1-git-send-email-amdragon@mit.edu>

notmuch_database_t now keeps a nesting count and we only start a
transaction or commit for the outermost atomic section.

Introduces a new error, NOTMUCH_STATUS_UNBALANCED_ATOMIC.
---
 lib/database-private.h |    1 +
 lib/database.cc        |   22 ++++++++++++++++++----
 lib/notmuch.h          |   10 ++++++++++
 notmuch-new.c          |    1 +
 4 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/lib/database-private.h b/lib/database-private.h
index f705009..88532d5 100644
--- a/lib/database-private.h
+++ b/lib/database-private.h
@@ -43,6 +43,7 @@ struct _notmuch_database {
 
     notmuch_bool_t needs_upgrade;
     notmuch_database_mode_t mode;
+    int atomic_nesting;
     Xapian::Database *xapian_db;
 
     unsigned int last_doc_id;
diff --git a/lib/database.cc b/lib/database.cc
index ddb6167..b766e94 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -273,6 +273,8 @@ notmuch_status_to_string (notmuch_status_t status)
 	return "Tag value is too long (exceeds NOTMUCH_TAG_MAX)";
     case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
 	return "Unbalanced number of calls to notmuch_message_freeze/thaw";
+    case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
+	return "Unbalanced number of calls to notmuch_database_begin_atomic/end_atomic";
     default:
     case NOTMUCH_STATUS_LAST_STATUS:
 	return "Unknown error status value";
@@ -611,6 +613,7 @@ notmuch_database_open (const char *path,
 
     notmuch->needs_upgrade = FALSE;
     notmuch->mode = mode;
+    notmuch->atomic_nesting = 0;
     try {
 	string last_thread_id;
 
@@ -977,8 +980,9 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
 notmuch_status_t
 notmuch_database_begin_atomic (notmuch_database_t *notmuch)
 {
-    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
-	return NOTMUCH_STATUS_SUCCESS;
+    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY ||
+	notmuch->atomic_nesting > 0)
+	goto DONE;
 
     try {
 	(static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->begin_transaction (false);
@@ -988,6 +992,9 @@ notmuch_database_begin_atomic (notmuch_database_t *notmuch)
 	notmuch->exception_reported = TRUE;
 	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
     }
+
+DONE:
+    notmuch->atomic_nesting++;
     return NOTMUCH_STATUS_SUCCESS;
 }
 
@@ -996,8 +1003,12 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch)
 {
     Xapian::WritableDatabase *db;
 
-    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
-	return NOTMUCH_STATUS_SUCCESS;
+    if (notmuch->atomic_nesting == 0)
+	return NOTMUCH_STATUS_UNBALANCED_ATOMIC;
+
+    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY ||
+	notmuch->atomic_nesting > 1)
+	goto DONE;
 
     db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
     try {
@@ -1015,6 +1026,9 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch)
 	notmuch->exception_reported = TRUE;
 	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
     }
+
+DONE:
+    notmuch->atomic_nesting--;
     return NOTMUCH_STATUS_SUCCESS;
 }
 
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 208d22c..830aa41 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -81,6 +81,9 @@ typedef int notmuch_bool_t;
  * NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: The notmuch_message_thaw
  *	function has been called more times than notmuch_message_freeze.
  *
+ * NOTMUCH_STATUS_UNBALANCED_ATOMIC: notmuch_database_end_atomic has
+ *	been called more times than notmuch_database_begin_atomic.
+ *
  * And finally:
  *
  * NOTMUCH_STATUS_LAST_STATUS: Not an actual status value. Just a way
@@ -97,6 +100,7 @@ typedef enum _notmuch_status {
     NOTMUCH_STATUS_NULL_POINTER,
     NOTMUCH_STATUS_TAG_TOO_LONG,
     NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
+    NOTMUCH_STATUS_UNBALANCED_ATOMIC,
 
     NOTMUCH_STATUS_LAST_STATUS
 } notmuch_status_t;
@@ -222,6 +226,9 @@ notmuch_database_upgrade (notmuch_database_t *database,
  * only ensures atomicity, not durability; neither begin nor end
  * necessarily flush modifications to disk.
  *
+ * Atomic sections may be nested.  begin_atomic and end_atomic must
+ * always be called in pairs.
+ *
  * Return value:
  *
  * NOTMUCH_STATUS_SUCCESS: Successfully entered atomic section.
@@ -240,6 +247,9 @@ notmuch_database_begin_atomic (notmuch_database_t *notmuch);
  *
  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
  *	atomic section not ended.
+ *
+ * NOTMUCH_STATUS_UNBALANCED_ATOMIC: The database is not currently in
+ *	an atomic section.
  */
 notmuch_status_t
 notmuch_database_end_atomic (notmuch_database_t *notmuch);
diff --git a/notmuch-new.c b/notmuch-new.c
index b76b608..d1bea55 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -466,6 +466,7 @@ add_files_recursive (notmuch_database_t *notmuch,
 	case NOTMUCH_STATUS_NULL_POINTER:
 	case NOTMUCH_STATUS_TAG_TOO_LONG:
 	case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
+	case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
 	case NOTMUCH_STATUS_LAST_STATUS:
 	    INTERNAL_ERROR ("add_message returned unexpected value: %d",  status);
 	    goto DONE;
-- 
1.7.5.1

  parent reply	other threads:[~2011-06-11 20:06 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-18  7:58 [PATCH 00/10] Fix 'notmuch new' atomicity issues Austin Clements
2011-02-18  7:58 ` [PATCH 01/10] test: Test atomicity of notmuch new Austin Clements
2011-05-04 20:32   ` [PATCH 01/10 v2] " Austin Clements
2011-02-18  7:58 ` [PATCH 02/10] new: Don't loose messages on SIGINT Austin Clements
2011-02-18  7:58 ` [PATCH 03/10] new: Defer updating directory mtimes until the end Austin Clements
2011-02-18  7:58 ` [PATCH 04/10] lib: Make _notmuch_message_sync capable of deleting a message Austin Clements
2011-02-18  7:58 ` [PATCH 05/10] lib: Indicate if there are more filenames after removal Austin Clements
2011-02-18  7:58 ` [PATCH 06/10] lib: Add API's to find by filename and remove a filename from a message Austin Clements
2011-02-18  7:58 ` [PATCH 07/10] new: Use the new filename removal API Austin Clements
2011-02-18  7:58 ` [PATCH 08/10] new: Synchronize maildir flags eagerly Austin Clements
2011-02-18  7:58 ` [PATCH 09/10] lib: Add notmuch_database_{begin,end}_atomic Austin Clements
2011-02-18  7:59 ` [PATCH 10/10] new: Wrap adding a message in an atomic section Austin Clements
2011-04-26  4:13 ` [PATCH 00/10] Fix 'notmuch new' atomicity issues Austin Clements
2011-05-04 20:30   ` Austin Clements
2011-05-29  2:51     ` Austin Clements
2011-06-08 22:05       ` Carl Worth
2011-06-10 21:11         ` Austin Clements
2011-06-10 22:02           ` Carl Worth
2011-06-10 22:27             ` Austin Clements
2011-06-11 20:04         ` [PATCH v6 00/17] " Austin Clements
2011-06-11 20:04           ` [PATCH 02/17] test: Report test failures from test_expect_* Austin Clements
2011-06-11 20:04           ` [PATCH 03/17] lib: Add missing status check in _notmuch_message_remove_filename Austin Clements
2011-06-11 20:04           ` [PATCH 04/17] test: Test atomicity of notmuch new Austin Clements
2011-06-11 20:04           ` [PATCH 05/17] new: Don't lose messages on SIGINT Austin Clements
2011-06-11 20:04           ` [PATCH 06/17] new: Defer updating directory mtimes until the end Austin Clements
2011-06-11 20:04           ` [PATCH 07/17] lib: Add notmuch_database_{begin,end}_atomic Austin Clements
2011-06-11 20:04           ` Austin Clements [this message]
2011-06-11 20:04           ` [PATCH 09/17] lib: Indicate if there are more filenames after removal Austin Clements
2011-06-11 20:04           ` [PATCH 10/17] lib: Remove message document directly after removing the last file name Austin Clements
2011-06-11 20:04           ` [PATCH 11/17] lib: Add an API to find a message by filename Austin Clements
2011-06-11 20:04           ` [PATCH 12/17] lib: Wrap notmuch_database_add_message in an atomic section Austin Clements
2011-06-11 20:04           ` [PATCH 13/17] new: Cleanup. Put removed/renamed message count in add_files_state_t Austin Clements
2011-06-11 20:04           ` [PATCH 14/17] new: Cleanup. De-duplicate file name removal code Austin Clements
2011-06-11 20:04           ` [PATCH 15/17] new: Synchronize maildir flags eagerly Austin Clements
2011-06-11 20:04           ` [PATCH 16/17] new: Wrap adding and removing messages in atomic sections Austin Clements
2011-06-11 20:04           ` [PATCH 17/17] lib: Improve notmuch_database_{add, remove}_message documentation Austin Clements
2011-06-11 20:49           ` [PATCH 01/17] test: Fix message when skipping test_expect_equal* tests Austin Clements
2011-06-11 20:53           ` [PATCH v6 00/17] Fix 'notmuch new' atomicity issues Austin Clements
2011-06-22 19:39           ` Austin Clements
2011-06-29  6:37             ` Austin Clements
2011-07-08 18:09             ` Austin Clements
2011-09-13 12:39               ` David Bremner
2011-09-24  3:14                 ` David Bremner
2011-09-24  4:03                   ` Austin Clements
2011-09-25  2:36                     ` David Bremner
2011-09-26 22:07                       ` Austin Clements
2011-09-26 23:32                         ` NEWS for 0.9 David Bremner
2011-09-27 13:08                           ` Ali Polatel
2011-09-28 16:36           ` [PATCH v6 00/17] Fix 'notmuch new' atomicity issues Sebastian Spaeth
2011-09-29 15:01             ` Austin Clements
2011-09-30  9:21               ` Sebastian Spaeth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1307822683-848-9-git-send-email-amdragon@mit.edu \
    --to=amdragon@mit.edu \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).