2 # Convert Contents.gz files into Sleepycat db files for efficient usage of
5 # Copyright (C) 2006 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 # Important, we want sorting and such to happen like in the C locale: binary,
27 # without any fancy collation. FIXME: is this actually adequate?
30 my $what = $ARGV[0] ? "head -10000|" : "";
32 # More RAM vs more disk I/O tradeoff parameters, does not change
33 # functionality. True will always use more RAM at the benefit of less
34 # temporary files, and is adviced when possible
35 my $SORT_REVERSE_CONCURRENTLY = 1;
40 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
41 &Packages::Config::init( './' );
43 my @archives = @ARCHIVES;
45 my @archs = @ARCHITECTURES;
47 $DBDIR .= "/contents";
48 -d $DBDIR || mkpath( $DBDIR );
50 for my $suite (@suites) {
51 for my $arch (@archs) {
53 my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
54 my $dbtime = (stat $filelist_db)[9];
55 my %packages_contents = ();
56 my %packages_contents_nr = ();
57 my %packages_contents_lastword = ();
60 $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
62 open REVERSED, "$extra>$DBDIR/reverse.tmp"
63 or die "Failed to open output reverse file: $!";
66 for my $archive (@archives) {
68 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
69 next unless -f $filename;
70 # Note: ctime, because mtime is set back via rsync
71 my $ftime = (stat $filename)[10];
72 next if defined $dbtime and $dbtime > $ftime;
73 print "$archive/$suite/$arch needs update\n";
77 for my $archive (@archives) {
79 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
80 next unless -f $filename;
81 print "Reading $archive/$suite/$arch...\n";
83 open CONT, "zcat $filename|$what"
85 while (<CONT>) {last if /^FILE/mo;}
86 open CONT, "zcat $filename|$what" if eof(CONT);
91 print "Doing line ".($./1000)."k (out of approx 1.5M)\n" if $. % 250000 == 0;
93 my ($file, $value) = ($1, $2);
94 $value =~ s#[^,/]+/##og;
95 my @packages = split /,/, $value;
97 $packages_contents_nr{$_}++;
98 my $lw = $packages_contents_lastword{$_} || "\0";
100 while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
102 $i = 255 if $i > 255;
103 $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
104 $packages_contents_lastword{$_} = "$file\0";
106 # Searches are case-insensitive
107 (my $nocase = $file) =~ tr [A-Z] [a-z];
108 my $case = ($nocase eq $file) ? '-' : $file;
110 print REVERSED (reverse $nocase)."\0".$case."\0".
111 (join ":$arch\0", @packages).":$arch\n";
118 print "Sorting reverse list if needed\n";
119 system("cd $DBDIR && sort reverse.tmp > reverse.sorted && mv reverse.{sorted,tmp}") == 0
120 or die "Failed to sort reverse"
121 unless $SORT_REVERSE_CONCURRENTLY;
123 print "Writing filelist db\n";
124 tie my %packages_contents_db, "DB_File", "$filelist_db.new",
125 O_RDWR|O_CREAT, 0666, $DB_BTREE
126 or die "Error creating DB: $!";
127 while (my ($k, $v) = each(%packages_contents)) {
128 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
131 untie %packages_contents_db;
133 rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
135 rename("$filelist_db.new", $filelist_db);
136 system("ln", "-sf", $filelist_db, "$DBDIR/filelists_${suite}_all.db") == 0
142 my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
143 for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
144 $go = 1 if not defined $suite_mtime
145 or $suite_mtime < (stat $file)[9];
149 print "Merging reverse path lists for ${suite}...\n";
151 open MERGED, "-|", "sort -m $DBDIR/reverse_${suite}_*.txt"
152 or die "Failed to open merged list";
153 open FILENAMES, ">", "$DBDIR/filenames_$suite.txt.new"
154 or die "Failed to open filenames list";
155 tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
156 O_RDWR|O_CREAT, 0666, $DB_BTREE
157 or die "Error creating DB: $!";
159 my $lastpath = my $lastcasepath = my $lastfile = "";
162 print "Doing line ".($./1000000)."M (out of approx. 16M)\n"
163 if $. % 1000000 == 0;
165 my @line = split /\0/o, $_;
166 my $revpath = shift @line;
167 my $casepath = shift @line;
168 if ($revpath ne $lastpath) {
169 # Wrap: Do useful stuff with this ($lastpath, @matches)
170 if ($lastpath ne "") {
172 while (my ($k, $v) = each %matches) {
173 push @matches, join("\0", $k, @$v);
175 $reverse_path_db{$lastpath} = join "\1", @matches;
178 $lastpath =~ s,/.*,,o;
179 if ($lastfile ne $lastpath) {
180 $lastfile = $lastpath;
181 print FILENAMES (reverse $lastfile)."\n";
184 $lastpath = $revpath;
185 $lastcasepath = $casepath;
186 $matches{$casepath} = \@line;
188 # } elsif ($lastcasepath ne "" and $casepath ne $lastcasepath) {
189 # warn reverse($revpath)." has more than one casepath: $casepath $lastcasepath\n";
191 push @{$matches{$casepath}}, @line;
193 # Note: do useful stuff here too, for out last entry. Maybe prevent this by
194 # adding a fake ultimate entry?
197 while (my ($k, $v) = each %matches) {
198 push @matches, join("\0", $k, @$v);
200 $reverse_path_db{$lastpath} = join "\1", @matches;
203 untie %reverse_path_db;
207 rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt";
208 rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db";