1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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;
17 my ($class, $git_dir, $writable) = @_;
18 my $d = "$git_dir/public-inbox";
19 if ($writable && !-d $d && !mkdir $d) {
21 -d $d or die "$d not created: $err";
23 my $f = "$d/msgmap.sqlite3";
24 my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
28 ReadOnly => !$writable,
29 sqlite_use_immediate_transaction => 1,
31 $dbh->do('PRAGMA case_sensitive_like = ON');
32 my $self = bless { dbh => $dbh }, $class;
36 $self->created_at(time) unless $self->created_at;
42 my ($self, $key, $value) = @_;
44 meta_select => 'SELECT val FROM meta WHERE key = ? LIMIT 1',
45 meta_update => 'UPDATE meta SET val = ? WHERE key = ? LIMIT 1',
46 meta_insert => 'INSERT INTO meta (key,val) VALUES (?,?)',
49 my $dbh = $self->{dbh};
52 return $dbh->selectrow_array(meta_select, undef, $key);
56 $prev = $dbh->selectrow_array(meta_select, undef, $key);
59 $dbh->do(meta_update, undef, $value, $key);
61 $dbh->do(meta_insert, undef, $key, $value);
66 return $prev unless $err;
73 my ($self, $commit) = @_;
74 $self->meta_accessor('last_commit', $commit);
78 my ($self, $second) = @_;
79 $self->meta_accessor('created_at', $second);
83 my ($self, $mid) = @_;
84 my $dbh = $self->{dbh};
85 use constant MID_INSERT => 'INSERT INTO msgmap (mid) VALUES (?)';
86 my $sth = $self->{mid_insert} ||= $dbh->prepare(MID_INSERT);
87 $sth->bind_param(1, $mid);
89 $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
93 my ($self, $num) = @_;
94 my $dbh = $self->{dbh};
95 use constant MID_FOR => 'SELECT mid FROM msgmap WHERE num = ? LIMIT 1';
96 my $sth = $self->{mid_for} ||= $dbh->prepare(MID_FOR);
97 $sth->bind_param(1, $num);
103 my ($self, $mid) = @_;
104 my $dbh = $self->{dbh};
105 use constant NUM_FOR => 'SELECT num FROM msgmap WHERE mid = ? LIMIT 1';
106 my $sth = $self->{num_for} ||= $dbh->prepare(NUM_FOR);
107 $sth->bind_param(1, $mid);
109 $sth->fetchrow_array;
114 my $dbh = $self->{dbh};
115 use constant NUM_MINMAX => 'SELECT MIN(num),MAX(num) FROM msgmap';
116 my $sth = $self->{num_minmax} ||= $dbh->prepare(NUM_MINMAX);
118 $sth->fetchrow_array;
122 my ($self, $pfx, $limit) = @_;
124 die "No prefix given" unless (defined $pfx && $pfx ne '');
125 $pfx =~ s/([%_])/\\$1/g;
129 $limit += 0; # force to integer
132 $self->{dbh}->selectcol_arrayref('SELECT mid FROM msgmap ' .
133 'WHERE mid LIKE ? ESCAPE ? ' .
134 "ORDER BY num DESC LIMIT $limit",
139 my ($self, $mid) = @_;
140 my $dbh = $self->{dbh};
141 use constant MID_DELETE => 'DELETE FROM msgmap WHERE mid = ?';
142 my $sth = $dbh->prepare(MID_DELETE);
143 $sth->bind_param(1, $mid);
151 $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM msgmap;') };
152 defined $e or $dbh->do('CREATE TABLE msgmap (' .
153 'num INTEGER PRIMARY KEY AUTOINCREMENT, '.
154 'mid VARCHAR(1000) NOT NULL, ' .
157 $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM meta') };
158 defined $e or $dbh->do('CREATE TABLE meta (' .
159 'key VARCHAR(32) PRIMARY KEY, '.
160 'val VARCHAR(255) NOT NULL)');
164 my ($self, $num, $cb) = @_;
165 my $dbh = $self->{dbh};
166 my $sth = $dbh->prepare('SELECT num FROM msgmap WHERE num > ? '.
167 'ORDER BY num ASC LIMIT 1000');
169 my $ary = $sth->fetchall_arrayref;
170 @$ary = map { $_->[0] } @$ary;
171 my $nr = scalar @$ary;