| 1 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | package Metabase::Archive::Filesystem; | ||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Metabase filesystem-based storage | ||||||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '1.003'; # VERSION | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Metabase::Fact; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Data::Stream::Bulk::Callback; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Data::GUID (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use File::Slurp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use JSON 2 (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use MooseX::Types::Path::Class; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 4 | with 'Metabase::Archive'; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 4 | has 'root_dir' => ( | |||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 24 | isa => 'Path::Class::Dir', | ||||||||||||||||||||||||||||||||
| 25 | coerce => 1, | ||||||||||||||||||||||||||||||||
| 26 | required => 1, | ||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # Ensure we have a directory we can write to | ||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | sub initialize { | ||||||||||||||||||||||||||||||
|
Subroutine
initialize: called CC=5 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 31 | 2 | my ($self, @fact_classes) = @_; | |||||||||||||||||||||||||||||||
| 32 | 2 | my $dir = $self->root_dir; | |||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | if ( -d $dir && -w $dir ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
-d $dir and -w $dir
MC/DC0%0/2
-d $dir and -w $dir
-d $dir
-w $dir
Truth table33%1/3
A and B
A= -d $dirB= -w $dir
| |||||||||||||||||||||||||||||||||
| 34 | 0 | return; | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | elsif ( ! -d $dir ) { | ||||||||||||||||||||||||||||||||
| 37 | 2 | $dir->mkpath; | |||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | Carp::confess "Could not create directory '$dir': $!" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | unless -d $dir; | ||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||||||
| 42 | 0 | Carp::confess "Directory '$dir' not writeable"; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | # given fact, store it and return guid; return | ||||||||||||||||||||||||||||||||
| 47 | # XXX can we store a fact with a GUID already? Replaces? Or error? | ||||||||||||||||||||||||||||||||
| 48 | # here assign only if no GUID already | ||||||||||||||||||||||||||||||||
| 49 | 9 | ▶ | sub store { | ||||||||||||||||||||||||||||||
|
Subroutine
store: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 50 | 9 | my ($self, $fact_struct) = @_; | |||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 9 | my $guid = $fact_struct->{metadata}{core}{guid}; | |||||||||||||||||||||||||||||||
| 53 | 9 | ▶ | unless ( $guid ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 0 | Carp::confess "Can't store: no GUID set for fact\n"; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # freeze and write the fact | ||||||||||||||||||||||||||||||||
| 58 | File::Slurp::write_file( | ||||||||||||||||||||||||||||||||
| 59 | 9 | $self->_guid_path( $guid ), | |||||||||||||||||||||||||||||||
| 60 | {binmode => ':raw'}, | ||||||||||||||||||||||||||||||||
| 61 | JSON->new->ascii->encode($fact_struct), | ||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 9 | return $guid; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | # given guid, retrieve it and return it | ||||||||||||||||||||||||||||||||
| 68 | # type is directory path | ||||||||||||||||||||||||||||||||
| 69 | # class isa Metabase::Fact::Subclass | ||||||||||||||||||||||||||||||||
| 70 | 5 | ▶ | sub extract { | ||||||||||||||||||||||||||||||
|
Subroutine
extract: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 71 | 5 | my ($self, $guid) = @_; | |||||||||||||||||||||||||||||||
| 72 | 5 | return $self->_extract_file( $self->_guid_path($guid) ); | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | sub _extract_file { | ||||||||||||||||||||||||||||||
|
Subroutine
_extract_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 76 | 8 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||
| 77 | # read the fact | ||||||||||||||||||||||||||||||||
| 78 | 8 | my $fact_struct = JSON->new->ascii->decode( | |||||||||||||||||||||||||||||||
| 79 | File::Slurp::read_file( $file, { binmode => ':raw' } ), | ||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||
| 81 | 8 | return $fact_struct; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | sub delete { | ||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 85 | 1 | my ($self, $guid) = @_; | |||||||||||||||||||||||||||||||
| 86 | 1 | unlink $self->_guid_path( $guid ); | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 2 | ▶ | sub iterator { | ||||||||||||||||||||||||||||||
|
Subroutine
iterator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 90 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 91 | 2 | my @queue = map { $_->children } $self->root_dir->children; | |||||||||||||||||||||||||||||||
| 92 | return Data::Stream::Bulk::Callback->new( | ||||||||||||||||||||||||||||||||
| 93 | 6 | ▶ | callback => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 94 | 6 | my $d = shift @queue; | |||||||||||||||||||||||||||||||
| 95 | 6 | ▶ | if ($d) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 4 | my @results; | |||||||||||||||||||||||||||||||
| 97 | $d->recurse( | ||||||||||||||||||||||||||||||||
| 98 | 15 | ▶ | callback => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 99 | 15 | my $f = shift; | |||||||||||||||||||||||||||||||
| 100 | 15 | ▶ | push @results, $self->_extract_file($f) if ! $f->is_dir | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | 4 | ); | |||||||||||||||||||||||||||||||
| 103 | 4 | return \@results; | |||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||||||
| 106 | 2 | return; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 2 | ); | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 15 | ▶ | sub _guid_path { | ||||||||||||||||||||||||||||||
|
Subroutine
_guid_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 113 | 15 | my ($self, $guid) = @_; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | # convert guid from "abc-def-ghi" => "abc/def" as a place to put the file | ||||||||||||||||||||||||||||||||
| 116 | 15 | my $guid_path = lc $guid; | |||||||||||||||||||||||||||||||
| 117 | 15 | $guid_path =~ s{-}{/}g; | |||||||||||||||||||||||||||||||
| 118 | 15 | $guid_path =~ s{/\w+$}{}; | |||||||||||||||||||||||||||||||
| 119 | 15 | my $fact_path = Path::Class::file( $self->root_dir, $guid_path, $guid ); | |||||||||||||||||||||||||||||||
| 120 | 15 | $fact_path->dir->mkpath; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 15 | return $fact_path->stringify; | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 4 | 1; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||||||