]> git.notmuchmail.org Git - notmuch/blob - message.cc
Convert notmuch_thread_ids_t to notmuch_terms_t
[notmuch] / message.cc
1 /* message.cc - Results of message-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <xapian.h>
25
26 struct _notmuch_message {
27     notmuch_database_t *notmuch;
28     Xapian::docid doc_id;
29     char *message_id;
30     char *filename;
31     Xapian::Document doc;
32 };
33
34 typedef struct _notmuch_terms {
35     char prefix_char;
36     Xapian::TermIterator iterator;
37     Xapian::TermIterator iterator_end;
38 } notmuch_terms_t;
39
40 struct _notmuch_tags {
41     notmuch_terms_t terms;
42 };
43
44 struct _notmuch_thread_ids {
45     notmuch_terms_t terms;
46 };
47
48 /* "128 bits of thread-id ought to be enough for anybody" */
49 #define NOTMUCH_THREAD_ID_BITS   128
50 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
51 typedef struct _thread_id {
52     char str[NOTMUCH_THREAD_ID_DIGITS + 1];
53 } thread_id_t;
54
55 /* We end up having to call the destructor explicitly because we had
56  * to use "placement new" in order to initialize C++ objects within a
57  * block that we allocated with talloc. So C++ is making talloc
58  * slightly less simple to use, (we wouldn't need
59  * talloc_set_destructor at all otherwise).
60  */
61 static int
62 _notmuch_message_destructor (notmuch_message_t *message)
63 {
64     message->doc.~Document ();
65
66     return 0;
67 }
68
69 /* Create a new notmuch_message_t object for an existing document in
70  * the database.
71  *
72  * Here, 'talloc owner' is an optional talloc context to which the new
73  * message will belong. This allows for the caller to not bother
74  * calling notmuch_message_destroy on the message, and no that all
75  * memory will be reclaimed with 'talloc_owner' is free. The caller
76  * still can call notmuch_message_destroy when finished with the
77  * message if desired.
78  *
79  * The 'talloc_owner' argument can also be NULL, in which case the
80  * caller *is* responsible for calling notmuch_message_destroy.
81  *
82  * If no document exists in the database with document ID of 'doc_id'
83  * then this function returns NULL.
84  */
85 notmuch_message_t *
86 _notmuch_message_create (const void *talloc_owner,
87                          notmuch_database_t *notmuch,
88                          unsigned int doc_id)
89 {
90     notmuch_message_t *message;
91
92     message = talloc (talloc_owner, notmuch_message_t);
93     if (unlikely (message == NULL))
94         return NULL;
95
96     message->notmuch = notmuch;
97     message->doc_id = doc_id;
98     message->message_id = NULL; /* lazily created */
99     message->filename = NULL; /* lazily created */
100     new (&message->doc) Xapian::Document;
101
102     talloc_set_destructor (message, _notmuch_message_destructor);
103
104     try {
105         message->doc = notmuch->xapian_db->get_document (doc_id);
106     } catch (const Xapian::DocNotFoundError &error) {
107         talloc_free (message);
108         return NULL;
109     }
110
111     return message;
112 }
113
114 /* Create a new notmuch_message_t object for a specific message ID,
115  * (which may or may not already exist in the databas).
116  *
117  * Here, 'talloc owner' is an optional talloc context to which the new
118  * message will belong. This allows for the caller to not bother
119  * calling notmuch_message_destroy on the message, and no that all
120  * memory will be reclaimed with 'talloc_owner' is free. The caller
121  * still can call notmuch_message_destroy when finished with the
122  * message if desired.
123  *
124  * The 'talloc_owner' argument can also be NULL, in which case the
125  * caller *is* responsible for calling notmuch_message_destroy.
126  *
127  * If there is already a document with message ID 'message_id' in the
128  * database, then the returned message can be used to query/modify the
129  * document. Otherwise, a new document will be inserted into the
130  * database before this function returns;
131  */
132 notmuch_message_t *
133 _notmuch_message_create_for_message_id (const void *talloc_owner,
134                                         notmuch_database_t *notmuch,
135                                         const char *message_id)
136 {
137     notmuch_message_t *message;
138     Xapian::Document doc;
139     unsigned int doc_id;
140     char *term;
141
142     message = notmuch_database_find_message (notmuch, message_id);
143     if (message)
144         return talloc_steal (talloc_owner, message);
145
146     term = talloc_asprintf (NULL, "%s%s",
147                             _find_prefix ("id"), message_id);
148     doc.add_term (term);
149     talloc_free (term);
150
151     doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
152
153     doc_id = notmuch->xapian_db->add_document (doc);
154
155     return _notmuch_message_create (talloc_owner, notmuch, doc_id);
156 }
157
158 const char *
159 notmuch_message_get_message_id (notmuch_message_t *message)
160 {
161     Xapian::TermIterator i;
162
163     if (message->message_id)
164         return message->message_id;
165
166     i = message->doc.termlist_begin ();
167     i.skip_to (_find_prefix ("id"));
168
169     if (i == message->doc.termlist_end ()) {
170         fprintf (stderr, "Internal error: Message with document ID of %d has no message ID.\n",
171                  message->doc_id);
172         exit (1);
173     }
174
175     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
176     return message->message_id;
177 }
178
179 /* Set the filename for 'message' to 'filename'.
180  *
181  * XXX: We should still figure out what we want to do for multiple
182  * files with identical message IDs. We will probably want to store a
183  * list of filenames here, (so that this will be "add_filename"
184  * instead of "set_filename"). Which would make this very similar to
185  * add_thread_ids.
186  *
187  * This change will not be reflected in the database until the next
188  * call to _notmuch_message_set_sync. */
189 void
190 _notmuch_message_set_filename (notmuch_message_t *message,
191                                const char *filename)
192 {
193     if (message->filename)
194         talloc_free (message->filename);
195     message->doc.set_data (filename);
196 }
197
198 const char *
199 notmuch_message_get_filename (notmuch_message_t *message)
200 {
201     std::string filename_str;
202
203     if (message->filename)
204         return message->filename;
205
206     filename_str = message->doc.get_data ();
207     message->filename = talloc_strdup (message, filename_str.c_str ());
208
209     return message->filename;
210 }
211
212 /* We end up having to call the destructors explicitly because we had
213  * to use "placement new" in order to initialize C++ objects within a
214  * block that we allocated with talloc. So C++ is making talloc
215  * slightly less simple to use, (we wouldn't need
216  * talloc_set_destructor at all otherwise).
217  */
218 static int
219 _notmuch_terms_destructor (notmuch_terms_t *terms)
220 {
221     terms->iterator.~TermIterator ();
222     terms->iterator_end.~TermIterator ();
223
224     return 0;
225 }
226
227 notmuch_terms_t *
228 _notmuch_terms_create (void *ctx,
229                        Xapian::Document doc,
230                        const char *prefix_name)
231 {
232     notmuch_terms_t *terms;
233     const char *prefix = _find_prefix (prefix_name);
234
235     /* Currently, notmuch_terms_t is written with the assumption that
236      * any prefix its derivatives use will be only a single
237      * character. */
238     assert (strlen (prefix) == 1);
239
240     terms = talloc (ctx, notmuch_terms_t);
241     if (unlikely (terms == NULL))
242         return NULL;
243
244     terms->prefix_char = *prefix;
245     new (&terms->iterator) Xapian::TermIterator;
246     new (&terms->iterator_end) Xapian::TermIterator;
247
248     talloc_set_destructor (terms, _notmuch_terms_destructor);
249
250     terms->iterator = doc.termlist_begin ();
251     terms->iterator.skip_to (prefix);
252     terms->iterator_end = doc.termlist_end ();
253
254     return terms;
255 }
256
257 /* The assertion is to ensure that 'type' is a derivative of
258  * notmuch_terms_t in that it contains a notmuch_terms_t as its first
259  * member. We do this by name of 'terms' as opposed to type, because
260  * that's as clever as I've been so far. */
261 #define _notmuch_terms_create_type(ctx, doc, prefix_name, type) \
262     (COMPILE_TIME_ASSERT(offsetof(type, terms) == 0),           \
263      (type *) _notmuch_terms_create (ctx, doc, prefix_name))
264
265 notmuch_tags_t *
266 notmuch_message_get_tags (notmuch_message_t *message)
267 {
268     return _notmuch_terms_create_type (message, message->doc, "tag",
269                                        notmuch_tags_t);
270 }
271
272 notmuch_thread_ids_t *
273 notmuch_message_get_thread_ids (notmuch_message_t *message)
274 {
275     return _notmuch_terms_create_type (message, message->doc, "thread",
276                                        notmuch_thread_ids_t);
277 }
278
279 void
280 _notmuch_message_set_date (notmuch_message_t *message,
281                            const char *date)
282 {
283     time_t time_value;
284
285     time_value = notmuch_parse_date (date, NULL);
286
287     message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
288                             Xapian::sortable_serialise (time_value));
289 }
290
291 void
292 _notmuch_message_add_thread_id (notmuch_message_t *message,
293                                 const char *thread_id)
294 {
295     std::string id_str;
296
297     _notmuch_message_add_term (message, "thread", thread_id);
298
299     id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
300
301     if (id_str.empty ()) {
302         message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id);
303     } else {
304         size_t pos;
305
306         /* Think about using a hash here if there's any performance
307          * problem. */
308         pos = id_str.find (thread_id);
309         if (pos == std::string::npos) {
310             id_str.append (",");
311             id_str.append (thread_id);
312             message->doc.add_value (NOTMUCH_VALUE_THREAD, id_str);
313         }
314     }
315 }
316
317 static void
318 thread_id_generate (thread_id_t *thread_id)
319 {
320     static int seeded = 0;
321     FILE *dev_random;
322     uint32_t value;
323     char *s;
324     int i;
325
326     if (! seeded) {
327         dev_random = fopen ("/dev/random", "r");
328         if (dev_random == NULL) {
329             srand (time (NULL));
330         } else {
331             fread ((void *) &value, sizeof (value), 1, dev_random);
332             srand (value);
333             fclose (dev_random);
334         }
335         seeded = 1;
336     }
337
338     s = thread_id->str;
339     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
340         value = rand ();
341         sprintf (s, "%08x", value);
342         s += 8;
343     }
344 }
345
346 void
347 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
348 {
349     /* If not part of any existing thread, generate a new thread_id. */
350     thread_id_t thread_id;
351
352     thread_id_generate (&thread_id);
353     _notmuch_message_add_term (message, "thread", thread_id.str);
354     message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id.str);
355 }
356
357 /* Synchronize changes made to message->doc out into the database. */
358 void
359 _notmuch_message_sync (notmuch_message_t *message)
360 {
361     Xapian::WritableDatabase *db = message->notmuch->xapian_db;
362
363     db->replace_document (message->doc_id, message->doc);
364 }
365
366 /* Add a name:value term to 'message', (the actual term will be
367  * encoded by prefixing the value with a short prefix). See
368  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
369  * names to prefix values.
370  *
371  * This change will not be reflected in the database until the next
372  * call to _notmuch_message_set_sync. */
373 notmuch_private_status_t
374 _notmuch_message_add_term (notmuch_message_t *message,
375                            const char *prefix_name,
376                            const char *value)
377 {
378
379     char *term;
380
381     if (value == NULL)
382         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
383
384     term = talloc_asprintf (message, "%s%s",
385                             _find_prefix (prefix_name), value);
386
387     if (strlen (term) > NOTMUCH_TERM_MAX)
388         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
389
390     message->doc.add_term (term);
391
392     talloc_free (term);
393
394     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
395 }
396
397 /* Remove a name:value term from 'message', (the actual term will be
398  * encoded by prefixing the value with a short prefix). See
399  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
400  * names to prefix values.
401  *
402  * This change will not be reflected in the database until the next
403  * call to _notmuch_message_set_sync. */
404 notmuch_private_status_t
405 _notmuch_message_remove_term (notmuch_message_t *message,
406                               const char *prefix_name,
407                               const char *value)
408 {
409     char *term;
410
411     if (value == NULL)
412         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
413
414     term = talloc_asprintf (message, "%s%s",
415                             _find_prefix (prefix_name), value);
416
417     if (strlen (term) > NOTMUCH_TERM_MAX)
418         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
419
420     message->doc.remove_term (term);
421
422     talloc_free (term);
423
424     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
425 }
426
427 notmuch_status_t
428 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
429 {
430     notmuch_private_status_t status;
431
432     if (tag == NULL)
433         return NOTMUCH_STATUS_NULL_POINTER;
434
435     if (strlen (tag) > NOTMUCH_TAG_MAX)
436         return NOTMUCH_STATUS_TAG_TOO_LONG;
437
438     status = _notmuch_message_add_term (message, "tag", tag);
439     if (status) {
440         fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
441                  status);
442         exit (1);
443     }
444
445     _notmuch_message_sync (message);
446
447     return NOTMUCH_STATUS_SUCCESS;
448 }
449
450 notmuch_status_t
451 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
452 {
453     notmuch_private_status_t status;
454
455     if (tag == NULL)
456         return NOTMUCH_STATUS_NULL_POINTER;
457
458     if (strlen (tag) > NOTMUCH_TAG_MAX)
459         return NOTMUCH_STATUS_TAG_TOO_LONG;
460
461     status = _notmuch_message_remove_term (message, "tag", tag);
462     if (status) {
463         fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
464                  status);
465         exit (1);
466     }
467
468     _notmuch_message_sync (message);
469
470     return NOTMUCH_STATUS_SUCCESS;
471 }
472
473 void
474 notmuch_message_destroy (notmuch_message_t *message)
475 {
476     talloc_free (message);
477 }
478
479 notmuch_bool_t
480 _notmuch_terms_has_more (notmuch_terms_t *terms)
481 {
482     std::string s;
483
484     if (terms->iterator == terms->iterator_end)
485         return FALSE;
486
487     s = *terms->iterator;
488     if (! s.empty () && s[0] == terms->prefix_char)
489         return TRUE;
490     else
491         return FALSE;
492 }
493
494 const char *
495 _notmuch_terms_get (notmuch_terms_t *terms)
496 {
497     return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
498 }
499
500 void
501 _notmuch_terms_advance (notmuch_terms_t *terms)
502 {
503     terms->iterator++;
504 }
505
506 void
507 _notmuch_terms_destroy (notmuch_terms_t *terms)
508 {
509     talloc_free (terms);
510 }
511
512 notmuch_bool_t
513 notmuch_tags_has_more (notmuch_tags_t *tags)
514 {
515     return _notmuch_terms_has_more (&tags->terms);
516 }
517
518 const char *
519 notmuch_tags_get (notmuch_tags_t *tags)
520 {
521     return _notmuch_terms_get (&tags->terms);
522 }
523
524 void
525 notmuch_tags_advance (notmuch_tags_t *tags)
526 {
527     return _notmuch_terms_advance (&tags->terms);
528 }
529
530 void
531 notmuch_tags_destroy (notmuch_tags_t *tags)
532 {
533     return _notmuch_terms_destroy (&tags->terms);
534 }
535
536 notmuch_bool_t
537 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
538 {
539     return _notmuch_terms_has_more (&thread_ids->terms);
540 }
541
542 const char *
543 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
544 {
545     return _notmuch_terms_get (&thread_ids->terms);
546 }
547
548 void
549 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
550 {
551     return _notmuch_terms_advance (&thread_ids->terms);
552 }
553
554 void
555 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
556 {
557     return _notmuch_terms_destroy (&thread_ids->terms);
558 }