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 }
43 sub load_from_row ($;$) {
44 my ($smsg, $cull) = @_;
45 bless $smsg, 'PublicInbox::SearchMsg';
46 if (defined(my $data = delete $smsg->{ddd})) {
47 $data = uncompress($data);
49 PublicInbox::SearchMsg::load_from_data($smsg, $data);
51 # saves over 600K for 1000+ message threads
52 PublicInbox::SearchMsg::psgi_cull($smsg) if $cull;
58 my ($self, $sql, $opts, @args) = @_;
59 my $dbh = $self->connect;
60 my $lim = (($opts->{limit} || 0) + 0) || DEFAULT_LIMIT;
62 my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
63 my $cull = $opts->{cull};
64 load_from_row($_, $cull) for @$msgs;
69 my ($self, $beg, $end) = @_;
70 do_get($self, <<'', {}, $beg, $end);
71 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
77 my ($self, $ts, $prev) = @_;
78 do_get($self, <<'', {}, $ts, $prev);
79 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
84 sub nothing () { wantarray ? (0, []) : [] };
87 my ($self, $mid, $prev) = @_;
88 my $dbh = $self->connect;
89 my $opts = { cull => 1 };
91 my $id = $dbh->selectrow_array(<<'', undef, $mid);
92 SELECT id FROM msgid WHERE mid = ? LIMIT 1
94 defined $id or return nothing;
96 my $num = $dbh->selectrow_array(<<'', undef, $id);
97 SELECT num FROM id2num WHERE id = ? AND num > 0
98 ORDER BY num ASC LIMIT 1
100 defined $num or return nothing;
102 my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
103 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
105 defined $tid or return nothing; # $sid may be undef
107 my $cond_all = '(tid = ? OR sid = ?) AND num > ?';
110 if ($prev) { # mboxrd stream, only
111 $num = $prev->{num} || 0;
115 my $cols = 'num,ts,ds,ddd';
117 return do_get($self, <<"", $opts, $tid, $sid, $num);
118 SELECT $cols FROM over WHERE $cond_all
119 ORDER BY $sort_col ASC
123 # HTML view always wants an array and never uses $prev,
124 # but the mbox stream never wants an array and always has $prev
125 die '$prev not supported with wantarray' if $prev;
126 my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $num);
127 SELECT COUNT(num) FROM over WHERE $cond_all
129 # giant thread, prioritize strict (tid) matches and throw
130 # in the loose (sid) matches at the end
131 my $msgs = do_get($self, <<"", $opts, $tid, $num);
132 SELECT $cols FROM over WHERE tid = ? AND num > ?
133 ORDER BY $sort_col ASC
135 # do we have room for loose matches? get the most recent ones, first:
136 my $lim = DEFAULT_LIMIT - scalar(@$msgs);
138 $opts->{limit} = $lim;
139 my $loose = do_get($self, <<"", $opts, $tid, $sid, $num);
140 SELECT $cols FROM over WHERE tid != ? AND sid = ? AND num > ?
141 ORDER BY $sort_col DESC
143 # TODO separate strict and loose matches here once --reindex
144 # is fixed to preserve `tid' properly
145 push @$msgs, @$loose;
151 my ($self, $opts, $after, $before) = @_;
153 if (defined($before)) {
154 if (defined($after)) {
155 $s = '+num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
156 @v = ($after, $before);
158 $s = '+num > 0 AND ts <= ? ORDER BY ts DESC';
162 if (defined($after)) {
163 $s = '+num > 0 AND ts >= ? ORDER BY ts ASC';
166 $s = '+num > 0 ORDER BY ts DESC';
169 my $msgs = do_get($self, <<"", $opts, @v);
170 SELECT ts,ds,ddd FROM over WHERE $s
172 return $msgs unless wantarray;
174 my $nr = $self->{dbh}->selectrow_array(<<'');
175 SELECT COUNT(num) FROM over WHERE num > 0
181 my ($self, $num) = @_;
182 my $dbh = $self->connect;
183 my $smsg = $dbh->selectrow_hashref(<<'', undef, $num);
184 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
186 return load_from_row($smsg) if $smsg;
191 my ($self, $mid, $id, $prev) = @_;
192 my $dbh = $self->connect;
194 unless (defined $$id) {
195 my $sth = $dbh->prepare_cached(<<'', undef, 1);
196 SELECT id FROM msgid WHERE mid = ? LIMIT 1
199 $$id = $sth->fetchrow_array;
200 defined $$id or return;
202 my $sth = $dbh->prepare_cached(<<"", undef, 1);
203 SELECT num FROM id2num WHERE id = ? AND num > ?
204 ORDER BY num ASC LIMIT 1
207 $sth->execute($$id, $$prev);
208 my $num = $sth->fetchrow_array or return;
211 $sth = $dbh->prepare_cached(<<"", undef, 1);
212 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
215 my $smsg = $sth->fetchrow_hashref or return;
216 load_from_row($smsg);