]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/V2Writable.pm
mm: min/max: return 0 instead of undef
[public-inbox.git] / lib / PublicInbox / V2Writable.pm
index 18f33655385b1814e0bce571ea94b30dd3593af8..7bef1c89a21ef220933ff67bd9992cd72b7a1ab1 100644 (file)
@@ -631,6 +631,9 @@ sub checkpoint ($;$) {
                        $_->shard_commit for @$shards;
                }
 
+               my $midx = $self->{midx}; # misc index
+               $midx->commit_txn if $midx;
+
                # last_commit is special, don't commit these until
                # Xapian shards are done:
                $dbh->begin_work if $dbh;
@@ -639,6 +642,7 @@ sub checkpoint ($;$) {
                        $dbh->commit;
                        $dbh->begin_work;
                }
+               $midx->begin_txn if $midx;
        }
        $self->{total_bytes} += $self->{transact_bytes};
        $self->{transact_bytes} = 0;
@@ -678,6 +682,7 @@ sub done {
        }
        eval { $self->{oidx}->dbh_close };
        $err .= "over close: $@\n" if $@;
+       delete $self->{midx};
        delete $self->{bnote};
        my $nbytes = $self->{total_bytes};
        $self->{total_bytes} = 0;
@@ -861,6 +866,7 @@ sub reindex_checkpoint ($$) {
        my ($self, $sync) = @_;
 
        $self->git->async_wait_all;
+       $self->update_last_commit($sync);
        ${$sync->{need_checkpoint}} = 0;
        my $mm_tmp = $sync->{mm_tmp};
        $mm_tmp->atfork_prepare if $mm_tmp;
@@ -955,19 +961,22 @@ sub index_oid { # cat_async callback
        if (do_idx($self, $bref, $eml, $smsg)) {
                ${$arg->{need_checkpoint}} = 1;
        }
+       ${$arg->{latest_cmt}} = $arg->{cur_cmt} // die 'BUG: {cur_cmt} missing';
 }
 
 # only update last_commit for $i on reindex iff newer than current
-# $sync will be used by subclasses
 sub update_last_commit {
-       my ($self, $sync, $unit, $cmt) = @_;
+       my ($self, $sync, $stk) = @_;
+       my $unit = $sync->{unit} // return;
+       my $latest_cmt = $stk ? $stk->{latest_cmt} : ${$sync->{latest_cmt}};
+       defined($latest_cmt) or return;
        my $last = last_epoch_commit($self, $unit->{epoch});
-       if (defined $last && is_ancestor($unit->{git}, $last, $cmt)) {
-               my @cmd = (qw(rev-list --count), "$last..$cmt");
+       if (defined $last && is_ancestor($self->git, $last, $latest_cmt)) {
+               my @cmd = (qw(rev-list --count), "$last..$latest_cmt");
                chomp(my $n = $unit->{git}->qx(@cmd));
                return if $n ne '' && $n == 0;
        }
-       last_epoch_commit($self, $unit->{epoch}, $cmt);
+       last_epoch_commit($self, $unit->{epoch}, $latest_cmt);
 }
 
 sub last_commits {
@@ -999,7 +1008,7 @@ sub log_range ($$$) {
        my $range = "$cur..$tip";
        $pr->("$i.git checking contiguity... ") if $pr;
        my $git = $unit->{git};
-       if (is_ancestor($git, $cur, $tip)) { # common case
+       if (is_ancestor($sync->{self}->git, $cur, $tip)) { # common case
                $pr->("OK\n") if $pr;
                my $n = $git->qx(qw(rev-list --count), $range);
                chomp($n);
@@ -1081,12 +1090,14 @@ sub sync_prepare ($$) {
                # messages to show up in mirrors, too.
                $sync->{D} //= $sync->{reindex} ? {} : undef; # OID_BIN => NR
                my $stk = log2stack($sync, $git, $range);
+               return 0 if $sync->{quit};
                my $nr = $stk ? $stk->num_records : 0;
                $pr->("$nr\n") if $pr;
                $unit->{stack} = $stk; # may be undef
                unshift @{$sync->{todo}}, $unit;
                $regen_max += $nr;
        }
+       return 0 if $sync->{quit};
 
        # XXX this should not happen unless somebody bypasses checks in
        # our code and blindly injects "d" file history into git repos
@@ -1098,9 +1109,11 @@ sub sync_prepare ($$) {
                        $oid = unpack('H*', $oid);
                        my $req = { %$sync, oid => $oid };
                        $self->git->cat_async($oid, $unindex_oid, $req);
+                       last if $sync->{quit};
                }
                $self->git->cat_async_wait;
        }
+       return 0 if $sync->{quit};
        if (!$regen_max) {
                $sync->{-regen_fmt} = "%u/?\n";
                return 0;
@@ -1232,6 +1245,7 @@ sub index_xap_step ($$$;$) {
 
 sub index_todo ($$$) {
        my ($self, $sync, $unit) = @_;
+       return if $sync->{quit};
        unindex_todo($self, $sync, $unit);
        my $stk = delete($unit->{stack}) or return;
        my $all = $self->git;
@@ -1245,8 +1259,16 @@ sub index_todo ($$$) {
                $pfx //= $unit->{git}->{git_dir};
        }
        local $self->{current_info} = "$pfx ";
-       while (my ($f, $at, $ct, $oid) = $stk->pop_rec) {
-               my $req = { %$sync, autime => $at, cotime => $ct, oid => $oid };
+       local $sync->{latest_cmt} = \(my $latest_cmt);
+       local $sync->{unit} = $unit;
+       while (my ($f, $at, $ct, $oid, $cmt) = $stk->pop_rec) {
+               my $req = {
+                       %$sync,
+                       autime => $at,
+                       cotime => $ct,
+                       oid => $oid,
+                       cur_cmt => $cmt
+               };
                if ($f eq 'm') {
                        if ($sync->{max_size}) {
                                $all->check_async($oid, \&check_size, $req);
@@ -1256,12 +1278,18 @@ sub index_todo ($$$) {
                } elsif ($f eq 'd') {
                        $all->cat_async($oid, $unindex_oid, $req);
                }
+               if ($sync->{quit}) {
+                       warn "waiting to quit...\n";
+                       $all->async_wait_all;
+                       $self->update_last_commit($sync);
+                       return;
+               }
                if (${$sync->{need_checkpoint}}) {
                        reindex_checkpoint($self, $sync);
                }
        }
        $all->async_wait_all;
-       $self->update_last_commit($sync, $unit, $stk->{latest_cmt});
+       $self->update_last_commit($sync, $stk);
 }
 
 sub xapian_only {
@@ -1322,6 +1350,11 @@ sub index_sync {
                ibx => $self->{ibx},
                epoch_max => $epoch_max,
        };
+       my $quit = sub { $sync->{quit} = 1 };
+       local $SIG{QUIT} = $quit;
+       local $SIG{INT} = $quit;
+       local $SIG{TERM} = $quit;
+
        if (sync_prepare($self, $sync)) {
                # tmp_clone seems to fail if inside a transaction, so
                # we rollback here (because we opened {mm} for reading)
@@ -1334,13 +1367,13 @@ sub index_sync {
 
                # xapian_only works incrementally w/o --reindex
                if ($seq && !$opt->{reindex}) {
-                       $art_beg = $sync->{mm_tmp}->max;
-                       $art_beg++ if defined($art_beg);
+                       $art_beg = $sync->{mm_tmp}->max || -1;
+                       $art_beg++;
                }
        }
        # work forwards through history
-       index_todo($self, $sync, $_) for @{$sync->{todo}};
-       $self->{oidx}->rethread_done($opt);
+       index_todo($self, $sync, $_) for @{delete($sync->{todo}) // []};
+       $self->{oidx}->rethread_done($opt) unless $sync->{quit};
        $self->done;
 
        if (my $nr = $sync->{nr}) {