blib/lib/CDB_File/Generator.pm

filter: Statementstmt 77.6% 59 / 76 Branchbran 37.5% 6 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 69.2% 9 / 13 Podpod 71.4% 5 / 7 totaltotal 65.2% 79 / 121 CC 11 SCAR 18.0
CC
11
Cov
66%
DESTROY
20.0
finish
6.0
new
4.1
CRAP 6.1
↑ Summary
1 package CDB_File::Generator;
2 1 $REVISION=q$Revision: 1.13 $ ;
3 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $VERSION='0.030';
5  
6 =head1 NAME
7  
8 CDB_File::Generator - generate massive sorted CDB files simply.
9  
10 =head1 SYNOPSIS
11  
12 use CDB_File::Generator;
13 $gen = new CDB::Generator "my.cdb";
14 $gen->("Fred", "Martha");
15 $gen->("Fred", "Olivia");
16 $gen->("Fred", "Jenny");
17 $gen->("Roger", "Joe");
18 $gen->("Roger", "Jenny");
19 $gen = undef;
20 use CDB_File;
21  
22  
23 =head1 DESCRIPTION
24  
25 This is a class which makes generating sorted large (much bigger than
26 memory, but the speed will depend on the efficiency of your sort
27 command. If you haven't got one, for example, it won't work at all.)
28 CDB files on the fly very easy
29  
30 =cut
31  
32 #this lets us have lots of unique temporary files.
33  
34 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 my $tempfile_no=0;
40 2 ▶ sub next_tmp_file () { $tempfile_no++ };
Subroutine next_tmp_file: called CC=1 SCAR=0.0
Pod undocumented
41 }
42  
43 =head1 METHODS
44  
45 =head2 Generator::new $cdbfile [$cdbmaketemp [{$tmpname [$sorttmpname] | $tmpdir}]]
46  
47 The new function creates a generator for a given filename, optionally
48 specifying where it sould put it's temporary files.
49  
50 =cut
51  
52 1 my $deftemp="/tmp/cdb_generator_tmp.$$.";
53  
54 1 ▶sub new ($$@) {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
55 1 my $class=shift;
56 1 my $self = bless {}, $class;
57 1 my $cdbfilename = shift;
58 1 $self->{"cdbfile"} = $cdbfilename;
59 1 $self->{"tmpoutfile"} = $deftemp . next_tmp_file();
60 1 $self->{"tmpsortfile"} = $deftemp . next_tmp_file();
61 1 my $tmpmake = $cdbfilename;
62 1 ▶ $tmpmake =~ s/.cdb$/.tmp/ or $tmpmake = $tmpmake . ".tmp";
Branch: 1 branch
BranchTrueFalseTotal
$tmpmake =~ s/.cdb$/.tmp/ or $tmpmake = $tmpmake . '.tmp' 1 0 1
63 1 ▶ Carp::croak "file $tmpmake for cdb temfile exists" if -e $tmpmake;
Branch: 1 branch
BranchTrueFalseTotal
if -e $tmpmake 0 1 1
64 1 $self->{"tmpmakefile"} = $tmpmake;
65 1 ▶ my $fh = new IO::File $self->{"tmpoutfile"}, '>'
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->{"tmpoutfile"}, ">") or die "couldn't create output file" 1 0 1
66 or die "couldn't create output file";
67 1 $self->{"fh"} = $fh;
68 1 $self->{"added"} = 0;
69 1 return $self;
70 }
71  
72 =head2 $gen->add($key, $value)
73  
74 Adds a value to the CDB being created
75  
76 =cut
77  
78 5 ▶sub add ($$$) {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
79 5 my ($self, $key, $value) = @_;
80 5 my $fh = $self->{"fh"};
81  
82 #change newlines so sort can sort everything as lines.
83 #change tabs so we can use them as a separator
84 5 $key =~ s,\\,\\\\,g; #now we have all \s in even numbered groups
85 5 $key =~ s,\n,\\n,g; #an odd \ followed by a newline is a nl
86 5 $key =~ s,\t,\\t,g; #an odd \ followed by a tab is a nl
87 5 $value =~ s,\\,\\\\,g; #....
88 5 $value =~ s,\n,\\n,g;
89 5 $value =~ s,\t,\\t,g; # ....
90  
91 5 print $fh $key, "\t", $value, "\n";
92  
93 5 $self->{"added"} ++;
94 }
95  
96 =head2 $gen->DESTROY
97  
98 This is not normally called by the user, but rather by the completion
99 of the cdbfile being writen out and that block of the program being
100 exited or by the program completing. When it us run, it calls the
101 finish method which ends the CDB creation. See below.
102  
103 =cut
104  
105 0 ▶sub DESTROY ($) {
Subroutine DESTROY: not called CC=4 SCAR=30.0
106 0 my $self=shift;
107 0 ▶ $self->{"added"} && $self->finish() unless $self->{"abort"};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"abort"} 0 0 0
Condition0%0/6
$self->{'abort'} || $self->{'added'} && $self->finish
l!l&&r!l&&!r
000
$self->{"added"} and $self->finish
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{'abort'} || $self->{'added'} && $self->finish
$self->{'abort'} $self->{"added"} $self->finish
Truth table0%0/4
A || B and C
A= $self->{'abort'}B= $self->{"added"}C= $self->finish
ABCresult
1XX1
0111
00X0
0100
108 0 foreach my $del ( "tmpoutfile", "tmpsortfile", "tmpmakefile") {
109 0 unlink $self->{$del};
110 }
111 }
112  
113 =head2 finish
114  
115 Finish ends of the cdb creation. First it closes the output temporary
116 file, then it sorts it to another file and finally it calls C<cdbmake>
117 to complete the creation job.
118  
119 In the current implementation this uses C<sort -u> and deletes repeats of
120 the same key with the same value.
121  
122 In order to increase database portability, by default all sorting is
123 done in the 'C' locale, even if the current program is working in
124 another locale. This is "the right thing" in many cases. Where you
125 are dealing with real word keys it won't be the right thing. In this case, use the locale function to set the locale.
126  
127 =cut
128  
129 0 ▶sub locale ($) {
Subroutine locale: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my $self=shift;
131 0 my $locale=shift;
132 0 $self->{locale}=$locale;
133 }
134  
135 1 ▶sub finish ($) {
Subroutine finish: called CC=5 SCAR=17.9
Pod documented
136 1 my $self=shift;
137 1 close $self->{"fh"};
138  
139 {
140 1 local $ENV{LC_ALL};
141 1 ▶ if ($self->{locale}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'locale'}) { } 0 1 1
142 0 $ENV{LC_ALL}=$self->{locale};
143 } else {
144 1 $ENV{LC_ALL}='C';
145 }
146 1 system 'sort' , '-u', '-o' ,$self->{"tmpsortfile"} , $self->{"tmpoutfile"};
147 }
148  
149 1 ▶ my $fh = new IO::File $self->{"tmpsortfile"}
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($self->{"tmpsortfile"}) or die "couldn't open sorted output file" 1 0 1
150 or die "couldn't open sorted output file";
151  
152 my $cdbmakeout = new IO::File ( '|cdbmake ' . $self->{"cdbfile"}
153 1 . ' ' . $self->{"tmpmakefile"} );
154  
155 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1 0 1
156 1 my ($key, $value) = m/^(.*)\t(.*)$/;
157  
158 #the \G s allow for multiple new lines in a row
159 #odd numbered slash with t is tab
160 1 $key =~ s,((?:\G|^|[^\\])(?:\\\\)*)\\t,$1\t,g;
161 #odd numbered slash with t is tab
162 1 $key =~ s,((?:\G|^|[^\\])(?:\\\\)*)\\n,$1\n,g;
163 #pairs of slashes match a single slash
164 1 $key =~ s,\\\\,\\,g;
165  
166 #same for value....
167 1 $value =~ s,((?:\G|^|[^\\])(?:\\\\)*)\\t,$1\t,g;
168 1 $value =~ s,((?:\G|^|[^\\])(?:\\\\)*)\\n,$1\n,g;
169 1 $value =~ s,\\\\,\\,g;
170  
171 1 print $cdbmakeout &gen_cdb_input($key, $value);
172 }
173 0 print $cdbmakeout "\n";
174 0 $cdbmakeout->close;
175 0 $fh->close;
176  
177 # $self->{"abort"} = 1;
178 #FIXME return codes etc..
179 unlink $self->{"tmpsortfile"} , $self->{"tmpoutfile"}
180 or warn "trouble deleting temp files "
181 0 ▶ . $self->{"tmpsortfile"} . $self->{"tmpoutfile"};
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"tmpsortfile"}, $self->{"tmpoutfile"} or warn 'trouble deleting temp files ' . $self->{"tmpsortfile"} . $self->{"tmpoutfile"} 0 0 0
182 0 $self->{"added"} = 0;
183 }
184  
185 =head2 $gen->abort
186  
187 If you decide not to create the CDB file you were creating, you have
188 to call this method. Otherwise, it will be created as your program
189 exits (or possibly earlier)
190  
191 =cut
192  
193 0 ▶sub abort ($) {
Subroutine abort: not called CC=1 SCAR=6.9
Pod documented
194 0 shift->{"abort"} = 1
195 }
196  
197 =head2 gen_cdb_input($key,$value)
198  
199 This is a little utility function which formats a cdbmake input line.
200  
201 =cut
202  
203 0 ▶sub gen_cdb_input ($$) {
Subroutine gen_cdb_input: not called CC=1 SCAR=6.9
Pod documented
204 0 my $key=shift;
205 0 my $value=shift;
206  
207 0 return "+" . length($key) . "," . length($value) . ":"
208 . $key . "->" . $value . "\n";
209 }
210  
211 =head1 BUGS
212  
213 We use the external programs C<sort> and C<cdbmake>. These almost
214 certainly improve our performance on large databases (and those are
215 all we care about), but they make portability difficult.. Possibly
216 system independent alternatives should be written and used where
217 needed.
218  
219 We should write out to the sort file with some encoding that gets rid
220 of new lines and then read back, de-coding that to feed it to cdbmake..
221  
222 =cut
223  
↑ Summary