[PATCH 2/4] Add log component to database struct.

david at tethera.net david at tethera.net
Sun Oct 24 14:01:04 PDT 2010


From: David Bremner <bremner at unb.ca>

- add accessor required because the notmuch database struct is opaque

- add a function notmuch_database_open_log to open a log file and
associate it with an open database.  The reasoning is that this is
preferable to breaking the notmuch_database_open API at this point.
---
 lib/database-private.h |    4 +++-
 lib/database.cc        |   36 ++++++++++++++++++++++++++++++++++++
 lib/notmuch.h          |   12 ++++++++++++
 3 files changed, 51 insertions(+), 1 deletions(-)

diff --git a/lib/database-private.h b/lib/database-private.h
index bd72f67..da4a72c 100644
--- a/lib/database-private.h
+++ b/lib/database-private.h
@@ -31,7 +31,7 @@
 #include <inttypes.h>
 
 #include "notmuch-private.h"
-
+#include "log-private.h"
 #include <xapian.h>
 
 struct _notmuch_database {
@@ -39,6 +39,8 @@ struct _notmuch_database {
 
     char *path;
 
+    notmuch_log_t *log;
+
     notmuch_bool_t needs_upgrade;
     notmuch_database_mode_t mode;
     Xapian::Database *xapian_db;
diff --git a/lib/database.cc b/lib/database.cc
index e4ac970..d453a0f 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -683,6 +683,10 @@ notmuch_database_open (const char *path,
 	    prefix_t *prefix = &PROBABILISTIC_PREFIX[i];
 	    notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
 	}
+
+	/* by default, logging is disabled */
+	notmuch->log = NULL;
+
     } catch (const Xapian::Error &error) {
 	fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
 		 error.get_msg().c_str());
@@ -718,12 +722,44 @@ notmuch_database_close (notmuch_database_t *notmuch)
     talloc_free (notmuch);
 }
 
+/* Attempt to open a log file in the same location as the xapian
+ * database.
+ *
+ * Caller should pass an open notmuch database to it.
+ */
+
+notmuch_status_t
+notmuch_database_open_log (notmuch_database_t *notmuch)
+{
+
+    char *log_path;
+
+    log_path = talloc_asprintf(notmuch, "%s/.notmuch/log",
+			       notmuch_database_get_path (notmuch));
+
+    if (log_path == NULL)
+	return NOTMUCH_STATUS_OUT_OF_MEMORY;
+
+    notmuch->log =  notmuch_log_open (notmuch, log_path, NOTMUCH_LOG_BUFFER_LINE);
+    if (notmuch->log == NULL)
+	return NOTMUCH_STATUS_FILE_ERROR;
+
+    talloc_free(log_path);
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 const char *
 notmuch_database_get_path (notmuch_database_t *notmuch)
 {
     return notmuch->path;
 }
 
+notmuch_log_t *
+notmuch_database_get_log (notmuch_database_t *notmuch)
+{
+    return notmuch->log;
+}
+
 unsigned int
 notmuch_database_get_version (notmuch_database_t *notmuch)
 {
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 1da84aa..54d839a 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -171,6 +171,14 @@ typedef enum {
 notmuch_database_t *
 notmuch_database_open (const char *path,
 		       notmuch_database_mode_t mode);
+/*
+ * Open the log file associated with a database.
+ *
+ * Caller should pass an open database.
+ */
+
+notmuch_status_t
+notmuch_database_open_log (notmuch_database_t *notmuch);
 
 /* Close the given notmuch database, freeing all associated
  * resources. See notmuch_database_open. */
@@ -188,6 +196,10 @@ notmuch_database_get_path (notmuch_database_t *database);
 unsigned int
 notmuch_database_get_version (notmuch_database_t *database);
 
+/* Return the log descriptor of the current database; NULL if no log is open */
+notmuch_log_t *
+notmuch_database_get_log (notmuch_database_t *database);
+
 /* Does this database need to be upgraded before writing to it?
  *
  * If this function returns TRUE then no functions that modify the
-- 
1.7.1



More information about the notmuch mailing list