]> git.notmuchmail.org Git - notmuch/commitdiff
notmuch: New function to retrieve all tags from the database.
authorJan Janak <jan@ryngle.com>
Mon, 23 Nov 2009 00:10:54 +0000 (01:10 +0100)
committerCarl Worth <cworth@cworth.org>
Thu, 26 Nov 2009 15:01:52 +0000 (07:01 -0800)
This patch adds a new function called notmuch_database_get_all_tags
which can be used to obtain a list of all tags from the database
(in other words, the list contains 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
Xapian::TermIterator as argument and uses the iterator to find tags.
This makes the function usable with different Xapian objects.

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_get_all_tags, the
function calls the generic function with db->xapian_db->allterms_begin()
as argument.

Finally, notmuch_database_get_all_tags is exported through
lib/notmuch.h

Signed-off-by: Jan Janak <jan@ryngle.com>
lib/database-private.h
lib/database.cc
lib/message.cc
lib/notmuch.h

index 431966fbd7de14d4a5297cefe0e8ca75d8a6f929..643b0507dc26c0500d980993107d9d7e7b911fe1 100644 (file)
@@ -35,4 +35,17 @@ struct _notmuch_database {
     Xapian::ValueRangeProcessor *value_range_processor;
 };
 
     Xapian::ValueRangeProcessor *value_range_processor;
 };
 
+/* Convert tags from Xapian internal format to notmuch format.
+ *
+ * The function gets a TermIterator as argument and uses that iterator to find
+ * all tag terms in the object. The tags are then converted to a
+ * notmuch_tags_t list and returned. The function needs to allocate memory for
+ * the resulting list and it uses the argument ctx as talloc context.
+ *
+ * The function returns NULL on failure.
+ */
+notmuch_tags_t *
+_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,
+                      Xapian::TermIterator &end);
+
 #endif
 #endif
index 3fe12dd50ee65af6d709edc511229a5e9f047e8f..23ddd4ae1314b2186b7839364b06cc0d25b5a575 100644 (file)
@@ -1029,3 +1029,46 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
     return ret;
 }
 
     return ret;
 }
+
+notmuch_tags_t *
+_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,
+                      Xapian::TermIterator &end)
+{
+    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 (i != end) {
+       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;
+}
+
+notmuch_tags_t *
+notmuch_database_get_all_tags (notmuch_database_t *db)
+{
+    Xapian::TermIterator i, end;
+    i = db->xapian_db->allterms_begin();
+    end = db->xapian_db->allterms_end();
+    return _notmuch_convert_tags(db, i, end);
+}
index 1e325e23f69fba316f21deeb39cff0f7935e78fc..a410394d3d5ac8c2d7772a6b3d1d65762c24ed85 100644 (file)
@@ -463,38 +463,10 @@ notmuch_message_get_date (notmuch_message_t *message)
 notmuch_tags_t *
 notmuch_message_get_tags (notmuch_message_t *message)
 {
 notmuch_tags_t *
 notmuch_message_get_tags (notmuch_message_t *message)
 {
-    const char *prefix = _find_prefix ("tag");
     Xapian::TermIterator i, end;
     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 (i != end) {
-       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;
+    i = message->doc.termlist_begin();
+    end = message->doc.termlist_end();
+    return _notmuch_convert_tags(message, i, end);
 }
 
 void
 }
 
 void
index 8bba442f62b3505a8c68d7b6a03a388daca44a7c..c05e802d584e798d9a78c9802bb0b8b8f7e4acb9 100644 (file)
@@ -280,6 +280,16 @@ notmuch_message_t *
 notmuch_database_find_message (notmuch_database_t *database,
                               const char *message_id);
 
 notmuch_database_find_message (notmuch_database_t *database,
                               const char *message_id);
 
+/* Return a list of all tags found in the database.
+ *
+ * This function creates a list of all tags found in the database. The
+ * resulting list contains all tags from all messages found in the database.
+ *
+ * On error this function returns NULL.
+ */
+notmuch_tags_t *
+notmuch_database_get_all_tags (notmuch_database_t *db);
+
 /* Create a new query for 'database'.
  *
  * Here, 'database' should be an open database, (see
 /* Create a new query for 'database'.
  *
  * Here, 'database' should be an open database, (see