blib/lib/App/MatrixTool/ServerIdStore.pm

filter: Statementstmt 27.4% 20 / 73 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 42.8% 6 / 14 Podpod 75.0% 3 / 4 totaltotal 22.3% 29 / 130 SCAR 52.6
CC
18
Cov
19%
_open_file
42.0
_read_file
42.0
put
12.0
CRAP 192.2
« blib/lib/App/MatrixTool/HTTPClient.pm ↑ Summary blib/lib/App/MatrixTool/Command/notary.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2015 -- leonerd@leonerd.org.uk
5  
6 package App::MatrixTool::ServerIdStore;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.06';
12  
13 1 ▶use Errno qw( ENOENT );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Path qw( make_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use MIME::Base64 qw( encode_base64 decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 C<App::MatrixTool::ServerIdStore> - storage keyed by server name and an ID
21  
22 =head1 DESCRIPTION
23  
24 Provides a simple flat-file database that stores data keyed by a remote server
25 name and ID field. This is persisted in a human-readable file.
26  
27 =cut
28  
29 sub new
30 {
31 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
32 0 my %args = @_;
33  
34 return bless {
35 path => $args{path},
36 data => {},
37 0 ▶ encode => $args{encode} // "base64",
Condition0%0/2
$args{"encode"} // "base64"
l!l
00
MC/DC0%0/1
$args{"encode"} // "base64"
$args{"encode"}
Truth table0%0/2
A
A= $args{"encode"}
Aresult
11
00
38 }, $class;
39 }
40  
41 =head1 METHODS
42  
43 =cut
44  
45 sub _open_file
46 {
47 0 ▶ my $self = shift;
Subroutine _open_file: not called CC=6 SCAR=37.4
48 0 my ( $mode ) = @_;
49  
50 0 my $path = $self->{path};
51  
52 0 ▶ if( $mode eq ">>" and not -f $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq ">>" and not -f $path) 0 0 0
Condition0%0/3
$mode eq ">>" and not -f $path
!ll&&!rl&&r
000
MC/DC0%0/2
$mode eq ">>" and not -f $path
$mode eq ">>" not -f $path
Truth table0%0/3
A and not(B)
A= $mode eq ">>"B= not -f $path
ABresult
0X0
100
111
53 0 make_path( dirname( $path ) );
54 }
55  
56 0 ▶ if( open my $fh, $mode, $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, $mode, $path) 0 0 0
57 0 return $fh;
58 }
59  
60 0 ▶ return undef if $! == ENOENT and $mode eq "<";
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 and $mode eq "<" 0 0 0
Condition0%0/3
$! == 2 and $mode eq "<"
!ll&&!rl&&r
000
MC/DC0%0/2
$! == 2 and $mode eq "<"
$! == 2 $mode eq "<"
Truth table0%0/3
A and B
A= $! == 2B= $mode eq "<"
ABresult
0X0
100
111
61 0 die "Cannot open $path - $!\n";
62 }
63  
64 sub _read_file
65 {
66 0 ▶ my $self = shift;
Subroutine _read_file: not called CC=6 SCAR=37.4
67 0 ▶ return if $self->{have_read};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"have_read"} 0 0 0
68  
69 0 ▶ if( my $fh = $self->_open_file( "<" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->_open_file("<")) 0 0 0
70 0 ▶ while( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
71 0 ▶ m/^\s*#/ and next; # ignore comment lines
Branch: 1 branch
BranchTrueFalseTotal
/^\s*#/ and next 0 0 0
72 0 my ( $server, $id, $key ) = split m/\s+/, $_;
73  
74 0 ▶ defined $key or warn( "Unable to parse line $_" ), next;
Branch: 1 branch
BranchTrueFalseTotal
defined $key or warn("Unable to parse line $_"), next 0 0 0
75  
76 0 $self->{data}{$server}{$id} = $self->_decode( $key );
77 }
78 }
79  
80 0 $self->{have_read}++;
81 }
82  
83 sub _encode
84 {
85 0 ▶ my $self = shift;
Subroutine _encode: not called CC=2 SCAR=17.9
86 0 ▶ return encode_base64( $_[0] ) if $self->{encode} eq "base64";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encode"} eq "base64" 0 0 0
87 0 return $_[0];
88 }
89  
90 sub _decode
91 {
92 0 ▶ my $self = shift;
Subroutine _decode: not called CC=2 SCAR=17.9
93 0 ▶ return decode_base64( $_[0] ) if $self->{encode} eq "base64";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encode"} eq "base64" 0 0 0
94 0 return $_[0];
95 }
96  
97 =head2 list
98  
99 %id_data = $store->list( server => $name )
100  
101 Returns a kvlist associating IDs to byte strings of data stored for the given
102 server.
103  
104 =cut
105  
106 sub list
107 {
108 0 ▶ my $self = shift;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
109 0 my %args = @_;
110  
111 0 my $server = $args{server};
112 0 $self->_read_file;
113  
114 0 my %ret;
115 0 foreach my $id ( keys %{ $self->{data}{$server} } ) {
116 0 $ret{$id} = $self->{data}{$server}{$id};
117 }
118  
119 0 return %ret;
120 }
121  
122 =head2 get
123  
124 $key = $store->get( server => $name, id => $id )
125  
126 Returns a byte string associated with the given server and ID, or C<undef> if
127 no such is known.
128  
129 =cut
130  
131 sub get
132 {
133 0 ▶ my $self = shift;
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
134 0 my %args = @_;
135  
136 0 my $server = $args{server};
137 0 my $id = $args{id};
138 0 $self->_read_file;
139  
140 0 ▶ return unless $self->{data}{$server};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"}{$server} 0 0 0
141 0 return $self->{data}{$server}{$id};
142 }
143  
144 =head2 put
145  
146 $store->put( server => $name, id => $id, data => $bytes )
147  
148 Stores a byte string associated with the server and ID.
149  
150 =cut
151  
152 sub put
153 {
154 0 ▶ my $self = shift;
Subroutine put: not called CC=3 SCAR=24.8
Pod documented
155 0 my %args = @_;
156  
157 0 my $server = $args{server};
158 0 my $id = $args{id};
159  
160 0 ▶ if( exists $self->{data}{$server}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"data"}{$server}{$id}) 0 0 0
161 0 ▶ return if $self->{data}{$server}{$id} eq $args{data};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}{$server}{$id} eq $args{"data"} 0 0 0
162 0 warn "ServerIdStore is overwriting a key with a different value!\n";
163 }
164  
165 0 my $fh = $self->_open_file( ">>" );
166 0 $fh->print( "$server $id " . $self->_encode( $args{data}, "" ) . "\n" );
167  
168 0 $self->{data}{$server}{$id} = $args{data};
169 }
170  
171 =head1 AUTHOR
172  
173 Paul Evans <leonerd@leonerd.org.uk>
174  
175 =cut
176  
177 1 0x55AA;
« blib/lib/App/MatrixTool/HTTPClient.pm ↑ Summary blib/lib/App/MatrixTool/Command/notary.pm »