X-Git-Url: https://git.deb.at/?p=deb%2Fpackages.git;a=blobdiff_plain;f=bin%2Fparse-contents;h=ef5c09ce2b4645395371392f5f3a732dd913cae6;hp=341ecd5783394cfcef859fae632d5896432cdc6a;hb=11d08c95c0b38b80f9595b56121a95744d63b03b;hpb=9c10f84340c63159bd3d242bab1fb3d9615eae3f diff --git a/bin/parse-contents b/bin/parse-contents index 341ecd5..ef5c09c 100755 --- a/bin/parse-contents +++ b/bin/parse-contents @@ -2,8 +2,6 @@ # Convert Contents.gz files into Sleepycat db files for efficient usage of # data # -# $Id$ -# # Copyright (C) 2006 Jeroen van Wolffelaar # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -17,11 +15,14 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use strict; +use warnings; use lib './lib'; +$| = 1; + # Important, we want sorting and such to happen like in the C locale: binary, # without any fancy collation. FIXME: is this actually adequate? $ENV{"LC_ALL"} = 'C'; @@ -35,49 +36,67 @@ my $SORT_REVERSE_CONCURRENTLY = 1; use DB_File; use Storable; +use File::Path; use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES ); &Packages::Config::init( './' ); -my @archives =( 'us'); #@ARCHIVES # NOT-IMPLEMENTED-YET +my @archives = @ARCHIVES; my @suites = @SUITES; my @archs = @ARCHITECTURES; $DBDIR .= "/contents"; +-d $DBDIR || mkpath( $DBDIR ); -for my $archive (@archives) { for my $suite (@suites) { - - for my $arch (@archs) { +for my $suite (@suites) { + for my $arch (@archs) { - my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz"; my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db"; - next unless -f $filename; - my $ftime = (stat $filename)[10]; # Note: ctime, because mtime is set back via rsync my $dbtime = (stat $filelist_db)[9]; - next if defined $dbtime and $dbtime > $ftime; - print "Reading $archive/$suite/$arch...\n"; - my %packages_contents = (); my %packages_contents_nr = (); my %packages_contents_lastword = (); - + my $extra = ""; $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY; - + open REVERSED, "$extra>$DBDIR/reverse.tmp" - or die "Failed to open output reverse file"; - - open CONT, "zcat $filename|$what"; - while () {last if /^FILE/mo;} - while () { - my $data = ""; - my %data = (); - chomp; - print "Doing line ".($./1000)."k (out of approx 1.5M)\n" if $. % 250000 == 0; - /^(.+?)\s+(\S+)$/o; - my ($file, $value) = ($1, $2); - $value =~ s#[^,/]+/##og; - my @packages = split /,/, $value; - for (@packages) { + or die "Failed to open output reverse file: $!"; + + my $changed = 0; + for my $archive (@archives) { + + my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz"; + next unless -f $filename; + # Note: ctime, because mtime is set back via rsync + my $ftime = (stat $filename)[10]; + next if defined $dbtime and $dbtime > $ftime; + print "$archive/$suite/$arch needs update\n"; + $changed++; + } + if ($changed) { + for my $archive (@archives) { + + my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz"; + next unless -f $filename; + print "Reading $archive/$suite/$arch...\n"; + + open CONT, "zcat $filename|$what" + or die $!; + while () { last if /^FILE/mo; } + if (eof(CONT)) { # no header found + close CONT; # explicit close to reset $. + open CONT, "zcat $filename|$what"; + } + while () { + my $data = ""; + my %data = (); + chomp; + print "Doing line ".($./1000)."k (out of approx 2.0M)\n" if $. % 250000 == 0; + /^(.+?)\s+(\S+)$/o; + my ($file, $value) = ($1, $2); + $value =~ s#[^,/]+/##og; + my @packages = split /,/, $value; + for (@packages) { $packages_contents_nr{$_}++; my $lw = $packages_contents_lastword{$_} || "\0"; my $i=0; @@ -86,92 +105,110 @@ for my $archive (@archives) { for my $suite (@suites) { $i = 255 if $i > 255; $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i)); $packages_contents_lastword{$_} = "$file\0"; - } - # Searches are case-insensitive - $file =~ tr [A-Z] [a-z]; - - print REVERSED (reverse $file)."\0".(join ":$arch\0", @packages).":$arch\n"; - } - close CONT; - close REVERSED; + } + # Searches are case-insensitive + (my $nocase = $file) =~ tr [A-Z] [a-z]; + my $case = ($nocase eq $file) ? '-' : $file; - print "Sorting reverse list if needed\n"; - system("cd $DBDIR && sort reverse.tmp > reverse.sorted && mv reverse.{sorted,tmp}") == 0 + print REVERSED (reverse $nocase)."\0".$case."\0". + (join ":$arch\0", @packages).":$arch\n"; + } + close CONT; + + } + close REVERSED; + + print "Sorting reverse list if needed\n"; + system("cd $DBDIR && sort reverse.tmp > reverse.sorted && mv reverse.{sorted,tmp}") == 0 or die "Failed to sort reverse" unless $SORT_REVERSE_CONCURRENTLY; - - print "Writing filelist db\n"; - tie my %packages_contents_db, "DB_File", "$filelist_db.new", - O_RDWR|O_CREAT, 0666, $DB_BTREE + + print "Writing filelist db\n"; + tie my %packages_contents_db, "DB_File", "$filelist_db.new", + O_RDWR|O_CREAT, 0666, $DB_BTREE or die "Error creating DB: $!"; - while (my ($k, $v) = each(%packages_contents)) { + while (my ($k, $v) = each(%packages_contents)) { $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k}) - . $v; - } - untie %packages_contents_db; - - rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt"); - - rename("$filelist_db.new", $filelist_db); - system("ln -sf $filelist_db $DBDIR/filelists_${suite}_all.db") == 0 + . $v; + } + untie %packages_contents_db; + + rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt"); + + rename("$filelist_db.new", $filelist_db); + system("ln", "-sf", $filelist_db, "$DBDIR/filelists_${suite}_all.db") == 0 or die "Oops"; - } - - print "Merging reverse path lists for ${suite}...\n"; - - open MERGED, "sort -m $DBDIR/reverse_${suite}_*.txt |" + } + } + + my $go = 0; + my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9]; + for my $file (glob "$DBDIR/reverse_${suite}_*.txt") { + $go = 1 if not defined $suite_mtime + or $suite_mtime < (stat $file)[9]; + } + next unless $go; + + print "Merging reverse path lists for ${suite}...\n"; + + open MERGED, "-|", "sort -m $DBDIR/reverse_${suite}_*.txt" or die "Failed to open merged list"; - open FILENAMES, "> $DBDIR/filenames_$suite.txt.new" + open FILENAMES, ">", "$DBDIR/filenames_$suite.txt.new" or die "Failed to open filenames list"; - tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new", - O_RDWR|O_CREAT, 0666, $DB_BTREE - or die "Error creating DB: $!"; - - my $lastpath = ""; - my $lastfile = ""; - my @matches = (); - while () { - print "Doing line ".($./1000000)."M (out of approx. 16M)\n" if $. % 1000000 == 0; + tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new", + O_RDWR|O_CREAT, 0666, $DB_BTREE + or die "Error creating DB: $!"; + + my $lastpath = my $lastcasepath = my $lastfile = ""; + my %matches = (); + while () { + print "Doing line ".($./1000000)."M (out of approx. 20M)\n" + if $. % 1000000 == 0; chomp; my @line = split /\0/o, $_; my $revpath = shift @line; + my $casepath = shift @line; if ($revpath ne $lastpath) { - # Wrap: Do useful stuff with this ($lastpath, @matches) - $reverse_path_db{$lastpath} = join "\0", @matches if $lastpath ne ""; - $lastpath =~ s,/.*,,o; - if ($lastfile ne $lastpath) { + # Wrap: Do useful stuff with this ($lastpath, @matches) + if ($lastpath ne "") { + my @matches; + while (my ($k, $v) = each %matches) { + push @matches, join("\0", $k, @$v); + } + $reverse_path_db{$lastpath} = join "\1", @matches; + %matches = (); + } + $lastpath =~ s,/.*,,o; + if ($lastfile ne $lastpath) { $lastfile = $lastpath; print FILENAMES (reverse $lastfile)."\n"; - } - # - $lastpath = $revpath; - @matches = @line; - next; + } + # + $lastpath = $revpath; + $lastcasepath = $casepath; + $matches{$casepath} = \@line; + next; +# } elsif ($lastcasepath ne "" and $casepath ne $lastcasepath) { +# warn reverse($revpath)." has more than one casepath: $casepath $lastcasepath\n"; + } + push @{$matches{$casepath}}, @line; + } + # Note: do useful stuff here too, for out last entry. Maybe prevent this by + # adding a fake ultimate entry? + { + my @matches; + while (my ($k, $v) = each %matches) { + push @matches, join("\0", $k, @$v); } - push @matches, @line - } - # Note: do useful stuff here too, for out last entry. Maybe prevent this by - # adding a fake ultimate entry? - $reverse_path_db{$lastpath} = join "\0", @matches; - - untie %reverse_path_db; - close FILENAMES; - close MERGED; - - rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt"; - rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db"; -}} - -#print "Writing databases...\n"; - -# FIXME: missing filenames due to optimising above. Need to store filenames -# per-suite/arch, but merge them in the end for better cached searching -#open FILENAMES, "> $DBDIR/filenames.txt.new"; -#for (keys %filenames) { -# print FILENAMES "$_\n"; -#} -#close FILENAMES; - -#rename("$DBDIR/filenames.txt.new", "$DBDIR/filenames.txt"); + $reverse_path_db{$lastpath} = join "\1", @matches; + } + + untie %reverse_path_db; + close FILENAMES; + close MERGED; + + rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt"; + rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db"; +} # vim: set ts=4