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 open my $fh, '+>>', $f or die "failed to open $f: $!";
23 $self->{filename} = $f; # die on stat() below:
29 @st = stat($f) or die "failed to stat $f: $!";
30 $st = pack('dd', $st[0], $st[1]); # 0: dev, 1: inode
31 $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
36 sqlite_use_immediate_transaction => 1,
39 @st = stat($f) or die "failed to stat $f: $!";
40 $st = pack('dd', $st[0], $st[1]);
41 } while ($st ne $self->{st} && $tries++ < 3);
42 warn "W: $f: .st_dev, .st_ino unstable\n" if $st ne $self->{st};
43 $dbh->do('PRAGMA synchronous = OFF') if ($rw // 0) > 1;
49 bless { filename => $f }, $class;
54 if (my $dbh = delete $self->{dbh}) {
55 $self->{filename} = $dbh->sqlite_db_filename;
59 sub connect { $_[0]->{dbh} //= $_[0]->dbh_new }
61 sub load_from_row ($;$) {
62 my ($smsg, $cull) = @_;
63 bless $smsg, 'PublicInbox::Smsg';
64 if (defined(my $data = delete $smsg->{ddd})) {
65 $data = uncompress($data);
67 PublicInbox::Smsg::load_from_data($smsg, $data);
69 # saves over 600K for 1000+ message threads
70 PublicInbox::Smsg::psgi_cull($smsg) if $cull;
76 my ($self, $sql, $opts, @args) = @_;
77 my $dbh = $self->connect;
78 my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
80 my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
81 my $cull = $opts->{cull};
82 load_from_row($_, $cull) for @$msgs;
87 my ($self, $beg, $end) = @_;
88 do_get($self, <<'', {}, $beg, $end);
89 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
95 my ($self, $ts, $prev) = @_;
96 do_get($self, <<'', {}, $ts, $prev);
97 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
102 sub nothing () { wantarray ? (0, []) : [] };
105 my ($self, $mid, $prev) = @_;
106 my $dbh = $self->connect;
107 my $opts = { cull => 1 };
109 my $id = $dbh->selectrow_array(<<'', undef, $mid);
110 SELECT id FROM msgid WHERE mid = ? LIMIT 1
112 defined $id or return nothing;
114 my $num = $dbh->selectrow_array(<<'', undef, $id);
115 SELECT num FROM id2num WHERE id = ? AND num > 0
116 ORDER BY num ASC LIMIT 1
118 defined $num or return nothing;
120 my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
121 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
123 defined $tid or return nothing; # $sid may be undef
125 my $cond_all = '(tid = ? OR sid = ?) AND num > ?';
128 if ($prev) { # mboxrd stream, only
129 $num = $prev->{num} || 0;
133 my $cols = 'num,ts,ds,ddd';
135 return do_get($self, <<"", $opts, $tid, $sid, $num);
136 SELECT $cols FROM over WHERE $cond_all
137 ORDER BY $sort_col ASC
141 # HTML view always wants an array and never uses $prev,
142 # but the mbox stream never wants an array and always has $prev
143 die '$prev not supported with wantarray' if $prev;
144 my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $num);
145 SELECT COUNT(num) FROM over WHERE $cond_all
147 # giant thread, prioritize strict (tid) matches and throw
148 # in the loose (sid) matches at the end
149 my $msgs = do_get($self, <<"", $opts, $tid, $num);
150 SELECT $cols FROM over WHERE tid = ? AND num > ?
151 ORDER BY $sort_col ASC
153 # do we have room for loose matches? get the most recent ones, first:
154 my $lim = DEFAULT_LIMIT - scalar(@$msgs);
156 $opts->{limit} = $lim;
157 my $loose = do_get($self, <<"", $opts, $tid, $sid, $num);
158 SELECT $cols FROM over WHERE tid != ? AND sid = ? AND num > ?
159 ORDER BY $sort_col DESC
161 # TODO separate strict and loose matches here once --reindex
162 # is fixed to preserve `tid' properly
163 push @$msgs, @$loose;
169 my ($self, $opts, $after, $before) = @_;
171 if (defined($before)) {
172 if (defined($after)) {
173 $s = '+num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
174 @v = ($after, $before);
176 $s = '+num > 0 AND ts <= ? ORDER BY ts DESC';
180 if (defined($after)) {
181 $s = '+num > 0 AND ts >= ? ORDER BY ts ASC';
184 $s = '+num > 0 ORDER BY ts DESC';
187 my $msgs = do_get($self, <<"", $opts, @v);
188 SELECT ts,ds,ddd FROM over WHERE $s
190 return $msgs unless wantarray;
192 my $nr = $self->{dbh}->selectrow_array(<<'');
193 SELECT COUNT(num) FROM over WHERE num > 0
199 my ($self, $num) = @_;
200 my $dbh = $self->connect;
201 my $sth = $dbh->prepare_cached(<<'', undef, 1);
202 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
205 my $smsg = $sth->fetchrow_hashref;
206 $smsg ? load_from_row($smsg) : undef;
210 my ($self, $mid, $id, $prev) = @_;
211 my $dbh = $self->connect;
213 unless (defined $$id) {
214 my $sth = $dbh->prepare_cached(<<'', undef, 1);
215 SELECT id FROM msgid WHERE mid = ? LIMIT 1
218 $$id = $sth->fetchrow_array;
219 defined $$id or return;
221 my $sth = $dbh->prepare_cached(<<"", undef, 1);
222 SELECT num FROM id2num WHERE id = ? AND num > ?
223 ORDER BY num ASC LIMIT 1
226 $sth->execute($$id, $$prev);
227 my $num = $sth->fetchrow_array or return;
230 $sth = $dbh->prepare_cached(<<"", undef, 1);
231 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
234 my $smsg = $sth->fetchrow_hashref or return;
235 load_from_row($smsg);
238 # IMAP search, this is limited by callers to UID_SLICE size (50K)
240 my ($self, $beg, $end, $sql) = @_;
241 my $dbh = $self->connect;
242 my $q = 'SELECT num FROM over WHERE num >= ? AND num <= ?';
244 # This is read-only, anyways; but caller should verify it's
245 # only sending \A[0-9]+\z for ds and ts column ranges
247 $q .= ' ORDER BY num ASC';
248 $dbh->selectcol_arrayref($q, undef, $beg, $end);
253 my $sth = $self->connect->prepare_cached(<<'', undef, 1);
254 SELECT MAX(num) FROM over WHERE num > 0
257 $sth->fetchrow_array // 0;
261 my ($self, $uid_base, $uid_end) = @_;
262 my $sth = $self->connect->prepare_cached(<<'', undef, 1);
263 SELECT COUNT(num) FROM over WHERE num > ? AND num <= ?
265 $sth->execute($uid_base, $uid_end);
266 $sth->fetchrow_array;
271 my $dbh = $self->{dbh} or return;
272 my $f = $dbh->sqlite_db_filename;
273 if (my @st = stat($f)) { # did st_dev, st_ino change?
274 my $st = pack('dd', $st[0], $st[1]);
276 # don't actually reopen, just let {dbh} be recreated later
277 if ($st ne ($self->{st} // $st)) {
278 delete($self->{dbh});
279 $self->{filename} = $f;
282 warn "W: stat $f: $!\n";