]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
Merge branch 'upstream'
[notmuch] / lib / database.cc
index 6b8c9989ea4f9c75ada9df3ae0591fca1b65c20f..cce7847860e5a963532936eb0f9f4fb84af2a5c4 100644 (file)
@@ -681,8 +681,7 @@ handle_sigalrm (unused (int signal))
 notmuch_status_t
 notmuch_database_upgrade (notmuch_database_t *notmuch,
                          void (*progress_notify) (void *closure,
-                                                  unsigned int count,
-                                                  unsigned int total),
+                                                  double progress),
                          void *closure)
 {
     Xapian::WritableDatabase *db;
@@ -691,6 +690,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     notmuch_bool_t timer_is_active = FALSE;
     unsigned int version;
     notmuch_status_t status;
+    unsigned int count = 0, total = 0;
 
     status = _notmuch_database_ensure_writable (notmuch);
     if (status)
@@ -722,14 +722,15 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     }
 
     /* Before version 1, each message document had its filename in the
-     * data field. Move that into the new format by calling
+     * data field. Copy that into the new format by calling
      * notmuch_message_add_filename.
      */
     if (version < 1) {
-       unsigned int count = 0, total;
        notmuch_query_t *query = notmuch_query_create (notmuch, "");
        notmuch_messages_t *messages;
        notmuch_message_t *message;
+       char *filename;
+       Xapian::TermIterator t, t_end;
 
        total = notmuch_query_count_messages (query);
 
@@ -737,22 +738,30 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
             notmuch_messages_has_more (messages);
             notmuch_messages_advance (messages))
        {
-           if (do_progress_notify)
-               progress_notify (closure, count, total);
+           if (do_progress_notify) {
+               progress_notify (closure, (double) count / total);
+               do_progress_notify = 0;
+           }
 
            message = notmuch_messages_get (messages);
 
-           _notmuch_message_upgrade_filename_storage (message);
+           filename = _notmuch_message_talloc_copy_data (message);
+           if (filename && *filename != '\0') {
+               _notmuch_message_add_filename (message, filename);
+               _notmuch_message_sync (message);
+           }
+           talloc_free (filename);
+
+           notmuch_message_destroy (message);
 
            count++;
        }
-    }
 
-    /* Also, before version 1 we stored directory timestamps in
-     * XTIMESTAMP documents instead of the current XDIRECTORY
-     * documents. So convert those as well. */
-    if (version < 1) {
-       Xapian::TermIterator t, t_end;
+       notmuch_query_destroy (query);
+
+       /* Also, before version 1 we stored directory timestamps in
+        * XTIMESTAMP documents instead of the current XDIRECTORY
+        * documents. So copy those as well. */
 
        t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
 
@@ -773,6 +782,11 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
                time_t mtime;
                notmuch_directory_t *directory;
 
+               if (do_progress_notify) {
+                   progress_notify (closure, (double) count / total);
+                   do_progress_notify = 0;
+               }
+
                document = find_document_for_doc_id (notmuch, *p);
                mtime = Xapian::sortable_unserialise (
                    document.get_value (NOTMUCH_VALUE_TIMESTAMP));
@@ -788,6 +802,66 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));
     db->flush ();
 
+    /* Now that the upgrade is complete we can remove the old data
+     * and documents that are no longer needed. */
+    if (version < 1) {
+       notmuch_query_t *query = notmuch_query_create (notmuch, "");
+       notmuch_messages_t *messages;
+       notmuch_message_t *message;
+       char *filename;
+
+       for (messages = notmuch_query_search_messages (query);
+            notmuch_messages_has_more (messages);
+            notmuch_messages_advance (messages))
+       {
+           if (do_progress_notify) {
+               progress_notify (closure, (double) count / total);
+               do_progress_notify = 0;
+           }
+
+           message = notmuch_messages_get (messages);
+
+           filename = _notmuch_message_talloc_copy_data (message);
+           if (filename && *filename != '\0') {
+               _notmuch_message_clear_data (message);
+               _notmuch_message_sync (message);
+           }
+           talloc_free (filename);
+
+           notmuch_message_destroy (message);
+       }
+
+       notmuch_query_destroy (query);
+    }
+
+    if (version < 1) {
+       Xapian::TermIterator t, t_end;
+
+       t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
+
+       for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP");
+            t != t_end;
+            t++)
+       {
+           Xapian::PostingIterator p, p_end;
+           std::string term = *t;
+
+           p_end = notmuch->xapian_db->postlist_end (term);
+
+           for (p = notmuch->xapian_db->postlist_begin (term);
+                p != p_end;
+                p++)
+           {
+               if (do_progress_notify) {
+                   progress_notify (closure, (double) count / total);
+                   do_progress_notify = 0;
+               }
+
+               db->delete_document (*p);
+           }
+       }
+    }
+
     if (timer_is_active) {
        /* Now stop the timer. */
        timerval.it_interval.tv_sec = 0;