]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SharedKV.pm
lei: avoid SQLite COUNT() for dedupe
[public-inbox.git] / lib / PublicInbox / SharedKV.pm
index b0588060435aae9525c6480b029d12f49b059988..3487e82086f47e2049d29b7f7d74bb8a3806aa33 100644 (file)
@@ -11,7 +11,7 @@ use parent qw(PublicInbox::Lock);
 use File::Temp qw(tempdir);
 use DBI ();
 use PublicInbox::Spawn;
-use File::Path qw(rmtree);
+use File::Path qw(rmtree make_path);
 
 sub dbh {
        my ($self, $lock) = @_;
@@ -27,7 +27,9 @@ sub dbh {
                });
                my $opt = $self->{opt} // {};
                $dbh->do('PRAGMA synchronous = OFF') if !$opt->{fsync};
-               $dbh->do('PRAGMA cache_size = '.($opt->{cache_size} || 80000));
+               if (my $s = $opt->{cache_size}) {
+                       $dbh->do("PRAGMA cache_size = $s");
+               }
                $dbh->do('PRAGMA journal_mode = '.
                                ($opt->{journal_mode} // 'WAL'));
                $dbh->do(<<'');
@@ -44,8 +46,8 @@ CREATE TABLE IF NOT EXISTS kv (
 sub new {
        my ($cls, $dir, $base, $opt) = @_;
        my $self = bless { opt => $opt }, $cls;
+       make_path($dir) if defined($dir) && !-d $dir;
        $dir //= $self->{"tmp$$.$self"} = tempdir("skv.$$-XXXX", TMPDIR => 1);
-       -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";
@@ -81,6 +83,15 @@ SELECT k,v FROM kv
        $sth
 }
 
+sub keys {
+       my ($self) = @_;
+       my $sth = $self->dbh->prepare_cached(<<'', undef, 1);
+SELECT k FROM kv
+
+       $sth->execute;
+       map { $_->[0] } @{$sth->fetchall_arrayref};
+}
+
 sub delete_by_val {
        my ($self, $val, $lock) = @_;
        $lock //= $self->lock_for_scope_fast;
@@ -143,6 +154,13 @@ SELECT COUNT(k) FROM kv
        $sth->fetchrow_array;
 }
 
+# faster than ->count due to how SQLite works
+sub has_entries {
+       my ($self) = @_;
+       my @n = $self->{dbh}->selectrow_array('SELECT k FROM kv LIMIT 1');
+       scalar(@n) ? 1 : undef;
+}
+
 sub dbh_release {
        my ($self, $lock) = @_;
        my $dbh = delete $self->{dbh} or return;