]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Cache all wanted fields whenever possible
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 27 Apr 2018 20:45:21 +0000 (23:45 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Sat, 14 Jan 2023 15:18:23 +0000 (15:18 +0000)
src/lib-storage/index/index-mail.c
src/lib-storage/index/index-mail.h

index 205ee7ab022697173e322a460ed18f273f730184..fd59a18f28a69d7173478575fac29297b5124ea8 100644 (file)
@@ -955,6 +955,12 @@ bool index_mail_want_cache(struct index_mail *mail, enum index_cache_field field
        case MAIL_CACHE_BODY_SNIPPET:
                fetch_field = MAIL_FETCH_BODY_SNIPPET;
                break;
+       case MAIL_CACHE_IMAP_BODY:
+               fetch_field = MAIL_FETCH_IMAP_BODY;
+               break;
+       case MAIL_CACHE_IMAP_BODYSTRUCTURE:
+               fetch_field = MAIL_FETCH_IMAP_BODYSTRUCTURE;
+               break;
        default:
                i_unreached();
        }
@@ -1471,6 +1477,13 @@ int index_mail_init_stream(struct index_mail *mail,
        return 0;
 }
 
+static bool index_mail_want_write_snippet(struct index_mail *mail,
+                                         enum index_cache_field field)
+{
+       if (field == MAIL_CACHE_BODY_SNIPPET) return TRUE;
+       return index_mail_want_cache(mail, MAIL_CACHE_BODY_SNIPPET);
+}
+
 static int
 index_mail_parse_bodystructure_full(struct index_mail *mail,
                                    enum index_cache_field field)
@@ -1480,7 +1493,7 @@ index_mail_parse_bodystructure_full(struct index_mail *mail,
        if ((data->save_bodystructure_header &&
             !data->parsed_bodystructure_header) ||
            !data->save_bodystructure_body ||
-           field == MAIL_CACHE_BODY_SNIPPET) {
+           index_mail_want_write_snippet(mail, field)) {
                /* we haven't parsed the header yet */
                const char *reason =
                        index_mail_cache_reason(&mail->mail.mail, "bodystructure");
@@ -1530,41 +1543,47 @@ static int index_mail_parse_bodystructure(struct index_mail *mail,
        }
        i_assert(data->parts != NULL);
 
-       /* if we didn't want to have the body(structure) cached,
-          it's still not written. */
-       switch (field) {
-       case MAIL_CACHE_IMAP_BODY:
-               if (data->body == NULL) {
-                       str = str_new(mail->mail.data_pool, 128);
-                       if (index_mail_write_bodystructure(mail, str, FALSE) < 0)
-                               return -1;
-                       data->body = str_c(str);
-               }
-               break;
-       case MAIL_CACHE_IMAP_BODYSTRUCTURE:
-               if (data->bodystructure == NULL) {
-                       str = str_new(mail->mail.data_pool, 128);
-                       if (index_mail_write_bodystructure(mail, str, TRUE) < 0)
-                               return -1;
-                       data->bodystructure = str_c(str);
-               }
-               break;
-       case MAIL_CACHE_BODY_SNIPPET:
-               if (data->body_snippet == NULL) {
-                       if (index_mail_write_body_snippet(mail) < 0)
-                               return -1;
+       if (data->body_snippet == NULL &&
+           index_mail_want_write_snippet(mail, field)) {
+               if (index_mail_write_body_snippet(mail) < 0)
+                       return -1;
+
+               index_mail_cache_add_if_wanted(mail, MAIL_CACHE_BODY_SNIPPET,
+                                              mail->data.body_snippet,
+                                              strlen(mail->data.body_snippet));
 
-                       if (index_mail_want_cache(mail, MAIL_CACHE_BODY_SNIPPET))
-                               index_mail_cache_add(mail, MAIL_CACHE_BODY_SNIPPET,
-                                                    mail->data.body_snippet,
-                                                    strlen(mail->data.body_snippet));
-               }
                i_assert(data->body_snippet != NULL &&
                         data->body_snippet[0] != '\0');
-               break;
-       default:
-               i_unreached();
        }
+
+       if (data->body == NULL &&
+           (field == MAIL_CACHE_IMAP_BODY ||
+            index_mail_want_cache(mail, MAIL_CACHE_IMAP_BODY))) {
+               str = str_new(mail->mail.data_pool, 128);
+               if (index_mail_write_bodystructure(mail, str, FALSE) < 0)
+                       return -1;
+               data->body = str_c(str);
+
+               index_mail_cache_add_if_wanted(mail, MAIL_CACHE_IMAP_BODY,
+                                              data->body, strlen(data->body));
+       }
+
+       if (data->bodystructure == NULL &&
+           (field == MAIL_CACHE_IMAP_BODYSTRUCTURE ||
+            index_mail_want_cache(mail, MAIL_CACHE_IMAP_BODYSTRUCTURE))) {
+               str = str_new(mail->mail.data_pool, 128);
+               if (index_mail_write_bodystructure(mail, str, TRUE) < 0)
+                       return -1;
+               data->bodystructure = str_c(str);
+
+               index_mail_cache_add_if_wanted(mail, MAIL_CACHE_IMAP_BODYSTRUCTURE,
+                                             data->bodystructure,
+                                             strlen(data->bodystructure));
+       }
+
+       if (!data->messageparts_saved_to_cache)
+               index_mail_body_parsed_cache_message_parts(mail);
+
        return 0;
 }
 
index db2f44d21e7d3f51d160eb69fa61f967f98e21eb..b06e07da57672f3aad29be61c604c0e989ece47a 100644 (file)
@@ -273,6 +273,16 @@ const uint32_t *index_mail_get_vsize_extension(struct mail *_mail);
 bool index_mail_want_cache(struct index_mail *mail, enum index_cache_field field);
 void index_mail_cache_add(struct index_mail *mail, enum index_cache_field field,
                          const void *data, size_t data_size);
+static inline bool
+index_mail_cache_add_if_wanted(struct index_mail *mail, enum index_cache_field field,
+                              const void *data, size_t data_size)
+{
+       bool want = index_mail_want_cache(mail, field);
+       if (want)
+               index_mail_cache_add(mail, field, data, data_size);
+       return want;
+}
+
 void index_mail_cache_add_idx(struct index_mail *mail, unsigned int field_idx,
                              const void *data, size_t data_size);