]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Mbox.pm
www: rework query responses to avoid COUNT in SQLite
[public-inbox.git] / lib / PublicInbox / Mbox.pm
index 79e09a70f575a576d6f75df176efa627d15431cd..05de6be1a678f75ac258b24233c9f52d8c7a585f 100644 (file)
@@ -26,13 +26,6 @@ sub subject_fn ($) {
        $fn eq '' ? 'no-subject' : $fn;
 }
 
-sub smsg_for ($$$) {
-       my ($head, $db, $mid) = @_;
-       my $doc_id = $head->get_docid;
-       my $doc = $db->get_document($doc_id);
-       PublicInbox::SearchMsg->wrap($doc, $mid)->load_expand;
-}
-
 sub mb_stream {
        my ($more) = @_;
        bless $more, 'PublicInbox::Mbox';
@@ -47,8 +40,7 @@ sub getline {
                return msg_str($ctx, $cur);
        }
        for (; !defined($cur) && $head != $tail; $head++) {
-               my $smsg = smsg_for($head, $db, $ctx->{mid});
-               next if $smsg->type ne 'mail';
+               my $smsg = PublicInbox::SearchMsg->get($head, $db, $ctx->{mid});
                my $mref = $ctx->{-inbox}->msg_by_smsg($smsg) or next;
                $cur = Email::Simple->new($mref);
                $cur = msg_str($ctx, $cur);
@@ -71,8 +63,8 @@ sub emit_raw {
                $srch->retry_reopen(sub {
                        ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
                        for (; !defined($first) && $head != $tail; $head++) {
-                               my $smsg = smsg_for($head, $db, $mid);
-                               next if $smsg->type ne 'mail';
+                               my @args = ($head, $db, $mid);
+                               my $smsg = PublicInbox::SearchMsg->get(@args);
                                my $mref = $ibx->msg_by_smsg($smsg) or next;
                                $first = Email::Simple->new($mref);
                        }
@@ -100,7 +92,7 @@ sub emit_raw {
 }
 
 sub msg_str {
-       my ($ctx, $simple) = @_; # Email::Simple object
+       my ($ctx, $simple, $mid) = @_; # Email::Simple object
        my $header_obj = $simple->header_obj;
 
        # drop potentially confusing headers, ssoma already should've dropped
@@ -110,7 +102,7 @@ sub msg_str {
        }
        my $ibx = $ctx->{-inbox};
        my $base = $ibx->base_url($ctx->{env});
-       my $mid = mid_clean($header_obj->header('Message-ID'));
+       $mid = $ctx->{mid} unless defined $mid;
        $mid = mid_escape($mid);
        my @append = (
                'Archived-At', "<$base$mid/>",
@@ -225,15 +217,16 @@ sub set_filename ($$) {
 sub getline {
        my ($self) = @_;
        my $ctx = $self->{ctx} or return;
-       my $res;
        my $ibx = $ctx->{-inbox};
        my $gz = $self->{gz};
+       my $msgs = $self->{msgs};
        do {
                # work on existing result set
-               while (defined(my $smsg = shift @{$self->{msgs}})) {
+               while (defined(my $smsg = shift @$msgs)) {
                        my $msg = eval { $ibx->msg_by_smsg($smsg) } or next;
                        $msg = Email::Simple->new($msg);
-                       $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg));
+                       $gz->write(PublicInbox::Mbox::msg_str($ctx, $msg,
+                                                               $smsg->mid));
 
                        # use subject of first message as subject
                        if (my $hdr = delete $self->{hdr}) {
@@ -254,11 +247,9 @@ sub getline {
                }
 
                # refill result set
-               $res = $self->{cb}->($self->{opts});
-               $self->{msgs} = $res->{msgs};
-               $res = scalar @{$self->{msgs}};
-               $self->{opts}->{offset} += $res;
-       } while ($res);
+               $msgs = $self->{msgs} = $self->{cb}->($self->{opts});
+               $self->{opts}->{offset} += scalar @$msgs;
+       } while (@$msgs);
        $gz->close;
        delete $self->{ctx};
        ${delete $self->{buf}};