[dovecot-cvs] dovecot/src/imap client.h,1.7,1.8 cmd-select.c,1.12,1.13 imap-fetch-body-section.c,1.1,1.2 imap-fetch.c,1.1,1.2 imap-fetch.h,1.1,1.2

cras at procontrol.fi cras at procontrol.fi
Tue Jan 21 07:37:37 EET 2003


Update of /home/cvs/dovecot/src/imap
In directory danu:/tmp/cvs-serv21976/imap

Modified Files:
	client.h cmd-select.c imap-fetch-body-section.c imap-fetch.c 
	imap-fetch.h 
Log Message:
Partial fetches are working fast again - faster than ever actually.



Index: client.h
===================================================================
RCS file: /home/cvs/dovecot/src/imap/client.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- client.h	11 Jan 2003 19:55:56 -0000	1.7
+++ client.h	21 Jan 2003 05:37:35 -0000	1.8
@@ -16,6 +16,7 @@
 
 	struct mail_storage *storage;
 	struct mailbox *mailbox;
+	unsigned int select_counter; /* increased when mailbox is changed */
 
 	time_t last_input;
 	unsigned int bad_counter;

Index: cmd-select.c
===================================================================
RCS file: /home/cvs/dovecot/src/imap/cmd-select.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- cmd-select.c	5 Jan 2003 13:09:51 -0000	1.12
+++ cmd-select.c	21 Jan 2003 05:37:35 -0000	1.13
@@ -36,6 +36,7 @@
 	/* set client's mailbox only after getting status to make sure
 	   we're not sending any expunge/exists replies too early to client */
 	client->mailbox = box;
+	client->select_counter++;
 
 	client_send_mailbox_flags(client, box, status.custom_flags,
 				  status.custom_flags_count);

Index: imap-fetch-body-section.c
===================================================================
RCS file: /home/cvs/dovecot/src/imap/imap-fetch-body-section.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- imap-fetch-body-section.c	20 Jan 2003 14:52:51 -0000	1.1
+++ imap-fetch-body-section.c	21 Jan 2003 05:37:35 -0000	1.2
@@ -31,6 +31,46 @@
 	int (*match_func) (const char *const *, const unsigned char *, size_t);
 };
 
+struct partial_cache {
+	unsigned int select_counter;
+	unsigned int uid;
+
+	uoff_t physical_start;
+	struct message_size pos;
+};
+
+static struct partial_cache partial = { 0, 0, 0, { 0, 0, 0 } };
+
+static int seek_partial(unsigned int select_counter, unsigned int uid,
+			struct partial_cache *partial, struct istream *stream,
+			uoff_t physical_start, uoff_t virtual_skip)
+{
+	int cr_skipped;
+
+	if (select_counter == partial->select_counter && uid == partial->uid &&
+	    physical_start == partial->physical_start &&
+	    virtual_skip >= partial->pos.virtual_size) {
+		/* we can use the cache */
+		virtual_skip -= partial->pos.virtual_size;
+	} else {
+		partial->select_counter = select_counter;
+		partial->uid = uid;
+		partial->physical_start = physical_start;
+		memset(&partial->pos, 0, sizeof(partial->pos));
+	}
+
+	i_warning("skipping %lld", virtual_skip);
+
+	i_stream_seek(stream, partial->physical_start +
+		      partial->pos.physical_size);
+	message_skip_virtual(stream, virtual_skip, &partial->pos, &cr_skipped);
+
+	if (cr_skipped)
+		partial->pos.virtual_size--;
+
+	return cr_skipped;
+}
+
 /* fetch BODY[] or BODY[TEXT] */
 static int fetch_body(struct imap_fetch_context *ctx,
 		      const struct imap_fetch_body_data *body,
@@ -39,6 +79,8 @@
 	struct message_size hdr_size, body_size;
 	struct istream *stream;
 	const char *str;
+	int skip_cr;
+	off_t ret;
 
 	stream = mail->get_stream(mail, &hdr_size, &body_size);
 	if (stream == NULL)
@@ -54,9 +96,16 @@
 	if (o_stream_send_str(ctx->output, str) < 0)
 		return FALSE;
 
-	/* FIXME: SLOW! we need some cache for this. */
-	return message_send(ctx->output, stream, &body_size,
-			    body->skip, body->max_size);
+	skip_cr = seek_partial(ctx->select_counter, mail->uid,
+			       &partial, stream, 0, body->skip);
+	ret = message_send(ctx->output, stream, &body_size,
+			   skip_cr, body->max_size);
+	if (ret > 0) {
+		partial.pos.physical_size =
+			stream->v_offset - partial.physical_start;
+		partial.pos.virtual_size += ret;
+	}
+	return ret >= 0;
 }
 
 static const char **get_fields_array(const char *fields)
@@ -258,7 +307,7 @@
 		if (o_stream_send_str(ctx->output, str) < 0)
 			return FALSE;
 		return message_send(ctx->output, input, size,
-				    body->skip, body->max_size);
+				    body->skip, body->max_size) >= 0;
 	}
 
 	/* partial headers - copy the wanted fields into memory, inserting
@@ -386,23 +435,28 @@
 static int fetch_part_body(struct imap_fetch_context *ctx,
 			   struct istream *stream,
 			   const struct imap_fetch_body_data *body,
-			   const struct message_part *part)
+			   struct mail *mail, const struct message_part *part)
 {
 	const char *str;
-
-	/* jump to beginning of part body */
-	i_stream_seek(stream, part->physical_pos +
-		      part->header_size.physical_size);
+	int skip_cr;
+	off_t ret;
 
 	str = t_strdup_printf("%s {%"PRIuUOFF_T"}\r\n",
 			      ctx->prefix, part->body_size.virtual_size);
 	if (o_stream_send_str(ctx->output, str) < 0)
 		return FALSE;
 
-	/* FIXME: potential performance problem with big messages:
-	   FETCH BODY[1]<100000..1024>, hopefully no clients do this */
-	return message_send(ctx->output, stream, &part->body_size,
-			    body->skip, body->max_size);
+	skip_cr = seek_partial(ctx->select_counter, mail->uid,
+			       &partial, stream, part->physical_pos +
+			       part->header_size.physical_size, body->skip);
+	ret = message_send(ctx->output, stream, &part->body_size,
+			   skip_cr, body->max_size);
+	if (ret > 0) {
+		partial.pos.physical_size =
+			stream->v_offset - partial.physical_start;
+		partial.pos.virtual_size += ret;
+	}
+	return ret >= 0;
 }
 
 static int fetch_part(struct imap_fetch_context *ctx, struct mail *mail,
@@ -421,7 +475,7 @@
 		return FALSE;
 
 	if (*section == '\0' || strcmp(section, "TEXT") == 0)
-		return fetch_part_body(ctx, stream, body, part);
+		return fetch_part_body(ctx, stream, body, mail, part);
 
 	if (strncmp(section, "HEADER", 6) == 0 ||
 	    strcmp(section, "MIME") == 0) {

Index: imap-fetch.c
===================================================================
RCS file: /home/cvs/dovecot/src/imap/imap-fetch.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- imap-fetch.c	20 Jan 2003 14:52:51 -0000	1.1
+++ imap-fetch.c	21 Jan 2003 05:37:35 -0000	1.2
@@ -112,7 +112,8 @@
 	if (o_stream_send_str(ctx->output, str) < 0)
 		return FALSE;
 
-	return message_send(ctx->output, stream, &body_size, 0, (uoff_t)-1);
+	return message_send(ctx->output, stream, &body_size,
+			    0, (uoff_t)-1) >= 0;
 }
 
 static int fetch_send_rfc822_header(struct imap_fetch_context *ctx,
@@ -134,7 +135,7 @@
 	if (o_stream_send_str(ctx->output, str) < 0)
 		return FALSE;
 
-	return message_send(ctx->output, stream, &hdr_size, 0, (uoff_t)-1);
+	return message_send(ctx->output, stream, &hdr_size, 0, (uoff_t)-1) >= 0;
 }
 
 static int fetch_send_rfc822_text(struct imap_fetch_context *ctx,
@@ -157,7 +158,8 @@
 		return FALSE;
 
 	i_stream_seek(stream, hdr_size.physical_size);
-	return message_send(ctx->output, stream, &body_size, 0, (uoff_t)-1);
+	return message_send(ctx->output, stream, &body_size,
+			    0, (uoff_t)-1) >= 0;
 }
 
 static int fetch_mail(struct imap_fetch_context *ctx, struct mail *mail)
@@ -258,6 +260,7 @@
 	ctx.imap_data = imap_data;
 	ctx.bodies = bodies;
 	ctx.output = client->output;
+	ctx.select_counter = client->select_counter;
 	ctx.str = t_str_new(8192);
 
 	ctx.fetch_ctx = client->mailbox->

Index: imap-fetch.h
===================================================================
RCS file: /home/cvs/dovecot/src/imap/imap-fetch.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- imap-fetch.h	20 Jan 2003 14:52:51 -0000	1.1
+++ imap-fetch.h	21 Jan 2003 05:37:35 -0000	1.2
@@ -28,6 +28,7 @@
 	string_t *str;
 	struct ostream *output;
 	const char *prefix;
+	unsigned int select_counter;
 
 	int first, failed;
 };




More information about the dovecot-cvs mailing list