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 {
45 notmuch_terms_t terms;
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];
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).
62 _notmuch_message_destructor (notmuch_message_t *message)
64 message->doc.~Document ();
69 /* Create a new notmuch_message_t object for an existing document in
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
79 * The 'talloc_owner' argument can also be NULL, in which case the
80 * caller *is* responsible for calling notmuch_message_destroy.
82 * If no document exists in the database with document ID of 'doc_id'
83 * then this function returns NULL and sets *status to
84 * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
86 * This function can also fail to due lack of available memory,
87 * returning NULL and optionally setting *status to
88 * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY. Caller can pass NULL
89 * for status if uninterested in distinguishing these two cases.
92 _notmuch_message_create (const void *talloc_owner,
93 notmuch_database_t *notmuch,
95 notmuch_private_status_t *status)
97 notmuch_message_t *message;
100 *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
102 message = talloc (talloc_owner, notmuch_message_t);
103 if (unlikely (message == NULL)) {
105 *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
109 message->notmuch = notmuch;
110 message->doc_id = doc_id;
111 message->message_id = NULL; /* lazily created */
112 message->filename = NULL; /* lazily created */
114 /* This is C++'s creepy "placement new", which is really just an
115 * ugly way to call a constructor for a pre-allocated object. So
116 * it's really not an error to not be checking for OUT_OF_MEMORY
117 * here, since this "new" isn't actually allocating memory. This
118 * is language-design comedy of the wrong kind. */
120 new (&message->doc) Xapian::Document;
122 talloc_set_destructor (message, _notmuch_message_destructor);
125 message->doc = notmuch->xapian_db->get_document (doc_id);
126 } catch (const Xapian::DocNotFoundError &error) {
127 talloc_free (message);
129 *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
136 /* Create a new notmuch_message_t object for a specific message ID,
137 * (which may or may not already exist in the databas).
139 * Here, 'talloc owner' is an optional talloc context to which the new
140 * message will belong. This allows for the caller to not bother
141 * calling notmuch_message_destroy on the message, and no that all
142 * memory will be reclaimed with 'talloc_owner' is free. The caller
143 * still can call notmuch_message_destroy when finished with the
144 * message if desired.
146 * The 'talloc_owner' argument can also be NULL, in which case the
147 * caller *is* responsible for calling notmuch_message_destroy.
149 * If there is already a document with message ID 'message_id' in the
150 * database, then the returned message can be used to query/modify the
151 * document. Otherwise, a new document will be inserted into the
152 * database before this function returns.
154 * If an error occurs, this function will return NULL and *status
155 * will be set as appropriate. (The status pointer argument must
159 _notmuch_message_create_for_message_id (const void *talloc_owner,
160 notmuch_database_t *notmuch,
161 const char *message_id,
162 notmuch_status_t *status)
164 notmuch_private_status_t private_status;
165 notmuch_message_t *message;
166 Xapian::Document doc;
170 *status = NOTMUCH_STATUS_SUCCESS;
172 message = notmuch_database_find_message (notmuch, message_id);
174 return talloc_steal (talloc_owner, message);
176 term = talloc_asprintf (NULL, "%s%s",
177 _find_prefix ("id"), message_id);
179 *status = NOTMUCH_STATUS_OUT_OF_MEMORY;
187 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
189 doc_id = notmuch->xapian_db->add_document (doc);
190 } catch (const Xapian::Error &error) {
191 *status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
195 message = _notmuch_message_create (talloc_owner, notmuch,
196 doc_id, &private_status);
198 if (private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)
200 INTERNAL_ERROR ("Failed to find document immediately after adding it.\n");
203 *status = (notmuch_status_t) private_status;
209 notmuch_message_get_message_id (notmuch_message_t *message)
211 Xapian::TermIterator i;
213 if (message->message_id)
214 return message->message_id;
216 i = message->doc.termlist_begin ();
217 i.skip_to (_find_prefix ("id"));
219 if (i == message->doc.termlist_end ()) {
220 INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
224 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
225 return message->message_id;
228 /* Set the filename for 'message' to 'filename'.
230 * XXX: We should still figure out what we want to do for multiple
231 * files with identical message IDs. We will probably want to store a
232 * list of filenames here, (so that this will be "add_filename"
233 * instead of "set_filename"). Which would make this very similar to
236 * This change will not be reflected in the database until the next
237 * call to _notmuch_message_set_sync. */
239 _notmuch_message_set_filename (notmuch_message_t *message,
240 const char *filename)
242 if (message->filename)
243 talloc_free (message->filename);
244 message->doc.set_data (filename);
248 notmuch_message_get_filename (notmuch_message_t *message)
250 std::string filename_str;
252 if (message->filename)
253 return message->filename;
255 filename_str = message->doc.get_data ();
256 message->filename = talloc_strdup (message, filename_str.c_str ());
258 return message->filename;
261 /* We end up having to call the destructors explicitly because we had
262 * to use "placement new" in order to initialize C++ objects within a
263 * block that we allocated with talloc. So C++ is making talloc
264 * slightly less simple to use, (we wouldn't need
265 * talloc_set_destructor at all otherwise).
268 _notmuch_terms_destructor (notmuch_terms_t *terms)
270 terms->iterator.~TermIterator ();
271 terms->iterator_end.~TermIterator ();
277 _notmuch_terms_create (void *ctx,
278 Xapian::Document doc,
279 const char *prefix_name)
281 notmuch_terms_t *terms;
282 const char *prefix = _find_prefix (prefix_name);
284 /* Currently, notmuch_terms_t is written with the assumption that
285 * any prefix its derivatives use will be only a single
287 assert (strlen (prefix) == 1);
289 terms = talloc (ctx, notmuch_terms_t);
290 if (unlikely (terms == NULL))
293 terms->prefix_char = *prefix;
294 new (&terms->iterator) Xapian::TermIterator;
295 new (&terms->iterator_end) Xapian::TermIterator;
297 talloc_set_destructor (terms, _notmuch_terms_destructor);
299 terms->iterator = doc.termlist_begin ();
300 terms->iterator.skip_to (prefix);
301 terms->iterator_end = doc.termlist_end ();
306 /* The assertion is to ensure that 'type' is a derivative of
307 * notmuch_terms_t in that it contains a notmuch_terms_t as its first
308 * member. We do this by name of 'terms' as opposed to type, because
309 * that's as clever as I've been so far. */
310 #define _notmuch_terms_create_type(ctx, doc, prefix_name, type) \
311 (COMPILE_TIME_ASSERT(offsetof(type, terms) == 0), \
312 (type *) _notmuch_terms_create (ctx, doc, prefix_name))
315 notmuch_message_get_tags (notmuch_message_t *message)
317 return _notmuch_terms_create_type (message, message->doc, "tag",
321 notmuch_thread_ids_t *
322 notmuch_message_get_thread_ids (notmuch_message_t *message)
324 return _notmuch_terms_create_type (message, message->doc, "thread",
325 notmuch_thread_ids_t);
329 _notmuch_message_set_date (notmuch_message_t *message,
334 time_value = notmuch_parse_date (date, NULL);
336 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
337 Xapian::sortable_serialise (time_value));
341 _notmuch_message_add_thread_id (notmuch_message_t *message,
342 const char *thread_id)
344 _notmuch_message_add_term (message, "thread", thread_id);
348 thread_id_generate (thread_id_t *thread_id)
350 static int seeded = 0;
357 dev_random = fopen ("/dev/random", "r");
358 if (dev_random == NULL) {
361 fread ((void *) &value, sizeof (value), 1, dev_random);
369 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
371 sprintf (s, "%08x", value);
377 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
379 /* If not part of any existing thread, generate a new thread_id. */
380 thread_id_t thread_id;
382 thread_id_generate (&thread_id);
383 _notmuch_message_add_term (message, "thread", thread_id.str);
386 /* Synchronize changes made to message->doc out into the database. */
388 _notmuch_message_sync (notmuch_message_t *message)
390 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
392 db->replace_document (message->doc_id, message->doc);
395 /* Add a name:value term to 'message', (the actual term will be
396 * encoded by prefixing the value with a short prefix). See
397 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
398 * names to prefix values.
400 * This change will not be reflected in the database until the next
401 * call to _notmuch_message_set_sync. */
402 notmuch_private_status_t
403 _notmuch_message_add_term (notmuch_message_t *message,
404 const char *prefix_name,
411 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
413 term = talloc_asprintf (message, "%s%s",
414 _find_prefix (prefix_name), value);
416 if (strlen (term) > NOTMUCH_TERM_MAX)
417 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
419 message->doc.add_term (term);
423 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
426 /* Remove a name:value term from 'message', (the actual term will be
427 * encoded by prefixing the value with a short prefix). See
428 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
429 * names to prefix values.
431 * This change will not be reflected in the database until the next
432 * call to _notmuch_message_set_sync. */
433 notmuch_private_status_t
434 _notmuch_message_remove_term (notmuch_message_t *message,
435 const char *prefix_name,
441 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
443 term = talloc_asprintf (message, "%s%s",
444 _find_prefix (prefix_name), value);
446 if (strlen (term) > NOTMUCH_TERM_MAX)
447 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
449 message->doc.remove_term (term);
453 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
457 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
459 notmuch_private_status_t status;
462 return NOTMUCH_STATUS_NULL_POINTER;
464 if (strlen (tag) > NOTMUCH_TAG_MAX)
465 return NOTMUCH_STATUS_TAG_TOO_LONG;
467 status = _notmuch_message_add_term (message, "tag", tag);
469 INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
473 _notmuch_message_sync (message);
475 return NOTMUCH_STATUS_SUCCESS;
479 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
481 notmuch_private_status_t status;
484 return NOTMUCH_STATUS_NULL_POINTER;
486 if (strlen (tag) > NOTMUCH_TAG_MAX)
487 return NOTMUCH_STATUS_TAG_TOO_LONG;
489 status = _notmuch_message_remove_term (message, "tag", tag);
491 INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
495 _notmuch_message_sync (message);
497 return NOTMUCH_STATUS_SUCCESS;
501 notmuch_message_destroy (notmuch_message_t *message)
503 talloc_free (message);
507 _notmuch_terms_has_more (notmuch_terms_t *terms)
511 if (terms->iterator == terms->iterator_end)
514 s = *terms->iterator;
515 if (! s.empty () && s[0] == terms->prefix_char)
522 _notmuch_terms_get (notmuch_terms_t *terms)
524 return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
528 _notmuch_terms_advance (notmuch_terms_t *terms)
534 _notmuch_terms_destroy (notmuch_terms_t *terms)
540 notmuch_tags_has_more (notmuch_tags_t *tags)
542 return _notmuch_terms_has_more (&tags->terms);
546 notmuch_tags_get (notmuch_tags_t *tags)
548 return _notmuch_terms_get (&tags->terms);
552 notmuch_tags_advance (notmuch_tags_t *tags)
554 return _notmuch_terms_advance (&tags->terms);
558 notmuch_tags_destroy (notmuch_tags_t *tags)
560 return _notmuch_terms_destroy (&tags->terms);
564 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
566 return _notmuch_terms_has_more (&thread_ids->terms);
570 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
572 return _notmuch_terms_get (&thread_ids->terms);
576 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
578 return _notmuch_terms_advance (&thread_ids->terms);
582 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
584 return _notmuch_terms_destroy (&thread_ids->terms);