c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef MAIL_INDEX_TRANSACTION_PRIVATE_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define MAIL_INDEX_TRANSACTION_PRIVATE_H
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen#include "seq-range-array.h"
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen#include "mail-transaction-log.h"
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo SirainenARRAY_DEFINE_TYPE(seq_array_array, ARRAY_TYPE(seq_array));
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstruct mail_index_transaction_keyword_update {
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen ARRAY_TYPE(seq_range) add_seq;
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen ARRAY_TYPE(seq_range) remove_seq;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen};
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
beb6125ee872e7fed57745ab33e6de99639180f3Timo Sirainenstruct mail_index_transaction_ext_hdr_update {
81a5d8714c566ce50c4a2409f59e82e716d576d2Timo Sirainen size_t alloc_size;
81a5d8714c566ce50c4a2409f59e82e716d576d2Timo Sirainen /* mask is in bytes, not bits */
81a5d8714c566ce50c4a2409f59e82e716d576d2Timo Sirainen unsigned char *mask;
81a5d8714c566ce50c4a2409f59e82e716d576d2Timo Sirainen unsigned char *data;
beb6125ee872e7fed57745ab33e6de99639180f3Timo Sirainen};
beb6125ee872e7fed57745ab33e6de99639180f3Timo Sirainen
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainenstruct mail_index_transaction_vfuncs {
5e40ed3f0a2c2acddc9b8eab59670c7a850114c5Timo Sirainen void (*reset)(struct mail_index_transaction *t);
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen int (*commit)(struct mail_index_transaction *t,
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen struct mail_index_transaction_commit_result *result_r);
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen void (*rollback)(struct mail_index_transaction *t);
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen};
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainenunion mail_index_transaction_module_context {
26e5bdf37d7d0deed1e2e8483366c83631b9d251Aki Tuomi struct mail_index_transaction_vfuncs super;
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen struct mail_index_module_register *reg;
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen};
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainenstruct mail_index_flag_update {
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen uint32_t uid1, uid2;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen uint16_t add_flags;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen uint16_t remove_flags;
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen};
5da1aa5197a43d83f0fb3eeb83125c7cd73d1b62Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct mail_index_transaction {
ef4d0eafab4d26bba047551db1e23ceff8aa9404Timo Sirainen struct mail_index_transaction *prev, *next;
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen int refcount;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen
db87d16551d1081ada01f787ea21aa3ed1402c31Timo Sirainen enum mail_index_transaction_flags flags;
26e5bdf37d7d0deed1e2e8483366c83631b9d251Aki Tuomi struct mail_index_transaction_vfuncs v, *vlast;
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen struct mail_index_view *view;
515f81466f673c1b4f72e053f1a9686e6fca6b61Timo Sirainen struct mail_index_view *latest_view;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen /* NOTE: If you add anything new, remember to update
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen mail_index_transaction_reset_v() to reset it. */
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mail_index_record) appends;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen uint32_t first_new_seq, last_new_seq;
cf93189100a9136f2fd508c26e1573410bc8c1aaTimo Sirainen uint32_t highest_append_uid;
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen /* lowest/highest sequence that updates flags/keywords */
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen uint32_t min_flagupdate_seq, max_flagupdate_seq;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mail_transaction_modseq_update) modseq_updates;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mail_transaction_expunge_guid) expunges;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mail_index_flag_update) updates;
c251a38df327599a62d341bf5c2282f31352faa5Timo Sirainen size_t last_update_idx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen unsigned char pre_hdr_change[sizeof(struct mail_index_header)];
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen unsigned char pre_hdr_mask[sizeof(struct mail_index_header)];
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen unsigned char post_hdr_change[sizeof(struct mail_index_header)];
aa38d1a0945f0bc13a225d043f53fad2eec666b1Timo Sirainen unsigned char post_hdr_mask[sizeof(struct mail_index_header)];
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mail_index_transaction_ext_hdr_update) ext_hdr_updates;
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo Sirainen ARRAY_TYPE(seq_array_array) ext_rec_updates;
c5794838af9995f50bfecb06a3cd4f9a0ac77858Timo Sirainen ARRAY_TYPE(seq_array_array) ext_rec_atomics;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mail_transaction_ext_intro) ext_resizes;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mail_transaction_ext_reset) ext_resets;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(uint32_t) ext_reset_ids;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(uint32_t) ext_reset_atomic;
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct mail_index_transaction_keyword_update) keyword_updates;
fee561b9d9162b130e662914fcebc9dd99b5c320Timo Sirainen buffer_t *attribute_updates; /* [+-][ps]key\0.. */
238812433b4f7965fd662dce0f4efccb092630a8Timo Sirainen buffer_t *attribute_updates_suffix; /* <timestamp>[<value len>].. */
1175f27441385a7011629f295f42708f9a3a4ffcTimo Sirainen
4b89231f4ec9cc69f4aea715e1d34f405c7e317dTimo Sirainen uint64_t min_highest_modseq;
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen uint64_t max_modseq;
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen ARRAY_TYPE(seq_range) *conflict_seqs;
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen /* Module-specific contexts. */
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(union mail_index_transaction_module_context *) module_contexts;
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool no_appends:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool sync_transaction:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool appends_nonsorted:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool expunges_nonsorted:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool drop_unnecessary_flag_updates:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool pre_hdr_changed:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool post_hdr_changed:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool reset:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool index_deleted:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool index_undeleted:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool commit_deleted_index:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool tail_offset_changed:1;
a022088674a5ae72ed29ae001834cbad62a4f19fTimo Sirainen /* non-extension updates. flag updates don't change this because
a022088674a5ae72ed29ae001834cbad62a4f19fTimo Sirainen they may be added and removed, so be sure to check that the updates
a022088674a5ae72ed29ae001834cbad62a4f19fTimo Sirainen array is non-empty also. */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool log_updates:1;
a2cbf1d392ee983520451bc9b849a490f28ac298Timo Sirainen /* extension updates */
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool log_ext_updates:1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#define MAIL_INDEX_TRANSACTION_HAS_CHANGES(t) \
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen ((t)->log_updates || (t)->log_ext_updates || \
8872e5c991430f96138a46e36b7f3c2c40d8e5c2Timo Sirainen (array_is_created(&(t)->updates) && array_count(&(t)->updates) > 0) || \
8872e5c991430f96138a46e36b7f3c2c40d8e5c2Timo Sirainen (t)->index_deleted || (t)->index_undeleted)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
2b8ff102f5117f917248f98ccbdc8e6a6af83c87Aki Tuomitypedef void hook_mail_index_transaction_created_t(struct mail_index_transaction *t);
2b8ff102f5117f917248f98ccbdc8e6a6af83c87Aki Tuomi
a102d189881a35d72ac3106a9e7e00577ae69310Aki Tuomivoid mail_index_transaction_hook_register(hook_mail_index_transaction_created_t *hook);
a102d189881a35d72ac3106a9e7e00577ae69310Aki Tuomivoid mail_index_transaction_hook_unregister(hook_mail_index_transaction_created_t *hook);
d6badc27cd6e8d3398877b6766cb0aaeef3a7800Timo Sirainen
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainenstruct mail_index_record *
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainenmail_index_transaction_lookup(struct mail_index_transaction *t, uint32_t seq);
24fc71a693331ffe77e2b6d81c70aca6fa055e47Timo Sirainen
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainenvoid mail_index_transaction_ref(struct mail_index_transaction *t);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenvoid mail_index_transaction_unref(struct mail_index_transaction **t);
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainenvoid mail_index_transaction_reset_v(struct mail_index_transaction *t);
09c3a491f4f6ccebe290c7709bdc0d79a187610bTimo Sirainen
985fa802913c96ce6f2e25bbc788ee39c416a7e0Timo Sirainenvoid mail_index_transaction_sort_appends(struct mail_index_transaction *t);
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainenvoid mail_index_transaction_sort_expunges(struct mail_index_transaction *t);
1d3f7c1278168d5b1cbfa9a2cc9929a0909056b4Timo Sirainenuint32_t mail_index_transaction_get_next_uid(struct mail_index_transaction *t);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid mail_index_transaction_set_log_updates(struct mail_index_transaction *t);
d97f939b1c09e9f90b01b6f81bfb1c05da990148Timo Sirainenvoid mail_index_update_day_headers(struct mail_index_transaction *t, time_t day_stamp);
985fa802913c96ce6f2e25bbc788ee39c416a7e0Timo Sirainen
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainenunsigned int
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainenmail_index_transaction_get_flag_update_pos(struct mail_index_transaction *t,
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen unsigned int left_idx,
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen unsigned int right_idx,
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen uint32_t seq);
515f81466f673c1b4f72e053f1a9686e6fca6b61Timo Sirainenvoid mail_index_transaction_lookup_latest_keywords(struct mail_index_transaction *t,
515f81466f673c1b4f72e053f1a9686e6fca6b61Timo Sirainen uint32_t seq,
515f81466f673c1b4f72e053f1a9686e6fca6b61Timo Sirainen ARRAY_TYPE(keyword_indexes) *keywords);
3fe9483b2b412a14493e3120751b0e99ecfe9388Timo Sirainen
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenbool mail_index_cancel_flag_updates(struct mail_index_transaction *t,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t seq);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainenbool mail_index_cancel_keyword_updates(struct mail_index_transaction *t,
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen uint32_t seq);
02bb8313a711dfe50c7f01e8132e13ca93ecfb42Timo Sirainen
9e6d83a3ef6abb393eeebca423cfd0d8cb08d430Timo Sirainen/* As input the array's each element starts with struct seq_range where
9e6d83a3ef6abb393eeebca423cfd0d8cb08d430Timo Sirainen uid1..uid2 are actually sequences within the transaction view. This function
9e6d83a3ef6abb393eeebca423cfd0d8cb08d430Timo Sirainen changes the sequences into UIDs. If the transaction has any appends, they
9e6d83a3ef6abb393eeebca423cfd0d8cb08d430Timo Sirainen must have already been assigned UIDs. */
9e6d83a3ef6abb393eeebca423cfd0d8cb08d430Timo Sirainenvoid mail_index_transaction_seq_range_to_uid(struct mail_index_transaction *t,
9e6d83a3ef6abb393eeebca423cfd0d8cb08d430Timo Sirainen ARRAY_TYPE(seq_range) *array);
a42d489d99bcf7d18cfd0352fdf0c9348cb224fcTimo Sirainenvoid mail_index_transaction_finish_so_far(struct mail_index_transaction *t);
5e88e4624aa6d482b5b195acd2f4e02aeb385f20Timo Sirainenvoid mail_index_transaction_finish(struct mail_index_transaction *t);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid mail_index_transaction_export(struct mail_index_transaction *t,
6a0855a04d75bce3c0a2f68517d02f86ae72087fTimo Sirainen struct mail_transaction_log_append_ctx *append_ctx,
6a0855a04d75bce3c0a2f68517d02f86ae72087fTimo Sirainen enum mail_index_transaction_change *changes_r);
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainenint mail_transaction_expunge_guid_cmp(const struct mail_transaction_expunge_guid *e1,
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen const struct mail_transaction_expunge_guid *e2);
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainenunsigned int
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainenmail_index_transaction_get_flag_update_pos(struct mail_index_transaction *t,
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen unsigned int left_idx,
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen unsigned int right_idx,
b346610430690398b8c840006004a2df4aa8ce92Timo Sirainen uint32_t seq);
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainenvoid mail_index_ext_using_reset_id(struct mail_index_transaction *t,
16c28dd75976f94acc4940d6ba68b6cd4853aac7Timo Sirainen uint32_t ext_id, uint32_t reset_id);
16c28dd75976f94acc4940d6ba68b6cd4853aac7Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#endif