]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ExtSearch.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / ExtSearch.pm
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>
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 PublicInbox::MiscSearch;
13 use DBI qw(:sql_types); # SQL_BLOB
14
15 # for ->reopen, ->mset, ->mset_to_artnums
16 use parent qw(PublicInbox::Search);
17
18 sub new {
19         my ($class, $topdir) = @_;
20         bless {
21                 topdir => $topdir,
22                 -primary_address => 'unknown@example.com',
23                 # xpfx => 'ei15'
24                 xpfx => "$topdir/ei".PublicInbox::Search::SCHEMA_VERSION
25         }, $class;
26 }
27
28 sub misc {
29         my ($self) = @_;
30         $self->{misc} //= PublicInbox::MiscSearch->new("$self->{xpfx}/misc");
31 }
32
33 # same as per-inbox ->over, for now...
34 sub over {
35         my ($self) = @_;
36         $self->{over} //= do {
37                 PublicInbox::Inbox::_cleanup_later($self);
38                 PublicInbox::Over->new("$self->{xpfx}/over.sqlite3");
39         };
40 }
41
42 sub git {
43         my ($self) = @_;
44         $self->{git} //= do {
45                 PublicInbox::Inbox::_cleanup_later($self);
46                 PublicInbox::Git->new("$self->{topdir}/ALL.git");
47         };
48 }
49
50 # returns a hashref of { $NEWSGROUP_NAME => $ART_NO } using the `xref3' table
51 sub nntp_xref_for { # NNTP only
52         my ($self, $xibx, $xsmsg) = @_;
53         my $dbh = over($self)->dbh;
54
55         my $sth = $dbh->prepare_cached(<<'', undef, 1);
56 SELECT ibx_id FROM inboxes WHERE eidx_key = ? LIMIT 1
57
58         $sth->execute($xibx->{newsgroup});
59         my $xibx_id = $sth->fetchrow_array // do {
60                 warn "W: `$xibx->{newsgroup}' not found in $self->{topdir}\n";
61                 return;
62         };
63
64         $sth = $dbh->prepare_cached(<<'', undef, 1);
65 SELECT docid FROM xref3 WHERE oidbin = ? AND xnum = ? AND ibx_id = ? LIMIT 1
66
67         $sth->bind_param(1, $xsmsg->oidbin, SQL_BLOB);
68
69         # NNTP::cmd_over can set {num} to zero according to RFC 3977 8.3.2
70         $sth->bind_param(2, $xsmsg->{num} || $xsmsg->{-orig_num});
71         $sth->bind_param(3, $xibx_id);
72         $sth->execute;
73         my $docid = $sth->fetchrow_array // do {
74                 warn <<EOF;
75 W: `$xibx->{newsgroup}:$xsmsg->{num}' not found in $self->{topdir}"
76 EOF
77                 return;
78         };
79
80         # LIMIT is number of newsgroups on server:
81         $sth = $dbh->prepare_cached(<<'', undef, 1);
82 SELECT ibx_id,xnum FROM xref3 WHERE docid = ? AND ibx_id != ?
83
84         $sth->execute($docid, $xibx_id);
85         my $rows = $sth->fetchall_arrayref;
86
87         my $eidx_key_sth = $dbh->prepare_cached(<<'', undef, 1);
88 SELECT eidx_key FROM inboxes WHERE ibx_id = ? LIMIT 1
89
90         my %xref = map {
91                 my ($ibx_id, $xnum) = @$_;
92
93                 $eidx_key_sth->execute($ibx_id);
94                 my $eidx_key = $eidx_key_sth->fetchrow_array;
95
96                 # only include if there's a newsgroup name
97                 $eidx_key && index($eidx_key, '/') >= 0 ?
98                         () : ($eidx_key => $xnum)
99         } @$rows;
100         $xref{$xibx->{newsgroup}} = $xsmsg->{num};
101         \%xref;
102 }
103
104 sub mm { undef }
105
106 sub altid_map { {} }
107
108 sub description {
109         my ($self) = @_;
110         ($self->{description} //=
111                 PublicInbox::Inbox::cat_desc("$self->{topdir}/description")) //
112                 '$EXTINDEX_DIR/description missing';
113 }
114
115 sub search {
116         PublicInbox::Inbox::_cleanup_later($_[0]);
117         $_[0];
118 }
119
120 no warnings 'once';
121 *base_url = \&PublicInbox::Inbox::base_url;
122 *smsg_eml = \&PublicInbox::Inbox::smsg_eml;
123 *smsg_by_mid = \&PublicInbox::Inbox::smsg_by_mid;
124 *msg_by_mid = \&PublicInbox::Inbox::msg_by_mid;
125 *modified = \&PublicInbox::Inbox::modified;
126 *recent = \&PublicInbox::Inbox::recent;
127
128 *max_git_epoch = *nntp_usable = *msg_by_path = \&mm; # undef
129 *isrch = \&search;
130
131 1;