| 1 | package Parse::ExuberantCTags::Merge; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 8 | 2 | ▶ | use constant DEBUG => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use constant SMALL_DEFAULT => 2**22; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use constant SUPER_SMALL_DEFAULT => 2**17; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use constant FILENAME => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use constant SORTED => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use constant MRG_LINE => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use constant MRG_FH => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | use Class::XSAccessor | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | constructor => 'new', | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | accessors => { | ||||||||||||||||||||||||||||
| 22 | small_size_threshold => 'small_size_threshold', | ||||||||||||||||||||||||||||
| 23 | super_small_size_threshold => 'super_small_size_threshold', | ||||||||||||||||||||||||||||
| 24 | tempdir => 'tempdir', | ||||||||||||||||||||||||||||
| 25 | 2 | }; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use Parse::ExuberantCTags::Merge::SimpleScopeGuard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 21 | ▶ | sub add_file { | ||||||||||||||||||||||||||
|
Subroutine
add_file: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 21 | my $file = shift; | |||||||||||||||||||||||||||
| 35 | 21 | ▶ | Carp::croak("Need file argument") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | if not defined $file; | ||||||||||||||||||||||||||||
| 37 | 21 | ▶ | Carp::croak("Input file '$file' does not exist") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | if not -f $file; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 21 | my %opts = @_; | |||||||||||||||||||||||||||
| 41 | 21 | my $sorted = $opts{sorted}; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 21 | ▶ | $self->{files} ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"files"} ||= []
MC/DC100%1/1
$self->{"files"} ||= []
$self->{"files"}
Truth table100%2/2
A
A= $self->{"files"}
| |||||||||||||||||||||||||||||
| 44 | 21 | push @{$self->{files}}, [$file, $sorted]; | |||||||||||||||||||||||||||
| 45 | 21 | return(); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 12 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=25 SCAR=32.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 12 | my $outfile = shift; | |||||||||||||||||||||||||||
| 52 | 12 | ▶ | Carp::croak("Need output file argument") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | if not defined $outfile; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # determine temporary directory | ||||||||||||||||||||||||||||
| 56 | 12 | my $tmpdir = $self->tempdir; | |||||||||||||||||||||||||||
| 57 | 12 | ▶ | if (not defined $tmpdir or not -d $tmpdir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $tmpdir and -d $tmpdir
MC/DC0%0/2
defined $tmpdir and -d $tmpdir
defined $tmpdir
-d $tmpdir
Truth table33%1/3
A and B
A= defined $tmpdirB= -d $tmpdir
| |||||||||||||||||||||||||||||
| 58 | 12 | $tmpdir = File::Spec->tmpdir(); | |||||||||||||||||||||||||||
| 59 | 12 | $self->tempdir($tmpdir); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 12 | my $total_size = 0; | |||||||||||||||||||||||||||
| 63 | 12 | my $sorted_size = 0; | |||||||||||||||||||||||||||
| 64 | 12 | my $unsorted_size = 0; | |||||||||||||||||||||||||||
| 65 | 12 | my @sorted; | |||||||||||||||||||||||||||
| 66 | my @unsorted; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 12 | my $files = $self->{files}; | |||||||||||||||||||||||||||
| 69 | 12 | ▶ | Carp::croak("Need input files") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $files or @$files == 0
MC/DC0%0/2
not defined $files or @$files == 0
not defined $files
@$files == 0
Truth table33%1/3
not(A) or B
A= not defined $filesB= @$files == 0
| |||||||||||||||||||||||||||||
| 70 | if not defined $files or @$files == 0; | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # only one sorted input file => copy | ||||||||||||||||||||||||||||
| 73 | 12 | ▶ | if (@$files == 1 and $files->[0][SORTED]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$files == 1 and $files->[0][1]
MC/DC100%2/2
@$files == 1 and $files->[0][1]
@$files == 1
$files->[0][1]
Truth table100%3/3
A and B
A= @$files == 1B= $files->[0][1]
| |||||||||||||||||||||||||||||
| 74 | 1 | warn "Only one sorted input file => copying" if DEBUG; | |||||||||||||||||||||||||||
| 75 | 1 | my $infile = $files->[0][FILENAME]; | |||||||||||||||||||||||||||
| 76 | 1 | ▶ | open my $fh, '<', $infile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | or die "Opening input file '$infile' for reading failed: $!"; | ||||||||||||||||||||||||||||
| 78 | 1 | ▶ | open my $ofh, '>', $outfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | or die "Opening output file '$outfile' for writing failed: $!"; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | print $ofh "!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted/\n"; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | local $/ = \1000000; | |||||||||||||||||||||||||||
| 84 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 1 | print $ofh $_; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 1 | close $fh; | |||||||||||||||||||||||||||
| 88 | 1 | close $ofh; | |||||||||||||||||||||||||||
| 89 | 1 | return(1); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # calculate the file sizes | ||||||||||||||||||||||||||||
| 93 | 11 | foreach my $file (@$files) { | |||||||||||||||||||||||||||
| 94 | 20 | my $fname = $file->[FILENAME]; | |||||||||||||||||||||||||||
| 95 | 20 | my $s = -s $fname; | |||||||||||||||||||||||||||
| 96 | 20 | $total_size += $s; | |||||||||||||||||||||||||||
| 97 | 20 | ▶ | if ($file->[SORTED]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 9 | $sorted_size += $s; | |||||||||||||||||||||||||||
| 99 | 9 | push @sorted, $fname; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 11 | $unsorted_size += $s; | |||||||||||||||||||||||||||
| 103 | 11 | push @unsorted, $fname; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # get size thresholds | ||||||||||||||||||||||||||||
| 108 | 11 | my $threshold_super_small = $self->super_small_size_threshold(); | |||||||||||||||||||||||||||
| 109 | 11 | ▶ | $threshold_super_small = SUPER_SMALL_DEFAULT if not defined $threshold_super_small; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 11 | my $threshold_small = $self->small_size_threshold(); | |||||||||||||||||||||||||||
| 111 | 11 | ▶ | $threshold_small = SMALL_DEFAULT if not defined $threshold_small; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 11 | warn "Thresholds: tiny=$threshold_super_small small=$threshold_small" if DEBUG > 1; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # storage of temporary files and guard to clean them up on scope exit | ||||||||||||||||||||||||||||
| 115 | 11 | my @tmpfiles; | |||||||||||||||||||||||||||
| 116 | 11 | my $guard = Parse::ExuberantCTags::Merge::SimpleScopeGuard->new(files => \@tmpfiles); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # select sort strategy | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # everything small, sort all in memory regardless | ||||||||||||||||||||||||||||
| 121 | 11 | ▶ | if ($total_size < $threshold_super_small) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 5 | warn "Total size < super-small-threshold => memory sort" if DEBUG; | |||||||||||||||||||||||||||
| 123 | 5 | ▶ | open my $ofh, '>', $outfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | or die "Could not open output file '$outfile' for writing: $!"; | ||||||||||||||||||||||||||||
| 125 | 5 | return $self->_memory_sort($ofh, @sorted, @unsorted); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # This must handle the unsorted files | ||||||||||||||||||||||||||||
| 129 | 6 | ▶ | if (@unsorted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 4 | warn "There are unsorted files..." if DEBUG; | |||||||||||||||||||||||||||
| 131 | 4 | ▶ | if ($unsorted_size < $threshold_small) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | # unsorted files are small and will be sorted in memory | ||||||||||||||||||||||||||||
| 133 | 2 | warn "Unsorted files small => memory sort" if DEBUG; | |||||||||||||||||||||||||||
| 134 | 2 | my ($tfh, $tmpfile); | |||||||||||||||||||||||||||
| 135 | 2 | ▶ | if (@sorted) { # if there are sorted files (must be largish), use a tempfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 1 | ($tfh, $tmpfile) = File::Temp::tempfile( | |||||||||||||||||||||||||||
| 137 | "ctagsSortXXXXXX", UNLINK => 0, DIR => $tmpdir | ||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||
| 139 | 1 | push @tmpfiles, $tmpfile; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { # unsorted only => use real output file | ||||||||||||||||||||||||||||
| 142 | 1 | ▶ | open $tfh, '>', $outfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | or die "Could not open output file '$outfile' for writing: $!"; | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 2 | $self->_memory_sort($tfh, @unsorted); | |||||||||||||||||||||||||||
| 146 | 2 | close $tfh; | |||||||||||||||||||||||||||
| 147 | 2 | ▶ | if (not @sorted) { # only unsorted data => done! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 1 | return 1; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 1 | push @sorted, $tmpfile; | |||||||||||||||||||||||||||
| 151 | 1 | $sorted_size += -s $tmpfile; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | elsif ($sorted_size < $threshold_small) { | ||||||||||||||||||||||||||||
| 154 | # handle everything with Sort::External | ||||||||||||||||||||||||||||
| 155 | # don't bother with merge-sorting the small sorted files | ||||||||||||||||||||||||||||
| 156 | 1 | warn "Sorted files small or not existant => external sort for all" if DEBUG; | |||||||||||||||||||||||||||
| 157 | 1 | ▶ | open my $ofh, '>', $outfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | or die "Could not open output file '$outfile' for writing: $!"; | ||||||||||||||||||||||||||||
| 159 | 1 | return $self->_external_sort($ofh, @unsorted, @sorted); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||
| 162 | # both are large. First do an external sort on the unsorted files, | ||||||||||||||||||||||||||||
| 163 | # then do a merge sort | ||||||||||||||||||||||||||||
| 164 | 1 | warn "potentially large files => external sort for unsorted files" if DEBUG; | |||||||||||||||||||||||||||
| 165 | 1 | my ($tfh, $tmpfile) = File::Temp::tempfile( | |||||||||||||||||||||||||||
| 166 | "ctagsSortXXXXXX", UNLINK => 0, DIR => $tmpdir | ||||||||||||||||||||||||||||
| 167 | ); | ||||||||||||||||||||||||||||
| 168 | 1 | push @tmpfiles, $tmpfile; | |||||||||||||||||||||||||||
| 169 | 1 | $self->_external_sort($tfh, @unsorted); | |||||||||||||||||||||||||||
| 170 | 1 | close $tfh; | |||||||||||||||||||||||||||
| 171 | 1 | push @sorted, $tmpfile; | |||||||||||||||||||||||||||
| 172 | 1 | $sorted_size += -s $tmpfile; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } # end if there is unsorted data | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # at this point, there should be only sorted files | ||||||||||||||||||||||||||||
| 177 | # left => merge sort | ||||||||||||||||||||||||||||
| 178 | 4 | warn "running merge sort" if DEBUG; | |||||||||||||||||||||||||||
| 179 | 4 | ▶ | open my $ofh, '>', $outfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | or die "Could not open output file '$outfile' for writing: $!"; | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 4 | return $self->_merge_sort($ofh, @sorted); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 4 | ▶ | sub _merge_sort { | ||||||||||||||||||||||||||
|
Subroutine
_merge_sort: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 187 | 4 | warn "running _merge_sort" if DEBUG; | |||||||||||||||||||||||||||
| 188 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 189 | 4 | my $ofh = shift; | |||||||||||||||||||||||||||
| 190 | 4 | my @infiles = @_; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 4 | print $ofh "!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted/\n"; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 4 | local $/ = "\n"; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # get the first lines and create a list of simple structs for sorting | ||||||||||||||||||||||||||||
| 197 | my @files = | ||||||||||||||||||||||||||||
| 198 | map { | ||||||||||||||||||||||||||||
| 199 | 4 | ▶ | open my $fh, '<', $_ or die "Can't open input file '$_' for reading: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 8 | my $first = <$fh>; | |||||||||||||||||||||||||||
| 201 | 8 | ▶ | $first = <$fh> if $first =~ /^!_TAG_FILE_SORTED\t/; # skip magic line | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 8 | [$first, $fh] | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | @infiles; | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # initial sort of the first lines | ||||||||||||||||||||||||||||
| 207 | 4 | @files = sort {$a->[MRG_LINE] cmp $b->[MRG_LINE]} @files; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # keep sorting until all sources run out | ||||||||||||||||||||||||||||
| 210 | 4 | ▶ | while (@files) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | # first file in the list always has the next "lowest" line | ||||||||||||||||||||||||||||
| 212 | 24 | my $next = $files[0]; | |||||||||||||||||||||||||||
| 213 | 24 | print $ofh $next->[MRG_LINE]; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # fetch a new line for this file handle | ||||||||||||||||||||||||||||
| 216 | 24 | my $fh = $next->[MRG_FH]; | |||||||||||||||||||||||||||
| 217 | 24 | $next->[MRG_LINE] = <$fh>; | |||||||||||||||||||||||||||
| 218 | 24 | ▶ | if (not defined $next->[MRG_LINE]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | # eof, lose the file | ||||||||||||||||||||||||||||
| 220 | 8 | splice(@files, 0, 1); | |||||||||||||||||||||||||||
| 221 | 8 | next; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # one pass of bubble sort to propagate the new line to its place | ||||||||||||||||||||||||||||
| 225 | 16 | ▶ | for (my $i = 1; $i < @files; ++$i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 8 | ▶ | if (($files[$i-1][MRG_LINE] cmp $files[$i][MRG_LINE]) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 0 | my $tmp = $files[$i-1]; | |||||||||||||||||||||||||||
| 228 | 0 | $files[$i-1] = $files[$i]; | |||||||||||||||||||||||||||
| 229 | 0 | $files[$i] = $tmp; | |||||||||||||||||||||||||||
| 230 | } else { | ||||||||||||||||||||||||||||
| 231 | 8 | last; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } # end while there are files | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 4 | return(1); | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 2 | ▶ | sub _external_sort { | ||||||||||||||||||||||||||
|
Subroutine
_external_sort: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 242 | 2 | warn "running _external_sort" if DEBUG; | |||||||||||||||||||||||||||
| 243 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 244 | 2 | my $ofh = shift; | |||||||||||||||||||||||||||
| 245 | 2 | my @infiles = @_; | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 2 | print $ofh "!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted/\n"; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 2 | require Sort::External; | |||||||||||||||||||||||||||
| 250 | 2 | my $exsort = Sort::External->new( | |||||||||||||||||||||||||||
| 251 | mem_threshold => 1024**2 * 32, # todo: configuration | ||||||||||||||||||||||||||||
| 252 | ); | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 2 | local $/ = "\n"; | |||||||||||||||||||||||||||
| 255 | 2 | foreach my $infile (@infiles) { | |||||||||||||||||||||||||||
| 256 | 3 | ▶ | open my $fh, '<', $infile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | or die "Could not open input file '$infile' for reading: $!"; | ||||||||||||||||||||||||||||
| 258 | 3 | my $first_line = <$fh>; | |||||||||||||||||||||||||||
| 259 | 3 | ▶ | $exsort->feed($first_line) if $first_line !~ /^!_TAG_FILE_SORTED/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 3 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 6 | $exsort->feed($_); | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 3 | close $fh; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 2 | $exsort->finish(); | |||||||||||||||||||||||||||
| 266 | 2 | ▶ | while (defined($_ = $exsort->fetch)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 9 | print $ofh $_; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 2 | return(1); | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 7 | ▶ | sub _memory_sort { | ||||||||||||||||||||||||||
|
Subroutine
_memory_sort: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 275 | 7 | warn "running _memory_sort" if DEBUG; | |||||||||||||||||||||||||||
| 276 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 277 | 7 | my $ofh = shift; | |||||||||||||||||||||||||||
| 278 | 7 | my @infiles = @_; | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 7 | local $/ = "\n"; | |||||||||||||||||||||||||||
| 281 | 7 | my @records; | |||||||||||||||||||||||||||
| 282 | 7 | foreach my $infile (@infiles) { | |||||||||||||||||||||||||||
| 283 | 11 | ▶ | open my $fh, '<', $infile | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | or die "Could not open input file '$infile' for reading: $!"; | ||||||||||||||||||||||||||||
| 285 | 11 | my $first_line = <$fh>; | |||||||||||||||||||||||||||
| 286 | 11 | ▶ | push @records, $first_line if $first_line !~ /^!_TAG_FILE_SORTED/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 11 | push @records, <$fh>; | |||||||||||||||||||||||||||
| 288 | 11 | close $fh; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | 7 | @records = sort @records; # check fast inplace sort | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 7 | print $ofh "!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted/\n"; | |||||||||||||||||||||||||||
| 293 | 7 | print $ofh @records; | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 7 | return(1); | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 2 | 1; | |||||||||||||||||||||||||||
| 299 | __END__ | ||||||||||||||||||||||||||||