lib/Module/Generic/File/Mmap.pm

filter: Statementstmt 13.6% 54 / 396 Branchbran 0.0% 0 / 216 Conditioncond 0.0% 0 / 139 MC/DCmcdc 0.0% 0 / 79 Subroutinesub 28.3% 17 / 60 Podpod 88.8% 32 / 36 totaltotal 11.1% 103 / 926 CC 168 SCAR 59.2
CC
168
Cov
7%
open
1175.2
read
506.0
write
506.0
CRAP 372.1
« lib/Module/Generic/File.pm ↑ Summary lib/Module/Generic/File/Cache.pm »
1 ##----------------------------------------------------------------------------
2 ## Module Generic - ~/lib/Module/Generic/File/Mmap.pm
3 ## Version v0.1.5
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/07/26
7 ## Modified 2025/04/23
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Module::Generic::File::Mmap;
14 BEGIN
15 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 warnings::register_categories( 'Module::Generic' );
19 2 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use vars qw( $VERSION $HAS_CACHE_MMAP $HAS_B64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 # This is disruptive for everybody. Bad idea.
23 # use JSON 4.03 qw( -convert_blessed_universally );
24 2 ▶ use JSON 4.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶ use Module::Generic::File qw( file sys_tmpdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 our $HAS_CACHE_MMAP = 0;
27 2 our $VERSION = 'v0.1.5';
28 };
29  
30 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub init
34 0 ▶{
Subroutine init: not called CC=15 SCAR=54.8
Pod documented
35 0 my $self = shift( @_ );
36 0 $self->{base64} = undef;
37 0 $self->{cache_file} = '';
38 # Default action when accessing a shared file cache? If 1, it will create it if it does not exist already
39 0 $self->{create} = 0;
40 0 $self->{destroy} = 0;
41 0 $self->{key} = Data::UUID->new->create_str;
42 0 $self->{mode} = 0666;
43 0 $self->{serial} = '';
44 0 $self->{size} = 0;
45 0 $self->{_packing_method} = 'json';
46 0 $self->{_init_strict_use_sub} = 1;
47 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
48 0 ▶ unless( $HAS_CACHE_MMAP )
Branch: 1 branch
BranchTrueFalseTotal
unless ($Module::Generic::File::Mmap::HAS_CACHE_MMAP) 0 0 0
49 {
50 0 ▶ $self->_load_class( 'Cache::FastMmap' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Cache::FastMmap") or return $self->pass_error 0 0 0
51 0 $HAS_CACHE_MMAP = 1;
52 }
53 0 ▶ return( $self->error( "No key was set." ) ) if( !defined( $self->{key} ) || !length( $self->{key} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"key"} and length $self->{"key"} 0 0 0
Condition0%0/3
defined $self->{"key"} and length $self->{"key"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"key"} and length $self->{"key"}
defined $self->{"key"} length $self->{"key"}
Truth table0%0/3
A and B
A= defined $self->{"key"}B= length $self->{"key"}
ABresult
0X0
100
111
54 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
55 0 my $tmpdir = sys_tmpdir();
56 0 $self->{_cache_dir} = $tmpdir->child( 'mmap_data' );
57 0 ▶ if( $self->{_cache_dir}->exists && !$self->{_cache_dir}->can_write )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_cache_dir"}->exists and not $self->{"_cache_dir"}->can_write) 0 0 0
Condition0%0/3
$self->{"_cache_dir"}->exists and not $self->{"_cache_dir"}->can_write
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_cache_dir"}->exists and not $self->{"_cache_dir"}->can_write
$self->{"_cache_dir"}->exists not $self->{"_cache_dir"}->can_write
Truth table0%0/3
A and not(B)
A= $self->{"_cache_dir"}->existsB= not $self->{"_cache_dir"}->can_write
ABresult
0X0
100
111
58 {
59 0 warn( "You do not have write permission to the mmap data directory $self->{_cache_dir}" );
60 }
61  
62 # A cache file will be provided either by the user, either by us with the open() method.
63 0 ▶ if( my $f = $self->cache_file )
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $self->cache_file) 0 0 0
64 {
65 # try-catch
66 0 local $@;
67 eval
68 0 {
69 # No serialiser because we manage ths ourself
70 my $cache = Cache::FastMmap->new(
71 ( ( defined( $self->{destroy} ) && length( $self->{destroy} ) ) ? ( unlink_on_exit => $self->{destroy} ) : () ),
72 share_file => "$f",
73 ( $self->{size} ? ( cache_size => $self->{size} ) : () ),
74 0 ▶ ( $self->{mode} ? ( permissions => $self->{mode} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $self->{'destroy'} && length $self->{'destroy'} ? : 0 0 0
$self->{'size'} ? : 0 0 0
$self->{'mode'} ? : 0 0 0
Condition0%0/3
defined $self->{"destroy"} and length $self->{"destroy"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"destroy"} and length $self->{"destroy"}
defined $self->{"destroy"} length $self->{"destroy"}
Truth table0%0/3
A and B
A= defined $self->{"destroy"}B= length $self->{"destroy"}
ABresult
0X0
100
111
75 serializer => '',
76 );
77 0 $self->{_cache} = $cache;
78 };
79 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
80 {
81 0 return( $self->error( "Error trying to instantiate a Cache::FastMmap object: $@" ) );
82 }
83 }
84 0 $self->{owner} = $$;
85 0 $self->{removed} = 0;
86 0 return( $self );
87 }
88  
89 # This class does not convert to an HASH
90 0 ▶sub as_hash { return( $_[0] ); }
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
91  
92 0 ▶sub base64 { return( shift->_set_get_scalar( 'base64', @_ ) ); }
Subroutine base64: not called CC=1 SCAR=6.9
Pod documented
93  
94 0 ▶sub cache_file { return( shift->_set_get_file( 'cache_file', @_ ) ); }
Subroutine cache_file: not called CC=1 SCAR=6.9
Pod documented
95  
96 0 ▶sub cbor { return( shift->_packing_method( 'cbor' ) ); }
Subroutine cbor: not called CC=1 SCAR=6.9
Pod documented
97  
98 0 ▶sub create { return( shift->_set_get_boolean( 'create', @_ ) ); }
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
99  
100 0 ▶sub destroy { return( shift->_set_get_boolean( 'destroy', @_ ) ); }
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
101  
102 sub exists
103 0 ▶{
Subroutine exists: not called CC=4 SCAR=30.0
Pod documented
104 0 my $self = shift( @_ );
105 0 ▶ my $cache = $self->_cache || return( $self->error( "No Cache::FastMmap object. It seems it is gone!" ) );
Condition0%0/2
$self->_cache || (return $self->error('No Cache::FastMmap object. It seems it is gone!'))
l!l
00
MC/DC0%0/1
$self->_cache || (return $self->error('No Cache::FastMmap object. It seems it is gone!'))
$self->_cache
Truth table0%0/2
A
A= $self->_cache
Aresult
11
00
106 0 ▶ my $file = $cache->{share_file} || return(0);
Condition0%0/2
$cache->{'share_file'} || (return 0)
l!l
00
MC/DC0%0/1
$cache->{'share_file'} || (return 0)
$cache->{'share_file'}
Truth table0%0/2
A
A= $cache->{'share_file'}
Aresult
11
00
107 0 ▶ return( -e( "$file" ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
-e "$file" ? : 0 0 0
108 }
109  
110 sub flags
111 0 ▶{
Subroutine flags: not called CC=5 SCAR=26.8
Pod documented
112 0 my $self = shift( @_ );
113 0 my $opts = $self->_get_args_as_hash( @_ );
114 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
116 0 ▶ $opts->{create} = $self->create unless( length( $opts->{create} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"create"} 0 0 0
117 0 ▶ $opts->{mode} = $self->mode unless( length( $opts->{mode} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"mode"} 0 0 0
118 0 my $flags = 0;
119 0 ▶ $flags |= 0600 if( $opts->{create} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"create"} 0 0 0
120 0 ▶ $flags |= ( $opts->{mode} || 0666 );
Condition0%0/2
$opts->{'mode'} || 438
l!l
00
MC/DC0%0/1
$opts->{'mode'} || 438
$opts->{'mode'}
Truth table0%0/2
A
A= $opts->{'mode'}
Aresult
11
00
121 0 return( $flags );
122 }
123  
124 sub ftok
125 0 ▶{
Subroutine ftok: not called CC=1 SCAR=6.9
Pod documented
126 0 my $self = shift( @_ );
127 0 my $id = shift( @_ );
128 0 my $f = sys_tmpdir();
129 return( int(
130 0 ( int( $f->inode ) & 0xffff ) |
131 int( ( $f->device ) << 16 ) |
132 ( ( $id & 0xff ) << 21 )
133 ) );
134 }
135  
136 0 ▶sub has_xs { return( shift->_is_class_loadable( 'Cache::FastMmap' ) ); }
Subroutine has_xs: not called CC=1 SCAR=6.9
Pod documented
137  
138 0 ▶sub id { return( shift->_set_get_scalar( 'id', @_ ) ); }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
139  
140 0 ▶sub json { return( shift->_packing_method( 'json' ) ); }
Subroutine json: not called CC=1 SCAR=6.9
Pod documented
141  
142 # sub key { return( shift->_set_get_scalar( 'key', @_ ) ); }
143 sub key
144 0 ▶{
Subroutine key: not called CC=2 SCAR=17.9
Pod documented
145 0 my $self = shift( @_ );
146 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
147 {
148 0 $self->{key} = shift( @_ );
149 0 $self->{serial} = $self->_str2key( $self->{key} );
150 }
151 0 return( $self->{key} );
152 }
153  
154 # NOTE: lock is a noop
155 0 ▶sub lock { return(1); }
Subroutine lock: not called CC=1 SCAR=6.9
Pod documented
156  
157 # NOTE: locked is a noop
158 0 ▶sub locked { return(1); }
Subroutine locked: not called CC=1 SCAR=6.9
Pod documented
159  
160 0 ▶sub mode { return( shift->_set_get_scalar( 'mode', @_ ) ); }
Subroutine mode: not called CC=1 SCAR=6.9
Pod documented
161  
162 sub open
163 0 ▶{
Subroutine open: not called CC=35 SCAR=70.7
Pod documented
164 0 my $self = shift( @_ );
165 0 my $opts = {};
166 0 ▶ if( ref( $_[0] ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
167 {
168 0 $opts = shift( @_ );
169 }
170 else
171 {
172 0 @$opts{ qw( key mode size ) } = @_;
173 }
174 0 ▶ $opts->{size} = $self->size unless( length( $opts->{size} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"size"} 0 0 0
175 0 $opts->{size} = int( $opts->{size} );
176 0 ▶ $opts->{mode} //= '';
Condition0%0/2
$opts->{"mode"} //= ""
l!l
00
MC/DC0%0/1
$opts->{"mode"} //= ""
$opts->{"mode"}
Truth table0%0/2
A
A= $opts->{"mode"}
Aresult
11
00
177 0 ▶ $opts->{key} = $opts->{key}->[0] if( ref( $opts->{key} // '' ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref($opts->{"key"} // "") eq "ARRAY" 0 0 0
Condition0%0/2
$opts->{"key"} // ""
l!l
00
MC/DC0%0/1
$opts->{"key"} // ""
$opts->{"key"}
Truth table0%0/2
A
A= $opts->{"key"}
Aresult
11
00
178 0 ▶ $opts->{key} //= '';
Condition0%0/2
$opts->{"key"} //= ""
l!l
00
MC/DC0%0/1
$opts->{"key"} //= ""
$opts->{"key"}
Truth table0%0/2
A
A= $opts->{"key"}
Aresult
11
00
179 2 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
180 0 my $serial;
181 0 ▶ if( length( $opts->{key} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $opts->{'key'}) { } 0 0 0
182 {
183 0 ▶ $serial = $self->_str2key( $opts->{key} ) ||
Condition0%0/2
$self->_str2key($opts->{'key'}) || (return $self->error("Cannot get serial from key '$opts->{'key'}': ", $self->error))
l!l
00
MC/DC0%0/1
$self->_str2key($opts->{'key'}) || (return $self->error("Cannot get serial from key '$opts->{'key'}': ", $self->error))
$self->_str2key($opts->{'key'})
Truth table0%0/2
A
A= $self->_str2key($opts->{'key'})
Aresult
11
00
184 return( $self->error( "Cannot get serial from key '$opts->{key}': ", $self->error ) );
185 }
186 else
187 {
188 0 $serial = $self->serial;
189 }
190 0 ▶ die( "There is no serial!!\n" ) if( !CORE::length( $serial ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $serial 0 0 0
191 0 my $create = 0;
192 0 ▶ if( $opts->{mode} eq 'w' || $opts->{key} =~ s/^>// )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'mode'} eq 'w' or $opts->{'key'} =~ s/^>//) { } 0 0 0
elsif ($opts->{'mode'} eq 'r' or $opts->{'key'} =~ s/^<//) { } 0 0 0
Condition0%0/3
$opts->{"mode"} eq "w" or $opts->{"key"} =~ s/^>//
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"mode"} eq "w" or $opts->{"key"} =~ s/^>//
$opts->{"mode"} eq "w" $opts->{"key"} =~ s/^>//
Truth table0%0/3
A or B
A= $opts->{"mode"} eq "w"B= $opts->{"key"} =~ s/^>//
ABresult
1X1
011
000
193 {
194 0 $create++;
195 }
196 ▶ elsif( $opts->{mode} eq 'r' || $opts->{key} =~ s/^<// )
Condition0%0/3
$opts->{"mode"} eq "r" or $opts->{"key"} =~ s/^<//
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"mode"} eq "r" or $opts->{"key"} =~ s/^<//
$opts->{"mode"} eq "r" $opts->{"key"} =~ s/^<//
Truth table0%0/3
A or B
A= $opts->{"mode"} eq "r"B= $opts->{"key"} =~ s/^<//
ABresult
1X1
011
000
197 {
198 0 $create = 0;
199 }
200 else
201 {
202 0 $create = $self->create;
203 }
204 0 ▶ my $flags = $self->flags( create => $create, ( $opts->{mode} =~ /^\d+$/ ? $opts->{mode} : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'mode'} =~ /^\d+$/ ? : 0 0 0
205  
206 0 ▶ my $cache_dir = $self->{_cache_dir} || return( $self->error( "Cache directory object is gone!" ) );
Condition0%0/2
$self->{'_cache_dir'} || (return $self->error('Cache directory object is gone!'))
l!l
00
MC/DC0%0/1
$self->{'_cache_dir'} || (return $self->error('Cache directory object is gone!'))
$self->{'_cache_dir'}
Truth table0%0/2
A
A= $self->{'_cache_dir'}
Aresult
11
00
207 0 ▶ return( $self->error( "Cache directory is not a Module::Generic::File object!" ) ) if( !$self->_is_a( $cache_dir => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($cache_dir, "Module::Generic::File") 0 0 0
208 0 ▶ if( $cache_dir->exists )
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_dir->exists) { } 0 0 0
209 {
210 0 ▶ return( $self->error( "Cache directory exists, but is not a directory!" ) ) if( !$cache_dir->is_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_dir->is_dir 0 0 0
211 }
212 else
213 {
214 0 ▶ $cache_dir->makepath || return( $self->pass_error( $cache_dir->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$cache_dir->makepath or return $self->pass_error($cache_dir->error) 0 0 0
215 }
216 0 my $fo = $cache_dir->child( $serial );
217 0 my $cache_file = "$fo";
218 0 my $io;
219 0 ▶ if( $fo->exists )
Branch: 1 branch
BranchTrueFalseTotal
if ($fo->exists) { } 0 0 0
220 {
221 # Need to find a way to make that more efficient
222 0 ▶ if( ( $flags & 0600 ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 384 or $flags & 48 or $flags & 6) { } 0 0 0
Condition0%0/6
$flags & 384 or $flags & 48 or $flags & 6
l!l&&r!l&&!r
000
$flags & 384 or $flags & 48
l!l&&r!l&&!r
000
MC/DC0%0/3
$flags & 384 or $flags & 48 or $flags & 6
$flags & 384 $flags & 48 $flags & 6
Truth table0%0/4
A or B or C
A= $flags & 384B= $flags & 48C= $flags & 6
ABCresult
1XX1
01X1
0011
0000
223 ( $flags & 0060 ) ||
224 ( $flags & 0006 ) )
225 {
226 0 ▶ return( $self->error( "Requested mode ($flags) requires writing, but uid $> is missing write privilege to the cache file \"$cache_file\"." ) ) if( !$fo->can_write );
Branch: 1 branch
BranchTrueFalseTotal
unless $fo->can_write 0 0 0
227 0 ▶ $io = $fo->open( '<', { binmode => 'raw', autoflush => 1 } ) ||
Condition0%0/2
$fo->open('<', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($fo->error))
l!l
00
MC/DC0%0/1
$fo->open('<', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($fo->error))
$fo->open('<', {'binmode', 'raw', 'autoflush', 1})
Truth table0%0/2
A
A= $fo->open('<', {'binmode', 'raw', 'autoflush', 1})
Aresult
11
00
228 return( $self->pass_error( $fo->error ) );
229 }
230 else
231 {
232 0 ▶ return( $self->error( "Requested mode ($flags) require reading, but missing access privilege to the cache file \"$cache_file\"." ) ) if( !$fo->can_read );
Branch: 1 branch
BranchTrueFalseTotal
unless $fo->can_read 0 0 0
233 0 ▶ $io = $fo->open( '<', { binmode => $opts->{binmode}, autoflush => 1 } ) ||
Condition0%0/2
$fo->open('<', {'binmode', $opts->{'binmode'}, 'autoflush', 1}) || (return $self->pass_error($fo->error))
l!l
00
MC/DC0%0/1
$fo->open('<', {'binmode', $opts->{'binmode'}, 'autoflush', 1}) || (return $self->pass_error($fo->error))
$fo->open('<', {'binmode', $opts->{'binmode'}, 'autoflush', 1})
Truth table0%0/2
A
A= $fo->open('<', {'binmode', $opts->{'binmode'}, 'autoflush', 1})
Aresult
11
00
234 return( $self->pass_error( $fo->error ) );
235 }
236 0 $io->close;
237 }
238 else
239 {
240 0 ▶ if( ( $flags & 0600 ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 384 or $flags & 48 or $flags & 6) { } 0 0 0
Condition0%0/6
$flags & 384 or $flags & 48 or $flags & 6
l!l&&r!l&&!r
000
$flags & 384 or $flags & 48
l!l&&r!l&&!r
000
MC/DC0%0/3
$flags & 384 or $flags & 48 or $flags & 6
$flags & 384 $flags & 48 $flags & 6
Truth table0%0/4
A or B or C
A= $flags & 384B= $flags & 48C= $flags & 6
ABCresult
1XX1
01X1
0011
0000
241 ( $flags & 0060 ) ||
242 ( $flags & 0006 ) )
243 {
244 0 ▶ $io = $fo->open( '+>', { binmode => 'raw', autoflush => 1 } ) ||
Condition0%0/2
$fo->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($fo->error))
l!l
00
MC/DC0%0/1
$fo->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($fo->error))
$fo->open('+>', {'binmode', 'raw', 'autoflush', 1})
Truth table0%0/2
A
A= $fo->open('+>', {'binmode', 'raw', 'autoflush', 1})
Aresult
11
00
245 return( $self->pass_error( $fo->error ) );
246 0 $fo->close;
247 }
248 else
249 {
250 0 return( $self->error( "Requested mode ($flags) require reading, but the cache file \"$cache_file\" does not exist yet." ) );
251 }
252 }
253  
254 0 $self->serial( $serial );
255 my $new = $self->new(
256 key => ( $opts->{key} || $self->key ),
257 debug => $self->debug,
258 mode => $flags,
259 destroy => $self->destroy,
260 cache_file => $fo,
261 _packing_method => $self->_packing_method,
262 0 ▶ ) || return( $self->error( "Cannot create object with key '", ( $opts->{key} || $self->key ), "': ", $self->error ) );
Condition0%0/8
$self->new('key', $opts->{'key'} || $self->key, 'debug', $self->debug, 'mode', $flags, 'destroy', $self->destroy, 'cache_file', $fo, '_packing_method', $self->_packing_method) || (return $self->error(q[Cannot create object with key '], $opts->{'key'} || $self->key, q[': ], $self->error))
l!l
00
$opts->{'key'} || $self->key
l!l&&r!l&&!r
000
$opts->{'key'} || $self->key
l!l&&r!l&&!r
000
MC/DC0%0/1
$self->new('key', $opts->{'key'} || $self->key, 'debug', $self->debug, 'mode', $flags, 'destroy', $self->destroy, 'cache_file', $fo, '_packing_method', $self->_packing_method) || (return $self->error(q[Cannot create object with key '], $opts->{'key'} || $self->key, q[': ], $self->error))
$self->new('key', $opts->{'key'} || $self->key, 'debug', $self->debug, 'mode', $flags, 'destroy', $self->destroy, 'cache_file', $fo, '_packing_method', $self->_packing_method)
MC/DC0%0/2
$opts->{'key'} || $self->key
$opts->{'key'} $self->key
MC/DC0%0/2
$opts->{'key'} || $self->key
$opts->{'key'} $self->key
Truth table0%0/2
A
A= $self->new('key', $opts->{'key'} || $self->key, 'debug', $self->debug, 'mode', $flags, 'destroy', $self->destroy, 'cache_file', $fo, '_packing_method', $self->_packing_method)
Aresult
11
00
Truth table0%0/3
A || B
A= $opts->{'key'}B= $self->key
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $opts->{'key'}B= $self->key
ABresult
1X1
011
000
263 0 $new->{base64} = $self->base64;
264 0 $new->key( $self->key );
265 0 $new->serial( $serial );
266 0 $new->id( Scalar::Util::refaddr( $new ) );
267 0 $new->size( $opts->{size} );
268 0 return( $new );
269 }
270  
271 0 ▶sub owner { return( shift->_set_get_scalar( 'owner', @_ ) ); }
Subroutine owner: not called CC=1 SCAR=6.9
Pod documented
272  
273 sub rand
274 0 ▶{
Subroutine rand: not called CC=1 SCAR=6.9
Pod documented
275 0 my $self = shift( @_ );
276 0 return( Data::UUID->new->create_str );
277 }
278  
279 # $self->read( $buffer, $size );
280 # $self->read( $buffer );
281 # my $data = $self->read;
282 sub read
283 0 ▶{
Subroutine read: not called CC=22 SCAR=62.3
Pod documented
284 0 my( $self, $buf ) = @_;
285 0 ▶ my $key = $self->key || return( $self->error( "No key set." ) );
Condition0%0/2
$self->key || (return $self->error('No key set.'))
l!l
00
MC/DC0%0/1
$self->key || (return $self->error('No key set.'))
$self->key
Truth table0%0/2
A
A= $self->key
Aresult
11
00
286 0 ▶ my $cache = $self->_cache || return( $self->error( "No Cache::FastMmap object. It seems it is gone!" ) );
Condition0%0/2
$self->_cache || (return $self->error('No Cache::FastMmap object. It seems it is gone!'))
l!l
00
MC/DC0%0/1
$self->_cache || (return $self->error('No Cache::FastMmap object. It seems it is gone!'))
$self->_cache
Truth table0%0/2
A
A= $self->_cache
Aresult
11
00
287 0 my $buffer;
288 # try-catch
289 0 local $@;
290 eval
291 0 {
292 0 $buffer = $cache->get( $key );
293 };
294 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
295 {
296 0 return( $self->error( "Error retrieving data in MMap file for key '$key': $@" ) );
297 }
298 0 ▶ my $bytes = CORE::length( $buffer // '' );
Condition0%0/2
$buffer // ""
l!l
00
MC/DC0%0/1
$buffer // ""
$buffer
Truth table0%0/2
A
A= $buffer
Aresult
11
00
299 0 my $packing = $self->_packing_method;
300 0 ▶ $packing = lc( $packing ) if( defined( $packing ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $packing 0 0 0
301 0 my $data;
302 0 ▶ if( CORE::defined( $buffer ) && CORE::length( $buffer ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer and length $buffer) { } 0 0 0
Condition0%0/3
defined $buffer and length $buffer
!ll&&!rl&&r
000
MC/DC0%0/2
defined $buffer and length $buffer
defined $buffer length $buffer
Truth table0%0/3
A and B
A= defined $bufferB= length $buffer
ABresult
0X0
100
111
303 {
304 # There may be encapsulation of data before writing data to memory.
305 # e.g.: MG[14]something here
306 0 ▶ if( index( $buffer, 'MG[' ) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($buffer, "MG[") == 0) 0 0 0
307 {
308 0 my $def = substr( $buffer, 0, index( $buffer, ']' ) + 1, '' );
309 # Get the string length stored
310 0 my $len = int( substr( $def, 3, -1 ) );
311 # Remove any possible remaining unwanted data
312 0 substr( $buffer, $len, length( $buffer ), '' );
313 }
314  
315 # try-catch
316 0 local $@;
317 eval
318 0 {
319 0 ▶ if( $packing eq 'json' )
Branch: 3 branches
BranchTrueFalseTotal
if ($packing eq 'json') { } 0 0 0
elsif ($packing eq 'cbor') { } 0 0 0
elsif ($packing eq 'sereal') { } 0 0 0
320 {
321 0 $data = $self->_decode_json( $buffer );
322 }
323 elsif( $packing eq 'cbor' )
324 {
325 $data = $self->deserialise(
326 data => $buffer,
327 serialiser => 'CBOR::XS',
328 allow_sharing => 1,
329 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
330 );
331 }
332 elsif( $packing eq 'sereal' )
333 {
334 $data = $self->deserialise(
335 data => $buffer,
336 serialiser => 'Sereal',
337 freeze_callbacks => 1,
338 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
339 );
340 }
341 # By default Storable::Improved
342 else
343 {
344 # $data = Storable::Improved::thaw( $buffer );
345 $data = $self->deserialise(
346 data => $buffer,
347 serialiser => 'Storable::Improved',
348 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
349 );
350 }
351 };
352 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
353 {
354 0 ▶ return( $self->error( "An error occured while decoding data using $packing with base64 set to '", ( $self->{base64} // '' ), "': $@" ) );
Condition0%0/2
$self->{"base64"} // ""
l!l
00
MC/DC0%0/1
$self->{"base64"} // ""
$self->{"base64"}
Truth table0%0/2
A
A= $self->{"base64"}
Aresult
11
00
355 }
356 }
357 else
358 {
359 0 $data = $buffer;
360 }
361  
362 # data decoded is not a reference and size was provided and is greater than 0
363 0 ▶ if( !ref( $data ) && scalar( @_ ) > 2 && int( $_[2] ) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $data and scalar @_ > 2 and int $_[2] > 0) 0 0 0
Condition0%0/6
not ref $data and scalar @_ > 2 and int $_[2] > 0
!ll&&!rl&&r
000
not ref $data and scalar @_ > 2
!ll&&!rl&&r
000
MC/DC0%0/3
not ref $data and scalar @_ > 2 and int $_[2] > 0
not ref $data scalar @_ > 2 int $_[2] > 0
Truth table0%0/4
not(A) and B and C
A= not ref $dataB= scalar @_ > 2C= int $_[2] > 0
ABCresult
0XX0
10X0
1100
1111
364 {
365 0 $data = substr( $data, 0, $_[2] );
366 }
367  
368 0 ▶ if( scalar( @_ ) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 0 0 0
369 {
370 0 $_[1] = $data;
371 0 ▶ return( CORE::length( $_[1] ) || "0E0" );
Condition0%0/2
length $_[1] || '0E0'
l!l
00
MC/DC0%0/1
length $_[1] || '0E0'
length $_[1]
Truth table0%0/2
A
A= length $_[1]
Aresult
11
00
372 }
373 else
374 {
375 0 return( $data );
376 }
377 }
378  
379 sub remove
380 0 ▶{
Subroutine remove: not called CC=4 SCAR=30.0
Pod documented
381 0 my $self = shift( @_ );
382 0 ▶ my $cache = $self->_cache || return( $self->error( "No Cache::FastMmap object. It seems it is gone!" ) );
Condition0%0/2
$self->_cache || (return $self->error('No Cache::FastMmap object. It seems it is gone!'))
l!l
00
MC/DC0%0/1
$self->_cache || (return $self->error('No Cache::FastMmap object. It seems it is gone!'))
$self->_cache
Truth table0%0/2
A
A= $self->_cache
Aresult
11
00
383 0 $cache->cleanup( @_ );
384 0 undef( $cache );
385 0 $self->_cache( undef );
386 0 ▶ if( my $f = $self->cache_file )
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $self->cache_file) 0 0 0
387 {
388 0 ▶ $f->unlink if( $f->exists );
Branch: 1 branch
BranchTrueFalseTotal
if $f->exists 0 0 0
389 }
390 0 $self->removed(1);
391 }
392  
393 0 ▶sub removed { return( shift->_set_get_boolean( 'removed', @_ ) ); }
Subroutine removed: not called CC=1 SCAR=6.9
Pod documented
394  
395 sub reset
396 0 ▶{
Subroutine reset: not called CC=3 SCAR=24.8
Pod documented
397 0 my $self = shift( @_ );
398 0 my $default;
399 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
400 {
401 0 $default = shift( @_ );
402 }
403 else
404 {
405 0 $default = '';
406 }
407 0 ▶ if( my $cache = $self->_cache )
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache = $self->_cache) 0 0 0
408 {
409 # my $key = $self->key || return( $self->error( "Could not find the mmap key!" ) );
410 # $cache->set( $key => $default );
411 0 $self->write( $default );
412 }
413 0 return( $self );
414 }
415  
416 0 ▶sub sereal { return( shift->_packing_method( 'sereal' ) ); }
Subroutine sereal: not called CC=1 SCAR=6.9
Pod documented
417  
418 0 ▶sub serial { return( shift->_set_get_scalar( 'serial', @_ ) ); }
Subroutine serial: not called CC=1 SCAR=6.9
Pod documented
419  
420 0 ▶sub serialiser { return( shift->_set_get_scalar( '_packing_method', @_ ) ); }
Subroutine serialiser: not called CC=1 SCAR=6.9
Pod documented
421  
422 {
423 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
424 2 *serializer = \&serialiser;
425 }
426  
427 0 ▶sub size { return( shift->_set_get_scalar( 'size', @_ ) ); }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
428  
429 sub stat
430 0 ▶{
Subroutine stat: not called CC=3 SCAR=24.8
Pod documented
431 0 my $self = shift( @_ );
432 0 ▶ my $file = $self->cache_file || return(0);
Condition0%0/2
$self->cache_file || (return 0)
l!l
00
MC/DC0%0/1
$self->cache_file || (return 0)
$self->cache_file
Truth table0%0/2
A
A= $self->cache_file
Aresult
11
00
433 0 ▶ return( $self->error( "Mmap cache file found in our object is not a Module::Generic::File object." ) ) if( !$self->_is_a( $file => 'Module::Generic::File' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($file, "Module::Generic::File") 0 0 0
434 0 return( $file->stat );
435 }
436  
437 0 ▶sub storable { return( shift->_packing_method( 'storable' ) ); }
Subroutine storable: not called CC=1 SCAR=6.9
Pod documented
438  
439 # NOTE: unlock is a noop
440 0 ▶sub unlock { return(1); }
Subroutine unlock: not called CC=1 SCAR=6.9
Pod documented
441  
442 sub write
443 0 ▶{
Subroutine write: not called CC=22 SCAR=62.3
Pod documented
444 0 my $self = shift( @_ );
445 0 my $data;
446 0 ▶ if( scalar( @_ ) == 1 && ref( $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0]) { } 0 0 0
Condition0%0/3
scalar @_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and ref $_[0]
scalar @_ == 1 ref $_[0]
Truth table0%0/3
A and B
A= scalar @_ == 1B= ref $_[0]
ABresult
0X0
100
111
447 {
448 0 $data = shift( @_ );
449 }
450 else
451 {
452 0 $data = \join( '', @_ );
453 }
454 0 ▶ my $key = $self->key || return( $self->error( "No key set." ) );
Condition0%0/2
$self->key || (return $self->error('No key set.'))
l!l
00
MC/DC0%0/1
$self->key || (return $self->error('No key set.'))
$self->key
Truth table0%0/2
A
A= $self->key
Aresult
11
00
455 0 ▶ my $cache = $self->_cache || return( $self->error( "No Cache::FastMmap object. It seems it is gone!" ) );
Condition0%0/2
$self->_cache || (return $self->error('No Cache::FastMmap object. It seems it is gone!'))
l!l
00
MC/DC0%0/1
$self->_cache || (return $self->error('No Cache::FastMmap object. It seems it is gone!'))
$self->_cache
Truth table0%0/2
A
A= $self->_cache
Aresult
11
00
456 0 my $packing = $self->_packing_method;
457 0 my $encoded;
458 0 ▶ if( $packing eq 'json' )
Branch: 3 branches
BranchTrueFalseTotal
if ($packing eq 'json') { } 0 0 0
elsif ($packing eq 'cbor') { } 0 0 0
elsif ($packing eq 'sereal') { } 0 0 0
459 {
460 # try-catch
461 0 local $@;
462 eval
463 0 {
464 0 $encoded = $self->_encode_json( $data );
465 };
466 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
467 {
468 0 return( $self->error( "An error occured encoding data provided using $packing: $@. Data was: '$data'" ) );
469 }
470 }
471 elsif( $packing eq 'cbor' )
472 {
473 # try-catch
474 0 local $@;
475 eval
476 0 {
477 $encoded = $self->serialise( $data,
478 serialiser => 'CBOR::XS',
479 allow_sharing => 1,
480 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
481 );
482 };
483 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
484 {
485 0 return( $self->error( "An error occured encoding data provided using $packing: $@. Data was: '$data'" ) );
486 }
487 0 ▶ return( $self->pass_error ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
488 }
489 elsif( $packing eq 'sereal' )
490 {
491 0 ▶ $self->_load_class( 'Sereal::Encoder' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Sereal::Encoder") or return $self->pass_error 0 0 0
492 0 my $const;
493 0 ▶ $const = \&{"Sereal\::Encoder::SRL_ZLIB"} if( defined( &{"Sereal\::Encoder::SRL_ZLIB"} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"Sereal::Encoder::SRL_ZLIB";} 0 0 0
494 # try-catch
495 0 local $@;
496 eval
497 0 {
498 $encoded = $self->serialise( $data,
499 serialiser => 'Sereal',
500 freeze_callbacks => 1,
501 ( defined( $const ) ? ( compress => $const->() ) : () ),
502 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $const ? : 0 0 0
defined $self->{'base64'} ? : 0 0 0
503 );
504 };
505 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
506 {
507 0 return( $self->error( "An error occured encoding data provided using $packing: $@. Data was: '$data'" ) );
508 }
509 0 ▶ return( $self->pass_error ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
510 }
511 # Default to Storable::Improved
512 else
513 {
514 # try-catch
515 0 local $@;
516 # local $Storable::forgive_me = 1;
517 # $encoded = Storable::Improved::freeze( $data );
518 eval
519 0 {
520 $encoded = $self->serialise( $data,
521 serialiser => 'Storable::Improved',
522 0 ▶ ( defined( $self->{base64} ) ? ( base64 => $self->{base64} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'base64'} ? : 0 0 0
523 );
524 };
525 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
526 {
527 0 return( $self->error( "An error occured encoding data provided using $packing: $@. Data was: '$data'" ) );
528 }
529 0 ▶ return( $self->pass_error ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
530 }
531  
532 # Simple encapsulation
533 # FYI: MG = Module::Generic
534 0 substr( $encoded, 0, 0, 'MG[' . length( $encoded ) . ']' );
535  
536 # try-catch
537 0 local $@;
538 eval
539 0 {
540 0 $cache->set( $key => $encoded );
541 };
542 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
543 {
544 0 return( $self->error( "Error writing data to MMap file for key '$key': $@" ) );
545 }
546 0 return( $self );
547 }
548  
549 0 ▶sub _cache { return( shift->_set_get_object_without_init( '_cache', 'Cache::FastMmap', @_ ) ); }
Subroutine _cache: not called CC=1 SCAR=6.9
550  
551 sub _decode_json
552 0 ▶{
Subroutine _decode_json: not called CC=4 SCAR=28.7
553 0 my $self = shift( @_ );
554 0 my $data = shift( @_ );
555 # Nothing to do
556 0 ▶ return( $data ) if( !defined( $data ) || !CORE::length( $data ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and length $data 0 0 0
Condition0%0/3
defined $data and length $data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table0%0/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
557 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
558 0 my $j = JSON->new->utf8->relaxed->allow_nonref;
559 0 my $seen = {};
560 0 my $crawl;
561 $crawl = sub
562 0 ▶ {
Subroutine __ANON__: not called CC=11 SCAR=48.8
563 0 my $this = shift( @_ );
564 0 my $type = Scalar::Util::reftype( $this );
565 0 ▶ return( $this ) if( ( $type eq 'HASH' || $type eq 'ARRAY' ) && ++$seen->{ Scalar::Util::refaddr( $this ) } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1 0 0 0
Condition0%0/6
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
!ll&&!rl&&r
000
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
$type eq "HASH" $type eq "ARRAY" ++$$seen{builtin::refaddr($this)} > 1
Truth table0%0/5
A or B and C
A= $type eq "HASH"B= $type eq "ARRAY"C= ++$$seen{builtin::refaddr($this)} > 1
ABCresult
00X0
1X00
0100
1X11
0111
566 0 ▶ if( $type eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
567 {
568 # Found a former scalar reference, restore it
569 0 ▶ if( CORE::exists( $this->{__scalar_gen_shm} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"__scalar_gen_shm"}) 0 0 0
570 {
571 0 return( \$this->{__scalar_gen_shm} );
572 }
573  
574 0 foreach my $k ( keys( %$this ) )
575 {
576 0 ▶ next if( !ref( $this->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->{$k} 0 0 0
577 0 $this->{ $k } = $crawl->( $this->{ $k } );
578 }
579 }
580 elsif( $type eq 'ARRAY' )
581 {
582 0 ▶ for( my $i = 0; $i < scalar( @$this ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$this) 0 0 0
583 {
584 0 ▶ next if( !ref( $this->[$i] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->[$i] 0 0 0
585 0 $this->[$i] = $crawl->( $this->[$i] );
586 }
587 }
588 0 return( $this );
589 0 };
590  
591 0 my $decoded;
592 # try-catch
593 0 local $@;
594 eval
595 0 {
596 0 $decoded = $j->decode( $data );
597 };
598 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
599 {
600 0 return( $self->error( "An error occurred while trying to decode JSON data: $@" ) );
601 }
602 0 my $result = $crawl->( $decoded );
603 0 return( $result );
604 }
605  
606 # Purpose of this method is to recursively check the given data and change scalar reference if they are anything else than 1 or 0, otherwise JSON would complain
607 sub _encode_json
608 0 ▶{
Subroutine _encode_json: not called CC=2 SCAR=17.0
609 0 my $self = shift( @_ );
610 0 my $data = shift( @_ );
611 0 my $seen = {};
612 0 my $crawl;
613 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
614 $crawl = sub
615 0 ▶ {
Subroutine __ANON__: not called CC=15 SCAR=54.8
616 0 my $this = shift( @_ );
617 0 my $type = Scalar::Util::reftype( $this );
618 # Skip this reference if it is either hash or array and we have already seen it in order to avoid looping.
619 0 ▶ return( $this ) if( ( $type eq 'HASH' || $type eq 'ARRAY' ) && ++$seen->{ Scalar::Util::refaddr( $this ) } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1 0 0 0
Condition0%0/6
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
!ll&&!rl&&r
000
$type eq "HASH" or $type eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "HASH" || $type eq "ARRAY" and ++$$seen{builtin::refaddr($this)} > 1
$type eq "HASH" $type eq "ARRAY" ++$$seen{builtin::refaddr($this)} > 1
Truth table0%0/5
A or B and C
A= $type eq "HASH"B= $type eq "ARRAY"C= ++$$seen{builtin::refaddr($this)} > 1
ABCresult
00X0
1X00
0100
1X11
0111
620 0 ▶ if( $type eq 'HASH' )
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'SCALAR') { } 0 0 0
621 {
622 0 foreach my $k ( keys( %$this ) )
623 {
624 0 ▶ next if( !ref( $this->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->{$k} 0 0 0
625 0 $this->{ $k } = $crawl->( $this->{ $k } );
626 }
627 }
628 elsif( $type eq 'ARRAY' )
629 {
630 0 ▶ for( my $i = 0; $i < scalar( @$this ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$this) 0 0 0
631 {
632 0 ▶ next if( !ref( $this->[$i] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->[$i] 0 0 0
633 0 $this->[$i] = $crawl->( $this->[$i] );
634 }
635 }
636 elsif( $type eq 'SCALAR' )
637 {
638 # The only supported value by JSON for a scalar reference
639 0 ▶ return( $this ) if( $$this eq "1" or $$this eq "0" );
Branch: 1 branch
BranchTrueFalseTotal
if $$this eq 1 or $$this eq 0 0 0 0
Condition0%0/3
$$this eq 1 or $$this eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$$this eq 1 or $$this eq 0
$$this eq 1 $$this eq 0
Truth table0%0/3
A or B
A= $$this eq 1B= $$this eq 0
ABresult
1X1
011
000
640 0 my $pkg;
641 0 ▶ if( ( $pkg = Scalar::Util::blessed( $this ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg = builtin::blessed($this)) { } 0 0 0
642 {
643 0 ▶ if( overload::Method( $this => '""' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (overload::Method($this, '""')) { } 0 0 0
644 {
645 0 $this = { __scalar_gen_shm => "$this", __package => $pkg };
646 }
647 else
648 {
649 0 $this = { __scalar_gen_shm => $$this, __package => $pkg };
650 }
651 }
652 else
653 {
654 0 $this = { __scalar_gen_shm => $$this };
655 }
656 }
657 0 return( $this );
658 0 };
659 0 my $ref = $crawl->( $data );
660 0 my $j = JSON->new->utf8->relaxed->allow_nonref->convert_blessed;
661  
662 0 my $encoded;
663 # try-catch
664 0 local $@;
665 eval
666 0 {
667 0 $encoded = $j->encode( $ref );
668 };
669 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
670 {
671 0 return( $self->error( "An error occurred while trying to JSON encode data: $@" ) );
672 }
673 0 return( $encoded );
674 }
675  
676 0 ▶sub _packing_method { return( shift->_set_get_scalar( '_packing_method', @_ ) ); }
Subroutine _packing_method: not called CC=1 SCAR=6.9
677  
678 sub _str2key
679 0 ▶{
Subroutine _str2key: not called CC=8 SCAR=40.0
680 0 my $self = shift( @_ );
681 0 my $key = shift( @_ );
682 2 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
683 0 ▶ if( !defined( $key ) || $key eq '' )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key or $key eq "") 0 0 0
Condition0%0/3
not defined $key or $key eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $key or $key eq ""
not defined $key $key eq ""
Truth table0%0/3
not(A) or B
A= not defined $keyB= $key eq ""
ABresult
1X1
011
000
684 {
685 0 return( Data::UUID->new->create_str );
686 }
687 # We do not actually use any path, but this is for standardisation with Module::Generic::SharedMem
688 0 my $path;
689 0 ▶ ( $key, $path ) = ref( $key ) eq 'ARRAY' ? @$key : ( $key, [getpwuid($>)]->[7] );
Branch: 1 branch
BranchTrueFalseTotal
ref $key eq 'ARRAY' ? : 0 0 0
690 0 ▶ $path = [getpwuid($path)]->[7] if( $path =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /^\d+$/ 0 0 0
691 0 ▶ if( $key =~ /^\d+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\d+$/) { } 0 0 0
692 {
693 0 ▶ my $id = $self->ftok( $key ) ||
Condition0%0/2
$self->ftok($key) || (return $self->error("Unable to get a key using ftok: $!"))
l!l
00
MC/DC0%0/1
$self->ftok($key) || (return $self->error("Unable to get a key using ftok: $!"))
$self->ftok($key)
Truth table0%0/2
A
A= $self->ftok($key)
Aresult
11
00
694 return( $self->error( "Unable to get a key using ftok: $!" ) );
695 0 return( $id );
696 }
697 else
698 {
699 0 my $id = 0;
700 0 $id += $_ for( unpack( "C*", $key ) );
701 0 my $val = $self->ftok( $id );
702 0 return( $val );
703 }
704 }
705  
706 sub FREEZE
707 0 ▶{
Subroutine FREEZE: not called CC=7 SCAR=40.3
Pod undocumented
708 0 my $self = CORE::shift( @_ );
709 0 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition0%0/2
shift @_ // ""
l!l
00
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
710 0 my $class = CORE::ref( $self );
711 0 my @props = qw( base64 create destroy key mode serial size _packing_method );
712 0 my $hash = {};
713 0 foreach my $prop ( @props )
714 {
715 0 ▶ if( exists( $self->{ $prop } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$prop}) 0 0 0
716 {
717 0 $hash->{ $prop } = $self->{ $prop };
718 }
719 }
720 0 ▶ if( my $cache = $self->{_cache} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache = $self->{"_cache"}) 0 0 0
721 {
722 0 my $file = $self->cache_file;
723 0 my $cache_data = $cache->get( $self->key );
724 0 $hash->{__cache_file} = "$file";
725 0 $hash->{__cache_data} = $cache_data;
726 }
727 # Return an array reference rather than a list so this works with Sereal and CBOR
728 # On or before Sereal version 4.023, Sereal did not support multiple values returned
729 0 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 0 0 0
730 {
731 0 require Sereal::Encoder;
732 0 require version;
733
734 0 ▶ if( version->parse( Sereal::Encoder->VERSION ) <= version->parse( '4.023' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse("Sereal::Encoder"->VERSION) <= "version"->parse("4.023")) 0 0 0
735 {
736 0 CORE::return( [$class, $hash] );
737 }
738 }
739 # But Storable want a list with the first element being the serialised element
740 0 CORE::return( $class, $hash );
741 }
742  
743 0 ▶sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
744  
745 0 ▶sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
746  
747 # NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
748 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
749 sub THAW
750 0 ▶{
Subroutine THAW: not called CC=22 SCAR=62.3
Pod undocumented
751 0 my( $self, undef, @args ) = @_;
752 0 ▶ my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 && ref $args[0] eq 'ARRAY' ? : 0 0 0
Condition0%0/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
753 0 ▶ my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && ref $ref eq 'ARRAY' && scalar @$ref > 1 ? : 0 0 0
Condition0%0/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
000
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
000
ref $self or $self
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
defined $ref ref $ref eq "ARRAY" scalar @$ref > 1
MC/DC0%0/2
ref $self or $self
ref $self $self
Truth table0%0/4
A and B and C
A= defined $refB= ref $ref eq "ARRAY"C= scalar @$ref > 1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
754 0 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
755 0 my $new;
756 0 my( $cache_data, $cache_file );
757 0 ▶ if( CORE::exists( $hash->{__cache_data} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"__cache_data"} and exists $hash->{"__cache_file"}) 0 0 0
Condition0%0/3
exists $hash->{"__cache_data"} and exists $hash->{"__cache_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $hash->{"__cache_data"} and exists $hash->{"__cache_file"}
exists $hash->{"__cache_data"} exists $hash->{"__cache_file"}
Truth table0%0/3
A and B
A= exists $hash->{"__cache_data"}B= exists $hash->{"__cache_file"}
ABresult
0X0
100
111
758 CORE::exists( $hash->{__cache_file} ) )
759 {
760 0 ( $cache_data, $cache_file ) = CORE::delete( @$hash{qw( __cache_data __cache_file )} );
761 }
762  
763 # Storable pattern requires to modify the object it created rather than returning a new one
764 0 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
765 {
766 0 foreach( CORE::keys( %$hash ) )
767 {
768 0 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
769 }
770 0 $new = $self;
771 }
772 else
773 {
774 0 $new = CORE::bless( $hash => $class );
775 }
776 0 ▶ if( CORE::defined( $cache_data ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache_data and defined $cache_file) 0 0 0
Condition0%0/3
defined $cache_data and defined $cache_file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cache_data and defined $cache_file
defined $cache_data defined $cache_file
Truth table0%0/3
A and B
A= defined $cache_dataB= defined $cache_file
ABresult
0X0
100
111
777 CORE::defined( $cache_file ) )
778 {
779 my $f = Module::Generic::File->new( $cache_file ) || do
780 0 ▶ {
Condition0%0/3
'Module::Generic::File'->new($cache_file) || do { warn qq[Unable to get a file object for "$cache_file": ], 'Module::Generic::File'->error, "\n" if $self->_warnings_is_enabled('Module::Generic'); return $new }
l!l&&r!l&&!r
000
MC/DC0%0/2
'Module::Generic::File'->new($cache_file) || do { warn qq[Unable to get a file object for "$cache_file": ], 'Module::Generic::File'->error, "\n" if $self->_warnings_is_enabled('Module::Generic'); return $new }
'Module::Generic::File'->new($cache_file) do { warn qq[Unable to get a file object for "$cache_file": ], 'Module::Generic::File'->error, "\n" if $self->_warnings_is_enabled('Module::Generic'); return $new }
Truth table0%0/3
A || B
A= 'Module::Generic::File'->new($cache_file)B= do { warn qq[Unable to get a file object for "$cache_file": ], 'Module::Generic::File'->error, "\n" if $self->_warnings_is_enabled('Module::Generic'); return $new }
ABresult
1X1
011
000
781 0 ▶ warn( "Unable to get a file object for \"$cache_file\": ", Module::Generic::File->error, "\n" ) if( $self->_warnings_is_enabled( 'Module::Generic' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled("Module::Generic") 0 0 0
782 0 return( $new );
783 };
784 0 $new->{cache_file} = $f;
785 $new->{_cache} = Cache::FastMmap->new(
786 ( ( defined( $new->{destroy} ) && length( $new->{destroy} ) ) ? ( unlink_on_exit => $new->{destroy} ) : () ),
787 share_file => "$f",
788 ( $new->{size} ? ( cache_size => $new->{size} ) : () ),
789 ( $new->{mode} ? ( permissions => $new->{mode} ) : () ),
790 serializer => '',
791 ) || do
792 0 ▶ {
Branch: 3 branches
BranchTrueFalseTotal
defined $new->{'destroy'} && length $new->{'destroy'} ? : 0 0 0
$new->{'size'} ? : 0 0 0
$new->{'mode'} ? : 0 0 0
Condition0%0/6
'Cache::FastMmap'->new(defined $new->{'destroy'} && length $new->{'destroy'} ? ('unlink_on_exit', $new->{'destroy'}) : (), 'share_file', "$f", $new->{'size'} ? ('cache_size', $new->{'size'}) : (), $new->{'mode'} ? ('permissions', $new->{'mode'}) : (), 'serializer', '') || do { warn "Unable to instantiate a Cache::FastMmap object.\n" if $self->_warnings_is_enabled('Module::Generic'); return $new }
l!l&&r!l&&!r
000
defined $new->{"destroy"} and length $new->{"destroy"}
!ll&&!rl&&r
000
MC/DC0%0/2
'Cache::FastMmap'->new(defined $new->{'destroy'} && length $new->{'destroy'} ? ('unlink_on_exit', $new->{'destroy'}) : (), 'share_file', "$f", $new->{'size'} ? ('cache_size', $new->{'size'}) : (), $new->{'mode'} ? ('permissions', $new->{'mode'}) : (), 'serializer', '') || do { warn "Unable to instantiate a Cache::FastMmap object.\n" if $self->_warnings_is_enabled('Module::Generic'); return $new }
'Cache::FastMmap'->new(defined $new->{'destroy'} && length $new->{'destroy'} ? ('unlink_on_exit', $new->{'destroy'}) : (), 'share_file', "$f", $new->{'size'} ? ('cache_size', $new->{'size'}) : (), $new->{'mode'} ? ('permissions', $new->{'mode'}) : (), 'serializer', '') do { warn "Unable to instantiate a Cache::FastMmap object.\n" if $self->_warnings_is_enabled('Module::Generic'); return $new }
MC/DC0%0/2
defined $new->{"destroy"} and length $new->{"destroy"}
defined $new->{"destroy"} length $new->{"destroy"}
Truth table0%0/3
A || B
A= 'Cache::FastMmap'->new(defined $new->{'destroy'} && length $new->{'destroy'} ? ('unlink_on_exit', $new->{'destroy'}) : (), 'share_file', "$f", $new->{'size'} ? ('cache_size', $new->{'size'}) : (), $new->{'mode'} ? ('permissions', $new->{'mode'}) : (), 'serializer', '')B= do { warn "Unable to instantiate a Cache::FastMmap object.\n" if $self->_warnings_is_enabled('Module::Generic'); return $new }
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= defined $new->{"destroy"}B= length $new->{"destroy"}
ABresult
0X0
100
111
793 0 ▶ warn( "Unable to instantiate a Cache::FastMmap object.\n" ) if( $self->_warnings_is_enabled( 'Module::Generic' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled("Module::Generic") 0 0 0
794 0 return( $new );
795 };
796 0 $new->{_cache}->set( $new->key => $cache_data );
797 }
798 0 $new->{owner} = $$;
799 0 CORE::return( $new );
800 }
801  
802 2 1;
803 # NOTE: POD
804 __END__
« lib/Module/Generic/File.pm ↑ Summary lib/Module/Generic/File/Cache.pm »