]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei_xsearch.t
f626c79099217810b9c52419e12775f73cab466f
[public-inbox.git] / t / lei_xsearch.t
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use List::Util qw(shuffle max);
7 use PublicInbox::TestCommon;
8 use PublicInbox::Eml;
9 require_mods(qw(DBD::SQLite Search::Xapian));
10 require PublicInbox::ExtSearchIdx;
11 require_git 2.6;
12 require_ok 'PublicInbox::LeiXSearch';
13 my ($home, $for_destroy) = tmpdir();
14 my @ibx;
15 for my $V (1..2) {
16         for my $i (3..6) {
17                 push @ibx, create_inbox("v$V-$i", indexlevel => 'full',
18                                         version => $V, sub {
19                         my ($im, $ibx) = @_;
20                         for my $j (0..9) {
21                                 my $eml = PublicInbox::Eml->new(<<EOM);
22 From: x\@example.com
23 To: $ibx->{-primary_address}
24 Date: Fri, 02 Oct 1993 0$V:0$i:0$j +0000
25 Subject: v${V}i${i}j$j
26 Message-ID: <v${V}i${i}j$j\@example>
27
28 ${V}er ${i}on j$j
29 EOM
30                                 $im->add($eml) or BAIL_OUT '->add';
31                         }
32                 }); # create_inbox
33         }
34 }
35 my $first = shift @ibx; is($first->{name}, 'v1-3', 'first plucked');
36 my $last = pop @ibx; is($last->{name}, 'v2-6', 'last plucked');
37 my $eidx = PublicInbox::ExtSearchIdx->new("$home/eidx");
38 $eidx->attach_inbox($first);
39 $eidx->attach_inbox($last);
40 $eidx->eidx_sync({fsync => 0});
41 my $es = PublicInbox::ExtSearch->new("$home/eidx");
42 my $lxs = PublicInbox::LeiXSearch->new;
43 for my $ibxish (shuffle($es, @ibx)) {
44         $lxs->prepare_external($ibxish);
45 }
46 for my $loc ($lxs->locals) {
47         $lxs->attach_external($loc);
48 }
49 my $nr = $lxs->xdb->get_doccount;
50 my $mset = $lxs->mset('d:19931002..19931003', { limit => $nr });
51 is($mset->size, $nr, 'got all messages');
52 my @msgs;
53 for my $mi ($mset->items) {
54         if (my $smsg = $lxs->smsg_for($mi)) {
55                 push @msgs, $smsg;
56         } else {
57                 diag "E: ${\$mi->get_docid} missing";
58         }
59 }
60 is(scalar(@msgs), $nr, 'smsgs retrieved for all');
61
62 $mset = $lxs->recent(undef, { limit => 1 });
63 is($mset->size, 1, 'one result');
64 my $max = max(map { $_->{docid} } @msgs);
65 is($lxs->smsg_for(($mset->items)[0])->{docid}, $max,
66         'got highest docid');
67
68 my @ibxish = $lxs->locals;
69 is(scalar(@ibxish), scalar(@ibx) + 1, 'got locals back');
70 is($lxs->search, $lxs, '->search works');
71 is($lxs->over, undef, '->over fails');
72
73 {
74         $lxs = PublicInbox::LeiXSearch->new;
75         my $v2ibx = create_inbox 'v2full', version => 2, sub {
76                 $_[0]->add(eml_load('t/plack-qp.eml'));
77         };
78         my $v1ibx = create_inbox 'v1medium', indexlevel => 'medium', sub {
79                 $_[0]->add(eml_load('t/utf8.eml'));
80         };
81         $lxs->prepare_external($v1ibx);
82         $lxs->prepare_external($v2ibx);
83         for my $loc ($lxs->locals) {
84                 $lxs->attach_external($loc);
85         }
86         my $mset = $lxs->mset('m:testmessage@example.com');
87         is($mset->size, 1, 'got m: match on medium+full XSearch mix');
88 }
89
90 done_testing;