]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
v2writable: improve "num_for" API and disambiguate
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index 7a89093cad8a3b58fe0346c5e55a75ee98215c1d..9d5078285ac9634a115685a96e03b5f157078ee9 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # This interface wraps and mimics PublicInbox::Import
@@ -24,7 +24,7 @@ use IO::Handle;
 my $PACKING_FACTOR = 0.4;
 
 # SATA storage lags behind what CPUs are capable of, so relying on
-# nproc(1) can be misleading and having extra Xapian partions is a
+# nproc(1) can be misleading and having extra Xapian shards is a
 # waste of FDs and space.  It can also lead to excessive IO latency
 # and slow things down.  Users on NVME or other fast storage can
 # use the NPROC env or switches in our script/public-inbox-* programs
@@ -57,7 +57,7 @@ sub count_shards ($) {
        my $n = 0;
        my $xpfx = $self->{xpfx};
 
-       # always load existing partitions in case core count changes:
+       # always load existing shards in case core count changes:
        # Also, shard count may change while -watch is running
        # due to "xcpdb --reshard"
        if (-d $xpfx) {
@@ -76,7 +76,7 @@ sub new {
        # $creat may be any true value, or 0/undef.  A hashref is true,
        # and $creat->{nproc} may be set to an integer
        my ($class, $v2ibx, $creat) = @_;
-       my $dir = $v2ibx->{mainrepo} or die "no mainrepo in inbox\n";
+       my $dir = $v2ibx->{inboxdir} or die "no inboxdir in inbox\n";
        unless (-d $dir) {
                if ($creat) {
                        require File::Path;
@@ -155,8 +155,7 @@ sub _add {
        # leaking FDs to it...
        $self->idx_init;
 
-       my $mid0;
-       my $num = num_for($self, $mime, \$mid0);
+       my ($num, $mid0) = v2_num_for($self, $mime);
        defined $num or return; # duplicate
        defined $mid0 or die "BUG: $mid0 undefined\n";
        my $im = $self->importer;
@@ -172,16 +171,15 @@ sub _add {
        $cmt;
 }
 
-sub num_for {
-       my ($self, $mime, $mid0) = @_;
+sub v2_num_for {
+       my ($self, $mime) = @_;
        my $mids = mids($mime->header_obj);
        if (@$mids) {
                my $mid = $mids->[0];
                my $num = $self->{mm}->mid_insert($mid);
                if (defined $num) { # common case
-                       $$mid0 = $mid;
-                       return $num;
-               };
+                       return ($num, $mid);
+               }
 
                # crap, Message-ID is already known, hope somebody just resent:
                foreach my $m (@$mids) {
@@ -190,7 +188,7 @@ sub num_for {
                        # easy, don't store duplicates
                        # note: do not add more diagnostic info here since
                        # it gets noisy on public-inbox-watch restarts
-                       return if $existing;
+                       return () if $existing;
                }
 
                # AltId may pre-populate article numbers (e.g. X-Mail-Count
@@ -201,8 +199,7 @@ sub num_for {
                        my $num = $self->{mm}->num_for($mid);
 
                        if (defined $num && !$self->{over}->get_art($num)) {
-                               $$mid0 = $mid;
-                               return $num;
+                               return ($num, $mid);
                        }
                }
 
@@ -215,39 +212,38 @@ sub num_for {
                        $num = $self->{mm}->mid_insert($m);
                        if (defined $num) {
                                warn "alternative <$m> for <$mid> found\n";
-                               $$mid0 = $m;
-                               return $num;
+                               return ($num, $m);
                        }
                }
        }
        # none of the existing Message-IDs are good, generate a new one:
-       num_for_harder($self, $mime, $mid0);
+       v2_num_for_harder($self, $mime);
 }
 
-sub num_for_harder {
-       my ($self, $mime, $mid0) = @_;
+sub v2_num_for_harder {
+       my ($self, $mime) = @_;
 
        my $hdr = $mime->header_obj;
        my $dig = content_digest($mime);
-       $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
-       my $num = $self->{mm}->mid_insert($$mid0);
+       my $mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
+       my $num = $self->{mm}->mid_insert($mid0);
        unless (defined $num) {
                # it's hard to spoof the last Received: header
                my @recvd = $hdr->header_raw('Received');
                $dig->add("Received: $_") foreach (@recvd);
-               $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
-               $num = $self->{mm}->mid_insert($$mid0);
+               $mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
+               $num = $self->{mm}->mid_insert($mid0);
 
                # fall back to a random Message-ID and give up determinism:
                until (defined($num)) {
                        $dig->add(rand);
-                       $$mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
-                       warn "using random Message-ID <$$mid0> as fallback\n";
-                       $num = $self->{mm}->mid_insert($$mid0);
+                       $mid0 = PublicInbox::Import::digest2mid($dig, $hdr);
+                       warn "using random Message-ID <$mid0> as fallback\n";
+                       $num = $self->{mm}->mid_insert($mid0);
                }
        }
-       PublicInbox::Import::append_mid($hdr, $$mid0);
-       $num;
+       PublicInbox::Import::append_mid($hdr, $mid0);
+       ($num, $mid0);
 }
 
 sub idx_shard {
@@ -292,7 +288,7 @@ sub idx_init {
                        $self->{shards} = $nshards;
                }
 
-               # need to create all parts before initializing msgmap FD
+               # need to create all shards before initializing msgmap FD
                my $max = $self->{shards} - 1;
 
                # idx_shards must be visible to all forked processes
@@ -304,7 +300,7 @@ sub idx_init {
                # Now that all subprocesses are up, we can open the FDs
                # for SQLite:
                my $mm = $self->{mm} = PublicInbox::Msgmap->new_file(
-                       "$self->{-inbox}->{mainrepo}/msgmap.sqlite3", 1);
+                       "$self->{-inbox}->{inboxdir}/msgmap.sqlite3", 1);
                $mm->{dbh}->begin_work;
        });
 }
@@ -315,7 +311,7 @@ sub idx_init {
 sub _replace_oids ($$$) {
        my ($self, $mime, $replace_map) = @_;
        $self->done;
-       my $pfx = "$self->{-inbox}->{mainrepo}/git";
+       my $pfx = "$self->{-inbox}->{inboxdir}/git";
        my $rewrites = []; # epoch => commit
        my $max = $self->{epoch_max};
 
@@ -616,17 +612,17 @@ sub checkpoint ($;$) {
                if ($wait) {
                        my $barrier = $self->barrier_init(scalar @$shards);
 
-                       # each partition needs to issue a barrier command
+                       # each shard needs to issue a barrier command
                        $_->remote_barrier for @$shards;
 
-                       # wait for each Xapian partition
+                       # wait for each Xapian shard
                        $self->barrier_wait($barrier);
                } else {
                        $_->remote_commit for @$shards;
                }
 
                # last_commit is special, don't commit these until
-               # remote partitions are done:
+               # remote shards are done:
                $dbh->begin_work;
                set_last_commits($self);
                $dbh->commit;
@@ -663,16 +659,12 @@ sub done {
 sub fill_alternates ($$) {
        my ($self, $epoch) = @_;
 
-       my $pfx = "$self->{-inbox}->{mainrepo}/git";
-       my $all = "$self->{-inbox}->{mainrepo}/all.git";
-       my @cmd;
+       my $pfx = "$self->{-inbox}->{inboxdir}/git";
+       my $all = "$self->{-inbox}->{inboxdir}/all.git";
+
        unless (-d $all) {
                PublicInbox::Import::init_bare($all);
        }
-       @cmd = (qw/git config/, "--file=$pfx/$epoch.git/config",
-                       'include.path', '../../all.git/config');
-       PublicInbox::Import::run_die(\@cmd);
-
        my $alt = "$all/objects/info/alternates";
        my %alts;
        my @add;
@@ -694,9 +686,12 @@ sub fill_alternates ($$) {
 
 sub git_init {
        my ($self, $epoch) = @_;
-       my $git_dir = "$self->{-inbox}->{mainrepo}/git/$epoch.git";
+       my $git_dir = "$self->{-inbox}->{inboxdir}/git/$epoch.git";
        my @cmd = (qw(git init --bare -q), $git_dir);
        PublicInbox::Import::run_die(\@cmd);
+       @cmd = (qw/git config/, "--file=$git_dir/config",
+                       'include.path', '../../all.git/config');
+       PublicInbox::Import::run_die(\@cmd);
        fill_alternates($self, $epoch);
        $git_dir
 }
@@ -704,7 +699,7 @@ sub git_init {
 sub git_dir_latest {
        my ($self, $max) = @_;
        $$max = -1;
-       my $pfx = "$self->{-inbox}->{mainrepo}/git";
+       my $pfx = "$self->{-inbox}->{inboxdir}/git";
        return unless -d $pfx;
        my $latest;
        opendir my $dh, $pfx or die "opendir $pfx: $!\n";
@@ -936,7 +931,7 @@ sub update_last_commit ($$$$) {
        last_epoch_commit($self, $i, $cmt);
 }
 
-sub git_dir_n ($$) { "$_[0]->{-inbox}->{mainrepo}/git/$_[1].git" }
+sub git_dir_n ($$) { "$_[0]->{-inbox}->{inboxdir}/git/$_[1].git" }
 
 sub last_commits ($$) {
        my ($self, $epoch_max) = @_;
@@ -1073,7 +1068,7 @@ sub unindex_oid ($$$) {
                                join(',',sort keys %gone), "\n";
                }
                foreach my $num (keys %gone) {
-                       $self->{unindexed}->{$_}++;
+                       $self->{unindexed}->{$num}++;
                        $self->{mm}->num_delete($num);
                }
                unindex_oid_remote($self, $oid, $mid);