index-transaction.c revision 398d66217c5916b14485714954c14cf8af0c8547
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2003-2009 Dovecot authors, see the included COPYING file */
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen#include "lib.h"
49e358eebea107aad9919dcc4bd88cee8519ba2eTimo Sirainen#include "array.h"
49e358eebea107aad9919dcc4bd88cee8519ba2eTimo Sirainen#include "index-storage.h"
49e358eebea107aad9919dcc4bd88cee8519ba2eTimo Sirainen#include "index-mail.h"
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainenvoid index_transaction_init(struct index_transaction_context *t,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen struct index_mailbox *ibox)
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen{
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen t->super = t->trans->v;
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen t->mailbox_ctx.box = &ibox->box;
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen t->ibox = ibox;
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen array_create(&t->mailbox_ctx.module_contexts, default_pool,
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen sizeof(void *), 5);
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen t->trans_view = mail_index_transaction_open_updated_view(t->trans);
7b29ccd796fc75af86f827192d2f8c0e8f0087bbTimo Sirainen t->cache_view = mail_cache_view_open(ibox->cache, t->trans_view);
7b29ccd796fc75af86f827192d2f8c0e8f0087bbTimo Sirainen t->cache_trans = mail_cache_get_transaction(t->cache_view, t->trans);
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
87a6b7df39d6822a5a8289a62f32deabff9b75e4Timo Sirainenstatic void index_transaction_free(struct index_transaction_context *t)
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen{
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen mail_cache_view_close(t->cache_view);
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen mail_index_view_close(&t->trans_view);
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen array_free(&t->mailbox_ctx.module_contexts);
da2aa032ccfa8e7e4a4380ef738014549f4d2c2dTimo Sirainen i_free(t);
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen}
7b29ccd796fc75af86f827192d2f8c0e8f0087bbTimo Sirainen
7b29ccd796fc75af86f827192d2f8c0e8f0087bbTimo Sirainenint index_transaction_finish_commit(struct index_transaction_context *t,
7b29ccd796fc75af86f827192d2f8c0e8f0087bbTimo Sirainen uint32_t *log_file_seq_r,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen uoff_t *log_file_offset_r)
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen{
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen int ret;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen i_assert(t->mail_ref_count == 0);
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvi ret = t->super.commit(t->trans, log_file_seq_r, log_file_offset_r);
43834f87bf431198f986e86052a4f6e558fdb07dTimo Sirainen if (ret < 0)
43834f87bf431198f986e86052a4f6e558fdb07dTimo Sirainen mail_storage_set_index_error(t->ibox);
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen else {
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen if (*log_file_seq_r != 0) {
09801f106cd531a28b4e03ec665e44c421264560Timo Sirainen t->ibox->commit_log_file_seq = *log_file_seq_r;
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen t->ibox->commit_log_file_offset = *log_file_offset_r;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen }
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch index_transaction_free(t);
fe363b433b8038a69b55169da9dca27892ad7d18Timo Sirainen return ret;
212a34c06ff45952c008ae9eec387ced783de6cfPhil Carmody}
212a34c06ff45952c008ae9eec387ced783de6cfPhil Carmody
212a34c06ff45952c008ae9eec387ced783de6cfPhil Carmodyvoid index_transaction_finish_rollback(struct index_transaction_context *t)
212a34c06ff45952c008ae9eec387ced783de6cfPhil Carmody{
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch i_assert(t->mail_ref_count == 0);
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch t->super.rollback(t->trans);
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch index_transaction_free(t);
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch}
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Boschstruct mailbox_transaction_context *
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Boschindex_transaction_begin(struct mailbox *box,
a9a928e40e3b691924c8e5e444e3e1a4320aa3bdStephan Bosch enum mailbox_transaction_flags flags)
10c96a244935de4add8213ba0b894178dfb889a5Timo Sirainen{
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)box;
bdcb00145ad87765e3fd22d4ebc4d2c029a326b9Timo Sirainen struct mail_index_transaction *t;
0c1835a90dd1dcedaeaedd1cd91672299cbeb5beTimo Sirainen struct index_transaction_context *it;
f4735bf7ec2019fdc730e9ebdb39e5a4ea580405Timo Sirainen enum mail_index_transaction_flags trans_flags;
f4735bf7ec2019fdc730e9ebdb39e5a4ea580405Timo Sirainen
f4735bf7ec2019fdc730e9ebdb39e5a4ea580405Timo Sirainen i_assert(box->opened);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen trans_flags = MAIL_INDEX_TRANSACTION_FLAG_AVOID_FLAG_UPDATES;
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen if ((flags & MAILBOX_TRANSACTION_FLAG_HIDE) != 0)
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen trans_flags |= MAIL_INDEX_TRANSACTION_FLAG_HIDE;
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen if ((flags & MAILBOX_TRANSACTION_FLAG_EXTERNAL) != 0)
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen trans_flags |= MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL;
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen if ((flags & MAILBOX_TRANSACTION_FLAG_REFRESH) != 0)
8cb72c59d5ea4e9e5f638d7ec840bb853f5a188eTimo Sirainen (void)mail_index_refresh(ibox->index);
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen t = mail_index_transaction_begin(ibox->view, trans_flags);
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen it = MAIL_STORAGE_CONTEXT(t);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (it == NULL) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_panic("mail storage transaction context mising for type %s",
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen box->storage->name);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen }
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen it->flags = flags;
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen return &it->mailbox_ctx;
1a0ece3e873e3864269ed7eaed957dc10c56d25fTimo Sirainen}
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainenint index_transaction_commit(struct mailbox_transaction_context *_t,
1a0ece3e873e3864269ed7eaed957dc10c56d25fTimo Sirainen uint32_t *uid_validity_r,
1a0ece3e873e3864269ed7eaed957dc10c56d25fTimo Sirainen uint32_t *first_saved_uid_r,
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen uint32_t *last_saved_uid_r)
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen{
c28f6aa0b70af4811c9ace9114fe827c2f503455Timo Sirainen struct index_transaction_context *t =
1a0ece3e873e3864269ed7eaed957dc10c56d25fTimo Sirainen (struct index_transaction_context *)_t;
1a0ece3e873e3864269ed7eaed957dc10c56d25fTimo Sirainen struct mail_index_transaction *itrans = t->trans;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen *uid_validity_r = 0;
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen *first_saved_uid_r = *last_saved_uid_r = 0;
46ce4d9273e6df12ef1912bbdb1c8b84b104f394Timo Sirainen
46ce4d9273e6df12ef1912bbdb1c8b84b104f394Timo Sirainen t->saved_uid_validity = uid_validity_r;
862ec874f9373e3e499e237d3b9f71fdf1413feeTimo Sirainen t->first_saved_uid = first_saved_uid_r;
5af5137f6dc0c9f358b7813e941e26f7bd735b3aTimo Sirainen t->last_saved_uid = last_saved_uid_r;
5af5137f6dc0c9f358b7813e941e26f7bd735b3aTimo Sirainen
5af5137f6dc0c9f358b7813e941e26f7bd735b3aTimo Sirainen return mail_index_transaction_commit(&itrans);
5af5137f6dc0c9f358b7813e941e26f7bd735b3aTimo Sirainen}
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainenvoid index_transaction_rollback(struct mailbox_transaction_context *_t)
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Sirainen{
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen struct index_transaction_context *t =
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen (struct index_transaction_context *)_t;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen struct mail_index_transaction *itrans = t->trans;
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen mail_index_transaction_rollback(&itrans);
7662010b03ffe5f2a6ecf4b4eb220d1c65efea76Timo Sirainen}
7662010b03ffe5f2a6ecf4b4eb220d1c65efea76Timo Sirainen
7662010b03ffe5f2a6ecf4b4eb220d1c65efea76Timo Sirainenvoid index_transaction_set_max_modseq(struct mailbox_transaction_context *_t,
7662010b03ffe5f2a6ecf4b4eb220d1c65efea76Timo Sirainen uint64_t max_modseq,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ARRAY_TYPE(seq_range) *seqs)
71aed7ba87b5fd5e96e97a22d89ac025b883d60aTimo Sirainen{
71aed7ba87b5fd5e96e97a22d89ac025b883d60aTimo Sirainen struct index_transaction_context *t =
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen (struct index_transaction_context *)_t;
71aed7ba87b5fd5e96e97a22d89ac025b883d60aTimo Sirainen
19557f192d37cd54a1a090a8a26d9d47265e4413Aki Tuomi mail_index_transaction_set_max_modseq(t->trans, max_modseq, seqs);
71aed7ba87b5fd5e96e97a22d89ac025b883d60aTimo Sirainen}
71aed7ba87b5fd5e96e97a22d89ac025b883d60aTimo Sirainen