bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainenstatic int virtual_search_record_cmp(const struct virtual_search_record *r1,
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenstatic int mail_search_get_result(struct mail_search_context *ctx)
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen for (arg = ctx->args->args; arg != NULL; arg = arg->next) {
f3bb2fbe87425dc89a839908985af496f7f65702Timo Sirainenstatic void virtual_search_get_records(struct mail_search_context *ctx,
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen (struct virtual_mailbox *)ctx->transaction->box;
f3bb2fbe87425dc89a839908985af496f7f65702Timo Sirainen while (index_storage_search_next_update_seq(ctx)) {
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen /* full match, no need to check this any further */
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen /* possible match, save and check later */
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mail_index_lookup_ext(mbox->box.view, ctx->seq,
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen mail_search_args_reset(ctx->args->args, FALSE);
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen array_sort(&vctx->records, virtual_search_record_cmp);
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen ctx->progress_max = array_count(&vctx->records);
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenvirtual_search_init(struct mailbox_transaction_context *t,
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen struct mailbox_header_lookup_ctx *wanted_headers)
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen ctx = index_storage_search_init(t, args, sort_program,
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen vctx = i_new(struct virtual_search_context, 1);
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen vctx->search_state = VIRTUAL_SEARCH_STATE_BUILD;
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen MODULE_CONTEXT_SET(ctx, virtual_storage_module, vctx);
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen seq_range_array_iter_init(&vctx->result_iter, &vctx->result);
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenint virtual_search_deinit(struct mail_search_context *ctx)
109c39ab66c5aa85bf37d5bbf4ce91c6f966268bAki Tuomi struct virtual_search_context *vctx = VIRTUAL_CONTEXT_REQUIRE(ctx);
f3bb2fbe87425dc89a839908985af496f7f65702Timo Sirainenbool virtual_search_next_nonblock(struct mail_search_context *ctx,
109c39ab66c5aa85bf37d5bbf4ce91c6f966268bAki Tuomi struct virtual_search_context *vctx = VIRTUAL_CONTEXT_REQUIRE(ctx);
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen struct index_search_context *ictx = (struct index_search_context *)ctx;
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen vctx->search_state = VIRTUAL_SEARCH_STATE_SORT;
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen vctx->search_state = VIRTUAL_SEARCH_STATE_RETURN;
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen return virtual_search_next_nonblock(ctx, mail_r, tryagain_r);
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen return index_storage_search_next_nonblock(ctx, mail_r, tryagain_r);
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen /* the messages won't be returned sorted, so we'll have to
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen do it ourself */
eef4ba0cc3e78f8c26804c1c9251a76580a41f0cTimo Sirainen while (index_storage_search_next_nonblock(ctx, mail_r, tryagain_r))
86bde2c1838d1ce967fa2b394bb952004a4adcb7Timo Sirainen seq_range_array_add(&vctx->result, (*mail_r)->seq);
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen vctx->search_state = VIRTUAL_SEARCH_STATE_SORT_DONE;
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen /* fall through */
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen if (!seq_range_array_iter_nth(&vctx->result_iter,
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenstatic void search_args_set_full_match(struct mail_search_arg *args)
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenbool virtual_search_next_update_seq(struct mail_search_context *ctx)
109c39ab66c5aa85bf37d5bbf4ce91c6f966268bAki Tuomi struct virtual_search_context *vctx = VIRTUAL_CONTEXT_REQUIRE(ctx);
f4616f1875297fb2f583d913c0f01b075bdecd5bTimo Sirainen /* go through potential results first */
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen ctx->seq = recs[vctx->next_record_idx++].virtual_seq - 1;
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen if (!index_storage_search_next_update_seq(ctx))
f4616f1875297fb2f583d913c0f01b075bdecd5bTimo Sirainen /* this is known to match fully */