[notmuch] [PATCH 1/3] notmuch: New function to retrieve all tags from the database.
[notmuch-archives.git] / 7f / 84b28175c6d3a9a740022ceeb4f011e9c59b9c
1 Return-Path: <jan@ryngle.com>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id C30BC431FC0\r
6         for <notmuch@notmuchmail.org>; Sun, 22 Nov 2009 16:11:23 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id Qaah+-OFysrk for <notmuch@notmuchmail.org>;\r
11         Sun, 22 Nov 2009 16:11:23 -0800 (PST)\r
12 Received: from mail-fx0-f214.google.com (mail-fx0-f214.google.com\r
13         [209.85.220.214])\r
14         by olra.theworths.org (Postfix) with ESMTP id C1B88431FBF\r
15         for <notmuch@notmuchmail.org>; Sun, 22 Nov 2009 16:11:22 -0800 (PST)\r
16 Received: by fxm6 with SMTP id 6so5354637fxm.0\r
17         for <notmuch@notmuchmail.org>; Sun, 22 Nov 2009 16:11:21 -0800 (PST)\r
18 Received: by 10.103.21.9 with SMTP id y9mr1894373mui.116.1258935081750;\r
19         Sun, 22 Nov 2009 16:11:21 -0800 (PST)\r
20 Received: from x61s.janakj (r2c34.net.upc.cz [62.245.66.34])\r
21         by mx.google.com with ESMTPS id j6sm14326522mue.12.2009.11.22.16.11.20\r
22         (version=TLSv1/SSLv3 cipher=RC4-MD5);\r
23         Sun, 22 Nov 2009 16:11:20 -0800 (PST)\r
24 Received: by x61s.janakj (Postfix, from userid 1000)\r
25         id F2A42440651; Mon, 23 Nov 2009 01:10:56 +0100 (CET)\r
26 From: Jan Janak <jan@ryngle.com>\r
27 To: notmuch@notmuchmail.org\r
28 Date: Mon, 23 Nov 2009 01:10:54 +0100\r
29 Message-Id: <1258935056-9746-1-git-send-email-jan@ryngle.com>\r
30 X-Mailer: git-send-email 1.6.3.3\r
31 Subject: [notmuch] [PATCH 1/3] notmuch: New function to retrieve all tags\r
32         from the database.\r
33 X-BeenThere: notmuch@notmuchmail.org\r
34 X-Mailman-Version: 2.1.12\r
35 Precedence: list\r
36 List-Id: "Use and development of the notmuch mail system."\r
37         <notmuch.notmuchmail.org>\r
38 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
39         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
40 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
41 List-Post: <mailto:notmuch@notmuchmail.org>\r
42 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
43 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
45 X-List-Received-Date: Mon, 23 Nov 2009 00:11:24 -0000\r
46 \r
47 This patch adds a new function called notmuch_database_get_all_tags\r
48 which can be used to obtain a list of all tags from the database\r
49 (in other words, the list contains all tags from all messages). The\r
50 function produces an alphabetically sorted list.\r
51 \r
52 To add support for the new function, we rip the guts off of\r
53 notmuch_message_get_tags and put them in a new generic function\r
54 called _notmuch_convert_tags. The generic function takes a\r
55 Xapian::TermIterator as argument and uses the iterator to find tags.\r
56 This makes the function usable with different Xapian objects.\r
57 \r
58 Function notmuch_message_get_tags is then reimplemented to call the\r
59 generic function with message->doc.termlist_begin() as argument.\r
60 \r
61 Similarly, we implement notmuch_message_database_get_all_tags, the\r
62 function calls the generic function with db->xapian_db->allterms_begin()\r
63 as argument.\r
64 \r
65 Finally, notmuch_database_get_all_tags is exported through\r
66 lib/notmuch.h\r
67 \r
68 Signed-off-by: Jan Janak <jan@ryngle.com>\r
69 ---\r
70  lib/database-private.h |   13 +++++++++++++\r
71  lib/database.cc        |   43 +++++++++++++++++++++++++++++++++++++++++++\r
72  lib/message.cc         |   34 +++-------------------------------\r
73  lib/notmuch.h          |   10 ++++++++++\r
74  4 files changed, 69 insertions(+), 31 deletions(-)\r
75 \r
76 diff --git a/lib/database-private.h b/lib/database-private.h\r
77 index 5f178f3..37f9bf8 100644\r
78 --- a/lib/database-private.h\r
79 +++ b/lib/database-private.h\r
80 @@ -34,4 +34,17 @@ struct _notmuch_database {\r
81      Xapian::TermGenerator *term_gen;\r
82  };\r
83  \r
84 +/* Convert tags from Xapian internal format to notmuch format.\r
85 + *\r
86 + * The function gets a TermIterator as argument and uses that iterator to find\r
87 + * all tag terms in the object. The tags are then converted to a\r
88 + * notmuch_tags_t list and returned. The function needs to allocate memory for\r
89 + * the resulting list and it uses the argument ctx as talloc context.\r
90 + *\r
91 + * The function returns NULL on failure.\r
92 + */\r
93 +notmuch_tags_t *\r
94 +_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,\r
95 +                      Xapian::TermIterator &end);\r
96 +\r
97  #endif\r
98 diff --git a/lib/database.cc b/lib/database.cc\r
99 index 2c90019..5d2add7 100644\r
100 --- a/lib/database.cc\r
101 +++ b/lib/database.cc\r
102 @@ -1026,3 +1026,46 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
103  \r
104      return ret;\r
105  }\r
106 +\r
107 +notmuch_tags_t *\r
108 +_notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,\r
109 +                      Xapian::TermIterator &end)\r
110 +{\r
111 +    const char *prefix = _find_prefix ("tag");\r
112 +    notmuch_tags_t *tags;\r
113 +    std::string tag;\r
114 +\r
115 +    /* Currently this iteration is written with the assumption that\r
116 +     * "tag" has a single-character prefix. */\r
117 +    assert (strlen (prefix) == 1);\r
118 +\r
119 +    tags = _notmuch_tags_create (ctx);\r
120 +    if (unlikely (tags == NULL))\r
121 +       return NULL;\r
122 +\r
123 +    i.skip_to (prefix);\r
124 +\r
125 +    while (i != end) {\r
126 +       tag = *i;\r
127 +\r
128 +       if (tag.empty () || tag[0] != *prefix)\r
129 +           break;\r
130 +\r
131 +       _notmuch_tags_add_tag (tags, tag.c_str () + 1);\r
132 +\r
133 +       i++;\r
134 +    }\r
135 +\r
136 +    _notmuch_tags_prepare_iterator (tags);\r
137 +\r
138 +    return tags;\r
139 +}\r
140 +\r
141 +notmuch_tags_t *\r
142 +notmuch_database_get_all_tags (notmuch_database_t *db)\r
143 +{\r
144 +    Xapian::TermIterator i, end;\r
145 +    i = db->xapian_db->allterms_begin();\r
146 +    end = db->xapian_db->allterms_end();\r
147 +    return _notmuch_convert_tags(db, i, end);\r
148 +}\r
149 diff --git a/lib/message.cc b/lib/message.cc\r
150 index 017c47b..d27ea92 100644\r
151 --- a/lib/message.cc\r
152 +++ b/lib/message.cc\r
153 @@ -459,38 +459,10 @@ notmuch_message_get_date (notmuch_message_t *message)\r
154  notmuch_tags_t *\r
155  notmuch_message_get_tags (notmuch_message_t *message)\r
156  {\r
157 -    const char *prefix = _find_prefix ("tag");\r
158      Xapian::TermIterator i, end;\r
159 -    notmuch_tags_t *tags;\r
160 -    std::string tag;\r
161 -\r
162 -    /* Currently this iteration is written with the assumption that\r
163 -     * "tag" has a single-character prefix. */\r
164 -    assert (strlen (prefix) == 1);\r
165 -\r
166 -    tags = _notmuch_tags_create (message);\r
167 -    if (unlikely (tags == NULL))\r
168 -       return NULL;\r
169 -\r
170 -    i = message->doc.termlist_begin ();\r
171 -    end = message->doc.termlist_end ();\r
172 -\r
173 -    i.skip_to (prefix);\r
174 -\r
175 -    while (i != end) {\r
176 -       tag = *i;\r
177 -\r
178 -       if (tag.empty () || tag[0] != *prefix)\r
179 -           break;\r
180 -\r
181 -       _notmuch_tags_add_tag (tags, tag.c_str () + 1);\r
182 -\r
183 -       i++;\r
184 -    }\r
185 -\r
186 -    _notmuch_tags_prepare_iterator (tags);\r
187 -\r
188 -    return tags;\r
189 +    i = message->doc.termlist_begin();\r
190 +    end = message->doc.termlist_end();\r
191 +    return _notmuch_convert_tags(message, i, end);\r
192  }\r
193  \r
194  void\r
195 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
196 index a61cd02..e2f1398 100644\r
197 --- a/lib/notmuch.h\r
198 +++ b/lib/notmuch.h\r
199 @@ -280,6 +280,16 @@ notmuch_message_t *\r
200  notmuch_database_find_message (notmuch_database_t *database,\r
201                                const char *message_id);\r
202  \r
203 +/* Return a list of all tags found in the database.\r
204 + *\r
205 + * This function creates a list of all tags found in the database. The\r
206 + * resulting list contains all tags from all messages found in the database.\r
207 + *\r
208 + * On error this function returns NULL.\r
209 + */\r
210 +notmuch_tags_t *\r
211 +notmuch_database_get_all_tags (notmuch_database_t *db);\r
212 +\r
213  /* Create a new query for 'database'.\r
214   *\r
215   * Here, 'database' should be an open database, (see\r
216 -- \r
217 1.6.3.3\r
218 \r