fts-backend-solr.c revision 6b09a3b269f4b10364c9a77f6614dbe3d306b79d
2e37d45867d081db150ab78dad303b9077aea24fTimo Sirainen/* Copyright (c) 2006-2008 Dovecot authors, see the included COPYING file */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic struct solr_connection *solr_conn = NULL;
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void solr_quote_str(string_t *dest, const char *str)
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen solr_connection_quote_str(solr_conn, dest, str);
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainenxml_encode_data(string_t *dest, const unsigned char *data, unsigned int len)
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen unsigned int i;
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen for (i = 0; i < len; i++) {
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen switch (data[i]) {
11352dc3e4b29f3d2763c82f8ea4f99e8daf4fa3Timo Sirainenstatic void xml_encode(string_t *dest, const char *str)
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen xml_encode_data(dest, (const unsigned char *)str, strlen(str));
942302b0247403645394d848b3c620ead262a2a5Timo Sirainenstatic struct fts_backend *
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenfts_backend_solr_init(struct mailbox *box ATTR_UNUSED)
cd8507179823de33d6e8242e10dbc15d136245b5Timo Sirainen solr_conn = solr_connection_init(getenv("FTS_SOLR"));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic void fts_backend_solr_deinit(struct fts_backend *backend)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstatic int fts_backend_solr_get_last_uid(struct fts_backend *backend,
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen unsigned int count;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen str_append(str, "fl=uid&rows=1&sort=uid%20desc&q=");
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen mailbox_get_status(backend->box, STATUS_UIDVALIDITY, &status);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen str_printfa(str, "uidv:%u%%20box:", status.uidvalidity);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen solr_quote_str(str, backend->box->storage->user);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen if (solr_connection_select(solr_conn, str_c(str), &uids) < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* nothing indexed yet for this mailbox */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen } else if (count == 1 && uidvals[0].seq1 == uidvals[0].seq2) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen i_error("fts_solr: Last UID lookup returned multiple rows");
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenfts_backend_solr_build_init(struct fts_backend *backend, uint32_t *last_uid_r,
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen ctx = i_new(struct solr_fts_backend_build_context, 1);
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen ctx->post = solr_connection_post_begin(solr_conn);
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen ctx->cmd = str_new(default_pool, SOLR_CMDBUF_SIZE);
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen mailbox_get_status(backend->box, STATUS_UIDVALIDITY, &status);
11352dc3e4b29f3d2763c82f8ea4f99e8daf4fa3Timo Sirainenfts_backend_solr_build_more(struct fts_backend_build_context *_ctx,
755aea84bbe2b15ed7fe991f6462a93333ff571fTimo Sirainen (struct solr_fts_backend_build_context *)_ctx;
755aea84bbe2b15ed7fe991f6462a93333ff571fTimo Sirainen /* body comes first, then headers */
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen /* uid changed */
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen "<field name=\"uid\">%u</field>"
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen "<field name=\"uidv\">%u</field>",
6b399f555c9c5c722d4cd5eab8faa02b2a4731d3Timo Sirainen str_append(cmd, "</field><field name=\"user\">");
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen str_printfa(cmd, "</field><field name=\"id\">%u/%u/",
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen str_append(cmd, "</field><field name=\"hdr\">");
6b399f555c9c5c722d4cd5eab8faa02b2a4731d3Timo Sirainen solr_connection_post_more(ctx->post, str_data(cmd),
7f3b826a89bcb7a72759912e99f574b28309fe1bTimo Sirainenfts_backend_solr_build_deinit(struct fts_backend_build_context *_ctx)
6b399f555c9c5c722d4cd5eab8faa02b2a4731d3Timo Sirainen (struct solr_fts_backend_build_context *)_ctx;
6b399f555c9c5c722d4cd5eab8faa02b2a4731d3Timo Sirainen solr_connection_post_more(ctx->post, str_data(ctx->cmd),
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen /* commit and wait until the documents we just indexed are
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen visible to the following search */
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "<commit waitFlush=\"false\" "
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "waitSearcher=\"true\"/>") < 0)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenfts_backend_solr_expunge(struct fts_backend *backend ATTR_UNUSED,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mailbox_get_status(mail->box, STATUS_UIDVALIDITY, &status);
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen (void)solr_connection_post(solr_conn, str_c(cmd));
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenfts_backend_solr_expunge_finish(struct fts_backend *backend ATTR_UNUSED,
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen "<commit waitFlush=\"false\" waitSearcher=\"false\"/>");
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainenstatic int fts_backend_solr_lock(struct fts_backend *backend ATTR_UNUSED)
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainenstatic void fts_backend_solr_unlock(struct fts_backend *backend ATTR_UNUSED)
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainenfts_backend_solr_lookup(struct fts_backend *backend, const char *key,
c4bb0320ab43ea35fa6df88fc745fdad906cee44Timo Sirainen i_assert((flags & FTS_LOOKUP_FLAG_INVERT) == 0);
047e3bbb00e68a0d43355e11a67b2e912e06de19Timo Sirainen /* body only */
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen i_assert((flags & FTS_LOOKUP_FLAG_BODY) != 0);
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen } else if ((flags & FTS_LOOKUP_FLAG_BODY) == 0) {
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen /* header only */
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen mailbox_get_status(backend->box, STATUS_UIDVALIDITY, &status);
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen str_printfa(str, "%%20uidv:%u%%20box:", status.uidvalidity);
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen solr_quote_str(str, backend->box->storage->user);