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