]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchIdx.pm
www: give tor2web some exposure, too
[public-inbox.git] / lib / PublicInbox / SearchIdx.pm
index 0582526e8b1c5b0f0866b39911e1a94d9a3507dc..f54f5f2f112c8510d209ffea95b19513d1b9609b 100644 (file)
@@ -17,6 +17,7 @@ use base qw(PublicInbox::Search);
 use PublicInbox::MID qw/mid_clean id_compress mid_mime/;
 use PublicInbox::MsgIter;
 use Carp qw(croak);
+use POSIX qw(strftime);
 require PublicInbox::Git;
 *xpfx = *PublicInbox::Search::xpfx;
 
@@ -30,9 +31,21 @@ use constant {
 };
 
 sub new {
-       my ($class, $git_dir, $creat) = @_;
+       my ($class, $inbox, $creat) = @_;
+       my $git_dir = $inbox;
+       my $altid;
+       if (ref $inbox) {
+               $git_dir = $inbox->{mainrepo};
+               $altid = $inbox->{altid};
+               if ($altid) {
+                       require PublicInbox::AltId;
+                       $altid = [ map {
+                               PublicInbox::AltId->new($inbox, $_);
+                       } @$altid ];
+               }
+       }
        require Search::Xapian::WritableDatabase;
-       my $self = bless { git_dir => $git_dir }, $class;
+       my $self = bless { git_dir => $git_dir, -altid => $altid }, $class;
        my $perm = $self->_git_config_perm;
        my $umask = _umask_for($perm);
        $self->{umask} = $umask;
@@ -116,7 +129,8 @@ sub add_message {
                        $doc->add_term(xpfx('path') . id_compress($path));
                }
 
-               add_val($doc, &PublicInbox::Search::TS, $smsg->ts);
+               my $ts = $smsg->ts;
+               add_val($doc, &PublicInbox::Search::TS, $ts);
 
                defined($num) and
                        add_val($doc, &PublicInbox::Search::NUM, $num);
@@ -127,6 +141,9 @@ sub add_message {
                add_val($doc, &PublicInbox::Search::LINES,
                                $mime->body_raw =~ tr!\n!\n!);
 
+               my $yyyymmdd = strftime('%Y%m%d', gmtime($ts));
+               $doc->add_value(&PublicInbox::Search::YYYYMMDD, $yyyymmdd);
+
                my $tg = $self->term_generator;
 
                $tg->set_document($doc);
@@ -169,7 +186,16 @@ sub add_message {
                });
 
                link_message($self, $smsg, $old_tid);
+               $tg->index_text($mid, 1);
                $doc->set_data($smsg->to_doc_data($blob));
+
+               if (my $altid = $self->{-altid}) {
+                       foreach my $alt (@$altid) {
+                               my $id = $alt->mid2alt($mid);
+                               next unless defined $id;
+                               $doc->add_term($alt->{xprefix} . $id);
+                       }
+               }
                if (defined $doc_id) {
                        $db->replace_document($doc_id, $doc);
                } else {
@@ -415,19 +441,24 @@ sub _index_sync {
 
        my $mm = _msgmap_init($self);
        my $dbh = $mm->{dbh} if $mm;
+       my $mm_only;
        my $cb = sub {
                my ($commit, $more) = @_;
                if ($dbh) {
                        $mm->last_commit($commit) if $commit;
                        $dbh->commit;
                }
-               $xdb->set_metadata($mkey, $commit) if $mkey && $commit;
-               $xdb->commit_transaction;
-               $xdb = _xdb_release($self);
+               if (!$mm_only) {
+                       $xdb->set_metadata($mkey, $commit) if $mkey && $commit;
+                       $xdb->commit_transaction;
+                       $xdb = _xdb_release($self);
+               }
                # let another process do some work... <
                if ($more) {
-                       $xdb = _xdb_acquire($self);
-                       $xdb->begin_transaction;
+                       if (!$mm_only) {
+                               $xdb = _xdb_acquire($self);
+                               $xdb->begin_transaction;
+                       }
                        $dbh->begin_work if $dbh;
                }
        };
@@ -451,14 +482,13 @@ sub _index_sync {
                        my $mkey_prev = $mkey;
                        $mkey = undef; # ignore xapian, for now
                        my $mlog = _git_log($self, $r);
+                       $mm_only = 1;
                        rlog($self, $mlog, *index_mm, *unindex_mm, $cb);
-                       $mlog = undef;
+                       $mm_only = $mlog = undef;
 
                        # now deal with Xapian
                        $mkey = $mkey_prev;
                        $dbh = undef;
-                       $xdb = _xdb_acquire($self);
-                       $xdb->begin_transaction;
                        rlog($self, $xlog, *index_mm2, *unindex_mm2, $cb);
                }
        } else {