| 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"
MC/DC0%0/1
$args{"encode"} // "base64"
$args{"encode"}
Truth table0%0/2
A
A= $args{"encode"}
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$mode eq ">>" and not -f $path
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
| |||||||||||||||||||||||||||||
| 53 | 0 | make_path( dirname( $path ) ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if( open my $fh, $mode, $path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | return $fh; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | return undef if $! == ENOENT and $mode eq "<"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$! == 2 and $mode eq "<"
MC/DC0%0/2
$! == 2 and $mode eq "<"
$! == 2
$mode eq "<"
Truth table0%0/3
A and B
A= $! == 2B= $mode eq "<"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if( my $fh = $self->_open_file( "<" ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | while( <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | m/^\s*#/ and next; # ignore comment lines | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | my ( $server, $id, $key ) = split m/\s+/, $_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | defined $key or warn( "Unable to parse line $_" ), next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | return if $self->{data}{$server}{$id} eq $args{data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||