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 journal_mode = TRUNCATE');
18 $dbh->do('PRAGMA cache_size = 80000');
23 sub get_counter ($$) {
25 my $sth = $dbh->prepare_cached(<<'', undef, 1);
26 SELECT val FROM counter WHERE key = ? LIMIT 1
32 sub adj_counter ($$$) {
33 my ($self, $key, $op) = @_;
34 my $dbh = $self->{dbh};
35 my $sth = $dbh->prepare_cached(<<"");
36 UPDATE counter SET val = val $op 1 WHERE key = ?
40 get_counter($dbh, $key);
43 sub next_tid { adj_counter($_[0], 'thread', '+') }
44 sub next_ghost_num { adj_counter($_[0], 'ghost', '-') }
47 my ($self, $tbl, $id_col, $val_col, $val) = @_;
48 my $dbh = $self->{dbh};
49 my $in = $dbh->prepare_cached(<<"")->execute($val);
50 INSERT OR IGNORE INTO $tbl ($val_col) VALUES (?)
53 my $sth = $dbh->prepare_cached(<<"", undef, 1);
54 SELECT $id_col FROM $tbl WHERE $val_col = ? LIMIT 1
59 $dbh->last_insert_id(undef, undef, $tbl, $id_col);
64 my ($self, $path) = @_;
65 return unless defined $path && $path ne '';
66 id_for($self, 'subject', 'sid', 'path' => $path);
70 my ($self, $mid) = @_;
71 id_for($self, 'msgid', 'id', 'mid' => $mid);
75 my ($self, $num) = @_;
76 my $dbh = $self->{dbh};
77 foreach (qw(over id2num)) {
78 $dbh->prepare_cached(<<"")->execute($num);
79 DELETE FROM $_ WHERE num = ?
84 # this includes ghosts
86 my ($self, $mid, $cols, $cb) = @_;
87 my $dbh = $self->{dbh};
90 I originally wanted to stuff everything into a single query:
92 SELECT over.* FROM over
93 LEFT JOIN id2num ON over.num = id2num.num
94 LEFT JOIN msgid ON msgid.id = id2num.id
95 WHERE msgid.mid = ? AND over.num >= ?
99 But it's faster broken out (and we're always in a
100 transaction for subroutines in this file)
103 my $sth = $dbh->prepare_cached(<<'', undef, 1);
104 SELECT id FROM msgid WHERE mid = ? LIMIT 1
107 my $id = $sth->fetchrow_array;
108 defined $id or return;
111 $cols = join(',', map { $_ } @$cols);
113 my $prev = get_counter($dbh, 'ghost');
115 $sth = $dbh->prepare_cached(<<"", undef, 1);
116 SELECT num FROM id2num WHERE id = ? AND num >= ?
120 $sth->execute($id, $prev);
121 my $nums = $sth->fetchall_arrayref;
122 my $nr = scalar(@$nums) or return;
123 $prev = $nums->[-1]->[0];
125 $sth = $dbh->prepare_cached(<<"", undef, 1);
126 SELECT $cols FROM over WHERE over.num = ? LIMIT 1
129 $sth->execute($_->[0]);
130 my $smsg = $sth->fetchrow_hashref;
131 $cb->(PublicInbox::Over::load_from_row($smsg)) or
134 return if $nr != $lim;
138 # this will create a ghost as necessary
139 sub resolve_mid_to_tid {
140 my ($self, $mid) = @_;
142 each_by_mid($self, $mid, ['tid'], sub {
144 my $cur_tid = $smsg->{tid};
146 merge_threads($self, $tid, $cur_tid);
152 defined $tid ? $tid : create_ghost($self, $mid);
156 my ($self, $mid) = @_;
157 my $id = $self->mid2id($mid);
158 my $num = $self->next_ghost_num;
159 $num < 0 or die "ghost num is non-negative: $num\n";
160 my $tid = $self->next_tid;
161 my $dbh = $self->{dbh};
162 $dbh->prepare_cached(<<'')->execute($num, $tid);
163 INSERT INTO over (num, tid) VALUES (?,?)
165 $dbh->prepare_cached(<<'')->execute($id, $num);
166 INSERT INTO id2num (id, num) VALUES (?,?)
172 my ($self, $winner_tid, $loser_tid) = @_;
173 return if $winner_tid == $loser_tid;
174 my $dbh = $self->{dbh};
175 $dbh->prepare_cached(<<'')->execute($winner_tid, $loser_tid);
176 UPDATE over SET tid = ? WHERE tid = ?
181 my ($self, $refs, $old_tid) = @_;
185 # first ref *should* be the thread root,
186 # but we can never trust clients to do the right thing
187 my $ref = $refs->[0];
188 $tid = resolve_mid_to_tid($self, $ref);
189 merge_threads($self, $tid, $old_tid) if defined $old_tid;
191 # the rest of the refs should point to this tid:
192 foreach my $i (1..$#$refs) {
194 my $ptid = resolve_mid_to_tid($self, $ref);
195 merge_threads($self, $tid, $ptid);
198 $tid = defined $old_tid ? $old_tid : $self->next_tid;
204 my ($self, $values) = @_;
205 my ($ts, $ds, $num, $mids, $refs, $xpath, $ddd) = @$values;
210 $self->delete_by_num($num);
211 foreach my $mid (@$mids) {
213 each_by_mid($self, $mid, ['tid'], sub {
215 my $cur_tid = $cur->{tid};
217 die "num must not be zero for $mid" if !$n;
218 $old_tid = $cur_tid unless defined $old_tid;
219 if ($n > 0) { # regular mail
220 merge_threads($self, $old_tid, $cur_tid);
221 } elsif ($n < 0) { # ghost
222 link_refs($self, $refs, $old_tid);
223 $self->delete_by_num($n);
228 $v > 1 and warn "BUG: vivified multiple ($v) ghosts for $mid\n";
231 my $tid = $vivified ? $old_tid : link_refs($self, $refs, $old_tid);
232 my $sid = $self->sid($xpath);
233 my $dbh = $self->{dbh};
234 my $sth = $dbh->prepare_cached(<<'');
235 INSERT INTO over (num, tid, sid, ts, ds, ddd)
239 my @v = ($num, $tid, $sid, $ts, $ds);
240 foreach (@v) { $sth->bind_param(++$n, $_) }
241 $sth->bind_param(++$n, $ddd, SQL_BLOB);
243 $sth = $dbh->prepare_cached(<<'');
244 INSERT INTO id2num (id, num) VALUES (?,?)
246 foreach my $mid (@$mids) {
247 my $id = $self->mid2id($mid);
248 $sth->execute($id, $num);
252 sub delete_articles {
253 my ($self, $nums) = @_;
254 my $dbh = $self->connect;
255 $self->delete_by_num($_) foreach @$nums;
259 my ($self, $oid, $mid) = @_;
261 each_by_mid($self, $mid, ['ddd'], sub {
263 $self->delete_by_num($smsg->{num}) if $smsg->{blob} eq $oid;
272 CREATE TABLE IF NOT EXISTS over (
273 num INTEGER NOT NULL,
274 tid INTEGER NOT NULL,
278 ddd VARBINARY, /* doc-data-deflated */
282 $dbh->do('CREATE INDEX IF NOT EXISTS idx_tid ON over (tid)');
283 $dbh->do('CREATE INDEX IF NOT EXISTS idx_sid ON over (sid)');
284 $dbh->do('CREATE INDEX IF NOT EXISTS idx_ts ON over (ts)');
285 $dbh->do('CREATE INDEX IF NOT EXISTS idx_ds ON over (ds)');
288 CREATE TABLE IF NOT EXISTS counter (
289 key VARCHAR(8) PRIMARY KEY NOT NULL,
290 val INTEGER DEFAULT 0,
294 $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('thread')");
295 $dbh->do("INSERT OR IGNORE INTO counter (key) VALUES ('ghost')");
298 CREATE TABLE IF NOT EXISTS subject (
299 sid INTEGER PRIMARY KEY AUTOINCREMENT,
300 path VARCHAR(40) NOT NULL,
305 CREATE TABLE IF NOT EXISTS id2num (
307 num INTEGER NOT NULL,
311 # performance critical:
312 $dbh->do('CREATE INDEX IF NOT EXISTS idx_inum ON id2num (num)');
313 $dbh->do('CREATE INDEX IF NOT EXISTS idx_id ON id2num (id)');
316 CREATE TABLE IF NOT EXISTS msgid (
317 id INTEGER PRIMARY KEY AUTOINCREMENT,
318 mid VARCHAR(244) NOT NULL,
326 delete $self->{txn} or return;
327 $self->{dbh}->commit;
332 return if $self->{txn};
333 my $dbh = $self->connect or return;
335 # $dbh->{Profile} = 2;
341 delete $self->{txn} or return;
342 $self->{dbh}->rollback;
347 die "in transaction" if $self->{txn};
348 $self->{dbh} = undef;
353 unless (-r $self->{filename}) {
355 require File::Basename;
356 File::Path::mkpath(File::Basename::dirname($self->{filename}));
359 PublicInbox::Over::connect($self);