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 if (eof(CONT)) { # no header found
87 close CONT; # explicit close to reset $.
88 open CONT, "zcat $filename|$what";
94 print "Doing line ".($./1000)."k (out of approx 2.0M)\n" if $. % 250000 == 0;
96 my ($file, $value) = ($1, $2);
97 $value =~ s#[^,/]+/##og;
98 my @packages = split /,/, $value;
100 $packages_contents_nr{$_}++;
101 my $lw = $packages_contents_lastword{$_} || "\0";
103 while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
105 $i = 255 if $i > 255;
106 $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
107 $packages_contents_lastword{$_} = "$file\0";
109 # Searches are case-insensitive
110 (my $nocase = $file) =~ tr [A-Z] [a-z];
111 my $case = ($nocase eq $file) ? '-' : $file;
113 print REVERSED (reverse $nocase)."\0".$case."\0".
114 (join ":$arch\0", @packages).":$arch\n";
121 print "Sorting reverse list if needed\n";
122 system("cd $DBDIR && sort reverse.tmp > reverse.sorted && mv reverse.{sorted,tmp}") == 0
123 or die "Failed to sort reverse"
124 unless $SORT_REVERSE_CONCURRENTLY;
126 print "Writing filelist db\n";
127 tie my %packages_contents_db, "DB_File", "$filelist_db.new",
128 O_RDWR|O_CREAT, 0666, $DB_BTREE
129 or die "Error creating DB: $!";
130 while (my ($k, $v) = each(%packages_contents)) {
131 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
134 untie %packages_contents_db;
136 rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
138 rename("$filelist_db.new", $filelist_db);
139 system("ln", "-sf", $filelist_db, "$DBDIR/filelists_${suite}_all.db") == 0
145 my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
146 for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
147 $go = 1 if not defined $suite_mtime
148 or $suite_mtime < (stat $file)[9];
152 print "Merging reverse path lists for ${suite}...\n";
154 open MERGED, "-|", "sort -m $DBDIR/reverse_${suite}_*.txt"
155 or die "Failed to open merged list";
156 open FILENAMES, ">", "$DBDIR/filenames_$suite.txt.new"
157 or die "Failed to open filenames list";
158 tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
159 O_RDWR|O_CREAT, 0666, $DB_BTREE
160 or die "Error creating DB: $!";
162 my $lastpath = my $lastcasepath = my $lastfile = "";
165 print "Doing line ".($./1000000)."M (out of approx. 20M)\n"
166 if $. % 1000000 == 0;
168 my @line = split /\0/o, $_;
169 my $revpath = shift @line;
170 my $casepath = shift @line;
171 if ($revpath ne $lastpath) {
172 # Wrap: Do useful stuff with this ($lastpath, @matches)
173 if ($lastpath ne "") {
175 while (my ($k, $v) = each %matches) {
176 push @matches, join("\0", $k, @$v);
178 $reverse_path_db{$lastpath} = join "\1", @matches;
181 $lastpath =~ s,/.*,,o;
182 if ($lastfile ne $lastpath) {
183 $lastfile = $lastpath;
184 print FILENAMES (reverse $lastfile)."\n";
187 $lastpath = $revpath;
188 $lastcasepath = $casepath;
189 $matches{$casepath} = \@line;
191 # } elsif ($lastcasepath ne "" and $casepath ne $lastcasepath) {
192 # warn reverse($revpath)." has more than one casepath: $casepath $lastcasepath\n";
194 push @{$matches{$casepath}}, @line;
196 # Note: do useful stuff here too, for out last entry. Maybe prevent this by
197 # adding a fake ultimate entry?
200 while (my ($k, $v) = each %matches) {
201 push @matches, join("\0", $k, @$v);
203 $reverse_path_db{$lastpath} = join "\1", @matches;
206 untie %reverse_path_db;
210 rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt";
211 rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db";