[notmuch] [PATCH 1/3] 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)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:35:40 +0000 (09:35 -0800)
7f/84b28175c6d3a9a740022ceeb4f011e9c59b9c [new file with mode: 0644]

diff --git a/7f/84b28175c6d3a9a740022ceeb4f011e9c59b9c b/7f/84b28175c6d3a9a740022ceeb4f011e9c59b9c
new file mode 100644 (file)
index 0000000..3b62347
--- /dev/null
@@ -0,0 +1,218 @@
+Return-Path: <jan@ryngle.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id C30BC431FC0\r
+       for <notmuch@notmuchmail.org>; Sun, 22 Nov 2009 16:11:23 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id Qaah+-OFysrk for <notmuch@notmuchmail.org>;\r
+       Sun, 22 Nov 2009 16:11:23 -0800 (PST)\r
+Received: from mail-fx0-f214.google.com (mail-fx0-f214.google.com\r
+       [209.85.220.214])\r
+       by olra.theworths.org (Postfix) with ESMTP id C1B88431FBF\r
+       for <notmuch@notmuchmail.org>; Sun, 22 Nov 2009 16:11:22 -0800 (PST)\r
+Received: by fxm6 with SMTP id 6so5354637fxm.0\r
+       for <notmuch@notmuchmail.org>; Sun, 22 Nov 2009 16:11:21 -0800 (PST)\r
+Received: by 10.103.21.9 with SMTP id y9mr1894373mui.116.1258935081750;\r
+       Sun, 22 Nov 2009 16:11:21 -0800 (PST)\r
+Received: from x61s.janakj (r2c34.net.upc.cz [62.245.66.34])\r
+       by mx.google.com with ESMTPS id j6sm14326522mue.12.2009.11.22.16.11.20\r
+       (version=TLSv1/SSLv3 cipher=RC4-MD5);\r
+       Sun, 22 Nov 2009 16:11:20 -0800 (PST)\r
+Received: by x61s.janakj (Postfix, from userid 1000)\r
+       id F2A42440651; Mon, 23 Nov 2009 01:10:56 +0100 (CET)\r
+From: Jan Janak <jan@ryngle.com>\r
+To: notmuch@notmuchmail.org\r
+Date: Mon, 23 Nov 2009 01:10:54 +0100\r
+Message-Id: <1258935056-9746-1-git-send-email-jan@ryngle.com>\r
+X-Mailer: git-send-email 1.6.3.3\r
+Subject: [notmuch] [PATCH 1/3] notmuch: New function to retrieve all tags\r
+       from the database.\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.12\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 23 Nov 2009 00:11:24 -0000\r
+\r
+This patch adds a new function called notmuch_database_get_all_tags\r
+which can be used to obtain a list of all tags from the database\r
+(in other words, the list contains all tags from all messages). The\r
+function produces an alphabetically sorted list.\r
+\r
+To add support for the new function, we rip the guts off of\r
+notmuch_message_get_tags and put them in a new generic function\r
+called _notmuch_convert_tags. The generic function takes a\r
+Xapian::TermIterator as argument and uses the iterator to find tags.\r
+This makes the function usable with different Xapian objects.\r
+\r
+Function notmuch_message_get_tags is then reimplemented to call the\r
+generic function with message->doc.termlist_begin() as argument.\r
+\r
+Similarly, we implement notmuch_message_database_get_all_tags, the\r
+function calls the generic function with db->xapian_db->allterms_begin()\r
+as argument.\r
+\r
+Finally, notmuch_database_get_all_tags is exported through\r
+lib/notmuch.h\r
+\r
+Signed-off-by: Jan Janak <jan@ryngle.com>\r
+---\r
+ lib/database-private.h |   13 +++++++++++++\r
+ lib/database.cc        |   43 +++++++++++++++++++++++++++++++++++++++++++\r
+ lib/message.cc         |   34 +++-------------------------------\r
+ lib/notmuch.h          |   10 ++++++++++\r
+ 4 files changed, 69 insertions(+), 31 deletions(-)\r
+\r
+diff --git a/lib/database-private.h b/lib/database-private.h\r
+index 5f178f3..37f9bf8 100644\r
+--- a/lib/database-private.h\r
++++ b/lib/database-private.h\r
+@@ -34,4 +34,17 @@ struct _notmuch_database {\r
+     Xapian::TermGenerator *term_gen;\r
+ };\r
\r
++/* Convert tags from Xapian internal format to notmuch format.\r
++ *\r
++ * The function gets a TermIterator as argument and uses that iterator to find\r
++ * all tag terms in the object. The tags are then converted to a\r
++ * notmuch_tags_t list and returned. The function needs to allocate memory for\r
++ * the resulting list and it uses the argument ctx as talloc context.\r
++ *\r
++ * The function returns NULL on failure.\r
++ */\r
++notmuch_tags_t *\r
++_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,\r
++                     Xapian::TermIterator &end);\r
++\r
+ #endif\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 2c90019..5d2add7 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -1026,3 +1026,46 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
\r
+     return ret;\r
+ }\r
++\r
++notmuch_tags_t *\r
++_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,\r
++                     Xapian::TermIterator &end)\r
++{\r
++    const char *prefix = _find_prefix ("tag");\r
++    notmuch_tags_t *tags;\r
++    std::string tag;\r
++\r
++    /* Currently this iteration is written with the assumption that\r
++     * "tag" has a single-character prefix. */\r
++    assert (strlen (prefix) == 1);\r
++\r
++    tags = _notmuch_tags_create (ctx);\r
++    if (unlikely (tags == NULL))\r
++      return NULL;\r
++\r
++    i.skip_to (prefix);\r
++\r
++    while (i != end) {\r
++      tag = *i;\r
++\r
++      if (tag.empty () || tag[0] != *prefix)\r
++          break;\r
++\r
++      _notmuch_tags_add_tag (tags, tag.c_str () + 1);\r
++\r
++      i++;\r
++    }\r
++\r
++    _notmuch_tags_prepare_iterator (tags);\r
++\r
++    return tags;\r
++}\r
++\r
++notmuch_tags_t *\r
++notmuch_database_get_all_tags (notmuch_database_t *db)\r
++{\r
++    Xapian::TermIterator i, end;\r
++    i = db->xapian_db->allterms_begin();\r
++    end = db->xapian_db->allterms_end();\r
++    return _notmuch_convert_tags(db, i, end);\r
++}\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 017c47b..d27ea92 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -459,38 +459,10 @@ notmuch_message_get_date (notmuch_message_t *message)\r
+ notmuch_tags_t *\r
+ notmuch_message_get_tags (notmuch_message_t *message)\r
+ {\r
+-    const char *prefix = _find_prefix ("tag");\r
+     Xapian::TermIterator i, end;\r
+-    notmuch_tags_t *tags;\r
+-    std::string tag;\r
+-\r
+-    /* Currently this iteration is written with the assumption that\r
+-     * "tag" has a single-character prefix. */\r
+-    assert (strlen (prefix) == 1);\r
+-\r
+-    tags = _notmuch_tags_create (message);\r
+-    if (unlikely (tags == NULL))\r
+-      return NULL;\r
+-\r
+-    i = message->doc.termlist_begin ();\r
+-    end = message->doc.termlist_end ();\r
+-\r
+-    i.skip_to (prefix);\r
+-\r
+-    while (i != end) {\r
+-      tag = *i;\r
+-\r
+-      if (tag.empty () || tag[0] != *prefix)\r
+-          break;\r
+-\r
+-      _notmuch_tags_add_tag (tags, tag.c_str () + 1);\r
+-\r
+-      i++;\r
+-    }\r
+-\r
+-    _notmuch_tags_prepare_iterator (tags);\r
+-\r
+-    return tags;\r
++    i = message->doc.termlist_begin();\r
++    end = message->doc.termlist_end();\r
++    return _notmuch_convert_tags(message, i, end);\r
+ }\r
\r
+ void\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index a61cd02..e2f1398 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -280,6 +280,16 @@ notmuch_message_t *\r
+ notmuch_database_find_message (notmuch_database_t *database,\r
+                              const char *message_id);\r
\r
++/* Return a list of all tags found in the database.\r
++ *\r
++ * This function creates a list of all tags found in the database. The\r
++ * resulting list contains all tags from all messages found in the database.\r
++ *\r
++ * On error this function returns NULL.\r
++ */\r
++notmuch_tags_t *\r
++notmuch_database_get_all_tags (notmuch_database_t *db);\r
++\r
+ /* Create a new query for 'database'.\r
+  *\r
+  * Here, 'database' should be an open database, (see\r
+-- \r
+1.6.3.3\r
+\r