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,
43 my ($class, $f, $writable) = @_;
44 return if !$writable && !-r $f;
46 my $dbh = dbh_new($f, $writable);
47 my $self = bless { dbh => $dbh }, $class;
52 $self->created_at(time) unless $self->created_at;
54 my (undef, $max) = $self->minmax();
56 $self->num_highwater($max);
62 # used to keep track of used numeric mappings for v2 reindex
65 my ($fh, $fn) = tempfile('msgmap-XXXXXXXX', EXLOCK => 0, TMPDIR => 1);
66 $self->{dbh}->sqlite_backup_to_file($fn);
67 my $tmp = ref($self)->new_file($fn, 1);
68 $tmp->{dbh}->do('PRAGMA synchronous = OFF');
69 $tmp->{tmp_name} = $fn; # SQLite won't work if unlinked, apparently
71 close $fh or die "failed to close $fn: $!";
75 # n.b. invoked directly by scripts/xhdr-num2mid
77 my ($self, $key, $value) = @_;
79 my $sql = 'SELECT val FROM meta WHERE key = ? LIMIT 1';
80 my $dbh = $self->{dbh};
82 defined $value or return $dbh->selectrow_array($sql, undef, $key);
84 $prev = $dbh->selectrow_array($sql, undef, $key);
87 $sql = 'UPDATE meta SET val = ? WHERE key = ?';
88 $dbh->do($sql, undef, $value, $key);
90 $sql = 'INSERT INTO meta (key,val) VALUES (?,?)';
91 $dbh->do($sql, undef, $key, $value);
97 my ($self, $commit) = @_;
98 $self->meta_accessor('last_commit', $commit);
101 # v2 uses this to keep track of how up-to-date Xapian is
102 # old versions may be automatically GC'ed away in the future,
103 # but it's a trivial amount of storage.
104 sub last_commit_xap {
105 my ($self, $version, $i, $commit) = @_;
106 $self->meta_accessor("last_xap$version-$i", $commit);
110 my ($self, $second) = @_;
111 $self->meta_accessor('created_at', $second);
115 my ($self, $num) = @_;
116 my $high = $self->{num_highwater} ||=
117 $self->meta_accessor('num_highwater');
118 if (defined($num) && (!defined($high) || ($num > $high))) {
119 $self->{num_highwater} = $num;
120 $self->meta_accessor('num_highwater', $num);
122 $self->{num_highwater};
126 my ($self, $mid) = @_;
127 my $dbh = $self->{dbh};
128 my $sth = $dbh->prepare_cached(<<'');
129 INSERT INTO msgmap (mid) VALUES (?)
131 return unless eval { $sth->execute($mid) };
132 my $num = $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
133 $self->num_highwater($num) if defined($num);
138 my ($self, $num) = @_;
139 my $dbh = $self->{dbh};
140 my $sth = $self->{mid_for} ||=
141 $dbh->prepare('SELECT mid FROM msgmap WHERE num = ? LIMIT 1');
142 $sth->bind_param(1, $num);
144 $sth->fetchrow_array;
148 my ($self, $mid) = @_;
149 my $dbh = $self->{dbh};
150 my $sth = $self->{num_for} ||=
151 $dbh->prepare('SELECT num FROM msgmap WHERE mid = ? LIMIT 1');
152 $sth->bind_param(1, $mid);
154 $sth->fetchrow_array;
159 my $dbh = $self->{dbh};
160 # breaking MIN and MAX into separate queries speeds up from 250ms
161 # to around 700us with 2.7million messages.
162 my $sth = $dbh->prepare_cached('SELECT MIN(num) FROM msgmap', undef, 1);
164 my $min = $sth->fetchrow_array;
165 $sth = $dbh->prepare_cached('SELECT MAX(num) FROM msgmap', undef, 1);
167 ($min, $sth->fetchrow_array);
171 my ($self, $mid) = @_;
172 my $dbh = $self->{dbh};
173 my $sth = $dbh->prepare('DELETE FROM msgmap WHERE mid = ?');
174 $sth->bind_param(1, $mid);
179 my ($self, $num) = @_;
180 my $dbh = $self->{dbh};
181 my $sth = $dbh->prepare('DELETE FROM msgmap WHERE num = ?');
182 $sth->bind_param(1, $num);
190 $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM msgmap;') };
191 defined $e or $dbh->do('CREATE TABLE msgmap (' .
192 'num INTEGER PRIMARY KEY AUTOINCREMENT, '.
193 'mid VARCHAR(1000) NOT NULL, ' .
196 $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM meta') };
197 defined $e or $dbh->do('CREATE TABLE meta (' .
198 'key VARCHAR(32) PRIMARY KEY, '.
199 'val VARCHAR(255) NOT NULL)');
204 my ($self, $num) = @_;
205 my $ids = $self->{dbh}->selectcol_arrayref(<<'', undef, $$num);
206 SELECT num FROM msgmap WHERE num > ?
207 ORDER BY num ASC LIMIT 1000
209 $$num = $ids->[-1] if @$ids;
214 my ($self, $beg, $end) = @_;
215 my $dbh = $self->{dbh};
216 my $attr = { Columns => [] };
217 my $mids = $dbh->selectall_arrayref(<<'', $attr, $$beg, $end);
218 SELECT num,mid FROM msgmap WHERE num >= ? AND num <= ?
219 ORDER BY num ASC LIMIT 1000
221 $$beg = $mids->[-1]->[0] + 1 if @$mids;
225 # only used for mapping external serial numbers (e.g. articles from gmane)
226 # see scripts/xhdr-num2mid or PublicInbox::Filter::RubyLang for usage
228 my ($self, $num, $mid) = @_;
229 my $sth = $self->{mid_set} ||= do {
230 $self->{dbh}->prepare(
231 'INSERT OR IGNORE INTO msgmap (num,mid) VALUES (?,?)');
233 my $result = $sth->execute($num, $mid);
234 $self->num_highwater($num) if (defined($result) && $result == 1);
241 my $f = delete $self->{tmp_name};
242 if (defined $f && $self->{pid} == $$) {
243 unlink $f or warn "failed to unlink $f: $!\n";
249 my $f = $self->{tmp_name} or die "not a temporary clone\n";
250 delete $self->{dbh} and die "tmp_clone dbh not prepared for parent";
251 my $dbh = $self->{dbh} = dbh_new($f, 1);
252 $dbh->do('PRAGMA synchronous = OFF');
257 my $f = $self->{tmp_name} or die "not a temporary clone\n";
258 $self->{pid} == $$ or
259 die "BUG: atfork_prepare not called from $self->{pid}\n";
260 $self->{dbh} or die "temporary clone not open\n";
261 # must clobber prepared statements
262 %$self = (tmp_name => $f, pid => $$);