File Coverage

blib/lib/Data/Keys/E/Dir/LockInPlace.pm
Criterion Covered Total %
statement 33 33 100.0
branch 5 6 83.3
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 49 50 98.0


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   3954 use warnings;
  3         6  
  3         117  
14 3     3   12 use strict;
  3         0  
  3         108  
15              
16             our $VERSION = '0.04';
17              
18 3     3   1338 use Moose::Role;
  3         11640  
  3         9  
19 3     3   13587 use Fcntl qw(:DEFAULT :flock);
  3         6  
  3         1986  
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   19 my $self = shift;
29 15         23 my $key = shift;
30 15         14 my $mode = shift;
31 15         14 my $create = shift;
32              
33 15         66 my ($new_key, $filename) = $self->_make_filename($key);
34              
35 15         100 my $lock_fh = IO::File->new();
36 15 100       430 $lock_fh->open($filename, ($create ? '+>>' : '<'))
    100          
37             or die 'failed to lock "'.$filename.'" - '.$!;
38            
39 12 50       2994601 flock($lock_fh, $mode)
40             or die 'failed to lock "'.$filename.'" - '.$!;
41              
42 12         755 $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 127 my $self = shift;
53 9         25 my $key = shift;
54 9         22 my $create = shift;
55 9         43 $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 9 my $self = shift;
66 6         9 my $key = shift;
67 6         6 my $create = shift;
68 6         21 $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 21 my $self = shift;
79 12         12 my $key = shift;
80              
81 12         512 close delete $self->_lock_inplace_data->{$key}->{'fh'};
82 12         483 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