unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH 1/2] notmuch: Support for notmuch_database_get_tags
@ 2009-11-19 11:34 Jan Janak
  2009-11-19 11:34 ` [PATCH 2/2] notmuch list: A new command to produce various lists Jan Janak
  0 siblings, 1 reply; 12+ messages in thread
From: Jan Janak @ 2009-11-19 11:34 UTC (permalink / raw)
  To: notmuch

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@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

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2009-11-22  3:53 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-19 11:34 [PATCH 1/2] notmuch: Support for notmuch_database_get_tags Jan Janak
2009-11-19 11:34 ` [PATCH 2/2] notmuch list: A new command to produce various lists Jan Janak
2009-11-19 11:52   ` Jan Janak
2009-11-19 14:41     ` Carl Worth
2009-11-19 15:40       ` Jan Janak
2009-11-19 17:59       ` Bdale Garbee
2009-11-20  0:26         ` Carl Worth
2009-11-21 21:07       ` Jan Janak
2009-11-21 23:07         ` Carl Worth
2009-11-21 23:21           ` Jed Brown
2009-11-22  3:53             ` Carl Worth
2009-11-22  3:45           ` Bart Trojanowski

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).