1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # bidirectional Message-ID <-> Article Number mapping for the NNTP
5 # and web interfaces. This is required for implementing stable article
6 # numbers for NNTP and allows prefix lookups for partial Message-IDs
7 # in case URLs get truncated from copy-n-paste errors by users.
9 # This is maintained by ::SearchIdx
10 package PublicInbox::Msgmap;
15 use File::Temp qw(tempfile);
16 use PublicInbox::Over;
19 my ($class, $git_dir, $writable) = @_;
20 my $d = "$git_dir/public-inbox";
21 if ($writable && !-d $d && !mkdir $d) {
23 -d $d or die "$d not created: $err";
25 new_file($class, "$d/msgmap.sqlite3", $writable);
29 my ($class, $f, $rw) = @_;
30 return if !$rw && !-r $f;
32 my $self = bless { filename => $f }, $class;
33 my $dbh = $self->{dbh} = PublicInbox::Over::dbh_new($self, $rw);
35 # TRUNCATE reduces I/O compared to the default (DELETE)
36 $dbh->do('PRAGMA journal_mode = TRUNCATE');
40 $self->created_at(time) unless $self->created_at;
42 my $max = $self->max // 0;
43 $self->num_highwater($max);
49 # used to keep track of used numeric mappings for v2 reindex
52 my ($fh, $fn) = tempfile('msgmap-XXXXXXXX', EXLOCK => 0, TMPDIR => 1);
54 if ($self->{dbh}->can('sqlite_backup_to_dbh')) {
55 $tmp = ref($self)->new_file($fn, 2);
56 $tmp->{dbh}->do('PRAGMA journal_mode = MEMORY');
57 $self->{dbh}->sqlite_backup_to_dbh($tmp->{dbh});
58 } else { # DBD::SQLite <= 1.61_01
59 $self->{dbh}->sqlite_backup_to_file($fn);
60 $tmp = ref($self)->new_file($fn, 2);
61 $tmp->{dbh}->do('PRAGMA journal_mode = MEMORY');
67 # n.b. invoked directly by scripts/xhdr-num2mid
69 my ($self, $key, $value) = @_;
71 my $sql = 'SELECT val FROM meta WHERE key = ? LIMIT 1';
72 my $dbh = $self->{dbh};
74 defined $value or return $dbh->selectrow_array($sql, undef, $key);
76 $prev = $dbh->selectrow_array($sql, undef, $key);
79 $sql = 'UPDATE meta SET val = ? WHERE key = ?';
80 $dbh->do($sql, undef, $value, $key);
82 $sql = 'INSERT INTO meta (key,val) VALUES (?,?)';
83 $dbh->do($sql, undef, $key, $value);
89 my ($self, $commit) = @_;
90 $self->meta_accessor('last_commit', $commit);
93 # v2 uses this to keep track of how up-to-date Xapian is
94 # old versions may be automatically GC'ed away in the future,
95 # but it's a trivial amount of storage.
97 my ($self, $version, $i, $commit) = @_;
98 $self->meta_accessor("last_xap$version-$i", $commit);
102 my ($self, $second) = @_;
103 $self->meta_accessor('created_at', $second);
107 my ($self, $num) = @_;
108 my $high = $self->{num_highwater} ||=
109 $self->meta_accessor('num_highwater');
110 if (defined($num) && (!defined($high) || ($num > $high))) {
111 $self->{num_highwater} = $num;
112 $self->meta_accessor('num_highwater', $num);
114 $self->{num_highwater};
118 my ($self, $mid) = @_;
119 my $dbh = $self->{dbh};
120 my $sth = $dbh->prepare_cached(<<'');
121 INSERT INTO msgmap (mid) VALUES (?)
123 return unless eval { $sth->execute($mid) };
124 my $num = $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
125 $self->num_highwater($num) if defined($num);
130 my ($self, $num) = @_;
131 my $dbh = $self->{dbh};
132 my $sth = $self->{mid_for} ||=
133 $dbh->prepare('SELECT mid FROM msgmap WHERE num = ? LIMIT 1');
134 $sth->bind_param(1, $num);
136 $sth->fetchrow_array;
140 my ($self, $mid) = @_;
141 my $dbh = $self->{dbh};
142 my $sth = $self->{num_for} ||=
143 $dbh->prepare('SELECT num FROM msgmap WHERE mid = ? LIMIT 1');
144 $sth->bind_param(1, $mid);
146 $sth->fetchrow_array;
150 my $sth = $_[0]->{dbh}->prepare_cached('SELECT MAX(num) FROM msgmap',
153 $sth->fetchrow_array;
157 # breaking MIN and MAX into separate queries speeds up from 250ms
158 # to around 700us with 2.7million messages.
159 my $sth = $_[0]->{dbh}->prepare_cached('SELECT MIN(num) FROM msgmap',
162 ($sth->fetchrow_array, max($_[0]));
166 my ($self, $mid) = @_;
167 my $dbh = $self->{dbh};
168 my $sth = $dbh->prepare('DELETE FROM msgmap WHERE mid = ?');
169 $sth->bind_param(1, $mid);
174 my ($self, $num) = @_;
175 my $dbh = $self->{dbh};
176 my $sth = $dbh->prepare('DELETE FROM msgmap WHERE num = ?');
177 $sth->bind_param(1, $num);
185 $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM msgmap;') };
186 defined $e or $dbh->do('CREATE TABLE msgmap (' .
187 'num INTEGER PRIMARY KEY AUTOINCREMENT, '.
188 'mid VARCHAR(1000) NOT NULL, ' .
191 $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM meta') };
192 defined $e or $dbh->do('CREATE TABLE meta (' .
193 'key VARCHAR(32) PRIMARY KEY, '.
194 'val VARCHAR(255) NOT NULL)');
199 my ($self, $num) = @_;
200 my $ids = $self->{dbh}->selectcol_arrayref(<<'', undef, $$num);
201 SELECT num FROM msgmap WHERE num > ?
202 ORDER BY num ASC LIMIT 1000
204 $$num = $ids->[-1] if @$ids;
209 my ($self, $beg, $end, $cols) = @_;
211 my $dbh = $self->{dbh};
212 my $attr = { Columns => [] };
213 my $mids = $dbh->selectall_arrayref(<<"", $attr, $$beg, $end);
214 SELECT $cols FROM msgmap WHERE num >= ? AND num <= ?
215 ORDER BY num ASC LIMIT 1000
217 $$beg = $mids->[-1]->[0] + 1 if @$mids;
221 # only used for mapping external serial numbers (e.g. articles from gmane)
222 # see scripts/xhdr-num2mid or PublicInbox::Filter::RubyLang for usage
224 my ($self, $num, $mid) = @_;
225 my $sth = $self->{mid_set} ||= do {
226 $self->{dbh}->prepare(
227 'INSERT OR IGNORE INTO msgmap (num,mid) VALUES (?,?)');
229 my $result = $sth->execute($num, $mid);
230 $self->num_highwater($num) if (defined($result) && $result == 1);
236 my $dbh = $self->{dbh} or return;
237 if (($self->{pid} // 0) == $$) {
238 my $f = $dbh->sqlite_db_filename;
239 unlink $f or warn "failed to unlink $f: $!\n";
245 $self->{pid} or die 'BUG: not a temporary clone';
246 $self->{dbh} and die 'BUG: tmp_clone dbh not prepared for parent';
247 defined($self->{filename}) or die 'BUG: {filename} not defined';
248 $self->{dbh} = PublicInbox::Over::dbh_new($self, 2);
253 my $pid = $self->{pid} or die 'BUG: not a temporary clone';
254 $pid == $$ or die "BUG: atfork_prepare not called by $pid";
255 my $dbh = $self->{dbh} or die 'BUG: temporary clone not open';
257 # must clobber prepared statements
258 %$self = (filename => $dbh->sqlite_db_filename, pid => $pid);
262 my ($self, $skip_artnum) = @_;
263 return meta_accessor($self, 'skip_artnum') if !defined($skip_artnum);
265 my $cur = num_highwater($self) // 0;
266 if ($skip_artnum < $cur) {
267 die "E: current article number $cur ",
268 "exceeds --skip-artnum=$skip_artnum\n";
272 my $mid = 'skip'.rand.'@'.rand.'.example.com';
273 $ok = mid_set($self, $skip_artnum, $mid);
275 mid_delete($self, $mid);
279 $ok or die '--skip-artnum failed';
281 # in the future, the indexer may use this value for
282 # new messages in old epochs
283 meta_accessor($self, 'skip_artnum', $skip_artnum);
289 # no filename if in-:memory:
290 my $f = $self->{dbh}->sqlite_db_filename // return;
291 if (my @st = stat($f)) { # did st_dev, st_ino change?
292 my $st = pack('dd', $st[0], $st[1]);
293 if ($st ne ($self->{st} // $st)) {
294 my $tmp = eval { ref($self)->new_file($f) };
296 warn "E: DBI->connect($f): $@\n";
302 warn "W: stat $f: $!\n";