]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Msgmap.pm
msgmap: reduce constant usage
[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
52         my $sql = 'SELECT val FROM meta WHERE key = ? LIMIT 1';
53         my $dbh = $self->{dbh};
54         my $prev;
55         defined $value or return $dbh->selectrow_array($sql, undef, $key);
56
57         $prev = $dbh->selectrow_array($sql, undef, $key);
58
59         if (defined $prev) {
60                 $sql = 'UPDATE meta SET val = ? WHERE key = ? LIMIT 1';
61                 $dbh->do($sql, undef, $value, $key);
62         } else {
63                 $sql = 'INSERT INTO meta (key,val) VALUES (?,?)';
64                 $dbh->do($sql, undef, $key, $value);
65         }
66         $prev;
67 }
68
69 sub last_commit {
70         my ($self, $commit) = @_;
71         $self->meta_accessor('last_commit', $commit);
72 }
73
74 sub created_at {
75         my ($self, $second) = @_;
76         $self->meta_accessor('created_at', $second);
77 }
78
79 sub mid_insert {
80         my ($self, $mid) = @_;
81         my $dbh = $self->{dbh};
82         my $sql = 'INSERT OR IGNORE INTO msgmap (mid) VALUES (?)';
83         my $sth = $self->{mid_insert} ||= $dbh->prepare($sql);
84         $sth->bind_param(1, $mid);
85         return if $sth->execute == 0;
86         $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
87 }
88
89 sub mid_for {
90         my ($self, $num) = @_;
91         my $dbh = $self->{dbh};
92         my $sth = $self->{mid_for} ||=
93                 $dbh->prepare('SELECT mid FROM msgmap WHERE num = ? LIMIT 1');
94         $sth->bind_param(1, $num);
95         $sth->execute;
96         $sth->fetchrow_array;
97 }
98
99 sub num_for {
100         my ($self, $mid) = @_;
101         my $dbh = $self->{dbh};
102         my $sth = $self->{num_for} ||=
103                 $dbh->prepare('SELECT num FROM msgmap WHERE mid = ? LIMIT 1');
104         $sth->bind_param(1, $mid);
105         $sth->execute;
106         $sth->fetchrow_array;
107 }
108
109 sub minmax {
110         my ($self) = @_;
111         my $dbh = $self->{dbh};
112         my $sth = $self->{num_minmax} ||=
113                 $dbh->prepare('SELECT MIN(num),MAX(num) FROM msgmap');
114         $sth->execute;
115         $sth->fetchrow_array;
116 }
117
118 sub mid_prefixes {
119         my ($self, $pfx, $limit) = @_;
120
121         die "No prefix given" unless (defined $pfx && $pfx ne '');
122         $pfx =~ s/([%_])/\\$1/g;
123         $pfx .= '%';
124
125         $limit ||= 100;
126         $limit += 0; # force to integer
127         $limit ||= 100;
128
129         $self->{dbh}->selectcol_arrayref('SELECT mid FROM msgmap ' .
130                                          'WHERE mid LIKE ? ESCAPE ? ' .
131                                          "ORDER BY num DESC LIMIT $limit",
132                                          undef, $pfx, '\\');
133 }
134
135 sub mid_delete {
136         my ($self, $mid) = @_;
137         my $dbh = $self->{dbh};
138         my $sth = $dbh->prepare('DELETE FROM msgmap WHERE mid = ?');
139         $sth->bind_param(1, $mid);
140         $sth->execute;
141 }
142
143 sub create_tables {
144         my ($dbh) = @_;
145         my $e;
146
147         $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM msgmap;') };
148         defined $e or $dbh->do('CREATE TABLE msgmap (' .
149                         'num INTEGER PRIMARY KEY AUTOINCREMENT, '.
150                         'mid VARCHAR(1000) NOT NULL, ' .
151                         'UNIQUE (mid) )');
152
153         $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM meta') };
154         defined $e or $dbh->do('CREATE TABLE meta (' .
155                         'key VARCHAR(32) PRIMARY KEY, '.
156                         'val VARCHAR(255) NOT NULL)');
157 }
158
159 # used by NNTP.pm
160 sub id_batch {
161         my ($self, $num, $cb) = @_;
162         my $dbh = $self->{dbh};
163         my $sth = $dbh->prepare('SELECT num FROM msgmap WHERE num > ? '.
164                                 'ORDER BY num ASC LIMIT 1000');
165         $sth->execute($num);
166         my $ary = $sth->fetchall_arrayref;
167         @$ary = map { $_->[0] } @$ary;
168         my $nr = scalar @$ary;
169         $cb->($ary) if $nr;
170         $nr;
171 }
172
173 # only used for mapping external serial numbers (e.g. articles from gmane)
174 # see scripts/xhdr-num2mid or PublicInbox::Filter::RubyLang for usage
175 sub mid_set {
176         my ($self, $num, $mid) = @_;
177         my $sth = $self->{mid_set} ||= do {
178                 $self->{dbh}->prepare(
179                         'INSERT OR IGNORE INTO msgmap (num,mid) VALUES (?,?)');
180         };
181         $sth->execute($num, $mid);
182 }
183
184 1;