notmuch_message_file_t *message_file;
notmuch_message_list_t *replies;
unsigned long flags;
+ /* For flags that are initialized on-demand, lazy_flags indicates
+ * if each flag has been initialized. */
+ unsigned long lazy_flags;
+
+ /* Message document modified since last sync */
+ notmuch_bool_t modified;
Xapian::Document doc;
Xapian::termcount termpos;
message->frozen = 0;
message->flags = 0;
+ message->lazy_flags = 0;
/* Each of these will be lazily created as needed. */
message->message_id = NULL;
*
* There is already a document with message ID 'message_id' in the
* database. The returned message can be used to query/modify the
- * document.
+ * document. The message may be a ghost message.
*
* NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
*
doc_id = _notmuch_database_generate_doc_id (notmuch);
} catch (const Xapian::Error &error) {
- fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
+ _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred creating message: %s\n",
error.get_msg().c_str());
notmuch->exception_reported = TRUE;
*status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
if (i == end)
return NULL;
- std::string term = *i;
+ const std::string &term = *i;
if (strncmp (term.c_str(), prefix, prefix_len))
return NULL;
const char *thread_prefix = _find_prefix ("thread"),
*tag_prefix = _find_prefix ("tag"),
*id_prefix = _find_prefix ("id"),
+ *type_prefix = _find_prefix ("type"),
*filename_prefix = _find_prefix ("file-direntry"),
*replyto_prefix = _find_prefix ("replyto");
message->message_id =
_notmuch_message_get_term (message, i, end, id_prefix);
+ /* Get document type */
+ assert (strcmp (id_prefix, type_prefix) < 0);
+ if (! NOTMUCH_TEST_BIT (message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST)) {
+ i.skip_to (type_prefix);
+ /* "T" is the prefix "type" fields. See
+ * BOOLEAN_PREFIX_INTERNAL. */
+ if (*i == "Tmail")
+ NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
+ else if (*i == "Tghost")
+ NOTMUCH_SET_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
+ else
+ INTERNAL_ERROR ("Message without type term");
+ NOTMUCH_SET_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
+ }
+
/* Get filename list. Here we get only the terms. We lazily
* expand them to full file names when needed in
* _notmuch_message_ensure_filename_list. */
- assert (strcmp (id_prefix, filename_prefix) < 0);
+ assert (strcmp (type_prefix, filename_prefix) < 0);
if (!message->filename_term_list && !message->filename_list)
message->filename_term_list =
_notmuch_database_get_terms_with_prefix (message, i, end,
message->tag_list = NULL;
}
+ if (strcmp ("type", prefix_name) == 0) {
+ NOTMUCH_CLEAR_BIT (&message->flags, NOTMUCH_MESSAGE_FLAG_GHOST);
+ NOTMUCH_CLEAR_BIT (&message->lazy_flags, NOTMUCH_MESSAGE_FLAG_GHOST);
+ }
+
if (strcmp ("file-direntry", prefix_name) == 0) {
talloc_free (message->filename_term_list);
talloc_free (message->filename_list);
if (unlikely (filename == NULL))
return;
- message->message_file = _notmuch_message_file_open_ctx (message, filename);
+ message->message_file = _notmuch_message_file_open_ctx (
+ _notmuch_message_database (message), message, filename);
}
const char *
return talloc_strdup (message, value.c_str ());
} catch (Xapian::Error &error) {
- fprintf (stderr, "A Xapian exception occurred when reading header: %s\n",
+ _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading header: %s\n",
error.get_msg().c_str());
message->notmuch->exception_reported = TRUE;
return NULL;
try {
message->doc.remove_term ((*i));
+ message->modified = TRUE;
} catch (const Xapian::InvalidArgumentError) {
/* Ignore failure to remove non-existent term. */
}
unsigned int directory_id;
const char *direntry, *directory;
char *colon;
+ const std::string &term = *i;
/* Terminate loop at first term without desired prefix. */
- if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))
+ if (strncmp (term.c_str (), direntry_prefix, direntry_prefix_len))
break;
/* Indicate that there are filenames remaining. */
status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
- direntry = (*i).c_str ();
+ direntry = term.c_str ();
direntry += direntry_prefix_len;
directory_id = strtol (direntry, &colon, 10);
_notmuch_message_clear_data (notmuch_message_t *message)
{
message->doc.set_data ("");
+ message->modified = TRUE;
}
static void
notmuch_message_get_flag (notmuch_message_t *message,
notmuch_message_flag_t flag)
{
- return message->flags & (1 << flag);
+ if (flag == NOTMUCH_MESSAGE_FLAG_GHOST &&
+ ! NOTMUCH_TEST_BIT (message->lazy_flags, flag))
+ _notmuch_message_ensure_metadata (message);
+
+ return NOTMUCH_TEST_BIT (message->flags, flag);
}
void
notmuch_message_flag_t flag, notmuch_bool_t enable)
{
if (enable)
- message->flags |= (1 << flag);
+ NOTMUCH_SET_BIT (&message->flags, flag);
else
- message->flags &= ~(1 << flag);
+ NOTMUCH_CLEAR_BIT (&message->flags, flag);
+ NOTMUCH_SET_BIT (&message->lazy_flags, flag);
}
time_t
try {
value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
} catch (Xapian::Error &error) {
- fprintf (stderr, "A Xapian exception occurred when reading date: %s\n",
+ _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading date: %s\n",
error.get_msg().c_str());
message->notmuch->exception_reported = TRUE;
return 0;
Xapian::sortable_serialise (time_value));
message->doc.add_value (NOTMUCH_VALUE_FROM, from);
message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
+ message->modified = TRUE;
}
/* Synchronize changes made to message->doc out into the database. */
if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
return;
+ if (! message->modified)
+ return;
+
db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
db->replace_document (message->doc_id, message->doc);
+ message->modified = FALSE;
}
/* Delete a message document from the database. */
return NOTMUCH_STATUS_SUCCESS;
}
+/* Transform a blank message into a ghost message. The caller must
+ * _notmuch_message_sync the message. */
+notmuch_private_status_t
+_notmuch_message_initialize_ghost (notmuch_message_t *message,
+ const char *thread_id)
+{
+ notmuch_private_status_t status;
+
+ status = _notmuch_message_add_term (message, "type", "ghost");
+ if (status)
+ return status;
+ status = _notmuch_message_add_term (message, "thread", thread_id);
+ if (status)
+ return status;
+
+ return NOTMUCH_PRIVATE_STATUS_SUCCESS;
+}
+
/* Ensure that 'message' is not holding any file object open. Future
* calls to various functions will still automatically open the
* message file as needed.
return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
message->doc.add_term (term, 0);
+ message->modified = TRUE;
talloc_free (term);
try {
message->doc.remove_term (term);
+ message->modified = TRUE;
} catch (const Xapian::InvalidArgumentError) {
/* We'll let the philosopher's try to wrestle with the
* question of whether failing to remove that which was not
{
talloc_free (message);
}
+
+notmuch_database_t *
+_notmuch_message_database (notmuch_message_t *message)
+{
+ return message->notmuch;
+}