| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Data::Keys::E::Dir::LockInPlace; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | Data::Keys::E::Dir::LockInPlace - place locks directly on the stored files | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | Uses F<flock> directly on the storage files. | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =cut | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 3 |  |  | 3 |  | 2685 | use warnings; | 
|  | 3 |  |  |  |  | 3 |  | 
|  | 3 |  |  |  |  | 96 |  | 
| 14 | 3 |  |  | 3 |  | 9 | use strict; | 
|  | 3 |  |  |  |  | 3 |  | 
|  | 3 |  |  |  |  | 96 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 3 |  |  | 3 |  | 1401 | use Moose::Role; | 
|  | 3 |  |  |  |  | 10671 |  | 
|  | 3 |  |  |  |  | 9 |  | 
| 19 | 3 |  |  | 3 |  | 11208 | use Fcntl qw(:DEFAULT :flock); | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 1830 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | has '_lock_inplace_data' => ( isa => 'HashRef', is => 'rw', default => sub { {} }); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =head1 METHODS | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =cut | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub _lock_mode { | 
| 28 | 15 |  |  | 15 |  | 25 | my $self   = shift; | 
| 29 | 15 |  |  |  |  | 10 | my $key    = shift; | 
| 30 | 15 |  |  |  |  | 12 | my $mode   = shift; | 
| 31 | 15 |  |  |  |  | 12 | my $create = shift; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 15 |  |  |  |  | 55 | my ($new_key, $filename) = $self->_make_filename($key); | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 15 |  |  |  |  | 78 | my $lock_fh = IO::File->new(); | 
| 36 | 15 | 100 |  |  |  | 377 | $lock_fh->open($filename, ($create ? '+>>' : '<')) | 
|  |  | 100 |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | or die 'failed to lock "'.$filename.'" - '.$!; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 12 | 50 |  |  |  | 2995499 | flock($lock_fh, $mode) | 
| 40 |  |  |  |  |  |  | or die 'failed to lock "'.$filename.'" - '.$!; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 12 |  |  |  |  | 654 | $self->_lock_inplace_data->{$key}->{'fh'} = $lock_fh; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | =head2 lock_ex | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | C<LOCK_EX> on a target key file. | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =cut | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub lock_ex { | 
| 52 | 9 |  |  | 9 | 1 | 110 | my $self   = shift; | 
| 53 | 9 |  |  |  |  | 13 | my $key    = shift; | 
| 54 | 9 |  |  |  |  | 7 | my $create = shift; | 
| 55 | 9 |  |  |  |  | 24 | $self->_lock_mode($key, LOCK_EX, $create); | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =head2 lock_sh | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | C<LOCK_SH> on a target key file. | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =cut | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub lock_sh { | 
| 65 | 6 |  |  | 6 | 1 | 6 | my $self   = shift; | 
| 66 | 6 |  |  |  |  | 3 | my $key    = shift; | 
| 67 | 6 |  |  |  |  | 9 | my $create = shift; | 
| 68 | 6 |  |  |  |  | 15 | $self->_lock_mode($key, LOCK_SH, $create); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =head2 unlock | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | Release lock from the target key file. | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =cut | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub unlock { | 
| 78 | 12 |  |  | 12 | 1 | 15 | my $self = shift; | 
| 79 | 12 |  |  |  |  | 12 | my $key  = shift; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 12 |  |  |  |  | 384 | close delete $self->_lock_inplace_data->{$key}->{'fh'}; | 
| 82 | 12 |  |  |  |  | 348 | delete $self->_lock_inplace_data->{$key}; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | 1; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | __END__ | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =head1 AUTHOR | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Jozef Kutej | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =cut |