[notmuch] [PATCH 1/2] notmuch: Support for notmuch_database_get_tags

Jan Janak jan at ryngle.com
Thu Nov 19 03:34:40 PST 2009


This patch adds a new function called notmuch_database_get_tags which
can be used to obtain a list of all tags defined in the database (that
is, the list all tags from all messages). The function produces an
alphabetically sorted list.

To add support for the new function, we rip the guts off of
notmuch_message_get_tags and put them in a new generic function
called _notmuch_convert_tags. The generic function takes a TermIterator
as argument and produces a notmuch_tags_t list of tags.

Function notmuch_message_get_tags is then reimplemented to call the
generic function with message->doc.termlist_begin() as argument.

Similarly, we implement notmuch_message_database_tags, the function
calls the generic function with db->xapian_db->allterms_begin() as
argument.

Finally, notmuch_database_get_tags is exported through lib/notmuch.h

Signed-off-by: Jan Janak <jan at ryngle.com>
---
 lib/database.cc |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/message.cc  |   38 ++++++--------------------------------
 lib/notmuch.h   |    4 ++++
 3 files changed, 58 insertions(+), 32 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 4998fc9..b1c15c3 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -983,3 +983,51 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
     return ret;
 }
+
+notmuch_tags_t *
+_notmuch_convert_tags (void* ctx, Xapian::TermIterator i);
+
+/* Converts tags from the format used in Xapian to a list in
+   notmuch_tags_t. */
+notmuch_tags_t *
+_notmuch_convert_tags (void* ctx, Xapian::TermIterator i)
+{
+    const char *prefix = _find_prefix ("tag");
+    notmuch_tags_t *tags;
+    std::string tag;
+
+    /* Currently this iteration is written with the assumption that
+     * "tag" has a single-character prefix. */
+    assert (strlen (prefix) == 1);
+
+    tags = _notmuch_tags_create (ctx);
+    if (unlikely (tags == NULL))
+	return NULL;
+
+    i.skip_to (prefix);
+
+    while (1) {
+	tag = *i;
+
+	if (tag.empty () || tag[0] != *prefix)
+	    break;
+
+	_notmuch_tags_add_tag (tags, tag.c_str () + 1);
+
+	i++;
+    }
+
+    _notmuch_tags_prepare_iterator (tags);
+
+    return tags;
+}
+
+/*
+ * Returns a list of all tags defined in a notmuch database. The resulting
+ * list is sorted alphabetically.
+ */
+notmuch_tags_t *
+notmuch_database_get_tags (notmuch_database_t *db)
+{
+	return _notmuch_convert_tags(db, db->xapian_db->allterms_begin());
+}
diff --git a/lib/message.cc b/lib/message.cc
index 9488fb6..af23bb2 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -41,6 +41,9 @@ struct _notmuch_message {
     Xapian::Document doc;
 };
 
+extern notmuch_tags_t *
+_notmuch_convert_tags (void* ctx, Xapian::TermIterator i);
+
 /* "128 bits of thread-id ought to be enough for anybody" */
 #define NOTMUCH_THREAD_ID_BITS	 128
 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
@@ -445,43 +448,14 @@ notmuch_message_get_date (notmuch_message_t *message)
     return Xapian::sortable_unserialise (value);
 }
 
+
 notmuch_tags_t *
 notmuch_message_get_tags (notmuch_message_t *message)
 {
-    const char *prefix = _find_prefix ("tag");
-    Xapian::TermIterator i, end;
-    notmuch_tags_t *tags;
-    std::string tag;
-
-    /* Currently this iteration is written with the assumption that
-     * "tag" has a single-character prefix. */
-    assert (strlen (prefix) == 1);
-
-    tags = _notmuch_tags_create (message);
-    if (unlikely (tags == NULL))
-	return NULL;
-
-    i = message->doc.termlist_begin ();
-    end = message->doc.termlist_end ();
-
-    i.skip_to (prefix);
-
-    while (1) {
-	tag = *i;
-
-	if (tag.empty () || tag[0] != *prefix)
-	    break;
-
-	_notmuch_tags_add_tag (tags, tag.c_str () + 1);
-
-	i++;
-    }
-
-    _notmuch_tags_prepare_iterator (tags);
-
-    return tags;
+	return _notmuch_convert_tags(message, message->doc.termlist_begin());
 }
 
+
 void
 _notmuch_message_set_date (notmuch_message_t *message,
 			   const char *date)
diff --git a/lib/notmuch.h b/lib/notmuch.h
index cc713a3..1edcfd6 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -271,6 +271,10 @@ notmuch_message_t *
 notmuch_database_find_message (notmuch_database_t *database,
 			       const char *message_id);
 
+notmuch_tags_t *
+notmuch_database_get_tags (notmuch_database_t *database);
+
+
 /* Create a new query for 'database'.
  *
  * Here, 'database' should be an open database, (see
-- 
1.6.3.3



More information about the notmuch mailing list