]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MiscSearch.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / MiscSearch.pm
1 # Copyright (C) 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 our $ART_MIN = 2; # NNTP article number
15 our $ART_MAX = 3; # NNTP article number
16
17 # avoid conflicting with message Search::prob_prefix for UI/UX reasons
18 my %PROB_PREFIX = (
19         description => 'S', # $INBOX_DIR/description
20         address => 'A',
21         listid => 'XLISTID',
22         url => 'XURL',
23         infourl => 'XINFOURL',
24         name => 'XNAME',
25         '' => 'S A XLISTID XNAME XURL XINFOURL'
26 );
27
28 sub new {
29         my ($class, $dir) = @_;
30         PublicInbox::Search::load_xapian();
31         $json //= PublicInbox::Config::json();
32         bless {
33                 xdb => $PublicInbox::Search::X{Database}->new($dir)
34         }, $class;
35 }
36
37 # read-only
38 sub mi_qp_new ($) {
39         my ($self) = @_;
40         my $xdb = $self->{xdb};
41         my $qp = $PublicInbox::Search::X{QueryParser}->new;
42         $qp->set_default_op(PublicInbox::Search::OP_AND());
43         $qp->set_database($xdb);
44         $qp->set_stemmer(PublicInbox::Search::stemmer($self));
45         $qp->set_stemming_strategy(PublicInbox::Search::STEM_SOME());
46         my $cb = $qp->can('set_max_wildcard_expansion') //
47                 $qp->can('set_max_expansion'); # Xapian 1.5.0+
48         $cb->($qp, 100);
49         $cb = $qp->can('add_valuerangeprocessor') //
50                 $qp->can('add_rangeprocessor'); # Xapian 1.5.0+
51         while (my ($name, $prefix) = each %PROB_PREFIX) {
52                 $qp->add_prefix($name, $_) for split(/ /, $prefix);
53         }
54         $qp->add_boolean_prefix('type', 'T');
55         $qp;
56 }
57
58 sub misc_enquire_once { # retry_reopen callback
59         my ($self, $qr, $opt) = @_;
60         my $eq = $PublicInbox::Search::X{Enquire}->new($self->{xdb});
61         $eq->set_query($qr);
62         my $desc = !$opt->{asc};
63         my $rel = $opt->{relevance} // 0;
64         if ($rel == -1) { # ORDER BY docid
65                 $eq->set_docid_order($PublicInbox::Search::ENQ_ASCENDING);
66                 $eq->set_weighting_scheme($PublicInbox::Search::X{BoolWeight}->new);
67         } elsif ($rel) {
68                 $eq->set_sort_by_relevance_then_value($MODIFIED, $desc);
69         } else {
70                 $eq->set_sort_by_value_then_relevance($MODIFIED, $desc);
71         }
72         $eq->get_mset($opt->{offset} || 0, $opt->{limit} || 200);
73 }
74
75 sub mset {
76         my ($self, $qs, $opt) = @_;
77         $opt ||= {};
78         reopen($self);
79         my $qp = $self->{qp} //= mi_qp_new($self);
80         $qs = 'type:inbox' if $qs eq '';
81         my $qr = $qp->parse_query($qs, $PublicInbox::Search::QP_FLAGS);
82         $opt->{relevance} = 1 unless exists $opt->{relevance};
83         retry_reopen($self, \&misc_enquire_once, $qr, $opt);
84 }
85
86 sub ibx_data_once {
87         my ($self, $ibx) = @_;
88         my $xdb = $self->{xdb};
89         my $term = 'Q'.$ibx->eidx_key; # may be {inboxdir}, so private
90         my $head = $xdb->postlist_begin($term);
91         my $tail = $xdb->postlist_end($term);
92         return if $head == $tail;
93         my $doc = $xdb->get_document($head->get_docid);
94         $ibx->{uidvalidity} //= int_val($doc, $UIDVALIDITY);
95         $ibx->{-modified} = int_val($doc, $MODIFIED);
96         $ibx->{-art_min} = int_val($doc, $ART_MIN);
97         $ibx->{-art_max} = int_val($doc, $ART_MAX);
98         $doc->get_data;
99 }
100
101 sub doc2ibx_cache_ent { # @_ == ($self, $doc) OR ($doc)
102         my ($doc) = $_[-1];
103         my $d;
104         my $data = $json->decode($doc->get_data);
105         for (values %$data) {
106                 $d = $_->{description} // next;
107                 $d =~ s/ \[epoch [0-9]+\]\z// or next;
108                 last;
109         }
110         {
111                 uidvalidity => int_val($doc, $UIDVALIDITY),
112                 -modified => int_val($doc, $MODIFIED),
113                 -art_min => int_val($doc, $ART_MIN), # may be undef
114                 -art_max => int_val($doc, $ART_MAX), # may be undef
115                 # extract description from manifest.js.gz epoch description
116                 description => $d
117         };
118 }
119
120 sub inbox_data {
121         my ($self, $ibx) = @_;
122         retry_reopen($self, \&ibx_data_once, $ibx);
123 }
124
125 sub ibx_cache_load {
126         my ($doc, $cache) = @_;
127         my ($eidx_key) = xap_terms('Q', $doc);
128         return unless defined($eidx_key); # expired
129         $cache->{$eidx_key} = doc2ibx_cache_ent($doc);
130 }
131
132 sub _nntpd_cache_load { # retry_reopen callback
133         my ($self) = @_;
134         my $opt = { limit => $self->{xdb}->get_doccount * 10, relevance => -1 };
135         my $mset = mset($self, 'type:newsgroup type:inbox', $opt);
136         my $cache = {};
137         for my $it ($mset->items) {
138                 ibx_cache_load($it->get_document, $cache);
139         }
140         $cache
141 }
142
143 # returns { newsgroup => $cache_entry } mapping, $cache_entry contains
144 # anything which may trigger seeks at startup, currently: description,
145 # -modified, and uidvalidity.
146 sub nntpd_cache_load {
147         my ($self) = @_;
148         retry_reopen($self, \&_nntpd_cache_load);
149 }
150
151 no warnings 'once';
152 *reopen = \&PublicInbox::Search::reopen;
153
154 1;