dovecot-2.0: More cleanups to transaction log writing code.
dovecot at dovecot.org
dovecot at dovecot.org
Sat May 9 22:52:51 EEST 2009
details: http://hg.dovecot.org/dovecot-2.0/rev/0e407ad46307
changeset: 9254:0e407ad46307
user: Timo Sirainen <tss at iki.fi>
date: Sat May 09 15:18:46 2009 -0400
description:
More cleanups to transaction log writing code.
diffstat:
3 files changed, 72 insertions(+), 65 deletions(-)
src/lib-index/mail-index-transaction-export.c | 87 ++++++++-----------------
src/lib-index/mail-transaction-log-append.c | 46 +++++++++++--
src/lib-index/mail-transaction-log.h | 4 +
diffs (292 lines):
diff -r e144fa1dd2ce -r 0e407ad46307 src/lib-index/mail-index-transaction-export.c
--- a/src/lib-index/mail-index-transaction-export.c Sat May 09 15:04:22 2009 -0400
+++ b/src/lib-index/mail-index-transaction-export.c Sat May 09 15:18:46 2009 -0400
@@ -12,39 +12,11 @@ struct mail_index_export_context {
struct mail_transaction_log_append_ctx *append_ctx;
};
-static void log_append_buffer(struct mail_index_export_context *ctx,
- const buffer_t *buf, const buffer_t *hdr_buf,
- enum mail_transaction_type type)
-{
- buffer_t *output = ctx->append_ctx->output;
- struct mail_transaction_header hdr;
-
- i_assert((type & MAIL_TRANSACTION_TYPE_MASK) != 0);
- i_assert((buf->used % 4) == 0);
- i_assert(hdr_buf == NULL || (hdr_buf->used % 4) == 0);
-
- if (buf->used == 0)
- return;
-
- memset(&hdr, 0, sizeof(hdr));
- hdr.type = type;
- if (type == MAIL_TRANSACTION_EXPUNGE)
- hdr.type |= MAIL_TRANSACTION_EXPUNGE_PROT;
- if ((ctx->trans->flags & MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL) != 0)
- hdr.type |= MAIL_TRANSACTION_EXTERNAL;
- hdr.size = sizeof(hdr) + buf->used +
- (hdr_buf == NULL ? 0 : hdr_buf->used);
- hdr.size = mail_index_uint32_to_offset(hdr.size);
-
- buffer_append(output, &hdr, sizeof(hdr));
- if (hdr_buf != NULL)
- buffer_append(output, hdr_buf->data, hdr_buf->used);
- buffer_append(output, buf->data, buf->used);
-
- if (mail_transaction_header_has_modseq(buf->data,
- CONST_PTR_OFFSET(buf->data, sizeof(hdr)),
- ctx->append_ctx->new_highest_modseq))
- ctx->append_ctx->new_highest_modseq++;
+static void
+log_append_buffer(struct mail_index_export_context *ctx,
+ const buffer_t *buf, enum mail_transaction_type type)
+{
+ mail_transaction_log_append_add(ctx->append_ctx, type, buf);
}
static const buffer_t *
@@ -147,7 +119,7 @@ static void log_append_ext_intro(struct
ctx->append_ctx->new_highest_modseq = 1;
}
- log_append_buffer(ctx, buf, NULL, MAIL_TRANSACTION_EXT_INTRO);
+ log_append_buffer(ctx, buf, MAIL_TRANSACTION_EXT_INTRO);
}
static void
@@ -183,7 +155,7 @@ log_append_ext_hdr_update(struct mail_in
}
if (buf->used % 4 != 0)
buffer_append_zero(buf, 4 - buf->used % 4);
- log_append_buffer(ctx, buf, NULL, MAIL_TRANSACTION_EXT_HDR_UPDATE);
+ log_append_buffer(ctx, buf, MAIL_TRANSACTION_EXT_HDR_UPDATE);
}
static void
@@ -260,7 +232,7 @@ mail_transaction_log_append_ext_intros(s
if (ext_reset.new_reset_id != 0) {
i_assert(ext_id < reset_id_count &&
ext_reset.new_reset_id == reset_ids[ext_id]);
- log_append_buffer(ctx, reset_buf, NULL,
+ log_append_buffer(ctx, reset_buf,
MAIL_TRANSACTION_EXT_RESET);
}
if (ext_id < hdrs_count && hdrs[ext_id].alloc_size > 0) {
@@ -297,13 +269,13 @@ static void log_append_ext_recs(struct m
reset_id = ext_id < reset_id_count ? reset_ids[ext_id] : 0;
log_append_ext_intro(ctx, ext_id, reset_id);
- log_append_buffer(ctx, updates[ext_id].arr.buffer, NULL, type);
+ log_append_buffer(ctx, updates[ext_id].arr.buffer, type);
}
}
static void
log_append_keyword_update(struct mail_index_export_context *ctx,
- buffer_t *hdr_buf, enum modify_type modify_type,
+ buffer_t *tmp_buf, enum modify_type modify_type,
const char *keyword, const buffer_t *buffer)
{
struct mail_transaction_keyword_update kt_hdr;
@@ -312,14 +284,14 @@ log_append_keyword_update(struct mail_in
kt_hdr.modify_type = modify_type;
kt_hdr.name_size = strlen(keyword);
- buffer_set_used_size(hdr_buf, 0);
- buffer_append(hdr_buf, &kt_hdr, sizeof(kt_hdr));
- buffer_append(hdr_buf, keyword, kt_hdr.name_size);
- if ((hdr_buf->used % 4) != 0)
- buffer_append_zero(hdr_buf, 4 - (hdr_buf->used % 4));
-
- log_append_buffer(ctx, buffer, hdr_buf,
- MAIL_TRANSACTION_KEYWORD_UPDATE);
+ buffer_set_used_size(tmp_buf, 0);
+ buffer_append(tmp_buf, &kt_hdr, sizeof(kt_hdr));
+ buffer_append(tmp_buf, keyword, kt_hdr.name_size);
+ if ((tmp_buf->used % 4) != 0)
+ buffer_append_zero(tmp_buf, 4 - (tmp_buf->used % 4));
+ buffer_append(tmp_buf, buffer->data, buffer->used);
+
+ log_append_buffer(ctx, tmp_buf, MAIL_TRANSACTION_KEYWORD_UPDATE);
}
static enum mail_index_sync_type
@@ -327,11 +299,11 @@ log_append_keyword_updates(struct mail_i
{
const struct mail_index_transaction_keyword_update *updates;
const char *const *keywords;
- buffer_t *hdr_buf;
+ buffer_t *tmp_buf;
enum mail_index_sync_type change_mask = 0;
unsigned int i, count, keywords_count;
- hdr_buf = buffer_create_dynamic(pool_datastack_create(), 64);
+ tmp_buf = buffer_create_dynamic(pool_datastack_create(), 64);
keywords = array_get_modifiable(&ctx->trans->view->index->keywords,
&keywords_count);
@@ -341,13 +313,13 @@ log_append_keyword_updates(struct mail_i
for (i = 0; i < count; i++) {
if (array_is_created(&updates[i].add_seq)) {
change_mask |= MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD;
- log_append_keyword_update(ctx, hdr_buf,
+ log_append_keyword_update(ctx, tmp_buf,
MODIFY_ADD, keywords[i],
updates[i].add_seq.arr.buffer);
}
if (array_is_created(&updates[i].remove_seq)) {
change_mask |= MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE;
- log_append_keyword_update(ctx, hdr_buf,
+ log_append_keyword_update(ctx, tmp_buf,
MODIFY_REMOVE, keywords[i],
updates[i].remove_seq.arr.buffer);
}
@@ -370,18 +342,17 @@ void mail_index_transaction_export(struc
mail_transaction_log_append_ext_intros(&ctx);
if (t->pre_hdr_changed) {
- log_append_buffer(&ctx,
- log_get_hdr_update_buffer(t, TRUE),
- NULL, MAIL_TRANSACTION_HEADER_UPDATE);
+ log_append_buffer(&ctx, log_get_hdr_update_buffer(t, TRUE),
+ MAIL_TRANSACTION_HEADER_UPDATE);
}
if (array_is_created(&t->appends)) {
change_mask |= MAIL_INDEX_SYNC_TYPE_APPEND;
- log_append_buffer(&ctx, t->appends.arr.buffer, NULL,
+ log_append_buffer(&ctx, t->appends.arr.buffer,
MAIL_TRANSACTION_APPEND);
}
if (array_is_created(&t->updates)) {
change_mask |= MAIL_INDEX_SYNC_TYPE_FLAGS;
- log_append_buffer(&ctx, t->updates.arr.buffer, NULL,
+ log_append_buffer(&ctx, t->updates.arr.buffer,
MAIL_TRANSACTION_FLAG_UPDATE);
}
@@ -398,7 +369,7 @@ void mail_index_transaction_export(struc
if (array_is_created(&t->keyword_resets)) {
change_mask |= MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET;
log_append_buffer(&ctx, t->keyword_resets.arr.buffer,
- NULL, MAIL_TRANSACTION_KEYWORD_RESET);
+ MAIL_TRANSACTION_KEYWORD_RESET);
}
if (array_is_created(&t->keyword_updates))
change_mask |= log_append_keyword_updates(&ctx);
@@ -408,13 +379,13 @@ void mail_index_transaction_export(struc
checking fsync_mask */
if ((t->flags & MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL) != 0)
change_mask |= MAIL_INDEX_SYNC_TYPE_EXPUNGE;
- log_append_buffer(&ctx, t->expunges.arr.buffer, NULL,
+ log_append_buffer(&ctx, t->expunges.arr.buffer,
MAIL_TRANSACTION_EXPUNGE);
}
if (t->post_hdr_changed) {
log_append_buffer(&ctx, log_get_hdr_update_buffer(t, FALSE),
- NULL, MAIL_TRANSACTION_HEADER_UPDATE);
+ MAIL_TRANSACTION_HEADER_UPDATE);
}
/* Update the tail offsets only when committing the sync transaction.
diff -r e144fa1dd2ce -r 0e407ad46307 src/lib-index/mail-transaction-log-append.c
--- a/src/lib-index/mail-transaction-log-append.c Sat May 09 15:04:22 2009 -0400
+++ b/src/lib-index/mail-transaction-log-append.c Sat May 09 15:18:46 2009 -0400
@@ -5,6 +5,36 @@
#include "write-full.h"
#include "mail-index-private.h"
#include "mail-transaction-log-private.h"
+
+void mail_transaction_log_append_add(struct mail_transaction_log_append_ctx *ctx,
+ enum mail_transaction_type type,
+ const buffer_t *buf)
+{
+ struct mail_transaction_header hdr;
+
+ i_assert((type & MAIL_TRANSACTION_TYPE_MASK) != 0);
+ i_assert((buf->used % 4) == 0);
+
+ if (buf->used == 0)
+ return;
+
+ memset(&hdr, 0, sizeof(hdr));
+ hdr.type = type;
+ if (type == MAIL_TRANSACTION_EXPUNGE)
+ hdr.type |= MAIL_TRANSACTION_EXPUNGE_PROT;
+ if (ctx->external)
+ hdr.type |= MAIL_TRANSACTION_EXTERNAL;
+ hdr.size = sizeof(hdr) + buf->used;
+ hdr.size = mail_index_uint32_to_offset(hdr.size);
+
+ buffer_append(ctx->output, &hdr, sizeof(hdr));
+ buffer_append(ctx->output, buf->data, buf->used);
+
+ if (mail_transaction_header_has_modseq(buf->data,
+ CONST_PTR_OFFSET(buf->data, sizeof(hdr)),
+ ctx->new_highest_modseq))
+ ctx->new_highest_modseq++;
+}
static int
log_buffer_move_to_memory(struct mail_transaction_log_append_ctx *ctx)
@@ -102,6 +132,7 @@ log_append_sync_offset_if_needed(struct
struct mail_transaction_log_file *file = ctx->log->head;
struct mail_transaction_header_update *u;
struct mail_transaction_header *hdr;
+ buffer_t *buf;
uint32_t offset;
if (file->max_tail_offset == file->sync_offset) {
@@ -119,15 +150,15 @@ log_append_sync_offset_if_needed(struct
return;
i_assert(offset > file->saved_tail_offset);
- hdr = buffer_append_space_unsafe(ctx->output, sizeof(*hdr));
- hdr->type = MAIL_TRANSACTION_HEADER_UPDATE | MAIL_TRANSACTION_EXTERNAL;
- hdr->size = sizeof(*hdr) + sizeof(*u) + sizeof(uint32_t);
- hdr->size = mail_index_uint32_to_offset(hdr->size);
-
- u = buffer_append_space_unsafe(ctx->output, sizeof(*u));
+ buf = buffer_create_static_hard(pool_datastack_create(),
+ sizeof(*u) + sizeof(offset));
+ u = buffer_append_space_unsafe(buf, sizeof(*u));
u->offset = offsetof(struct mail_index_header, log_file_tail_offset);
u->size = sizeof(offset);
- buffer_append(ctx->output, &offset, sizeof(offset));
+ buffer_append(buf, &offset, sizeof(offset));
+
+ mail_transaction_log_append_add(ctx, MAIL_TRANSACTION_HEADER_UPDATE,
+ buf);
}
static int
@@ -172,6 +203,7 @@ int mail_transaction_log_append_begin(st
ctx = i_new(struct mail_transaction_log_append_ctx, 1);
ctx->log = index->log;
ctx->output = buffer_create_dynamic(default_pool, 1024);
+ ctx->external = (t->flags & MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL) != 0;
*ctx_r = ctx;
return 0;
diff -r e144fa1dd2ce -r 0e407ad46307 src/lib-index/mail-transaction-log.h
--- a/src/lib-index/mail-transaction-log.h Sat May 09 15:04:22 2009 -0400
+++ b/src/lib-index/mail-transaction-log.h Sat May 09 15:18:46 2009 -0400
@@ -136,6 +136,7 @@ struct mail_transaction_log_append_ctx {
buffer_t *output;
uint64_t new_highest_modseq;
+ unsigned int external:1;
unsigned int append_sync_offset:1;
unsigned int sync_includes_this:1;
unsigned int want_fsync:1;
@@ -225,6 +226,9 @@ void mail_transaction_log_views_close(st
int mail_transaction_log_append_begin(struct mail_index_transaction *t,
struct mail_transaction_log_append_ctx **ctx_r);
+void mail_transaction_log_append_add(struct mail_transaction_log_append_ctx *ctx,
+ enum mail_transaction_type type,
+ const buffer_t *buf);
int mail_transaction_log_append_commit(struct mail_transaction_log_append_ctx **ctx);
/* Lock transaction log for index synchronization. Log cannot be read or
More information about the dovecot-cvs
mailing list