From: Timo Sirainen Date: Fri, 27 Apr 2018 20:45:21 +0000 (+0300) Subject: lib-storage: Cache all wanted fields whenever possible X-Git-Tag: 2.4.0~3187 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=234f81214535027633a34d45cd55b2f3564924fa;p=thirdparty%2Fdovecot%2Fcore.git lib-storage: Cache all wanted fields whenever possible --- diff --git a/src/lib-storage/index/index-mail.c b/src/lib-storage/index/index-mail.c index 205ee7ab02..fd59a18f28 100644 --- a/src/lib-storage/index/index-mail.c +++ b/src/lib-storage/index/index-mail.c @@ -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; } diff --git a/src/lib-storage/index/index-mail.h b/src/lib-storage/index/index-mail.h index db2f44d21e..b06e07da57 100644 --- a/src/lib-storage/index/index-mail.h +++ b/src/lib-storage/index/index-mail.h @@ -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);