fts-search.c revision c8b5a21a139992e66b4ad02adb69eaf929b3d024
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (c) 2006-2008 Dovecot authors, see the included COPYING file */
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainenuid_range_to_seqs(struct mailbox *box, const ARRAY_TYPE(seq_range) *uid_range,
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen unsigned int i, count;
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen for (i = 0; i < count; i++) {
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen mailbox_get_uids(box, range[i].seq1, range[i].seq2,
c8b5a21a139992e66b4ad02adb69eaf929b3d024Timo Sirainen seq_range_array_add_range(seq_range, seq1, seq2);
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainenstatic void fts_uid_results_to_seq(struct fts_search_context *fctx)
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen uid_range_to_seqs(fctx->t->box, &uid_range, &fctx->definite_seqs);
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen uid_range_to_seqs(fctx->t->box, &uid_range, &fctx->maybe_seqs);
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainenstatic int fts_search_lookup_arg(struct fts_search_context *fctx,
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen const char *key;
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen /* we can filter out messages that don't have the header,
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen but we can't trust definite results list. */
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen /* we're only checking the existence
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen of the header. */
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen fctx->fbox->backend_fast : fctx->fbox->backend_substr;
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen /* can't filter this */
f2f86ec77d1e4986e95990976447c2d1520a8357Timo Sirainen /* convert key to titlecase */
f2f86ec77d1e4986e95990976447c2d1520a8357Timo Sirainen if (charset_to_utf8_str(fctx->charset, CHARSET_FLAG_DECOMP_TITLECASE,
f2f86ec77d1e4986e95990976447c2d1520a8357Timo Sirainen /* unknown charset, can't handle this */
f2f86ec77d1e4986e95990976447c2d1520a8357Timo Sirainen /* let the core code handle this error */
f2f86ec77d1e4986e95990976447c2d1520a8357Timo Sirainen } else if (!backend->locked && fts_backend_lock(backend) <= 0)
f2f86ec77d1e4986e95990976447c2d1520a8357Timo Sirainen ret = fts_backend_lookup(backend, str_c(key_utf8), flags,
f2f86ec77d1e4986e95990976447c2d1520a8357Timo Sirainen ret = fts_backend_filter(backend, str_c(key_utf8), flags,
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainenvoid fts_search_lookup(struct fts_search_context *fctx)
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen /* start filtering with the best arg */
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen ret = fts_search_lookup_arg(fctx, fctx->best_arg, FALSE);
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen /* filter the rest */
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen for (arg = fctx->args; arg != NULL && ret == 0; arg = arg->next) {
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen fts_backend_unlock(fctx->fbox->backend_substr);
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainenstatic bool arg_is_better(const struct mail_search_arg *new_arg,
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen /* avoid NOTs */
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen /* prefer not to use headers. they have a larger possibility of
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen having lots of identical strings */
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen return strlen(new_arg->value.str) > strlen(old_arg->value.str);
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainenfts_search_args_find_best(struct mail_search_arg *args,
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainenvoid fts_search_analyze(struct fts_search_context *fctx)
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen struct mail_search_arg *best_fast_arg = NULL, *best_substr_arg = NULL;
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen fts_search_args_find_best(fctx->args, &best_fast_arg, &best_substr_arg);
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen if (best_fast_arg != NULL && fctx->fbox->backend_fast != NULL) {
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen /* use fast backend whenever possible */
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen fctx->build_backend = fctx->fbox->backend_fast;
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen } else if (best_fast_arg != NULL || best_substr_arg != NULL) {
67f1723e1685b4bf73c1cca0a1e08a0a87ffd410Timo Sirainen fctx->build_backend = fctx->fbox->backend_substr;