mail-index-transaction.c revision 1aa80711dcef7f193038b1f3ff65f331b43a4beb
/* Copyright (c) 2003-2009 Dovecot authors, see the included COPYING file */
/* Inside transaction we keep messages stored in sequences in uid fields.
Before they're written to transaction log the sequences are changed to
UIDs. This is because we're able to compress sequence ranges better. */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "bsearch-insert-pos.h"
#include "seq-range-array.h"
#include "mail-index-view-private.h"
#include "mail-index-modseq.h"
#include "mail-transaction-log.h"
#include "mail-cache-private.h"
#include "mail-index-transaction-private.h"
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
(struct mail_index_transaction *t) = NULL;
static bool
void mail_index_transaction_reset(struct mail_index_transaction *t)
{
unsigned i, count;
if (array_is_created(&t->ext_rec_updates)) {
for (i = 0; i < count; i++) {
if (array_is_created(&recs[i]))
array_free(&recs[i]);
}
array_free(&t->ext_rec_updates);
}
if (array_is_created(&t->ext_rec_atomics)) {
for (i = 0; i < count; i++) {
if (array_is_created(&recs[i]))
array_free(&recs[i]);
}
array_free(&t->ext_rec_atomics);
}
if (array_is_created(&t->ext_hdr_updates)) {
for (i = 0; i < count; i++) {
}
array_free(&t->ext_hdr_updates);
}
if (array_is_created(&t->keyword_updates)) {
struct mail_index_transaction_keyword_update *u;
for (i = 0; i < count; i++) {
if (array_is_created(&u[i].add_seq))
array_free(&u[i].add_seq);
if (array_is_created(&u[i].remove_seq))
array_free(&u[i].remove_seq);
}
array_free(&t->keyword_updates);
}
if (array_is_created(&t->keyword_resets))
array_free(&t->keyword_resets);
if (array_is_created(&t->appends))
array_free(&t->appends);
if (array_is_created(&t->expunges))
array_free(&t->expunges);
if (array_is_created(&t->updates))
array_free(&t->updates);
if (array_is_created(&t->ext_resizes))
array_free(&t->ext_resizes);
if (array_is_created(&t->ext_resets))
array_free(&t->ext_resets);
if (array_is_created(&t->ext_reset_ids))
array_free(&t->ext_reset_ids);
if (array_is_created(&t->ext_reset_atomic))
array_free(&t->ext_reset_atomic);
t->last_new_seq = 0;
t->last_update_idx = 0;
t->min_flagupdate_seq = 0;
t->max_flagupdate_seq = 0;
if (t->cache_trans_ctx != NULL)
t->appends_nonsorted = FALSE;
t->pre_hdr_changed = FALSE;
t->post_hdr_changed = FALSE;
t->log_updates = FALSE;
t->log_ext_updates = FALSE;
}
static bool mail_index_transaction_has_changes(struct mail_index_transaction *t)
{
/* flag updates aren't included in log_updates */
return array_is_created(&t->appends) ||
array_is_created(&t->expunges) ||
array_is_created(&t->keyword_resets) ||
array_is_created(&t->keyword_updates) ||
t->pre_hdr_changed || t->post_hdr_changed;
}
static void mail_index_transaction_free(struct mail_index_transaction *t)
{
array_free(&t->module_contexts);
mail_index_view_close(&t->view);
i_free(t);
}
struct mail_index_view *
{
return t->view;
}
bool mail_index_transaction_is_expunged(struct mail_index_transaction *t,
{
return array_is_created(&t->expunges) &&
}
void mail_index_transaction_ref(struct mail_index_transaction *t)
{
t->refcount++;
}
{
struct mail_index_transaction *t = *_t;
if (--t->refcount == 0)
}
{
}
{
const void *base;
unsigned int count;
}
void *old_record)
{
void *p;
unsigned int idx, aligned_record_size;
/* records need to be 32bit aligned */
if (!array_is_created(array)) {
sizeof(seq) + aligned_record_size,
}
/* already there, update */
if (old_record != NULL) {
/* save the old record before overwriting it */
}
return TRUE;
} else {
/* insert */
return FALSE;
}
}
static uint32_t
{
const struct mail_index_record *rec;
if (seq >= t->first_new_seq)
else {
}
}
static void
mail_index_convert_to_uids(struct mail_index_transaction *t,
{
unsigned int i, count;
if (!array_is_created(array))
return;
for (i = 0; i < count; i++) {
}
}
static uint32_t
get_nonexpunged_uid2(struct mail_index_transaction *t,
{
seq1++;
seq1++;
uid1++;
}
return uid1;
}
static void
{
unsigned int i, count;
if (!array_is_created(array))
return;
for (i = 0; i < count; i++) {
/* simple conversion */
} else {
/* remove expunged UIDs */
count++;
/* continue the range without the inserted seqs */
}
}
}
static void keyword_updates_convert_to_uids(struct mail_index_transaction *t)
{
unsigned int i, count;
if (!array_is_created(&t->keyword_updates))
return;
for (i = 0; i < count; i++) {
}
}
void mail_index_transaction_convert_to_uids(struct mail_index_transaction *t)
{
unsigned int i, count;
if (array_is_created(&t->ext_rec_updates)) {
for (i = 0; i < count; i++)
mail_index_convert_to_uids(t, (void *)&updates[i]);
}
if (array_is_created(&t->ext_rec_atomics)) {
for (i = 0; i < count; i++)
mail_index_convert_to_uids(t, (void *)&updates[i]);
}
mail_index_convert_to_uid_ranges(t, &t->expunges);
mail_index_convert_to_uid_ranges(t, (void *)&t->updates);
}
struct uid_map {
};
{
}
static void
{
struct mail_index_header hdr;
const struct mail_index_record *rec;
int i, days;
/* get beginning of today */
return;
/* get number of days since last message */
/* @UNSAFE: move days forward and fill the missing days with old
day_first_uid[0]. */
for (i = 1; i < days; i++)
}
static void
const uint32_t *old_to_newseq_map)
{
unsigned int ext_count;
unsigned int i, j, count;
if (!array_is_created(updates))
return;
for (j = 0; j < count; j++) {
old_array = &ext_rec_arrays[j];
if (!array_is_created(old_array))
continue;
for (i = 0; i < ext_count; i++) {
}
ext_rec_arrays[j] = new_array;
}
}
static void
sort_appends_seq_range(struct mail_index_transaction *t,
const uint32_t *old_to_newseq_map)
{
unsigned int i, count;
for (i = 0; i < count; i++) {
break;
}
if (i == count) {
/* nothing to do */
return;
}
i++;
}
for (i = 0; i < count; i++) {
}
}
static void
const uint32_t *old_to_newseq_map)
{
unsigned int i, count;
if (array_is_created(&t->keyword_updates)) {
for (i = 0; i < count; i++) {
}
&updates[i].remove_seq,
}
}
}
if (array_is_created(&t->keyword_resets)) {
sort_appends_seq_range(t, &t->keyword_resets,
}
}
void mail_index_transaction_sort_appends(struct mail_index_transaction *t)
{
struct uid_map *new_uid_map;
unsigned int i, count;
if (!t->appends_nonsorted)
return;
/* first make a copy of the UIDs and map them to sequences */
for (i = 0; i < count; i++) {
new_uid_map[i].idx = i;
}
/* now sort the UID map */
/* sort mail records */
for (i = 1; i < count; i++) {
i_panic("Duplicate UIDs added in transaction");
}
sizeof(*sorted_recs) * count);
for (i = 0; i < count; i++)
t->appends_nonsorted = FALSE;
}
{
unsigned int offset;
/* get next_uid from appends if they have UIDs */
}
if (t->post_hdr_mask[offset] != 0) {
hdr = (const void *)t->post_hdr_change;
}
if (t->pre_hdr_mask[offset] != 0) {
hdr = (const void *)t->pre_hdr_change;
}
return next_uid;
}
static int mail_index_transaction_commit_v(struct mail_index_transaction *t,
{
int ret;
i_assert(t->first_new_seq >
if (t->cache_trans_ctx != NULL)
if (array_is_created(&t->appends)) {
}
/* if we're committing a normal transaction, we want to
have those changes in the index mapping immediately. this
is especially important when committing cache offset
updates.
however if we're syncing the index now, the mapping must
be done later as MAIL_INDEX_SYNC_HANDLER_FILE so that
expunge handlers get run for the newly expunged messages
(and sync handlers that require HANDLER_FILE as well). */
}
return ret;
}
static void mail_index_transaction_rollback_v(struct mail_index_transaction *t)
{
if (t->cache_trans_ctx != NULL)
}
{
struct mail_index_transaction *t = *_t;
if (mail_index_view_is_inconsistent(t->view)) {
return -1;
}
}
{
struct mail_index_transaction *t = *_t;
t->v.rollback(t);
}
struct mail_index_record *
{
}
{
struct mail_index_record *rec;
i_assert(!t->no_appends);
t->log_updates = TRUE;
if (!array_is_created(&t->appends))
/* sequence number is visible only inside given view,
so let it generate it */
if (t->last_new_seq != 0)
*seq_r = ++t->last_new_seq;
else
if (uid != 0) {
if (!t->appends_nonsorted &&
t->last_new_seq != t->first_new_seq) {
/* if previous record's UID is larger than this one,
we'll have to sort the appends later */
t->appends_nonsorted = TRUE;
i_panic("Duplicate UIDs added in transaction");
}
if (t->highest_append_uid < uid)
t->highest_append_uid = uid;
}
}
void mail_index_append_assign_uids(struct mail_index_transaction *t,
{
struct mail_index_record *recs;
unsigned int i, count;
if (!array_is_created(&t->appends))
return;
/* find the first mail with uid = 0 */
for (i = 0; i < count; i++) {
break;
}
for (; i < count; i++) {
}
*next_uid_r = first_uid;
}
static void
{
if (!array_is_created(updates))
return;
for (i = 0; i < count; i++) {
if (array_is_created(&seqs[i]) &&
}
}
static void
{
unsigned int i, count;
/* remove extension updates */
/* remove keywords */
if (array_is_created(&t->keyword_resets))
if (array_is_created(&t->keyword_updates)) {
for (i = 0; i < count; i++) {
seq);
}
}
}
}
/* and finally remove the append itself */
t->last_new_seq--;
if (t->first_new_seq > t->last_new_seq) {
t->last_new_seq = 0;
t->appends_nonsorted = FALSE;
array_free(&t->appends);
}
}
{
if (seq >= t->first_new_seq) {
/* we can handle only the last append. otherwise we'd have to
renumber sequences and that gets tricky. for now this is
enough, since we typically want to expunge all the
appends. */
} else {
t->log_updates = TRUE;
/* expunges is a sorted array of {seq1, seq2, ..}, .. */
}
}
static void update_minmax_flagupdate_seq(struct mail_index_transaction *t,
{
if (t->min_flagupdate_seq == 0) {
t->min_flagupdate_seq = seq1;
t->max_flagupdate_seq = seq2;
} else {
if (t->min_flagupdate_seq > seq1)
t->min_flagupdate_seq = seq1;
if (t->max_flagupdate_seq < seq2)
t->max_flagupdate_seq = seq2;
}
}
static bool
const struct mail_transaction_flag_update *u)
{
const struct mail_index_record *rec;
if ((t->flags & MAIL_INDEX_TRANSACTION_FLAG_AVOID_FLAG_UPDATES) == 0)
return TRUE;
return TRUE;
}
return FALSE;
}
unsigned int
unsigned int left_idx,
unsigned int right_idx,
{
const struct mail_transaction_flag_update *updates;
/* find the first update with either overlapping range,
or the update which will come after our insert */
else
break;
}
idx++;
return idx;
}
static void
struct mail_transaction_flag_update u,
unsigned int idx)
{
unsigned int count;
/* insert new update */
tmp_update = u;
move = 0;
} else {
/* split existing update from beginning */
move = 1;
}
if (mail_transaction_update_want_add(t, &tmp_update)) {
&count);
}
}
/* split existing update from end */
if (mail_transaction_update_want_add(t, &tmp_update))
}
~u.remove_flags;
~u.add_flags;
/* we can remove this update completely */
idx--;
}
/* break here before idx++ so last_update_idx is set
correctly */
break;
}
}
if (mail_transaction_update_want_add(t, &u)) {
count++;
}
}
}
enum modify_type modify_type,
enum mail_flags flags)
{
switch (modify_type) {
case MODIFY_REPLACE:
break;
case MODIFY_ADD:
break;
case MODIFY_REMOVE:
break;
}
}
void mail_index_update_flags_range(struct mail_index_transaction *t,
enum modify_type modify_type,
enum mail_flags flags)
{
struct mail_index_record *rec;
struct mail_transaction_flag_update u, *last_update;
if (seq2 >= t->first_new_seq) {
/* updates for appended messages, modify them directly */
}
if (seq1 >= t->first_new_seq)
return;
/* range contains also existing messages. update them next. */
}
memset(&u, 0, sizeof(u));
switch (modify_type) {
case MODIFY_REPLACE:
break;
case MODIFY_ADD:
break;
case MODIFY_REMOVE:
u.remove_flags = flags;
break;
}
if (!array_is_created(&t->updates)) {
if (mail_transaction_update_want_add(t, &u))
return;
}
if (t->last_update_idx < count) {
/* fast path - hopefully we're updating the next message,
or a message that is to be appended as last update */
last_update += t->last_update_idx;
/* we can just update the UID range */
if (mail_transaction_update_want_add(t, &u))
return;
}
/* hopefully we can just append it */
t->last_update_idx++;
last_update++;
}
}
if (t->last_update_idx == count) {
if (mail_transaction_update_want_add(t, &u))
else if (t->last_update_idx > 0)
t->last_update_idx--;
} else {
/* slow path */
/* added after this */
} else {
/* added before this or on top of this */
first_idx = 0;
}
mail_index_insert_flag_update(t, u, idx);
}
}
enum modify_type modify_type,
enum mail_flags flags)
{
}
void mail_index_update_header(struct mail_index_transaction *t,
bool prepend)
{
t->log_updates = TRUE;
if (prepend) {
t->pre_hdr_changed = TRUE;
} else {
t->post_hdr_changed = TRUE;
}
}
{
struct mail_transaction_ext_intro intro;
/* get ext_id from transaction's map if it's there */
/* have to create it */
const struct mail_index_registered_ext *rext;
} else {
const struct mail_index_ext *ext;
}
/* allow only header size changes if extension records have already
been changed in transaction */
(old_record_size == record_size &&
old_record_align == record_align));
t->log_ext_updates = TRUE;
if (!array_is_created(&t->ext_resizes))
}
{
struct mail_transaction_ext_reset reset;
if (!array_is_created(&t->ext_resets))
t->log_ext_updates = TRUE;
}
{
if (!array_is_created(&t->ext_reset_atomic))
}
static bool
{
unsigned int i, count;
if (array_is_created(arr)) {
for (i = 0; i < count; i++) {
if (array_is_created(&array[i]))
return TRUE;
}
}
return FALSE;
}
static bool
{
unsigned int i, count;
return TRUE;
return TRUE;
if (array_is_created(&t->ext_hdr_updates)) {
const struct mail_index_transaction_ext_hdr_update *hdr;
for (i = 0; i < count; i++) {
if (hdr[i].alloc_size > 0)
return TRUE;
}
}
if (array_is_created(&t->ext_resets)) {
const struct mail_transaction_ext_reset *resets;
for (i = 0; i < count; i++) {
if (resets[i].new_reset_id != 0)
return TRUE;
}
}
if (array_is_created(&t->ext_resizes)) {
const struct mail_transaction_ext_intro *resizes;
for (i = 0; i < count; i++) {
return TRUE;
}
}
return FALSE;
}
static void
{
/* if extension records have been updated, clear them */
if (array_is_created(array))
}
}
void mail_index_ext_set_reset_id(struct mail_index_transaction *t,
{
if (array_is_created(&t->ext_hdr_updates) &&
/* if extension headers have been updated, clear them */
struct mail_index_transaction_ext_hdr_update *hdr;
if (hdr->alloc_size > 0) {
}
hdr->alloc_size = 0;
}
if (array_is_created(&t->ext_resets) &&
/* clear resets */
}
if (array_is_created(&t->ext_resizes) &&
/* clear resizes */
}
if (!array_is_created(&t->ext_reset_ids))
}
void mail_index_update_header_ext(struct mail_index_transaction *t,
{
struct mail_index_transaction_ext_hdr_update *hdr;
if (!array_is_created(&t->ext_hdr_updates))
}
t->log_ext_updates = TRUE;
}
{
const struct mail_index_registered_ext *rext;
const struct mail_transaction_ext_intro *intro;
unsigned int count;
seq <= t->last_new_seq));
t->log_ext_updates = TRUE;
if (!array_is_created(&t->ext_resizes)) {
count = 0;
} else {
}
/* resized record */
} else {
}
if (!array_is_created(&t->ext_rec_updates))
/* @UNSAFE */
old_data_r)) {
/* not found, clear old_data if it was given */
if (old_data_r != NULL)
}
}
{
seq <= t->last_new_seq));
/* currently non-external transactions can be applied multiple times,
causing multiple increments. */
t->log_ext_updates = TRUE;
if (!array_is_created(&t->ext_rec_atomics))
}
struct mail_keywords *
const char *const keywords[])
{
struct mail_keywords *k;
if (count == 0) {
return k;
}
/* @UNSAFE */
k = i_malloc(sizeof(struct mail_keywords) +
/* look up the keywords from index. they're never removed from there
so we can permanently store indexes to them. */
/* ignore if this is a duplicate */
for (i = 0; i < src; i++) {
break;
}
if (i == src)
dest++;
}
return k;
}
struct mail_keywords *
const ARRAY_TYPE(keyword_indexes)
{
struct mail_keywords *k;
const unsigned int *indexes;
if (count == 0) {
return k;
}
/* @UNSAFE */
k = i_malloc(sizeof(struct mail_keywords) +
/* copy but skip duplicates */
for (i = 0; i < src; i++) {
break;
}
if (i == src)
}
return k;
}
{
}
static bool
enum modify_type modify_type,
struct mail_keywords *keywords)
{
struct mail_index_transaction_keyword_update *u;
const unsigned int *existing_idx;
unsigned int i, j, existing_count;
bool found;
if (seq < t->first_new_seq)
return TRUE;
u = array_idx_modifiable(&t->keyword_updates,
if (array_is_created(&u->add_seq) ||
array_is_created(&u->remove_seq))
return TRUE;
for (j = 0; j < existing_count; j++) {
break;
}
}
switch (modify_type) {
case MODIFY_ADD:
case MODIFY_REPLACE:
if (!found)
return TRUE;
break;
case MODIFY_REMOVE:
if (found)
return TRUE;
break;
}
}
return FALSE;
}
enum modify_type modify_type,
struct mail_keywords *keywords)
{
struct mail_index_transaction_keyword_update *u;
unsigned int i, ku_count;
bool changed;
seq <= t->last_new_seq));
}
if ((t->flags & MAIL_INDEX_TRANSACTION_FLAG_AVOID_FLAG_UPDATES) != 0) {
T_BEGIN {
keywords);
} T_END;
if (!changed)
return;
}
/* Update add_seq and remove_seq arrays which describe the keyword
changes. Don't bother updating remove_seq or keyword resets for
newly added messages since they default to not having any
keywords anyway. */
switch (modify_type) {
case MODIFY_ADD:
u = array_idx_modifiable(&t->keyword_updates,
}
break;
case MODIFY_REMOVE:
u = array_idx_modifiable(&t->keyword_updates,
if (seq < t->first_new_seq)
}
break;
case MODIFY_REPLACE:
if (array_is_created(&t->keyword_updates)) {
u = array_get_modifiable(&t->keyword_updates,
&ku_count);
for (i = 0; i < ku_count; i++) {
}
}
/* Add the wanted keyword back */
u = array_idx_modifiable(&t->keyword_updates,
}
if (seq < t->first_new_seq)
break;
}
t->log_updates = TRUE;
}
void mail_index_reset(struct mail_index_transaction *t)
{
}
void mail_index_transaction_set_max_modseq(struct mail_index_transaction *t,
{
t->max_modseq = max_modseq;
t->conflict_seqs = seqs;
}
static bool
{
if (array_is_created(array)) {
if (array_count(array) == 0)
return TRUE;
}
}
return FALSE;
}
static bool
{
struct mail_index_transaction_keyword_update *kw;
unsigned int i, count;
if (array_is_created(&t->keyword_updates)) {
for (i = 0; i < count; i++) {
seq))
}
if (!have_kw_changes)
array_free(&t->keyword_updates);
}
if (!array_is_created(&t->updates))
return ret;
/* exists */
if (count > 1)
else
array_free(&t->updates);
else {
/* need to split it in two */
tmp_update = updates[i];
}
}
return ret;
}
void mail_index_transaction_check_conflicts(struct mail_index_transaction *t)
{
i_assert(t->max_modseq != 0);
/* no conflicts possible */
return;
}
if (t->min_flagupdate_seq == 0) {
/* no flag updates */
return;
}
if (mail_index_update_cancel(t, seq))
}
}
}
static struct mail_index_transaction_vfuncs trans_vfuncs = {
};
struct mail_index_transaction *
{
struct mail_index_transaction *t;
/* don't allow syncing view while there's ongoing transactions */
t->refcount = 1;
t->v = trans_vfuncs;
/* transaction view cannot work if new records are being added
in two places. make sure it doesn't happen. */
t->no_appends = TRUE;
} else {
t->first_new_seq =
}
return t;
}