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.
86 _notmuch_message_create (const void *talloc_owner,
87 notmuch_database_t *notmuch,
90 notmuch_message_t *message;
92 message = talloc (talloc_owner, notmuch_message_t);
93 if (unlikely (message == NULL))
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;
102 talloc_set_destructor (message, _notmuch_message_destructor);
105 message->doc = notmuch->xapian_db->get_document (doc_id);
106 } catch (const Xapian::DocNotFoundError &error) {
107 talloc_free (message);
114 /* Create a new notmuch_message_t object for a specific message ID,
115 * (which may or may not already exist in the databas).
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.
124 * The 'talloc_owner' argument can also be NULL, in which case the
125 * caller *is* responsible for calling notmuch_message_destroy.
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;
133 _notmuch_message_create_for_message_id (const void *talloc_owner,
134 notmuch_database_t *notmuch,
135 const char *message_id)
137 notmuch_message_t *message;
138 Xapian::Document doc;
142 message = notmuch_database_find_message (notmuch, message_id);
144 return talloc_steal (talloc_owner, message);
146 term = talloc_asprintf (NULL, "%s%s",
147 _find_prefix ("id"), message_id);
151 doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
153 doc_id = notmuch->xapian_db->add_document (doc);
155 return _notmuch_message_create (talloc_owner, notmuch, doc_id);
159 notmuch_message_get_message_id (notmuch_message_t *message)
161 Xapian::TermIterator i;
163 if (message->message_id)
164 return message->message_id;
166 i = message->doc.termlist_begin ();
167 i.skip_to (_find_prefix ("id"));
169 if (i == message->doc.termlist_end ()) {
170 fprintf (stderr, "Internal error: Message with document ID of %d has no message ID.\n",
175 message->message_id = talloc_strdup (message, (*i).c_str () + 1);
176 return message->message_id;
179 /* Set the filename for 'message' to 'filename'.
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
187 * This change will not be reflected in the database until the next
188 * call to _notmuch_message_set_sync. */
190 _notmuch_message_set_filename (notmuch_message_t *message,
191 const char *filename)
193 if (message->filename)
194 talloc_free (message->filename);
195 message->doc.set_data (filename);
199 notmuch_message_get_filename (notmuch_message_t *message)
201 std::string filename_str;
203 if (message->filename)
204 return message->filename;
206 filename_str = message->doc.get_data ();
207 message->filename = talloc_strdup (message, filename_str.c_str ());
209 return message->filename;
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).
219 _notmuch_terms_destructor (notmuch_terms_t *terms)
221 terms->iterator.~TermIterator ();
222 terms->iterator_end.~TermIterator ();
228 _notmuch_terms_create (void *ctx,
229 Xapian::Document doc,
230 const char *prefix_name)
232 notmuch_terms_t *terms;
233 const char *prefix = _find_prefix (prefix_name);
235 /* Currently, notmuch_terms_t is written with the assumption that
236 * any prefix its derivatives use will be only a single
238 assert (strlen (prefix) == 1);
240 terms = talloc (ctx, notmuch_terms_t);
241 if (unlikely (terms == NULL))
244 terms->prefix_char = *prefix;
245 new (&terms->iterator) Xapian::TermIterator;
246 new (&terms->iterator_end) Xapian::TermIterator;
248 talloc_set_destructor (terms, _notmuch_terms_destructor);
250 terms->iterator = doc.termlist_begin ();
251 terms->iterator.skip_to (prefix);
252 terms->iterator_end = doc.termlist_end ();
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))
266 notmuch_message_get_tags (notmuch_message_t *message)
268 return _notmuch_terms_create_type (message, message->doc, "tag",
272 notmuch_thread_ids_t *
273 notmuch_message_get_thread_ids (notmuch_message_t *message)
275 return _notmuch_terms_create_type (message, message->doc, "thread",
276 notmuch_thread_ids_t);
280 _notmuch_message_set_date (notmuch_message_t *message,
285 time_value = notmuch_parse_date (date, NULL);
287 message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
288 Xapian::sortable_serialise (time_value));
292 _notmuch_message_add_thread_id (notmuch_message_t *message,
293 const char *thread_id)
297 _notmuch_message_add_term (message, "thread", thread_id);
299 id_str = message->doc.get_value (NOTMUCH_VALUE_THREAD);
301 if (id_str.empty ()) {
302 message->doc.add_value (NOTMUCH_VALUE_THREAD, thread_id);
306 /* Think about using a hash here if there's any performance
308 pos = id_str.find (thread_id);
309 if (pos == std::string::npos) {
311 id_str.append (thread_id);
312 message->doc.add_value (NOTMUCH_VALUE_THREAD, id_str);
318 thread_id_generate (thread_id_t *thread_id)
320 static int seeded = 0;
327 dev_random = fopen ("/dev/random", "r");
328 if (dev_random == NULL) {
331 fread ((void *) &value, sizeof (value), 1, dev_random);
339 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
341 sprintf (s, "%08x", value);
347 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
349 /* If not part of any existing thread, generate a new thread_id. */
350 thread_id_t thread_id;
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);
357 /* Synchronize changes made to message->doc out into the database. */
359 _notmuch_message_sync (notmuch_message_t *message)
361 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
363 db->replace_document (message->doc_id, message->doc);
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.
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,
382 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
384 term = talloc_asprintf (message, "%s%s",
385 _find_prefix (prefix_name), value);
387 if (strlen (term) > NOTMUCH_TERM_MAX)
388 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
390 message->doc.add_term (term);
394 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
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.
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,
412 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
414 term = talloc_asprintf (message, "%s%s",
415 _find_prefix (prefix_name), value);
417 if (strlen (term) > NOTMUCH_TERM_MAX)
418 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
420 message->doc.remove_term (term);
424 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
428 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
430 notmuch_private_status_t status;
433 return NOTMUCH_STATUS_NULL_POINTER;
435 if (strlen (tag) > NOTMUCH_TAG_MAX)
436 return NOTMUCH_STATUS_TAG_TOO_LONG;
438 status = _notmuch_message_add_term (message, "tag", tag);
440 fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
445 _notmuch_message_sync (message);
447 return NOTMUCH_STATUS_SUCCESS;
451 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
453 notmuch_private_status_t status;
456 return NOTMUCH_STATUS_NULL_POINTER;
458 if (strlen (tag) > NOTMUCH_TAG_MAX)
459 return NOTMUCH_STATUS_TAG_TOO_LONG;
461 status = _notmuch_message_remove_term (message, "tag", tag);
463 fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
468 _notmuch_message_sync (message);
470 return NOTMUCH_STATUS_SUCCESS;
474 notmuch_message_destroy (notmuch_message_t *message)
476 talloc_free (message);
480 _notmuch_terms_has_more (notmuch_terms_t *terms)
484 if (terms->iterator == terms->iterator_end)
487 s = *terms->iterator;
488 if (! s.empty () && s[0] == terms->prefix_char)
495 _notmuch_terms_get (notmuch_terms_t *terms)
497 return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
501 _notmuch_terms_advance (notmuch_terms_t *terms)
507 _notmuch_terms_destroy (notmuch_terms_t *terms)
513 notmuch_tags_has_more (notmuch_tags_t *tags)
515 return _notmuch_terms_has_more (&tags->terms);
519 notmuch_tags_get (notmuch_tags_t *tags)
521 return _notmuch_terms_get (&tags->terms);
525 notmuch_tags_advance (notmuch_tags_t *tags)
527 return _notmuch_terms_advance (&tags->terms);
531 notmuch_tags_destroy (notmuch_tags_t *tags)
533 return _notmuch_terms_destroy (&tags->terms);
537 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
539 return _notmuch_terms_has_more (&thread_ids->terms);
543 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
545 return _notmuch_terms_get (&thread_ids->terms);
549 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
551 return _notmuch_terms_advance (&thread_ids->terms);
555 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
557 return _notmuch_terms_destroy (&thread_ids->terms);