1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/* Copyright (c) 2006-2018 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;
guid_128_t selected_box_guid;
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;
struct mailbox *box;
uint32_t last_uid;
uint32_t last_indexed_uid;
char *first_box_vname;
uint32_t uid, part_num;
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;
};
static int fts_backend_lucene_mkdir(struct lucene_fts_backend *backend)
{
if (backend->dir_created)
return 0;
backend->dir_created = TRUE;
if (mailbox_list_mkdir_root(backend->backend.ns->list,
backend->dir_path,
MAILBOX_LIST_PATH_TYPE_INDEX) < 0)
return -1;
return 0;
}
static int
fts_lucene_get_mailbox_guid(struct mailbox *box, guid_128_t guid_r)
{
struct mailbox_metadata metadata;
if (mailbox_get_metadata(box, MAILBOX_METADATA_GUID,
&metadata) < 0) {
i_error("lucene: Couldn't get mailbox %s GUID: %s",
box->vname, mailbox_get_last_internal_error(box, NULL));
return -1;
}
memcpy(guid_r, metadata.guid, GUID_128_SIZE);
return 0;
}
static int
fts_backend_select(struct lucene_fts_backend *backend, struct mailbox *box)
{
guid_128_t guid;
unsigned char guid_hex[MAILBOX_GUID_HEX_LENGTH];
wchar_t wguid_hex[MAILBOX_GUID_HEX_LENGTH];
buffer_t buf;
unsigned int i;
i_assert(box != NULL);
if (backend->selected_box == box &&
backend->selected_box_generation == box->generation_sequence)
return 0;
if (fts_lucene_get_mailbox_guid(box, guid) < 0)
return -1;
buffer_create_from_data(&buf, guid_hex, MAILBOX_GUID_HEX_LENGTH);
binary_to_hex_append(&buf, guid, GUID_128_SIZE);
for (i = 0; i < N_ELEMENTS(wguid_hex); i++)
wguid_hex[i] = guid_hex[i];
lucene_index_select_mailbox(backend->index, wguid_hex);
backend->selected_box = box;
memcpy(backend->selected_box_guid, guid,
sizeof(backend->selected_box_guid));
backend->selected_box_generation = box->generation_sequence;
return 0;
}
static struct fts_backend *fts_backend_lucene_alloc(void)
{
struct lucene_fts_backend *backend;
backend = i_new(struct lucene_fts_backend, 1);
backend->backend = fts_backend_lucene;
return &backend->backend;
}
static int
fts_backend_lucene_init(struct fts_backend *_backend, const char **error_r)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
struct fts_lucene_user *fuser =
FTS_LUCENE_USER_CONTEXT(_backend->ns->user);
const char *path;
if (fuser == NULL) {
/* invalid settings */
*error_r = "Invalid fts_lucene settings";
return -1;
}
/* fts already checked that index exists */
if (fuser->set.use_libfts) {
/* change our flags so we get proper input */
_backend->flags &= ENUM_NEGATE(FTS_BACKEND_FLAG_FUZZY_SEARCH);
_backend->flags |= FTS_BACKEND_FLAG_TOKENIZED_INPUT;
}
path = mailbox_list_get_root_forced(_backend->ns->list,
MAILBOX_LIST_PATH_TYPE_INDEX);
backend->dir_path = i_strconcat(path, "/"LUCENE_INDEX_DIR_NAME, NULL);
backend->index = lucene_index_init(backend->dir_path,
_backend->ns->list,
&fuser->set);
path = t_strconcat(backend->dir_path, "/"LUCENE_EXPUNGE_LOG_NAME, NULL);
backend->expunge_log = fts_expunge_log_init(path);
return 0;
}
static void fts_backend_lucene_deinit(struct fts_backend *_backend)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
if (backend->index != NULL)
lucene_index_deinit(backend->index);
if (backend->expunge_log != NULL)
fts_expunge_log_deinit(&backend->expunge_log);
i_free(backend->dir_path);
i_free(backend);
}
static int
fts_backend_lucene_get_last_uid(struct fts_backend *_backend,
struct mailbox *box, uint32_t *last_uid_r)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
struct fts_lucene_user *fuser =
FTS_LUCENE_USER_CONTEXT_REQUIRE(_backend->ns->user);
struct fts_index_header hdr;
uint32_t set_checksum;
int ret;
if (fts_index_get_header(box, &hdr)) {
set_checksum = fts_lucene_settings_checksum(&fuser->set);
ret = fts_index_have_compatible_settings(_backend->ns->list,
set_checksum);
if (ret < 0)
return -1;
if (ret == 0) {
/* need to rebuild the index */
*last_uid_r = 0;
} else {
*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_select(backend, box) < 0)
return -1;
if (lucene_index_get_last_uid(backend->index, 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_lucene_update_init(struct fts_backend *_backend)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
struct lucene_fts_backend_update_context *ctx;
struct fts_lucene_user *fuser =
FTS_LUCENE_USER_CONTEXT_REQUIRE(_backend->ns->user);
i_assert(!backend->updating);
ctx = i_new(struct lucene_fts_backend_update_context, 1);
ctx->ctx.backend = _backend;
ctx->mime_parts = fuser->set.mime_parts;
backend->updating = TRUE;
return &ctx->ctx;
}
static bool
fts_backend_lucene_need_optimize(struct lucene_fts_backend_update_context *ctx)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)ctx->ctx.backend;
unsigned int expunges;
uint32_t numdocs;
if (ctx->added_msgs >= LUCENE_OPTIMIZE_BATCH_MSGS_COUNT)
return TRUE;
if (lucene_index_get_doc_count(backend->index, &numdocs) < 0)
return FALSE;
if (fts_expunge_log_uid_count(backend->expunge_log, &expunges) < 0)
return FALSE;
return expunges > 0 &&
numdocs / expunges <= 50; /* >2% of index has been expunged */
}
static int
fts_backend_lucene_update_deinit(struct fts_backend_update_context *_ctx)
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_ctx->backend;
int ret = _ctx->failed ? -1 : 0;
i_assert(backend->updating);
backend->updating = FALSE;
if (ctx->lucene_opened) {
if (lucene_index_build_deinit(backend->index) < 0)
ret = -1;
}
if (ctx->expunge_ctx != NULL) {
if (fts_expunge_log_append_commit(&ctx->expunge_ctx) < 0) {
struct stat st;
ret = -1;
if (stat(backend->dir_path, &st) < 0 && errno == ENOENT) {
/* lucene-indexes directory doesn't even exist,
so dovecot.index's last_index_uid is wrong.
rescan to update them. */
(void)lucene_index_rescan(backend->index);
ret = 0;
}
}
}
if (fts_backend_lucene_need_optimize(ctx)) {
if (ctx->lucene_opened)
(void)fts_backend_optimize(_ctx->backend);
else if (ctx->first_box_vname != NULL) {
struct mail_user *user = backend->backend.ns->user;
const char *cmd, *path;
int fd;
/* the optimize affects all mailboxes within namespace,
so just use any mailbox name in it */
cmd = t_strdup_printf("OPTIMIZE\t0\t%s\t%s\n",
str_tabescape(user->username),
str_tabescape(ctx->first_box_vname));
fd = fts_indexer_cmd(user, cmd, &path);
i_close_fd(&fd);
}
}
i_free(ctx->first_box_vname);
i_free(ctx);
return ret;
}
static void
fts_backend_lucene_update_set_mailbox(struct fts_backend_update_context *_ctx,
struct mailbox *box)
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
if (ctx->last_uid != 0) {
fts_index_set_last_uid(ctx->box, ctx->last_uid);
ctx->last_uid = 0;
}
if (ctx->first_box_vname == NULL && box != NULL)
ctx->first_box_vname = i_strdup(box->vname);
ctx->box = box;
ctx->last_indexed_uid_set = FALSE;
}
static void
fts_backend_lucene_update_expunge(struct fts_backend_update_context *_ctx,
uint32_t uid)
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_ctx->backend;
struct fts_index_header hdr;
if (!ctx->last_indexed_uid_set) {
if (!fts_index_get_header(ctx->box, &hdr))
ctx->last_indexed_uid = 0;
else
ctx->last_indexed_uid = hdr.last_indexed_uid;
ctx->last_indexed_uid_set = TRUE;
}
if (ctx->last_indexed_uid == 0 ||
uid > ctx->last_indexed_uid + 100) {
/* 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;
}
if (ctx->expunge_ctx == NULL) {
ctx->expunge_ctx =
fts_expunge_log_append_begin(backend->expunge_log);
}
if (fts_backend_select(backend, ctx->box) < 0)
_ctx->failed = TRUE;
fts_expunge_log_append_next(ctx->expunge_ctx,
backend->selected_box_guid, uid);
}
static bool
fts_backend_lucene_update_set_build_key(struct fts_backend_update_context *_ctx,
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 =
(struct lucene_fts_backend *)_ctx->backend;
if (!ctx->lucene_opened) {
if (fts_backend_lucene_mkdir(backend) < 0)
ctx->ctx.failed = TRUE;
if (lucene_index_build_init(backend->index) < 0)
ctx->ctx.failed = TRUE;
ctx->lucene_opened = TRUE;
}
if (fts_backend_select(backend, ctx->box) < 0)
_ctx->failed = TRUE;
switch (key->type) {
case FTS_BACKEND_BUILD_KEY_HDR:
case FTS_BACKEND_BUILD_KEY_MIME_HDR:
i_assert(key->hdr_name != NULL);
i_free(ctx->hdr_name);
ctx->hdr_name = i_strdup(key->hdr_name);
break;
case FTS_BACKEND_BUILD_KEY_BODY_PART:
i_free_and_null(ctx->hdr_name);
break;
case FTS_BACKEND_BUILD_KEY_BODY_PART_BINARY:
i_unreached();
}
if (key->uid != ctx->last_uid) {
i_assert(key->uid >= ctx->last_uid);
ctx->last_uid = key->uid;
ctx->added_msgs++;
}
ctx->uid = key->uid;
if (ctx->mime_parts)
ctx->part_num = message_part_to_idx(key->part);
return TRUE;
}
static void
fts_backend_lucene_update_unset_build_key(struct fts_backend_update_context *_ctx)
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
ctx->uid = 0;
ctx->part_num = 0;
i_free_and_null(ctx->hdr_name);
}
static int
fts_backend_lucene_update_build_more(struct fts_backend_update_context *_ctx,
const unsigned char *data, size_t size)
{
struct lucene_fts_backend_update_context *ctx =
(struct lucene_fts_backend_update_context *)_ctx;
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_ctx->backend;
int ret;
i_assert(ctx->uid != 0);
if (_ctx->failed)
return -1;
T_BEGIN {
ret = lucene_index_build_more(backend->index, ctx->uid,
ctx->part_num, data, size,
ctx->hdr_name);
} T_END;
return ret;
}
static int
fts_backend_lucene_refresh(struct fts_backend *_backend)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
if (backend->index != NULL)
lucene_index_close(backend->index);
return 0;
}
static int fts_backend_lucene_rescan(struct fts_backend *_backend)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
if (lucene_index_rescan(backend->index) < 0)
return -1;
return lucene_index_optimize(backend->index);
}
static int fts_backend_lucene_optimize(struct fts_backend *_backend)
{
struct lucene_fts_backend *backend =
(struct lucene_fts_backend *)_backend;
int ret;
ret = lucene_index_expunge_from_log(backend->index,
backend->expunge_log);
if (ret == 0) {
/* log was corrupted, need to rescan */
ret = lucene_index_rescan(backend->index);
}
if (ret >= 0)
ret = lucene_index_optimize(backend->index);
return ret;
}
static int
fts_backend_lucene_lookup(struct fts_backend *_backend, struct mailbox *box,
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;
if (fts_backend_select(backend, box) < 0)
return -1;
T_BEGIN {
ret = lucene_index_lookup(backend->index, args, flags, result);
} T_END;
return ret;
}
/* a char* hash function from ASU -- from glib */
static unsigned int wstr_hash(const wchar_t *s)
{
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
mailboxes_get_guids(struct mailbox *const boxes[],
HASH_TABLE_TYPE(wguid_result) guids,
struct fts_multi_result *result)
{
ARRAY(struct fts_result) box_results;
struct fts_result *box_result;
const char *guid;
wchar_t *guid_dup;
unsigned int i, j;
p_array_init(&box_results, result->pool, 32);
/* first create the box_results - we'll be using pointers to them
later on and appending to the array changes the pointers */
for (i = 0; boxes[i] != NULL; i++) {
box_result = array_append_space(&box_results);
box_result->box = boxes[i];
}
for (i = 0; boxes[i] != NULL; i++) {
if (fts_mailbox_get_guid(boxes[i], &guid) < 0)
return -1;
i_assert(strlen(guid) == MAILBOX_GUID_HEX_LENGTH);
guid_dup = t_new(wchar_t, MAILBOX_GUID_HEX_LENGTH + 1);
for (j = 0; j < MAILBOX_GUID_HEX_LENGTH; j++)
guid_dup[j] = guid[j];
box_result = array_idx_modifiable(&box_results, i);
hash_table_insert(guids, guid_dup, box_result);
}
array_append_zero(&box_results);
result->box_results = array_front_modifiable(&box_results);
return 0;
}
static int
fts_backend_lucene_lookup_multi(struct fts_backend *_backend,
struct mailbox *const boxes[],
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 {
HASH_TABLE_TYPE(wguid_result) guids;
hash_table_create(&guids, default_pool, 0, wstr_hash, wcscmp);
ret = mailboxes_get_guids(boxes, guids, result);
if (ret == 0) {
ret = lucene_index_lookup_multi(backend->index,
guids, args, flags,
result);
}
hash_table_destroy(&guids);
} T_END;
return ret;
}
static void fts_backend_lucene_lookup_done(struct fts_backend *_backend)
{
/* 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",
.flags = FTS_BACKEND_FLAG_BUILD_FULL_WORDS |
FTS_BACKEND_FLAG_FUZZY_SEARCH,
{
fts_backend_lucene_alloc,
fts_backend_lucene_init,
fts_backend_lucene_deinit,
fts_backend_lucene_get_last_uid,
fts_backend_lucene_update_init,
fts_backend_lucene_update_deinit,
fts_backend_lucene_update_set_mailbox,
fts_backend_lucene_update_expunge,
fts_backend_lucene_update_set_build_key,
fts_backend_lucene_update_unset_build_key,
fts_backend_lucene_update_build_more,
fts_backend_lucene_refresh,
fts_backend_lucene_rescan,
fts_backend_lucene_optimize,
fts_backend_default_can_lookup,
fts_backend_lucene_lookup,
fts_backend_lucene_lookup_multi,
fts_backend_lucene_lookup_done
}
};
|