]> git.notmuchmail.org Git - notmuch/blobdiff - lib/message.cc
correct comment referring to notmuch_database_remove_message
[notmuch] / lib / message.cc
index a7a13cc26aa181ec94f336048385403b903d4d37..d4eb209b283d6e0212d92d9b47cb4ea434068807 100644 (file)
@@ -43,6 +43,9 @@ struct visible _notmuch_message {
      * 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;
 };
@@ -252,7 +255,7 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
 
        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;
@@ -283,7 +286,7 @@ _notmuch_message_get_term (notmuch_message_t *message,
     if (i == end)
        return NULL;
 
-    std::string term = *i;
+    const std::string &term = *i;
     if (strncmp (term.c_str(), prefix, prefix_len))
        return NULL;
 
@@ -437,7 +440,8 @@ _notmuch_message_ensure_message_file (notmuch_message_t *message)
     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 *
@@ -467,7 +471,7 @@ notmuch_message_get_header (notmuch_message_t *message, const char *header)
                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;
@@ -538,6 +542,7 @@ _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)
 
        try {
            message->doc.remove_term ((*i));
+           message->modified = TRUE;
        } catch (const Xapian::InvalidArgumentError) {
            /* Ignore failure to remove non-existent term. */
        }
@@ -641,15 +646,16 @@ _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)
        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);
@@ -722,7 +728,7 @@ _notmuch_message_add_filename (notmuch_message_t *message,
  * Note: This function does not remove a document from the database,
  * even if the specified filename is the only filename for this
  * message. For that functionality, see
- * _notmuch_database_remove_message. */
+ * notmuch_database_remove_message. */
 notmuch_status_t
 _notmuch_message_remove_filename (notmuch_message_t *message,
                                  const char *filename)
@@ -791,6 +797,7 @@ void
 _notmuch_message_clear_data (notmuch_message_t *message)
 {
     message->doc.set_data ("");
+    message->modified = TRUE;
 }
 
 static void
@@ -920,7 +927,7 @@ notmuch_message_get_date (notmuch_message_t *message)
     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;
@@ -988,6 +995,17 @@ _notmuch_message_set_header_values (notmuch_message_t *message,
                            Xapian::sortable_serialise (time_value));
     message->doc.add_value (NOTMUCH_VALUE_FROM, from);
     message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);
+    message->modified = TRUE;
+}
+
+/* Upgrade a message to support NOTMUCH_FEATURE_LAST_MOD.  The caller
+ * must call _notmuch_message_sync. */
+void
+_notmuch_message_upgrade_last_mod (notmuch_message_t *message)
+{
+    /* _notmuch_message_sync will update the last modification
+     * revision; we just have to ask it to. */
+    message->modified = TRUE;
 }
 
 /* Synchronize changes made to message->doc out into the database. */
@@ -999,8 +1017,24 @@ _notmuch_message_sync (notmuch_message_t *message)
     if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
        return;
 
+    if (! message->modified)
+       return;
+
+    /* Update the last modification of this message. */
+    if (message->notmuch->features & NOTMUCH_FEATURE_LAST_MOD)
+       /* sortable_serialise gives a reasonably compact encoding,
+        * which directly translates to reduced IO when scanning the
+        * value stream.  Since it's built for doubles, we only get 53
+        * effective bits, but that's still enough for the database to
+        * last a few centuries at 1 million revisions per second. */
+       message->doc.add_value (NOTMUCH_VALUE_LAST_MOD,
+                               Xapian::sortable_serialise (
+                                   _notmuch_database_new_revision (
+                                       message->notmuch)));
+
     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. */
@@ -1075,6 +1109,7 @@ _notmuch_message_add_term (notmuch_message_t *message,
        return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
 
     message->doc.add_term (term, 0);
+    message->modified = TRUE;
 
     talloc_free (term);
 
@@ -1143,6 +1178,7 @@ _notmuch_message_remove_term (notmuch_message_t *message,
 
     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
@@ -1625,3 +1661,9 @@ notmuch_message_destroy (notmuch_message_t *message)
 {
     talloc_free (message);
 }
+
+notmuch_database_t *
+_notmuch_message_database (notmuch_message_t *message)
+{
+    return message->notmuch;
+}