]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Over.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / Over.pm
1 # Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # for XOVER, OVER in NNTP, and feeds/homepage/threads in PSGI
5 # Unlike Msgmap, this is an _UNSTABLE_ database which can be
6 # tweaked/updated over time and rebuilt.
7 package PublicInbox::Over;
8 use strict;
9 use warnings;
10 use DBI;
11 use DBD::SQLite;
12 use PublicInbox::SearchMsg;
13 use Compress::Zlib qw(uncompress);
14 use constant DEFAULT_LIMIT => 1000;
15
16 sub dbh_new {
17         my ($self, $rw) = @_;
18         my $f = $self->{filename};
19         if ($rw && !-f $f) { # SQLite defaults mode to 0644, we want 0666
20                 open my $fh, '+>>', $f or die "failed to open $f: $!";
21         }
22         my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
23                 AutoCommit => 1,
24                 RaiseError => 1,
25                 PrintError => 0,
26                 ReadOnly => !$rw,
27                 sqlite_use_immediate_transaction => 1,
28         });
29         $dbh->{sqlite_unicode} = 1;
30         $dbh;
31 }
32
33 sub new {
34         my ($class, $f) = @_;
35         bless { filename => $f }, $class;
36 }
37
38 sub disconnect { $_[0]->{dbh} = undef }
39
40 sub connect { $_[0]->{dbh} ||= $_[0]->dbh_new }
41
42 sub load_from_row ($;$) {
43         my ($smsg, $cull) = @_;
44         bless $smsg, 'PublicInbox::SearchMsg';
45         if (defined(my $data = delete $smsg->{ddd})) {
46                 $data = uncompress($data);
47                 utf8::decode($data);
48                 PublicInbox::SearchMsg::load_from_data($smsg, $data);
49
50                 # saves over 600K for 1000+ message threads
51                 PublicInbox::SearchMsg::psgi_cull($smsg) if $cull;
52         }
53         $smsg
54 }
55
56 sub do_get {
57         my ($self, $sql, $opts, @args) = @_;
58         my $dbh = $self->connect;
59         my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
60         $sql .= "LIMIT $lim";
61         my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
62         my $cull = $opts->{cull};
63         load_from_row($_, $cull) for @$msgs;
64         $msgs
65 }
66
67 sub query_xover {
68         my ($self, $beg, $end) = @_;
69         do_get($self, <<'', {}, $beg, $end);
70 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
71 ORDER BY num ASC
72
73 }
74
75 sub query_ts {
76         my ($self, $ts, $prev) = @_;
77         do_get($self, <<'', {}, $ts, $prev);
78 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
79 ORDER BY num ASC
80
81 }
82
83 sub nothing () { wantarray ? (0, []) : [] };
84
85 sub get_thread {
86         my ($self, $mid, $prev) = @_;
87         my $dbh = $self->connect;
88         my $opts = { cull => 1 };
89
90         my $id = $dbh->selectrow_array(<<'', undef, $mid);
91 SELECT id FROM msgid WHERE mid = ? LIMIT 1
92
93         defined $id or return nothing;
94
95         my $num = $dbh->selectrow_array(<<'', undef, $id);
96 SELECT num FROM id2num WHERE id = ? AND num > 0
97 ORDER BY num ASC LIMIT 1
98
99         defined $num or return nothing;
100
101         my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
102 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
103
104         defined $tid or return nothing; # $sid may be undef
105
106         my $cond_all = '(tid = ? OR sid = ?) AND num > ?';
107         my $sort_col = 'ds';
108         $num = 0;
109         if ($prev) { # mboxrd stream, only
110                 $num = $prev->{num} || 0;
111                 $sort_col = 'num';
112         }
113
114         my $cols = 'num,ts,ds,ddd';
115         unless (wantarray) {
116                 return do_get($self, <<"", $opts, $tid, $sid, $num);
117 SELECT $cols FROM over WHERE $cond_all
118 ORDER BY $sort_col ASC
119
120         }
121
122         # HTML view always wants an array and never uses $prev,
123         # but the mbox stream never wants an array and always has $prev
124         die '$prev not supported with wantarray' if $prev;
125         my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $num);
126 SELECT COUNT(num) FROM over WHERE $cond_all
127
128         # giant thread, prioritize strict (tid) matches and throw
129         # in the loose (sid) matches at the end
130         my $msgs = do_get($self, <<"", $opts, $tid, $num);
131 SELECT $cols FROM over WHERE tid = ? AND num > ?
132 ORDER BY $sort_col ASC
133
134         # do we have room for loose matches? get the most recent ones, first:
135         my $lim = DEFAULT_LIMIT - scalar(@$msgs);
136         if ($lim > 0) {
137                 $opts->{limit} = $lim;
138                 my $loose = do_get($self, <<"", $opts, $tid, $sid, $num);
139 SELECT $cols FROM over WHERE tid != ? AND sid = ? AND num > ?
140 ORDER BY $sort_col DESC
141
142                 # TODO separate strict and loose matches here once --reindex
143                 # is fixed to preserve `tid' properly
144                 push @$msgs, @$loose;
145         }
146         ($nr, $msgs);
147 }
148
149 sub recent {
150         my ($self, $opts, $after, $before) = @_;
151         my ($s, @v);
152         if (defined($before)) {
153                 if (defined($after)) {
154                         $s = '+num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
155                         @v = ($after, $before);
156                 } else {
157                         $s = '+num > 0 AND ts <= ? ORDER BY ts DESC';
158                         @v = ($before);
159                 }
160         } else {
161                 if (defined($after)) {
162                         $s = '+num > 0 AND ts >= ? ORDER BY ts ASC';
163                         @v = ($after);
164                 } else {
165                         $s = '+num > 0 ORDER BY ts DESC';
166                 }
167         }
168         my $msgs = do_get($self, <<"", $opts, @v);
169 SELECT ts,ds,ddd FROM over WHERE $s
170
171         return $msgs unless wantarray;
172
173         my $nr = $self->{dbh}->selectrow_array(<<'');
174 SELECT COUNT(num) FROM over WHERE num > 0
175
176         ($nr, $msgs);
177 }
178
179 sub get_art {
180         my ($self, $num) = @_;
181         my $dbh = $self->connect;
182         my $smsg = $dbh->selectrow_hashref(<<'', undef, $num);
183 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
184
185         return load_from_row($smsg) if $smsg;
186         undef;
187 }
188
189 sub next_by_mid {
190         my ($self, $mid, $id, $prev) = @_;
191         my $dbh = $self->connect;
192
193         unless (defined $$id) {
194                 my $sth = $dbh->prepare_cached(<<'', undef, 1);
195         SELECT id FROM msgid WHERE mid = ? LIMIT 1
196
197                 $sth->execute($mid);
198                 $$id = $sth->fetchrow_array;
199                 defined $$id or return;
200         }
201         my $sth = $dbh->prepare_cached(<<"", undef, 1);
202 SELECT num FROM id2num WHERE id = ? AND num > ?
203 ORDER BY num ASC LIMIT 1
204
205         $$prev ||= 0;
206         $sth->execute($$id, $$prev);
207         my $num = $sth->fetchrow_array or return;
208         $$prev = $num;
209
210         $sth = $dbh->prepare_cached(<<"", undef, 1);
211 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
212
213         $sth->execute($num);
214         my $smsg = $sth->fetchrow_hashref or return;
215         load_from_row($smsg);
216 }
217
218 1;