1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # fork()-friendly key-value store. Will be used for making
5 # augmenting Maildirs and mboxes less expensive, maybe.
6 # We use flock(2) to avoid SQLite lock problems (busy timeouts, backoff)
7 package PublicInbox::SharedKV;
10 use parent qw(PublicInbox::Lock);
11 use File::Temp 0.19 (); # 0.19 for ->newdir
13 use PublicInbox::Spawn;
16 my ($self, $lock) = @_;
18 my $f = $self->{filename};
19 $lock //= $self->lock_for_scope;
20 my $dbh = DBI->connect("dbi:SQLite:dbname=$f", '', '', {
24 sqlite_use_immediate_transaction => 1,
25 # no sqlite_unicode here, this is for binary data
27 my $opt = $self->{opt} // {};
28 $dbh->do('PRAGMA synchronous = OFF') if !$opt->{fsync};
29 $dbh->do('PRAGMA cache_size = '.($opt->{cache_size} || 80000));
30 $dbh->do('PRAGMA journal_mode = '.
31 ($opt->{journal_mode} // 'WAL'));
33 CREATE TABLE IF NOT EXISTS kv (
34 k VARBINARY PRIMARY KEY NOT NULL,
44 my ($cls, $dir, $base, $opt) = @_;
45 my $self = bless { opt => $opt }, $cls;
46 unless (defined $dir) {
47 $self->{tmp} = File::Temp->newdir('kv-XXXXXX', TMPDIR => 1);
48 $dir = $self->{tmp}->dirname;
50 -d $dir or mkdir($dir) or die "mkdir($dir): $!";
52 my $f = $self->{filename} = "$dir/$base.sqlite3";
53 $self->{lock_path} = $opt->{lock_path} // "$dir/$base.flock";
55 open my $fh, '+>>', $f or die "failed to open $f: $!";
56 PublicInbox::Spawn::nodatacow_fd(fileno($fh));
63 my $lock = $self->lock_for_scope;
64 $self->dbh($lock)->do('CREATE INDEX IF NOT EXISTS idx_v ON kv (v)');
68 my ($self, $key, $val, $lock) = @_;
69 $lock //= $self->lock_for_scope;
70 my $e = $self->{dbh}->prepare_cached(<<'')->execute($key, $val);
71 INSERT OR IGNORE INTO kv (k,v) VALUES (?, ?)
76 # caller calls sth->fetchrow_array
79 my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
87 my ($self, $val, $lock) = @_;
88 $lock //= $self->lock_for_scope;
89 $self->{dbh}->prepare_cached(<<'')->execute($val) + 0;
90 DELETE FROM kv WHERE v = ?
95 my ($self, $oldval, $newval, $lock) = @_;
96 $lock //= $self->lock_for_scope;
97 $self->{dbh}->prepare_cached(<<'')->execute($newval, $oldval) + 0;
98 UPDATE kv SET v = ? WHERE v = ?
103 my ($self, $key, $val) = @_;
105 my $e = $self->{dbh}->prepare_cached(<<'')->execute($key, $val);
106 INSERT OR REPLACE INTO kv (k,v) VALUES (?,?)
108 $e == 0 ? undef : $e;
110 $self->{dbh}->prepare_cached(<<'')->execute($key);
111 DELETE FROM kv WHERE k = ?
117 my ($self, $key) = @_;
118 my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
119 SELECT v FROM kv WHERE k = ?
122 $sth->fetchrow_array;
126 my ($self, $key, $newval, $lock) = @_;
127 $lock //= $self->lock_for_scope;
128 my $oldval = get($self, $key);
129 if (defined $newval) {
130 set($self, $key, $newval);
132 $self->{dbh}->prepare_cached(<<'')->execute($key);
133 DELETE FROM kv WHERE k = ?
141 my $sth = $self->{dbh}->prepare_cached(<<'');
142 SELECT COUNT(k) FROM kv
145 $sth->fetchrow_array;