]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Msgmap.pm
msgmap: fix use of transactions
[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         my $f = "$d/msgmap.sqlite3";
24         my $dbh = DBI->connect("dbi:SQLite:dbname=$f",'','', {
25                 AutoCommit => 1,
26                 RaiseError => 1,
27                 PrintError => 0,
28                 ReadOnly => !$writable,
29                 sqlite_use_immediate_transaction => 1,
30         });
31         $dbh->do('PRAGMA case_sensitive_like = ON');
32         my $self = bless { dbh => $dbh }, $class;
33
34         if ($writable) {
35                 create_tables($dbh);
36                 $dbh->begin_work;
37                 $self->created_at(time) unless $self->created_at;
38                 $dbh->commit;
39         }
40         $self;
41 }
42
43 sub meta_accessor {
44         my ($self, $key, $value) = @_;
45         use constant {
46                 meta_select => 'SELECT val FROM meta WHERE key = ? LIMIT 1',
47                 meta_update => 'UPDATE meta SET val = ? WHERE key = ? LIMIT 1',
48                 meta_insert => 'INSERT INTO meta (key,val) VALUES (?,?)',
49         };
50
51         my $dbh = $self->{dbh};
52         my $prev;
53         defined $value or
54                 return $dbh->selectrow_array(meta_select, undef, $key);
55
56         $prev = $dbh->selectrow_array(meta_select, undef, $key);
57
58         if (defined $prev) {
59                 $dbh->do(meta_update, undef, $value, $key);
60         } else {
61                 $dbh->do(meta_insert, undef, $key, $value);
62         }
63         $prev;
64 }
65
66 sub last_commit {
67         my ($self, $commit) = @_;
68         $self->meta_accessor('last_commit', $commit);
69 }
70
71 sub created_at {
72         my ($self, $second) = @_;
73         $self->meta_accessor('created_at', $second);
74 }
75
76 sub mid_insert {
77         my ($self, $mid) = @_;
78         my $dbh = $self->{dbh};
79         use constant MID_INSERT => 'INSERT INTO msgmap (mid) VALUES (?)';
80         my $sth = $self->{mid_insert} ||= $dbh->prepare(MID_INSERT);
81         $sth->bind_param(1, $mid);
82         $sth->execute;
83         $dbh->last_insert_id(undef, undef, 'msgmap', 'num');
84 }
85
86 sub mid_for {
87         my ($self, $num) = @_;
88         my $dbh = $self->{dbh};
89         use constant MID_FOR => 'SELECT mid FROM msgmap WHERE num = ? LIMIT 1';
90         my $sth = $self->{mid_for} ||= $dbh->prepare(MID_FOR);
91         $sth->bind_param(1, $num);
92         $sth->execute;
93         $sth->fetchrow_array;
94 }
95
96 sub num_for {
97         my ($self, $mid) = @_;
98         my $dbh = $self->{dbh};
99         use constant NUM_FOR => 'SELECT num FROM msgmap WHERE mid = ? LIMIT 1';
100         my $sth = $self->{num_for} ||= $dbh->prepare(NUM_FOR);
101         $sth->bind_param(1, $mid);
102         $sth->execute;
103         $sth->fetchrow_array;
104 }
105
106 sub minmax {
107         my ($self) = @_;
108         my $dbh = $self->{dbh};
109         use constant NUM_MINMAX => 'SELECT MIN(num),MAX(num) FROM msgmap';
110         my $sth = $self->{num_minmax} ||= $dbh->prepare(NUM_MINMAX);
111         $sth->execute;
112         $sth->fetchrow_array;
113 }
114
115 sub mid_prefixes {
116         my ($self, $pfx, $limit) = @_;
117
118         die "No prefix given" unless (defined $pfx && $pfx ne '');
119         $pfx =~ s/([%_])/\\$1/g;
120         $pfx .= '%';
121
122         $limit ||= 100;
123         $limit += 0; # force to integer
124         $limit ||= 100;
125
126         $self->{dbh}->selectcol_arrayref('SELECT mid FROM msgmap ' .
127                                          'WHERE mid LIKE ? ESCAPE ? ' .
128                                          "ORDER BY num DESC LIMIT $limit",
129                                          undef, $pfx, '\\');
130 }
131
132 sub mid_delete {
133         my ($self, $mid) = @_;
134         my $dbh = $self->{dbh};
135         use constant MID_DELETE => 'DELETE FROM msgmap WHERE mid = ?';
136         my $sth = $dbh->prepare(MID_DELETE);
137         $sth->bind_param(1, $mid);
138         $sth->execute;
139 }
140
141 sub create_tables {
142         my ($dbh) = @_;
143         my $e;
144
145         $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM msgmap;') };
146         defined $e or $dbh->do('CREATE TABLE msgmap (' .
147                         'num INTEGER PRIMARY KEY AUTOINCREMENT, '.
148                         'mid VARCHAR(1000) NOT NULL, ' .
149                         'UNIQUE (mid) )');
150
151         $e = eval { $dbh->selectrow_array('EXPLAIN SELECT * FROM meta') };
152         defined $e or $dbh->do('CREATE TABLE meta (' .
153                         'key VARCHAR(32) PRIMARY KEY, '.
154                         'val VARCHAR(255) NOT NULL)');
155 }
156
157 sub id_batch {
158         my ($self, $num, $cb) = @_;
159         my $dbh = $self->{dbh};
160         my $sth = $dbh->prepare('SELECT num FROM msgmap WHERE num > ? '.
161                                 'ORDER BY num ASC LIMIT 1000');
162         $sth->execute($num);
163         my $ary = $sth->fetchall_arrayref;
164         @$ary = map { $_->[0] } @$ary;
165         my $nr = scalar @$ary;
166         $cb->($ary) if $nr;
167         $nr;
168 }
169
170 1;