line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hash::Storage::Driver::OneFile; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
493
|
use v5.10; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
6
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
7
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
20
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
474
|
use File::Slurp; |
|
1
|
|
|
|
|
9056
|
|
|
1
|
|
|
|
|
61
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
6
|
use base "Hash::Storage::Driver::Base"; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
531
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub init { |
14
|
|
|
|
|
|
|
my ($self) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub get { |
19
|
|
|
|
|
|
|
my ( $self, $id ) = @_; |
20
|
|
|
|
|
|
|
my $hashes = $self->_load_data(); |
21
|
|
|
|
|
|
|
return $hashes->{$id}; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub set { |
25
|
|
|
|
|
|
|
my ( $self, $id, $fields ) = @_; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$self->do_exclusively( sub { |
28
|
|
|
|
|
|
|
my $hashes = $self->_load_data(); |
29
|
|
|
|
|
|
|
@{ $hashes->{$id} }{ keys %$fields } = values %$fields; |
30
|
|
|
|
|
|
|
$self->_save_data($hashes); |
31
|
|
|
|
|
|
|
} ); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub del { |
35
|
|
|
|
|
|
|
my ( $self, $id ) = @_; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$self->do_exclusively( sub { |
38
|
|
|
|
|
|
|
my $hashes = $self->_load_data(); |
39
|
|
|
|
|
|
|
delete $hashes->{$id}; |
40
|
|
|
|
|
|
|
$self->_save_data($hashes); |
41
|
|
|
|
|
|
|
} ); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub list { |
45
|
|
|
|
|
|
|
my ( $self, @query ) = @_; |
46
|
|
|
|
|
|
|
my $hashes = $self->_load_data(); |
47
|
|
|
|
|
|
|
my @hashes = values %$hashes; |
48
|
|
|
|
|
|
|
return $self->do_filtering(\@hashes, \@query); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub count { |
52
|
|
|
|
|
|
|
my ( $self, $filter ) = @_; |
53
|
|
|
|
|
|
|
my $hashes = $self->list(where => $filter); |
54
|
|
|
|
|
|
|
return scalar(@$hashes); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _load_data { |
58
|
|
|
|
|
|
|
my $self = shift; |
59
|
|
|
|
|
|
|
return {} unless -e $self->{file}; |
60
|
|
|
|
|
|
|
my $serialized = read_file( $self->{file} ); |
61
|
|
|
|
|
|
|
my $data = $self->{serializer}->deserialize($serialized); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return $data; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub _save_data { |
67
|
|
|
|
|
|
|
my ( $self, $data ) = @_; |
68
|
|
|
|
|
|
|
my $serialized = $self->{serializer}->serialize($data); |
69
|
|
|
|
|
|
|
write_file( $self->{file}, { atomic => 1 }, $serialized ); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; # End of Hash::Storage |