Re: Avoiding the "huge INBOX of death"
[notmuch-archives.git] / a6 / e380d8f05c72dce9d9a273b8d498efb8b14b09
1 Return-Path: <chris@chris-wilson.co.uk>\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 21584431FBF\r
6         for <notmuch@notmuchmail.org>; Sat, 21 Nov 2009 12:08:37 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id VQzzlMdMW8IG for <notmuch@notmuchmail.org>;\r
11         Sat, 21 Nov 2009 12:08:36 -0800 (PST)\r
12 Received: from orsmga101.jf.intel.com (mga06.intel.com [134.134.136.21])\r
13         by olra.theworths.org (Postfix) with ESMTP id A64A0431FAE\r
14         for <notmuch@notmuchmail.org>; Sat, 21 Nov 2009 12:08:35 -0800 (PST)\r
15 Received: from orsmga002.jf.intel.com ([10.7.209.21])\r
16         by orsmga101.jf.intel.com with ESMTP; 21 Nov 2009 12:08:27 -0800\r
17 X-ExtLoop1: 1\r
18 X-IronPort-AV: E=Sophos;i="4.47,264,1257148800"; d="scan'208";a="469402951"\r
19 Received: from unknown (HELO localhost.localdomain) ([10.255.16.192])\r
20         by orsmga002.jf.intel.com with ESMTP; 21 Nov 2009 12:25:37 -0800\r
21 From: Chris Wilson <chris@chris-wilson.co.uk>\r
22 To: notmuch@notmuchmail.org\r
23 Date: Sat, 21 Nov 2009 20:08:31 +0000\r
24 Message-Id: <1258834111-18285-1-git-send-email-chris@chris-wilson.co.uk>\r
25 X-Mailer: git-send-email 1.6.5.3\r
26 Subject: [notmuch] [PATCH] Permit opening the notmuch database in read-only\r
27         mode.\r
28 X-BeenThere: notmuch@notmuchmail.org\r
29 X-Mailman-Version: 2.1.12\r
30 Precedence: list\r
31 List-Id: "Use and development of the notmuch mail system."\r
32         <notmuch.notmuchmail.org>\r
33 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
34         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
35 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
36 List-Post: <mailto:notmuch@notmuchmail.org>\r
37 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
38 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
39         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
40 X-List-Received-Date: Sat, 21 Nov 2009 20:08:37 -0000\r
41 \r
42 We only rarely need to actually open the database for writing, but we\r
43 always create a Xapian::WritableDatabase. This has the effect of\r
44 preventing searches and like whilst updating the index.\r
45 \r
46 Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>\r
47 Acked-by: Carl Worth <cworth@cworth.org>\r
48 [ickle: Updated to add WRITABLE mode to notmuch-new]\r
49 ---\r
50  lib/database-private.h |    3 ++-\r
51  lib/database.cc        |   31 ++++++++++++++++++++++++-------\r
52  lib/message.cc         |   15 +++++++++++++--\r
53  lib/notmuch-private.h  |    1 +\r
54  lib/notmuch.h          |   13 +++++++++++--\r
55  notmuch-dump.c         |    3 ++-\r
56  notmuch-new.c          |    4 +++-\r
57  notmuch-reply.c        |    3 ++-\r
58  notmuch-restore.c      |    3 ++-\r
59  notmuch-search.c       |    3 ++-\r
60  notmuch-show.c         |    3 ++-\r
61  notmuch-tag.c          |    3 ++-\r
62  12 files changed, 66 insertions(+), 19 deletions(-)\r
63 \r
64 diff --git a/lib/database-private.h b/lib/database-private.h\r
65 index 76e26ce..79c7916 100644\r
66 --- a/lib/database-private.h\r
67 +++ b/lib/database-private.h\r
68 @@ -27,7 +27,8 @@\r
69  \r
70  struct _notmuch_database {\r
71      char *path;\r
72 -    Xapian::WritableDatabase *xapian_db;\r
73 +    notmuch_database_mode_t mode;\r
74 +    Xapian::Database *xapian_db;\r
75      Xapian::QueryParser *query_parser;\r
76      Xapian::TermGenerator *term_gen;\r
77  };\r
78 diff --git a/lib/database.cc b/lib/database.cc\r
79 index 207246c..fb38664 100644\r
80 --- a/lib/database.cc\r
81 +++ b/lib/database.cc\r
82 @@ -172,6 +172,8 @@ notmuch_status_to_string (notmuch_status_t status)\r
83         return "No error occurred";\r
84      case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
85         return "Out of memory";\r
86 +    case NOTMUCH_STATUS_READONLY_DATABASE:\r
87 +       return "The database is read-only";\r
88      case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
89         return "A Xapian exception occurred";\r
90      case NOTMUCH_STATUS_FILE_ERROR:\r
91 @@ -438,7 +440,8 @@ notmuch_database_create (const char *path)\r
92         goto DONE;\r
93      }\r
94  \r
95 -    notmuch = notmuch_database_open (path);\r
96 +    notmuch = notmuch_database_open (path,\r
97 +                                    NOTMUCH_DATABASE_MODE_WRITABLE);\r
98  \r
99    DONE:\r
100      if (notmuch_path)\r
101 @@ -448,7 +451,8 @@ notmuch_database_create (const char *path)\r
102  }\r
103  \r
104  notmuch_database_t *\r
105 -notmuch_database_open (const char *path)\r
106 +notmuch_database_open (const char *path,\r
107 +                      notmuch_database_mode_t mode)\r
108  {\r
109      notmuch_database_t *notmuch = NULL;\r
110      char *notmuch_path = NULL, *xapian_path = NULL;\r
111 @@ -481,9 +485,14 @@ notmuch_database_open (const char *path)\r
112      if (notmuch->path[strlen (notmuch->path) - 1] == '/')\r
113         notmuch->path[strlen (notmuch->path) - 1] = '\0';\r
114  \r
115 +    notmuch->mode = mode;\r
116      try {\r
117 -       notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,\r
118 -                                                          Xapian::DB_CREATE_OR_OPEN);\r
119 +       if (mode == NOTMUCH_DATABASE_MODE_WRITABLE) {\r
120 +           notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,\r
121 +                                                              Xapian::DB_CREATE_OR_OPEN);\r
122 +       } else {\r
123 +           notmuch->xapian_db = new Xapian::Database (xapian_path);\r
124 +       }\r
125         notmuch->query_parser = new Xapian::QueryParser;\r
126         notmuch->term_gen = new Xapian::TermGenerator;\r
127         notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));\r
128 @@ -521,7 +530,8 @@ notmuch_database_open (const char *path)\r
129  void\r
130  notmuch_database_close (notmuch_database_t *notmuch)\r
131  {\r
132 -    notmuch->xapian_db->flush ();\r
133 +    if (notmuch->mode == NOTMUCH_DATABASE_MODE_WRITABLE)\r
134 +       (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();\r
135  \r
136      delete notmuch->term_gen;\r
137      delete notmuch->query_parser;\r
138 @@ -567,11 +577,18 @@ notmuch_database_set_timestamp (notmuch_database_t *notmuch,\r
139                                 const char *key, time_t timestamp)\r
140  {\r
141      Xapian::Document doc;\r
142 +    Xapian::WritableDatabase *db;\r
143      unsigned int doc_id;\r
144      notmuch_private_status_t status;\r
145      notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
146      char *db_key = NULL;\r
147  \r
148 +    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {\r
149 +       fprintf (stderr, "Attempted to update a read-only database.\n");\r
150 +       return NOTMUCH_STATUS_READONLY_DATABASE;\r
151 +    }\r
152 +\r
153 +    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
154      db_key = timestamp_db_key (key);\r
155  \r
156      try {\r
157 @@ -586,9 +603,9 @@ notmuch_database_set_timestamp (notmuch_database_t *notmuch,\r
158             doc.add_term (term);\r
159             talloc_free (term);\r
160  \r
161 -           notmuch->xapian_db->add_document (doc);\r
162 +           db->add_document (doc);\r
163         } else {\r
164 -           notmuch->xapian_db->replace_document (doc_id, doc);\r
165 +           db->replace_document (doc_id, doc);\r
166         }\r
167  \r
168      } catch (Xapian::Error &error) {\r
169 diff --git a/lib/message.cc b/lib/message.cc\r
170 index e0b8a8e..7ba06c9 100644\r
171 --- a/lib/message.cc\r
172 +++ b/lib/message.cc\r
173 @@ -168,9 +168,15 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,\r
174  {\r
175      notmuch_message_t *message;\r
176      Xapian::Document doc;\r
177 +    Xapian::WritableDatabase *db;\r
178      unsigned int doc_id;\r
179      char *term;\r
180  \r
181 +    if (notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY) {\r
182 +       *status_ret = NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE;\r
183 +       return NULL;\r
184 +    }\r
185 +\r
186      *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
187  \r
188      message = notmuch_database_find_message (notmuch, message_id);\r
189 @@ -184,13 +190,14 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,\r
190         return NULL;\r
191      }\r
192  \r
193 +    db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);\r
194      try {\r
195         doc.add_term (term);\r
196         talloc_free (term);\r
197  \r
198         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);\r
199  \r
200 -       doc_id = notmuch->xapian_db->add_document (doc);\r
201 +       doc_id = db->add_document (doc);\r
202      } catch (const Xapian::Error &error) {\r
203         *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;\r
204         return NULL;\r
205 @@ -543,8 +550,12 @@ _notmuch_message_ensure_thread_id (notmuch_message_t *message)\r
206  void\r
207  _notmuch_message_sync (notmuch_message_t *message)\r
208  {\r
209 -    Xapian::WritableDatabase *db = message->notmuch->xapian_db;\r
210 +    Xapian::WritableDatabase *db;\r
211 +\r
212 +    if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READONLY)\r
213 +       return;\r
214  \r
215 +    db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);\r
216      db->replace_document (message->doc_id, message->doc);\r
217  }\r
218  \r
219 diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
220 index 1583498..2b4bf31 100644\r
221 --- a/lib/notmuch-private.h\r
222 +++ b/lib/notmuch-private.h\r
223 @@ -110,6 +110,7 @@ typedef enum _notmuch_private_status {\r
224      /* First, copy all the public status values. */\r
225      NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,\r
226      NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,\r
227 +    NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE = NOTMUCH_STATUS_READONLY_DATABASE,\r
228      NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,\r
229      NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,\r
230      NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,\r
231 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
232 index cc713a3..89ed7ad 100644\r
233 --- a/lib/notmuch.h\r
234 +++ b/lib/notmuch.h\r
235 @@ -86,6 +86,7 @@ typedef int notmuch_bool_t;\r
236  typedef enum _notmuch_status {\r
237      NOTMUCH_STATUS_SUCCESS = 0,\r
238      NOTMUCH_STATUS_OUT_OF_MEMORY,\r
239 +    NOTMUCH_STATUS_READONLY_DATABASE,\r
240      NOTMUCH_STATUS_XAPIAN_EXCEPTION,\r
241      NOTMUCH_STATUS_FILE_ERROR,\r
242      NOTMUCH_STATUS_FILE_NOT_EMAIL,\r
243 @@ -139,11 +140,18 @@ notmuch_database_create (const char *path);\r
244  /* XXX: I think I'd like this to take an extra argument of\r
245   * notmuch_status_t* for returning a status value on failure. */\r
246  \r
247 +typedef enum {\r
248 +    NOTMUCH_DATABASE_MODE_READONLY = 0,\r
249 +    NOTMUCH_DATABASE_MODE_WRITABLE\r
250 +} notmuch_database_mode_t;\r
251 +\r
252  /* Open an existing notmuch database located at 'path'.\r
253   *\r
254   * The database should have been created at some time in the past,\r
255   * (not necessarily by this process), by calling\r
256 - * notmuch_database_create with 'path'.\r
257 + * notmuch_database_create with 'path'. By default the database should be\r
258 + * opened for reading only. In order to write to the database you need to\r
259 + * pass the NOTMUCH_DATABASE_MODE_WRITABLE mode.\r
260   *\r
261   * An existing notmuch database can be identified by the presence of a\r
262   * directory named ".notmuch" below 'path'.\r
263 @@ -155,7 +163,8 @@ notmuch_database_create (const char *path);\r
264   * an error message on stderr).\r
265   */\r
266  notmuch_database_t *\r
267 -notmuch_database_open (const char *path);\r
268 +notmuch_database_open (const char *path,\r
269 +                      notmuch_database_mode_t mode);\r
270  \r
271  /* Close the given notmuch database, freeing all associated\r
272   * resources. See notmuch_database_open. */\r
273 diff --git a/notmuch-dump.c b/notmuch-dump.c\r
274 index 4c6e321..86570d0 100644\r
275 --- a/notmuch-dump.c\r
276 +++ b/notmuch-dump.c\r
277 @@ -35,7 +35,8 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
278      if (config == NULL)\r
279         return 1;\r
280  \r
281 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
282 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
283 +                                    NOTMUCH_DATABASE_MODE_READONLY);\r
284      if (notmuch == NULL)\r
285         return 1;\r
286  \r
287 diff --git a/notmuch-new.c b/notmuch-new.c\r
288 index 6264628..dded885 100644\r
289 --- a/notmuch-new.c\r
290 +++ b/notmuch-new.c\r
291 @@ -195,6 +195,7 @@ add_files_recursive (notmuch_database_t *notmuch,\r
292                                  next);\r
293                         break;\r
294                     /* Fatal issues. Don't process anymore. */\r
295 +                   case NOTMUCH_STATUS_READONLY_DATABASE:\r
296                     case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
297                     case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
298                         fprintf (stderr, "Error: %s. Halting processing.\n",\r
299 @@ -420,7 +421,8 @@ notmuch_new_command (void *ctx,\r
300         add_files_state.ignore_read_only_directories = FALSE;\r
301         add_files_state.total_files = count;\r
302      } else {\r
303 -       notmuch = notmuch_database_open (db_path);\r
304 +       notmuch = notmuch_database_open (db_path,\r
305 +                                        NOTMUCH_DATABASE_MODE_WRITABLE);\r
306         add_files_state.ignore_read_only_directories = TRUE;\r
307         add_files_state.total_files = 0;\r
308      }\r
309 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
310 index c6122aa..291cd02 100644\r
311 --- a/notmuch-reply.c\r
312 +++ b/notmuch-reply.c\r
313 @@ -223,7 +223,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
314         return 1;\r
315      }\r
316  \r
317 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
318 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
319 +                                    NOTMUCH_DATABASE_MODE_READONLY);\r
320      if (notmuch == NULL)\r
321         return 1;\r
322  \r
323 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
324 index b8f99a3..001f3fc 100644\r
325 --- a/notmuch-restore.c\r
326 +++ b/notmuch-restore.c\r
327 @@ -36,7 +36,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
328      if (config == NULL)\r
329         return 1;\r
330  \r
331 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
332 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
333 +                                    NOTMUCH_DATABASE_MODE_WRITABLE);\r
334      if (notmuch == NULL)\r
335         return 1;\r
336  \r
337 diff --git a/notmuch-search.c b/notmuch-search.c\r
338 index 2b1c0fe..cde9735 100644\r
339 --- a/notmuch-search.c\r
340 +++ b/notmuch-search.c\r
341 @@ -129,7 +129,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
342      if (config == NULL)\r
343         return 1;\r
344  \r
345 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
346 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
347 +                                    NOTMUCH_DATABASE_MODE_READONLY);\r
348      if (notmuch == NULL)\r
349         return 1;\r
350  \r
351 diff --git a/notmuch-show.c b/notmuch-show.c\r
352 index a21d223..a764e44 100644\r
353 --- a/notmuch-show.c\r
354 +++ b/notmuch-show.c\r
355 @@ -220,7 +220,8 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
356         return 1;\r
357      }\r
358  \r
359 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
360 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
361 +                                    NOTMUCH_DATABASE_MODE_READONLY);\r
362      if (notmuch == NULL)\r
363         return 1;\r
364  \r
365 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
366 index 12ab86c..75d464f 100644\r
367 --- a/notmuch-tag.c\r
368 +++ b/notmuch-tag.c\r
369 @@ -94,7 +94,8 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
370      if (config == NULL)\r
371         return 1;\r
372  \r
373 -    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
374 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),\r
375 +                                    NOTMUCH_DATABASE_MODE_WRITABLE);\r
376      if (notmuch == NULL)\r
377         return 1;\r
378  \r
379 -- \r
380 1.6.5.3\r
381 \r