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::OverIdx;
10 use base qw(PublicInbox::Over);
12 use DBI qw(:sql_types); # SQL_BLOB
16 my $dbh = $self->SUPER::dbh_new;
17 $dbh->do('PRAGMA synchronous = OFF'); # commit_fsync instead
18 $dbh->do('PRAGMA journal_mode = TRUNCATE');
19 $dbh->do('PRAGMA cache_size = 80000');
25 my $fn = $_[0]->{filename};
26 if (open my $fh, '+<', $fn) {
32 sub get_counter ($$) {
34 my $sth = $dbh->prepare_cached(<<'', undef, 1);
35 SELECT val FROM counter WHERE key = ? LIMIT 1
41 sub adj_counter ($$$) {
42 my ($self, $key, $op) = @_;
43 my $dbh = $self->{dbh};
44 my $sth = $dbh->prepare_cached(<<"");
45 UPDATE counter SET val = val $op 1 WHERE key = ?
49 get_counter($dbh, $key);
52 sub next_tid { adj_counter($_[0], 'thread', '+') }
53 sub next_ghost_num { adj_counter($_[0], 'ghost', '-') }
56 my ($self, $tbl, $id_col, $val_col, $val) = @_;
57 my $dbh = $self->{dbh};
58 my $in = $dbh->prepare_cached(<<"")->execute($val);
59 INSERT OR IGNORE INTO $tbl ($val_col) VALUES (?)
62 my $sth = $dbh->prepare_cached(<<"", undef, 1);
63 SELECT $id_col FROM $tbl WHERE $val_col = ? LIMIT 1
68 $dbh->last_insert_id(undef, undef, $tbl, $id_col);
73 my ($self, $path) = @_;
74 return unless defined $path && $path ne '';
75 id_for($self, 'subject', 'sid', 'path' => $path);
79 my ($self, $mid) = @_;
80 id_for($self, 'msgid', 'id', 'mid' => $mid);
84 my ($self, $num) = @_;
85 my $dbh = $self->{dbh};
86 foreach (qw(over id2num)) {
87 $dbh->prepare_cached(<<"")->execute($num);
88 DELETE FROM $_ WHERE num = ?
93 # this includes ghosts
95 my ($self, $mid, $cols, $cb) = @_;
96 my $dbh = $self->{dbh};
99 I originally wanted to stuff everything into a single query:
101 SELECT over.* FROM over
102 LEFT JOIN id2num ON over.num = id2num.num
103 LEFT JOIN msgid ON msgid.id = id2num.id
104 WHERE msgid.mid = ? AND over.num >= ?
105 ORDER BY over.num ASC
108 But it's faster broken out (and we're always in a
109 transaction for subroutines in this file)
112 my $sth = $dbh->prepare_cached(<<'', undef, 1);
113 SELECT id FROM msgid WHERE mid = ? LIMIT 1
116 my $id = $sth->fetchrow_array;
117 defined $id or return;
120 $cols = join(',', map { $_ } @$cols);
122 my $prev = get_counter($dbh, 'ghost');
124 $sth = $dbh->prepare_cached(<<"", undef, 1);
125 SELECT num FROM id2num WHERE id = ? AND num >= ?
129 $sth->execute($id, $prev);
130 my $nums = $sth->fetchall_arrayref;
131 my $nr = scalar(@$nums) or return;
132 $prev = $nums->[-1]->[0];
134 $sth = $dbh->prepare_cached(<<"", undef, 1);
135 SELECT $cols FROM over WHERE over.num = ? LIMIT 1
138 $sth->execute($_->[0]);
139 my $smsg = $sth->fetchrow_hashref;
140 $cb->(PublicInbox::Over::load_from_row($smsg)) or
143 return if $nr != $lim;
147 # this will create a ghost as necessary
148 sub resolve_mid_to_tid {
149 my ($self, $mid) = @_;
151 each_by_mid($self, $mid, ['tid'], sub {
153 my $cur_tid = $smsg->{tid};
155 merge_threads($self, $tid, $cur_tid);
161 defined $tid ? $tid : create_ghost($self, $mid);
165 my ($self, $mid) = @_;
166 my $id = $self->mid2id($mid);
167 my $num = $self->next_ghost_num;
168 $num < 0 or die "ghost num is non-negative: $num\n";
169 my $tid = $self->next_tid;
170 my $dbh = $self->{dbh};
171 $dbh->prepare_cached(<<'')->execute($num, $tid);
172 INSERT INTO over (num, tid) VALUES (?,?)
174 $dbh->prepare_cached(<<'')->execute($id, $num);
175 INSERT INTO id2num (id, num) VALUES (?,?)
181 my ($self, $winner_tid, $loser_tid) = @_;
182 return if $winner_tid == $loser_tid;
183 my $dbh = $self->{dbh};
184 $dbh->prepare_cached(<<'')->execute($winner_tid, $loser_tid);
185 UPDATE over SET tid = ? WHERE tid = ?
190 my ($self, $refs, $old_tid) = @_;
194 # first ref *should* be the thread root,
195 # but we can never trust clients to do the right thing
196 my $ref = $refs->[0];
197 $tid = resolve_mid_to_tid($self, $ref);
198 merge_threads($self, $tid, $old_tid) if defined $old_tid;
200 # the rest of the refs should point to this tid:
201 foreach my $i (1..$#$refs) {
203 my $ptid = resolve_mid_to_tid($self, $ref);
204 merge_threads($self, $tid, $ptid);
207 $tid = defined $old_tid ? $old_tid : $self->next_tid;
213 my ($self, $values) = @_;
214 my ($ts, $num, $mids, $refs, $xpath, $ddd) = @$values;
219 $self->delete_by_num($num);
220 foreach my $mid (@$mids) {
222 each_by_mid($self, $mid, ['tid'], sub {
224 my $cur_tid = $cur->{tid};
226 die "num must not be zero for $mid" if !$n;
227 $old_tid = $cur_tid unless defined $old_tid;
228 if ($n > 0) { # regular mail
229 merge_threads($self, $old_tid, $cur_tid);
230 } elsif ($n < 0) { # ghost
231 link_refs($self, $refs, $old_tid);
232 $self->delete_by_num($n);
237 $v > 1 and warn "BUG: vivified multiple ($v) ghosts for $mid\n";
240 my $tid = $vivified ? $old_tid : link_refs($self, $refs, $old_tid);
241 my $sid = $self->sid($xpath);
242 my $dbh = $self->{dbh};
243 my $sth = $dbh->prepare_cached(<<'');
244 INSERT INTO over (num, tid, sid, ts, ddd)
248 my @v = ($num, $tid, $sid, $ts);
249 foreach (@v) { $sth->bind_param(++$n, $_) }
250 $sth->bind_param(++$n, $ddd, SQL_BLOB);
252 $sth = $dbh->prepare_cached(<<'');
253 INSERT INTO id2num (id, num) VALUES (?,?)
255 foreach my $mid (@$mids) {
256 my $id = $self->mid2id($mid);
257 $sth->execute($id, $num);
261 sub delete_articles {
262 my ($self, $nums) = @_;
263 my $dbh = $self->connect;
264 $self->delete_by_num($_) foreach @$nums;
268 my ($self, $oid, $mid) = @_;
270 each_by_mid($self, $mid, ['ddd'], sub {
272 $self->delete_by_num($smsg->{num}) if $smsg->{blob} eq $oid;
281 CREATE TABLE IF NOT EXISTS over (
282 num INTEGER NOT NULL,
283 tid INTEGER NOT NULL,
286 ddd VARBINARY, /* doc-data-deflated */
290 $dbh->do('CREATE INDEX IF NOT EXISTS idx_tid ON over (tid)');
291 $dbh->do('CREATE INDEX IF NOT EXISTS idx_sid ON over (sid)');
292 $dbh->do('CREATE INDEX IF NOT EXISTS idx_ts ON over (ts)');
295 CREATE TABLE IF NOT EXISTS counter (
296 key VARCHAR(8) PRIMARY KEY NOT NULL,
297 val INTEGER DEFAULT 0,
301 $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('thread')");
302 $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('ghost')");
305 CREATE TABLE IF NOT EXISTS subject (
306 sid INTEGER PRIMARY KEY AUTOINCREMENT,
307 path VARCHAR(40) NOT NULL,
312 CREATE TABLE IF NOT EXISTS id2num (
314 num INTEGER NOT NULL,
318 # performance critical:
319 $dbh->do('CREATE INDEX IF NOT EXISTS idx_inum ON id2num (num)');
320 $dbh->do('CREATE INDEX IF NOT EXISTS idx_id ON id2num (id)');
323 CREATE TABLE IF NOT EXISTS msgid (
324 id INTEGER PRIMARY KEY AUTOINCREMENT,
325 mid VARCHAR(244) NOT NULL,
333 delete $self->{txn} or return;
334 $self->{dbh}->commit;
339 return if $self->{txn};
340 my $dbh = $self->connect or return;
342 # $dbh->{Profile} = 2;
348 delete $self->{txn} or return;
349 $self->{dbh}->rollback;
354 die "in transaction" if $self->{txn};
355 $self->{dbh} = undef;
360 unless (-r $self->{filename}) {
362 require File::Basename;
363 File::Path::mkpath(File::Basename::dirname($self->{filename}));
366 PublicInbox::Over::connect($self);