X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSearchIdx.pm;h=2e0b9a43e24750fce0e083c37120a47f34d8da8a;hb=7503aeb540af5afd5cb1b554b3c29f35f5fc918d;hp=446cfb0d50afd8be723a24c4f6f5751de80ba548;hpb=2f5bb422a75f38e9c2fdca5d526fd8832ca9de45;p=public-inbox.git diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm index 446cfb0d..2e0b9a43 100644 --- a/lib/PublicInbox/SearchIdx.pm +++ b/lib/PublicInbox/SearchIdx.pm @@ -11,6 +11,7 @@ use strict; use warnings; use base qw(PublicInbox::Search PublicInbox::Lock); use PublicInbox::MIME; +use PublicInbox::InboxWritable; use PublicInbox::MID qw/mid_clean id_compress mid_mime mids references/; use PublicInbox::MsgIter; use Carp qw(croak); @@ -18,11 +19,6 @@ use POSIX qw(strftime); require PublicInbox::Git; use constant { - PERM_UMASK => 0, - OLD_PERM_GROUP => 1, - OLD_PERM_EVERYBODY => 2, - PERM_GROUP => 0660, - PERM_EVERYBODY => 0664, BATCH_BYTES => 1_000_000, DEBUG => !!$ENV{DEBUG}, }; @@ -62,20 +58,19 @@ sub new { PublicInbox::AltId->new($ibx, $_); } @$altid ]; } - $git = $ibx->git; - } else { - $git = PublicInbox::Git->new($git_dir); # v1 only + } else { # v1 + $ibx = { mainrepo => $git_dir, version => 1 }; } + $ibx = PublicInbox::InboxWritable->new($ibx); require Search::Xapian::WritableDatabase; my $self = bless { mainrepo => $mainrepo, - git => $git, + -inbox => $ibx, + git => $ibx->git, -altid => $altid, version => $version, }, $class; - my $perm = $self->_git_config_perm; - my $umask = _umask_for($perm); - $self->{umask} = $umask; + $ibx->umask_prepare; if ($version == 1) { $self->{lock_path} = "$mainrepo/ssoma.lock"; } elsif ($version == 2) { @@ -119,25 +114,12 @@ sub add_val ($$$) { $doc->add_value($col, $num); } -sub add_values ($$) { - my ($doc, $values) = @_; - - my $ts = $values->[PublicInbox::Search::TS]; +sub add_values { + my ($doc, $ts, $ds, $num) = @_; add_val($doc, PublicInbox::Search::TS, $ts); - - my $num = $values->[PublicInbox::Search::NUM]; - defined($num) and add_val($doc, PublicInbox::Search::NUM, $num); - - my $bytes = $values->[PublicInbox::Search::BYTES]; - defined($bytes) and add_val($doc, PublicInbox::Search::BYTES, $bytes); - - my $lines = $values->[PublicInbox::Search::LINES]; - add_val($doc, PublicInbox::Search::LINES, $lines); - - my $ds = $values->[PublicInbox::Search::DS]; - add_val($doc, PublicInbox::Search::DS, $ds); my $yyyymmdd = strftime('%Y%m%d', gmtime($ds)); add_val($doc, PublicInbox::Search::YYYYMMDD, $yyyymmdd); + defined($num) and add_val($doc, PublicInbox::Search::NUM, $num); } sub index_users ($$) { @@ -300,8 +282,10 @@ sub add_message { } my $lines = $mime->body_raw =~ tr!\n!\n!; - my @values = ($smsg->ds, $num, $bytes, $lines, $smsg->ts); - add_values($doc, \@values); + $smsg->{lines} = $mime->body_raw =~ tr!\n!\n!; + defined $bytes or $bytes = length($mime->as_string); + $smsg->{bytes} = $bytes; + add_values($doc, $smsg->ts, $smsg->ds, $num); my $tg = $self->term_generator; @@ -371,8 +355,8 @@ sub add_message { $self->delete_article($num) if defined $num; # for reindexing if ($skel) { - push @values, $mids, $xpath, $data; - $skel->index_skeleton(\@values); + my @vals = ($smsg->ts, $num, $mids, $xpath, $data); + $skel->index_skeleton(\@vals); $doc->add_boolean_term('Q' . $_) foreach @$mids; $doc->add_boolean_term('XNUM' . $num) if defined $num; $doc_id = $self->{xdb}->add_document($doc); @@ -389,6 +373,14 @@ sub add_message { $doc_id; } +# returns begin and end PostingIterator +sub find_doc_ids { + my ($self, $termval) = @_; + my $db = $self->{xdb}; + + ($db->postlist_begin($termval), $db->postlist_end($termval)); +} + sub batch_do { my ($self, $termval, $cb) = @_; my $batch_size = 1000; # don't let @ids grow too large to avoid OOM @@ -536,9 +528,9 @@ sub link_and_save { $doc->add_boolean_term('XPATH' . $xpath) if defined $xpath; $doc->add_boolean_term('Q' . $_) foreach @$mids; - my $vivified = 0; $self->{skel} and die "Should not have read-only skel here\n";; foreach my $mid (@$mids) { + my $vivified = 0; $self->each_smsg_by_mid($mid, sub { my ($cur) = @_; my $type = $cur->type; @@ -560,10 +552,8 @@ sub link_and_save { ++$vivified; 1; }); - } - if ($vivified > 1) { - my $id = '<'.join('> <', @$mids).'>'; - warn "BUG: vivified multiple ($vivified) ghosts for $id\n"; + $vivified > 1 and warn + "BUG: vivified multiple ($vivified) ghosts for $mid\n"; } # not really important, but we return any vivified ghost docid, here: return $doc_id if defined $doc_id; @@ -640,7 +630,7 @@ sub do_cat_mail { sub index_sync { my ($self, $opts) = @_; - with_umask($self, sub { $self->_index_sync($opts) }); + $self->{-inbox}->with_umask(sub { $self->_index_sync($opts) }) } sub batch_adjust ($$$$) { @@ -838,65 +828,6 @@ sub merge_threads { }); } -sub _read_git_config_perm { - my ($self) = @_; - my @cmd = qw(config); - if ($self->{version} == 2) { - push @cmd, "--file=$self->{mainrepo}/all.git/config"; - } - my $fh = $self->{git}->popen(@cmd, 'core.sharedRepository'); - local $/ = "\n"; - my $perm = <$fh>; - chomp $perm if defined $perm; - $perm; -} - -sub _git_config_perm { - my $self = shift; - my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self); - return PERM_GROUP if (!defined($perm) || $perm eq ''); - return PERM_UMASK if ($perm eq 'umask'); - return PERM_GROUP if ($perm eq 'group'); - if ($perm =~ /\A(?:all|world|everybody)\z/) { - return PERM_EVERYBODY; - } - return PERM_GROUP if ($perm =~ /\A(?:true|yes|on|1)\z/); - return PERM_UMASK if ($perm =~ /\A(?:false|no|off|0)\z/); - - my $i = oct($perm); - return PERM_UMASK if ($i == PERM_UMASK); - return PERM_GROUP if ($i == OLD_PERM_GROUP); - return PERM_EVERYBODY if ($i == OLD_PERM_EVERYBODY); - - if (($i & 0600) != 0600) { - die "core.sharedRepository mode invalid: ". - sprintf('%.3o', $i) . "\nOwner must have permissions\n"; - } - ($i & 0666); -} - -sub _umask_for { - my ($perm) = @_; # _git_config_perm return value - my $rv = $perm; - return umask if $rv == 0; - - # set +x bit if +r or +w were set - $rv |= 0100 if ($rv & 0600); - $rv |= 0010 if ($rv & 0060); - $rv |= 0001 if ($rv & 0006); - (~$rv & 0777); -} - -sub with_umask { - my ($self, $cb) = @_; - my $old = umask $self->{umask}; - my $rv = eval { $cb->() }; - my $err = $@; - umask $old; - die $err if $err; - $rv; -} - sub DESTROY { # order matters for unlocking $_[0]->{xdb} = undef;