X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;ds=sidebyside;f=lib%2FPublicInbox%2FSharedKV.pm;h=90ccf2b4388c34dc48b21ad647c60b47b1ab98c0;hb=2231c8b183be0be5d8a9738a3e417b5c3a09c7c7;hp=95a3cb147d2973fa3c91e84593936c487d9723c6;hpb=14fa0abdcc7b6513540e529375e53edd74ce13e8;p=public-inbox.git diff --git a/lib/PublicInbox/SharedKV.pm b/lib/PublicInbox/SharedKV.pm index 95a3cb14..90ccf2b4 100644 --- a/lib/PublicInbox/SharedKV.pm +++ b/lib/PublicInbox/SharedKV.pm @@ -9,7 +9,7 @@ use strict; use v5.10.1; use parent qw(PublicInbox::Lock); use File::Temp qw(tempdir); -use DBI (); +use DBI qw(:sql_types); # SQL_BLOB use PublicInbox::Spawn; use File::Path qw(rmtree make_path); @@ -56,18 +56,15 @@ sub new { $self; } -sub index_values { - my ($self) = @_; - my $lock = $self->lock_for_scope_fast; - $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_fast; - my $e = $self->{dbh}->prepare_cached(<<'')->execute($key, $val); + my $sth = $self->{dbh}->prepare_cached(<<''); INSERT OR IGNORE INTO kv (k,v) VALUES (?, ?) + $sth->bind_param(1, $key, SQL_BLOB); + $sth->bind_param(2, $val, SQL_BLOB); + my $e = $sth->execute; $e == 0 ? undef : $e; } @@ -94,36 +91,30 @@ sub keys { } else { @pfx = (); # [0] may've been undef } - map { $_->[0] } @{$self->dbh->selectall_arrayref($sql, undef, @pfx)}; -} - -sub delete_by_val { - my ($self, $val, $lock) = @_; - $lock //= $self->lock_for_scope_fast; - $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_fast; - $self->{dbh}->prepare_cached(<<'')->execute($newval, $oldval) + 0; -UPDATE kv SET v = ? WHERE v = ? - + my $sth = $self->dbh->prepare($sql); + if (@pfx) { + $sth->bind_param(1, $pfx[0], SQL_BLOB); + $sth->bind_param(2, $pfx[1]); + } + $sth->execute; + map { $_->[0] } @{$sth->fetchall_arrayref}; } sub set { my ($self, $key, $val) = @_; if (defined $val) { - my $e = $self->{dbh}->prepare_cached(<<'')->execute($key, $val); + my $sth = $self->{dbh}->prepare_cached(<<''); INSERT OR REPLACE INTO kv (k,v) VALUES (?,?) + $sth->bind_param(1, $key, SQL_BLOB); + $sth->bind_param(2, $val, SQL_BLOB); + my $e = $sth->execute; $e == 0 ? undef : $e; } else { - $self->{dbh}->prepare_cached(<<'')->execute($key); + my $sth = $self->{dbh}->prepare_cached(<<''); DELETE FROM kv WHERE k = ? + $sth->bind_param(1, $key, SQL_BLOB); } } @@ -132,7 +123,8 @@ sub get { my $sth = $self->{dbh}->prepare_cached(<<'', undef, 1); SELECT v FROM kv WHERE k = ? - $sth->execute($key); + $sth->bind_param(1, $key, SQL_BLOB); + $sth->execute; $sth->fetchrow_array; } @@ -143,9 +135,11 @@ sub xchg { if (defined $newval) { set($self, $key, $newval); } else { - $self->{dbh}->prepare_cached(<<'')->execute($key); + my $sth = $self->{dbh}->prepare_cached(<<''); DELETE FROM kv WHERE k = ? + $sth->bind_param(1, $key, SQL_BLOB); + $sth->execute; } $oldval; }