c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef FTS_API_PRIVATE_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define FTS_API_PRIVATE_H
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen#include "unichar.h"
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen#include "fts-api.h"
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen
e9257b4538b9f464693c5d5e548486f6d292301cTimo Sirainenstruct mail_user;
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainenstruct mailbox_list;
e9257b4538b9f464693c5d5e548486f6d292301cTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#define MAILBOX_GUID_HEX_LENGTH (GUID_128_SIZE*2)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstruct fts_backend_vfuncs {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct fts_backend *(*alloc)(void);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen int (*init)(struct fts_backend *backend, const char **error_r);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen void (*deinit)(struct fts_backend *backend);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen int (*get_last_uid)(struct fts_backend *backend, struct mailbox *box,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen uint32_t *last_uid_r);
fda168427e1950518acd6d600f1a10a29a5baef0Timo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct fts_backend_update_context *
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen (*update_init)(struct fts_backend *backend);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen int (*update_deinit)(struct fts_backend_update_context *ctx);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen void (*update_set_mailbox)(struct fts_backend_update_context *ctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct mailbox *box);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen void (*update_expunge)(struct fts_backend_update_context *ctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen uint32_t uid);
3d4450c252790b03bb5ce054987ac91110f1ff62Timo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* Start a build for specified key */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen bool (*update_set_build_key)(struct fts_backend_update_context *ctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen const struct fts_backend_build_key *key);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* Finish a build for specified key - guaranteed to be called */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen void (*update_unset_build_key)(struct fts_backend_update_context *ctx);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen /* Add data for current build key */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen int (*update_build_more)(struct fts_backend_update_context *ctx,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen const unsigned char *data, size_t size);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen int (*refresh)(struct fts_backend *backend);
a569c6df4ffe05acc242e269d926449e50702d59Timo Sirainen int (*rescan)(struct fts_backend *backend);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen int (*optimize)(struct fts_backend *backend);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen bool (*can_lookup)(struct fts_backend *backend,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen const struct mail_search_arg *args);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen int (*lookup)(struct fts_backend *backend, struct mailbox *box,
117fb8c00336dc54bab9cfa547249df7a4970611Timo Sirainen struct mail_search_arg *args, enum fts_lookup_flags flags,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct fts_result *result);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen int (*lookup_multi)(struct fts_backend *backend,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct mailbox *const boxes[],
117fb8c00336dc54bab9cfa547249df7a4970611Timo Sirainen struct mail_search_arg *args,
117fb8c00336dc54bab9cfa547249df7a4970611Timo Sirainen enum fts_lookup_flags flags,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct fts_multi_result *result);
b7fdf4fe23801de680e0be5aca0596a3c9ea3f8fTimo Sirainen void (*lookup_done)(struct fts_backend *backend);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen};
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen
eb0ede66120bb63c0212bad69e67efca1eb47324Timo Sirainenenum fts_backend_flags {
2116e38edb504e72eadaf5a51c3dd9b1a81039eaTimo Sirainen /* Backend supports indexing binary MIME parts */
0fbea4dd37f44d2fcfcd4a36a36a6e545696f0e6Timo Sirainen FTS_BACKEND_FLAG_BINARY_MIME_PARTS = 0x01,
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen /* Send built text to backend normalized rather than
0fbea4dd37f44d2fcfcd4a36a36a6e545696f0e6Timo Sirainen preserving original case */
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen FTS_BACKEND_FLAG_NORMALIZE_INPUT = 0x02,
84f447a23beceb6ab5898083052e1b5bae51f033Timo Sirainen /* Send only fully indexable words rather than randomly sized blocks */
e51bfe0db17f1a2e44932d0d6d3958ae409a8bcfTimo Sirainen FTS_BACKEND_FLAG_BUILD_FULL_WORDS = 0x04,
e51bfe0db17f1a2e44932d0d6d3958ae409a8bcfTimo Sirainen /* Fuzzy search works */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen FTS_BACKEND_FLAG_FUZZY_SEARCH = 0x08,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* Tokenize all the input. update_build_more() will be called a single
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen directly indexable token at a time. Searching will modify the search
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen args so that lookup() sees only tokens that can be directly
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen searched. */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen FTS_BACKEND_FLAG_TOKENIZED_INPUT = 0x10
eb0ede66120bb63c0212bad69e67efca1eb47324Timo Sirainen};
eb0ede66120bb63c0212bad69e67efca1eb47324Timo Sirainen
eb0ede66120bb63c0212bad69e67efca1eb47324Timo Sirainenstruct fts_backend {
eb0ede66120bb63c0212bad69e67efca1eb47324Timo Sirainen const char *name;
eb0ede66120bb63c0212bad69e67efca1eb47324Timo Sirainen enum fts_backend_flags flags;
94f90df2cfb7587bb5af432b2ba065d1c364e1f7Timo Sirainen
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen struct fts_backend_vfuncs v;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct mail_namespace *ns;
bc93929cdd9000ca560a5f42a27f50ab307f1efbTimo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool updating:1;
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen};
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenstruct fts_backend_update_context {
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen struct fts_backend *backend;
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen normalizer_func_t *normalizer;
94f90df2cfb7587bb5af432b2ba065d1c364e1f7Timo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct mailbox *cur_box, *backend_box;
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool build_key_open:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool failed:1;
809923ba26218971792182ae8894936f9ac99364Timo Sirainen};
809923ba26218971792182ae8894936f9ac99364Timo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenstruct fts_index_header {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen uint32_t last_indexed_uid;
809923ba26218971792182ae8894936f9ac99364Timo Sirainen
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainen /* Checksum of settings. If the settings change, the index should
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainen be rebuilt. */
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainen uint32_t settings_checksum;
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainen uint32_t unused;
809923ba26218971792182ae8894936f9ac99364Timo Sirainen};
809923ba26218971792182ae8894936f9ac99364Timo Sirainen
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenvoid fts_backend_register(const struct fts_backend *backend);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenvoid fts_backend_unregister(const char *name);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenbool fts_backend_default_can_lookup(struct fts_backend *backend,
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen const struct mail_search_arg *args);
12055678401e913f4be130fa41b22fbeb626cc7eTimo Sirainen
809923ba26218971792182ae8894936f9ac99364Timo Sirainenvoid fts_filter_uids(ARRAY_TYPE(seq_range) *definite_dest,
809923ba26218971792182ae8894936f9ac99364Timo Sirainen const ARRAY_TYPE(seq_range) *definite_filter,
809923ba26218971792182ae8894936f9ac99364Timo Sirainen ARRAY_TYPE(seq_range) *maybe_dest,
809923ba26218971792182ae8894936f9ac99364Timo Sirainen const ARRAY_TYPE(seq_range) *maybe_filter);
809923ba26218971792182ae8894936f9ac99364Timo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen/* Returns TRUE if ok, FALSE if no fts header */
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainenbool fts_index_get_header(struct mailbox *box, struct fts_index_header *hdr_r);
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainenint fts_index_set_header(struct mailbox *box,
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainen const struct fts_index_header *hdr);
b66d803de86bfb411165b3465b0d9ef64ecfe2a1Timo Sirainenint ATTR_NOWARN_UNUSED_RESULT
b66d803de86bfb411165b3465b0d9ef64ecfe2a1Timo Sirainenfts_index_set_last_uid(struct mailbox *box, uint32_t last_uid);
263d455e9198e8aa45c7a99601ad6a5a9dcdd915Timo Sirainenint fts_backend_reset_last_uids(struct fts_backend *backend);
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainenint fts_index_have_compatible_settings(struct mailbox_list *list,
54d437dea655b338e45a8917880f3d3a2a7b698dTimo Sirainen uint32_t checksum);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen/* Returns TRUE if FTS backend should index the header for optimizing
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen separate lookups */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenbool fts_header_want_indexed(const char *hdr_name);
4ac43ad128cb9b22f1c936659a1ff62e5b6c8bc3Timo Sirainen/* Returns TRUE if header's values should be considered to have a language. */
4ac43ad128cb9b22f1c936659a1ff62e5b6c8bc3Timo Sirainenbool fts_header_has_language(const char *hdr_name);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenint fts_mailbox_get_guid(struct mailbox *box, const char **guid_r);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen#endif