fts-api.h revision 12055678401e913f4be130fa41b22fbeb626cc7e
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmody#ifndef FTS_API_H
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#define FTS_API_H
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainenstruct mail;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstruct mailbox;
3c493c276f599d9b9cd10764876d648003046954Timo Sirainenstruct fts_backend_build_context;
3c493c276f599d9b9cd10764876d648003046954Timo Sirainen
c19f0dd4d274c15ef7b592dcc9d8c2c8c1e22abeTimo Sirainen#include "seq-range-array.h"
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
b13f738e8eb3f24dc2abf2c804f954b4d864ac6fTimo Sirainenenum fts_lookup_flags {
61b0637759146621cbb7edcbd0b03a71cfd66dfeTimo Sirainen /* Search within header and/or body.
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen At least one of these must be set. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen FTS_LOOKUP_FLAG_HEADER = 0x01,
b039dabf4c53f72454e795930e7643b6e0e625f9Timo Sirainen FTS_LOOKUP_FLAG_BODY = 0x02,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen /* The key must NOT be found */
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen FTS_LOOKUP_FLAG_INVERT = 0x04
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen};
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstruct fts_backend_uid_map {
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen const char *mailbox;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen uint32_t uidvalidity;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen uint32_t uid;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen};
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo SirainenARRAY_DEFINE_TYPE(fts_backend_uid_map, struct fts_backend_uid_map);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstruct fts_score_map {
14e581093719c4353205bf5ba4743d8575c49d1bTimo Sirainen uint32_t uid;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen float score;
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen};
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo SirainenARRAY_DEFINE_TYPE(fts_score_map, struct fts_score_map);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenstruct fts_backend *
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainenfts_backend_init(const char *backend_name, struct mailbox *box);
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainenvoid fts_backend_deinit(struct fts_backend **backend);
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen/* Get the last_uid for the mailbox. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenint fts_backend_get_last_uid(struct fts_backend *backend, uint32_t *last_uid_r);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen/* Get last_uids for all mailboxes that might be backend mailboxes for a
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen virtual mailbox. The backend can use mailbox_get_virtual_backend_boxes() or
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen mailbox_get_virtual_box_patterns() functions to get the list of mailboxes.
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen Depending on virtual mailbox configuration, this function may also return
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen mailboxes that don't even match the virtual mailbox patterns. The caller
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen needs to be able to ignore the unnecessary ones. */
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainenint fts_backend_get_all_last_uids(struct fts_backend *backend, pool_t pool,
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen ARRAY_TYPE(fts_backend_uid_map) *last_uids);
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen
441f0e0d84082eb498e620327ebf4de509052d15Timo Sirainen/* Initialize adding new data to the index. last_uid_r is set to the last
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen indexed message's IMAP UID */
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainenint fts_backend_build_init(struct fts_backend *backend, uint32_t *last_uid_r,
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen struct fts_backend_build_context **ctx_r);
c040ee67d0ac0fb7375bb543965bf67dcae6affaTimo Sirainen/* Switch to building index for mail's headers or MIME part headers. */
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainenvoid fts_backend_build_hdr(struct fts_backend_build_context *ctx, uint32_t uid);
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen/* Switch to building index for the next body part. If backend doesn't want
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen to index this body part (based on content type/disposition check), it can
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen return FALSE and caller will skip to next part. The backend must return
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen TRUE for all text/xxx and message/rfc822 content types.
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen The content_type contains a valid parsed "type/subtype" string. For messages
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen without (valid) Content-Type header, the content_type is set to "text/plain".
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen The content_disposition is passed without parsing/validation if it exists,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen otherwise it's NULL. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenbool fts_backend_build_body_begin(struct fts_backend_build_context *ctx,
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen uint32_t uid, const char *content_type,
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen const char *content_disposition);
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen/* Called once when the whole body part has been sent. */
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainenvoid fts_backend_build_body_end(struct fts_backend_build_context *ctx);
e12d2886114e48ccd871ae73fd0a962375096a8aTimo Sirainen/* Add more content to the index for the currently selected header/body part.
e12d2886114e48ccd871ae73fd0a962375096a8aTimo Sirainen The data must contain only full valid UTF-8 characters, but it doesn't need
e12d2886114e48ccd871ae73fd0a962375096a8aTimo Sirainen to be NUL-terminated. size contains the data size in bytes, not characters.
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen This function may be called many times and the data block sizes may be
e12d2886114e48ccd871ae73fd0a962375096a8aTimo Sirainen small. Backend returns 0 if ok, -1 if build should be aborted. */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenint fts_backend_build_more(struct fts_backend_build_context *ctx,
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen const unsigned char *data, size_t size);
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen/* Finish adding new data to the index. */
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainenint fts_backend_build_deinit(struct fts_backend_build_context **ctx);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen/* Returns TRUE if there exists a build context. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenbool fts_backend_is_building(struct fts_backend *backend);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen/* Expunge given mail from the backend. Note that the transaction may still
96f2533c48ce5def0004931606a2fdf275578880Timo Sirainen fail later, so backend shouldn't do anything irreversible. */
83c21c990eb2a370f0da56240e73dac846f4acc3Timo Sirainenvoid fts_backend_expunge(struct fts_backend *backend, struct mail *mail);
83c21c990eb2a370f0da56240e73dac846f4acc3Timo Sirainen/* Called after transaction has been committed or rollbacked. */
83c21c990eb2a370f0da56240e73dac846f4acc3Timo Sirainenvoid fts_backend_expunge_finish(struct fts_backend *backend,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen struct mailbox *box, bool committed);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen/* Lock/unlock the backend for multiple lookups. Returns 1 if locked, 0 if
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen locking timeouted, -1 if error. If backend doesn't require locking, it
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen always returns 1.
63ca9cacc5d2d1b1cebfc430bb89637f8c138e4cTimo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen It's not required to call these functions manually, but if you're doing
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen multiple lookup/filter operations this avoids multiple lock/unlock calls. */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenint fts_backend_lock(struct fts_backend *backend);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenvoid fts_backend_unlock(struct fts_backend *backend);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen/* Start building a FTS lookup. */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenstruct fts_backend_lookup_context *
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenfts_backend_lookup_init(struct fts_backend *backend);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen/* Add a new search key to the lookup. The keys are ANDed together. */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainenvoid fts_backend_lookup_add(struct fts_backend_lookup_context *ctx,
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen const char *key, enum fts_lookup_flags flags);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen/* Finish the lookup and return found UIDs. The definite_uids are returned
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen to client directly, while for maybe_uids Dovecot first verifies (by
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen opening and reading the mail) that they really do contain the searched
3c493c276f599d9b9cd10764876d648003046954Timo Sirainen keys. The maybe_uids is useful with backends that can only filter out
3c493c276f599d9b9cd10764876d648003046954Timo Sirainen messages, but can't definitively say if the search matched a message. */
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainenint fts_backend_lookup_deinit(struct fts_backend_lookup_context **ctx,
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ARRAY_TYPE(seq_range) *definite_uids,
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen ARRAY_TYPE(seq_range) *maybe_uids,
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen ARRAY_TYPE(fts_score_map) *scores);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen#endif
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen