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 = $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: $!";
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",'','', {
32 sqlite_use_immediate_transaction => 1,
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};
44 bless { filename => $f }, $class;
47 sub disconnect { $_[0]->{dbh} = undef }
49 sub connect { $_[0]->{dbh} ||= $_[0]->dbh_new }
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);
57 PublicInbox::Smsg::load_from_data($smsg, $data);
59 # saves over 600K for 1000+ message threads
60 PublicInbox::Smsg::psgi_cull($smsg) if $cull;
66 my ($self, $sql, $opts, @args) = @_;
67 my $dbh = $self->connect;
68 my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
70 my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
71 my $cull = $opts->{cull};
72 load_from_row($_, $cull) for @$msgs;
77 my ($self, $beg, $end) = @_;
78 do_get($self, <<'', {}, $beg, $end);
79 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
85 my ($self, $ts, $prev) = @_;
86 do_get($self, <<'', {}, $ts, $prev);
87 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
92 sub nothing () { wantarray ? (0, []) : [] };
95 my ($self, $mid, $prev) = @_;
96 my $dbh = $self->connect;
97 my $opts = { cull => 1 };
99 my $id = $dbh->selectrow_array(<<'', undef, $mid);
100 SELECT id FROM msgid WHERE mid = ? LIMIT 1
102 defined $id or return nothing;
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
108 defined $num or return nothing;
110 my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
111 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
113 defined $tid or return nothing; # $sid may be undef
115 my $cond_all = '(tid = ? OR sid = ?) AND num > ?';
118 if ($prev) { # mboxrd stream, only
119 $num = $prev->{num} || 0;
123 my $cols = 'num,ts,ds,ddd';
125 return do_get($self, <<"", $opts, $tid, $sid, $num);
126 SELECT $cols FROM over WHERE $cond_all
127 ORDER BY $sort_col ASC
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
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
143 # do we have room for loose matches? get the most recent ones, first:
144 my $lim = DEFAULT_LIMIT - scalar(@$msgs);
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
151 # TODO separate strict and loose matches here once --reindex
152 # is fixed to preserve `tid' properly
153 push @$msgs, @$loose;
159 my ($self, $opts, $after, $before) = @_;
161 if (defined($before)) {
162 if (defined($after)) {
163 $s = '+num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
164 @v = ($after, $before);
166 $s = '+num > 0 AND ts <= ? ORDER BY ts DESC';
170 if (defined($after)) {
171 $s = '+num > 0 AND ts >= ? ORDER BY ts ASC';
174 $s = '+num > 0 ORDER BY ts DESC';
177 my $msgs = do_get($self, <<"", $opts, @v);
178 SELECT ts,ds,ddd FROM over WHERE $s
180 return $msgs unless wantarray;
182 my $nr = $self->{dbh}->selectrow_array(<<'');
183 SELECT COUNT(num) FROM over WHERE num > 0
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
195 my $smsg = $sth->fetchrow_hashref;
196 $smsg ? load_from_row($smsg) : undef;
200 my ($self, $mid, $id, $prev) = @_;
201 my $dbh = $self->connect;
203 unless (defined $$id) {
204 my $sth = $dbh->prepare_cached(<<'', undef, 1);
205 SELECT id FROM msgid WHERE mid = ? LIMIT 1
208 $$id = $sth->fetchrow_array;
209 defined $$id or return;
211 my $sth = $dbh->prepare_cached(<<"", undef, 1);
212 SELECT num FROM id2num WHERE id = ? AND num > ?
213 ORDER BY num ASC LIMIT 1
216 $sth->execute($$id, $$prev);
217 my $num = $sth->fetchrow_array or return;
220 $sth = $dbh->prepare_cached(<<"", undef, 1);
221 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
224 my $smsg = $sth->fetchrow_hashref or return;
225 load_from_row($smsg);
228 # IMAP search, this is limited by callers to UID_SLICE size (50K)
230 my ($self, $beg, $end, $sql) = @_;
231 my $dbh = $self->connect;
232 my $q = 'SELECT num FROM over WHERE num >= ? AND num <= ?';
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
237 $q .= ' ORDER BY num ASC';
238 $dbh->selectcol_arrayref($q, undef, $beg, $end);
243 my $sth = $self->connect->prepare_cached(<<'', undef, 1);
244 SELECT MAX(num) FROM over WHERE num > 0
247 $sth->fetchrow_array // 0;
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 <= ?
255 $sth->execute($uid_base, $uid_end);
256 $sth->fetchrow_array;
261 if (my @st = stat($self->{filename})) { # did st_dev, st_ino change?
262 my $st = pack('dd', $st[0], $st[1]);
264 # don't actually reopen, just let {dbh} be recreated later
265 delete($self->{dbh}) if ($st ne ($self->{st} // $st));
267 warn "W: stat $self->{filename}: $!\n";