X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FOverIdx.pm;h=c8f61e012e0977dc98b794357ee033b801dc8e0a;hb=77eafbd653d2efac546f2c330d8cf5e84bef2712;hp=5601e602c0181aebfd89d01d541d51fa2ac05b64;hpb=592f8239dceb6604e538fd51e62bcce50d6e1972;p=public-inbox.git diff --git a/lib/PublicInbox/OverIdx.pm b/lib/PublicInbox/OverIdx.pm index 5601e602..c8f61e01 100644 --- a/lib/PublicInbox/OverIdx.pm +++ b/lib/PublicInbox/OverIdx.pm @@ -12,15 +12,16 @@ use strict; use warnings; use base qw(PublicInbox::Over); use IO::Handle; -use DBI; +use DBI qw(:sql_types); # SQL_BLOB use PublicInbox::MID qw/id_compress mids_for_index references/; use PublicInbox::Smsg qw(subject_normalized); use Compress::Zlib qw(compress); use PublicInbox::Search; +use Carp qw(croak); sub dbh_new { my ($self) = @_; - my $dbh = $self->SUPER::dbh_new(1); + my $dbh = $self->SUPER::dbh_new($self->{-no_sync} ? 2 : 1); # TRUNCATE reduces I/O compared to the default (DELETE) # We do not use WAL since we're optimized for read-only ops, @@ -37,6 +38,13 @@ sub dbh_new { $dbh; } +sub new { + my ($class, $f) = @_; + my $self = $class->SUPER::new($f); + $self->{min_tid} = 0; + $self; +} + sub get_counter ($$) { my ($dbh, $key) = @_; my $sth = $dbh->prepare_cached(<<'', undef, 1); @@ -164,8 +172,12 @@ sub _resolve_mid_to_tid { my $cur_tid = $smsg->{tid}; if (defined $$tid) { merge_threads($self, $$tid, $cur_tid); - } else { + } elsif ($cur_tid > $self->{min_tid}) { $$tid = $cur_tid; + } else { # rethreading, queue up dead ghosts + $$tid = next_tid($self); + my $num = $smsg->{num}; + push(@{$self->{-ghosts_to_delete}}, $num) if $num < 0; } 1; } @@ -175,7 +187,10 @@ sub resolve_mid_to_tid { my ($self, $mid) = @_; my $tid; each_by_mid($self, $mid, ['tid'], \&_resolve_mid_to_tid, \$tid); - defined $tid ? $tid : create_ghost($self, $mid); + if (my $del = delete $self->{-ghosts_to_delete}) { + delete_by_num($self, $_) for @$del; + } + $tid // create_ghost($self, $mid); } sub create_ghost { @@ -221,7 +236,7 @@ sub link_refs { merge_threads($self, $tid, $ptid); } } else { - $tid = defined $old_tid ? $old_tid : next_tid($self); + $tid = $old_tid // next_tid($self); } $tid; } @@ -256,11 +271,10 @@ sub subject_path ($) { } sub add_overview { - my ($self, $mime, $smsg) = @_; - $smsg->{lines} = $mime->body_raw =~ tr!\n!\n!; - my $hdr = $mime->header_obj; - my $mids = mids_for_index($hdr); - my $refs = parse_references($smsg, $hdr, $mids); + my ($self, $eml, $smsg) = @_; + $smsg->{lines} = $eml->body_raw =~ tr!\n!\n!; + my $mids = mids_for_index($eml); + my $refs = parse_references($smsg, $eml, $mids); my $subj = $smsg->{subject}; my $xpath; if ($subj ne '') { @@ -278,10 +292,17 @@ sub _add_over { my $cur_tid = $smsg->{tid}; my $n = $smsg->{num}; die "num must not be zero for $mid" if !$n; - $$old_tid = $cur_tid unless defined $$old_tid; + my $cur_valid = $cur_tid > $self->{min_tid}; + if ($n > 0) { # regular mail - merge_threads($self, $$old_tid, $cur_tid); + if ($cur_valid) { + $$old_tid //= $cur_tid; + merge_threads($self, $$old_tid, $cur_tid); + } else { + $$old_tid //= next_tid($self); + } } elsif ($n < 0) { # ghost + $$old_tid //= $cur_valid ? $cur_tid : next_tid($self); link_refs($self, $refs, $$old_tid); delete_by_num($self, $n); $$v++; @@ -297,6 +318,7 @@ sub add_over { begin_lazy($self); delete_by_num($self, $num, \$old_tid); + $old_tid = undef if ($old_tid // 0) <= $self->{min_tid}; foreach my $mid (@$mids) { my $v = 0; each_by_mid($self, $mid, ['tid'], \&_add_over, @@ -314,7 +336,7 @@ VALUES (?,?,?,?,?,?) my $n = 0; my @v = ($num, $tid, $sid, $ts, $ds); foreach (@v) { $sth->bind_param(++$n, $_) } - $sth->bind_param(++$n, $ddd); + $sth->bind_param(++$n, $ddd, SQL_BLOB); $sth->execute; $sth = $dbh->prepare_cached(<<''); INSERT INTO id2num (id, num) VALUES (?,?) @@ -456,4 +478,47 @@ sub create { $self->disconnect; } +sub rethread_prepare { + my ($self, $opt) = @_; + return unless $opt->{rethread}; + begin_lazy($self); + my $min = $self->{min_tid} = get_counter($self->{dbh}, 'thread') // 0; + my $pr = $opt->{-progress}; + $pr->("rethread min THREADID ".($min + 1)."\n") if $pr && $min; +} + +sub rethread_done { + my ($self, $opt) = @_; + return unless $opt->{rethread} && $self->{txn}; + defined(my $min = $self->{min_tid}) or croak('BUG: no min_tid'); + my $dbh = $self->{dbh} or croak('BUG: no dbh'); + my $rows = $dbh->selectall_arrayref(<<'', { Slice => {} }, $min); +SELECT num,tid FROM over WHERE num < 0 AND tid < ? + + my $show_id = $dbh->prepare('SELECT id FROM id2num WHERE num = ?'); + my $show_mid = $dbh->prepare('SELECT mid FROM msgid WHERE id = ?'); + my $pr = $opt->{-progress}; + my $total = 0; + for my $r (@$rows) { + my $exp = 0; + $show_id->execute($r->{num}); + while (defined(my $id = $show_id->fetchrow_array)) { + ++$exp; + $show_mid->execute($id); + my $mid = $show_mid->fetchrow_array; + if (!defined($mid)) { + warn <{num} ID=$id THREADID=$r->{tid} has no Message-ID +EOF + next; + } + $pr->(<{num} <$mid> THREADID=$r->{tid} culled +EOM + } + delete_by_num($self, $r->{num}); + } + $pr->("I: rethread culled $total ghosts\n") if $pr && $total; +} + 1;