]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Msgmap.pm
msgmap: mid_insert ignores duplicates instead of die-ing
[public-inbox.git] / lib / PublicInbox / Msgmap.pm
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3
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.
8 #
9 # This is maintained by ::SearchIdx
10 package PublicInbox::Msgmap;
11 use strict;
12 use warnings;
13 use DBI;
14 use DBD::SQLite;
15
16 sub new {
17         my ($class, $git_dir, $writable) = @_;
18         my $d = "$git_dir/public-inbox";
19         if ($writable && !-d $d && !mkdir $d) {
20                 my $err = $!;
21                 -d $d or die "$d not created: $err";
22         }
23         new_file($class, "$d/msgmap.sqlite3", $writable);
24 }
25
26 sub new_file {
27         my ($class, $f, $writable) = @_;
28
29         my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
30                 AutoCommit => 1,
31                 RaiseError => 1,
32                 PrintError => 0,
33                 ReadOnly => !$writable,
34                 sqlite_use_immediate_transaction => 1,
35         });
36         $dbh->do('PRAGMA case_sensitive_like = ON');
37         my $self = bless { dbh => $dbh }, $class;
38
39         if ($writable) {
40                 create_tables($dbh);
41                 $dbh->begin_work;
42                 $self->created_at(time) unless $self->created_at;
43                 $dbh->commit;
44         }
45         $self;
46 }
47
48 # n.b. invoked directly by scripts/xhdr-num2mid
49 sub meta_accessor {
50         my ($self, $key, $value) = @_;
51         use constant {
52                 meta_select => 'SELECT val FROM meta WHERE key = ? LIMIT 1',
53                 meta_update => 'UPDATE meta SET val = ? WHERE key = ? LIMIT 1',
54                 meta_insert => 'INSERT INTO meta (key,val) VALUES (?,?)',
55         };
56
57         my $dbh = $self->{dbh};
58         my $prev;
59         defined $value or
60                 return $dbh->selectrow_array(meta_select, undef, $key);
61
62         $prev = $dbh->selectrow_array(meta_select, undef, $key);
63
64         if (defined $prev) {
65                 $dbh->do(meta_update, undef, $value, $key);
66         } else {
67                 $dbh->do(meta_insert, undef, $key, $value);
68         }
69         $prev;
70 }
71
72 sub last_commit {
73         my ($self, $commit) = @_;
74         $self->meta_accessor('last_commit', $commit);
75 }
76
77 sub created_at {
78         my ($self, $second) = @_;
79         $self->meta_accessor('created_at', $second);
80 }
81
82 sub mid_insert {
83         my ($self, $mid) = @_;
84         my $dbh = $self->{dbh};
85         my $sql = 'INSERT OR IGNORE INTO msgmap (mid) VALUES (?)';
86         my $sth = $self->{mid_insert} ||= $dbh->prepare($sql);
87         $sth->bind_param(1, $mid);
88         return if $sth->execute == 0;
89         $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
90 }
91
92 sub mid_for {
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);
98         $sth->execute;
99         $sth->fetchrow_array;
100 }
101
102 sub num_for {
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);
108         $sth->execute;
109         $sth->fetchrow_array;
110 }
111
112 sub minmax {
113         my ($self) = @_;
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);
117         $sth->execute;
118         $sth->fetchrow_array;
119 }
120
121 sub mid_prefixes {
122         my ($self, $pfx, $limit) = @_;
123
124         die "No prefix given" unless (defined $pfx && $pfx ne '');
125         $pfx =~ s/([%_])/\\$1/g;
126         $pfx .= '%';
127
128         $limit ||= 100;
129         $limit += 0; # force to integer
130         $limit ||= 100;
131
132         $self->{dbh}->selectcol_arrayref('SELECT mid FROM msgmap ' .
133                                          'WHERE mid LIKE ? ESCAPE ? ' .
134                                          "ORDER BY num DESC LIMIT $limit",
135                                          undef, $pfx, '\\');
136 }
137
138 sub mid_delete {
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);
144         $sth->execute;
145 }
146
147 sub create_tables {
148         my ($dbh) = @_;
149         my $e;
150
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, ' .
155                         'UNIQUE (mid) )');
156
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)');
161 }
162
163 # used by NNTP.pm
164 sub id_batch {
165         my ($self, $num, $cb) = @_;
166         my $dbh = $self->{dbh};
167         my $sth = $dbh->prepare('SELECT num FROM msgmap WHERE num > ? '.
168                                 'ORDER BY num ASC LIMIT 1000');
169         $sth->execute($num);
170         my $ary = $sth->fetchall_arrayref;
171         @$ary = map { $_->[0] } @$ary;
172         my $nr = scalar @$ary;
173         $cb->($ary) if $nr;
174         $nr;
175 }
176
177 # only used for mapping external serial numbers (e.g. articles from gmane)
178 # see scripts/xhdr-num2mid for usage
179 sub mid_set {
180         my ($self, $num, $mid) = @_;
181         my $sth = $self->{mid_set} ||= do {
182                 my $sql = 'INSERT INTO msgmap (num, mid) VALUES (?,?)';
183                 $self->{dbh}->prepare($sql);
184         };
185         $sth->execute($num, $mid);
186 }
187
188 1;