From: Eric Wong Date: Thu, 31 Dec 2020 13:51:24 +0000 (+0000) Subject: sharedkv: split out index_values X-Git-Tag: v1.7.0~1465 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=a7539312d51443c9a705e64b16ac4fdcd4b17a6e;p=public-inbox.git sharedkv: split out index_values In most cases, we won't need to index by value, so don't waste cycles or space on it. --- diff --git a/lib/PublicInbox/SharedKV.pm b/lib/PublicInbox/SharedKV.pm index 52a7424e..983952f5 100644 --- a/lib/PublicInbox/SharedKV.pm +++ b/lib/PublicInbox/SharedKV.pm @@ -36,7 +36,6 @@ CREATE TABLE IF NOT EXISTS kv ( UNIQUE (k) ) - $dbh->do('CREATE INDEX IF NOT EXISTS idx_v ON kv (v)'); $dbh; } } @@ -59,6 +58,12 @@ sub new { $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; diff --git a/t/shared_kv.t b/t/shared_kv.t index 4b727462..ad901328 100644 --- a/t/shared_kv.t +++ b/t/shared_kv.t @@ -26,6 +26,7 @@ is($skv->get($dead), $cafe, 'get after xchg'); is($skv->xchg($dead, undef), $cafe, 'xchg to undef'); is($skv->get($dead), undef, 'get after xchg to undef'); is($skv->get($cafe), $dead, 'get after set_maybe'); +ok($skv->index_values, 'index_values works'); is($skv->replace_values($dead, $cafe), 1, 'replaced one by value'); is($skv->get($cafe), $cafe, 'value updated'); is($skv->replace_values($dead, $cafe), 0, 'replaced none by value');