Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / f0 / 098a1c416f419679860c7b07434e51954800e9
1 Return-Path: <bremner@tesseract.cs.unb.ca>\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 arlo.cworth.org (Postfix) with ESMTP id 854826DE192C\r
6  for <notmuch@notmuchmail.org>; Fri, 14 Aug 2015 09:50:27 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.132\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.132 tagged_above=-999 required=5 tests=[AWL=0.122, \r
12  T_HEADER_FROM_DIFFERENT_DOMAINS=0.01] autolearn=disabled\r
13 Received: from arlo.cworth.org ([127.0.0.1])\r
14  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
15  with ESMTP id 02SCi6IQTfUQ for <notmuch@notmuchmail.org>;\r
16  Fri, 14 Aug 2015 09:50:21 -0700 (PDT)\r
17 Received: from gitolite.debian.net (gitolite.debian.net [87.98.215.224])\r
18  by arlo.cworth.org (Postfix) with ESMTPS id 8FA896DE1A24\r
19  for <notmuch@notmuchmail.org>; Fri, 14 Aug 2015 09:50:20 -0700 (PDT)\r
20 Received: from remotemail by gitolite.debian.net with local (Exim 4.80)\r
21  (envelope-from <bremner@tesseract.cs.unb.ca>)\r
22  id 1ZQI9m-0003e4-T7; Fri, 14 Aug 2015 16:48:38 +0000\r
23 Received: (nullmailer pid 15752 invoked by uid 1000); Fri, 14 Aug 2015\r
24  16:48:02 -0000\r
25 From: David Bremner <david@tethera.net>\r
26 To: notmuch@notmuchmail.org\r
27 Subject: [PATCH 1/5] lib: Add per-message last modification tracking\r
28 Date: Fri, 14 Aug 2015 18:47:54 +0200\r
29 Message-Id: <1439570878-15165-2-git-send-email-david@tethera.net>\r
30 X-Mailer: git-send-email 2.5.0\r
31 In-Reply-To: <1439570878-15165-1-git-send-email-david@tethera.net>\r
32 References: <1439570878-15165-1-git-send-email-david@tethera.net>\r
33 X-BeenThere: notmuch@notmuchmail.org\r
34 X-Mailman-Version: 2.1.18\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: Fri, 14 Aug 2015 16:50:27 -0000\r
46 \r
47 From: Austin Clements <amdragon@mit.edu>\r
48 \r
49 This adds a new document value that stores the revision of the last\r
50 modification to message metadata, where the revision number increases\r
51 monotonically with each database commit.\r
52 \r
53 An alternative would be to store the wall-clock time of the last\r
54 modification of each message.  In principle this is simpler and has\r
55 the advantage that any process can determine the current timestamp\r
56 without support from libnotmuch.  However, even assuming a computer's\r
57 clock never goes backward and ignoring clock skew in networked\r
58 environments, this has a fatal flaw.  Xapian uses (optimistic)\r
59 snapshot isolation, which means reads can be concurrent with writes.\r
60 Given this, consider the following time line with a write and two read\r
61 transactions:\r
62 \r
63    write  |-X-A--------------|\r
64    read 1       |---B---|\r
65    read 2                      |---|\r
66 \r
67 The write transaction modifies message X and records the wall-clock\r
68 time of the modification at A.  The writer hangs around for a while\r
69 and later commits its change.  Read 1 is concurrent with the write, so\r
70 it doesn't see the change to X.  It does some query and records the\r
71 wall-clock time of its results at B.  Transaction read 2 later starts\r
72 after the write commits and queries for changes since wall-clock time\r
73 B (say the reads are performing an incremental backup).  Even though\r
74 read 1 could not see the change to X, read 2 is told (correctly) that\r
75 X has not changed since B, the time of the last read.  In fact, X\r
76 changed before wall-clock time A, but the change was not visible until\r
77 *after* wall-clock time B, so read 2 misses the change to X.\r
78 \r
79 This is tricky to solve in full-blown snapshot isolation, but because\r
80 Xapian serializes writes, we can use a simple, monotonically\r
81 increasing database revision number.  Furthermore, maintaining this\r
82 revision number requires no more IO than a wall-clock time solution\r
83 because Xapian already maintains statistics on the upper (and lower)\r
84 bound of each value stream.\r
85 ---\r
86  lib/database-private.h | 16 +++++++++++++++-\r
87  lib/database.cc        | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--\r
88  lib/message.cc         | 22 ++++++++++++++++++++++\r
89  lib/notmuch-private.h  | 10 +++++++++-\r
90  4 files changed, 94 insertions(+), 4 deletions(-)\r
91 \r
92 diff --git a/lib/database-private.h b/lib/database-private.h\r
93 index 24243db..5c5a2bb 100644\r
94 --- a/lib/database-private.h\r
95 +++ b/lib/database-private.h\r
96 @@ -100,6 +100,12 @@ enum _notmuch_features {\r
97       *\r
98       * Introduced: version 3. */\r
99      NOTMUCH_FEATURE_INDEXED_MIMETYPES = 1 << 5,\r
100 +\r
101 +    /* If set, messages store the revision number of the last\r
102 +     * modification in NOTMUCH_VALUE_LAST_MOD.\r
103 +     *\r
104 +     * Introduced: version 3. */\r
105 +    NOTMUCH_FEATURE_LAST_MOD = 1 << 6,\r
106  };\r
107  \r
108  /* In C++, a named enum is its own type, so define bitwise operators\r
109 @@ -145,6 +151,8 @@ struct _notmuch_database {\r
110  \r
111      notmuch_database_mode_t mode;\r
112      int atomic_nesting;\r
113 +    /* TRUE if changes have been made in this atomic section */\r
114 +    notmuch_bool_t atomic_dirty;\r
115      Xapian::Database *xapian_db;\r
116  \r
117      /* Bit mask of features used by this database.  This is a\r
118 @@ -158,6 +166,11 @@ struct _notmuch_database {\r
119       * next library call. May be NULL */\r
120      char *status_string;\r
121  \r
122 +    /* Highest committed revision number.  Modifications are recorded\r
123 +     * under a higher revision number, which can be generated with\r
124 +     * notmuch_database_new_revision. */\r
125 +    unsigned long revision;\r
126 +\r
127      Xapian::QueryParser *query_parser;\r
128      Xapian::TermGenerator *term_gen;\r
129      Xapian::ValueRangeProcessor *value_range_processor;\r
130 @@ -179,7 +192,8 @@ struct _notmuch_database {\r
131   * will have it). */\r
132  #define NOTMUCH_FEATURES_CURRENT \\r
133      (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_DIRECTORY_DOCS | \\r
134 -     NOTMUCH_FEATURE_BOOL_FOLDER | NOTMUCH_FEATURE_GHOSTS)\r
135 +     NOTMUCH_FEATURE_BOOL_FOLDER | NOTMUCH_FEATURE_GHOSTS | \\r
136 +     NOTMUCH_FEATURE_LAST_MOD)\r
137  \r
138  /* Return the list of terms from the given iterator matching a prefix.\r
139   * The prefix will be stripped from the strings in the returned list.\r
140 diff --git a/lib/database.cc b/lib/database.cc\r
141 index 6a15174..52e2e8f 100644\r
142 --- a/lib/database.cc\r
143 +++ b/lib/database.cc\r
144 @@ -101,6 +101,9 @@ typedef struct {\r
145   *\r
146   *     SUBJECT:        The value of the "Subject" header\r
147   *\r
148 + *     LAST_MOD:       The revision number as of the last tag or\r
149 + *                     filename change.\r
150 + *\r
151   * In addition, terms from the content of the message are added with\r
152   * "from", "to", "attachment", and "subject" prefixes for use by the\r
153   * user in searching. Similarly, terms from the path of the mail\r
154 @@ -310,6 +313,8 @@ static const struct {\r
155       * them. */\r
156      { NOTMUCH_FEATURE_INDEXED_MIMETYPES,\r
157        "indexed MIME types", "w"},\r
158 +    { NOTMUCH_FEATURE_LAST_MOD,\r
159 +      "modification tracking", "w"},\r
160  };\r
161  \r
162  const char *\r
163 @@ -737,6 +742,23 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)\r
164      return NOTMUCH_STATUS_SUCCESS;\r
165  }\r
166  \r
167 +/* Allocate a revision number for the next change. */\r
168 +unsigned long\r
169 +_notmuch_database_new_revision (notmuch_database_t *notmuch)\r
170 +{\r
171 +    unsigned long new_revision = notmuch->revision + 1;\r
172 +\r
173 +    /* If we're in an atomic section, hold off on updating the\r
174 +     * committed revision number until we commit the atomic section.\r
175 +     */\r
176 +    if (notmuch->atomic_nesting)\r
177 +       notmuch->atomic_dirty = TRUE;\r
178 +    else\r
179 +       notmuch->revision = new_revision;\r
180 +\r
181 +    return new_revision;\r
182 +}\r
183 +\r
184  /* Parse a database features string from the given database version.\r
185   * Returns the feature bit set.\r
186   *\r
187 @@ -904,6 +926,7 @@ notmuch_database_open_verbose (const char *path,\r
188      notmuch->atomic_nesting = 0;\r
189      try {\r
190         string last_thread_id;\r
191 +       string last_mod;\r
192  \r
193         if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {\r
194             notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,\r
195 @@ -962,6 +985,14 @@ notmuch_database_open_verbose (const char *path,\r
196                 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);\r
197         }\r
198  \r
199 +       /* Get current highest revision number. */\r
200 +       last_mod = notmuch->xapian_db->get_value_upper_bound (\r
201 +           NOTMUCH_VALUE_LAST_MOD);\r
202 +       if (last_mod.empty ())\r
203 +           notmuch->revision = 0;\r
204 +       else\r
205 +           notmuch->revision = Xapian::sortable_unserialise (last_mod);\r
206 +\r
207         notmuch->query_parser = new Xapian::QueryParser;\r
208         notmuch->term_gen = new Xapian::TermGenerator;\r
209         notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));\r
210 @@ -1369,7 +1400,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
211  \r
212      /* Figure out how much total work we need to do. */\r
213      if (new_features &\r
214 -       (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER)) {\r
215 +       (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |\r
216 +        NOTMUCH_FEATURE_LAST_MOD)) {\r
217         notmuch_query_t *query = notmuch_query_create (notmuch, "");\r
218         total += notmuch_query_count_messages (query);\r
219         notmuch_query_destroy (query);\r
220 @@ -1396,7 +1428,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
221  \r
222      /* Perform per-message upgrades. */\r
223      if (new_features &\r
224 -       (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER)) {\r
225 +       (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |\r
226 +        NOTMUCH_FEATURE_LAST_MOD)) {\r
227         notmuch_query_t *query = notmuch_query_create (notmuch, "");\r
228         notmuch_messages_t *messages;\r
229         notmuch_message_t *message;\r
230 @@ -1433,6 +1466,14 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
231             if (new_features & NOTMUCH_FEATURE_BOOL_FOLDER)\r
232                 _notmuch_message_upgrade_folder (message);\r
233  \r
234 +           /* Prior to NOTMUCH_FEATURE_LAST_MOD, messages did not\r
235 +            * track modification revisions.  Give all messages the\r
236 +            * next available revision; since we just started tracking\r
237 +            * revisions for this database, that will be 1.\r
238 +            */\r
239 +           if (new_features & NOTMUCH_FEATURE_LAST_MOD)\r
240 +               _notmuch_message_upgrade_last_mod (message);\r
241 +\r
242             _notmuch_message_sync (message);\r
243  \r
244             notmuch_message_destroy (message);\r
245 @@ -1615,6 +1656,11 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch)\r
246         return NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
247      }\r
248  \r
249 +    if (notmuch->atomic_dirty) {\r
250 +       ++notmuch->revision;\r
251 +       notmuch->atomic_dirty = FALSE;\r
252 +    }\r
253 +\r
254  DONE:\r
255      notmuch->atomic_nesting--;\r
256      return NOTMUCH_STATUS_SUCCESS;\r
257 diff --git a/lib/message.cc b/lib/message.cc\r
258 index 1ddce3c..26b5e76 100644\r
259 --- a/lib/message.cc\r
260 +++ b/lib/message.cc\r
261 @@ -998,6 +998,16 @@ _notmuch_message_set_header_values (notmuch_message_t *message,\r
262      message->modified = TRUE;\r
263  }\r
264  \r
265 +/* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD.  The caller\r
266 + * must call _notmuch_message_sync. */\r
267 +void\r
268 +_notmuch_message_upgrade_last_mod (notmuch_message_t *message)\r
269 +{\r
270 +    /* _notmuch_message_sync will update the last modification\r
271 +     * revision; we just have to ask it to. */\r
272 +    message->modified = TRUE;\r
273 +}\r
274 +\r
275  /* Synchronize changes made to message->doc out into the database. */\r
276  void\r
277  _notmuch_message_sync (notmuch_message_t *message)\r
278 @@ -1010,6 +1020,18 @@ _notmuch_message_sync (notmuch_message_t *message)\r
279      if (! message->modified)\r
280         return;\r
281  \r
282 +    /* Update the last modification of this message. */\r
283 +    if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)\r
284 +       /* sortable_serialise gives a reasonably compact encoding,\r
285 +        * which directly translates to reduced IO when scanning the\r
286 +        * value stream.  Since it's built for doubles, we only get 53\r
287 +        * effective bits, but that's still enough for the database to\r
288 +        * last a few centuries at 1 million revisions per second. */\r
289 +       message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,\r
290 +                               Xapian::sortable_serialise (\r
291 +                                   _notmuch_database_new_revision (\r
292 +                                       message->notmuch)));\r
293 +\r
294      db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);\r
295      db->replace_document (message->doc_id, message->doc);\r
296      message->modified = FALSE;\r
297 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
298 index cc9ce12..f52b4e4 100644\r
299 --- a/lib/notmuch-private.h\r
300 +++ b/lib/notmuch-private.h\r
301 @@ -107,7 +107,8 @@ typedef enum {\r
302      NOTMUCH_VALUE_TIMESTAMP = 0,\r
303      NOTMUCH_VALUE_MESSAGE_ID,\r
304      NOTMUCH_VALUE_FROM,\r
305 -    NOTMUCH_VALUE_SUBJECT\r
306 +    NOTMUCH_VALUE_SUBJECT,\r
307 +    NOTMUCH_VALUE_LAST_MOD,\r
308  } notmuch_value_t;\r
309  \r
310  /* Xapian (with flint backend) complains if we provide a term longer\r
311 @@ -194,6 +195,9 @@ void\r
312  _notmuch_database_log (notmuch_database_t *notmuch,\r
313                        const char *format, ...);\r
314  \r
315 +unsigned long\r
316 +_notmuch_database_new_revision (notmuch_database_t *notmuch);\r
317 +\r
318  const char *\r
319  _notmuch_database_relative_path (notmuch_database_t *notmuch,\r
320                                  const char *path);\r
321 @@ -305,6 +309,10 @@ _notmuch_message_set_header_values (notmuch_message_t *message,\r
322                                     const char *date,\r
323                                     const char *from,\r
324                                     const char *subject);\r
325 +\r
326 +void\r
327 +_notmuch_message_upgrade_last_mod (notmuch_message_t *message);\r
328 +\r
329  void\r
330  _notmuch_message_sync (notmuch_message_t *message);\r
331  \r
332 -- \r
333 2.5.0\r
334 \r