Re: [PATCH v4 08/16] reorganize indexing of multipart/signed and multipart/encrypted
[notmuch-archives.git] / 0b / f0598ca8bc4120c326e8d947be38f83ddeafaa
1 Return-Path: <bremner@tethera.net>\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 F2694431FDC\r
6         for <notmuch@notmuchmail.org>; Tue, 11 Mar 2014 16:03:14 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id ImtzEESjRaGX for <notmuch@notmuchmail.org>;\r
16         Tue, 11 Mar 2014 16:03:12 -0700 (PDT)\r
17 Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
18         (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id D31B4431FD0\r
21         for <notmuch@notmuchmail.org>; Tue, 11 Mar 2014 16:02:31 -0700 (PDT)\r
22 Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
23         (envelope-from <bremner@tethera.net>)\r
24         id 1WNVgt-0001w3-9r; Tue, 11 Mar 2014 20:02:31 -0300\r
25 Received: (nullmailer pid 25847 invoked by uid 1000); Tue, 11 Mar 2014\r
26         23:01:45 -0000\r
27 From: David Bremner <david@tethera.net>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v6 04/14] lib: refactor folder term update after filename\r
30         removal\r
31 Date: Tue, 11 Mar 2014 20:01:30 -0300\r
32 Message-Id: <1394578900-25618-5-git-send-email-david@tethera.net>\r
33 X-Mailer: git-send-email 1.8.5.3\r
34 In-Reply-To: <1394578900-25618-1-git-send-email-david@tethera.net>\r
35 References: <1394578900-25618-1-git-send-email-david@tethera.net>\r
36 X-BeenThere: notmuch@notmuchmail.org\r
37 X-Mailman-Version: 2.1.13\r
38 Precedence: list\r
39 List-Id: "Use and development of the notmuch mail system."\r
40         <notmuch.notmuchmail.org>\r
41 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
42         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
43 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
44 List-Post: <mailto:notmuch@notmuchmail.org>\r
45 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
46 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
47         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
48 X-List-Received-Date: Tue, 11 Mar 2014 23:03:15 -0000\r
49 \r
50 From: Jani Nikula <jani@nikula.org>\r
51 \r
52 Abstract some blocks of code for reuse. No functional changes.\r
53 ---\r
54  lib/message.cc | 135 ++++++++++++++++++++++++++++-----------------------------\r
55  1 file changed, 66 insertions(+), 69 deletions(-)\r
56 \r
57 diff --git a/lib/message.cc b/lib/message.cc\r
58 index c91f3a5..7aff4ae 100644\r
59 --- a/lib/message.cc\r
60 +++ b/lib/message.cc\r
61 @@ -481,6 +481,68 @@ notmuch_message_get_replies (notmuch_message_t *message)\r
62      return _notmuch_messages_create (message->replies);\r
63  }\r
64  \r
65 +static void\r
66 +_notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)\r
67 +{\r
68 +    Xapian::TermIterator i;\r
69 +    size_t prefix_len = strlen (prefix);\r
70 +\r
71 +    while (1) {\r
72 +       i = message->doc.termlist_begin ();\r
73 +       i.skip_to (prefix);\r
74 +\r
75 +       /* Terminate loop when no terms remain with desired prefix. */\r
76 +       if (i == message->doc.termlist_end () ||\r
77 +           strncmp ((*i).c_str (), prefix, prefix_len))\r
78 +           break;\r
79 +\r
80 +       try {\r
81 +           message->doc.remove_term ((*i));\r
82 +       } catch (const Xapian::InvalidArgumentError) {\r
83 +           /* Ignore failure to remove non-existent term. */\r
84 +       }\r
85 +    }\r
86 +}\r
87 +\r
88 +/* Add directory based terms for all filenames of the message. */\r
89 +static notmuch_status_t\r
90 +_notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)\r
91 +{\r
92 +    const char *direntry_prefix = _find_prefix ("file-direntry");\r
93 +    int direntry_prefix_len = strlen (direntry_prefix);\r
94 +    Xapian::TermIterator i = message->doc.termlist_begin ();\r
95 +    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
96 +\r
97 +    for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {\r
98 +       unsigned int directory_id;\r
99 +       const char *direntry, *directory;\r
100 +       char *colon;\r
101 +\r
102 +       /* Terminate loop at first term without desired prefix. */\r
103 +       if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))\r
104 +           break;\r
105 +\r
106 +       /* Indicate that there are filenames remaining. */\r
107 +       status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
108 +\r
109 +       direntry = (*i).c_str ();\r
110 +       direntry += direntry_prefix_len;\r
111 +\r
112 +       directory_id = strtol (direntry, &colon, 10);\r
113 +\r
114 +       if (colon == NULL || *colon != ':')\r
115 +           INTERNAL_ERROR ("malformed direntry");\r
116 +\r
117 +       directory = _notmuch_database_get_directory_path (ctx,\r
118 +                                                         message->notmuch,\r
119 +                                                         directory_id);\r
120 +       if (strlen (directory))\r
121 +           _notmuch_message_gen_terms (message, "folder", directory);\r
122 +    }\r
123 +\r
124 +    return status;\r
125 +}\r
126 +\r
127  /* Add an additional 'filename' for 'message'.\r
128   *\r
129   * This change will not be reflected in the database until the next\r
130 @@ -536,17 +598,12 @@ notmuch_status_t\r
131  _notmuch_message_remove_filename (notmuch_message_t *message,\r
132                                   const char *filename)\r
133  {\r
134 -    const char *direntry_prefix = _find_prefix ("file-direntry");\r
135 -    int direntry_prefix_len = strlen (direntry_prefix);\r
136 -    const char *folder_prefix = _find_prefix ("folder");\r
137 -    int folder_prefix_len = strlen (folder_prefix);\r
138      void *local = talloc_new (message);\r
139 +    const char *folder_prefix = _find_prefix ("folder");\r
140      char *zfolder_prefix = talloc_asprintf(local, "Z%s", folder_prefix);\r
141 -    int zfolder_prefix_len = strlen (zfolder_prefix);\r
142      char *direntry;\r
143      notmuch_private_status_t private_status;\r
144      notmuch_status_t status;\r
145 -    Xapian::TermIterator i, last;\r
146  \r
147      status = _notmuch_database_filename_to_direntry (\r
148         local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);\r
149 @@ -567,73 +624,13 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
150       *  3. adding back terms for all remaining filenames of the message. */\r
151  \r
152      /* 1. removing all "folder:" terms */\r
153 -    while (1) {\r
154 -       i = message->doc.termlist_begin ();\r
155 -       i.skip_to (folder_prefix);\r
156 -\r
157 -       /* Terminate loop when no terms remain with desired prefix. */\r
158 -       if (i == message->doc.termlist_end () ||\r
159 -           strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))\r
160 -       {\r
161 -           break;\r
162 -       }\r
163 -\r
164 -       try {\r
165 -           message->doc.remove_term ((*i));\r
166 -       } catch (const Xapian::InvalidArgumentError) {\r
167 -           /* Ignore failure to remove non-existent term. */\r
168 -       }\r
169 -    }\r
170 +    _notmuch_message_remove_terms (message, folder_prefix);\r
171  \r
172      /* 2. removing all "folder:" stemmed terms */\r
173 -    while (1) {\r
174 -       i = message->doc.termlist_begin ();\r
175 -       i.skip_to (zfolder_prefix);\r
176 -\r
177 -       /* Terminate loop when no terms remain with desired prefix. */\r
178 -       if (i == message->doc.termlist_end () ||\r
179 -           strncmp ((*i).c_str (), zfolder_prefix, zfolder_prefix_len))\r
180 -       {\r
181 -           break;\r
182 -       }\r
183 -\r
184 -       try {\r
185 -           message->doc.remove_term ((*i));\r
186 -       } catch (const Xapian::InvalidArgumentError) {\r
187 -           /* Ignore failure to remove non-existent term. */\r
188 -       }\r
189 -    }\r
190 +    _notmuch_message_remove_terms (message, zfolder_prefix);\r
191  \r
192      /* 3. adding back terms for all remaining filenames of the message. */\r
193 -    i = message->doc.termlist_begin ();\r
194 -    i.skip_to (direntry_prefix);\r
195 -\r
196 -    for (; i != message->doc.termlist_end (); i++) {\r
197 -       unsigned int directory_id;\r
198 -       const char *direntry, *directory;\r
199 -       char *colon;\r
200 -\r
201 -       /* Terminate loop at first term without desired prefix. */\r
202 -       if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))\r
203 -           break;\r
204 -\r
205 -       /* Indicate that there are filenames remaining. */\r
206 -       status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
207 -\r
208 -       direntry = (*i).c_str ();\r
209 -       direntry += direntry_prefix_len;\r
210 -\r
211 -       directory_id = strtol (direntry, &colon, 10);\r
212 -\r
213 -       if (colon == NULL || *colon != ':')\r
214 -           INTERNAL_ERROR ("malformed direntry");\r
215 -\r
216 -       directory = _notmuch_database_get_directory_path (local,\r
217 -                                                         message->notmuch,\r
218 -                                                         directory_id);\r
219 -       if (strlen (directory))\r
220 -           _notmuch_message_gen_terms (message, "folder", directory);\r
221 -    }\r
222 +    status = _notmuch_message_add_directory_terms (local, message);\r
223  \r
224      talloc_free (local);\r
225  \r
226 -- \r
227 1.8.5.3\r
228 \r