| 1 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package Metabase::Archive::S3; | ||||||||||
| 6 | 2 | our $VERSION = '1.000'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use MooseX::Types::Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use Metabase::Fact; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use Data::GUID (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use Data::Stream::Bulk::Filter 0.08; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | ▶ | use JSON 2 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | ▶ | use Net::Amazon::S3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use Path::Class (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use Compress::Zlib 2 qw(compress uncompress); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 2 | with 'Metabase::Backend::AWS'; | |||||||||
| 24 | 2 | with 'Metabase::Archive' => { -version => 1.000 }; | |||||||||
| 25 | |||||||||||
| 26 | # Prefix string must have a trailing slash but not leading slash | ||||||||||
| 27 | subtype 'PrefixStr' | ||||||||||
| 28 | => as 'Str' | ||||||||||
| 29 | 0 | ▶ | => where { $_ =~ m{^\w} && $_ =~ m{/$} }; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||
| 30 | |||||||||||
| 31 | coerce 'PrefixStr' | ||||||||||
| 32 | 0 | ▶ | => from 'Str' => via { s{/$}{}; s{^/}{}; $_ . "/" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=3.5
| |||||||||||
| 33 | |||||||||||
| 34 | 2 | has 'bucket' => ( | |||||||||
| 35 | is => 'ro', | ||||||||||
| 36 | isa => 'Str', | ||||||||||
| 37 | required => 1, | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | 2 | has 'prefix' => ( | |||||||||
| 41 | is => 'ro', | ||||||||||
| 42 | isa => 'PrefixStr', | ||||||||||
| 43 | required => 1, | ||||||||||
| 44 | coerce => 1, | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | 2 | has 'compressed' => ( | |||||||||
| 48 | is => 'rw', | ||||||||||
| 49 | isa => 'Bool', | ||||||||||
| 50 | default => 1, | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | has 's3_bucket' => ( | ||||||||||
| 54 | is => 'ro', | ||||||||||
| 55 | isa => 'Net::Amazon::S3::Client::Bucket', | ||||||||||
| 56 | required => 1, | ||||||||||
| 57 | lazy => 1, | ||||||||||
| 58 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 59 | 0 | my $self = shift; | |||||||||
| 60 | 0 | my $s3 = Net::Amazon::S3->new( | |||||||||
| 61 | aws_access_key_id => $self->access_key_id, | ||||||||||
| 62 | aws_secret_access_key => $self->secret_access_key, | ||||||||||
| 63 | retry => 1, | ||||||||||
| 64 | ); | ||||||||||
| 65 | 0 | my $client = Net::Amazon::S3::Client->new( s3 => $s3 ); | |||||||||
| 66 | 0 | ▶ | my $method = (grep { $_ eq $self->bucket } $client->buckets) ? 'bucket' : 'create_bucket'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | return $client->$method( name => $self->bucket ); | |||||||||
| 68 | } | ||||||||||
| 69 | 2 | ); | |||||||||
| 70 | |||||||||||
| 71 | has '_json' => ( | ||||||||||
| 72 | is => 'ro', | ||||||||||
| 73 | required => 1, | ||||||||||
| 74 | lazy => 1, | ||||||||||
| 75 | 0 | ▶ | default => sub { JSON->new->ascii }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 76 | 2 | ); | |||||||||
| 77 | |||||||||||
| 78 | 0 | ▶ | sub initialize {} | ||||||||
|
Subroutine
initialize: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 79 | |||||||||||
| 80 | # given fact, store it and return guid; | ||||||||||
| 81 | 0 | ▶ | sub store { | ||||||||
|
Subroutine
store: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 82 | 0 | my ( $self, $fact_struct ) = @_; | |||||||||
| 83 | 0 | my $guid = $fact_struct->{metadata}{core}{guid}; | |||||||||
| 84 | 0 | my $type = $fact_struct->{metadata}{core}{type}; | |||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | unless ($guid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | Carp::confess "Can't store: no GUID set for fact\n"; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | my $json = $self->_json->encode($fact_struct); | |||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | if ( $self->compressed ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | $json = compress($json); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 0 | my $s3_object = $self->s3_bucket->object( | |||||||||
| 97 | key => $self->prefix . lc $guid, | ||||||||||
| 98 | # acl_short => 'public-read', | ||||||||||
| 99 | content_type => 'application/json', | ||||||||||
| 100 | ); | ||||||||||
| 101 | 0 | $s3_object->put($json); | |||||||||
| 102 | |||||||||||
| 103 | 0 | return $guid; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # given guid, retrieve it and return it | ||||||||||
| 107 | # type is directory path | ||||||||||
| 108 | # class isa Metabase::Fact::Subclass | ||||||||||
| 109 | 0 | ▶ | sub extract { | ||||||||
|
Subroutine
extract: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 110 | 0 | my ( $self, $guid ) = @_; | |||||||||
| 111 | |||||||||||
| 112 | 0 | my $s3_object = $self->s3_bucket->object( key => $self->prefix . lc $guid ); | |||||||||
| 113 | 0 | return $self->_extract_struct( $s3_object ); | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub _extract_struct { | ||||||||
|
Subroutine
_extract_struct: not called CC=2 SCAR=17.9
| |||||||||||
| 117 | 0 | my ( $self, $s3_object ) = @_; | |||||||||
| 118 | |||||||||||
| 119 | 0 | my $json = $s3_object->get; | |||||||||
| 120 | 0 | ▶ | if ( $self->compressed ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | $json = uncompress($json); | |||||||||
| 122 | } | ||||||||||
| 123 | 0 | my $struct = $self->_json->decode($json); | |||||||||
| 124 | 0 | return $struct; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | # DO NOT lc() GUID | ||||||||||
| 128 | 0 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 129 | 0 | my ( $self, $guid ) = @_; | |||||||||
| 130 | |||||||||||
| 131 | 0 | my $s3_object = $self->s3_bucket->object( key => $self->prefix . $guid ); | |||||||||
| 132 | 0 | $s3_object->delete; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | sub iterator { | ||||||||
|
Subroutine
iterator: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 136 | 0 | my ($self) = @_; | |||||||||
| 137 | return Data::Stream::Bulk::Filter->new( | ||||||||||
| 138 | stream => $self->s3_bucket->list( { prefix => $self->prefix } ), | ||||||||||
| 139 | 0 | ▶ | filter => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 140 | 0 | return [ map { $self->_extract_struct( $_ ) } @{ $_[0] } ]; | |||||||||
| 141 | }, | ||||||||||
| 142 | 0 | ); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 2 | 1; | |||||||||
| 146 | |||||||||||
| 147 | # ABSTRACT: Metabase storage using Amazon S3 | ||||||||||
| 148 | # | ||||||||||
| 149 | # This file is part of Metabase-Backend-AWS | ||||||||||
| 150 | # | ||||||||||
| 151 | # This software is Copyright (c) 2011 by David Golden. | ||||||||||
| 152 | # | ||||||||||
| 153 | # This is free software, licensed under: | ||||||||||
| 154 | # | ||||||||||
| 155 | # The Apache License, Version 2.0, January 2004 | ||||||||||
| 156 | # | ||||||||||
| 157 | |||||||||||
| 158 | |||||||||||
| 159 | __END__ | ||||||||||