From 2d247d56660689dee5ae58c252621f9343044dd8 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sun, 11 Sep 2011 20:51:35 -0400 Subject: [PATCH] Add flush and reopen to notmuch database + python bindings. Flush and reopen methods in the libnotmuch and python bindings allows concurent access to the database. Flush allows read_write databases to commit their changes, forcing a flush to disk and reopen allows a database to reread from the disk when changes are detected or known about. --- bindings/python/notmuch/database.py | 10 ++++++++++ lib/database.cc | 28 ++++++++++++++++++++++++---- lib/notmuch.h | 8 ++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py index f18ca14..b6c1c31 100644 --- a/bindings/python/notmuch/database.py +++ b/bindings/python/notmuch/database.py @@ -432,6 +432,16 @@ class Database(object): return Query(self, querystring) + def reopen(self): + """Reopens a read only database, when the data has changed, this is required.""" + if self._db is not None: + nmlib.notmuch_database_reopen(self._db) + + def flush(self): + """Flushes the search database, only available when in read-write.""" + if self._db is not None: + nmlib.notmuch_database_flush(self._db) + def __repr__(self): return "'Notmuch DB " + self.get_path() + "'" diff --git a/lib/database.cc b/lib/database.cc index 9c2f4ec..9258137 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -700,18 +700,38 @@ notmuch_database_open (const char *path, } void -notmuch_database_close (notmuch_database_t *notmuch) +notmuch_database_reopen (notmuch_database_t *notmuch) +{ + try { + notmuch->xapian_db->reopen (); + } catch (const Xapian::Error &error) { + if (! notmuch->exception_reported) { + fprintf (stderr, "Error: A Xapian exception reopening database: %s\n", + error.get_msg().c_str()); + notmuch->exception_reported = TRUE; + } + } +} + +void +notmuch_database_flush (notmuch_database_t *notmuch) { try { if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE) (static_cast (notmuch->xapian_db))->flush (); - } catch (const Xapian::Error &error) { + } catch (const Xapian::Error &error) { if (! notmuch->exception_reported) { - fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n", - error.get_msg().c_str()); + fprintf (stderr, "Error: A Xapian exception flushing database: %s\n", + error.get_msg().c_str()); + notmuch->exception_reported = TRUE; } } +} +void +notmuch_database_close (notmuch_database_t *notmuch) +{ + notmuch_database_flush (notmuch); delete notmuch->term_gen; delete notmuch->query_parser; delete notmuch->xapian_db; diff --git a/lib/notmuch.h b/lib/notmuch.h index 974be8d..ca87b89 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -171,6 +171,14 @@ notmuch_database_t * notmuch_database_open (const char *path, notmuch_database_mode_t mode); +/* Reopen a read_only database when the data source has changed */ +void +notmuch_database_reopen (notmuch_database_t *database); + +/* Force a flush of the xapian database, also done on close */ +void +notmuch_database_flush (notmuch_database_t *database); + /* Close the given notmuch database, freeing all associated * resources. See notmuch_database_open. */ void -- 1.7.1