changeset 5729:c3e32c3fb2bf HEAD

Update day headers while committing appends.
author Timo Sirainen <tss@iki.fi>
date Wed, 13 Jun 2007 23:28:23 +0300
parents 7aab5d99fb45
children ae289a38b59a
files src/lib-index/mail-index-sync-update.c src/lib-index/mail-index-transaction.c
diffstat 2 files changed, 53 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-index-sync-update.c	Wed Jun 13 23:00:58 2007 +0300
+++ b/src/lib-index/mail-index-sync-update.c	Wed Jun 13 23:28:23 2007 +0300
@@ -479,44 +479,6 @@
 	return 1;
 }
 
-static void
-mail_index_update_day_headers(struct mail_index_header *hdr, uint32_t uid)
-{
-	// FIXME: move as header updates to transaction committing
-	const int max_days =
-		sizeof(hdr->day_first_uid) / sizeof(hdr->day_first_uid[0]);
-	struct tm tm;
-	time_t stamp;
-	int i, days;
-
-	/* get beginning of today */
-	tm = *localtime(&ioloop_time);
-	tm.tm_hour = 0;
-	tm.tm_min = 0;
-	tm.tm_sec = 0;
-	stamp = mktime(&tm);
-	if (stamp == (time_t)-1)
-		i_panic("mktime(today) failed");
-
-	if ((time_t)hdr->day_stamp >= stamp)
-		return;
-
-	/* get number of days since last message */
-	days = (stamp - hdr->day_stamp) / (3600*24);
-	if (days > max_days)
-		days = max_days;
-
-	/* @UNSAFE: move days forward and fill the missing days with old
-	   day_first_uid[0]. */
-	memcpy(hdr->day_first_uid + days,
-	       hdr->day_first_uid, max_days - days);
-	for (i = 1; i < days; i++)
-		hdr->day_first_uid[i] = hdr->day_first_uid[0];
-
-	hdr->day_stamp = stamp;
-	hdr->day_first_uid[0] = uid;
-}
-
 int mail_index_sync_record(struct mail_index_sync_map_ctx *ctx,
 			   const struct mail_transaction_header *hdr,
 			   const void *data)
@@ -900,9 +862,6 @@
 	map->hdr.log_file_mailbox_offset =
 		index->log->head->mailbox_sync_max_offset;
 
-	/*FIXME: if (first_append_uid != 0)
-		mail_index_update_day_headers(&map->hdr, first_append_uid);*/
-
 	if (map->write_base_header) {
 		i_assert(MAIL_INDEX_MAP_IS_IN_MEMORY(map));
 		buffer_write(map->hdr_copy_buf, 0, &map->hdr, sizeof(map->hdr));
--- a/src/lib-index/mail-index-transaction.c	Wed Jun 13 23:00:58 2007 +0300
+++ b/src/lib-index/mail-index-transaction.c	Wed Jun 13 23:28:23 2007 +0300
@@ -5,6 +5,7 @@
    UIDs. This is because we're able to compress sequence ranges better. */
 
 #include "lib.h"
+#include "ioloop.h"
 #include "array.h"
 #include "seq-range-array.h"
 #include "mail-index-view-private.h"
@@ -14,6 +15,7 @@
 
 #include <stddef.h>
 #include <stdlib.h>
+#include <time.h>
 
 void (*hook_mail_index_transaction_created)
 		(struct mail_index_transaction *t) = NULL;
@@ -289,6 +291,53 @@
 		(m1->uid > m2->uid ? 1 : 0);
 }
 
+static void
+mail_index_update_day_headers(struct mail_index_transaction *t)
+{
+	struct mail_index_header hdr;
+	const struct mail_index_record *rec;
+	const int max_days =
+		sizeof(hdr.day_first_uid) / sizeof(hdr.day_first_uid[0]);
+	struct tm tm;
+	time_t stamp;
+	int i, days;
+
+	hdr = *mail_index_get_header(t->view);
+	rec = array_idx(&t->appends, 0);
+
+	/* get beginning of today */
+	tm = *localtime(&ioloop_time);
+	tm.tm_hour = 0;
+	tm.tm_min = 0;
+	tm.tm_sec = 0;
+	stamp = mktime(&tm);
+	i_assert(stamp != (time_t)-1);
+
+	if ((time_t)hdr.day_stamp >= stamp)
+		return;
+
+	/* get number of days since last message */
+	days = (stamp - hdr.day_stamp) / (3600*24);
+	if (days > max_days)
+		days = max_days;
+
+	/* @UNSAFE: move days forward and fill the missing days with old
+	   day_first_uid[0]. */
+	memcpy(hdr.day_first_uid + days, hdr.day_first_uid, max_days - days);
+	for (i = 1; i < days; i++)
+		hdr.day_first_uid[i] = hdr.day_first_uid[0];
+
+	hdr.day_stamp = stamp;
+	hdr.day_first_uid[0] = rec->uid;
+
+	mail_index_update_header(t,
+		offsetof(struct mail_index_header, day_stamp),
+		&hdr.day_stamp, sizeof(hdr.day_stamp), FALSE);
+	mail_index_update_header(t,
+		offsetof(struct mail_index_header, day_first_uid),
+		hdr.day_first_uid, sizeof(hdr.day_first_uid), FALSE);
+}
+
 void mail_index_transaction_sort_appends(struct mail_index_transaction *t)
 {
 	struct mail_index_record *recs, *sorted_recs;
@@ -380,7 +429,10 @@
                 t->cache_trans_ctx = NULL;
 	}
 
-	mail_index_transaction_sort_appends(t);
+	if (array_is_created(&t->appends)) {
+		mail_index_transaction_sort_appends(t);
+		mail_index_update_day_headers(t);
+	}
 
 	if (mail_index_transaction_convert_to_uids(t) < 0)
 		ret = -1;