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>
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;
12 use PublicInbox::Smsg;
13 use Compress::Zlib qw(uncompress);
14 use constant DEFAULT_LIMIT => 1000;
18 my $f = delete $self->{filename};
19 if (!-f $f) { # SQLite defaults mode to 0644, we want 0666
21 require PublicInbox::Spawn;
22 open my $fh, '+>>', $f or die "failed to open $f: $!";
23 PublicInbox::Spawn::nodatacow_fd(fileno($fh));
25 open $fh, '+>>', $j or die "failed to open $j: $!";
26 PublicInbox::Spawn::nodatacow_fd(fileno($fh));
28 $self->{filename} = $f; # die on stat() below:
34 @st = stat($f) or die "failed to stat $f: $!";
35 $st = pack('dd', $st[0], $st[1]); # 0: dev, 1: inode
36 $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
41 sqlite_use_immediate_transaction => 1,
44 @st = stat($f) or die "failed to stat $f: $!";
45 $st = pack('dd', $st[0], $st[1]);
46 } while ($st ne $self->{st} && $tries++ < 3);
47 warn "W: $f: .st_dev, .st_ino unstable\n" if $st ne $self->{st};
48 $dbh->do('PRAGMA synchronous = OFF') if ($rw // 0) > 1;
54 bless { filename => $f }, $class;
59 if (my $dbh = delete $self->{dbh}) {
60 $self->{filename} = $dbh->sqlite_db_filename;
64 sub connect { $_[0]->{dbh} //= $_[0]->dbh_new }
66 sub load_from_row ($;$) {
67 my ($smsg, $cull) = @_;
68 bless $smsg, 'PublicInbox::Smsg';
69 if (defined(my $data = delete $smsg->{ddd})) {
70 $data = uncompress($data);
71 PublicInbox::Smsg::load_from_data($smsg, $data);
73 # saves over 600K for 1000+ message threads
74 PublicInbox::Smsg::psgi_cull($smsg) if $cull;
80 my ($self, $sql, $opts, @args) = @_;
81 my $dbh = $self->connect;
82 my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
84 my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
85 my $cull = $opts->{cull};
86 load_from_row($_, $cull) for @$msgs;
91 my ($self, $beg, $end) = @_;
92 do_get($self, <<'', {}, $beg, $end);
93 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
99 my ($self, $ts, $prev) = @_;
100 do_get($self, <<'', {}, $ts, $prev);
101 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
106 sub nothing () { wantarray ? (0, []) : [] };
109 my ($self, $mid, $prev) = @_;
110 my $dbh = $self->connect;
111 my $opts = { cull => 1 };
113 my $id = $dbh->selectrow_array(<<'', undef, $mid);
114 SELECT id FROM msgid WHERE mid = ? LIMIT 1
116 defined $id or return nothing;
118 my $num = $dbh->selectrow_array(<<'', undef, $id);
119 SELECT num FROM id2num WHERE id = ? AND num > 0
120 ORDER BY num ASC LIMIT 1
122 defined $num or return nothing;
124 my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
125 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
127 defined $tid or return nothing; # $sid may be undef
129 my $cond_all = '(tid = ? OR sid = ?) AND num > ?';
132 if ($prev) { # mboxrd stream, only
133 $num = $prev->{num} || 0;
137 my $cols = 'num,ts,ds,ddd';
139 return do_get($self, <<"", $opts, $tid, $sid, $num);
140 SELECT $cols FROM over WHERE $cond_all
141 ORDER BY $sort_col ASC
145 # HTML view always wants an array and never uses $prev,
146 # but the mbox stream never wants an array and always has $prev
147 die '$prev not supported with wantarray' if $prev;
148 my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $num);
149 SELECT COUNT(num) FROM over WHERE $cond_all
151 # giant thread, prioritize strict (tid) matches and throw
152 # in the loose (sid) matches at the end
153 my $msgs = do_get($self, <<"", $opts, $tid, $num);
154 SELECT $cols FROM over WHERE tid = ? AND num > ?
155 ORDER BY $sort_col ASC
157 # do we have room for loose matches? get the most recent ones, first:
158 my $lim = DEFAULT_LIMIT - scalar(@$msgs);
160 $opts->{limit} = $lim;
161 my $loose = do_get($self, <<"", $opts, $tid, $sid, $num);
162 SELECT $cols FROM over WHERE tid != ? AND sid = ? AND num > ?
163 ORDER BY $sort_col DESC
165 # TODO separate strict and loose matches here once --reindex
166 # is fixed to preserve `tid' properly
167 push @$msgs, @$loose;
173 my ($self, $opts, $after, $before) = @_;
175 if (defined($before)) {
176 if (defined($after)) {
177 $s = '+num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
178 @v = ($after, $before);
180 $s = '+num > 0 AND ts <= ? ORDER BY ts DESC';
184 if (defined($after)) {
185 $s = '+num > 0 AND ts >= ? ORDER BY ts ASC';
188 $s = '+num > 0 ORDER BY ts DESC';
191 my $msgs = do_get($self, <<"", $opts, @v);
192 SELECT ts,ds,ddd FROM over WHERE $s
194 return $msgs unless wantarray;
196 my $nr = $self->{dbh}->selectrow_array(<<'');
197 SELECT COUNT(num) FROM over WHERE num > 0
203 my ($self, $num) = @_;
204 my $dbh = $self->connect;
205 my $sth = $dbh->prepare_cached(<<'', undef, 1);
206 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
209 my $smsg = $sth->fetchrow_hashref;
210 $smsg ? load_from_row($smsg) : undef;
214 my ($self, $mid, $id, $prev) = @_;
215 my $dbh = $self->connect;
217 unless (defined $$id) {
218 my $sth = $dbh->prepare_cached(<<'', undef, 1);
219 SELECT id FROM msgid WHERE mid = ? LIMIT 1
222 $$id = $sth->fetchrow_array;
223 defined $$id or return;
225 my $sth = $dbh->prepare_cached(<<"", undef, 1);
226 SELECT num FROM id2num WHERE id = ? AND num > ?
227 ORDER BY num ASC LIMIT 1
230 $sth->execute($$id, $$prev);
231 my $num = $sth->fetchrow_array or return;
234 $sth = $dbh->prepare_cached(<<"", undef, 1);
235 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
238 my $smsg = $sth->fetchrow_hashref or return;
239 load_from_row($smsg);
242 # IMAP search, this is limited by callers to UID_SLICE size (50K)
244 my ($self, $beg, $end, $sql) = @_;
245 my $dbh = $self->connect;
246 my $q = 'SELECT num FROM over WHERE num >= ? AND num <= ?';
248 # This is read-only, anyways; but caller should verify it's
249 # only sending \A[0-9]+\z for ds and ts column ranges
251 $q .= ' ORDER BY num ASC';
252 $dbh->selectcol_arrayref($q, undef, $beg, $end);
257 my $sth = $self->connect->prepare_cached(<<'', undef, 1);
258 SELECT MAX(num) FROM over WHERE num > 0
261 $sth->fetchrow_array // 0;
265 my ($self, $uid_base, $uid_end) = @_;
266 my $sth = $self->connect->prepare_cached(<<'', undef, 1);
267 SELECT COUNT(num) FROM over WHERE num > ? AND num <= ?
269 $sth->execute($uid_base, $uid_end);
270 $sth->fetchrow_array;
275 my $dbh = $self->{dbh} or return;
276 my $f = $dbh->sqlite_db_filename;
277 if (my @st = stat($f)) { # did st_dev, st_ino change?
278 my $st = pack('dd', $st[0], $st[1]);
280 # don't actually reopen, just let {dbh} be recreated later
281 if ($st ne ($self->{st} // $st)) {
282 delete($self->{dbh});
283 $self->{filename} = $f;
286 warn "W: stat $f: $!\n";