fts-backend-lucene.c revision f2496be45dc16d1f93cb82a146b91d273b0621c0
/* Copyright (c) 2006-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "hash.h"
#include "hex-binary.h"
#include "strescape.h"
#include "message-part.h"
#include "mail-namespace.h"
#include "mail-storage-private.h"
#include "fts-expunge-log.h"
#include "lucene-wrapper.h"
#include "fts-indexer.h"
#include "fts-lucene-plugin.h"
#include <wchar.h>
#define LUCENE_INDEX_DIR_NAME "lucene-indexes"
#define LUCENE_EXPUNGE_LOG_NAME "dovecot-expunges.log"
#define LUCENE_OPTIMIZE_BATCH_MSGS_COUNT 100
struct lucene_fts_backend {
struct fts_backend backend;
char *dir_path;
struct lucene_index *index;
struct mailbox *selected_box;
unsigned int selected_box_generation;
struct fts_expunge_log *expunge_log;
bool dir_created:1;
bool updating:1;
};
struct lucene_fts_backend_update_context {
struct fts_backend_update_context ctx;
char *first_box_vname;
char *hdr_name;
unsigned int added_msgs;
struct fts_expunge_log_append_ctx *expunge_ctx;
bool lucene_opened;
bool last_indexed_uid_set;
bool mime_parts;
};
{
if (backend->dir_created)
return 0;
return -1;
return 0;
}
static int
{
struct mailbox_metadata metadata;
&metadata) < 0) {
i_error("lucene: Couldn't get mailbox %s GUID: %s",
return -1;
}
return 0;
}
static int
{
unsigned char guid_hex[MAILBOX_GUID_HEX_LENGTH];
unsigned int i;
return 0;
return -1;
for (i = 0; i < N_ELEMENTS(wguid_hex); i++)
sizeof(backend->selected_box_guid));
return 0;
}
static struct fts_backend *fts_backend_lucene_alloc(void)
{
struct lucene_fts_backend *backend;
}
static int
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
struct fts_lucene_user *fuser =
const char *path;
/* invalid settings */
*error_r = "Invalid fts_lucene settings";
return -1;
}
/* fts already checked that index exists */
/* change our flags so we get proper input */
}
return 0;
}
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
}
static int
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
struct fts_lucene_user *fuser =
struct fts_index_header hdr;
int ret;
if (ret < 0)
return -1;
if (ret == 0) {
/* need to rebuild the index */
*last_uid_r = 0;
} else {
}
return 0;
}
/* either nothing has been indexed, or the index was corrupted.
do it the slow way. */
return -1;
return -1;
return 0;
}
static struct fts_backend_update_context *
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
struct lucene_fts_backend_update_context *ctx;
struct fts_lucene_user *fuser =
}
static bool
{
struct lucene_fts_backend *backend =
unsigned int expunges;
return TRUE;
return FALSE;
return FALSE;
return expunges > 0 &&
}
static int
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
struct lucene_fts_backend *backend =
if (ctx->lucene_opened) {
ret = -1;
}
/* lucene-indexes directory doesn't even exist,
so dovecot.index's last_index_uid is wrong.
rescan to update them. */
ret = 0;
}
ret = -1;
}
}
if (fts_backend_lucene_need_optimize(ctx)) {
if (ctx->lucene_opened)
int fd;
/* the optimize affects all mailboxes within namespace,
so just use any mailbox name in it */
if (fd != -1)
i_close_fd(&fd);
}
}
return ret;
}
static void
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
}
}
static void
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
struct lucene_fts_backend *backend =
struct fts_index_header hdr;
if (!ctx->last_indexed_uid_set) {
ctx->last_indexed_uid = 0;
else
}
if (ctx->last_indexed_uid == 0 ||
/* don't waste time adding expunge to log for a message that
isn't even indexed. this check is racy, because indexer may
just be in the middle of indexing this message. we'll
attempt to avoid that by skipping the expunging only if
indexing hasn't been done for a while (100 msgs). */
return;
}
ctx->expunge_ctx =
}
}
static bool
const struct fts_backend_build_key *key)
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
struct lucene_fts_backend *backend =
if (!ctx->lucene_opened) {
if (fts_backend_lucene_mkdir(backend) < 0)
}
break;
break;
i_unreached();
}
ctx->added_msgs++;
}
if (ctx->mime_parts)
return TRUE;
}
static void
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
}
static int
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
struct lucene_fts_backend *backend =
int ret;
return -1;
T_BEGIN {
} T_END;
return ret;
}
static int
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
return 0;
}
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
return -1;
}
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
int ret;
if (ret == 0) {
/* log was corrupted, need to rescan */
}
if (ret >= 0)
return ret;
}
static int
struct mail_search_arg *args,
enum fts_lookup_flags flags,
struct fts_result *result)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
int ret;
return -1;
T_BEGIN {
} T_END;
return ret;
}
/* a char* hash function from ASU -- from glib */
{
unsigned int g, h = 0;
while (*s != '\0') {
h = (h << 4) + *s;
if ((g = h & 0xf0000000UL) != 0) {
h = h ^ (g >> 24);
h = h ^ g;
}
s++;
}
return h;
}
static int
struct fts_multi_result *result)
{
struct fts_result *box_result;
const char *guid;
unsigned int i, j;
/* first create the box_results - we'll be using pointers to them
later on and appending to the array changes the pointers */
}
return -1;
for (j = 0; j < MAILBOX_GUID_HEX_LENGTH; j++)
}
return 0;
}
static int
struct mail_search_arg *args,
enum fts_lookup_flags flags,
struct fts_multi_result *result)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
int ret;
T_BEGIN {
if (ret == 0) {
result);
}
} T_END;
return ret;
}
{
/* the next refresh is going to close the index anyway, so we might as
well do it now */
(void)fts_backend_lucene_refresh(_backend);
}
struct fts_backend fts_backend_lucene = {
.name = "lucene",
{
}
};