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: $!";
22 my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
27 sqlite_use_immediate_transaction => 1,
29 $dbh->{sqlite_unicode} = 1;
35 bless { filename => $f }, $class;
38 sub disconnect { $_[0]->{dbh} = undef }
40 sub connect { $_[0]->{dbh} ||= $_[0]->dbh_new }
42 sub load_from_row ($;$) {
43 my ($smsg, $cull) = @_;
44 bless $smsg, 'PublicInbox::Smsg';
45 if (defined(my $data = delete $smsg->{ddd})) {
46 $data = uncompress($data);
48 PublicInbox::Smsg::load_from_data($smsg, $data);
50 # saves over 600K for 1000+ message threads
51 PublicInbox::Smsg::psgi_cull($smsg) if $cull;
57 my ($self, $sql, $opts, @args) = @_;
58 my $dbh = $self->connect;
59 my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
61 my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
62 my $cull = $opts->{cull};
63 load_from_row($_, $cull) for @$msgs;
68 my ($self, $beg, $end) = @_;
69 do_get($self, <<'', {}, $beg, $end);
70 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
76 my ($self, $ts, $prev) = @_;
77 do_get($self, <<'', {}, $ts, $prev);
78 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
83 sub nothing () { wantarray ? (0, []) : [] };
86 my ($self, $mid, $prev) = @_;
87 my $dbh = $self->connect;
88 my $opts = { cull => 1 };
90 my $id = $dbh->selectrow_array(<<'', undef, $mid);
91 SELECT id FROM msgid WHERE mid = ? LIMIT 1
93 defined $id or return nothing;
95 my $num = $dbh->selectrow_array(<<'', undef, $id);
96 SELECT num FROM id2num WHERE id = ? AND num > 0
97 ORDER BY num ASC LIMIT 1
99 defined $num or return nothing;
101 my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
102 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
104 defined $tid or return nothing; # $sid may be undef
106 my $cond_all = '(tid = ? OR sid = ?) AND num > ?';
109 if ($prev) { # mboxrd stream, only
110 $num = $prev->{num} || 0;
114 my $cols = 'num,ts,ds,ddd';
116 return do_get($self, <<"", $opts, $tid, $sid, $num);
117 SELECT $cols FROM over WHERE $cond_all
118 ORDER BY $sort_col ASC
122 # HTML view always wants an array and never uses $prev,
123 # but the mbox stream never wants an array and always has $prev
124 die '$prev not supported with wantarray' if $prev;
125 my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $num);
126 SELECT COUNT(num) FROM over WHERE $cond_all
128 # giant thread, prioritize strict (tid) matches and throw
129 # in the loose (sid) matches at the end
130 my $msgs = do_get($self, <<"", $opts, $tid, $num);
131 SELECT $cols FROM over WHERE tid = ? AND num > ?
132 ORDER BY $sort_col ASC
134 # do we have room for loose matches? get the most recent ones, first:
135 my $lim = DEFAULT_LIMIT - scalar(@$msgs);
137 $opts->{limit} = $lim;
138 my $loose = do_get($self, <<"", $opts, $tid, $sid, $num);
139 SELECT $cols FROM over WHERE tid != ? AND sid = ? AND num > ?
140 ORDER BY $sort_col DESC
142 # TODO separate strict and loose matches here once --reindex
143 # is fixed to preserve `tid' properly
144 push @$msgs, @$loose;
150 my ($self, $opts, $after, $before) = @_;
152 if (defined($before)) {
153 if (defined($after)) {
154 $s = '+num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
155 @v = ($after, $before);
157 $s = '+num > 0 AND ts <= ? ORDER BY ts DESC';
161 if (defined($after)) {
162 $s = '+num > 0 AND ts >= ? ORDER BY ts ASC';
165 $s = '+num > 0 ORDER BY ts DESC';
168 my $msgs = do_get($self, <<"", $opts, @v);
169 SELECT ts,ds,ddd FROM over WHERE $s
171 return $msgs unless wantarray;
173 my $nr = $self->{dbh}->selectrow_array(<<'');
174 SELECT COUNT(num) FROM over WHERE num > 0
180 my ($self, $num) = @_;
181 my $dbh = $self->connect;
182 my $smsg = $dbh->selectrow_hashref(<<'', undef, $num);
183 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
185 return load_from_row($smsg) if $smsg;
190 my ($self, $mid, $id, $prev) = @_;
191 my $dbh = $self->connect;
193 unless (defined $$id) {
194 my $sth = $dbh->prepare_cached(<<'', undef, 1);
195 SELECT id FROM msgid WHERE mid = ? LIMIT 1
198 $$id = $sth->fetchrow_array;
199 defined $$id or return;
201 my $sth = $dbh->prepare_cached(<<"", undef, 1);
202 SELECT num FROM id2num WHERE id = ? AND num > ?
203 ORDER BY num ASC LIMIT 1
206 $sth->execute($$id, $$prev);
207 my $num = $sth->fetchrow_array or return;
210 $sth = $dbh->prepare_cached(<<"", undef, 1);
211 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
214 my $smsg = $sth->fetchrow_hashref or return;
215 load_from_row($smsg);