[dovecot-cvs] dovecot/src/lib-index mail-index-view-sync.c, 1.40, 1.41 mail-transaction-util.c, 1.24, 1.25 mail-transaction-util.h, 1.11, 1.12

cras at dovecot.org cras at dovecot.org
Tue Apr 26 22:12:11 EEST 2005


Update of /var/lib/cvs/dovecot/src/lib-index
In directory talvi:/tmp/cvs-serv26533/lib-index

Modified Files:
	mail-index-view-sync.c mail-transaction-util.c 
	mail-transaction-util.h 
Log Message:
Expunge code cleanups.



Index: mail-index-view-sync.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-index/mail-index-view-sync.c,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -d -r1.40 -r1.41
--- mail-index-view-sync.c	8 Apr 2005 12:14:42 -0000	1.40
+++ mail-index-view-sync.c	26 Apr 2005 19:12:08 -0000	1.41
@@ -11,7 +11,7 @@
 	struct mail_index_view *view;
 	enum mail_transaction_type trans_sync_mask;
 	struct mail_index_sync_map_ctx sync_map_ctx;
-	buffer_t *expunges;
+	array_t ARRAY_DEFINE(expunges, struct mail_transaction_expunge);
 
 	const struct mail_transaction_header *hdr;
 	const void *data;
@@ -27,13 +27,79 @@
 	uoff_t log_file_offset;
 };
 
+static void
+mail_transaction_log_sort_expunges(array_t *expunges,
+				   const struct mail_transaction_expunge *src,
+				   size_t src_size)
+{
+	ARRAY_SET_TYPE(expunges, struct mail_transaction_expunge);
+	const struct mail_transaction_expunge *src_end;
+	struct mail_transaction_expunge *dest;
+	struct mail_transaction_expunge new_exp;
+	unsigned int first, i, dest_count;
+
+	i_assert(src_size % sizeof(*src) == 0);
+
+	/* @UNSAFE */
+	dest = array_get_modifyable(expunges, &dest_count);
+	if (dest_count == 0) {
+		array_append(expunges, src, src_size / sizeof(*src));
+		return;
+	}
+
+	src_end = CONST_PTR_OFFSET(src, src_size);
+	for (i = 0; src != src_end; src++) {
+		/* src[] must be sorted. */
+		i_assert(src+1 == src_end || src->uid2 < src[1].uid1);
+		i_assert(src->uid1 <= src->uid2);
+
+		for (; i < dest_count; i++) {
+			if (src->uid1 < dest[i].uid1)
+				break;
+		}
+
+		new_exp = *src;
+
+		first = i;
+		while (i < dest_count && src->uid2 >= dest[i].uid1-1) {
+			/* we can/must merge with next record */
+			if (new_exp.uid2 < dest[i].uid2)
+				new_exp.uid2 = dest[i].uid2;
+			i++;
+		}
+
+		if (first > 0 && new_exp.uid1 <= dest[first-1].uid2+1) {
+			/* continue previous record */
+			if (dest[first-1].uid2 < new_exp.uid2)
+				dest[first-1].uid2 = new_exp.uid2;
+		} else if (i == first) {
+			array_insert(expunges, i, &new_exp, 1);
+			i++; first++;
+
+			dest = array_get_modifyable(expunges, &dest_count);
+		} else {
+			/* use next record */
+			dest[first] = new_exp;
+			first++;
+		}
+
+		if (i > first) {
+			array_delete(expunges, first, i - first);
+
+			dest = array_get_modifyable(expunges, &dest_count);
+			i = first;
+		}
+	}
+}
+
 static int
-view_sync_get_expunges(struct mail_index_view *view, buffer_t **expunges_r)
+view_sync_get_expunges(struct mail_index_view *view, array_t *expunges_r)
 {
+	ARRAY_SET_TYPE(expunges_r, struct mail_transaction_expunge);
 	const struct mail_transaction_header *hdr;
 	struct mail_transaction_expunge *src, *src_end, *dest;
 	const void *data;
-	size_t size;
+	unsigned int count;
 	int ret;
 
 	if (mail_transaction_log_view_set(view->log_view,
@@ -44,23 +110,22 @@
 					  MAIL_TRANSACTION_EXPUNGE) < 0)
 		return -1;
 
-	*expunges_r = buffer_create_dynamic(default_pool, 512);
+	ARRAY_CREATE(expunges_r, default_pool,
+		     struct mail_transaction_expunge, 64);
 	while ((ret = mail_transaction_log_view_next(view->log_view,
 						     &hdr, &data, NULL)) > 0) {
 		i_assert((hdr->type & MAIL_TRANSACTION_EXPUNGE) != 0);
-		mail_transaction_log_sort_expunges(*expunges_r,
-						   data, hdr->size);
+		mail_transaction_log_sort_expunges(expunges_r, data, hdr->size);
 	}
 
 	if (ret < 0) {
-		buffer_free(*expunges_r);
-		*expunges_r = NULL;
+		array_free(expunges_r);
 		return -1;
 	}
 
 	/* convert to sequences */
-	src = dest = buffer_get_modifyable_data(*expunges_r, &size);
-	src_end = PTR_OFFSET(src, size);
+	src = dest = array_get_modifyable(expunges_r, &count);
+	src_end = src + count;
 	for (; src != src_end; src++) {
 		ret = mail_index_lookup_uid_range(view, src->uid1,
 						  src->uid2,
@@ -69,11 +134,11 @@
 		i_assert(ret == 0);
 
 		if (dest->uid1 == 0)
-			size -= sizeof(*dest);
+			count--;
 		else
 			dest++;
 	}
-	buffer_set_used_size(*expunges_r, size);
+	array_delete(expunges_r, count, array_count(expunges_r) - count);
 	return 0;
 }
 
@@ -90,7 +155,7 @@
 	struct mail_index_view_sync_ctx *ctx;
 	struct mail_index_map *map;
 	enum mail_transaction_type mask, want_mask;
-	buffer_t *expunges = NULL;
+	array_t expunges = { 0, 0 };
 
 	/* We must sync flags as long as view is mmap()ed, as the flags may
 	   have already changed under us. */
@@ -120,8 +185,8 @@
 					  view->log_file_offset,
 					  hdr->log_file_seq,
 					  hdr->log_file_int_offset, mask) < 0) {
-		if (expunges != NULL)
-			buffer_free(expunges);
+		if (array_is_created(&expunges))
+			array_free(&expunges);
 		return -1;
 	}
 
@@ -365,12 +430,10 @@
 mail_index_view_sync_get_expunges(struct mail_index_view_sync_ctx *ctx,
 				  unsigned int *count_r)
 {
-	const uint32_t *data;
-	size_t size;
+	const struct mail_transaction_expunge *data;
 
-	data = buffer_get_data(ctx->expunges, &size);
-	*count_r = size / (sizeof(uint32_t)*2);
-	return data;
+	data = array_get(&ctx->expunges, count_r);
+	return (const uint32_t *)data;
 }
 
 void mail_index_view_sync_end(struct mail_index_view_sync_ctx *ctx)
@@ -405,8 +468,8 @@
 					    view->log_file_offset,
 					    MAIL_TRANSACTION_TYPE_MASK);
 
-	if (ctx->expunges != NULL)
-		buffer_free(ctx->expunges);
+	if (array_is_created(&ctx->expunges))
+		array_free(&ctx->expunges);
 
 	view->syncing = FALSE;
 	i_free(ctx);

Index: mail-transaction-util.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-index/mail-transaction-util.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- mail-transaction-util.c	14 Mar 2005 20:48:25 -0000	1.24
+++ mail-transaction-util.c	26 Apr 2005 19:12:08 -0000	1.25
@@ -51,72 +51,3 @@
 	}
 	return type;
 }
-
-void
-mail_transaction_log_sort_expunges(buffer_t *expunges_buf,
-				   const struct mail_transaction_expunge *src,
-				   size_t src_buf_size)
-{
-	const struct mail_transaction_expunge *src_end;
-	struct mail_transaction_expunge *dest;
-	struct mail_transaction_expunge new_exp;
-	size_t first, i, dest_count;
-
-	i_assert(src_buf_size % sizeof(*src) == 0);
-
-	/* @UNSAFE */
-	dest = buffer_get_modifyable_data(expunges_buf, &dest_count);
-	dest_count /= sizeof(*dest);
-
-	if (dest_count == 0) {
-		buffer_append(expunges_buf, src, src_buf_size);
-		return;
-	}
-
-	src_end = CONST_PTR_OFFSET(src, src_buf_size);
-	for (i = 0; src != src_end; src++) {
-		/* src[] must be sorted. */
-		i_assert(src+1 == src_end || src->uid1 < src[1].uid1);
-
-		for (; i < dest_count; i++) {
-			if (src->uid1 < dest[i].uid1)
-				break;
-		}
-
-		new_exp = *src;
-
-		first = i;
-		while (i < dest_count && src->uid2 >= dest[i].uid1-1) {
-			/* we can/must merge with next record */
-			if (new_exp.uid2 < dest[i].uid2)
-				new_exp.uid2 = dest[i].uid2;
-			i++;
-		}
-
-		if (first > 0 && new_exp.uid1 <= dest[first-1].uid2+1) {
-			/* continue previous record */
-			if (dest[first-1].uid2 < new_exp.uid2)
-				dest[first-1].uid2 = new_exp.uid2;
-		} else if (i == first) {
-			buffer_insert(expunges_buf, i * sizeof(new_exp),
-				      &new_exp, sizeof(new_exp));
-			i++; first++;
-
-			dest = buffer_get_modifyable_data(expunges_buf, NULL);
-			dest_count++;
-		} else {
-			/* use next record */
-			dest[first] = new_exp;
-			first++;
-		}
-
-		if (i > first) {
-			buffer_delete(expunges_buf, first * sizeof(new_exp),
-				      (i - first) * sizeof(new_exp));
-
-			dest = buffer_get_modifyable_data(expunges_buf, NULL);
-			dest_count -= i - first;
-			i = first;
-		}
-	}
-}

Index: mail-transaction-util.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib-index/mail-transaction-util.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- mail-transaction-util.h	17 Oct 2004 18:24:21 -0000	1.11
+++ mail-transaction-util.h	26 Apr 2005 19:12:08 -0000	1.12
@@ -13,9 +13,4 @@
 enum mail_transaction_type
 mail_transaction_type_mask_get(enum mail_index_sync_type sync_type);
 
-void
-mail_transaction_log_sort_expunges(buffer_t *expunges_buf,
-				   const struct mail_transaction_expunge *src,
-				   size_t src_buf_size);
-
 #endif



More information about the dovecot-cvs mailing list