bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2006-2018 Dovecot authors, see the included COPYING file */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenuid_range_to_seqs(struct fts_search_context *fctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen p_array_init(seq_range, fctx->result_pool, count);
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen for (i = 0; i < count; i++) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen mailbox_get_seq_range(fctx->box, range[i].seq1, range[i].seq2,
c8b5a21a139992e66b4ad02adb69eaf929b3d024Timo Sirainen seq_range_array_add_range(seq_range, seq1, seq2);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenstatic int fts_search_lookup_level_single(struct fts_search_context *fctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen p_array_init(&result.definite_uids, fctx->result_pool, 32);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen p_array_init(&result.maybe_uids, fctx->result_pool, 32);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen p_array_init(&result.scores, fctx->result_pool, 32);
117fb8c00336dc54bab9cfa547249df7a4970611Timo Sirainen if (fts_backend_lookup(fctx->backend, fctx->box, args, flags,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen level->args_matches = buffer_create_dynamic(fctx->result_pool, 16);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen fts_search_serialize(level->args_matches, args);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen uid_range_to_seqs(fctx, &result.definite_uids, &level->definite_seqs);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen uid_range_to_seqs(fctx, &result.maybe_uids, &level->maybe_seqs);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct fts_search_level *level, struct fts_result *br)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen for (i = 0; i < count; i++)
86bde2c1838d1ce967fa2b394bb952004a4adcb7Timo Sirainen seq_range_array_add(&backend_uids, scores[i].uid);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen box->virtual_vfuncs->get_virtual_uid_map(box, br->box,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen i_assert(array_count(&vuids_arr) == array_count(&br->scores));
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen for (i = 0; i < count; i++) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen score = array_append_space(&level->score_map);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenmailbox_cmp_fts_backend(struct mailbox *const *m1, struct mailbox *const *m2)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenmulti_add_lookup_result(struct fts_search_context *fctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen unsigned int i;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen fts_search_serialize(level->args_matches, args);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (level->args_matches->used != orig_size * 2 ||
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen i_panic("incompatible fts backends for namespaces");
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen buffer_set_used_size(level->args_matches, orig_size);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen for (i = 0; result->box_results[i].box != NULL; i++) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct fts_result *br = &result->box_results[i];
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen fctx->box->virtual_vfuncs->get_virtual_uids(fctx->box,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen uid_range_to_seqs(fctx, &vuids, &level->definite_seqs);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen fctx->box->virtual_vfuncs->get_virtual_uids(fctx->box,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen uid_range_to_seqs(fctx, &vuids, &level->maybe_seqs);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenstatic int fts_search_lookup_level_multi(struct fts_search_context *fctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen ARRAY_TYPE(mailboxes) mailboxes_arr, tmp_mailboxes;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen p_array_init(&mailboxes_arr, fctx->result_pool, 8);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen fctx->box->virtual_vfuncs->get_virtual_backend_boxes(fctx->box,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen array_sort(&mailboxes_arr, mailbox_cmp_fts_backend);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen level->args_matches = buffer_create_dynamic(fctx->result_pool, 16);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen p_array_init(&level->score_map, fctx->result_pool, 1);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen mailboxes = array_get(&mailboxes_arr, &mailbox_count);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen for (i = 0; i < mailbox_count; i = j) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen array_append(&tmp_mailboxes, &mailboxes[i], 1);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (fts_mailbox_backend(mailboxes[j]) != backend)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen array_append(&tmp_mailboxes, &mailboxes[j], 1);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (multi_add_lookup_result(fctx, level, args, &result) < 0)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenstatic int fts_search_lookup_level(struct fts_search_context *fctx,
d05e9a7a1c48d6a20e48c034be1f50f59becb377Timo Sirainen fts_search_lookup_level_single(fctx, args, and_args) :
d05e9a7a1c48d6a20e48c034be1f50f59becb377Timo Sirainen fts_search_lookup_level_multi(fctx, args, and_args);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (args->type != SEARCH_OR && args->type != SEARCH_SUB)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (fts_search_lookup_level(fctx, args->value.subargs,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenfts_search_merge_scores_and(ARRAY_TYPE(fts_score_map) *dest,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen unsigned int desti, srci, dest_count, src_count;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen dest_map = array_get_modifiable(dest, &dest_count);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* arg_scores are summed to current scores. we could drop UIDs that
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen don't exist in both, but that's just extra work so don't bother */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen for (desti = srci = 0; desti < dest_count && srci < src_count;) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen else if (dest_map[desti].uid > src_map[srci].uid)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (dest_map[desti].score < src_map[srci].score)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenfts_search_merge_scores_or(ARRAY_TYPE(fts_score_map) *dest,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen const struct fts_score_map *src_map, *src2_map;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen unsigned int srci, src2i, src_count, src2_count;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* add any missing UIDs to current scores. if any existing UIDs have
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen lower scores than in arg_scores, increase them. */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen for (srci = src2i = 0; srci < src_count || src2i < src2_count;) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen i_assert(src_map[srci].uid == src2_map[src2i].uid);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (src_map[srci].score > src2_map[src2i].score)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenfts_search_merge_scores_level(struct fts_search_context *fctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct mail_search_arg *args, unsigned int *idx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen bool and_args, ARRAY_TYPE(fts_score_map) *scores)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen The (simplified) args can look like:
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen A and B and (C or D) and (E or F) and ...
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen A or B or (C and D) or (E and F) or ...
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen The A op B part's scores are in level->scores. The child args'
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen scores are in the sub levels' scores.
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen array_append_array(scores, &level->score_map);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (args->type != SEARCH_OR && args->type != SEARCH_SUB)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen fts_search_merge_scores_level(fctx, args->value.subargs, idx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen fts_search_merge_scores_and(scores, &arg_scores);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen fts_search_merge_scores_or(scores, &arg_scores);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenstatic void fts_search_merge_scores(struct fts_search_context *fctx)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen fts_search_merge_scores_level(fctx, fctx->args->args, &idx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenvoid fts_search_lookup(struct fts_search_context *fctx)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (fts_backend_get_last_uid(fctx->backend, fctx->box, &last_uid) < 0)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen mailbox_get_seq_range(fctx->box, last_uid+1, (uint32_t)-1,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen fctx->first_unindexed_seq = seq1 != 0 ? seq1 : (uint32_t)-1;
fe3f637ef0b9544a567b54f3743cf8e3649d8d0fTimo Sirainen if ((fctx->backend->flags & FTS_BACKEND_FLAG_TOKENIZED_INPUT) != 0) {
fe3f637ef0b9544a567b54f3743cf8e3649d8d0fTimo Sirainen if (fts_search_args_expand(fctx->backend, fctx->args) < 0)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen fts_search_serialize(fctx->orig_matches, fctx->args->args);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (fts_search_lookup_level(fctx, fctx->args->args, TRUE) == 0) {