File Coverage

blib/lib/KinoSearch1/Store/RAMInvIndex.pm
Criterion Covered Total %
statement 70 71 98.5
branch 8 14 57.1
condition n/a
subroutine 21 22 95.4
pod 1 10 10.0
total 100 117 85.4


line stmt bran cond sub pod time code
1             package KinoSearch1::Store::RAMInvIndex;
2 38     38   274077 use strict;
  38         588  
  38         1482  
3 38     38   634 use warnings;
  38         77  
  38         1101  
4 38     38   5648 use KinoSearch1::Util::ToolSet;
  38         104  
  38         6058  
5 38     38   238 use base qw( KinoSearch1::Store::InvIndex );
  38         75  
  38         9601  
6              
7             BEGIN {
8 38     38   568 __PACKAGE__->init_instance_vars(
9             # members
10             ramfiles => undef,
11             );
12             }
13              
14 38     38   238 use Digest::MD5 qw( md5_hex );
  38         82  
  38         2027  
15 38     38   6623 use KinoSearch1::Store::FSInvIndex;
  38         86  
  38         952  
16 38     38   341 use KinoSearch1::Store::InStream;
  38         90  
  38         1038  
17 38     38   204 use KinoSearch1::Store::OutStream;
  38         72  
  38         1070  
18 38     38   21481 use KinoSearch1::Store::RAMLock;
  38         155  
  38         32002  
19              
20             sub init_instance {
21 48     48 1 110 my $self = shift;
22 48         373 $self->{ramfiles} = {};
23              
24             # read in an FSInvIndex if specified
25 48 100       286 $self->_read_invindex if defined $self->{path};
26             }
27              
28             sub _read_invindex {
29 1     1   5 my $self = shift;
30              
31             # open an FSInvIndex for reading
32 1         12 my $source_invindex
33             = KinoSearch1::Store::FSInvIndex->new( path => $self->{path}, );
34              
35             # copy every file in the FSInvIndex into RAM.
36 1         7 for my $filename ( $source_invindex->list ) {
37 1         5 my $source_stream = $source_invindex->open_instream($filename);
38 1         5 my $outstream = $self->open_outstream($filename);
39 1         20 $outstream->absorb($source_stream);
40 1         8 $source_stream->close;
41 1         6 $outstream->close;
42             }
43              
44 1         6 $source_invindex->close;
45             }
46              
47             sub open_outstream {
48 839     839 0 18636 my ( $self, $filename ) = @_;
49              
50             # use perl scalars as virtual files;
51 839         1160 my $data = '';
52 839         2272 $self->{ramfiles}{$filename} = \$data;
53 839 50   32   9845 open( my $fh, '>:scalar', \$data ) or die $!;
  32         410  
  32         67  
  32         327  
54 839         53261 binmode($fh);
55 839         10740 return KinoSearch1::Store::OutStream->new($fh);
56             }
57              
58             sub open_instream {
59 619     619 0 1116 my ( $self, $filename, $offset, $len ) = @_;
60 619 50       1891 confess("File '$filename' not loaded into RAM")
61             unless exists $self->{ramfiles}{$filename};
62 619 50       9240 open( my $fh, '<:scalar', $self->{ramfiles}{$filename} ) or die $!;
63 619         935 binmode($fh);
64 619         6305 return KinoSearch1::Store::InStream->new( $$fh, $offset, $len );
65             }
66              
67             sub list {
68 1     1 0 590 keys %{ $_[0]->{ramfiles} };
  1         9  
69             }
70              
71             sub file_exists {
72 1206     1206 0 2150 my ( $self, $filename ) = @_;
73 1206         7462 return ( exists $self->{ramfiles}{$filename} );
74             }
75              
76             sub rename_file {
77 135     135 0 299 my ( $self, $from, $to ) = @_;
78 135 50       490 confess("File '$from' not currently in RAM")
79             unless exists $self->{ramfiles}{$from};
80 135         1578 $self->{ramfiles}{$to} = delete $self->{ramfiles}{$from};
81             }
82              
83             sub delete_file {
84 672     672 0 1600 my ( $self, $filename ) = @_;
85 672         1548 my $file = delete $self->{ramfiles}{$filename};
86 672 50       3531 confess("File '$filename' not currently in RAM")
87             unless $file;
88             }
89              
90             sub slurp_file {
91 11     11 0 21276 my ( $self, $filename ) = @_;
92 11         37 my $virtual_file_ref = $self->{ramfiles}{$filename};
93 11 50       41 confess("File '$filename' not currently in RAM")
94             unless defined $virtual_file_ref;
95              
96             # return a copy of the virtual file's content
97 11         79 return $$virtual_file_ref;
98             }
99              
100             sub make_lock {
101 190     190 0 1011 my $self = shift;
102 190         1401 return KinoSearch1::Store::RAMLock->new( @_, invindex => $self );
103             }
104 0     0 0 0 sub close { }
105              
106             1;
107              
108             __END__