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