Changeset 309:1181f65d21a7
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r302
|
r309
|
|
| 68 | 68 | my ($subject_count, $sender_count); |
| 69 | 69 | my $prev_date = 0; |
| | 70 | |
| | 71 | my (@month, %month_index, $m_idx); |
| 70 | 72 | |
| 71 | 73 | my $db = new Lancelot::DB $listaddr, { user => $opts{user} } |
| … |
… |
|
| 122 | 124 | my ($n, $msg) = @_; |
| 123 | 125 | |
| 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)) { |
| 126 | 128 | print STDERR "Unusual date: $n: $m[$n]->{date} (vs. $prev_date)\n"; |
| 127 | 129 | } |
| 128 | | $prev_date = $m[$n]->{date}; |
| | 130 | $prev_date = $m[$n]->{date}->epoch; |
| 129 | 131 | |
| 130 | 132 | $m[$n]->{msgid} = $msg->header("Message-ID") || sprintf $msgid, $n; |
| … |
… |
|
| 139 | 141 | \%last_subject, \%message); |
| 140 | 142 | $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; |
| 141 | 151 | } |
| 142 | 152 | |
| … |
… |
|
| 172 | 182 | my %kids; |
| 173 | 183 | |
| 174 | | print STDERR "identify_threads: from=$from to=$to\n"; |
| | 184 | # print STDERR "identify_threads: from=$from to=$to\n"; |
| 175 | 185 | for (my $i = $from; $i <= $to; $i++) { |
| 176 | 186 | next unless $m[$i]; |
| … |
… |
|
| 210 | 220 | } |
| 211 | 221 | |
| | 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 | |
| 212 | 228 | print STDERR "\nWriting messages and threads ...\n"; |
| 213 | 229 | $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(?,?,?,?,?,?,?)"); |
| 215 | 231 | |
| 216 | 232 | foreach my $hh (0 .. $#head) { |
| … |
… |
|
| 224 | 240 | my $m = $m[$k]; |
| 225 | 241 | $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, |
| 227 | 243 | $m->{subject}, $m->{msgid}); |
| 228 | 244 | $count++; |
| 229 | 245 | } |
| 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}, |
| 231 | 248 | $hh ? $head[$hh-1] : -1, $hh < $#head ? $head[$hh+1] : -1); |
| 232 | 249 | } |
-
|
r302
|
r309
|
|
| 29 | 29 | use Encode qw/encode decode/; |
| 30 | 30 | use Email::Date; |
| | 31 | use Time::Piece; |
| 31 | 32 | |
| 32 | 33 | use Lancelot::Log qw/log/; |
| … |
… |
|
| 108 | 109 | |
| 109 | 110 | $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; |
| 112 | 112 | |
| 113 | 113 | my $dbh = open_database($adir); |
| … |
… |
|
| 139 | 139 | |
| 140 | 140 | $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")); |
| 142 | 142 | |
| 143 | 143 | thread_message($dbh, $number, $msg, $sub_id, $date); |
| … |
… |
|
| 164 | 164 | my ($prev_thread) = $dbh->selectrow_array("SELECT MAX(id) FROM thread", {}); |
| 165 | 165 | $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); |
| 168 | 173 | $dbh->do("UPDATE message SET next=? WHERE next=-1", {}, $id); |
| 169 | 174 | } |
| … |
… |
|
| 178 | 183 | if ($msg->header("Delivery-Date")) { |
| 179 | 184 | return Time::Piece->strptime($msg->header("Delivery-Date"), |
| 180 | | "%Y%m%d%H%M%S")->epoch; |
| | 185 | "%Y%m%d%H%M%S"); |
| 181 | 186 | } |
| 182 | 187 | if (defined ($rec = $msg->header("Received")) |
| 183 | 188 | && ($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"); |
| 185 | 190 | } |
| 186 | 191 | if ($msg->header("Date")) { |
| 187 | | return find_date($msg)->epoch; |
| 188 | | } |
| 189 | | return -1; |
| | 192 | return find_date($msg); |
| | 193 | } |
| | 194 | return undef; |
| 190 | 195 | } |
| 191 | 196 | |
| … |
… |
|
| 274 | 279 | $count++; |
| 275 | 280 | } |
| | 281 | $dbh->do("UPDATE thread SET lastmsg_id=?", {}, $thread[$#thread-1]); |
| 276 | 282 | } |
| 277 | 283 | |
| … |
… |
|
| 329 | 335 | $dbh->do('CREATE INDEX subject_text ON subject(text)'); |
| 330 | 336 | $dbh->do(<<'END'); |
| | 337 | CREATE TABLE month ( |
| | 338 | id INTEGER PRIMARY KEY, |
| | 339 | year INTEGER NOT NULL, |
| | 340 | month INTEGER NOT NULL); |
| | 341 | END |
| | 342 | $dbh->do('CREATE INDEX month_year_month ON month(year,month)'); |
| | 343 | $dbh->do(<<'END'); |
| 331 | 344 | CREATE TABLE thread ( |
| 332 | 345 | id INTEGER PRIMARY KEY, |
| | 346 | month_id INTEGER, |
| 333 | 347 | subject_id INTEGER, |
| 334 | 348 | date INTEGER, |
| | 349 | lastmsg_id INTEGER, |
| 335 | 350 | prev INTEGER, |
| 336 | | next INTEGER) |
| | 351 | next INTEGER); |
| 337 | 352 | END |
| 338 | 353 | return $dbh; |