Changeset 309:1181f65d21a7

Show
Ignore:
Timestamp:
08/18/11 11:27:09 (9 months ago)
Author:
Anselm Lingnau <anselm@…>
Branch:
default
Message:

Tweaks to the archive index database, for the benefit of Project Guinevere.
This introduces a month index for threads as well as a pointer to the last
message in a thread. We also use Time::Piece throughout for consistency.

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • bin/pl-thread

    r302 r309  
    6868my ($subject_count, $sender_count); 
    6969my $prev_date = 0; 
     70 
     71my (@month, %month_index, $m_idx); 
    7072 
    7173my $db = new Lancelot::DB $listaddr, { user => $opts{user} } 
     
    122124    my ($n, $msg) = @_; 
    123125 
    124     $m[$n]->{date} = Lancelot::Module::archive_store::determine_date($msg); 
    125     if ($prev_date != 0 && (abs($m[$n]->{date} - $prev_date) > 15*24*3600)) { 
     126    $m[$n]->{date} = Lancelot::Module::archive_store::determine_date($msg) || gmtime; 
     127    if ($prev_date != 0 && (abs($m[$n]->{date}->epoch - $prev_date) > 15*24*3600)) { 
    126128        print STDERR "Unusual date: $n: $m[$n]->{date} (vs. $prev_date)\n"; 
    127129    } 
    128     $prev_date = $m[$n]->{date}; 
     130    $prev_date = $m[$n]->{date}->epoch; 
    129131 
    130132    $m[$n]->{msgid} = $msg->header("Message-ID") || sprintf $msgid, $n; 
     
    139141                                   \%last_subject, \%message); 
    140142    $m[$n]->{root} = find_root($n); 
     143    my ($year, $month) = ($m[$n]->{date}->year, $m[$n]->{date}->mon); 
     144    my $m_id; 
     145    unless ($m_id = $month_index{"$year-$month"}) { 
     146        push @month, [ ++$m_idx, $year, $month ]; 
     147        $m_id = $month_index{"$year-$month"} = $month[$#month]->[0]; 
     148    } 
     149    $m[$n]->{month_id} = $m_id; 
     150    $m[$m[$n]->{root}]->{lastmsg_id} = $n; 
    141151} 
    142152 
     
    172182    my %kids; 
    173183 
    174     print STDERR "identify_threads: from=$from to=$to\n"; 
     184    # print STDERR "identify_threads: from=$from to=$to\n"; 
    175185    for (my $i = $from; $i <= $to; $i++) { 
    176186        next unless $m[$i]; 
     
    210220    } 
    211221 
     222    print STDERR "Writing month records ...\n"; 
     223    $sth = $dbh->prepare("INSERT INTO month VALUES(?,?,?)"); 
     224    foreach my $m (@month) { 
     225        $sth->execute(@$m); 
     226    } 
     227 
    212228    print STDERR "\nWriting messages and threads ...\n"; 
    213229    $sth = $dbh->prepare("INSERT INTO message VALUES(?,?,?,?,?,?,?,?,?,?,?)"); 
    214     my $tth = $dbh->prepare("INSERT INTO thread VALUES(?,?,?,?,?)"); 
     230    my $tth = $dbh->prepare("INSERT INTO thread VALUES(?,?,?,?,?,?,?)"); 
    215231 
    216232    foreach my $hh (0 .. $#head) { 
     
    224240            my $m = $m[$k]; 
    225241            $sth->execute($k, $m->{root}, $count, $m->{parent}, $m->{level}, 
    226                           $t[$i-1], $t[$i+1], $m->{sender}, $m->{date}, 
     242                          $t[$i-1], $t[$i+1], $m->{sender}, $m->{date}->epoch, 
    227243                          $m->{subject}, $m->{msgid}); 
    228244            $count++; 
    229245        } 
    230         $tth->execute($h, $m[$t[1]]->{subject}, $m[$t[1]]->{date}, 
     246        $tth->execute($h, $m[$t[1]]->{month_id}, $m[$t[1]]->{subject}, 
     247                      $m[$t[1]]->{date}->epoch, $m[$t[1]]->{lastmsg_id}, 
    231248                      $hh ? $head[$hh-1] : -1, $hh < $#head ? $head[$hh+1] : -1); 
    232249    } 
  • lib/Lancelot/Module/archive_store.pm

    r302 r309  
    2929use Encode qw/encode decode/; 
    3030use Email::Date; 
     31use Time::Piece; 
    3132 
    3233use Lancelot::Log qw/log/; 
     
    108109 
    109110    $msg->header_set("Delivery-Date", localtime->strftime("%Y%m%d%H%M%S")); 
    110     my $date = determine_date($msg); 
    111     $date = time if $date < 0; 
     111    my $date = determine_date($msg) || gmtime; 
    112112 
    113113    my $dbh = open_database($adir); 
     
    139139 
    140140    $dbh->do("INSERT INTO message VALUES (?,0,0,0,0,0,-1,?,?,?,?)", {}, 
    141              $number, $snd_id, $date, $sub_id, $msg->header("Message-ID")); 
     141             $number, $snd_id, $date->epoch, $sub_id, $msg->header("Message-ID")); 
    142142 
    143143    thread_message($dbh, $number, $msg, $sub_id, $date); 
     
    164164        my ($prev_thread) = $dbh->selectrow_array("SELECT MAX(id) FROM thread", {}); 
    165165        $dbh->do("UPDATE thread SET next=? WHERE next=-1", {}, $id); 
    166         $dbh->do("INSERT INTO thread VALUES (?,?,?,?,-1)", {}, 
    167                  $id, $sub_id, $date, $prev_thread); 
     166        my ($year, $month) = ($date->year, $date->mon); 
     167        $dbh->do("INSERT OR IGNORE INTO month VALUES (NULL, ?, ?)", {}, 
     168                 $year, $month); 
     169        my ($month_id) = $dbh->selectrow_array( 
     170            "SELECT id FROM month WHERE year=? AND month=?", {}, $year, $month); 
     171        $dbh->do("INSERT INTO thread VALUES (?,?,?,?,?,?,-1)", {}, 
     172                 $id, $month_id, $sub_id, $date->epoch, $id, $prev_thread); 
    168173        $dbh->do("UPDATE message SET next=? WHERE next=-1", {}, $id); 
    169174    } 
     
    178183    if ($msg->header("Delivery-Date")) { 
    179184        return Time::Piece->strptime($msg->header("Delivery-Date"), 
    180                                      "%Y%m%d%H%M%S")->epoch; 
     185                                     "%Y%m%d%H%M%S"); 
    181186    } 
    182187    if (defined ($rec = $msg->header("Received")) 
    183188        && ($rec =~ /\(qmail .*\); (.*) -0000$/)) { 
    184         return Time::Piece->strptime($1, "%d %B %Y %H:%M:%S")->epoch; 
     189        return Time::Piece->strptime($1, "%d %B %Y %H:%M:%S"); 
    185190    } 
    186191    if ($msg->header("Date")) { 
    187         return find_date($msg)->epoch; 
    188     } 
    189     return -1; 
     192        return find_date($msg); 
     193    } 
     194    return undef; 
    190195} 
    191196 
     
    274279        $count++; 
    275280    } 
     281    $dbh->do("UPDATE thread SET lastmsg_id=?", {}, $thread[$#thread-1]); 
    276282} 
    277283 
     
    329335    $dbh->do('CREATE INDEX subject_text ON subject(text)'); 
    330336    $dbh->do(<<'END'); 
     337    CREATE TABLE month ( 
     338        id INTEGER PRIMARY KEY, 
     339        year INTEGER NOT NULL, 
     340        month INTEGER NOT NULL); 
     341END 
     342    $dbh->do('CREATE INDEX month_year_month ON month(year,month)'); 
     343    $dbh->do(<<'END'); 
    331344    CREATE TABLE thread ( 
    332345        id INTEGER PRIMARY KEY, 
     346        month_id INTEGER, 
    333347        subject_id INTEGER, 
    334348        date INTEGER, 
     349        lastmsg_id INTEGER, 
    335350        prev INTEGER, 
    336         next INTEGER) 
     351        next INTEGER); 
    337352END 
    338353    return $dbh;