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
27 # Important, we want sorting and such to happen like in the C locale: binary,
28 # without any fancy collation. FIXME: is this actually adequate?
31 my $what = $ARGV[0] ? "head -10000|" : "";
33 # More RAM vs more disk I/O tradeoff parameters, does not change
34 # functionality. True will always use more RAM at the benefit of less
35 # temporary files, and is adviced when possible
36 my $SORT_REVERSE_CONCURRENTLY = 1;
41 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
42 &Packages::Config::init( './' );
44 my @archives =( 'us'); #@ARCHIVES # NOT-IMPLEMENTED-YET
46 my @archs = @ARCHITECTURES;
48 $DBDIR .= "/contents";
49 -d $DBDIR || mkpath( $DBDIR );
51 for my $archive (@archives) { for my $suite (@suites) {
53 for my $arch (@archs) {
55 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
56 my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
57 next unless -f $filename;
58 my $ftime = (stat $filename)[10]; # Note: ctime, because mtime is set back via rsync
59 my $dbtime = (stat $filelist_db)[9];
60 next if defined $dbtime and $dbtime > $ftime;
61 print "Reading $archive/$suite/$arch...\n";
63 my %packages_contents = ();
64 my %packages_contents_nr = ();
65 my %packages_contents_lastword = ();
68 $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
70 open REVERSED, "$extra>$DBDIR/reverse.tmp"
71 or die "Failed to open output reverse file: $!";
73 open CONT, "zcat $filename|$what"
75 while (<CONT>) {last if /^FILE/mo;}
80 print "Doing line ".($./1000)."k (out of approx 1.5M)\n" if $. % 250000 == 0;
82 my ($file, $value) = ($1, $2);
83 $value =~ s#[^,/]+/##og;
84 my @packages = split /,/, $value;
86 $packages_contents_nr{$_}++;
87 my $lw = $packages_contents_lastword{$_} || "\0";
89 while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
92 $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
93 $packages_contents_lastword{$_} = "$file\0";
95 # Searches are case-insensitive
96 $file =~ tr [A-Z] [a-z];
98 print REVERSED (reverse $file)."\0".(join ":$arch\0", @packages).":$arch\n";
103 print "Sorting reverse list if needed\n";
104 system("cd $DBDIR && sort reverse.tmp > reverse.sorted && mv reverse.{sorted,tmp}") == 0
105 or die "Failed to sort reverse"
106 unless $SORT_REVERSE_CONCURRENTLY;
108 print "Writing filelist db\n";
109 tie my %packages_contents_db, "DB_File", "$filelist_db.new",
110 O_RDWR|O_CREAT, 0666, $DB_BTREE
111 or die "Error creating DB: $!";
112 while (my ($k, $v) = each(%packages_contents)) {
113 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
116 untie %packages_contents_db;
118 rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
120 rename("$filelist_db.new", $filelist_db);
121 system("ln -sf $filelist_db $DBDIR/filelists_${suite}_all.db") == 0
127 my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
128 for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
129 $go = 1 if not defined $suite_mtime
130 or $suite_mtime < (stat $file)[9];
134 print "Merging reverse path lists for ${suite}...\n";
136 open MERGED, "sort -m $DBDIR/reverse_${suite}_*.txt |"
137 or die "Failed to open merged list";
138 open FILENAMES, "> $DBDIR/filenames_$suite.txt.new"
139 or die "Failed to open filenames list";
140 tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
141 O_RDWR|O_CREAT, 0666, $DB_BTREE
142 or die "Error creating DB: $!";
148 print "Doing line ".($./1000000)."M (out of approx. 16M)\n" if $. % 1000000 == 0;
150 my @line = split /\0/o, $_;
151 my $revpath = shift @line;
152 if ($revpath ne $lastpath) {
153 # Wrap: Do useful stuff with this ($lastpath, @matches)
154 $reverse_path_db{$lastpath} = join "\0", @matches if $lastpath ne "";
155 $lastpath =~ s,/.*,,o;
156 if ($lastfile ne $lastpath) {
157 $lastfile = $lastpath;
158 print FILENAMES (reverse $lastfile)."\n";
161 $lastpath = $revpath;
167 # Note: do useful stuff here too, for out last entry. Maybe prevent this by
168 # adding a fake ultimate entry?
169 $reverse_path_db{$lastpath} = join "\0", @matches;
171 untie %reverse_path_db;
175 rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt";
176 rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db";