]> git.deb.at Git - deb/packages.git/blob - bin/parse-contents
541cba24beff985572e0b6f9a25198ebc0a81bd0
[deb/packages.git] / bin / parse-contents
1 #!/usr/bin/perl -w
2 # Convert Contents.gz files into Sleepycat db files for efficient usage of
3 # data
4 #
5 # $Id$
6 #
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.
12
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.
17
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
21
22 use strict;
23 use lib './lib';
24
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?
27 $ENV{"LC_ALL"} = 'C';
28
29 my $what = $ARGV[0] ? "head -10000|" : "";
30
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;
35
36 use DB_File;
37 use Storable;
38 use File::Path;
39 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
40 &Packages::Config::init( './' );
41
42 my @archives =( 'us'); #@ARCHIVES # NOT-IMPLEMENTED-YET
43 my @suites = @SUITES;
44 my @archs = @ARCHITECTURES;
45
46 $DBDIR .= "/contents";
47 -d $DBDIR || mkpath( $DBDIR );
48
49 for my $archive (@archives) { for my $suite (@suites) {
50
51   for my $arch (@archs) {
52
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";
60
61         my %packages_contents = ();
62         my %packages_contents_nr = ();
63         my %packages_contents_lastword = ();
64
65         my $extra = "";
66         $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
67
68         open REVERSED, "$extra>$DBDIR/reverse.tmp"
69                 or die "Failed to open output reverse file: $!";
70
71         open CONT, "zcat $filename|$what"
72                 or die $!;
73         while (<CONT>) {last if /^FILE/mo;}
74         while (<CONT>) {
75                 my $data = "";
76                 my %data = ();
77                 chomp;
78                 print "Doing line ".($./1000)."k (out of approx 1.5M)\n" if $. % 250000 == 0;
79                 /^(.+?)\s+(\S+)$/o;
80                 my ($file, $value) = ($1, $2);
81                 $value =~ s#[^,/]+/##og;
82                 my @packages = split /,/, $value;
83                 for (@packages) {
84                         $packages_contents_nr{$_}++;
85                         my $lw = $packages_contents_lastword{$_} || "\0";
86                         my $i=0;
87                         while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
88                         $i--;
89                         $i = 255 if $i > 255;
90                         $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
91                         $packages_contents_lastword{$_} = "$file\0";
92                 }
93                 # Searches are case-insensitive
94                 $file =~ tr [A-Z] [a-z];
95
96                 print REVERSED (reverse $file)."\0".(join ":$arch\0", @packages).":$arch\n";
97         }
98         close CONT;
99         close REVERSED;
100
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;
105
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})
112                         . $v;
113         }
114         untie %packages_contents_db;
115
116         rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
117
118         rename("$filelist_db.new", $filelist_db);
119         system("ln -sf $filelist_db $DBDIR/filelists_${suite}_all.db") == 0
120                 or die "Oops";
121   }
122
123
124   my $go = 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];
129   }
130   next unless $go;
131
132   print "Merging reverse path lists for ${suite}...\n";
133
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: $!";
141
142   my $lastpath = "";
143   my $lastfile = "";
144   my @matches = ();
145   while (<MERGED>) {
146         print "Doing line ".($./1000000)."M (out of approx. 16M)\n" if $. % 1000000 == 0;
147         chomp;
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";
157           }
158           #
159           $lastpath = $revpath;
160           @matches = @line;
161           next;
162         }
163         push @matches, @line
164   }
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;
168
169   untie %reverse_path_db;
170   close FILENAMES;
171   close MERGED;
172
173   rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt";
174   rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db";
175 }}
176
177 # vim: set ts=4