1 /* message.cc - Results of message-based searches from a notmuch database
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-private.h"
22 #include "database-private.h"
26 struct _notmuch_message {
27 notmuch_database_t *notmuch;
34 typedef struct _notmuch_terms {
36 Xapian::TermIterator iterator;
37 Xapian::TermIterator iterator_end;
40 struct _notmuch_tags {
41 notmuch_terms_t terms;
44 struct _notmuch_thread_ids {
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];
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).
63 _notmuch_message_destructor (notmuch_message_t *message)
65 message->doc.~Document ();
70 /* Create a new notmuch_message_t object for an existing document in
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
80 * The 'talloc_owner' argument can also be NULL, in which case the
81 * caller *is* responsible for calling notmuch_message_destroy.
83 * If no document exists in the database with document ID of 'doc_id'
84 * then this function returns NULL.
87 _notmuch_message_create (const void *talloc_owner,
88 notmuch_database_t *notmuch,
91 notmuch_message_t *message;
93 message = talloc (talloc_owner, notmuch_message_t);
94 if (unlikely (message == NULL))
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;
103 talloc_set_destructor (message, _notmuch_message_destructor);
106 message->doc = notmuch->xapian_db->get_document (doc_id);
107 } catch (const Xapian::DocNotFoundError &error) {
108 talloc_free (message);
115 /* Create a new notmuch_message_t object for a specific message ID,
116 * (which may or may not already exist in the databas).
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.
125 * The 'talloc_owner' argument can also be NULL, in which case the
126 * caller *is* responsible for calling notmuch_message_destroy.
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;
134 _notmuch_message_create_for_message_id (const void *talloc_owner,
135 notmuch_database_t *notmuch,
136 const char *message_id)
138 notmuch_message_t *message;
139 Xapian::Document doc;
143 message = notmuch_database_find_message (notmuch, message_id);
145 return talloc_steal (talloc_owner, message);
147 term = talloc_asprintf (NULL, "%s%s",
148 _find_prefix ("id"), message_id);
152 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
154 doc_id = notmuch->xapian_db->add_document (doc);
156 return _notmuch_message_create (talloc_owner, notmuch, doc_id);
160 notmuch_message_get_message_id (notmuch_message_t *message)
162 Xapian::TermIterator i;
164 if (message->message_id)
165 return message->message_id;
167 i = message->doc.termlist_begin ();
168 i.skip_to (_find_prefix ("id"));
170 if (i == message->doc.termlist_end ()) {
171 fprintf (stderr, "Internal error: Message with document ID of %d has no message ID.\n",
176 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
177 return message->message_id;
180 /* Set the filename for 'message' to 'filename'.
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
188 * This change will not be reflected in the database until the next
189 * call to _notmuch_message_set_sync. */
191 _notmuch_message_set_filename (notmuch_message_t *message,
192 const char *filename)
194 if (message->filename)
195 talloc_free (message->filename);
196 message->doc.set_data (filename);
200 notmuch_message_get_filename (notmuch_message_t *message)
202 std::string filename_str;
204 if (message->filename)
205 return message->filename;
207 filename_str = message->doc.get_data ();
208 message->filename = talloc_strdup (message, filename_str.c_str ());
210 return message->filename;
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).
220 _notmuch_terms_destructor (notmuch_terms_t *terms)
222 terms->iterator.~TermIterator ();
223 terms->iterator_end.~TermIterator ();
229 _notmuch_terms_create (void *ctx,
230 Xapian::Document doc,
231 const char *prefix_name)
233 notmuch_terms_t *terms;
234 const char *prefix = _find_prefix (prefix_name);
236 /* Currently, notmuch_terms_t is written with the assumption that
237 * any prefix its derivatives use will be only a single
239 assert (strlen (prefix) == 1);
241 terms = talloc (ctx, notmuch_terms_t);
242 if (unlikely (terms == NULL))
245 terms->prefix_char = *prefix;
246 new (&terms->iterator) Xapian::TermIterator;
247 new (&terms->iterator_end) Xapian::TermIterator;
249 talloc_set_destructor (terms, _notmuch_terms_destructor);
251 terms->iterator = doc.termlist_begin ();
252 terms->iterator.skip_to (prefix);
253 terms->iterator_end = doc.termlist_end ();
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))
263 notmuch_message_get_tags (notmuch_message_t *message)
265 return _notmuch_terms_create_type (message, message->doc, "tag",
269 notmuch_thread_ids_t *
270 notmuch_message_get_thread_ids (notmuch_message_t *message)
272 notmuch_thread_ids_t *thread_ids;
275 thread_ids = talloc (message, notmuch_thread_ids_t);
276 if (unlikely (thread_ids == NULL))
279 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
280 thread_ids->next = talloc_strdup (message, id_str.c_str ());
282 /* Initialize thread_ids->current and terminate first ID. */
283 notmuch_thread_ids_advance (thread_ids);
289 _notmuch_message_set_date (notmuch_message_t *message,
294 time_value = notmuch_parse_date (date, NULL);
296 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
297 Xapian::sortable_serialise (time_value));
301 _notmuch_message_add_thread_id (notmuch_message_t *message,
302 const char *thread_id)
306 _notmuch_message_add_term (message, "thread", thread_id);
308 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
310 if (id_str.empty ()) {
311 message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id);
315 /* Think about using a hash here if there's any performance
317 pos = id_str.find (thread_id);
318 if (pos == std::string::npos) {
320 id_str.append (thread_id);
321 message->doc.add_value (NOTMUCH_VALUE_THREAD, id_str);
327 thread_id_generate (thread_id_t *thread_id)
329 static int seeded = 0;
336 dev_random = fopen ("/dev/random", "r");
337 if (dev_random == NULL) {
340 fread ((void *) &value, sizeof (value), 1, dev_random);
348 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
350 sprintf (s, "%08x", value);
356 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
358 /* If not part of any existing thread, generate a new thread_id. */
359 thread_id_t thread_id;
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);
366 /* Synchronize changes made to message->doc out into the database. */
368 _notmuch_message_sync (notmuch_message_t *message)
370 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
372 db->replace_document (message->doc_id, message->doc);
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.
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,
391 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
393 term = talloc_asprintf (message, "%s%s",
394 _find_prefix (prefix_name), value);
396 if (strlen (term) > NOTMUCH_TERM_MAX)
397 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
399 message->doc.add_term (term);
403 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
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.
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,
421 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
423 term = talloc_asprintf (message, "%s%s",
424 _find_prefix (prefix_name), value);
426 if (strlen (term) > NOTMUCH_TERM_MAX)
427 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
429 message->doc.remove_term (term);
433 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
437 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
439 notmuch_private_status_t status;
442 return NOTMUCH_STATUS_NULL_POINTER;
444 if (strlen (tag) > NOTMUCH_TAG_MAX)
445 return NOTMUCH_STATUS_TAG_TOO_LONG;
447 status = _notmuch_message_add_term (message, "tag", tag);
449 fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
454 _notmuch_message_sync (message);
456 return NOTMUCH_STATUS_SUCCESS;
460 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
462 notmuch_private_status_t status;
465 return NOTMUCH_STATUS_NULL_POINTER;
467 if (strlen (tag) > NOTMUCH_TAG_MAX)
468 return NOTMUCH_STATUS_TAG_TOO_LONG;
470 status = _notmuch_message_remove_term (message, "tag", tag);
472 fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
477 _notmuch_message_sync (message);
479 return NOTMUCH_STATUS_SUCCESS;
483 notmuch_message_destroy (notmuch_message_t *message)
485 talloc_free (message);
489 _notmuch_terms_has_more (notmuch_terms_t *terms)
493 if (terms->iterator == terms->iterator_end)
496 s = *terms->iterator;
497 if (! s.empty () && s[0] == terms->prefix_char)
504 _notmuch_terms_get (notmuch_terms_t *terms)
506 return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
510 _notmuch_terms_advance (notmuch_terms_t *terms)
516 _notmuch_terms_destroy (notmuch_terms_t *terms)
522 notmuch_tags_has_more (notmuch_tags_t *tags)
524 return _notmuch_terms_has_more (&tags->terms);
528 notmuch_tags_get (notmuch_tags_t *tags)
530 return _notmuch_terms_get (&tags->terms);
534 notmuch_tags_advance (notmuch_tags_t *tags)
536 return _notmuch_terms_advance (&tags->terms);
540 notmuch_tags_destroy (notmuch_tags_t *tags)
542 return _notmuch_terms_destroy (&tags->terms);
546 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
548 if (thread_ids->current == NULL || *thread_ids->current == '\0')
555 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
557 return thread_ids->current;
561 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
563 thread_ids->current = strsep (&thread_ids->next, ",");
567 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
569 talloc_free (thread_ids);