File Coverage

blib/lib/KinoSearch1/Util/SortExternal.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 4 100.0
condition 1 3 33.3
subroutine 8 8 100.0
pod 1 3 33.3
total 46 50 92.0


line stmt bran cond sub pod time code
1             package KinoSearch1::Util::SortExternal;
2 34     34   27158 use strict;
  34         84  
  34         1170  
3 34     34   186 use warnings;
  34         74  
  34         896  
4 34     34   799 use KinoSearch1::Util::ToolSet;
  34         75  
  34         4740  
5 34     34   200 use base qw( KinoSearch1::Util::CClass );
  34         70  
  34         2343916  
6              
7             BEGIN {
8 34     34   389 __PACKAGE__->init_instance_vars(
9             # constructor args
10             invindex => undef,
11             seg_name => undef,
12             mem_threshold => 2**24,
13             );
14             }
15             our %instance_vars;
16              
17             sub new {
18 69     69 1 7152 my $class = shift;
19 69         361 verify_args( \%instance_vars, @_ );
20 69         426 my %args = ( %instance_vars, @_ );
21 69         175 my $invindex = $args{invindex};
22              
23 69   33     387 $class = ref($class) || $class;
24              
25 69         192 my $filename = "$args{seg_name}.srt";
26 69 100       273 $invindex->delete_file($filename) if $invindex->file_exists($filename);
27 69         274 my $outstream = $invindex->open_outstream($filename);
28              
29 69         4974 return _new( $class, $outstream,
30             @args{qw( invindex seg_name mem_threshold )} );
31             }
32              
33             # Prepare to start fetching sorted results.
34             sub sort_all {
35 69     69 0 25768 my $self = shift;
36              
37             # deal with any items in the cache right now
38 69 100       727 if ( $self->_get_num_runs == 0 ) {
39             # if we've never exceeded mem_threshold, sort in-memory
40 66         25352 $self->_sort_cache;
41             }
42             else {
43             # create a run from whatever's in the cache right now
44 3         80 $self->_sort_run;
45             }
46              
47             # done adding elements, so close file and reopen as an instream
48 69         829 $self->_get_outstream->close;
49 69         430 my $filename = $self->_get_seg_name . ".srt";
50 69         785 my $instream = $self->_get_invindex()->open_instream($filename);
51 69         392 $self->_set_instream($instream);
52              
53             # allow fetching now that we're set up
54 69         380 $self->_enable_fetch;
55             }
56              
57 62     62 0 511 sub close { shift->_get_instream()->close }
58              
59             1;
60              
61             __END__