]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Over.pm
over: unset sqlite_unicode attribute
[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::Smsg;
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 (@st, $st, $dbh);
23         my $tries = 0;
24         do {
25                 @st = stat($f) or die "failed to stat $f: $!";
26                 $st = pack('dd', $st[0], $st[1]); # 0: dev, 1: inode
27                 $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
28                         AutoCommit => 1,
29                         RaiseError => 1,
30                         PrintError => 0,
31                         ReadOnly => !$rw,
32                         sqlite_use_immediate_transaction => 1,
33                 });
34                 $self->{st} = $st;
35                 @st = stat($f) or die "failed to stat $f: $!";
36                 $st = pack('dd', $st[0], $st[1]);
37         } while ($st ne $self->{st} && $tries++ < 3);
38         warn "W: $f: .st_dev, .st_ino unstable\n" if $st ne $self->{st};
39         $dbh;
40 }
41
42 sub new {
43         my ($class, $f) = @_;
44         bless { filename => $f }, $class;
45 }
46
47 sub disconnect { $_[0]->{dbh} = undef }
48
49 sub connect { $_[0]->{dbh} ||= $_[0]->dbh_new }
50
51 sub load_from_row ($;$) {
52         my ($smsg, $cull) = @_;
53         bless $smsg, 'PublicInbox::Smsg';
54         if (defined(my $data = delete $smsg->{ddd})) {
55                 $data = uncompress($data);
56                 utf8::decode($data);
57                 PublicInbox::Smsg::load_from_data($smsg, $data);
58
59                 # saves over 600K for 1000+ message threads
60                 PublicInbox::Smsg::psgi_cull($smsg) if $cull;
61         }
62         $smsg
63 }
64
65 sub do_get {
66         my ($self, $sql, $opts, @args) = @_;
67         my $dbh = $self->connect;
68         my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
69         $sql .= "LIMIT $lim";
70         my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
71         my $cull = $opts->{cull};
72         load_from_row($_, $cull) for @$msgs;
73         $msgs
74 }
75
76 sub query_xover {
77         my ($self, $beg, $end) = @_;
78         do_get($self, <<'', {}, $beg, $end);
79 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
80 ORDER BY num ASC
81
82 }
83
84 sub query_ts {
85         my ($self, $ts, $prev) = @_;
86         do_get($self, <<'', {}, $ts, $prev);
87 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
88 ORDER BY num ASC
89
90 }
91
92 sub nothing () { wantarray ? (0, []) : [] };
93
94 sub get_thread {
95         my ($self, $mid, $prev) = @_;
96         my $dbh = $self->connect;
97         my $opts = { cull => 1 };
98
99         my $id = $dbh->selectrow_array(<<'', undef, $mid);
100 SELECT id FROM msgid WHERE mid = ? LIMIT 1
101
102         defined $id or return nothing;
103
104         my $num = $dbh->selectrow_array(<<'', undef, $id);
105 SELECT num FROM id2num WHERE id = ? AND num > 0
106 ORDER BY num ASC LIMIT 1
107
108         defined $num or return nothing;
109
110         my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
111 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
112
113         defined $tid or return nothing; # $sid may be undef
114
115         my $cond_all = '(tid = ? OR sid = ?) AND num > ?';
116         my $sort_col = 'ds';
117         $num = 0;
118         if ($prev) { # mboxrd stream, only
119                 $num = $prev->{num} || 0;
120                 $sort_col = 'num';
121         }
122
123         my $cols = 'num,ts,ds,ddd';
124         unless (wantarray) {
125                 return do_get($self, <<"", $opts, $tid, $sid, $num);
126 SELECT $cols FROM over WHERE $cond_all
127 ORDER BY $sort_col ASC
128
129         }
130
131         # HTML view always wants an array and never uses $prev,
132         # but the mbox stream never wants an array and always has $prev
133         die '$prev not supported with wantarray' if $prev;
134         my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $num);
135 SELECT COUNT(num) FROM over WHERE $cond_all
136
137         # giant thread, prioritize strict (tid) matches and throw
138         # in the loose (sid) matches at the end
139         my $msgs = do_get($self, <<"", $opts, $tid, $num);
140 SELECT $cols FROM over WHERE tid = ? AND num > ?
141 ORDER BY $sort_col ASC
142
143         # do we have room for loose matches? get the most recent ones, first:
144         my $lim = DEFAULT_LIMIT - scalar(@$msgs);
145         if ($lim > 0) {
146                 $opts->{limit} = $lim;
147                 my $loose = do_get($self, <<"", $opts, $tid, $sid, $num);
148 SELECT $cols FROM over WHERE tid != ? AND sid = ? AND num > ?
149 ORDER BY $sort_col DESC
150
151                 # TODO separate strict and loose matches here once --reindex
152                 # is fixed to preserve `tid' properly
153                 push @$msgs, @$loose;
154         }
155         ($nr, $msgs);
156 }
157
158 sub recent {
159         my ($self, $opts, $after, $before) = @_;
160         my ($s, @v);
161         if (defined($before)) {
162                 if (defined($after)) {
163                         $s = '+num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
164                         @v = ($after, $before);
165                 } else {
166                         $s = '+num > 0 AND ts <= ? ORDER BY ts DESC';
167                         @v = ($before);
168                 }
169         } else {
170                 if (defined($after)) {
171                         $s = '+num > 0 AND ts >= ? ORDER BY ts ASC';
172                         @v = ($after);
173                 } else {
174                         $s = '+num > 0 ORDER BY ts DESC';
175                 }
176         }
177         my $msgs = do_get($self, <<"", $opts, @v);
178 SELECT ts,ds,ddd FROM over WHERE $s
179
180         return $msgs unless wantarray;
181
182         my $nr = $self->{dbh}->selectrow_array(<<'');
183 SELECT COUNT(num) FROM over WHERE num > 0
184
185         ($nr, $msgs);
186 }
187
188 sub get_art {
189         my ($self, $num) = @_;
190         my $dbh = $self->connect;
191         my $sth = $dbh->prepare_cached(<<'', undef, 1);
192 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
193
194         $sth->execute($num);
195         my $smsg = $sth->fetchrow_hashref;
196         $smsg ? load_from_row($smsg) : undef;
197 }
198
199 sub next_by_mid {
200         my ($self, $mid, $id, $prev) = @_;
201         my $dbh = $self->connect;
202
203         unless (defined $$id) {
204                 my $sth = $dbh->prepare_cached(<<'', undef, 1);
205         SELECT id FROM msgid WHERE mid = ? LIMIT 1
206
207                 $sth->execute($mid);
208                 $$id = $sth->fetchrow_array;
209                 defined $$id or return;
210         }
211         my $sth = $dbh->prepare_cached(<<"", undef, 1);
212 SELECT num FROM id2num WHERE id = ? AND num > ?
213 ORDER BY num ASC LIMIT 1
214
215         $$prev ||= 0;
216         $sth->execute($$id, $$prev);
217         my $num = $sth->fetchrow_array or return;
218         $$prev = $num;
219
220         $sth = $dbh->prepare_cached(<<"", undef, 1);
221 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
222
223         $sth->execute($num);
224         my $smsg = $sth->fetchrow_hashref or return;
225         load_from_row($smsg);
226 }
227
228 # IMAP search, this is limited by callers to UID_SLICE size (50K)
229 sub uid_range {
230         my ($self, $beg, $end, $sql) = @_;
231         my $dbh = $self->connect;
232         my $q = 'SELECT num FROM over WHERE num >= ? AND num <= ?';
233
234         # This is read-only, anyways; but caller should verify it's
235         # only sending \A[0-9]+\z for ds and ts column ranges
236         $q .= $$sql if $sql;
237         $q .= ' ORDER BY num ASC';
238         $dbh->selectcol_arrayref($q, undef, $beg, $end);
239 }
240
241 sub max {
242         my ($self) = @_;
243         my $sth = $self->connect->prepare_cached(<<'', undef, 1);
244 SELECT MAX(num) FROM over WHERE num > 0
245
246         $sth->execute;
247         $sth->fetchrow_array // 0;
248 }
249
250 sub imap_exists {
251         my ($self, $uid_base, $uid_end) = @_;
252         my $sth = $self->connect->prepare_cached(<<'', undef, 1);
253 SELECT COUNT(num) FROM over WHERE num > ? AND num <= ?
254
255         $sth->execute($uid_base, $uid_end);
256         $sth->fetchrow_array;
257 }
258
259 sub check_inodes {
260         my ($self) = @_;
261         if (my @st = stat($self->{filename})) { # did st_dev, st_ino change?
262                 my $st = pack('dd', $st[0], $st[1]);
263
264                 # don't actually reopen, just let {dbh} be recreated later
265                 delete($self->{dbh}) if ($st ne ($self->{st} // $st));
266         } else {
267                 warn "W: stat $self->{filename}: $!\n";
268         }
269 }
270
271 1;