1 # Copyright (C) 2015-2018 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);
18 my ($class, $git_dir, $writable) = @_;
19 my $d = "$git_dir/public-inbox";
20 if ($writable && !-d $d && !mkdir $d) {
22 -d $d or die "$d not created: $err";
24 new_file($class, "$d/msgmap.sqlite3", $writable);
28 my ($f, $writable) = @_;
29 if ($writable && !-f $f) { # SQLite defaults mode to 0644, we want 0666
30 open my $fh, '+>>', $f or die "failed to open $f: $!";
32 my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
36 ReadOnly => !$writable,
37 sqlite_use_immediate_transaction => 1,
39 $dbh->do('PRAGMA case_sensitive_like = ON');
44 my ($class, $f, $writable) = @_;
45 return if !$writable && !-r $f;
47 my $dbh = dbh_new($f, $writable);
48 my $self = bless { dbh => $dbh }, $class;
53 $self->created_at(time) unless $self->created_at;
59 # used to keep track of used numeric mappings for v2 reindex
62 my ($fh, $fn) = tempfile('msgmap-XXXXXXXX', EXLOCK => 0, TMPDIR => 1);
63 $self->{dbh}->sqlite_backup_to_file($fn);
64 my $tmp = ref($self)->new_file($fn, 1);
65 $tmp->{dbh}->do('PRAGMA synchronous = OFF');
66 $tmp->{tmp_name} = $fn; # SQLite won't work if unlinked, apparently
68 close $fh or die "failed to close $fn: $!";
72 # n.b. invoked directly by scripts/xhdr-num2mid
74 my ($self, $key, $value) = @_;
76 my $sql = 'SELECT val FROM meta WHERE key = ? LIMIT 1';
77 my $dbh = $self->{dbh};
79 defined $value or return $dbh->selectrow_array($sql, undef, $key);
81 $prev = $dbh->selectrow_array($sql, undef, $key);
84 $sql = 'UPDATE meta SET val = ? WHERE key = ?';
85 $dbh->do($sql, undef, $value, $key);
87 $sql = 'INSERT INTO meta (key,val) VALUES (?,?)';
88 $dbh->do($sql, undef, $key, $value);
94 my ($self, $commit) = @_;
95 $self->meta_accessor('last_commit', $commit);
98 # v2 uses this to keep track of how up-to-date Xapian is
99 # old versions may be automatically GC'ed away in the future,
100 # but it's a trivial amount of storage.
101 sub last_commit_xap {
102 my ($self, $version, $i, $commit) = @_;
103 $self->meta_accessor("last_xap$version-$i", $commit);
107 my ($self, $second) = @_;
108 $self->meta_accessor('created_at', $second);
112 my ($self, $mid) = @_;
113 my $dbh = $self->{dbh};
114 my $sth = $dbh->prepare_cached(<<'');
115 INSERT OR IGNORE INTO msgmap (mid) VALUES (?)
117 return if $sth->execute($mid) == 0;
118 $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
122 my ($self, $num) = @_;
123 my $dbh = $self->{dbh};
124 my $sth = $self->{mid_for} ||=
125 $dbh->prepare('SELECT mid FROM msgmap WHERE num = ? LIMIT 1');
126 $sth->bind_param(1, $num);
128 $sth->fetchrow_array;
132 my ($self, $mid) = @_;
133 my $dbh = $self->{dbh};
134 my $sth = $self->{num_for} ||=
135 $dbh->prepare('SELECT num FROM msgmap WHERE mid = ? LIMIT 1');
136 $sth->bind_param(1, $mid);
138 $sth->fetchrow_array;
143 my $dbh = $self->{dbh};
144 # breaking MIN and MAX into separate queries speeds up from 250ms
145 # to around 700us with 2.7million messages.
146 my $sth = $dbh->prepare_cached('SELECT MIN(num) FROM msgmap', undef, 1);
148 my $min = $sth->fetchrow_array;
149 $sth = $dbh->prepare_cached('SELECT MAX(num) FROM msgmap', undef, 1);
151 ($min, $sth->fetchrow_array);
155 my ($self, $pfx, $limit) = @_;
157 die "No prefix given" unless (defined $pfx && $pfx ne '');
158 $pfx =~ s/([%_])/\\$1/g;
162 $limit += 0; # force to integer
165 $self->{dbh}->selectcol_arrayref('SELECT mid FROM msgmap ' .
166 'WHERE mid LIKE ? ESCAPE ? ' .
167 "ORDER BY num DESC LIMIT $limit",
172 my ($self, $mid) = @_;
173 my $dbh = $self->{dbh};
174 my $sth = $dbh->prepare('DELETE FROM msgmap WHERE mid = ?');
175 $sth->bind_param(1, $mid);
180 my ($self, $num) = @_;
181 my $dbh = $self->{dbh};
182 my $sth = $dbh->prepare('DELETE FROM msgmap WHERE num = ?');
183 $sth->bind_param(1, $num);
191 $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM msgmap;') };
192 defined $e or $dbh->do('CREATE TABLE msgmap (' .
193 'num INTEGER PRIMARY KEY AUTOINCREMENT, '.
194 'mid VARCHAR(1000) NOT NULL, ' .
197 $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM meta') };
198 defined $e or $dbh->do('CREATE TABLE meta (' .
199 'key VARCHAR(32) PRIMARY KEY, '.
200 'val VARCHAR(255) NOT NULL)');
205 my ($self, $num) = @_;
206 my $ids = $self->{dbh}->selectcol_arrayref(<<'', undef, $$num);
207 SELECT num FROM msgmap WHERE num > ?
208 ORDER BY num ASC LIMIT 1000
210 $$num = $ids->[-1] if @$ids;
215 my ($self, $beg, $end) = @_;
216 my $dbh = $self->{dbh};
217 my $attr = { Columns => [] };
218 my $mids = $dbh->selectall_arrayref(<<'', $attr, $$beg, $end);
219 SELECT num,mid FROM msgmap WHERE num >= ? AND num <= ?
222 $$beg = $mids->[-1]->[0] + 1 if @$mids;
226 # only used for mapping external serial numbers (e.g. articles from gmane)
227 # see scripts/xhdr-num2mid or PublicInbox::Filter::RubyLang for usage
229 my ($self, $num, $mid) = @_;
230 my $sth = $self->{mid_set} ||= do {
231 $self->{dbh}->prepare(
232 'INSERT OR IGNORE INTO msgmap (num,mid) VALUES (?,?)');
234 $sth->execute($num, $mid);
240 my $f = delete $self->{tmp_name};
241 if (defined $f && $self->{pid} == $$) {
242 unlink $f or warn "failed to unlink $f: $!\n";
248 my $f = $self->{tmp_name} or die "not a temporary clone\n";
249 delete $self->{dbh} and die "tmp_clone dbh not prepared for parent";
250 my $dbh = $self->{dbh} = dbh_new($f, 1);
251 $dbh->do('PRAGMA synchronous = OFF');
256 my $f = $self->{tmp_name} or die "not a temporary clone\n";
257 $self->{pid} == $$ or
258 die "BUG: atfork_prepare not called from $self->{pid}\n";
259 $self->{dbh} or die "temporary clone not open\n";
260 # must clobber prepared statements
261 %$self = (tmp_name => $f, pid => $$);