fts-backend-lucene.c revision a10ed8c47534b4c6b6bf2711ccfe577e720a47b4
/* Copyright (c) 2006-2012 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "hash.h"
#include "hex-binary.h"
#include "strescape.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;
unsigned int dir_created:1;
unsigned int 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;
};
{
const char *error;
if (backend->dir_created)
return 0;
MAILBOX_LIST_PATH_TYPE_INDEX, &error) < 0) {
i_error("lucene: Couldn't create root dir %s: %s",
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++)
} else {
}
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;
}
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;
set_checksum)) {
/* 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;
}
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;
}
if (fts_backend_lucene_need_optimize(ctx)) {
if (ctx->lucene_opened)
else {
int fd;
/* the optimize affects all mailboxes within namespace,
so just use any mailbox name in it */
if (fd != -1)
}
}
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++;
}
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 fts_result *result)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
int ret;
return -1;
T_BEGIN {
result);
} T_END;
return ret;
}
/* a char* hash function from ASU -- from glib */
static unsigned int wstr_hash(const void *p)
{
const wchar_t *s = p;
unsigned int g, h = 0;
while (*s != '\0') {
h = (h << 4) + *s;
if ((g = h & 0xf0000000UL)) {
h = h ^ (g >> 24);
h = h ^ g;
}
s++;
}
return h;
}
static int
{
struct fts_result *box_result;
const char *guid;
unsigned int i, j;
return -1;
for (j = 0; j < MAILBOX_GUID_HEX_LENGTH; j++)
}
(void)array_append_space(&box_results);
return 0;
}
static int
struct fts_multi_result *result)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
int ret;
T_BEGIN {
struct hash_table *guids;
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 */
}
struct fts_backend fts_backend_lucene = {
.name = "lucene",
{
}
};