1 # Copyright (C) 2018 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::SearchMsg;
13 use Compress::Zlib qw(uncompress);
14 use constant DEFAULT_LIMIT => 1000;
18 my $ro = ref($self) eq 'PublicInbox::Over';
19 my $f = $self->{filename};
20 if (!$ro && !-f $f) { # SQLite defaults mode to 0644, we want 0666
21 open my $fh, '+>>', $f or die "failed to open $f: $!";
23 my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
28 sqlite_use_immediate_transaction => 1,
30 $dbh->{sqlite_unicode} = 1;
36 bless { filename => $f }, $class;
39 sub disconnect { $_[0]->{dbh} = undef }
41 sub connect { $_[0]->{dbh} ||= $_[0]->dbh_new }
45 bless $smsg, 'PublicInbox::SearchMsg';
46 if (defined(my $data = delete $smsg->{ddd})) {
47 $data = uncompress($data);
49 $smsg->load_from_data($data);
55 my ($self, $sql, $opts, @args) = @_;
56 my $dbh = $self->connect;
57 my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
59 my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
60 load_from_row($_) for @$msgs;
65 my ($self, $beg, $end) = @_;
66 do_get($self, <<'', {}, $beg, $end);
67 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
73 my ($self, $ts, $prev) = @_;
74 do_get($self, <<'', {}, $ts, $prev);
75 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
80 sub nothing () { wantarray ? (0, []) : [] };
83 my ($self, $mid, $prev) = @_;
84 my $dbh = $self->connect;
86 my $id = $dbh->selectrow_array(<<'', undef, $mid);
87 SELECT id FROM msgid WHERE mid = ? LIMIT 1
89 defined $id or return nothing;
91 my $num = $dbh->selectrow_array(<<'', undef, $id);
92 SELECT num FROM id2num WHERE id = ? AND num > 0
93 ORDER BY num ASC LIMIT 1
95 defined $num or return nothing;
97 my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
98 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
100 defined $tid or return nothing; # $sid may be undef
102 my $cond_all = '(tid = ? OR sid = ?) AND num > ?';
105 if ($prev) { # mboxrd stream, only
106 $num = $prev->{num} || 0;
110 my $cols = 'num,ts,ds,ddd';
112 return do_get($self, <<"", {}, $tid, $sid, $num);
113 SELECT $cols FROM over WHERE $cond_all
114 ORDER BY $sort_col ASC
118 # HTML view always wants an array and never uses $prev,
119 # but the mbox stream never wants an array and always has $prev
120 die '$prev not supported with wantarray' if $prev;
121 my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $num);
122 SELECT COUNT(num) FROM over WHERE $cond_all
124 # giant thread, prioritize strict (tid) matches and throw
125 # in the loose (sid) matches at the end
126 my $msgs = do_get($self, <<"", {}, $tid, $num);
127 SELECT $cols FROM over WHERE tid = ? AND num > ?
128 ORDER BY $sort_col ASC
130 # do we have room for loose matches? get the most recent ones, first:
131 my $lim = DEFAULT_LIMIT - scalar(@$msgs);
133 my $opts = { limit => $lim };
134 my $loose = do_get($self, <<"", $opts, $tid, $sid, $num);
135 SELECT $cols FROM over WHERE tid != ? AND sid = ? AND num > ?
136 ORDER BY $sort_col DESC
138 # TODO separate strict and loose matches here once --reindex
139 # is fixed to preserve `tid' properly
140 push @$msgs, @$loose;
146 my ($self, $opts, $after, $before) = @_;
148 if (defined($before)) {
149 if (defined($after)) {
150 $s = '+num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
151 @v = ($after, $before);
153 $s = '+num > 0 AND ts <= ? ORDER BY ts DESC';
157 if (defined($after)) {
158 $s = '+num > 0 AND ts >= ? ORDER BY ts ASC';
161 $s = '+num > 0 ORDER BY ts DESC';
164 my $msgs = do_get($self, <<"", $opts, @v);
165 SELECT ts,ds,ddd FROM over WHERE $s
167 return $msgs unless wantarray;
169 my $nr = $self->{dbh}->selectrow_array(<<'');
170 SELECT COUNT(num) FROM over WHERE num > 0
176 my ($self, $num) = @_;
177 my $dbh = $self->connect;
178 my $smsg = $dbh->selectrow_hashref(<<'', undef, $num);
179 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
181 return load_from_row($smsg) if $smsg;
186 my ($self, $mid, $id, $prev) = @_;
187 my $dbh = $self->connect;
189 unless (defined $$id) {
190 my $sth = $dbh->prepare_cached(<<'', undef, 1);
191 SELECT id FROM msgid WHERE mid = ? LIMIT 1
194 $$id = $sth->fetchrow_array;
195 defined $$id or return;
197 my $sth = $dbh->prepare_cached(<<"", undef, 1);
198 SELECT num FROM id2num WHERE id = ? AND num > ?
199 ORDER BY num ASC LIMIT 1
202 $sth->execute($$id, $$prev);
203 my $num = $sth->fetchrow_array or return;
206 $sth = $dbh->prepare_cached(<<"", undef, 1);
207 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
210 my $smsg = $sth->fetchrow_hashref or return;
211 load_from_row($smsg);