1 # Copyright (C) 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 (v1) and ::V2Writable (v2)
10 package PublicInbox::Msgmap;
15 use PublicInbox::Over;
16 use Scalar::Util qw(blessed);
19 my ($class, $ibx, $rw) = @_;
23 $rw = 2 if $rw && $ibx->{-no_fsync};
27 return if !$rw && !-r $f;
29 my $self = bless { filename => $f }, $class;
30 my $dbh = $self->{dbh} = PublicInbox::Over::dbh_new($self, $rw);
34 unless ($self->created_at) {
38 -f "$ibx->{inboxdir}/inbox.config.example") {
39 $t = (stat(_))[9]; # mtime set by "curl -R"
41 $self->created_at($t // time);
43 $self->num_highwater(max($self));
49 # used to keep track of used numeric mappings for v2 reindex
51 my ($self, $dir) = @_;
53 my $tmp = "mm_tmp-$$-XXXX";
54 my ($fh, $fn) = File::Temp::tempfile($tmp, EXLOCK => 0, DIR => $dir);
55 require PublicInbox::Syscall;
56 PublicInbox::Syscall::nodatacow_fh($fh);
57 $self->{dbh}->sqlite_backup_to_file($fn);
58 $tmp = ref($self)->new_file($fn, 2);
59 $tmp->{dbh}->do('PRAGMA journal_mode = MEMORY');
64 # n.b. invoked directly by scripts/xhdr-num2mid
66 my ($self, $key, $value) = @_;
68 my $sql = 'SELECT val FROM meta WHERE key = ? LIMIT 1';
69 my $prev = $self->{dbh}->selectrow_array($sql, undef, $key);
70 $value // return $prev;
73 $sql = 'UPDATE meta SET val = ? WHERE key = ?';
74 $self->{dbh}->do($sql, undef, $value, $key);
76 $sql = 'INSERT INTO meta (key,val) VALUES (?,?)';
77 $self->{dbh}->do($sql, undef, $key, $value);
83 my ($self, $commit) = @_;
84 $self->meta_accessor('last_commit', $commit);
87 # v2 uses this to keep track of how up-to-date Xapian is
88 # old versions may be automatically GC'ed away in the future,
89 # but it's a trivial amount of storage.
91 my ($self, $version, $i, $commit) = @_;
92 $self->meta_accessor("last_xap$version-$i", $commit);
95 # this is the UIDVALIDITY for IMAP (cf. RFC 3501 sec 2.3.1.1. item 3)
97 my ($self, $second) = @_;
98 $self->meta_accessor('created_at', $second);
102 my ($self, $num) = @_;
103 my $high = $self->meta_accessor('num_highwater');
104 if (defined($num) && (!defined($high) || ($num > $high))) {
106 $self->meta_accessor('num_highwater', $num);
112 my ($self, $mid) = @_;
113 my $sth = $self->{dbh}->prepare_cached(<<'');
114 INSERT INTO msgmap (mid) VALUES (?)
116 return unless eval { $sth->execute($mid) };
117 my $num = $self->{dbh}->last_insert_id(undef, undef, 'msgmap', 'num');
118 $self->num_highwater($num) if defined($num);
123 my ($self, $num) = @_;
124 my $sth = $self->{dbh}->prepare_cached(<<"", undef, 1);
125 SELECT mid FROM msgmap WHERE num = ? LIMIT 1
128 $sth->fetchrow_array;
132 my ($self, $mid) = @_;
133 my $sth = $self->{dbh}->prepare_cached(<<"", undef, 1);
134 SELECT num FROM msgmap WHERE mid = ? LIMIT 1
137 $sth->fetchrow_array;
141 my $sth = $_[0]->{dbh}->prepare_cached('SELECT MAX(num) FROM msgmap',
144 $sth->fetchrow_array // 0;
148 # breaking MIN and MAX into separate queries speeds up from 250ms
149 # to around 700us with 2.7million messages.
150 my $sth = $_[0]->{dbh}->prepare_cached('SELECT MIN(num) FROM msgmap',
153 ($sth->fetchrow_array // 0, max($_[0]));
157 my ($self, $mid) = @_;
158 $self->{dbh}->do('DELETE FROM msgmap WHERE mid = ?', undef, $mid);
162 my ($self, $num) = @_;
163 $self->{dbh}->do('DELETE FROM msgmap WHERE num = ?', undef, $num);
170 CREATE TABLE IF NOT EXISTS msgmap (
171 num INTEGER PRIMARY KEY AUTOINCREMENT,
172 mid VARCHAR(1000) NOT NULL,
177 CREATE TABLE IF NOT EXISTS meta (
178 key VARCHAR(32) PRIMARY KEY,
179 val VARCHAR(255) NOT NULL
185 my ($self, $beg, $end, $cols) = @_;
187 my $attr = { Columns => [] };
188 my $mids = $self->{dbh}->selectall_arrayref(<<"", $attr, $$beg, $end);
189 SELECT $cols FROM msgmap WHERE num >= ? AND num <= ?
190 ORDER BY num ASC LIMIT 1000
192 $$beg = $mids->[-1]->[0] + 1 if @$mids;
196 # only used for mapping external serial numbers (e.g. articles from gmane)
197 # see scripts/xhdr-num2mid or PublicInbox::Filter::RubyLang for usage
199 my ($self, $num, $mid) = @_;
200 my $sth = $self->{dbh}->prepare_cached(<<"");
201 INSERT OR IGNORE INTO msgmap (num,mid) VALUES (?,?)
203 my $result = $sth->execute($num, $mid);
204 $self->num_highwater($num) if (defined($result) && $result == 1);
210 my $dbh = $self->{dbh} or return;
211 if (($self->{pid} // 0) == $$) {
212 my $f = $dbh->sqlite_db_filename;
213 unlink $f or warn "failed to unlink $f: $!\n";
219 $self->{pid} or die 'BUG: not a temporary clone';
220 $self->{dbh} and die 'BUG: tmp_clone dbh not prepared for parent';
221 defined($self->{filename}) or die 'BUG: {filename} not defined';
222 $self->{dbh} = PublicInbox::Over::dbh_new($self, 2);
223 $self->{dbh}->do('PRAGMA journal_mode = MEMORY');
228 my $pid = $self->{pid} or die 'BUG: not a temporary clone';
229 $pid == $$ or die "BUG: atfork_prepare not called by $pid";
230 my $dbh = $self->{dbh} or die 'BUG: temporary clone not open';
232 # must clobber prepared statements
233 %$self = (filename => $dbh->sqlite_db_filename, pid => $pid);
237 my ($self, $skip_artnum) = @_;
238 return meta_accessor($self, 'skip_artnum') if !defined($skip_artnum);
240 my $cur = num_highwater($self) // 0;
241 if ($skip_artnum < $cur) {
242 die "E: current article number $cur ",
243 "exceeds --skip-artnum=$skip_artnum\n";
247 my $mid = 'skip'.rand.'@'.rand.'.example.com';
248 $ok = mid_set($self, $skip_artnum, $mid);
250 mid_delete($self, $mid);
254 $ok or die '--skip-artnum failed';
256 # in the future, the indexer may use this value for
257 # new messages in old epochs
258 meta_accessor($self, 'skip_artnum', $skip_artnum);
264 $self->{dbh} // return;
265 my $rw = !$self->{dbh}->{ReadOnly};
266 PublicInbox::Over::check_inodes($self);
267 $self->{dbh} //= PublicInbox::Over::dbh_new($self, !$rw);