Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 3966C431FBF for ; Thu, 19 Nov 2009 03:35:07 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id r5UMGTB+G9l7 for ; Thu, 19 Nov 2009 03:35:06 -0800 (PST) Received: from mail.iptel.org (smtp.iptel.org [213.192.59.67]) by olra.theworths.org (Postfix) with ESMTP id DEAC8431FAE for ; Thu, 19 Nov 2009 03:35:05 -0800 (PST) Received: by mail.iptel.org (Postfix, from userid 103) id B27F2370667; Thu, 19 Nov 2009 12:34:42 +0100 (CET) Received: from x61s.janakj (nat.sip-server.net [213.192.30.130]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mail.iptel.org (Postfix) with ESMTPSA id B9041370659 for ; Thu, 19 Nov 2009 12:34:41 +0100 (CET) Received: by x61s.janakj (Postfix, from userid 1000) id 89305440651; Thu, 19 Nov 2009 12:34:41 +0100 (CET) From: Jan Janak To: notmuch@notmuchmail.org Date: Thu, 19 Nov 2009 12:34:40 +0100 Message-Id: <1258630481-5133-1-git-send-email-jan@ryngle.com> X-Mailer: git-send-email 1.6.3.3 Subject: [notmuch] [PATCH 1/2] notmuch: Support for notmuch_database_get_tags X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Nov 2009 11:35:07 -0000 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 --- 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