]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
config: support "inboxdir" in addition to "mainrepo"
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index 9c291066487d03be512706442f45df4c3e1022c4..aed3875a037254b94222faa2147e9f17abe31612 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # based on notmuch, but with no concept of folders, files or flags
 #
@@ -29,36 +29,27 @@ use constant {
 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 $inboxdir = $ibx->{inboxdir};
+       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 {
-               mainrepo => $mainrepo,
+               inboxdir => $inboxdir,
                -inbox => $ibx,
                git => $ibx->git,
                -altid => $altid,
@@ -67,13 +58,13 @@ sub new {
        }, $class;
        $ibx->umask_prepare;
        if ($version == 1) {
-               $self->{lock_path} = "$mainrepo/ssoma.lock";
+               $self->{lock_path} = "$inboxdir/ssoma.lock";
                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";
@@ -111,13 +102,17 @@ sub _xdb_acquire {
                $self->lock_acquire;
 
                # don't create empty Xapian directories if we don't need Xapian
-               my $is_part = defined($self->{partition});
-               if (!$is_part || ($is_part && need_xapian($self))) {
+               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 ($$$) {
@@ -608,7 +603,7 @@ sub _msgmap_init {
        die "BUG: _msgmap_init is only for v1\n" if $self->{version} != 1;
        $self->{mm} ||= eval {
                require PublicInbox::Msgmap;
-               PublicInbox::Msgmap->new($self->{mainrepo}, 1);
+               PublicInbox::Msgmap->new($self->{inboxdir}, 1);
        };
 }
 
@@ -694,7 +689,7 @@ sub _last_x_commit {
                $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;
@@ -802,7 +797,7 @@ sub remote_close {
 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;
@@ -828,6 +823,15 @@ sub commit_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};