]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MiscSearch.pm
6b575b0da083246bbc2c0e0fe7a9104a1af2bed4
[public-inbox.git] / lib / PublicInbox / MiscSearch.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 counterpart to MiscIdx
5 package PublicInbox::MiscSearch;
6 use strict;
7 use v5.10.1;
8 use PublicInbox::Search qw(retry_reopen int_val xap_terms);
9 my $json;
10
11 # Xapian value columns:
12 our $MODIFIED = 0;
13 our $UIDVALIDITY = 1; # (created time)
14
15 # avoid conflicting with message Search::prob_prefix for UI/UX reasons
16 my %PROB_PREFIX = (
17         description => 'S', # $INBOX_DIR/description
18         address => 'A',
19         listid => 'XLISTID',
20         url => 'XURL',
21         infourl => 'XINFOURL',
22         name => 'XNAME',
23         '' => 'S A XLISTID XNAME XURL XINFOURL'
24 );
25
26 sub new {
27         my ($class, $dir) = @_;
28         PublicInbox::Search::load_xapian();
29         $json //= PublicInbox::Config::json();
30         bless {
31                 xdb => $PublicInbox::Search::X{Database}->new($dir)
32         }, $class;
33 }
34
35 # read-only
36 sub mi_qp_new ($) {
37         my ($self) = @_;
38         my $xdb = $self->{xdb};
39         my $qp = $PublicInbox::Search::X{QueryParser}->new;
40         $qp->set_default_op(PublicInbox::Search::OP_AND());
41         $qp->set_database($xdb);
42         $qp->set_stemmer(PublicInbox::Search::stemmer($self));
43         $qp->set_stemming_strategy(PublicInbox::Search::STEM_SOME());
44         my $cb = $qp->can('set_max_wildcard_expansion') //
45                 $qp->can('set_max_expansion'); # Xapian 1.5.0+
46         $cb->($qp, 100);
47         $cb = $qp->can('add_valuerangeprocessor') //
48                 $qp->can('add_rangeprocessor'); # Xapian 1.5.0+
49         while (my ($name, $prefix) = each %PROB_PREFIX) {
50                 $qp->add_prefix($name, $_) for split(/ /, $prefix);
51         }
52         $qp->add_boolean_prefix('type', 'T');
53         $qp;
54 }
55
56 sub misc_enquire_once { # retry_reopen callback
57         my ($self, $qr, $opt) = @_;
58         my $eq = $PublicInbox::Search::X{Enquire}->new($self->{xdb});
59         $eq->set_query($qr);
60         my $desc = !$opt->{asc};
61         my $rel = $opt->{relevance} // 0;
62         if ($rel == -1) { # ORDER BY docid
63                 $eq->set_docid_order($PublicInbox::Search::ENQ_ASCENDING);
64                 $eq->set_weighting_scheme($PublicInbox::Search::X{BoolWeight}->new);
65         } elsif ($rel) {
66                 $eq->set_sort_by_relevance_then_value($MODIFIED, $desc);
67         } else {
68                 $eq->set_sort_by_value_then_relevance($MODIFIED, $desc);
69         }
70         $eq->get_mset($opt->{offset} || 0, $opt->{limit} || 200);
71 }
72
73 sub mset {
74         my ($self, $qs, $opt) = @_;
75         $opt ||= {};
76         reopen($self);
77         my $qp = $self->{qp} //= mi_qp_new($self);
78         $qs = 'type:inbox' if $qs eq '';
79         my $qr = $qp->parse_query($qs, $PublicInbox::Search::QP_FLAGS);
80         $opt->{relevance} = 1 unless exists $opt->{relevance};
81         retry_reopen($self, \&misc_enquire_once, $qr, $opt);
82 }
83
84 sub ibx_matches_once { # retry_reopen callback
85         my ($self, $qr, $by_newsgroup) = @_;
86         # double in case no newsgroups are configured:
87         my $limit = scalar(keys %$by_newsgroup) * 2;
88         my $opt = { limit => $limit, offset => 0, relevance => -1 };
89         my $ret = {}; # newsgroup => $ibx of matches
90         while (1) {
91                 my $mset = misc_enquire_once($self, $qr, $opt);
92                 for my $mi ($mset->items) {
93                         my ($eidx_key) = xap_terms('Q', $mi->get_document);
94                         if (defined($eidx_key)) {
95                                 if (my $ibx = $by_newsgroup->{$eidx_key}) {
96                                         $ret->{$eidx_key} = $ibx;
97                                 }
98                         } else {
99                                 warn <<EOF;
100 W: docid=${\$mi->get_docid} has no `Q' (eidx_key) term
101 EOF
102                         }
103                 }
104                 my $nr = $mset->size;
105                 return $ret if $nr < $limit;
106                 $opt->{offset} += $nr;
107         }
108 }
109
110 # returns a newsgroup => PublicInbox::Inbox mapping
111 sub newsgroup_matches {
112         my ($self, $qs, $pi_cfg) = @_;
113         my $qp = $self->{qp} //= mi_qp_new($self);
114         $qs .= ' type:inbox';
115         my $qr = $qp->parse_query($qs, $PublicInbox::Search::QP_FLAGS);
116         retry_reopen($self, \&ibx_matches_once, $qr, $pi_cfg->{-by_newsgroup});
117 }
118
119 sub ibx_data_once {
120         my ($self, $ibx) = @_;
121         my $xdb = $self->{xdb};
122         my $term = 'Q'.$ibx->eidx_key; # may be {inboxdir}, so private
123         my $head = $xdb->postlist_begin($term);
124         my $tail = $xdb->postlist_end($term);
125         if ($head != $tail) {
126                 my $doc = $xdb->get_document($head->get_docid);
127                 $ibx->{uidvalidity} //= int_val($doc, $UIDVALIDITY);
128                 $ibx->{-modified} = int_val($doc, $MODIFIED);
129                 $doc->get_data;
130         } else {
131                 undef;
132         }
133 }
134
135 sub doc2ibx_cache_ent { # @_ == ($self, $doc) OR ($doc)
136         my ($doc) = $_[-1];
137         my $d;
138         my $data = $json->decode($doc->get_data);
139         for (values %$data) {
140                 $d = $_->{description} // next;
141                 $d =~ s/ \[epoch [0-9]+\]\z// or next;
142                 last;
143         }
144         {
145                 uidvalidity => int_val($doc, $UIDVALIDITY),
146                 -modified => int_val($doc, $MODIFIED),
147                 # extract description from manifest.js.gz epoch description
148                 description => $d
149         };
150 }
151
152 sub inbox_data {
153         my ($self, $ibx) = @_;
154         retry_reopen($self, \&ibx_data_once, $ibx);
155 }
156
157 sub ibx_cache_load {
158         my ($doc, $cache) = @_;
159         my ($eidx_key) = xap_terms('Q', $doc);
160         return unless defined($eidx_key); # expired
161         $cache->{$eidx_key} = doc2ibx_cache_ent($doc);
162 }
163
164 sub _nntpd_cache_load { # retry_reopen callback
165         my ($self) = @_;
166         my $opt = { limit => $self->{xdb}->get_doccount * 10, relevance => -1 };
167         my $mset = mset($self, 'type:newsgroup type:inbox', $opt);
168         my $cache = {};
169         for my $it ($mset->items) {
170                 ibx_cache_load($it->get_document, $cache);
171         }
172         $cache
173 }
174
175 # returns { newsgroup => $cache_entry } mapping, $cache_entry contains
176 # anything which may trigger seeks at startup, currently: description,
177 # -modified, and uidvalidity.
178 sub nntpd_cache_load {
179         my ($self) = @_;
180         retry_reopen($self, \&_nntpd_cache_load);
181 }
182
183 no warnings 'once';
184 *reopen = \&PublicInbox::Search::reopen;
185
186 1;