]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtSearch.pm
20ec322475de99808b3ba80113dbdb9a924292a1
[public-inbox.git] / lib / PublicInbox / ExtSearch.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Read-only external (detached) index for cross inbox search.
5 # This is a read-only counterpart to PublicInbox::ExtSearchIdx
6 # and behaves like PublicInbox::Inbox AND PublicInbox::Search
7 package PublicInbox::ExtSearch;
8 use strict;
9 use v5.10.1;
10 use PublicInbox::Over;
11 use PublicInbox::Inbox;
12 use File::Spec ();
13 use PublicInbox::MiscSearch;
14 use DBI qw(:sql_types); # SQL_BLOB
15
16 # for ->reopen, ->mset, ->mset_to_artnums
17 use parent qw(PublicInbox::Search);
18
19 sub new {
20         my (undef, $topdir) = @_;
21         $topdir = File::Spec->canonpath($topdir);
22         bless {
23                 topdir => $topdir,
24                 # xpfx => 'ei15'
25                 xpfx => "$topdir/ei".PublicInbox::Search::SCHEMA_VERSION
26         }, __PACKAGE__;
27 }
28
29 sub misc {
30         my ($self) = @_;
31         $self->{misc} //= PublicInbox::MiscSearch->new("$self->{xpfx}/misc");
32 }
33
34 sub search { $_[0] } # self
35
36 # overrides PublicInbox::Search::_xdb
37 sub _xdb {
38         my ($self) = @_;
39         $self->xdb_sharded;
40 }
41
42 # same as per-inbox ->over, for now...
43 sub over {
44         my ($self) = @_;
45         $self->{over} //= PublicInbox::Over->new("$self->{xpfx}/over.sqlite3");
46 }
47
48 sub git {
49         my ($self) = @_;
50         $self->{git} //= PublicInbox::Git->new("$self->{topdir}/ALL.git");
51 }
52
53 # returns an arrayref of [ $NEWSGROUP_NAME:$ART_NO ] using
54 # the `xref3' table
55 sub nntp_xref_for { # NNTP only
56         my ($self, $xibx, $xsmsg) = @_;
57         my $dbh = over($self)->dbh;
58
59         my $sth = $dbh->prepare_cached(<<'', undef, 1);
60 SELECT ibx_id FROM inboxes WHERE eidx_key = ? LIMIT 1
61
62         $sth->execute($xibx->{newsgroup});
63         my $xibx_id = $sth->fetchrow_array // do {
64                 warn "W: `$xibx->{newsgroup}' not found in $self->{topdir}\n";
65                 return;
66         };
67
68         $sth = $dbh->prepare_cached(<<'', undef, 1);
69 SELECT docid FROM xref3 WHERE oidbin = ? AND xnum = ? AND ibx_id = ? LIMIT 1
70
71         $sth->bind_param(1, pack('H*', $xsmsg->{blob}), SQL_BLOB);
72         $sth->bind_param(2, $xsmsg->{num});
73         $sth->bind_param(3, $xibx_id);
74         $sth->execute;
75         my $docid = $sth->fetchrow_array // do {
76                 warn <<EOF;
77 W: `$xibx->{newsgroup}:$xsmsg->{num}' not found in $self->{topdir}"
78 EOF
79                 return;
80         };
81
82         # LIMIT is number of newsgroups on server:
83         $sth = $dbh->prepare_cached(<<'', undef, 1);
84 SELECT ibx_id,xnum FROM xref3 WHERE docid = ?
85
86         $sth->execute($docid);
87         my $rows = $sth->fetchall_arrayref;
88
89         my $eidx_key_sth = $dbh->prepare_cached(<<'', undef, 1);
90 SELECT eidx_key FROM inboxes WHERE ibx_id = ? LIMIT 1
91
92         my %xref = map {
93                 my ($ibx_id, $xnum) = @$_;
94                 if ($ibx_id == $xibx_id) {
95                         ();
96                 } else {
97                         $eidx_key_sth->execute($ibx_id);
98                         my $eidx_key = $eidx_key_sth->fetchrow_array;
99
100                         # only include if there's a newsgroup name
101                         $eidx_key && index($eidx_key, '/') >= 0 ?
102                                 () : ($eidx_key => $xnum)
103                 }
104         } @$rows;
105         [ map { "$_:$xref{$_}" } sort keys %xref ]; # match NNTP LIST order
106 }
107
108 sub mm { undef }
109
110 sub altid_map { {} }
111
112 sub description {
113         my ($self) = @_;
114         ($self->{description} //=
115                 PublicInbox::Inbox::cat_desc("$self->{topdir}/description")) //
116                 '$EXTINDEX_DIR/description missing';
117 }
118
119 sub cloneurl { [] } # TODO
120
121 sub base_url { 'https://example.com/TODO/' }
122 sub nntp_url { [] }
123
124 no warnings 'once';
125 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
126 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
127 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
128 *modified = \&PublicInbox::Inbox::modified;
129 *recent = \&PublicInbox::Inbox::recent;
130
131 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
132
133 1;