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 $dbh = DBI->connect("dbi:SQLite:dbname=$self->{filename}",'','', {
23 sqlite_use_immediate_transaction => 1,
25 $dbh->{sqlite_unicode} = 1;
31 bless { filename => $f }, $class;
34 sub disconnect { $_[0]->{dbh} = undef }
36 sub connect { $_[0]->{dbh} ||= $_[0]->dbh_new }
40 bless $smsg, 'PublicInbox::SearchMsg';
41 if (defined(my $data = delete $smsg->{ddd})) {
42 $data = uncompress($data);
44 $smsg->load_from_data($data);
50 my ($self, $sql, $opts, @args) = @_;
51 my $dbh = $self->connect;
52 my $lim = (($opts->{limit} || 0) + 0) || 1000;
54 my $msgs = $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
55 load_from_row($_) for @$msgs;
60 my ($self, $beg, $end) = @_;
61 do_get($self, <<'', {}, $beg, $end);
62 SELECT * FROM over WHERE num >= ? AND num <= ?
68 my ($self, $ts, $prev) = @_;
69 do_get($self, <<'', {}, $ts, $prev);
70 SELECT num,ddd FROM over WHERE ts >= ? AND num > ?
75 sub nothing () { wantarray ? (0, []) : [] };
78 my ($self, $mid, $prev) = @_;
79 my $dbh = $self->connect;
81 my $id = $dbh->selectrow_array(<<'', undef, $mid);
82 SELECT id FROM msgid WHERE mid = ? LIMIT 1
84 defined $id or return nothing;
86 my $num = $dbh->selectrow_array(<<'', undef, $id);
87 SELECT num FROM id2num WHERE id = ? AND num > 0
88 ORDER BY num ASC LIMIT 1
90 defined $num or return nothing;
92 my ($tid, $sid) = $dbh->selectrow_array(<<'', undef, $num);
93 SELECT tid,sid FROM over WHERE num = ? LIMIT 1
95 defined $tid or return nothing; # $sid may be undef
98 my $cond = 'FROM over WHERE (tid = ? OR sid = ?) AND num > ?';
99 my $msgs = do_get($self, <<"", {}, $tid, $sid, $prev);
100 SELECT * $cond ORDER BY num ASC
102 return $msgs unless wantarray;
104 my $nr = $dbh->selectrow_array(<<"", undef, $tid, $sid, $prev);
105 SELECT COUNT(num) $cond
111 my ($self, $opts, $after, $before) = @_;
113 if (defined($before)) {
114 if (defined($after)) {
115 $s = 'num > 0 AND ts >= ? AND ts <= ? ORDER BY ts DESC';
116 @v = ($after, $before);
118 $s = 'num > 0 AND ts <= ? ORDER BY ts DESC';
122 if (defined($after)) {
123 $s = 'num > 0 AND ts >= ? ORDER BY ts ASC';
126 $s = 'num > 0 ORDER BY ts DESC';
129 my $msgs = do_get($self, <<"", $opts, @v);
130 SELECT * FROM over WHERE $s
132 return $msgs unless wantarray;
134 my $nr = $self->{dbh}->selectrow_array(<<'');
135 SELECT COUNT(num) FROM over WHERE num > 0
141 my ($self, $num) = @_;
142 my $dbh = $self->connect;
143 my $smsg = $dbh->selectrow_hashref(<<'', undef, $num);
144 SELECT * from OVER where num = ? LIMIT 1
146 return load_from_row($smsg) if $smsg;