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