2 # Convert Contents.gz files into Sleepycat db files for efficient usage of
7 # Copyright (C) 2006 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 # Important, we want sorting and such to happen like in the C locale: binary,
26 # without any fancy collation. FIXME: is this actually adequate?
29 my $what = $ARGV[0] ? "head -10000|" : "";
31 # More RAM vs more disk I/O tradeoff parameters, does not change
32 # functionality. True will always use more RAM at the benefit of less
33 # temporary files, and is adviced when possible
34 my $SORT_REVERSE_CONCURRENTLY = 1;
39 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
40 &Packages::Config::init( './' );
42 my @archives =( 'us'); #@ARCHIVES # NOT-IMPLEMENTED-YET
44 my @archs = @ARCHITECTURES;
46 $DBDIR .= "/contents";
47 -d $DBDIR || mkpath( $DBDIR );
49 for my $archive (@archives) { for my $suite (@suites) {
51 for my $arch (@archs) {
53 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
54 my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
55 next unless -f $filename;
56 my $ftime = (stat $filename)[10]; # Note: ctime, because mtime is set back via rsync
57 my $dbtime = (stat $filelist_db)[9];
58 next if defined $dbtime and $dbtime > $ftime;
59 print "Reading $archive/$suite/$arch...\n";
61 my %packages_contents = ();
62 my %packages_contents_nr = ();
63 my %packages_contents_lastword = ();
66 $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
68 open REVERSED, "$extra>$DBDIR/reverse.tmp"
69 or die "Failed to open output reverse file: $!";
71 open CONT, "zcat $filename|$what"
73 while (<CONT>) {last if /^FILE/mo;}
78 print "Doing line ".($./1000)."k (out of approx 1.5M)\n" if $. % 250000 == 0;
80 my ($file, $value) = ($1, $2);
81 $value =~ s#[^,/]+/##og;
82 my @packages = split /,/, $value;
84 $packages_contents_nr{$_}++;
85 my $lw = $packages_contents_lastword{$_} || "\0";
87 while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
90 $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
91 $packages_contents_lastword{$_} = "$file\0";
93 # Searches are case-insensitive
94 $file =~ tr [A-Z] [a-z];
96 print REVERSED (reverse $file)."\0".(join ":$arch\0", @packages).":$arch\n";
101 print "Sorting reverse list if needed\n";
102 system("cd $DBDIR && sort reverse.tmp > reverse.sorted && mv reverse.{sorted,tmp}") == 0
103 or die "Failed to sort reverse"
104 unless $SORT_REVERSE_CONCURRENTLY;
106 print "Writing filelist db\n";
107 tie my %packages_contents_db, "DB_File", "$filelist_db.new",
108 O_RDWR|O_CREAT, 0666, $DB_BTREE
109 or die "Error creating DB: $!";
110 while (my ($k, $v) = each(%packages_contents)) {
111 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
114 untie %packages_contents_db;
116 rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
118 rename("$filelist_db.new", $filelist_db);
119 system("ln -sf $filelist_db $DBDIR/filelists_${suite}_all.db") == 0
125 my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
126 for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
127 $go = 1 if not defined $suite_mtime
128 or $suite_mtime < (stat $file)[9];
132 print "Merging reverse path lists for ${suite}...\n";
134 open MERGED, "sort -m $DBDIR/reverse_${suite}_*.txt |"
135 or die "Failed to open merged list";
136 open FILENAMES, "> $DBDIR/filenames_$suite.txt.new"
137 or die "Failed to open filenames list";
138 tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
139 O_RDWR|O_CREAT, 0666, $DB_BTREE
140 or die "Error creating DB: $!";
146 print "Doing line ".($./1000000)."M (out of approx. 16M)\n" if $. % 1000000 == 0;
148 my @line = split /\0/o, $_;
149 my $revpath = shift @line;
150 if ($revpath ne $lastpath) {
151 # Wrap: Do useful stuff with this ($lastpath, @matches)
152 $reverse_path_db{$lastpath} = join "\0", @matches if $lastpath ne "";
153 $lastpath =~ s,/.*,,o;
154 if ($lastfile ne $lastpath) {
155 $lastfile = $lastpath;
156 print FILENAMES (reverse $lastfile)."\n";
159 $lastpath = $revpath;
165 # Note: do useful stuff here too, for out last entry. Maybe prevent this by
166 # adding a fake ultimate entry?
167 $reverse_path_db{$lastpath} = join "\0", @matches;
169 untie %reverse_path_db;
173 rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt";
174 rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db";