use PublicInbox::OverIdx;
use PublicInbox::Spawn qw(spawn);
use PublicInbox::Git qw(git_unquote);
-use Compress::Zlib qw(compress);
use constant {
BATCH_BYTES => defined($ENV{XAPIAN_FLUSH_THRESHOLD}) ?
my $xapianlevels = qr/\A(?:full|medium)\z/;
sub new {
- my ($class, $ibx, $creat, $part) = @_;
+ my ($class, $ibx, $creat, $shard) = @_;
+ ref $ibx or die "BUG: expected PublicInbox::Inbox object: $ibx";
my $levels = qr/\A(?:full|medium|basic)\z/;
- my $mainrepo = $ibx; # for "public-inbox-index" w/o entry in config
- my $git_dir = $mainrepo;
- my ($altid, $git);
- my $version = 1;
+ my $mainrepo = $ibx->{mainrepo};
+ my $version = $ibx->{version} || 1;
my $indexlevel = 'full';
- if (ref $ibx) {
- $mainrepo = $ibx->{mainrepo};
- $altid = $ibx->{altid};
- $version = $ibx->{version} || 1;
- if ($altid) {
- require PublicInbox::AltId;
- $altid = [ map {
- PublicInbox::AltId->new($ibx, $_);
- } @$altid ];
- }
- if ($ibx->{indexlevel}) {
- if ($ibx->{indexlevel} =~ $levels) {
- $indexlevel = $ibx->{indexlevel};
- } else {
- die("Invalid indexlevel $ibx->{indexlevel}\n");
- }
+ my $altid = $ibx->{altid};
+ if ($altid) {
+ require PublicInbox::AltId;
+ $altid = [ map { PublicInbox::AltId->new($ibx, $_); } @$altid ];
+ }
+ if ($ibx->{indexlevel}) {
+ if ($ibx->{indexlevel} =~ $levels) {
+ $indexlevel = $ibx->{indexlevel};
+ } else {
+ die("Invalid indexlevel $ibx->{indexlevel}\n");
}
- } else { # FIXME: old tests: old tests
- $ibx = { mainrepo => $git_dir, version => 1 };
}
$ibx = PublicInbox::InboxWritable->new($ibx);
my $self = bless {
my $dir = $self->xdir;
$self->{over} = PublicInbox::OverIdx->new("$dir/over.sqlite3");
} elsif ($version == 2) {
- defined $part or die "partition is required for v2\n";
- # partition is a number
- $self->{partition} = $part;
+ defined $shard or die "shard is required for v2\n";
+ # shard is a number
+ $self->{shard} = $shard;
$self->{lock_path} = undef;
} else {
die "unsupported inbox version=$version\n";
if ($self->{creat}) {
require File::Path;
$self->lock_acquire;
- File::Path::mkpath($dir);
+
+ # don't create empty Xapian directories if we don't need Xapian
+ my $is_shard = defined($self->{shard});
+ if (!$is_shard || ($is_shard && need_xapian($self))) {
+ File::Path::mkpath($dir);
+ }
}
return unless defined $flag;
- $self->{xdb} = Search::Xapian::WritableDatabase->new($dir, $flag);
+ my $xdb = eval { Search::Xapian::WritableDatabase->new($dir, $flag) };
+ if ($@) {
+ die "Failed opening $dir: ", $@;
+ }
+ $self->{xdb} = $xdb;
}
sub add_val ($$$) {
$@ ? undef : $mime;
}
+# called by public-inbox-index
sub index_sync {
my ($self, $opts) = @_;
+ delete $self->{lock_path} if $opts->{-skip_lock};
$self->{-inbox}->with_umask(sub { $self->_index_sync($opts) })
}
-sub batch_adjust ($$$$) {
- my ($max, $bytes, $batch_cb, $latest) = @_;
+sub batch_adjust ($$$$$) {
+ my ($max, $bytes, $batch_cb, $latest, $nr) = @_;
$$max -= $bytes;
if ($$max <= 0) {
$$max = BATCH_BYTES;
- $batch_cb->($latest);
+ $batch_cb->($nr, $latest);
}
}
my %D;
my $line;
my $newest;
+ my $nr = 0;
while (defined($line = <$log>)) {
if ($line =~ /$addmsg/o) {
my $blob = $1;
next;
}
my $mime = do_cat_mail($git, $blob, \$bytes) or next;
- batch_adjust(\$max, $bytes, $batch_cb, $latest);
+ batch_adjust(\$max, $bytes, $batch_cb, $latest, ++$nr);
$add_cb->($self, $mime, $bytes, $blob);
} elsif ($line =~ /$delmsg/o) {
my $blob = $1;
my $mime = do_cat_mail($git, $blob, \$bytes) or next;
$del_cb->($self, $mime);
}
- $batch_cb->($latest, $newest);
+ $batch_cb->($nr, $latest, $newest);
}
sub _msgmap_init {
}
sub _git_log {
- my ($self, $range) = @_;
+ my ($self, $opts, $range) = @_;
my $git = $self->{git};
if (index($range, '..') < 0) {
# Count the new files so they can be added newest to oldest
# and still have numbers increasing from oldest to newest
my $fcount = 0;
+ my $pr = $opts->{-progress};
+ $pr->("counting changes\n\t$range ... ") if $pr;
# can't use 'rev-list --count' if we use --diff-filter
my $fh = $git->popen(qw(log --pretty=tformat:%h
--no-notes --no-color --no-renames
--diff-filter=AM), $range);
++$fcount while <$fh>;
+ close $fh;
my $high = $self->{mm}->num_highwater;
+ $pr->("$fcount\n") if $pr; # continue previous line
+ $self->{ntodo} = $fcount;
if (index($range, '..') < 0) {
if ($high && $high == $fcount) {
$lx = $lm;
}
# Use last_commit from msgmap if it is older or unset
- if (!$lm || ($lx && $lx && is_ancestor($self->{git}, $lm, $lx))) {
+ if (!$lm || ($lx && $lm && is_ancestor($self->{git}, $lm, $lx))) {
$lx = $lm;
}
$lx;
}
+sub reindex_from ($$) {
+ my ($reindex, $last_commit) = @_;
+ return $last_commit unless $reindex;
+ ref($reindex) eq 'HASH' ? $reindex->{from} : '';
+}
+
# indexes all unindexed messages (v1 only)
sub _index_sync {
my ($self, $opts) = @_;
my ($last_commit, $lx, $xlog);
my $git = $self->{git};
$git->batch_prepare;
+ my $pr = $opts->{-progress};
my $xdb = $self->begin_txn_lazy;
my $mm = _msgmap_init($self);
do {
$xlog = undef;
$last_commit = _last_x_commit($self, $mm);
- $lx = $opts->{reindex} ? '' : $last_commit;
+ $lx = reindex_from($opts->{reindex}, $last_commit);
$self->{over}->rollback_lazy;
$self->{over}->disconnect;
# ensure we leak no FDs to "git log" with Xapian <= 1.2
my $range = $lx eq '' ? $tip : "$lx..$tip";
- $xlog = _git_log($self, $range);
+ $xlog = _git_log($self, $opts, $range);
$xdb = $self->begin_txn_lazy;
} while (_last_x_commit($self, $mm) ne $last_commit);
my $dbh = $mm->{dbh} if $mm;
my $cb = sub {
- my ($commit, $newest) = @_;
+ my ($nr, $commit, $newest) = @_;
if ($dbh) {
if ($newest) {
my $cur = $mm->last_commit || '';
$git->cleanup;
$xdb = _xdb_release($self);
# let another process do some work... <
+ $pr->("indexed $nr/$self->{ntodo}\n") if $pr && $nr;
if (!$newest) {
$xdb = $self->begin_txn_lazy;
$dbh->begin_work if $dbh;
sub remote_remove {
my ($self, $oid, $mid) = @_;
if (my $w = $self->{w}) {
- # triggers remove_by_oid in a partition
+ # triggers remove_by_oid in a shard
print $w "D $oid $mid\n" or die "failed to write remove $!";
} else {
$self->begin_txn_lazy;
delete $self->{txn} or return;
$self->{-inbox}->with_umask(sub {
if (my $xdb = $self->{xdb}) {
+
+ # store 'indexlevel=medium' in v2 shard=0 and
+ # v1 (only one shard)
+ # This metadata is read by Admin::detect_indexlevel:
+ if (!$self->{shard} # undef or 0, not >0
+ && $self->{indexlevel} eq 'medium') {
+ $xdb->set_metadata('indexlevel', 'medium');
+ }
+
$xdb->commit_transaction;
}
$self->{over}->commit_lazy if $self->{over};