fts-storage.c revision eb0ede66120bb63c0212bad69e67efca1eb47324
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
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainen/* Copyright (C) 2006 Timo Sirainen */
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen *((void **)array_idx_modifiable(&(obj)->module_contexts, \
0fd246126fece57712566c725d6353f255f5fcfaTimo Sirainen const char *env;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstatic unsigned int fts_storage_module_id = 0;
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainenstatic int fts_mailbox_close(struct mailbox *box)
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstatic int uid_range_to_seq(struct mailbox *box,
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen unsigned int i, count;
94f90df2cfb7587bb5af432b2ba065d1c364e1f7Timo Sirainen for (i = 0; i < count; i++) {
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen if (mailbox_get_uids(box, range[i].seq1, range[i].seq2,
38a4c09de37bc2ebdc38427a2b958c46dfdcffb1Timo Sirainenstatic int fts_build_mail_flush(struct fts_storage_build_context *ctx)
38a4c09de37bc2ebdc38427a2b958c46dfdcffb1Timo Sirainen if (fts_backend_build_more(ctx->build, ctx->uid, str_data(ctx->headers),
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstatic bool fts_build_update_save_part(struct fts_storage_build_context *ctx,
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen /* we'll index only text/xxx and message/rfc822 parts for now */
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenstatic int fts_build_mail_header(struct fts_storage_build_context *ctx,
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen const struct message_header_line *hdr = block->hdr;
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen /* hdr->full_value is always set because we get the block from
38a4c09de37bc2ebdc38427a2b958c46dfdcffb1Timo Sirainen message_decoder */
043c8a96a035379bcba04f487d58457beefdfcaaTimo Sirainen str_append_n(ctx->headers, hdr->middle, hdr->middle_len);
043c8a96a035379bcba04f487d58457beefdfcaaTimo Sirainen str_append_n(ctx->headers, hdr->full_value, hdr->full_value_len);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen if (strcasecmp(hdr->name, "Content-Type") == 0) {
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainenfts_build_mail(struct fts_storage_build_context *ctx, struct mail *mail)
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen parser = message_parser_init(pool_datastack_create(), input);
992118a50af940482b6cf884a89be56d7015580aTimo Sirainen ret = message_parser_parse_next_block(parser, &raw_block);
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen if (!message_decoder_decode_next_block(decoder, &raw_block,
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen /* end of headers */
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen if (fts_build_update_save_part(ctx, &block)) {
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen if (fts_backend_build_more(ctx->build, mail->uid,
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainenstatic int fts_build_new(struct fts_backend *backend,
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if (fts_backend_get_last_uid(backend, &last_uid) < 0)
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if (mailbox_get_uids(t->box, last_uid+1, (uint32_t)-1,
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* no new messages */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen ctx.build = fts_backend_build_init(backend, &last_uid_locked);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* changed, need to get again the sequences */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if (mailbox_get_uids(t->box, last_uid+1, (uint32_t)-1,
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* no new messages */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen search_ctx = mailbox_search_init(t, NULL, &search_arg, NULL);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen while (mailbox_search_next(search_ctx, mail) > 0) {
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen#define SEARCH_ARG_IS_OPTIMIZABLE(backend, arg) \
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen ((arg)->type == SEARCH_BODY_FAST || (arg)->type == SEARCH_TEXT_FAST || \
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen ((backend)->flags & FTS_BACKEND_FLAG_EXACT_LOOKUPS) != 0 && \
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen ((arg)->type == SEARCH_BODY || (arg)->type == SEARCH_TEXT)))
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainenfts_mailbox_search_init(struct mailbox_transaction_context *t,
af3f857bb3166ed99595e11a9d18e5b5cc670e1aTimo Sirainen const char *charset, struct mail_search_arg *args,
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen struct fts_mailbox *fbox = FTS_CONTEXT(t->box);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen ctx = fbox->super.search_init(t, charset, args, sort_program);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen array_idx_set(&ctx->module_contexts, fts_storage_module_id, &fctx);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if (fbox->backend_exact == NULL && fbox->backend_fast == NULL)
b8835b8a21c617ceb82ddc5a176243faf36aa8f7Timo Sirainen /* FIXME: handle AND/OR. Maybe also header lookups? */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* first check if we can use backend_fast */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen for (tmp = args; tmp != NULL; tmp = tmp->next) {
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* nope. what about backend_exact? */
b8835b8a21c617ceb82ddc5a176243faf36aa8f7Timo Sirainen for (tmp = args; tmp != NULL; tmp = tmp->next) {
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* update the backend */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if (fts_backend_lookup(backend, args->value.str, &uid_result) < 0) {
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* failed, fallback to reading everything */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if ((backend->flags & FTS_BACKEND_FLAG_DEFINITE_LOOKUPS) != 0) {
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if (SEARCH_ARG_IS_OPTIMIZABLE(backend, args)) {
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if (fts_backend_filter(backend, args->value.str,
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen /* failed, but we already have limited
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen the search, so just ignore this */
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen (void)uid_range_to_seq(t->box, &uid_result, &fctx->result);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainenstatic int fts_mailbox_search_next_update_seq(struct mail_search_context *ctx)
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen struct fts_mailbox *fbox = FTS_CONTEXT(ctx->transaction->box);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen struct fts_search_context *fctx = FTS_CONTEXT(ctx);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen unsigned int count;
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen return fbox->super.search_next_update_seq(ctx);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen range = array_get_modifiable(&fctx->result, &count);
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainen ret = fbox->super.search_next_update_seq(ctx);
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainenstatic int fts_mailbox_search_deinit(struct mail_search_context *ctx)
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainen struct fts_mailbox *fbox = FTS_CONTEXT(ctx->transaction->box);
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainen struct fts_search_context *fctx = FTS_CONTEXT(ctx);
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainenstatic int fts_mail_expunge(struct mail *_mail)
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen struct mail_private *mail = (struct mail_private *)_mail;
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen struct fts_mailbox *fbox = FTS_CONTEXT(_mail->box);
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen struct fts_transaction_context *ft = FTS_CONTEXT(_mail->transaction);
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainen fts_backend_expunge(fbox->backend_exact, _mail);
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainen fts_backend_expunge(fbox->backend_fast, _mail);
d0ef8bc2b961a68dd0f75662c2160bd296b9476bTimo Sirainenstatic struct mail *
0fd246126fece57712566c725d6353f255f5fcfaTimo Sirainenfts_mail_alloc(struct mailbox_transaction_context *t,
0fd246126fece57712566c725d6353f255f5fcfaTimo Sirainen struct mailbox_header_lookup_ctx *wanted_headers)
0fd246126fece57712566c725d6353f255f5fcfaTimo Sirainen struct fts_mailbox *fbox = FTS_CONTEXT(t->box);
c4b376dd6e0c423006d7ac83a39253bcaf8e7c47Timo Sirainen _mail = fbox->super.mail_alloc(t, wanted_fields, wanted_headers);
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen if (fbox->backend_exact != NULL || fbox->backend_fast != NULL) {
2793e3bd31d212d6506686aa70773e13d9d98195Timo Sirainen fmail = p_new(mail->pool, struct fts_mail, 1);
0fd246126fece57712566c725d6353f255f5fcfaTimo Sirainenstatic void fts_box_backends_init(struct mailbox *box)
1176124297af5c56e932c0863c6637ff21d8a0efTimo Sirainen const char *const *tmp;
0fd246126fece57712566c725d6353f255f5fcfaTimo Sirainen for (tmp = t_strsplit(fbox->env, ", "); *tmp != NULL; tmp++) {
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen ft = i_new(struct fts_transaction_context, 1);
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen /* the backend creation is delayed until the first transaction is
eb0ede66120bb63c0212bad69e67efca1eb47324Timo Sirainen started. at that point the mailbox has been synced at least once. */
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen t = fbox->super.transaction_begin(box, flags);
0fd246126fece57712566c725d6353f255f5fcfaTimo Sirainen array_idx_set(&t->module_contexts, fts_storage_module_id, &ft);
bc93929cdd9000ca560a5f42a27f50ab307f1efbTimo Sirainenfts_transaction_finish(struct mailbox *box, struct fts_transaction_context *ft,
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen fts_backend_expunge_finish(fbox->backend_fast,
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainenstatic void fts_transaction_rollback(struct mailbox_transaction_context *t)
bc93929cdd9000ca560a5f42a27f50ab307f1efbTimo Sirainen struct fts_transaction_context *ft = FTS_CONTEXT(t);
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainenstatic int fts_transaction_commit(struct mailbox_transaction_context *t,
a0c453a8edaec90fb0d945c874de0b1845bc7d7eTimo Sirainen struct fts_transaction_context *ft = FTS_CONTEXT(t);
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen ret = fbox->super.transaction_commit(t, flags);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen const char *env;
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen box->v.search_next_update_seq = fts_mailbox_search_next_update_seq;
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen box->v.search_deinit = fts_mailbox_search_deinit;
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen box->v.transaction_begin = fts_transaction_begin;
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen box->v.transaction_rollback = fts_transaction_rollback;
88dc563319efecd6e68bad16b0d92672da05584aTimo Sirainen box->v.transaction_commit = fts_transaction_commit;
1b0cfbf3cc77a670b92fff5c30f7b1eb17a63ab1Timo Sirainen fts_storage_module_id = mail_storage_module_id++;