From a7539312d51443c9a705e64b16ac4fdcd4b17a6e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 31 Dec 2020 13:51:24 +0000 Subject: [PATCH] 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. --- lib/PublicInbox/SharedKV.pm | 7 ++++++- t/shared_kv.t | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) 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'); -- 2.44.0