dovecot: Optimize physical mail size calculation by using the ne...

dovecot at dovecot.org dovecot at dovecot.org
Tue Dec 4 15:20:05 EET 2007


details:   http://hg.dovecot.org/dovecot/rev/48ddc0c4036c
changeset: 6930:48ddc0c4036c
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Dec 04 15:20:01 2007 +0200
description:
Optimize physical mail size calculation by using the next message's offset
or the mbox size for the last message.

diffstat:

3 files changed, 49 insertions(+), 10 deletions(-)
src/lib-storage/index/mbox/mbox-file.c |   32 +++++++++++++++++++++++---------
src/lib-storage/index/mbox/mbox-file.h |    3 +++
src/lib-storage/index/mbox/mbox-mail.c |   24 +++++++++++++++++++++++-

diffs (108 lines):

diff -r 8c80093fbab4 -r 48ddc0c4036c src/lib-storage/index/mbox/mbox-file.c
--- a/src/lib-storage/index/mbox/mbox-file.c	Tue Dec 04 14:47:43 2007 +0200
+++ b/src/lib-storage/index/mbox/mbox-file.c	Tue Dec 04 15:20:01 2007 +0200
@@ -131,17 +131,15 @@ void mbox_file_close_stream(struct mbox_
 	}
 }
 
-int mbox_file_seek(struct mbox_mailbox *mbox, struct mail_index_view *view,
-		   uint32_t seq, bool *deleted_r)
+int mbox_file_lookup_offset(struct mbox_mailbox *mbox,
+			    struct mail_index_view *view,
+			    uint32_t seq, uoff_t *offset_r)
 {
 	const void *data;
-	uint64_t offset;
-	int ret;
+	bool deleted;
 
-	*deleted_r = FALSE;
-
-	mail_index_lookup_ext(view, seq, mbox->mbox_ext_idx, &data, deleted_r);
-	if (*deleted_r)
+	mail_index_lookup_ext(view, seq, mbox->mbox_ext_idx, &data, &deleted);
+	if (deleted)
 		return -1;
 
 	if (data == NULL) {
@@ -152,7 +150,23 @@ int mbox_file_seek(struct mbox_mailbox *
 		return 0;
 	}
 
-	offset = *((const uint64_t *)data);
+	*offset_r = *((const uint64_t *)data);
+	return 1;
+}
+
+int mbox_file_seek(struct mbox_mailbox *mbox, struct mail_index_view *view,
+		   uint32_t seq, bool *deleted_r)
+{
+	uoff_t offset;
+	int ret;
+
+	ret = mbox_file_lookup_offset(mbox, view, seq, &offset);
+	if (ret <= 0) {
+		*deleted_r = ret < 0;
+		return ret;
+	}
+	*deleted_r = FALSE;
+
 	if (istream_raw_mbox_seek(mbox->mbox_stream, offset) < 0) {
 		if (offset == 0) {
 			mail_storage_set_error(&mbox->storage->storage,
diff -r 8c80093fbab4 -r 48ddc0c4036c src/lib-storage/index/mbox/mbox-file.h
--- a/src/lib-storage/index/mbox/mbox-file.h	Tue Dec 04 14:47:43 2007 +0200
+++ b/src/lib-storage/index/mbox/mbox-file.h	Tue Dec 04 15:20:01 2007 +0200
@@ -7,6 +7,9 @@ int mbox_file_open_stream(struct mbox_ma
 int mbox_file_open_stream(struct mbox_mailbox *mbox);
 void mbox_file_close_stream(struct mbox_mailbox *mbox);
 
+int mbox_file_lookup_offset(struct mbox_mailbox *mbox,
+			    struct mail_index_view *view,
+			    uint32_t seq, uoff_t *offset_r);
 int mbox_file_seek(struct mbox_mailbox *mbox, struct mail_index_view *view,
 		   uint32_t seq, bool *deleted_r);
 
diff -r 8c80093fbab4 -r 48ddc0c4036c src/lib-storage/index/mbox/mbox-mail.c
--- a/src/lib-storage/index/mbox/mbox-mail.c	Tue Dec 04 14:47:43 2007 +0200
+++ b/src/lib-storage/index/mbox/mbox-mail.c	Tue Dec 04 15:20:01 2007 +0200
@@ -171,8 +171,10 @@ static int mbox_mail_get_physical_size(s
 	struct index_mail *mail = (struct index_mail *)_mail;
 	struct index_mail_data *data = &mail->data;
 	struct mbox_mailbox *mbox = (struct mbox_mailbox *)mail->ibox;
+	const struct mail_index_header *hdr;
 	struct istream *stream;
 	uoff_t hdr_offset, body_offset, body_size;
+	uoff_t next_offset;
 
 	if (mbox_mail_seek(mail) < 0)
 		return -1;
@@ -186,7 +188,27 @@ static int mbox_mail_get_physical_size(s
 					  "Couldn't get mbox size");
 		return -1;
 	}
-	body_size = istream_raw_mbox_get_body_size(stream, (uoff_t)-1);
+
+	/* use the next message's offset to avoid reading through the entire
+	   message body to find out its size */
+	hdr = mail_index_get_header(mail->trans->trans_view);
+	if (_mail->seq == hdr->messages_count) {
+		/* last message, use the synced mbox size */
+		int trailer_size;
+
+		trailer_size = (mbox->storage->storage.flags &
+				MAIL_STORAGE_FLAG_SAVE_CRLF) != 0 ? 2 : 1;
+
+		body_size = hdr->sync_size - body_offset - trailer_size;
+	} else if (mbox_file_lookup_offset(mbox, mail->trans->trans_view,
+					   _mail->seq + 1, &next_offset) > 0) {
+		body_size = next_offset - body_offset;
+	} else {
+		body_size = (uoff_t)-1;
+	}
+
+	/* verify that the calculated body size is correct */
+	body_size = istream_raw_mbox_get_body_size(stream, body_size);
 
 	data->physical_size = (body_offset - hdr_offset) + body_size;
 	*size_r = data->physical_size;


More information about the dovecot-cvs mailing list