user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
blob d75d89989f3602b7410d5a900f5c50722374995a 3536 bytes (raw)
name: lib/PublicInbox/SharedKV.pm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 
# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>

# fork()-friendly key-value store.  Will be used for making
# augmenting Maildirs and mboxes less expensive, maybe.
# We use flock(2) to avoid SQLite lock problems (busy timeouts, backoff)
package PublicInbox::SharedKV;
use strict;
use v5.10.1;
use parent qw(PublicInbox::Lock);
use File::Temp 0.19 (); # 0.19 for ->newdir
use DBI ();
use PublicInbox::Spawn;

sub dbh {
	my ($self, $lock) = @_;
	$self->{dbh} //= do {
		my $f = $self->{filename};
		$lock //= $self->lock_for_scope;
		my $dbh = DBI->connect("dbi:SQLite:dbname=$f", '', '', {
			AutoCommit => 1,
			RaiseError => 1,
			PrintError => 0,
			sqlite_use_immediate_transaction => 1,
			# no sqlite_unicode here, this is for binary data
		});
		my $opt = $self->{opt} // {};
		$dbh->do('PRAGMA synchronous = OFF') if !$opt->{fsync};
		$dbh->do('PRAGMA cache_size = '.($opt->{cache_size} || 80000));
		$dbh->do('PRAGMA journal_mode = '.
				($opt->{journal_mode} // 'WAL'));
		$dbh->do(<<'');
CREATE TABLE IF NOT EXISTS kv (
	k VARBINARY PRIMARY KEY NOT NULL,
	v VARBINARY NOT NULL,
	UNIQUE (k)
)

		$dbh;
	}
}

sub new {
	my ($cls, $dir, $base, $opt) = @_;
	my $self = bless { opt => $opt }, $cls;
	unless (defined $dir) {
		$self->{tmp} = File::Temp->newdir('kv-XXXXXX', TMPDIR => 1);
		$dir = $self->{tmp}->dirname;
	}
	-d $dir or mkdir($dir) or die "mkdir($dir): $!";
	$base //= '';
	my $f = $self->{filename} = "$dir/$base.sqlite3";
	$self->{lock_path} = $opt->{lock_path} // "$dir/$base.flock";
	unless (-f $f) {
		open my $fh, '+>>', $f or die "failed to open $f: $!";
		PublicInbox::Spawn::nodatacow_fd(fileno($fh));
	}
	$self;
}

sub index_values {
	my ($self) = @_;
	my $lock = $self->lock_for_scope;
	$self->dbh($lock)->do('CREATE INDEX IF NOT EXISTS idx_v ON kv (v)');
}

sub set_maybe {
	my ($self, $key, $val, $lock) = @_;
	$lock //= $self->lock_for_scope;
	my $e = $self->{dbh}->prepare_cached(<<'')->execute($key, $val);
INSERT OR IGNORE INTO kv (k,v) VALUES (?, ?)

	$e == 0 ? undef : $e;
}

# caller calls sth->fetchrow_array
sub each_kv_iter {
	my ($self) = @_;
	my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
SELECT k,v FROM kv

	$sth->execute;
	$sth
}

sub delete_by_val {
	my ($self, $val, $lock) = @_;
	$lock //= $self->lock_for_scope;
	$self->{dbh}->prepare_cached(<<'')->execute($val) + 0;
DELETE FROM kv WHERE v = ?

}

sub replace_values {
	my ($self, $oldval, $newval, $lock) = @_;
	$lock //= $self->lock_for_scope;
	$self->{dbh}->prepare_cached(<<'')->execute($newval, $oldval) + 0;
UPDATE kv SET v = ? WHERE v = ?

}

sub set {
	my ($self, $key, $val) = @_;
	if (defined $val) {
		my $e = $self->{dbh}->prepare_cached(<<'')->execute($key, $val);
INSERT OR REPLACE INTO kv (k,v) VALUES (?,?)

		$e == 0 ? undef : $e;
	} else {
		$self->{dbh}->prepare_cached(<<'')->execute($key);
DELETE FROM kv WHERE k = ?

	}
}

sub get {
	my ($self, $key) = @_;
	my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1);
SELECT v FROM kv WHERE k = ?

	$sth->execute($key);
	$sth->fetchrow_array;
}

sub xchg {
	my ($self, $key, $newval, $lock) = @_;
	$lock //= $self->lock_for_scope;
	my $oldval = get($self, $key);
	if (defined $newval) {
		set($self, $key, $newval);
	} else {
		$self->{dbh}->prepare_cached(<<'')->execute($key);
DELETE FROM kv WHERE k = ?

	}
	$oldval;
}

sub count {
	my ($self) = @_;
	my $sth = $self->{dbh}->prepare_cached(<<'');
SELECT COUNT(k) FROM kv

	$sth->execute;
	$sth->fetchrow_array;
}

1;

debug log:

solving d75d8998 ...
found d75d8998 in https://80x24.org/public-inbox.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).