a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen#ifndef MAIL_INDEX_STRMAP_H
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen#define MAIL_INDEX_STRMAP_H
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen#include "hash2.h"
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstruct mail_index;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstruct mail_index_view;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstruct mail_index_strmap_header {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen#define MAIL_INDEX_STRMAP_VERSION 1
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint8_t version;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint8_t unused[3];
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint32_t uid_validity;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen};
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstruct mail_index_strmap_rec {
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint32_t uid;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint32_t ref_index;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen /* unique index number for the string */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint32_t str_idx;
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen};
a914bff43644dd9b3977244203839ca74161e40cTimo SirainenARRAY_DEFINE_TYPE(mail_index_strmap_rec, struct mail_index_strmap_rec);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainentypedef bool
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenmail_index_strmap_key_cmp_t(const char *key,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const struct mail_index_strmap_rec *rec,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen void *context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen/* Returns 1 if matches, 0 if not, -1 if one of the records is expunged and
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen the result can't be determined */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainentypedef int
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenmail_index_strmap_rec_cmp_t(const struct mail_index_strmap_rec *rec1,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const struct mail_index_strmap_rec *rec2,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen void *context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen/* called when string indexes are renumbered. idx_map[old_idx] = new_idx.
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen if new_idx is 0, the record was expunged. As a special case if count=0,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen the strmap was reset. */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainentypedef void mail_index_strmap_remap_t(const uint32_t *idx_map,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen unsigned int old_count,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen unsigned int new_count, void *context);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstruct mail_index_strmap *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenmail_index_strmap_init(struct mail_index *index, const char *suffix);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenvoid mail_index_strmap_deinit(struct mail_index_strmap **strmap);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen/* Returns strmap records and hash that can be used for read-only access.
19557f192d37cd54a1a090a8a26d9d47265e4413Aki Tuomi The records array always terminates with a record containing zeros (but it's
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen not counted in the array count). */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstruct mail_index_strmap_view *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenmail_index_strmap_view_open(struct mail_index_strmap *strmap,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen struct mail_index_view *idx_view,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen mail_index_strmap_key_cmp_t *key_compare_cb,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen mail_index_strmap_rec_cmp_t *rec_compare_cb,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen mail_index_strmap_remap_t *remap_cb,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen void *context,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const ARRAY_TYPE(mail_index_strmap_rec) **recs_r,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const struct hash2_table **hash_r);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenvoid mail_index_strmap_view_close(struct mail_index_strmap_view **view);
f24202d1a342ac63fb8c28e6fa74e16d37f29ca3Timo Sirainenvoid mail_index_strmap_view_set_corrupted(struct mail_index_strmap_view *view);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen/* Return the highest used string index. */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenuint32_t mail_index_strmap_view_get_highest_idx(struct mail_index_strmap_view *view);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen/* Synchronize strmap: Caller adds missing entries, expunged messages may be
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen removed internally and the changes are written to disk. Note that the strmap
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen recs/hash shouldn't be used until _sync_commit() is called, because the
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen string indexes may be renumbered if another process had already written the
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen same changes as us. */
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenstruct mail_index_strmap_view_sync *
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenmail_index_strmap_view_sync_init(struct mail_index_strmap_view *view,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint32_t *last_uid_r);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenvoid mail_index_strmap_view_sync_add(struct mail_index_strmap_view_sync *sync,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint32_t uid, uint32_t ref_index,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen const char *key);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenvoid mail_index_strmap_view_sync_add_unique(struct mail_index_strmap_view_sync *sync,
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen uint32_t uid, uint32_t ref_index);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenvoid mail_index_strmap_view_sync_commit(struct mail_index_strmap_view_sync **sync);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainenvoid mail_index_strmap_view_sync_rollback(struct mail_index_strmap_view_sync **sync);
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen
a914bff43644dd9b3977244203839ca74161e40cTimo Sirainen#endif