1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Provides everything the PublicInbox::Search object does;
5 # but uses global ExtSearch (->ALL) with an eidx_key query to
6 # emulate per-Inbox search using ->ALL.
7 package PublicInbox::Isearch;
10 use PublicInbox::ExtSearch;
11 use PublicInbox::Search;
14 my (undef, $ibx, $es) = @_;
15 bless { es => $es, eidx_key => $ibx->eidx_key }, __PACKAGE__;
20 my $sth = $self->{es}->over->dbh->prepare_cached(<<'', undef, 1);
21 SELECT ibx_id FROM inboxes WHERE eidx_key = ? LIMIT 1
23 $sth->execute($self->{eidx_key});
24 $sth->fetchrow_array //
25 die "E: `$self->{eidx_key}' not in $self->{es}->{topdir}\n";
30 my ($self, $str, $opt) = @_;
31 my %opt = $opt ? %$opt : ();
32 $opt{eidx_key} = $self->{eidx_key};
33 if (my $uid_range = $opt{uid_range}) {
34 my ($beg, $end) = @$uid_range;
35 my $ibx_id = $self->{-ibx_id} //= _ibx_id($self);
36 my $dbh = $self->{es}->{over}->dbh;
37 my $sth = $dbh->prepare_cached(<<'', undef, 1);
38 SELECT MIN(docid) FROM xref3 WHERE ibx_id = ? AND xnum >= ? AND xnum <= ?
40 $sth->execute($ibx_id, $beg, $end);
41 my @r = ($sth->fetchrow_array);
43 $sth = $dbh->prepare_cached(<<'', undef, 1);
44 SELECT MAX(docid) FROM xref3 WHERE ibx_id = ? AND xnum >= ? AND xnum <= ?
46 $sth->execute($ibx_id, $beg, $end);
47 $r[1] = $sth->fetchrow_array;
48 if (defined($r[1]) && defined($r[0])) {
49 $opt{limit} = $r[1] - $r[0] + 1;
54 $opt{uid_range} = \@r;
56 $self->{es}->mset($str, \%opt);
60 my ($self, $mset, $opt) = @_;
61 my $docids = PublicInbox::Search::mset_to_artnums($self->{es}, $mset);
62 my $ibx_id = $self->{-ibx_id} //= _ibx_id($self);
63 my $qmarks = join(',', map { '?' } @$docids);
64 if ($opt && ($opt->{relevance} // 0) == -1) { # -1 => ENQ_ASCENDING
67 if (my $r = $opt->{uid_range}) {
68 $range = 'AND xnum >= ? AND xnum <= ?';
71 my $rows = $self->{es}->over->dbh->
72 selectall_arrayref(<<"", undef, $ibx_id, @$docids, @r);
73 SELECT xnum FROM xref3 WHERE ibx_id = ? AND docid IN ($qmarks) $range
76 return [ map { $_->[0] } @$rows ];
79 my $rows = $self->{es}->over->dbh->
80 selectall_arrayref(<<"", undef, $ibx_id, @$docids);
81 SELECT docid,xnum FROM xref3 WHERE ibx_id = ? AND docid IN ($qmarks)
84 my %order = map { $_ => ++$i } @$docids;
86 for my $row (@$rows) { # @row = ($docid, $xnum)
87 my $idx = delete($order{$row->[0]}) // next;
88 $xnums[$idx] = $row->[1];
90 if (scalar keys %order) {
91 warn "W: $self->{es}->{topdir} #",
92 join(', ', sort { $a <=> $b } keys %order),
93 " not mapped to `$self->{eidx_key}'\n";
94 warn "W: $self->{es}->{topdir} may need to be reindexed\n";
95 @xnums = grep { defined } @xnums;
101 my ($self, $ibx, $mset) = @_; # $ibx is a real inbox, not eidx
102 my $xnums = mset_to_artnums($self, $mset);
104 my %order = map { $_ => ++$i } @$xnums;
105 my $unordered = $ibx->over->get_all(@$xnums);
107 for my $smsg (@$unordered) {
108 my $idx = delete($order{$smsg->{num}}) // do {
109 warn "W: $ibx->{inboxdir} #$smsg->{num}\n";
114 if (scalar keys %order) {
115 warn "W: $ibx->{inboxdir} #",
116 join(', ', sort { $a <=> $b } keys %order),
117 " no longer valid\n";
118 warn "W: $self->{es}->{topdir} may need to be reindexed\n";
120 wantarray ? ($mset->get_matches_estimated, \@msgs) : \@msgs;
123 sub has_threadid { 1 }
125 sub help { $_[0]->{es}->help }