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)
295 _notmuch_message_add_term (message, "thread", thread_id);
299 thread_id_generate (thread_id_t *thread_id)
301 static int seeded = 0;
308 dev_random = fopen ("/dev/random", "r");
309 if (dev_random == NULL) {
312 fread ((void *) &value, sizeof (value), 1, dev_random);
320 for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
322 sprintf (s, "%08x", value);
328 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
330 /* If not part of any existing thread, generate a new thread_id. */
331 thread_id_t thread_id;
333 thread_id_generate (&thread_id);
334 _notmuch_message_add_term (message, "thread", thread_id.str);
337 /* Synchronize changes made to message->doc out into the database. */
339 _notmuch_message_sync (notmuch_message_t *message)
341 Xapian::WritableDatabase *db = message->notmuch->xapian_db;
343 db->replace_document (message->doc_id, message->doc);
346 /* Add a name:value term to 'message', (the actual term will be
347 * encoded by prefixing the value with a short prefix). See
348 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
349 * names to prefix values.
351 * This change will not be reflected in the database until the next
352 * call to _notmuch_message_set_sync. */
353 notmuch_private_status_t
354 _notmuch_message_add_term (notmuch_message_t *message,
355 const char *prefix_name,
362 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
364 term = talloc_asprintf (message, "%s%s",
365 _find_prefix (prefix_name), value);
367 if (strlen (term) > NOTMUCH_TERM_MAX)
368 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
370 message->doc.add_term (term);
374 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
377 /* Remove a name:value term from 'message', (the actual term will be
378 * encoded by prefixing the value with a short prefix). See
379 * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
380 * names to prefix values.
382 * This change will not be reflected in the database until the next
383 * call to _notmuch_message_set_sync. */
384 notmuch_private_status_t
385 _notmuch_message_remove_term (notmuch_message_t *message,
386 const char *prefix_name,
392 return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
394 term = talloc_asprintf (message, "%s%s",
395 _find_prefix (prefix_name), value);
397 if (strlen (term) > NOTMUCH_TERM_MAX)
398 return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
400 message->doc.remove_term (term);
404 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
408 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
410 notmuch_private_status_t status;
413 return NOTMUCH_STATUS_NULL_POINTER;
415 if (strlen (tag) > NOTMUCH_TAG_MAX)
416 return NOTMUCH_STATUS_TAG_TOO_LONG;
418 status = _notmuch_message_add_term (message, "tag", tag);
420 fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
425 _notmuch_message_sync (message);
427 return NOTMUCH_STATUS_SUCCESS;
431 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
433 notmuch_private_status_t status;
436 return NOTMUCH_STATUS_NULL_POINTER;
438 if (strlen (tag) > NOTMUCH_TAG_MAX)
439 return NOTMUCH_STATUS_TAG_TOO_LONG;
441 status = _notmuch_message_remove_term (message, "tag", tag);
443 fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
448 _notmuch_message_sync (message);
450 return NOTMUCH_STATUS_SUCCESS;
454 notmuch_message_destroy (notmuch_message_t *message)
456 talloc_free (message);
460 _notmuch_terms_has_more (notmuch_terms_t *terms)
464 if (terms->iterator == terms->iterator_end)
467 s = *terms->iterator;
468 if (! s.empty () && s[0] == terms->prefix_char)
475 _notmuch_terms_get (notmuch_terms_t *terms)
477 return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
481 _notmuch_terms_advance (notmuch_terms_t *terms)
487 _notmuch_terms_destroy (notmuch_terms_t *terms)
493 notmuch_tags_has_more (notmuch_tags_t *tags)
495 return _notmuch_terms_has_more (&tags->terms);
499 notmuch_tags_get (notmuch_tags_t *tags)
501 return _notmuch_terms_get (&tags->terms);
505 notmuch_tags_advance (notmuch_tags_t *tags)
507 return _notmuch_terms_advance (&tags->terms);
511 notmuch_tags_destroy (notmuch_tags_t *tags)
513 return _notmuch_terms_destroy (&tags->terms);
517 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
519 return _notmuch_terms_has_more (&thread_ids->terms);
523 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
525 return _notmuch_terms_get (&thread_ids->terms);
529 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
531 return _notmuch_terms_advance (&thread_ids->terms);
535 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
537 return _notmuch_terms_destroy (&thread_ids->terms);