1 # Copyright (C) 2020-2021 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";
28 sub query_approxidate { $_[0]->{es}->query_approxidate($_[1], $_[2]) }
31 my ($self, $str, $opt) = @_;
32 my %opt = $opt ? %$opt : ();
33 $opt{eidx_key} = $self->{eidx_key};
34 if (my $uid_range = $opt{uid_range}) {
35 my ($beg, $end) = @$uid_range;
36 my $ibx_id = $self->{-ibx_id} //= _ibx_id($self);
37 my $dbh = $self->{es}->over->dbh;
38 my $sth = $dbh->prepare_cached(<<'', undef, 1);
39 SELECT MIN(docid) FROM xref3 WHERE ibx_id = ? AND xnum >= ? AND xnum <= ?
41 $sth->execute($ibx_id, $beg, $end);
42 my @r = ($sth->fetchrow_array);
44 $sth = $dbh->prepare_cached(<<'', undef, 1);
45 SELECT MAX(docid) FROM xref3 WHERE ibx_id = ? AND xnum >= ? AND xnum <= ?
47 $sth->execute($ibx_id, $beg, $end);
48 $r[1] = $sth->fetchrow_array;
49 if (defined($r[1]) && defined($r[0])) {
50 $opt{limit} = $r[1] - $r[0] + 1;
55 $opt{uid_range} = \@r;
57 $self->{es}->mset($str, \%opt);
61 my ($self, $mset, $opt) = @_;
62 my $docids = PublicInbox::Search::mset_to_artnums($self->{es}, $mset);
63 my $ibx_id = $self->{-ibx_id} //= _ibx_id($self);
64 my $qmarks = join(',', map { '?' } @$docids);
65 if ($opt && ($opt->{relevance} // 0) == -1) { # -1 => ENQ_ASCENDING
68 if (my $r = $opt->{uid_range}) {
69 $range = 'AND xnum >= ? AND xnum <= ?';
72 my $rows = $self->{es}->over->dbh->
73 selectall_arrayref(<<"", undef, $ibx_id, @$docids, @r);
74 SELECT xnum FROM xref3 WHERE ibx_id = ? AND docid IN ($qmarks) $range
77 return [ map { $_->[0] } @$rows ];
80 my $rows = $self->{es}->over->dbh->
81 selectall_arrayref(<<"", undef, $ibx_id, @$docids);
82 SELECT docid,xnum FROM xref3 WHERE ibx_id = ? AND docid IN ($qmarks)
85 my %order = map { $_ => ++$i } @$docids;
87 for my $row (@$rows) { # @row = ($docid, $xnum)
88 my $idx = delete($order{$row->[0]}) // next;
89 $xnums[$idx] = $row->[1];
91 if (scalar keys %order) {
92 warn "W: $self->{es}->{topdir} #",
93 join(', ', sort { $a <=> $b } keys %order),
94 " not mapped to `$self->{eidx_key}'\n";
95 warn "W: $self->{es}->{topdir} may need to be reindexed\n";
96 @xnums = grep { defined } @xnums;
102 my ($self, $ibx, $mset) = @_; # $ibx is a real inbox, not eidx
103 my $xnums = mset_to_artnums($self, $mset);
105 my %order = map { $_ => ++$i } @$xnums;
106 my $unordered = $ibx->over->get_all(@$xnums);
108 for my $smsg (@$unordered) {
109 my $idx = delete($order{$smsg->{num}}) // do {
110 warn "W: $ibx->{inboxdir} #$smsg->{num}\n";
115 if (scalar keys %order) {
116 warn "W: $ibx->{inboxdir} #",
117 join(', ', sort { $a <=> $b } keys %order),
118 " no longer valid\n";
119 warn "W: $self->{es}->{topdir} may need to be reindexed\n";
121 wantarray ? ($mset->get_matches_estimated, \@msgs) : \@msgs;
124 sub has_threadid { 1 }
126 sub help { $_[0]->{es}->help }