mail-index-view.c revision bb2b91b4c5363348b737237893d414639510a561
89a126810703c666309310d0f3189e9834d70b5bTimo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#include "lib.h"
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#include "array.h"
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen#include "buffer.h"
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen#include "mail-index-view-private.h"
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen#include "mail-transaction-log.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainenvoid mail_index_view_clone(struct mail_index_view *dest,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen const struct mail_index_view *src)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen memset(dest, 0, sizeof(dest));
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dest->refcount = 1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dest->methods = src->methods;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dest->index = src->index;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dest->log_view = mail_transaction_log_view_open(src->index->log);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dest->indexid = src->indexid;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dest->map = src->map;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dest->map->refcount++;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dest->hdr = src->hdr;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen dest->log_file_seq = src->log_file_seq;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen dest->log_file_offset = src->log_file_offset;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen}
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainenvoid mail_index_view_ref(struct mail_index_view *view)
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen{
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen view->refcount++;
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen}
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainenstatic void _view_close(struct mail_index_view *view)
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen{
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen i_assert(view->refcount == 0);
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen
cb951d3282610a9a0960230865bc5f3e3347b203Timo Sirainen mail_index_view_unlock(view);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen mail_transaction_log_view_close(view->log_view);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen if (array_is_created(&view->log_syncs))
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen array_free(&view->log_syncs);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen mail_index_unmap(view->index, view->map);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (array_is_created(&view->map_refs)) {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen mail_index_view_unref_maps(view);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen array_free(&view->map_refs);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen i_free(view);
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen}
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainenint mail_index_view_lock_head(struct mail_index_view *view, int update_index)
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen unsigned int lock_id;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (MAIL_INDEX_MAP_IS_IN_MEMORY(view->index->map))
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen return 0;
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (!mail_index_is_locked(view->index, view->lock_id)) {
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen if (mail_index_lock_shared(view->index, update_index,
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen &view->lock_id) < 0)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen return -1;
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (mail_index_map(view->index, FALSE) <= 0) {
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen view->inconsistent = TRUE;
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen return -1;
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (view->index->indexid != view->indexid) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* index was rebuilt */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen view->inconsistent = TRUE;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen return -1;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen } else if (update_index) {
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen if (mail_index_lock_shared(view->index, TRUE, &lock_id) < 0)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen return -1;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen mail_index_unlock(view->index, view->lock_id);
fd1f0e9ef52b3e157cfd1a01c464c2ac7458ab17Timo Sirainen view->lock_id = lock_id;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen i_assert(view->index->lock_type != F_UNLCK);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint mail_index_view_lock(struct mail_index_view *view)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (mail_index_view_is_inconsistent(view))
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen return -1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen if (view->map != view->index->map) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* not head mapping, no need to lock */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return mail_index_view_lock_head(view, FALSE);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid mail_index_view_unlock(struct mail_index_view *view)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (view->lock_id != 0) {
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen mail_index_unlock(view->index, view->lock_id);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen view->lock_id = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint mail_index_view_is_inconsistent(struct mail_index_view *view)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (view->index->indexid != view->indexid)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen view->inconsistent = TRUE;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return view->inconsistent;
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstruct mail_index *mail_index_view_get_index(struct mail_index_view *view)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return view->index;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid mail_index_view_transaction_ref(struct mail_index_view *view)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen view->transactions++;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid mail_index_view_transaction_unref(struct mail_index_view *view)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen i_assert(view->transactions > 0);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen view->transactions--;
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
26a8b7deb3a5b6f26f9c4d71538e1248f680e4beTimo Sirainenstatic void mail_index_view_ref_map(struct mail_index_view *view,
26a8b7deb3a5b6f26f9c4d71538e1248f680e4beTimo Sirainen struct mail_index_map *map)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail_index_map *const *maps;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen unsigned int i, count;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (array_is_created(&view->map_refs)) {
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen maps = array_get(&view->map_refs, &count);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* if map is already referenced, do nothing */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (i = 0; i < count; i++) {
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (maps[i] == map)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen return;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen }
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen } else {
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen ARRAY_CREATE(&view->map_refs, default_pool,
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen struct mail_index_map *, 4);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen /* reference the given mapping. the reference is dropped when the view
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen is synchronized or closed. */
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen map->refcount++;
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen array_append(&view->map_refs, &map, 1);
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainenvoid mail_index_view_unref_maps(struct mail_index_view *view)
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail_index_map *const *maps;
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen unsigned int i, count;
8a1c866a4c429f26c8746525f82024bc387f1407Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (!array_is_created(&view->map_refs))
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen return;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen maps = array_get(&view->map_refs, &count);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen for (i = 0; i < count; i++)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen mail_index_unmap(view->index, maps[i]);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen array_clear(&view->map_refs);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainenstatic uint32_t _view_get_messages_count(struct mail_index_view *view)
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen{
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen return view->hdr.messages_count;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstatic const struct mail_index_header *
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen_view_get_header(struct mail_index_view *view)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return &view->hdr;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstatic int _view_lookup_full(struct mail_index_view *view, uint32_t seq,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail_index_map **map_r,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const struct mail_index_record **rec_r)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail_index_map *map;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen const struct mail_index_record *rec, *head_rec;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen i_assert(seq > 0 && seq <= mail_index_view_get_messages_count(view));
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen if (mail_index_view_lock(view) < 0)
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen return -1;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen /* look up the record */
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen rec = MAIL_INDEX_MAP_IDX(view->map, seq-1);
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen if (view->map == view->index->map) {
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen /* view's mapping is latest. we can use it directly. */
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen *map_r = view->map;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen *rec_r = rec;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen return 1;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen }
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen /* look up the record from head mapping. it may contain some changes. */
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen if (mail_index_view_lock_head(view, FALSE) < 0)
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen return -1;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen /* start looking up from the same sequence as in the old view.
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen if there are no expunges, it's there. otherwise it's somewhere
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen before (since records can't be inserted).
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen usually there are only a few expunges, so just going downwards from
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen our initial sequence position is probably faster than binary
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen search. */
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen if (seq > view->index->hdr->messages_count)
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen seq = view->index->hdr->messages_count;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen if (seq == 0) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen /* everything is expunged from head. use the old record. */
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *map_r = view->map;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *rec_r = rec;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen return 0;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen }
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen map = view->index->map;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen do {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen seq--;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen head_rec = MAIL_INDEX_MAP_IDX(map, seq);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen if (head_rec->uid <= rec->uid)
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen break;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen } while (seq > 0);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen if (head_rec->uid == rec->uid) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen /* found it. use it. reference the index mapping so that the
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen returned record doesn't get invalidated after next sync. */
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen mail_index_view_ref_map(view, view->index->map);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *map_r = view->index->map;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *rec_r = head_rec;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen return 1;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen } else {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen /* expuned from head. use the old record. */
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *map_r = view->map;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *rec_r = rec;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen return 0;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen }
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen}
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainenstatic int _view_lookup_uid(struct mail_index_view *view, uint32_t seq,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen uint32_t *uid_r)
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen{
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen i_assert(seq > 0 && seq <= mail_index_view_get_messages_count(view));
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen if (mail_index_view_lock(view) < 0)
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen return -1;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen *uid_r = MAIL_INDEX_MAP_IDX(view->map, seq-1)->uid;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen return 0;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen}
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainenstatic uint32_t mail_index_bsearch_uid(struct mail_index_view *view,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen uint32_t uid, uint32_t *left_idx_p,
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen int nearest_side)
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen{
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen const struct mail_index_record *rec_base, *rec;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen uint32_t idx, left_idx, right_idx, record_size;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen i_assert(view->hdr.messages_count <= view->map->records_count);
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen rec_base = view->map->records;
a449ed9df03e9f7e93d1ec278754416ac3ad9073Timo Sirainen record_size = view->map->hdr.record_size;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen idx = left_idx = *left_idx_p;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen right_idx = view->hdr.messages_count;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen while (left_idx < right_idx) {
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen idx = (left_idx + right_idx) / 2;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen rec = CONST_PTR_OFFSET(rec_base, idx * record_size);
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen if (rec->uid < uid)
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen left_idx = idx+1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen else if (rec->uid > uid)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen right_idx = idx;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen else
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen break;
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (idx == view->hdr.messages_count) {
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen /* no messages available */
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen return 0;
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen *left_idx_p = left_idx;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen rec = CONST_PTR_OFFSET(rec_base, idx * record_size);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (rec->uid != uid) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (nearest_side > 0) {
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen /* we want uid or larger */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return rec->uid > uid ? idx+1 :
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen idx == view->hdr.messages_count-1 ? 0 : idx+2;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen } else {
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen /* we want uid or smaller */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return rec->uid < uid ? idx + 1 : idx;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
f3bf2314198da2877ce640360581a61d60c90991Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return idx+1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
12dc81583d1958cb301a617e19fbd40e8d376397Timo Sirainenstatic int _view_lookup_uid_range(struct mail_index_view *view,
12dc81583d1958cb301a617e19fbd40e8d376397Timo Sirainen uint32_t first_uid, uint32_t last_uid,
12dc81583d1958cb301a617e19fbd40e8d376397Timo Sirainen uint32_t *first_seq_r, uint32_t *last_seq_r)
12dc81583d1958cb301a617e19fbd40e8d376397Timo Sirainen{
94e1adead9faddec88a623485b9999a87b1684faTimo Sirainen uint32_t left_idx;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen i_assert(first_uid > 0);
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen i_assert(first_uid <= last_uid);
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen if (mail_index_view_lock(view) < 0)
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen return -1;
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen if (last_uid >= view->map->hdr.next_uid) {
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen last_uid = view->map->hdr.next_uid-1;
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen if (first_uid > last_uid) {
fc84f8af4794f4bb6caf6e5ec3fb1f8cebd0462aTimo Sirainen *first_seq_r = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *last_seq_r = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen }
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen left_idx = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *first_seq_r = mail_index_bsearch_uid(view, first_uid, &left_idx, 1);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (*first_seq_r == 0 ||
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen MAIL_INDEX_MAP_IDX(view->map, *first_seq_r-1)->uid > last_uid) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *first_seq_r = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *last_seq_r = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (first_uid == last_uid) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *last_seq_r = *first_seq_r;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* optimization - binary lookup only from right side: */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *last_seq_r = mail_index_bsearch_uid(view, last_uid, &left_idx, -1);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen i_assert(*last_seq_r >= *first_seq_r);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstatic int _view_lookup_first(struct mail_index_view *view,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen enum mail_flags flags, uint8_t flags_mask,
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen uint32_t *seq_r)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen#define LOW_UPDATE(x) \
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen STMT_START { if ((x) > low_uid) low_uid = x; } STMT_END
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen const struct mail_index_record *rec;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen uint32_t seq, low_uid = 1;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen *seq_r = 0;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if (mail_index_view_lock(view) < 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return -1;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if ((flags_mask & MAIL_RECENT) != 0 && (flags & MAIL_RECENT) != 0)
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen LOW_UPDATE(view->map->hdr.first_recent_uid_lowwater);
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if ((flags_mask & MAIL_SEEN) != 0 && (flags & MAIL_SEEN) == 0)
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen LOW_UPDATE(view->map->hdr.first_unseen_uid_lowwater);
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if ((flags_mask & MAIL_DELETED) != 0 && (flags & MAIL_DELETED) != 0)
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen LOW_UPDATE(view->map->hdr.first_deleted_uid_lowwater);
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if (low_uid == 1)
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen seq = 1;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen else {
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if (mail_index_lookup_uid_range(view, low_uid, low_uid,
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen &seq, &seq) < 0)
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen return -1;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen if (seq == 0)
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen return 0;
de5c7c99783cd86f3bdbc057345cbee923b51a20Timo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen i_assert(view->hdr.messages_count <= view->map->records_count);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (; seq <= view->hdr.messages_count; seq++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen rec = MAIL_INDEX_MAP_IDX(view->map, seq-1);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if ((rec->flags & flags_mask) == (uint8_t)flags) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *seq_r = seq;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen break;
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return 0;
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen}
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenstatic int _view_lookup_ext_full(struct mail_index_view *view, uint32_t seq,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint32_t ext_id, struct mail_index_map **map_r,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const void **data_r)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const struct mail_index_ext *ext;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen const struct mail_index_record *rec;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen uint32_t idx, offset;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen int ret;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen if ((ret = mail_index_lookup_full(view, seq, map_r, &rec)) < 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return -1;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (rec == NULL || !mail_index_map_get_ext_idx(*map_r, ext_id, &idx)) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen *data_r = NULL;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen return ret;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ext = array_idx(&(*map_r)->extensions, idx);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen offset = ext->record_offset;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen *data_r = offset == 0 ? NULL : CONST_PTR_OFFSET(rec, offset);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen return ret;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenstatic int _view_get_header_ext(struct mail_index_view *view,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct mail_index_map *map, uint32_t ext_id,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen const void **data_r, size_t *data_size_r)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen const struct mail_index_ext *ext;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen uint32_t idx;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen /* if we have a mapping, the view where it's from is already locked */
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (map == NULL) {
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen /* no mapping given, use head mapping */
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (mail_index_view_lock_head(view, FALSE) < 0)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return -1;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen map = view->index->map;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (!mail_index_map_get_ext_idx(map, ext_id, &idx)) {
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen /* extension doesn't exist in this index file */
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *data_r = NULL;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *data_size_r = 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen ext = array_idx(&map->extensions, idx);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *data_r = CONST_PTR_OFFSET(map->hdr_base, ext->hdr_offset);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *data_size_r = ext->hdr_size;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenvoid mail_index_view_close(struct mail_index_view *view)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (--view->refcount > 0)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->methods.close(view);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenuint32_t mail_index_view_get_messages_count(struct mail_index_view *view)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return view->methods.get_messages_count(view);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenconst struct mail_index_header *
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenmail_index_get_header(struct mail_index_view *view)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen return view->methods.get_header(view);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenint mail_index_lookup(struct mail_index_view *view, uint32_t seq,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const struct mail_index_record **rec_r)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail_index_map *map;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen return mail_index_lookup_full(view, seq, &map, rec_r);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint mail_index_lookup_full(struct mail_index_view *view, uint32_t seq,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail_index_map **map_r,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const struct mail_index_record **rec_r)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen return view->methods.lookup_full(view, seq, map_r, rec_r);
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint mail_index_lookup_keywords(struct mail_index_view *view, uint32_t seq,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen array_t *keyword_idx)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ARRAY_SET_TYPE(keyword_idx, unsigned int);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct mail_index_map *map;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const void *data;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen const unsigned char *keyword_data;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen const unsigned int *keyword_idx_map;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen unsigned int i, j, keyword_count, index_idx;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint32_t ext_id, idx;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint16_t record_size;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen int ret;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen array_clear(keyword_idx);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* get the keywords data. */
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen ext_id = view->index->keywords_ext_id;
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen ret = mail_index_lookup_ext_full(view, seq, ext_id, &map, &data);
bd7b1a9000b12349e2a99bb43b3ce8b96a18e92bTimo Sirainen if (ret < 0)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return -1;
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen if (data == NULL) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* no keywords at all in index */
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen return ret;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen (void)mail_index_ext_get_size(view, ext_id, map, NULL,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen &record_size, NULL);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* keyword_idx_map[] contains file => index keyword mapping */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (!array_is_created(&map->keyword_idx_map)) {
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen keyword_idx_map = NULL;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen keyword_count = 0;
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen } else {
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen keyword_idx_map = array_get(&map->keyword_idx_map,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen &keyword_count);
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen keyword_data = data;
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen for (i = 0, idx = 0; i < record_size; i++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* first do the quick check to see if there's keywords at all */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (keyword_data[i] == 0)
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen continue;
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen
f6f94063c4c3080280b87ab47ac2bb756ba002f9Timo Sirainen idx = i * CHAR_BIT;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen for (j = 0; j < CHAR_BIT; j++, idx++) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if ((keyword_data[i] & (1 << j)) == 0)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen continue;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen if (idx >= keyword_count) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* keyword header is updated, re-read
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen it so we know what this one is
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen called */
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen if (mail_index_map_read_keywords(view->index,
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen map) < 0)
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen return -1;
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (!array_is_created(&map->keyword_idx_map))
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return ret;
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen /* pointer may have changed. update it. */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen keyword_idx_map =
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen array_get(&map->keyword_idx_map,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen &keyword_count);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen if (idx >= keyword_count) {
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen /* extra bits set in keyword bytes.
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen shouldn't happen, but just ignore. */
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen break;
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen }
e392fcb39a06609af20a9e79017683f194de3ddeTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen index_idx = keyword_idx_map[idx];
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen array_append(keyword_idx, &index_idx, 1);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen }
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen return ret;
43d32cbe60fdaef2699d99f1ca259053e9350411Timo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint mail_index_lookup_uid(struct mail_index_view *view, uint32_t seq,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint32_t *uid_r)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen return view->methods.lookup_uid(view, seq, uid_r);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainenint mail_index_lookup_uid_range(struct mail_index_view *view,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint32_t first_uid, uint32_t last_uid,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint32_t *first_seq_r, uint32_t *last_seq_r)
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return view->methods.lookup_uid_range(view, first_uid, last_uid,
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen first_seq_r, last_seq_r);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenint mail_index_lookup_first(struct mail_index_view *view, enum mail_flags flags,
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen uint8_t flags_mask, uint32_t *seq_r)
62300a38f91227b9de043a9a8ec1d4f1978e1138Timo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen return view->methods.lookup_first(view, flags, flags_mask, seq_r);
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen}
1795e934ebcd58175d3b5bbdd811b13c7889efa3Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainenint mail_index_lookup_ext(struct mail_index_view *view, uint32_t seq,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen uint32_t ext_id, const void **data_r)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen{
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen struct mail_index_map *map;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return view->methods.lookup_ext_full(view, seq, ext_id, &map, data_r);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen}
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainenint mail_index_lookup_ext_full(struct mail_index_view *view, uint32_t seq,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen uint32_t ext_id, struct mail_index_map **map_r,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen const void **data_r)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen{
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return view->methods.lookup_ext_full(view, seq, ext_id, map_r, data_r);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen}
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainenint mail_index_get_header_ext(struct mail_index_view *view, uint32_t ext_id,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen const void **data_r, size_t *data_size_r)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen{
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return view->methods.get_header_ext(view, NULL, ext_id,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen data_r, data_size_r);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen}
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainenint mail_index_map_get_header_ext(struct mail_index_view *view,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen struct mail_index_map *map, uint32_t ext_id,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen const void **data_r, size_t *data_size_r)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen{
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen return view->methods.get_header_ext(view, map, ext_id,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen data_r, data_size_r);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen}
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainenint mail_index_ext_get_size(struct mail_index_view *view __attr_unused__,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen uint32_t ext_id, struct mail_index_map *map,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen uint32_t *hdr_size_r, uint16_t *record_size_r,
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen uint16_t *record_align_r)
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen{
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen const struct mail_index_ext *ext;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen uint32_t idx;
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen i_assert(map != NULL);
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen if (!mail_index_map_get_ext_idx(map, ext_id, &idx)) {
b2e181e33889f0a4a3ba9dc23d676cbfe1bf7782Timo Sirainen /* extension doesn't exist in this index file */
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (hdr_size_r != NULL)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *hdr_size_r = 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (record_size_r != NULL)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *record_size_r = 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (record_align_r != NULL)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *record_align_r = 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen }
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen ext = array_idx(&map->extensions, idx);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (hdr_size_r != NULL)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *hdr_size_r = ext->hdr_size;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (record_size_r != NULL)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *record_size_r = ext->record_size;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (record_align_r != NULL)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen *record_align_r = ext->record_align;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenstatic struct mail_index_view_methods view_methods = {
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_close,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_get_messages_count,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_get_header,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_lookup_full,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_lookup_uid,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_lookup_uid_range,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_lookup_first,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_lookup_ext_full,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen _view_get_header_ext
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen};
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenstruct mail_index_view *mail_index_view_open(struct mail_index *index)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen struct mail_index_view *view;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen i_assert(index->map != NULL);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view = i_new(struct mail_index_view, 1);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->refcount = 1;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->methods = view_methods;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->index = index;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->log_view = mail_transaction_log_view_open(index->log);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->indexid = index->indexid;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->map = index->map;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->map->refcount++;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->hdr = view->map->hdr;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->log_file_seq = view->map->hdr.log_file_seq;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->log_file_offset =
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen I_MIN(view->map->hdr.log_file_int_offset,
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen view->map->hdr.log_file_ext_offset);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return view;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenconst struct mail_index_ext *
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainenmail_index_view_get_ext(struct mail_index_view *view, uint32_t ext_id)
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen{
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen uint32_t idx;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen if (!mail_index_map_get_ext_idx(view->map, ext_id, &idx))
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return 0;
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen return array_idx(&view->map->extensions, idx);
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen}
503e5ef896c7b4a51cf73efb0d132860a8c747e6Timo Sirainen