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