From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 1B8751FA19 for ; Thu, 31 Dec 2020 13:51:55 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 06/36] sharedkv: split out index_values Date: Thu, 31 Dec 2020 13:51:24 +0000 Message-Id: <20201231135154.6070-7-e@80x24.org> In-Reply-To: <20201231135154.6070-1-e@80x24.org> References: <20201231135154.6070-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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');