| 1 | #!perl | ||
| 2 | # ABSTRACT: 'threads' version of object representing Blobs in Azure Blob Storage | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | package Net::Azure::StorageClient::Blob::Thread; | ||
| 9 | $Net::Azure::StorageClient::Blob::Thread::VERSION = '0.6'; | ||
| 10 | 1 | ▶ | use parent qw/Net::Azure::StorageClient::Blob/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use threads; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Thread::Semaphore; | ||
| 13 | |||
| 14 | use namespace::clean; | ||
| 15 | |||
| 16 | sub download_use_thread { | ||
| 17 | my ( $self, $args ) = @_; | ||
| 18 | my $thread = $args->{ thread } || 10; | ||
| 19 | my $semaphore = Thread::Semaphore->new( $thread ); | ||
| 20 | my $download_items = $args->{ download_items }; | ||
| 21 | my $params = $args->{ params }; | ||
| 22 | my $container_name = $args->{ container_name }; | ||
| 23 | my %th; | ||
| 24 | for my $key ( keys %$download_items ) { | ||
| 25 | my $item; | ||
| 26 | if ( $self->{ container_name } ) { | ||
| 27 | $item = $key; | ||
| 28 | } else { | ||
| 29 | $item = $container_name . '/' . $key, | ||
| 30 | } | ||
| 31 | $th{ $key } = threads->new(\&_download, | ||
| 32 | $self, | ||
| 33 | $item, | ||
| 34 | $download_items->{ $key }, | ||
| 35 | $params, | ||
| 36 | $semaphore ); | ||
| 37 | } | ||
| 38 | my @responses; | ||
| 39 | for my $key ( keys %$download_items ) { | ||
| 40 | my ( $res ) = $th{ $key }->join(); | ||
| 41 | push ( @responses, $res ); | ||
| 42 | } | ||
| 43 | return @responses; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _download { | ||
| 47 | my ( $self, $from, $to, $params, $semaphore ) = @_; | ||
| 48 | $semaphore->down(); | ||
| 49 | $params->{ force } = 1; | ||
| 50 | my $res = $self->download( $from, $to, $params ); | ||
| 51 | $semaphore->up(); | ||
| 52 | return $res; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub upload_use_thread { | ||
| 56 | my ( $self, $args ) = @_; | ||
| 57 | my $thread = $args->{ thread } || 10; | ||
| 58 | my $semaphore = Thread::Semaphore->new( $thread ); | ||
| 59 | my $upload_items = $args->{ upload_items }; | ||
| 60 | my $params = $args->{ params }; | ||
| 61 | my %th; | ||
| 62 | for my $key ( keys %$upload_items ) { | ||
| 63 | $th{ $key } = threads->new(\&_upload, | ||
| 64 | $self, | ||
| 65 | $key, | ||
| 66 | $upload_items->{ $key }, | ||
| 67 | $params, | ||
| 68 | $semaphore ); | ||
| 69 | } | ||
| 70 | my @responses; | ||
| 71 | for my $key ( keys %$upload_items ) { | ||
| 72 | my ( $res ) = $th{ $key }->join(); | ||
| 73 | push ( @responses, $res ); | ||
| 74 | } | ||
| 75 | return @responses; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _upload { | ||
| 79 | my ( $self, $from, $to, $params, $semaphore ) = @_; | ||
| 80 | $semaphore->down(); | ||
| 81 | $params->{ force } = 1; | ||
| 82 | my $res = $self->upload( $from, $to, $params ); | ||
| 83 | $semaphore->up(); | ||
| 84 | return $res; | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; | ||
| 88 | |||
| 89 | __END__ | ||