Re: [PATCH 9/9] add has: query prefix to search for specific properties
[notmuch-archives.git] / af / 5db08f553703d823c69957f7631085e7d505f6
1 Return-Path: <aclements@csail.mit.edu>\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 D2907431FD6\r
6         for <notmuch@notmuchmail.org>; Mon,  6 Oct 2014 16:17:37 -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: -2.3\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3] 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 NoJv7c5qr-eN for <notmuch@notmuchmail.org>;\r
16         Mon,  6 Oct 2014 16:17:34 -0700 (PDT)\r
17 Received: from dmz-mailsec-scanner-7.mit.edu (dmz-mailsec-scanner-7.mit.edu\r
18         [18.7.68.36])\r
19         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 1F8E8431FBD\r
22         for <notmuch@notmuchmail.org>; Mon,  6 Oct 2014 16:17:29 -0700 (PDT)\r
23 X-AuditID: 12074424-f79346d000004923-3d-543323088ef7\r
24 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
25         (using TLS with cipher AES256-SHA (256/256 bits))\r
26         (Client did not present a certificate)\r
27         by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP\r
28         id 76.B5.18723.80323345; Mon,  6 Oct 2014 19:17:28 -0400 (EDT)\r
29 Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
30         by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s96NHRgh032097; \r
31         Mon, 6 Oct 2014 19:17:28 -0400\r
32 Received: from drake.dyndns.org ([12.185.136.2]) (authenticated bits=0)\r
33         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
34         by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s96NHPDN014532\r
35         (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
36         Mon, 6 Oct 2014 19:17:27 -0400\r
37 Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
38         (envelope-from <aclements@csail.mit.edu>)\r
39         id 1XbHWs-0001Jm-4i; Mon, 06 Oct 2014 17:17:22 -0600\r
40 From: Austin Clements <aclements@csail.mit.edu>\r
41 To: notmuch@notmuchmail.org\r
42 Subject: [PATCH v2 08/12] lib: Implement ghost-based thread linking\r
43 Date: Mon,  6 Oct 2014 17:17:14 -0600\r
44 Message-Id: <1412637438-4821-9-git-send-email-aclements@csail.mit.edu>\r
45 X-Mailer: git-send-email 2.1.0\r
46 In-Reply-To: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu>\r
47 References: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu>\r
48 X-Brightmail-Tracker:\r
49  H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixG6nrsuhbBxi0DhL1uJGazejxfWbM5kd\r
50         mDyerbrF7LHl0HvmAKYoLpuU1JzMstQifbsErozFWxcwF0w1r2idk9PA2K7TxcjJISFgIrH7\r
51         dRMjhC0mceHeerYuRi4OIYHZTBI7Z19mhXA2MEosnd4HlVnKJHHw/QRmCGcJo8TF3UfZQfrZ\r
52         BPQlVqydxApiiwhIS+y8OxvMZhawlLj6tZ2pi5GDQ1jAWWLHbk+QMIuAqsSj8wvAVvMKuEl8\r
53         erqNGeIMOYkNu/+DxTkF3CXWXJkMNkYIqOb5z6csExj5FzAyrGKUTcmt0s1NzMwpTk3WLU5O\r
54         zMtLLdI118vNLNFLTSndxAgOJBeVHYzNh5QOMQpwMCrx8EbsMAwRYk0sK67MPcQoycGkJMo7\r
55         h904RIgvKT+lMiOxOCO+qDQntfgQowQHs5IIL9dvoxAh3pTEyqrUonyYlDQHi5I476YffCFC\r
56         AumJJanZqakFqUUwWRkODiUJ3g5FoKGCRanpqRVpmTklCGkmDk6Q4TxAww+B1PAWFyTmFmem\r
57         Q+RPMSpKifMGgyQEQBIZpXlwvbBIf8UoDvSKMO98kCoeYJKA634FNJgJaLDpHH2QwSWJCCmp\r
58         BsZZm0R3tTK6zWYvZ80odn19sD/mu1xD1NNk39D4J9+PuvC/deNrqTosPyvNpkK5Z2WWXF7W\r
59         G4UgmTvfhGRXbj59i71MeeVJBt0HT8QSlArad64+nm9m9OnZl4WmC++2nZ5su4ct4sRzXeY6\r
60         k/RKwc7o+We0D8bHzz+5b+YPWWlltgmKYbvSxZVYijMSDbWYi4oTAS37XWjPAgAA\r
61 X-BeenThere: notmuch@notmuchmail.org\r
62 X-Mailman-Version: 2.1.13\r
63 Precedence: list\r
64 List-Id: "Use and development of the notmuch mail system."\r
65         <notmuch.notmuchmail.org>\r
66 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
68 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
69 List-Post: <mailto:notmuch@notmuchmail.org>\r
70 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
71 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
72         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
73 X-List-Received-Date: Mon, 06 Oct 2014 23:17:38 -0000\r
74 \r
75 From: Austin Clements <amdragon@mit.edu>\r
76 \r
77 This updates the thread linking code to use ghost messages instead of\r
78 user metadata to link messages into threads.\r
79 \r
80 In contrast with the old approach, this is actually correct.\r
81 Previously, thread merging updated only the thread IDs of message\r
82 documents, not thread IDs stored in user metadata.  As originally\r
83 diagnosed by Mark Walters [1] and as demonstrated by the broken\r
84 T260-thread-order test, this can cause notmuch to fail to link\r
85 messages even though they're in the same thread.  In principle the old\r
86 approach could have been fixed by updating the user metadata thread\r
87 IDs as well, but these are not indexed and hence this would have\r
88 required a full scan of all stored thread IDs.  Ghost messages solve\r
89 this problem naturally by reusing the exact same thread ID and message\r
90 ID representation and indexing as regular messages.\r
91 \r
92 Furthermore, thanks to this greater symmetry, ghost messages are also\r
93 algorithmically simpler.  We continue to support the old user metadata\r
94 format, so this patch can't delete any code, but when we do remove\r
95 support for the old format, several functions can simply be deleted.\r
96 \r
97 [1] id:8738h7kv2q.fsf@qmul.ac.uk\r
98 ---\r
99  lib/database.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++++--------\r
100  1 file changed, 75 insertions(+), 11 deletions(-)\r
101 \r
102 diff --git a/lib/database.cc b/lib/database.cc\r
103 index c641bcd..fdcc526 100644\r
104 --- a/lib/database.cc\r
105 +++ b/lib/database.cc\r
106 @@ -1752,6 +1752,12 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)\r
107                             message_id);\r
108  }\r
109  \r
110 +static notmuch_status_t\r
111 +_resolve_message_id_to_thread_id_old (notmuch_database_t *notmuch,\r
112 +                                     void *ctx,\r
113 +                                     const char *message_id,\r
114 +                                     const char **thread_id_ret);\r
115 +\r
116  /* Find the thread ID to which the message with 'message_id' belongs.\r
117   *\r
118   * Note: 'thread_id_ret' must not be NULL!\r
119 @@ -1760,9 +1766,9 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)\r
120   *\r
121   * Note: If there is no message in the database with the given\r
122   * 'message_id' then a new thread_id will be allocated for this\r
123 - * message and stored in the database metadata, (where this same\r
124 + * message ID and stored in the database metadata so that the\r
125   * thread ID can be looked up if the message is added to the database\r
126 - * later).\r
127 + * later.\r
128   */\r
129  static notmuch_status_t\r
130  _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,\r
131 @@ -1770,6 +1776,49 @@ _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,\r
132                                   const char *message_id,\r
133                                   const char **thread_id_ret)\r
134  {\r
135 +    notmuch_private_status_t status;\r
136 +    notmuch_message_t *message;\r
137 +\r
138 +    if (! (notmuch->features & NOTMUCH_FEATURE_GHOSTS))\r
139 +       return _resolve_message_id_to_thread_id_old (notmuch, ctx, message_id,\r
140 +                                                    thread_id_ret);\r
141 +\r
142 +    /* Look for this message (regular or ghost) */\r
143 +    message = _notmuch_message_create_for_message_id (\r
144 +       notmuch, message_id, &status);\r
145 +    if (status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
146 +       /* Message exists */\r
147 +       *thread_id_ret = talloc_steal (\r
148 +           ctx, notmuch_message_get_thread_id (message));\r
149 +    } else if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
150 +       /* Message did not exist.  Give it a fresh thread ID and\r
151 +        * populate this message as a ghost message. */\r
152 +       *thread_id_ret = talloc_strdup (\r
153 +           ctx, _notmuch_database_generate_thread_id (notmuch));\r
154 +       if (! *thread_id_ret) {\r
155 +           status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;\r
156 +       } else {\r
157 +           status = _notmuch_message_initialize_ghost (message, *thread_id_ret);\r
158 +           if (status == 0)\r
159 +               /* Commit the new ghost message */\r
160 +               _notmuch_message_sync (message);\r
161 +       }\r
162 +    } else {\r
163 +       /* Create failed. Fall through. */\r
164 +    }\r
165 +\r
166 +    notmuch_message_destroy (message);\r
167 +\r
168 +    return COERCE_STATUS (status, "Error creating ghost message");\r
169 +}\r
170 +\r
171 +/* Pre-ghost messages _resolve_message_id_to_thread_id */\r
172 +static notmuch_status_t\r
173 +_resolve_message_id_to_thread_id_old (notmuch_database_t *notmuch,\r
174 +                                     void *ctx,\r
175 +                                     const char *message_id,\r
176 +                                     const char **thread_id_ret)\r
177 +{\r
178      notmuch_status_t status;\r
179      notmuch_message_t *message;\r
180      string thread_id_string;\r
181 @@ -2007,7 +2056,7 @@ _consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch,\r
182      }\r
183  }\r
184  \r
185 -/* Given a (mostly empty) 'message' and its corresponding\r
186 +/* Given a blank or ghost 'message' and its corresponding\r
187   * 'message_file' link it to existing threads in the database.\r
188   *\r
189   * The first check is in the metadata of the database to see if we\r
190 @@ -2035,16 +2084,22 @@ _consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch,\r
191  static notmuch_status_t\r
192  _notmuch_database_link_message (notmuch_database_t *notmuch,\r
193                                 notmuch_message_t *message,\r
194 -                               notmuch_message_file_t *message_file)\r
195 +                               notmuch_message_file_t *message_file,\r
196 +                               notmuch_bool_t is_ghost)\r
197  {\r
198      void *local = talloc_new (NULL);\r
199      notmuch_status_t status;\r
200 -    const char *thread_id;\r
201 +    const char *thread_id = NULL;\r
202  \r
203      /* Check if the message already had a thread ID */\r
204 -    thread_id = _consume_metadata_thread_id (local, notmuch, message);\r
205 -    if (thread_id)\r
206 -       _notmuch_message_add_term (message, "thread", thread_id);\r
207 +    if (notmuch->features & NOTMUCH_FEATURE_GHOSTS) {\r
208 +       if (is_ghost)\r
209 +           thread_id = notmuch_message_get_thread_id (message);\r
210 +    } else {\r
211 +       thread_id = _consume_metadata_thread_id (local, notmuch, message);\r
212 +       if (thread_id)\r
213 +           _notmuch_message_add_term (message, "thread", thread_id);\r
214 +    }\r
215  \r
216      status = _notmuch_database_link_message_to_parents (notmuch, message,\r
217                                                         message_file,\r
218 @@ -2079,6 +2134,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
219      notmuch_message_t *message = NULL;\r
220      notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, ret2;\r
221      notmuch_private_status_t private_status;\r
222 +    notmuch_bool_t is_ghost = false;\r
223  \r
224      const char *date, *header;\r
225      const char *from, *to, *subject;\r
226 @@ -2171,12 +2227,20 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
227  \r
228         _notmuch_message_add_filename (message, filename);\r
229  \r
230 -       /* Is this a newly created message object? */\r
231 -       if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
232 +       /* Is this a newly created message object or a ghost\r
233 +        * message?  We have to be slightly careful: if this is a\r
234 +        * blank message, it's not safe to call\r
235 +        * notmuch_message_get_flag yet. */\r
236 +       if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND ||\r
237 +           (is_ghost = notmuch_message_get_flag (\r
238 +               message, NOTMUCH_MESSAGE_FLAG_GHOST))) {\r
239             _notmuch_message_add_term (message, "type", "mail");\r
240 +           if (is_ghost)\r
241 +               /* Convert ghost message to a regular message */\r
242 +               _notmuch_message_remove_term (message, "type", "ghost");\r
243  \r
244             ret = _notmuch_database_link_message (notmuch, message,\r
245 -                                                 message_file);\r
246 +                                                 message_file, is_ghost);\r
247             if (ret)\r
248                 goto DONE;\r
249  \r
250 -- \r
251 2.1.0\r
252 \r