Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 22 / 61de98c359ab34a45bac149c15225583a6cfa7
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 2B5096DE1B72\r
6  for <notmuch@notmuchmail.org>; Sun,  5 Apr 2015 16:02:01 -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.555\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.555 tagged_above=-999 required=5 tests=[AWL=0.545, \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 SA5aGV8hJqmh for <notmuch@notmuchmail.org>;\r
16  Sun,  5 Apr 2015 16:01:59 -0700 (PDT)\r
17 Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
18  [87.98.215.224])\r
19  by arlo.cworth.org (Postfix) with ESMTPS id EB6CB6DE1B7C\r
20  for <notmuch@notmuchmail.org>; Sun,  5 Apr 2015 16:01:58 -0700 (PDT)\r
21 Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
22  4.80) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
23  id 1YetWb-0002oM-4f; Sun, 05 Apr 2015 23:00:17 +0000\r
24 Received: (nullmailer pid 2239 invoked by uid 1000); Sun, 05 Apr 2015\r
25  22:59:25 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: notmuch@notmuchmail.org\r
28 Subject: [WIP2 02/12] lib: Add per-message last modification tracking\r
29 Date: Mon,  6 Apr 2015 07:59:04 +0900\r
30 Message-Id: <1428274754-1698-3-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.1.4\r
32 In-Reply-To: <1428274754-1698-1-git-send-email-david@tethera.net>\r
33 References: <1428274754-1698-1-git-send-email-david@tethera.net>\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.18\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38  <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
40  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
45  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Sun, 05 Apr 2015 23:02:01 -0000\r
47 \r
48 From: Austin Clements <amdragon@mit.edu>\r
49 \r
50 This adds a new document value that stores the revision of the last\r
51 modification to message metadata, where the revision number increases\r
52 monotonically with each database commit.\r
53 \r
54 An alternative would be to store the wall-clock time of the last\r
55 modification of each message.  In principle this is simpler and has\r
56 the advantage that any process can determine the current timestamp\r
57 without support from libnotmuch.  However, even assuming a computer's\r
58 clock never goes backward and ignoring clock skew in networked\r
59 environments, this has a fatal flaw.  Xapian uses (optimistic)\r
60 snapshot isolation, which means reads can be concurrent with writes.\r
61 Given this, consider the following time line with a write and two read\r
62 transactions:\r
63 \r
64    write  |-X-A--------------|\r
65    read 1       |---B---|\r
66    read 2                      |---|\r
67 \r
68 The write transaction modifies message X and records the wall-clock\r
69 time of the modification at A.  The writer hangs around for a while\r
70 and later commits its change.  Read 1 is concurrent with the write, so\r
71 it doesn't see the change to X.  It does some query and records the\r
72 wall-clock time of its results at B.  Transaction read 2 later starts\r
73 after the write commits and queries for changes since wall-clock time\r
74 B (say the reads are performing an incremental backup).  Even though\r
75 read 1 could not see the change to X, read 2 is told (correctly) that\r
76 X has not changed since B, the time of the last read.  In fact, X\r
77 changed before wall-clock time A, but the change was not visible until\r
78 *after* wall-clock time B, so read 2 misses the change to X.\r
79 \r
80 This is tricky to solve in full-blown snapshot isolation, but because\r
81 Xapian serializes writes, we can use a simple, monotonically\r
82 increasing database revision number.  Furthermore, maintaining this\r
83 revision number requires no more IO than a wall-clock time solution\r
84 because Xapian already maintains statistics on the upper (and lower)\r
85 bound of each value stream.\r
86 ---\r
87  lib/database-private.h | 16 +++++++++++++++-\r
88  lib/database.cc        | 49 +++++++++++++++++++++++++++++++++++++++++++++++--\r
89  lib/message.cc         | 22 ++++++++++++++++++++++\r
90  lib/notmuch-private.h  | 10 +++++++++-\r
91  4 files changed, 93 insertions(+), 4 deletions(-)\r
92 \r
93 diff --git a/lib/database-private.h b/lib/database-private.h\r
94 index 24243db..5c5a2bb 100644\r
95 --- a/lib/database-private.h\r
96 +++ b/lib/database-private.h\r
97 @@ -100,6 +100,12 @@ enum _notmuch_features {\r
98       *\r
99       * Introduced: version 3. */\r
100      NOTMUCH_FEATURE_INDEXED_MIMETYPES = 1 << 5,\r
101 +\r
102 +    /* If set, messages store the revision number of the last\r
103 +     * modification in NOTMUCH_VALUE_LAST_MOD.\r
104 +     *\r
105 +     * Introduced: version 3. */\r
106 +    NOTMUCH_FEATURE_LAST_MOD = 1 << 6,\r
107  };\r
108  \r
109  /* In C++, a named enum is its own type, so define bitwise operators\r
110 @@ -145,6 +151,8 @@ struct _notmuch_database {\r
111  \r
112      notmuch_database_mode_t mode;\r
113      int atomic_nesting;\r
114 +    /* TRUE if changes have been made in this atomic section */\r
115 +    notmuch_bool_t atomic_dirty;\r
116      Xapian::Database *xapian_db;\r
117  \r
118      /* Bit mask of features used by this database.  This is a\r
119 @@ -158,6 +166,11 @@ struct _notmuch_database {\r
120       * next library call. May be NULL */\r
121      char *status_string;\r
122  \r
123 +    /* Highest committed revision number.  Modifications are recorded\r
124 +     * under a higher revision number, which can be generated with\r
125 +     * notmuch_database_new_revision. */\r
126 +    unsigned long revision;\r
127 +\r
128      Xapian::QueryParser *query_parser;\r
129      Xapian::TermGenerator *term_gen;\r
130      Xapian::ValueRangeProcessor *value_range_processor;\r
131 @@ -179,7 +192,8 @@ struct _notmuch_database {\r
132   * will have it). */\r
133  #define NOTMUCH_FEATURES_CURRENT \\r
134      (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_DIRECTORY_DOCS | \\r
135 -     NOTMUCH_FEATURE_BOOL_FOLDER | NOTMUCH_FEATURE_GHOSTS)\r
136 +     NOTMUCH_FEATURE_BOOL_FOLDER | NOTMUCH_FEATURE_GHOSTS | \\r
137 +     NOTMUCH_FEATURE_LAST_MOD)\r
138  \r
139  /* Return the list of terms from the given iterator matching a prefix.\r
140   * The prefix will be stripped from the strings in the returned list.\r
141 diff --git a/lib/database.cc b/lib/database.cc\r
142 index cffab62..4378979 100644\r
143 --- a/lib/database.cc\r
144 +++ b/lib/database.cc\r
145 @@ -101,6 +101,9 @@ typedef struct {\r
146   *\r
147   *     SUBJECT:        The value of the "Subject" header\r
148   *\r
149 + *     LAST_MOD:       The revision number as of the last tag or\r
150 + *                     filename change.\r
151 + *\r
152   * In addition, terms from the content of the message are added with\r
153   * "from", "to", "attachment", and "subject" prefixes for use by the\r
154   * user in searching. Similarly, terms from the path of the mail\r
155 @@ -310,6 +313,8 @@ static const struct {\r
156       * them. */\r
157      { NOTMUCH_FEATURE_INDEXED_MIMETYPES,\r
158        "indexed MIME types", "w"},\r
159 +    { NOTMUCH_FEATURE_LAST_MOD,\r
160 +      "modification tracking", "w"},\r
161  };\r
162  \r
163  const char *\r
164 @@ -729,6 +734,23 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)\r
165      return NOTMUCH_STATUS_SUCCESS;\r
166  }\r
167  \r
168 +/* Allocate a revision number for the next change. */\r
169 +unsigned long\r
170 +_notmuch_database_new_revision (notmuch_database_t *notmuch)\r
171 +{\r
172 +    unsigned long new_revision = notmuch->revision + 1;\r
173 +\r
174 +    /* If we're in an atomic section, hold off on updating the\r
175 +     * committed revision number until we commit the atomic section.\r
176 +     */\r
177 +    if (notmuch->atomic_nesting)\r
178 +       notmuch->atomic_dirty = TRUE;\r
179 +    else\r
180 +       notmuch->revision = new_revision;\r
181 +\r
182 +    return new_revision;\r
183 +}\r
184 +\r
185  /* Parse a database features string from the given database version.\r
186   * Returns the feature bit set.\r
187   *\r
188 @@ -890,6 +912,7 @@ notmuch_database_open_verbose (const char *path,\r
189      notmuch->atomic_nesting = 0;\r
190      try {\r
191         string last_thread_id;\r
192 +       string last_mod;\r
193  \r
194         if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {\r
195             notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,\r
196 @@ -948,6 +971,14 @@ notmuch_database_open_verbose (const char *path,\r
197                 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);\r
198         }\r
199  \r
200 +       /* Get current highest revision number. */\r
201 +       last_mod = notmuch->xapian_db->get_value_upper_bound (\r
202 +           NOTMUCH_VALUE_LAST_MOD);\r
203 +       if (last_mod.empty ())\r
204 +           notmuch->revision = 0;\r
205 +       else\r
206 +           notmuch->revision = Xapian::sortable_unserialise (last_mod);\r
207 +\r
208         notmuch->query_parser = new Xapian::QueryParser;\r
209         notmuch->term_gen = new Xapian::TermGenerator;\r
210         notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));\r
211 @@ -1355,7 +1386,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
212  \r
213      /* Figure out how much total work we need to do. */\r
214      if (new_features &\r
215 -       (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER)) {\r
216 +       (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |\r
217 +        NOTMUCH_FEATURE_LAST_MOD)) {\r
218         notmuch_query_t *query = notmuch_query_create (notmuch, "");\r
219         total += notmuch_query_count_messages (query);\r
220         notmuch_query_destroy (query);\r
221 @@ -1382,7 +1414,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
222  \r
223      /* Perform per-message upgrades. */\r
224      if (new_features &\r
225 -       (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER)) {\r
226 +       (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |\r
227 +        NOTMUCH_FEATURE_LAST_MOD)) {\r
228         notmuch_query_t *query = notmuch_query_create (notmuch, "");\r
229         notmuch_messages_t *messages;\r
230         notmuch_message_t *message;\r
231 @@ -1419,6 +1452,13 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
232             if (new_features & NOTMUCH_FEATURE_BOOL_FOLDER)\r
233                 _notmuch_message_upgrade_folder (message);\r
234  \r
235 +           /* Prior to NOTMUCH_FEATURE_LAST_MOD, messages did not\r
236 +            * track modification revisions.  Give all messages a\r
237 +            * revision of 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 @@ -1601,6 +1641,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.1.4\r
334 \r