[PATCH v4 03/13] lib: refactor folder term update after filename removal
[notmuch-archives.git] / 0a / 6d18f07cef64a5656ecbd8ee659ed794f934e2
1 Return-Path: <jani@nikula.org>\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 20883431FDA\r
6         for <notmuch@notmuchmail.org>; Sun,  9 Mar 2014 14:40:57 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id mLZpxZ7aTQnc for <notmuch@notmuchmail.org>;\r
17         Sun,  9 Mar 2014 14:40:49 -0700 (PDT)\r
18 Received: from mail-lb0-f169.google.com (mail-lb0-f169.google.com\r
19         [209.85.217.169]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 90772431FC0\r
22         for <notmuch@notmuchmail.org>; Sun,  9 Mar 2014 14:40:46 -0700 (PDT)\r
23 Received: by mail-lb0-f169.google.com with SMTP id l4so4121151lbv.14\r
24         for <notmuch@notmuchmail.org>; Sun, 09 Mar 2014 14:40:45 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=1e100.net; s=20130820;\r
27         h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
28         :references:in-reply-to:references;\r
29         bh=DRhH5Pt/0/QzLlQI4FG2Gr6Q/xv07wQQRvopzTcguxE=;\r
30         b=jhacgN59421YKFKaFOqIAiSnLtTHRPS8k1SwUEaCvohncS4FeKMNBRcWVPKiFEUd25\r
31         AAM6l+WOR6wnAJlVw+xLzDgkBpX/EAUQQl0xG8mScfizVJ/XiXV5UqtAoxG6NSPbae34\r
32         +wCWWonxmrxHDohHqvDsK4NGnuHoMrAxVVh5NbE3AWjjs/LWYhRRv9EiyTScrdlkJ1xq\r
33         vCi58DsREFnAcU1+5LafVbWwPukV++CRi4Pg2ucRfB7BSDH69nS2PheSUSSLJiWjXahi\r
34         QsLV/WBuCWdHlSGBnZgdvYIK0pbd+glZ/jmzpYJmCK6CzryvEoMGaIURa9TeBjSvFmrN\r
35         ELjQ==\r
36 X-Gm-Message-State:\r
37  ALoCoQmHiHEEmX53PeqT4UfTECP2nvv9ipkwbelpwUmL6zYYu050mCNDeB/BtFYmlVh3+3KvTE5M\r
38 X-Received: by 10.112.205.5 with SMTP id lc5mr4464lbc.40.1394401245092;\r
39         Sun, 09 Mar 2014 14:40:45 -0700 (PDT)\r
40 Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
41         [88.195.111.91]) by mx.google.com with ESMTPSA id\r
42         wv4sm10382957lbb.10.2014.03.09.14.40.43 for <multiple recipients>\r
43         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
44         Sun, 09 Mar 2014 14:40:44 -0700 (PDT)\r
45 From: Jani Nikula <jani@nikula.org>\r
46 To: notmuch@notmuchmail.org\r
47 Subject: [PATCH v4 03/13] lib: refactor folder term update after filename\r
48         removal\r
49 Date: Sun,  9 Mar 2014 23:40:24 +0200\r
50 Message-Id:\r
51  <54310c07d86909b8f1e087261b9d052968803325.1394400503.git.jani@nikula.org>\r
52 X-Mailer: git-send-email 1.9.0\r
53 In-Reply-To: <cover.1394400503.git.jani@nikula.org>\r
54 References: <cover.1394400503.git.jani@nikula.org>\r
55 In-Reply-To: <cover.1394400503.git.jani@nikula.org>\r
56 References: <cover.1394400503.git.jani@nikula.org>\r
57 X-BeenThere: notmuch@notmuchmail.org\r
58 X-Mailman-Version: 2.1.13\r
59 Precedence: list\r
60 List-Id: "Use and development of the notmuch mail system."\r
61         <notmuch.notmuchmail.org>\r
62 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
63         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
64 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
65 List-Post: <mailto:notmuch@notmuchmail.org>\r
66 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
67 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
68         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
69 X-List-Received-Date: Sun, 09 Mar 2014 21:40:57 -0000\r
70 \r
71 Abstract some blocks of code for reuse. No functional changes.\r
72 ---\r
73  lib/message.cc | 135 ++++++++++++++++++++++++++++-----------------------------\r
74  1 file changed, 66 insertions(+), 69 deletions(-)\r
75 \r
76 diff --git a/lib/message.cc b/lib/message.cc\r
77 index c91f3a59836f..7aff4ae5111a 100644\r
78 --- a/lib/message.cc\r
79 +++ b/lib/message.cc\r
80 @@ -481,6 +481,68 @@ notmuch_message_get_replies (notmuch_message_t *message)\r
81      return _notmuch_messages_create (message->replies);\r
82  }\r
83  \r
84 +static void\r
85 +_notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)\r
86 +{\r
87 +    Xapian::TermIterator i;\r
88 +    size_t prefix_len = strlen (prefix);\r
89 +\r
90 +    while (1) {\r
91 +       i = message->doc.termlist_begin ();\r
92 +       i.skip_to (prefix);\r
93 +\r
94 +       /* Terminate loop when no terms remain with desired prefix. */\r
95 +       if (i == message->doc.termlist_end () ||\r
96 +           strncmp ((*i).c_str (), prefix, prefix_len))\r
97 +           break;\r
98 +\r
99 +       try {\r
100 +           message->doc.remove_term ((*i));\r
101 +       } catch (const Xapian::InvalidArgumentError) {\r
102 +           /* Ignore failure to remove non-existent term. */\r
103 +       }\r
104 +    }\r
105 +}\r
106 +\r
107 +/* Add directory based terms for all filenames of the message. */\r
108 +static notmuch_status_t\r
109 +_notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)\r
110 +{\r
111 +    const char *direntry_prefix = _find_prefix ("file-direntry");\r
112 +    int direntry_prefix_len = strlen (direntry_prefix);\r
113 +    Xapian::TermIterator i = message->doc.termlist_begin ();\r
114 +    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
115 +\r
116 +    for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {\r
117 +       unsigned int directory_id;\r
118 +       const char *direntry, *directory;\r
119 +       char *colon;\r
120 +\r
121 +       /* Terminate loop at first term without desired prefix. */\r
122 +       if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))\r
123 +           break;\r
124 +\r
125 +       /* Indicate that there are filenames remaining. */\r
126 +       status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
127 +\r
128 +       direntry = (*i).c_str ();\r
129 +       direntry += direntry_prefix_len;\r
130 +\r
131 +       directory_id = strtol (direntry, &colon, 10);\r
132 +\r
133 +       if (colon == NULL || *colon != ':')\r
134 +           INTERNAL_ERROR ("malformed direntry");\r
135 +\r
136 +       directory = _notmuch_database_get_directory_path (ctx,\r
137 +                                                         message->notmuch,\r
138 +                                                         directory_id);\r
139 +       if (strlen (directory))\r
140 +           _notmuch_message_gen_terms (message, "folder", directory);\r
141 +    }\r
142 +\r
143 +    return status;\r
144 +}\r
145 +\r
146  /* Add an additional 'filename' for 'message'.\r
147   *\r
148   * This change will not be reflected in the database until the next\r
149 @@ -536,17 +598,12 @@ notmuch_status_t\r
150  _notmuch_message_remove_filename (notmuch_message_t *message,\r
151                                   const char *filename)\r
152  {\r
153 -    const char *direntry_prefix = _find_prefix ("file-direntry");\r
154 -    int direntry_prefix_len = strlen (direntry_prefix);\r
155 -    const char *folder_prefix = _find_prefix ("folder");\r
156 -    int folder_prefix_len = strlen (folder_prefix);\r
157      void *local = talloc_new (message);\r
158 +    const char *folder_prefix = _find_prefix ("folder");\r
159      char *zfolder_prefix = talloc_asprintf(local, "Z%s", folder_prefix);\r
160 -    int zfolder_prefix_len = strlen (zfolder_prefix);\r
161      char *direntry;\r
162      notmuch_private_status_t private_status;\r
163      notmuch_status_t status;\r
164 -    Xapian::TermIterator i, last;\r
165  \r
166      status = _notmuch_database_filename_to_direntry (\r
167         local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);\r
168 @@ -567,73 +624,13 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
169       *  3. adding back terms for all remaining filenames of the message. */\r
170  \r
171      /* 1. removing all "folder:" terms */\r
172 -    while (1) {\r
173 -       i = message->doc.termlist_begin ();\r
174 -       i.skip_to (folder_prefix);\r
175 -\r
176 -       /* Terminate loop when no terms remain with desired prefix. */\r
177 -       if (i == message->doc.termlist_end () ||\r
178 -           strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))\r
179 -       {\r
180 -           break;\r
181 -       }\r
182 -\r
183 -       try {\r
184 -           message->doc.remove_term ((*i));\r
185 -       } catch (const Xapian::InvalidArgumentError) {\r
186 -           /* Ignore failure to remove non-existent term. */\r
187 -       }\r
188 -    }\r
189 +    _notmuch_message_remove_terms (message, folder_prefix);\r
190  \r
191      /* 2. removing all "folder:" stemmed terms */\r
192 -    while (1) {\r
193 -       i = message->doc.termlist_begin ();\r
194 -       i.skip_to (zfolder_prefix);\r
195 -\r
196 -       /* Terminate loop when no terms remain with desired prefix. */\r
197 -       if (i == message->doc.termlist_end () ||\r
198 -           strncmp ((*i).c_str (), zfolder_prefix, zfolder_prefix_len))\r
199 -       {\r
200 -           break;\r
201 -       }\r
202 -\r
203 -       try {\r
204 -           message->doc.remove_term ((*i));\r
205 -       } catch (const Xapian::InvalidArgumentError) {\r
206 -           /* Ignore failure to remove non-existent term. */\r
207 -       }\r
208 -    }\r
209 +    _notmuch_message_remove_terms (message, zfolder_prefix);\r
210  \r
211      /* 3. adding back terms for all remaining filenames of the message. */\r
212 -    i = message->doc.termlist_begin ();\r
213 -    i.skip_to (direntry_prefix);\r
214 -\r
215 -    for (; i != message->doc.termlist_end (); i++) {\r
216 -       unsigned int directory_id;\r
217 -       const char *direntry, *directory;\r
218 -       char *colon;\r
219 -\r
220 -       /* Terminate loop at first term without desired prefix. */\r
221 -       if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))\r
222 -           break;\r
223 -\r
224 -       /* Indicate that there are filenames remaining. */\r
225 -       status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
226 -\r
227 -       direntry = (*i).c_str ();\r
228 -       direntry += direntry_prefix_len;\r
229 -\r
230 -       directory_id = strtol (direntry, &colon, 10);\r
231 -\r
232 -       if (colon == NULL || *colon != ':')\r
233 -           INTERNAL_ERROR ("malformed direntry");\r
234 -\r
235 -       directory = _notmuch_database_get_directory_path (local,\r
236 -                                                         message->notmuch,\r
237 -                                                         directory_id);\r
238 -       if (strlen (directory))\r
239 -           _notmuch_message_gen_terms (message, "folder", directory);\r
240 -    }\r
241 +    status = _notmuch_message_add_directory_terms (local, message);\r
242  \r
243      talloc_free (local);\r
244  \r
245 -- \r
246 1.9.0\r
247 \r