fts-backend-solr-old.c revision 683d9f20766cd13e72f7d16ce1e341a8811b2679
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen/* Copyright (c) 2006-2012 Dovecot authors, see the included COPYING file */
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen#include "lib.h"
dbb1fb1c51727e2050792f8c333b212e22a36d69Timo Sirainen#include "array.h"
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen#include "str.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "hash.h"
dbb1fb1c51727e2050792f8c333b212e22a36d69Timo Sirainen#include "strescape.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "unichar.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "imap-utf7.h"
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen#include "mail-storage-private.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "mailbox-list-private.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "mail-search.h"
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen#include "fts-api.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include "solr-connection.h"
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen#include "fts-solr-plugin.h"
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen#include <ctype.h>
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen#define SOLR_CMDBUF_SIZE (1024*64)
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen#define SOLR_MAX_MULTI_ROWS 100000
bb6a0eeab27569790d58a036f67dcd2a965fc539Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstruct solr_fts_backend {
31be5ed1551c98cddeb2295a594f010aaf4b76bcTimo Sirainen struct fts_backend backend;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen char *id_username, *id_namespace;
31be5ed1551c98cddeb2295a594f010aaf4b76bcTimo Sirainen struct mail_namespace *default_ns;
31be5ed1551c98cddeb2295a594f010aaf4b76bcTimo Sirainen};
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainenstruct solr_fts_backend_update_context {
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen struct fts_backend_update_context ctx;
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct mailbox *cur_box;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen char *id_box_name;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen struct solr_connection_post *post;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen uint32_t prev_uid, uid_validity;
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen string_t *cmd, *hdr;
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen bool headers_open;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen bool body_open;
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen bool documents_added;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen};
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainenstatic bool is_valid_xml_char(unichar_t chr)
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen{
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen /* Valid characters in XML:
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen [#x10000-#x10FFFF]
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen This function gets called only for #x80 and higher */
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen if (chr > 0xd7ff && chr < 0xe000)
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen return FALSE;
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen if (chr > 0xfffd && chr < 0x10000)
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen return FALSE;
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen return chr < 0x10ffff;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
e192a3b1ca8ae857e7d87298ea507d32977ba570Timo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic void
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenxml_encode_data(string_t *dest, const unsigned char *data, unsigned int len)
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen{
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unichar_t chr;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen unsigned int i;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (i = 0; i < len; i++) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen switch (data[i]) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen case '&':
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen str_append(dest, "&amp;");
dbb1fb1c51727e2050792f8c333b212e22a36d69Timo Sirainen break;
dbb1fb1c51727e2050792f8c333b212e22a36d69Timo Sirainen case '<':
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen str_append(dest, "&lt;");
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen break;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen case '>':
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen str_append(dest, "&gt;");
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen break;
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen case '\t':
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen case '\n':
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen case '\r':
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen /* exceptions to the following control char check */
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen str_append_c(dest, data[i]);
33d63688ed8b26dc333e3c2edbfb2fe6e412604dTimo Sirainen break;
645f258ea29afaf09b673fc65d1bd788dfec8db8Timo Sirainen default:
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen if (data[i] < 32) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* SOLR doesn't like control characters.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen replace them with spaces. */
e667602217af55105d44d8d9b75f09a8a9ac2f14Timo Sirainen str_append_c(dest, ' ');
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen } else if (data[i] >= 0x80) {
6389aeec8c26b585e583c364b48ad12adf741898Timo Sirainen /* make sure the character is valid for XML
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen so we don't get XML parser errors */
96541d31299bb40b5a6efdbf9b4cb3d4f4b4a069Timo Sirainen unsigned int char_len =
96541d31299bb40b5a6efdbf9b4cb3d4f4b4a069Timo Sirainen uni_utf8_char_bytes(data[i]);
644268f7848a7c4221146d0b11feb8ed5bbed233Timo Sirainen if (i + char_len <= len &&
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen uni_utf8_get_char_n(data + i, char_len, &chr) == 1 &&
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen is_valid_xml_char(chr))
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen str_append_n(dest, data + i, char_len);
55773f17bccf6361d6599ffcbe072d7c9fe205bfTimo Sirainen else {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen str_append_n(dest, utf8_replacement_char,
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen UTF8_REPLACEMENT_CHAR_LEN);
bb6a0eeab27569790d58a036f67dcd2a965fc539Timo Sirainen }
a58e4aec412a30352d9d45e63726cac044aa6aa5Timo Sirainen i += char_len - 1;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen } else {
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen str_append_c(dest, data[i]);
1b4441e3e6f9e78ebeae8218de971959cd55bf60Timo Sirainen }
2ebeb22b9a8a8bb7fbe2f2e2908478a220792b87Timo Sirainen break;
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen }
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen }
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen}
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainenstatic void xml_encode(string_t *dest, const char *str)
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen{
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen xml_encode_data(dest, (const unsigned char *)str, strlen(str));
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
33d63688ed8b26dc333e3c2edbfb2fe6e412604dTimo Sirainenstatic const char *solr_escape_id_str(const char *str)
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen{
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen string_t *tmp;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen const char *p;
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen for (p = str; *p != '\0'; p++) {
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen if (*p == '/' || *p == '!')
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen break;
98dd8e6e81f11f1e6040ca72f4916242d246c863Timo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (*p == '\0')
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen return str;
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen tmp = t_str_new(64);
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen for (p = str; *p != '\0'; p++) {
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen switch (*p) {
c36ec256c1bd1abe1c12e792cf64f0b7e3b3135aTimo Sirainen case '/':
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen str_append(tmp, "!\\");
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen break;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen case '!':
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen str_append(tmp, "!!");
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen break;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen default:
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen str_append_c(tmp, *p);
a12399903f415a7e14c2816cffa2f7a09dcbb097Timo Sirainen break;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen }
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen }
77af0bd168cf3e3ddc3ae68abc82bfad7e9b5ff4Timo Sirainen return str_c(tmp);
6789ed17e7ca4021713507baf0dcf6979bb42e0cTimo Sirainen}
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen
d2b94d25f842cd1b7acaf4dd7de858f7c6a821c9Timo Sirainenstatic void solr_quote(string_t *dest, const char *str)
d2b94d25f842cd1b7acaf4dd7de858f7c6a821c9Timo Sirainen{
d2b94d25f842cd1b7acaf4dd7de858f7c6a821c9Timo Sirainen str_append_c(dest, '"');
2aecf7be5834e7f6520f8deaad683a6fa1de4d61Timo Sirainen str_append(dest, str_escape(str));
2aecf7be5834e7f6520f8deaad683a6fa1de4d61Timo Sirainen str_append_c(dest, '"');
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainenstatic void solr_quote_http(string_t *dest, const char *str)
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen{
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen str_append(dest, "%22");
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen solr_connection_http_escape(solr_conn, dest, str);
dce5a2719df4fc64a8762d2aa94ba98dcf9cd6feTimo Sirainen str_append(dest, "%22");
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen}
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainen
87460b08cb97b31cde640d4975a6aa2c1d0e7226Timo Sirainenstatic void fts_solr_set_default_ns(struct solr_fts_backend *backend)
1bdda5c0c30463160c47151537e6bb2c6c994841Timo Sirainen{
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen struct mail_namespace *ns = backend->backend.ns;
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen struct fts_solr_user *fuser = FTS_SOLR_USER_CONTEXT(ns->user);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct fts_solr_settings *set = &fuser->set;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen const char *str;
90adcaa0a00eba29b7fbd50ca66be11c8d086d6aTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (backend->default_ns != NULL)
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen return;
d67fde1a8ebc1d85704c5986d8f93aae97eccef3Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (set->default_ns_prefix != NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen backend->default_ns =
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_namespace_find_prefix(ns->user->namespaces,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen set->default_ns_prefix);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (backend->default_ns == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_error("fts_solr: default_ns setting points to "
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen "nonexistent namespace");
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen }
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen if (backend->default_ns == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen backend->default_ns =
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_namespace_find_inbox(ns->user->namespaces);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen while (backend->default_ns->alias_for != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen backend->default_ns = backend->default_ns->alias_for;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
5cda0bfea032000c4a51134c748d9efe6614870bTimo Sirainen if (ns != backend->default_ns) {
97511ac4d7607e1ba64ce151eda3d9b5f9775519Timo Sirainen str = solr_escape_id_str(ns->prefix);
5cda0bfea032000c4a51134c748d9efe6614870bTimo Sirainen backend->id_namespace = i_strdup(str);
5cda0bfea032000c4a51134c748d9efe6614870bTimo Sirainen }
97511ac4d7607e1ba64ce151eda3d9b5f9775519Timo Sirainen}
e54512a5189192fe72d1e2c53927c98c5ac920b4Timo Sirainen
e54512a5189192fe72d1e2c53927c98c5ac920b4Timo Sirainenstatic void fts_box_name_get_root(struct mail_namespace **ns, const char **name)
2ebeb22b9a8a8bb7fbe2f2e2908478a220792b87Timo Sirainen{
2ebeb22b9a8a8bb7fbe2f2e2908478a220792b87Timo Sirainen struct mail_namespace *orig_ns = *ns;
2ebeb22b9a8a8bb7fbe2f2e2908478a220792b87Timo Sirainen
e8a59a1671127f87e2d22f42e84c572f28299d81Timo Sirainen while ((*ns)->alias_for != NULL)
*ns = (*ns)->alias_for;
if (**name == '\0' && *ns != orig_ns &&
((*ns)->flags & NAMESPACE_FLAG_INBOX_USER) != 0) {
/* ugly workaround to allow selecting INBOX from a Maildir/
when it's not in the inbox=yes namespace. */
*name = "INBOX";
}
}
static const char *
fts_box_get_root(struct mailbox *box, struct mail_namespace **ns_r)
{
struct mail_namespace *ns = mailbox_get_namespace(box);
const char *name;
if (t_imap_utf8_to_utf7(box->name, &name) < 0)
i_unreached();
fts_box_name_get_root(&ns, &name);
*ns_r = ns;
return name;
}
static struct fts_backend *fts_backend_solr_alloc(void)
{
struct solr_fts_backend *backend;
backend = i_new(struct solr_fts_backend, 1);
backend->backend = fts_backend_solr_old;
return &backend->backend;
}
static int
fts_backend_solr_init(struct fts_backend *_backend,
const char **error_r ATTR_UNUSED)
{
struct solr_fts_backend *backend = (struct solr_fts_backend *)_backend;
struct fts_solr_user *fuser = FTS_SOLR_USER_CONTEXT(_backend->ns->user);
const struct fts_solr_settings *set = &fuser->set;
const char *str;
if (solr_conn == NULL)
solr_conn = solr_connection_init(set->url, set->debug);
str = solr_escape_id_str(_backend->ns->user->username);
backend->id_username = i_strdup(str);
return 0;
}
static void fts_backend_solr_deinit(struct fts_backend *_backend)
{
struct solr_fts_backend *backend = (struct solr_fts_backend *)_backend;
i_free(backend->id_namespace);
i_free(backend->id_username);
i_free(backend);
}
static void
solr_add_ns_query(string_t *str, struct solr_fts_backend *backend,
struct mail_namespace *ns, bool neg)
{
while (ns->alias_for != NULL)
ns = ns->alias_for;
if (ns == backend->default_ns || *ns->prefix == '\0') {
if (!neg)
str_append(str, " -ns:[* TO *]");
else
str_append(str, " +ns:[* TO *]");
} else {
if (!neg)
str_append(str, " +ns:");
else
str_append(str, " -ns:");
solr_quote(str, ns->prefix);
}
}
static void
solr_add_ns_query_http(string_t *str, struct solr_fts_backend *backend,
struct mail_namespace *ns)
{
string_t *tmp;
tmp = t_str_new(64);
solr_add_ns_query(tmp, backend, ns, FALSE);
solr_connection_http_escape(solr_conn, str, str_c(tmp));
}
static int
fts_backend_solr_get_last_uid_fallback(struct solr_fts_backend *backend,
struct mailbox *box,
uint32_t *last_uid_r)
{
struct mail_namespace *ns;
struct mailbox_status status;
struct solr_result **results;
const struct seq_range *uidvals;
const char *box_name;
unsigned int count;
string_t *str;
pool_t pool;
int ret = 0;
str = t_str_new(256);
str_append(str, "fl=uid&rows=1&sort=uid+desc&q=");
box_name = fts_box_get_root(box, &ns);
mailbox_get_open_status(box, STATUS_UIDVALIDITY, &status);
str_printfa(str, "uidv:%u+box:", status.uidvalidity);
solr_quote_http(str, box_name);
solr_add_ns_query_http(str, backend, ns);
str_append(str, "+user:");
solr_quote_http(str, ns->user->username);
pool = pool_alloconly_create("solr last uid lookup", 1024);
if (solr_connection_select(solr_conn, str_c(str),
pool, &results) < 0)
ret = -1;
else if (results[0] == NULL) {
/* no UIDs */
*last_uid_r = 0;
} else {
uidvals = array_get(&results[0]->uids, &count);
i_assert(count > 0);
if (count == 1 && uidvals[0].seq1 == uidvals[0].seq2) {
*last_uid_r = uidvals[0].seq1;
} else {
i_error("fts_solr: Last UID lookup returned multiple rows");
ret = -1;
}
}
pool_unref(&pool);
return ret;
}
static int
fts_backend_solr_get_last_uid(struct fts_backend *_backend,
struct mailbox *box, uint32_t *last_uid_r)
{
struct solr_fts_backend *backend =
(struct solr_fts_backend *)_backend;
struct fts_index_header hdr;
if (fts_index_get_header(box, &hdr)) {
*last_uid_r = hdr.last_indexed_uid;
return 0;
}
/* either nothing has been indexed, or the index was corrupted.
do it the slow way. */
if (fts_backend_solr_get_last_uid_fallback(backend, box, last_uid_r) < 0)
return -1;
fts_index_set_last_uid(box, *last_uid_r);
return 0;
}
static struct fts_backend_update_context *
fts_backend_solr_update_init(struct fts_backend *_backend)
{
struct solr_fts_backend *backend =
(struct solr_fts_backend *)_backend;
struct solr_fts_backend_update_context *ctx;
ctx = i_new(struct solr_fts_backend_update_context, 1);
ctx->ctx.backend = _backend;
ctx->cmd = str_new(default_pool, SOLR_CMDBUF_SIZE);
ctx->hdr = str_new(default_pool, 4096);
fts_solr_set_default_ns(backend);
return &ctx->ctx;
}
static void xml_encode_id(struct solr_fts_backend_update_context *ctx,
string_t *str, uint32_t uid)
{
struct solr_fts_backend *backend =
(struct solr_fts_backend *)ctx->ctx.backend;
if (uid != 0)
str_printfa(str, "%u/", uid);
else
str_append(str, "L/");
if (backend->id_namespace != NULL) {
xml_encode(str, backend->id_namespace);
str_append_c(str, '/');
}
str_printfa(str, "%u/", ctx->uid_validity);
xml_encode(str, backend->id_username);
str_append_c(str, '/');
xml_encode(str, ctx->id_box_name);
}
static void
fts_backend_solr_add_doc_prefix(struct solr_fts_backend_update_context *ctx,
uint32_t uid)
{
struct solr_fts_backend *backend =
(struct solr_fts_backend *)ctx->ctx.backend;
struct mailbox *box = ctx->cur_box;
struct mail_namespace *ns;
const char *box_name;
ctx->documents_added = TRUE;
str_printfa(ctx->cmd, "<doc>"
"<field name=\"uid\">%u</field>"
"<field name=\"uidv\">%u</field>",
uid, ctx->uid_validity);
box_name = fts_box_get_root(box, &ns);
if (ns != backend->default_ns) {
str_append(ctx->cmd, "<field name=\"ns\">");
xml_encode(ctx->cmd, ns->prefix);
str_append(ctx->cmd, "</field>");
}
str_append(ctx->cmd, "<field name=\"box\">");
xml_encode(ctx->cmd, box_name);
str_append(ctx->cmd, "</field><field name=\"user\">");
xml_encode(ctx->cmd, ns->user->username);
str_append(ctx->cmd, "</field>");
}
static int
fts_backed_solr_build_commit(struct solr_fts_backend_update_context *ctx)
{
if (ctx->post == NULL)
return 0;
str_append(ctx->cmd, "</doc></add>");
solr_connection_post_more(ctx->post, str_data(ctx->cmd),
str_len(ctx->cmd));
return solr_connection_post_end(ctx->post);
}
static int
fts_backend_solr_update_deinit(struct fts_backend_update_context *_ctx)
{
struct solr_fts_backend_update_context *ctx =
(struct solr_fts_backend_update_context *)_ctx;
const char *str;
int ret;
ret = fts_backed_solr_build_commit(ctx);
/* commit and wait until the documents we just indexed are
visible to the following search */
str = t_strdup_printf("<commit waitFlush=\"false\" "
"waitSearcher=\"%s\"/>",
ctx->documents_added ? "true" : "false");
if (solr_connection_post(solr_conn, str) < 0)
ret = -1;
str_free(&ctx->cmd);
str_free(&ctx->hdr);
i_free(ctx->id_box_name);
i_free(ctx);
return ret;
}
static void
fts_backend_solr_update_set_mailbox(struct fts_backend_update_context *_ctx,
struct mailbox *box)
{
struct solr_fts_backend_update_context *ctx =
(struct solr_fts_backend_update_context *)_ctx;
struct mailbox_status status;
struct mail_namespace *ns;
if (ctx->prev_uid != 0) {
fts_index_set_last_uid(ctx->cur_box, ctx->prev_uid);
ctx->prev_uid = 0;
}
ctx->cur_box = box;
ctx->uid_validity = 0;
i_free_and_null(ctx->id_box_name);
if (box != NULL) {
ctx->id_box_name = i_strdup(fts_box_get_root(box, &ns));
mailbox_get_open_status(box, STATUS_UIDVALIDITY, &status);
ctx->uid_validity = status.uidvalidity;
}
}
static void
fts_backend_solr_update_expunge(struct fts_backend_update_context *_ctx,
uint32_t uid)
{
struct solr_fts_backend_update_context *ctx =
(struct solr_fts_backend_update_context *)_ctx;
T_BEGIN {
string_t *cmd;
cmd = t_str_new(256);
str_append(cmd, "<delete><id>");
xml_encode_id(ctx, cmd, uid);
str_append(cmd, "</id></delete>");
(void)solr_connection_post(solr_conn, str_c(cmd));
} T_END;
}
static void
fts_backend_solr_uid_changed(struct solr_fts_backend_update_context *ctx,
uint32_t uid)
{
if (ctx->post == NULL) {
i_assert(ctx->prev_uid == 0);
ctx->post = solr_connection_post_begin(solr_conn);
str_append(ctx->cmd, "<add>");
} else {
ctx->headers_open = FALSE;
if (ctx->body_open) {
ctx->body_open = FALSE;
str_append(ctx->cmd, "</field>");
}
str_append(ctx->cmd, "<field name=\"hdr\">");
str_append_str(ctx->cmd, ctx->hdr);
str_append(ctx->cmd, "</field>");
str_truncate(ctx->hdr, 0);
str_append(ctx->cmd, "</doc>");
}
ctx->prev_uid = uid;
fts_backend_solr_add_doc_prefix(ctx, uid);
str_printfa(ctx->cmd, "<field name=\"id\">");
xml_encode_id(ctx, ctx->cmd, uid);
str_append(ctx->cmd, "</field>");
}
static bool
fts_backend_solr_update_set_build_key(struct fts_backend_update_context *_ctx,
const struct fts_backend_build_key *key)
{
struct solr_fts_backend_update_context *ctx =
(struct solr_fts_backend_update_context *)_ctx;
if (key->uid != ctx->prev_uid)
fts_backend_solr_uid_changed(ctx, key->uid);
switch (key->type) {
case FTS_BACKEND_BUILD_KEY_HDR:
case FTS_BACKEND_BUILD_KEY_MIME_HDR:
xml_encode(ctx->hdr, key->hdr_name);
str_append(ctx->hdr, ": ");
ctx->headers_open = TRUE;
break;
case FTS_BACKEND_BUILD_KEY_BODY_PART:
ctx->headers_open = FALSE;
if (!ctx->body_open) {
ctx->body_open = TRUE;
str_append(ctx->cmd, "<field name=\"body\">");
}
break;
case FTS_BACKEND_BUILD_KEY_BODY_PART_BINARY:
i_unreached();
}
return TRUE;
}
static void
fts_backend_solr_update_unset_build_key(struct fts_backend_update_context *_ctx)
{
struct solr_fts_backend_update_context *ctx =
(struct solr_fts_backend_update_context *)_ctx;
if (ctx->headers_open)
str_append_c(ctx->hdr, '\n');
else {
i_assert(ctx->body_open);
str_append_c(ctx->cmd, '\n');
}
}
static int
fts_backend_solr_update_build_more(struct fts_backend_update_context *_ctx,
const unsigned char *data, size_t size)
{
struct solr_fts_backend_update_context *ctx =
(struct solr_fts_backend_update_context *)_ctx;
xml_encode_data(ctx->cmd, data, size);
if (str_len(ctx->cmd) > SOLR_CMDBUF_SIZE-128) {
solr_connection_post_more(ctx->post, str_data(ctx->cmd),
str_len(ctx->cmd));
str_truncate(ctx->cmd, 0);
}
return 0;
}
static int fts_backend_solr_refresh(struct fts_backend *backend ATTR_UNUSED)
{
return 0;
}
static int fts_backend_solr_optimize(struct fts_backend *backend ATTR_UNUSED)
{
return 0;
}
static bool
solr_add_definite_query(string_t *str, struct mail_search_arg *arg)
{
switch (arg->type) {
case SEARCH_TEXT: {
if (arg->match_not)
str_append_c(str, '-');
str_append(str, "(hdr:");
solr_quote_http(str, arg->value.str);
str_append(str, "+OR+body:");
solr_quote_http(str, arg->value.str);
str_append(str, ")");
break;
}
case SEARCH_BODY:
if (arg->match_not)
str_append_c(str, '-');
str_append(str, "body:");
solr_quote_http(str, arg->value.str);
break;
default:
return FALSE;
}
return TRUE;
}
static bool
solr_add_definite_query_args(string_t *str, struct mail_search_arg *arg,
bool and_args)
{
unsigned int last_len;
last_len = str_len(str);
for (; arg != NULL; arg = arg->next) {
if (solr_add_definite_query(str, arg)) {
arg->match_always = TRUE;
last_len = str_len(str);
if (and_args)
str_append(str, "+AND+");
else
str_append(str, "+OR+");
}
}
if (str_len(str) == last_len)
return FALSE;
str_truncate(str, last_len);
return TRUE;
}
static int
fts_backend_solr_lookup(struct fts_backend *_backend, struct mailbox *box,
struct mail_search_arg *args, bool and_args,
struct fts_result *result)
{
struct solr_fts_backend *backend =
(struct solr_fts_backend *)_backend;
struct mail_namespace *ns;
struct mailbox_status status;
string_t *str;
const char *box_name;
pool_t pool;
struct solr_result **results;
int ret;
fts_solr_set_default_ns(backend);
mailbox_get_open_status(box, STATUS_UIDVALIDITY | STATUS_UIDNEXT,
&status);
str = t_str_new(256);
str_printfa(str, "fl=uid,score&rows=%u&sort=uid+asc&q=",
status.uidnext);
if (!solr_add_definite_query_args(str, args, and_args)) {
/* can't search this query */
return 0;
}
/* use a separate filter query for selecting the mailbox. it shouldn't
affect the score and there could be some caching benefits too. */
str_append(str, "&fq=%2Buser:");
solr_quote_http(str, box->storage->user->username);
box_name = fts_box_get_root(box, &ns);
str_printfa(str, "+%%2Buidv:%u+%%2Bbox:", status.uidvalidity);
solr_quote_http(str, box_name);
solr_add_ns_query_http(str, backend, ns);
pool = pool_alloconly_create("fts solr search", 1024);
ret = solr_connection_select(solr_conn, str_c(str), pool, &results);
if (ret == 0 && results[0] != NULL) {
array_append_array(&result->definite_uids, &results[0]->uids);
array_append_array(&result->scores, &results[0]->scores);
}
result->scores_sorted = TRUE;
pool_unref(&pool);
return ret;
}
static char *
mailbox_get_id(struct solr_fts_backend *backend, struct mail_namespace *ns,
const char *mailbox, uint32_t uidvalidity)
{
string_t *str = t_str_new(64);
str_printfa(str, "%u\001", uidvalidity);
str_append(str, mailbox);
if (ns != backend->default_ns)
str_printfa(str, "\001%s", ns->prefix);
return str_c_modifiable(str);
}
static int
solr_search_multi(struct solr_fts_backend *backend, string_t *str,
struct mailbox *const boxes[],
struct fts_multi_result *result)
{
struct solr_result **solr_results;
struct fts_result *fts_result;
ARRAY(struct fts_result) fts_results;
struct mail_namespace *ns;
struct mailbox_status status;
HASH_TABLE(char *, struct mailbox *) mailboxes;
struct mailbox *box;
const char *box_name;
char *box_id;
unsigned int i, len;
/* use a separate filter query for selecting the mailbox. it shouldn't
affect the score and there could be some caching benefits too. */
str_append(str, "&fq=%2Buser:");
if (backend->backend.ns->owner != NULL)
solr_quote_http(str, backend->backend.ns->owner->username);
else
str_append(str, "%22%22");
hash_table_create(&mailboxes, default_pool, 0, str_hash, strcmp);
str_append(str, "%2B(");
len = str_len(str);
for (i = 0; boxes[i] != NULL; i++) {
if (str_len(str) != len)
str_append(str, "+OR+");
box_name = fts_box_get_root(boxes[i], &ns);
mailbox_get_open_status(boxes[i], STATUS_UIDVALIDITY, &status);
str_printfa(str, "%%2B(%%2Buidv:%u+%%2Bbox:", status.uidvalidity);
solr_quote_http(str, box_name);
solr_add_ns_query_http(str, backend, ns);
str_append_c(str, ')');
box_id = mailbox_get_id(backend, ns, box_name, status.uidvalidity);
hash_table_insert(mailboxes, box_id, boxes[i]);
}
str_append_c(str, ')');
if (solr_connection_select(solr_conn, str_c(str),
result->pool, &solr_results) < 0) {
hash_table_destroy(&mailboxes);
return -1;
}
p_array_init(&fts_results, result->pool, 32);
for (i = 0; solr_results[i] != NULL; i++) {
box = hash_table_lookup(mailboxes, solr_results[i]->box_id);
if (box == NULL) {
i_warning("fts_solr: Lookup returned unexpected mailbox "
"with id=%s", solr_results[i]->box_id);
continue;
}
fts_result = array_append_space(&fts_results);
fts_result->box = box;
fts_result->definite_uids = solr_results[i]->uids;
fts_result->scores = solr_results[i]->scores;
fts_result->scores_sorted = TRUE;
}
array_append_zero(&fts_results);
result->box_results = array_idx_modifiable(&fts_results, 0);
hash_table_destroy(&mailboxes);
return 0;
}
static int
fts_backend_solr_lookup_multi(struct fts_backend *_backend,
struct mailbox *const boxes[],
struct mail_search_arg *args, bool and_args,
struct fts_multi_result *result)
{
struct solr_fts_backend *backend =
(struct solr_fts_backend *)_backend;
string_t *str;
fts_solr_set_default_ns(backend);
str = t_str_new(256);
str_printfa(str, "fl=ns,box,uidv,uid,score&rows=%u&sort=box+asc,uid+asc&q=",
SOLR_MAX_MULTI_ROWS);
if (solr_add_definite_query_args(str, args, and_args)) {
if (solr_search_multi(backend, str, boxes, result) < 0)
return -1;
}
/* FIXME: maybe_uids could be handled also with some more work.. */
return 0;
}
struct fts_backend fts_backend_solr_old = {
.name = "solr_old",
.flags = 0,
{
fts_backend_solr_alloc,
fts_backend_solr_init,
fts_backend_solr_deinit,
fts_backend_solr_get_last_uid,
fts_backend_solr_update_init,
fts_backend_solr_update_deinit,
fts_backend_solr_update_set_mailbox,
fts_backend_solr_update_expunge,
fts_backend_solr_update_set_build_key,
fts_backend_solr_update_unset_build_key,
fts_backend_solr_update_build_more,
fts_backend_solr_refresh,
NULL,
fts_backend_solr_optimize,
fts_backend_default_can_lookup,
fts_backend_solr_lookup,
fts_backend_solr_lookup_multi,
NULL
}
};