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);
17 my $ro = ref($self) eq 'PublicInbox::Over';
18 my $f = $self->{filename};
19 if (!$ro && !-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 }
44 bless $smsg, 'PublicInbox::SearchMsg';
45 if (defined(my $data = delete $smsg->{ddd})) {
46 $data = uncompress($data);
48 $smsg->load_from_data($data);
54 my ($self, $sql, $opts, @args) = @_;
55 my $dbh = $self->connect;
56 my $lim = (($opts->{limit} || 0) + 0) || 1000;
58 my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
59 load_from_row($_) for @$msgs;
64 my ($self, $beg, $end) = @_;
65 do_get($self, <<'', {}, $beg, $end);
66 SELECT num,ts,ds,ddd FROM over WHERE num >= ? AND num <= ?
72 my ($self, $ts, $prev) = @_;
73 do_get($self, <<'', {}, $ts, $prev);
74 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
79 sub nothing () { wantarray ? (0, []) : [] };
82 my ($self, $mid, $prev) = @_;
83 my $dbh = $self->connect;
85 my $id = $dbh->selectrow_array(<<'', undef, $mid);
86 SELECT id FROM msgid WHERE mid = ? LIMIT 1
88 defined $id or return nothing;
90 my $num = $dbh->selectrow_array(<<'', undef, $id);
91 SELECT num FROM id2num WHERE id = ? AND num > 0
92 ORDER BY num ASC LIMIT 1
94 defined $num or return nothing;
96 my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
97 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
99 defined $tid or return nothing; # $sid may be undef
103 $num = $prev->{num} || 0;
106 my $cond = '(tid = ? OR sid = ?) AND num > ?';
107 my $msgs = do_get($self, <<"", {}, $tid, $sid, $num);
108 SELECT num,ts,ds,ddd FROM over WHERE $cond ORDER BY $sort_col ASC
110 return $msgs unless wantarray;
112 my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $num);
113 SELECT COUNT(num) FROM over WHERE $cond
119 my ($self, $opts, $after, $before) = @_;
121 if (defined($before)) {
122 if (defined($after)) {
123 $s = 'num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
124 @v = ($after, $before);
126 $s = 'num > 0 AND ts <= ? ORDER BY ts DESC';
130 if (defined($after)) {
131 $s = 'num > 0 AND ts >= ? ORDER BY ts ASC';
134 $s = 'num > 0 ORDER BY ts DESC';
137 my $msgs = do_get($self, <<"", $opts, @v);
138 SELECT ts,ds,ddd FROM over WHERE $s
140 return $msgs unless wantarray;
142 my $nr = $self->{dbh}->selectrow_array(<<'');
143 SELECT COUNT(num) FROM over WHERE num > 0
149 my ($self, $num) = @_;
150 my $dbh = $self->connect;
151 my $smsg = $dbh->selectrow_hashref(<<'', undef, $num);
152 SELECT num,ds,ts,ddd FROM over WHERE num = ? LIMIT 1
154 return load_from_row($smsg) if $smsg;
159 my ($self, $mid, $id, $prev) = @_;
160 my $dbh = $self->connect;
162 unless (defined $$id) {
163 my $sth = $dbh->prepare_cached(<<'', undef, 1);
164 SELECT id FROM msgid WHERE mid = ? LIMIT 1
167 $$id = $sth->fetchrow_array;
168 defined $$id or return;
170 my $sth = $dbh->prepare_cached(<<"", undef, 1);
171 SELECT num FROM id2num WHERE id = ? AND num > ?
172 ORDER BY num ASC LIMIT 1
175 $sth->execute($$id, $$prev);
176 my $num = $sth->fetchrow_array or return;
179 $sth = $dbh->prepare_cached(<<"", undef, 1);
180 SELECT num,ts,ds,ddd FROM over WHERE num = ? LIMIT 1
183 my $smsg = $sth->fetchrow_hashref or return;
184 load_from_row($smsg);