Patch: Flush and Reopen
authorMartin Owens <doctormo@gmail.com>
Thu, 8 Sep 2011 01:34:34 +0000 (21:34 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:39:23 +0000 (09:39 -0800)
0e/d742bc1ca88a22a3042dd095a112619120e32c [new file with mode: 0644]

diff --git a/0e/d742bc1ca88a22a3042dd095a112619120e32c b/0e/d742bc1ca88a22a3042dd095a112619120e32c
new file mode 100644 (file)
index 0000000..70f683b
--- /dev/null
@@ -0,0 +1,274 @@
+Return-Path: <doctormo@gmail.com>\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 8890C431FD0\r
+       for <notmuch@notmuchmail.org>; Wed,  7 Sep 2011 18:34:39 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+       FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] 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 TT4P7oUPK3CL for <notmuch@notmuchmail.org>;\r
+       Wed,  7 Sep 2011 18:34:38 -0700 (PDT)\r
+Received: from mail-qw0-f41.google.com (mail-qw0-f41.google.com\r
+       [209.85.216.41]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 77C7A431FB6\r
+       for <notmuch@notmuchmail.org>; Wed,  7 Sep 2011 18:34:38 -0700 (PDT)\r
+Received: by qwa26 with SMTP id 26so234852qwa.28\r
+       for <notmuch@notmuchmail.org>; Wed, 07 Sep 2011 18:34:36 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
+       h=subject:from:to:cc:content-type:date:message-id:mime-version\r
+       :x-mailer; bh=4ayS/J3O86g7aEykmELa/D+9gYJoKMLSar97FHUthLw=;\r
+       b=C5QfcfY/Vz5WQOxtrwgAvFXhn22FxPMmFEE8DFbfZLzQiOw6UI9Mx//PuH/dlxRTh+\r
+       ZwI7/SvtF6rJaJPqnuH0Rz4jNyce1GB9+rr8O1dvGO1FypTcgkh6Fw7l1Q5w+D48mrTu\r
+       VW79mu+yljQOC7Rg5w/LTM59RoVKpkfMVcWBg=\r
+Received: by 10.224.215.130 with SMTP id he2mr75841qab.0.1315445676769;\r
+       Wed, 07 Sep 2011 18:34:36 -0700 (PDT)\r
+Received: from [192.168.1.190] (pool-68-163-190-45.bos.east.verizon.net.\r
+       [68.163.190.45])\r
+       by mx.google.com with ESMTPS id dx7sm1644073qab.11.2011.09.07.18.34.34\r
+       (version=SSLv3 cipher=OTHER); Wed, 07 Sep 2011 18:34:35 -0700 (PDT)\r
+Subject: Patch: Flush and Reopen\r
+From: Martin Owens <doctormo@gmail.com>\r
+To: Notmuch developer list <notmuch@notmuchmail.org>\r
+Content-Type: multipart/mixed; boundary="=-QH9fXRHOIjOq1XQ/OK0q"\r
+Date: Wed, 07 Sep 2011 21:34:34 -0400\r
+Message-ID: <1315445674.32058.183.camel@delen>\r
+Mime-Version: 1.0\r
+X-Mailer: Evolution 2.30.3 \r
+X-Mailman-Approved-At: Thu, 08 Sep 2011 14:21:55 -0700\r
+Cc: Paul Tagliamonte <paultag@ubuntu.com>\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: Thu, 08 Sep 2011 01:34:39 -0000\r
+\r
+\r
+--=-QH9fXRHOIjOq1XQ/OK0q\r
+Content-Type: text/plain; charset="UTF-8"\r
+Content-Transfer-Encoding: 7bit\r
+\r
+Hello,\r
+\r
+Per discussions on irc, I've attached a patch for your consideration\r
+which allows the developer to run two new database commands;\r
+\r
+notmuch_database_flush - used on read_write databases to commit changes\r
+to disk\r
+notmuch_database_reopen - used on read_only databases to reload from the\r
+disk\r
+\r
+Used in conjunction they can allow access by multiple programs at the\r
+same time. There are also changes to the python bindings to add these\r
+two methods to the database class.\r
+\r
+Regards, Martin Owens\r
+\r
+--=-QH9fXRHOIjOq1XQ/OK0q\r
+Content-Disposition: attachment;\r
+ filename*0=0001-Add-flush-and-reopen-methods-to-the-libnotmuch-and-t.pat;\r
+ filename*1=ch\r
+Content-Type: text/x-patch;\r
+ name="0001-Add-flush-and-reopen-methods-to-the-libnotmuch-and-t.patch";\r
+ charset="UTF-8"\r
+Content-Transfer-Encoding: 7bit\r
+\r
+>From 92b89aad3101faa42c6fdca9b6683ebfa42ab3b0 Mon Sep 17 00:00:00 2001\r
+From: Martin Owens <doctormo@gmail.com>\r
+Date: Wed, 7 Sep 2011 21:15:54 -0400\r
+Subject: [PATCH] Add flush and reopen methods to the libnotmuch and to the python bindings. Flush allows read_write databases to commit their changes, forcing a flush to disk and reopen allows a read_only database to reread from the disk.\r
+\r
+Also added a handler for Xapian DatabaseChangedError which occurs when the flush happens to read only databases.\r
+---\r
+ bindings/python/notmuch/database.py |   10 ++++++++++\r
+ debian/changelog                    |    6 +++++-\r
+ debian/control                      |    2 +-\r
+ debian/libnotmuch1.symbols          |    2 ++\r
+ lib/database.cc                     |   33 ++++++++++++++++++++++++++-------\r
+ lib/notmuch.h                       |    8 ++++++++\r
+ lib/query.cc                        |   10 +++++++---\r
+ 7 files changed, 59 insertions(+), 12 deletions(-)\r
+\r
+diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py\r
+index f18ca14..b6c1c31 100644\r
+--- a/bindings/python/notmuch/database.py\r
++++ b/bindings/python/notmuch/database.py\r
+@@ -432,6 +432,16 @@ class Database(object):\r
\r
+         return Query(self, querystring)\r
\r
++    def reopen(self):\r
++        """Reopens a read only database, when the data has changed, this is required."""\r
++        if self._db is not None:\r
++            nmlib.notmuch_database_reopen(self._db)\r
++\r
++    def flush(self):\r
++        """Flushes the search database, only available when in read-write."""\r
++        if self._db is not None:\r
++            nmlib.notmuch_database_flush(self._db)\r
++\r
+     def __repr__(self):\r
+         return "'Notmuch DB " + self.get_path() + "'"\r
\r
+diff --git a/debian/changelog b/debian/changelog\r
+index c2b7552..565edfb 100644\r
+--- a/debian/changelog\r
++++ b/debian/changelog\r
+@@ -1,8 +1,12 @@\r
+ notmuch (0.8~rc1-1) experimental; urgency=low\r
\r
++  [ David Bremner ]\r
+   * Upstream release candidate.\r
\r
+- -- David Bremner <bremner@debian.org>  Tue, 06 Sep 2011 22:24:24 -0300\r
++  [ Martin Owens (DoctorMO) ]\r
++  * Re-new-world-order\r
++\r
++ -- Martin Owens (DoctorMO) <doctormo@gmail.com>  Wed, 07 Sep 2011 18:19:50 -0400\r
\r
+ notmuch (0.7-1) unstable; urgency=low\r
\r
+diff --git a/debian/control b/debian/control\r
+index 03afdf4..a72fe1b 100644\r
+--- a/debian/control\r
++++ b/debian/control\r
+@@ -5,7 +5,7 @@ Maintainer: Carl Worth <cworth@debian.org>\r
+ Uploaders: Jameson Graef Rollins <jrollins@finestructure.net>, martin f. krafft <madduck@debian.org>, \r
+          David Bremner <bremner@debian.org>\r
+ Build-Depends: debhelper (>= 7.0.50~), pkg-config, libxapian-dev, \r
+- libgmime-2.4-dev, libtalloc-dev, libz-dev,  python-all (>= 2.6.6-3~),\r
++ libgmime-2.4-dev, libtalloc-dev, libz-dev,  python-all (>= 2.6.6),\r
+  emacs23-nox | emacs23 (>=23~) | emacs23-lucid (>=23~)\r
+ Standards-Version: 3.9.2\r
+ Homepage: http://notmuchmail.org/\r
+diff --git a/debian/libnotmuch1.symbols b/debian/libnotmuch1.symbols\r
+index 05d86e6..7e42b0e 100644\r
+--- a/debian/libnotmuch1.symbols\r
++++ b/debian/libnotmuch1.symbols\r
+@@ -3,6 +3,7 @@ libnotmuch.so.1 libnotmuch1 #MINVER#\r
+  notmuch_database_close@Base 0.3\r
+  notmuch_database_create@Base 0.3\r
+  notmuch_database_find_message@Base 0.3\r
++ notmuch_database_flush@Base 0.3\r
+  notmuch_database_get_all_tags@Base 0.3\r
+  notmuch_database_get_directory@Base 0.3\r
+  notmuch_database_get_path@Base 0.3\r
+@@ -10,6 +11,7 @@ libnotmuch.so.1 libnotmuch1 #MINVER#\r
+  notmuch_database_needs_upgrade@Base 0.3\r
+  notmuch_database_open@Base 0.3\r
+  notmuch_database_remove_message@Base 0.3\r
++ notmuch_database_reopen@Base 0.3\r
+  notmuch_database_upgrade@Base 0.3\r
+  notmuch_directory_destroy@Base 0.3\r
+  notmuch_directory_get_child_directories@Base 0.3\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 9c2f4ec..58b6c75 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -700,18 +700,37 @@ notmuch_database_open (const char *path,\r
+ }\r
\r
+ void\r
+-notmuch_database_close (notmuch_database_t *notmuch)\r
++notmuch_database_reopen (notmuch_database_t *notmuch)\r
+ {\r
+     try {\r
+-      if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)\r
+-          (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();\r
++        if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)\r
++            (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->reopen();\r
+     } catch (const Xapian::Error &error) {\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
++        if (! notmuch->exception_reported) {\r
++            fprintf (stderr, "Error: A Xapian exception occurred reopening database: %s\n",\r
++                error.get_msg().c_str());\r
++        }\r
+     }\r
++}\r
\r
++void\r
++notmuch_database_flush (notmuch_database_t *notmuch)\r
++{\r
++    try {\r
++        if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)\r
++            (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush();\r
++    } catch (const Xapian::Error &error) {\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
++\r
++void\r
++notmuch_database_close (notmuch_database_t *notmuch)\r
++{\r
++    notmuch_database_flush (notmuch);\r
+     delete notmuch->term_gen;\r
+     delete notmuch->query_parser;\r
+     delete notmuch->xapian_db;\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 974be8d..ca87b89 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -171,6 +171,14 @@ notmuch_database_t *\r
+ notmuch_database_open (const char *path,\r
+                      notmuch_database_mode_t mode);\r
\r
++/* Reopen a read_only database when the data source has changed */\r
++void\r
++notmuch_database_reopen (notmuch_database_t *database);\r
++\r
++/* Force a flush of the xapian database, also done on close */\r
++void\r
++notmuch_database_flush (notmuch_database_t *database);\r
++\r
+ /* Close the given notmuch database, freeing all associated\r
+  * resources. See notmuch_database_open. */\r
+ void\r
+diff --git a/lib/query.cc b/lib/query.cc\r
+index 6f02b04..d116367 100644\r
+--- a/lib/query.cc\r
++++ b/lib/query.cc\r
+@@ -185,10 +185,14 @@ notmuch_query_search_messages (notmuch_query_t *query)\r
+       messages->iterator_end = mset.end ();\r
\r
+       return &messages->base;\r
+-\r
++    } catch (const Xapian::DatabaseModifiedError &error) {\r
++      fprintf (stderr, "Database changed, you need to reopen it before performing queries.\n");\r
++      notmuch->exception_reported = TRUE;\r
++      talloc_free (messages);\r
++      return NULL;\r
+     } catch (const Xapian::Error &error) {\r
+-      fprintf (stderr, "A Xapian exception occurred performing query: %s\n",\r
+-               error.get_msg().c_str());\r
++      fprintf (stderr, "A Xapian exception occurred performing query: %s '%s'\n",\r
++               error.get_msg().c_str(), error.get_type());\r
+       fprintf (stderr, "Query string was: %s\n", query->query_string);\r
+       notmuch->exception_reported = TRUE;\r
+       talloc_free (messages);\r
+-- \r
+1.7.1\r
+\r
+\r
+--=-QH9fXRHOIjOq1XQ/OK0q--\r
+\r