]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
indexer: index_mailbox_precache_real() - Avoid opening the mailbox when unnecessary
authorMarco Bettini <marco.bettini@open-xchange.com>
Mon, 31 Jul 2023 15:54:43 +0000 (15:54 +0000)
committerMarco Bettini <marco.bettini@open-xchange.com>
Tue, 10 Oct 2023 08:04:41 +0000 (08:04 +0000)
src/indexer/master-connection.c

index c1e18d1aa7e5be5959d36c85e936ea77d74e3ecf..2c7f8d3c8bdb59ed82a2256f3b155a77c57903b6 100644 (file)
@@ -81,13 +81,6 @@ index_mailbox_precache_real(struct master_connection *conn, struct mailbox *box)
        struct event *index_event = event_create(box->event);
        event_add_category(index_event, &event_category_indexer_worker);
 
-       if (mailbox_get_metadata(box, MAILBOX_METADATA_PRECACHE_FIELDS,
-                                &metadata) < 0) {
-               e_error(index_event, "Precache-fields lookup failed: %s",
-                       mailbox_get_last_internal_error(box, NULL));
-               event_unref(&index_event);
-               return -1;
-       }
        if (mailbox_get_status(box, STATUS_MESSAGES | STATUS_FTS_LAST_INDEXED_UID,
                               &status) < 0) {
                e_error(index_event, "Status lookup failed: %s",
@@ -96,6 +89,22 @@ index_mailbox_precache_real(struct master_connection *conn, struct mailbox *box)
                return -1;
        }
 
+       if (status.fts_last_indexed_uid >= status.uidnext - 1) {
+               e_debug(index_event,
+                       "Index is already up to date "
+                       "(last_indexed_uid=%u, uidnext=%u)",
+                       status.fts_last_indexed_uid, status.uidnext);
+               event_unref(&index_event);
+               return 0;
+       }
+
+       if (mailbox_get_metadata(box, MAILBOX_METADATA_PRECACHE_FIELDS,
+                                &metadata) < 0) {
+               e_error(index_event, "Precache-fields lookup failed: %s",
+                       mailbox_get_last_internal_error(box, NULL));
+               event_unref(&index_event);
+               return -1;
+       }
        uint32_t seq = 0, unused ATTR_UNUSED;
        if (status.fts_last_indexed_uid > 0)
                mailbox_get_seq_range(box, 1, status.fts_last_indexed_uid,