unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jan Janak <jan@ryngle.com>
To: notmuch@notmuchmail.org
Subject: [PATCH 1/3] notmuch: New function to retrieve all tags from the database.
Date: Mon, 23 Nov 2009 01:10:54 +0100	[thread overview]
Message-ID: <1258935056-9746-1-git-send-email-jan@ryngle.com> (raw)

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 |   13 +++++++++++++
 lib/database.cc        |   43 +++++++++++++++++++++++++++++++++++++++++++
 lib/message.cc         |   34 +++-------------------------------
 lib/notmuch.h          |   10 ++++++++++
 4 files changed, 69 insertions(+), 31 deletions(-)

diff --git a/lib/database-private.h b/lib/database-private.h
index 5f178f3..37f9bf8 100644
--- a/lib/database-private.h
+++ b/lib/database-private.h
@@ -34,4 +34,17 @@ struct _notmuch_database {
     Xapian::TermGenerator *term_gen;
 };
 
+/* 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
diff --git a/lib/database.cc b/lib/database.cc
index 2c90019..5d2add7 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1026,3 +1026,46 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
     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);
+}
diff --git a/lib/message.cc b/lib/message.cc
index 017c47b..d27ea92 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -459,38 +459,10 @@ notmuch_message_get_date (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;
-    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
diff --git a/lib/notmuch.h b/lib/notmuch.h
index a61cd02..e2f1398 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -280,6 +280,16 @@ notmuch_message_t *
 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
-- 
1.6.3.3

             reply	other threads:[~2009-11-23  0:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-23  0:10 Jan Janak [this message]
2009-11-23  0:10 ` [PATCH 2/3] notmuch: New command 'search-tags' Jan Janak
2009-11-23  0:10   ` [PATCH 3/3] notmuch.el: Select tag names with completion Jan Janak
2009-11-25  4:05     ` [PATCH] notmuch.el: When removing tags, offer only those a msg/thread has set Jan Janak
2009-11-26 15:15     ` [PATCH 3/3] notmuch.el: Select tag names with completion Carl Worth
2009-11-27 11:30       ` Jan Janak
2009-11-28  4:10         ` Carl Worth
2009-11-25  3:30   ` [PATCH 1/2] lib: New function to collect tags from a list of messages Jan Janak
2009-11-25  3:30     ` [PATCH 2/2] search-tags: Add support for search-terms Jan Janak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1258935056-9746-1-git-send-email-jan@ryngle.com \
    --to=jan@ryngle.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).