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