line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::MemoryMap::Writer; |
2
|
2
|
|
|
2
|
|
16
|
use Mojo::Base -base; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
22
|
|
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
466
|
use Fcntl qw(:flock); |
|
2
|
|
|
|
|
14
|
|
|
2
|
|
|
|
|
319
|
|
5
|
2
|
|
|
2
|
|
1089
|
use Sereal qw(decode_sereal encode_sereal); |
|
2
|
|
|
|
|
1955
|
|
|
2
|
|
|
|
|
1045
|
|
6
|
|
|
|
|
|
|
|
7
|
288
|
50
|
|
288
|
|
7635
|
sub DESTROY { flock(shift->{fh}, LOCK_UN) or die "Couldn't flock: $!" } |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub change { |
10
|
200
|
|
|
200
|
1
|
487
|
my ($self, $cb) = @_; |
11
|
200
|
|
|
|
|
458
|
my $data = $self->fetch; |
12
|
200
|
|
|
|
|
987
|
$cb->($_) for $data; |
13
|
200
|
|
|
|
|
1615
|
return $self->store($data); |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub fetch { |
17
|
279
|
|
|
279
|
1
|
662
|
my $self = shift; |
18
|
279
|
|
|
|
|
439
|
my $len = unpack 'N', substr(${$self->{map}}, 0, 4); |
|
279
|
|
|
|
|
1124
|
|
19
|
279
|
|
|
|
|
565
|
return decode_sereal(substr(${$self->{map}}, 4, $len)); |
|
279
|
|
|
|
|
11252
|
|
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub new { |
23
|
288
|
|
|
288
|
1
|
864
|
my $self = shift->SUPER::new(@_); |
24
|
288
|
50
|
|
|
|
5344
|
flock($self->{fh}, LOCK_EX) or die "Couldn't flock: $!"; |
25
|
288
|
|
|
|
|
253521
|
return $self; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub store { |
29
|
209
|
|
|
209
|
1
|
431
|
my ($self, $data) = @_; |
30
|
|
|
|
|
|
|
|
31
|
209
|
|
|
|
|
11768
|
my $bin = encode_sereal($data); |
32
|
209
|
|
|
|
|
8800
|
my $bytes = pack('N', length $bin) . $bin; |
33
|
|
|
|
|
|
|
|
34
|
209
|
|
|
|
|
393
|
${$self->{usage}} = my $usage = length $bytes; |
|
209
|
|
|
|
|
419
|
|
35
|
209
|
100
|
|
|
|
357
|
return undef if $usage > length ${$self->{map}}; |
|
209
|
|
|
|
|
606
|
|
36
|
208
|
|
|
|
|
342
|
substr ${$self->{map}}, 0, $usage, $bytes; |
|
208
|
|
|
|
|
766
|
|
37
|
|
|
|
|
|
|
|
38
|
208
|
|
|
|
|
2769
|
return 1; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
1; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=encoding utf8 |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 NAME |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Mojo::MemoryMap::Writer - Writer |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 SYNOPSIS |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
use Mojo::MemoryMap::Writer; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $writer = Mojo::MemoryMap::Writer->new(map => $map); |
54
|
|
|
|
|
|
|
$writer->store({foo => 123}); |
55
|
|
|
|
|
|
|
say $writer->fetch->{foo}; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 DESCRIPTION |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
L is a scope guard for L that allows you to write safely to anonymous mapped |
60
|
|
|
|
|
|
|
memory segments. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 METHODS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
L inherits all methods from L and implements the following new ones. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head2 change |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $bool = $writer->change(sub {...}); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Fetch data, modify it in the closure and store it again right away. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Remove a value safely |
73
|
|
|
|
|
|
|
$writer->change(sub { delete $_->{foo} }); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 fetch |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $data = $writer->fetch; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Fetch data. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 new |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $writer = Mojo::MemoryMap::Writer->new; |
84
|
|
|
|
|
|
|
my $writer = Mojo::MemoryMap::Writer->new(map => $map); |
85
|
|
|
|
|
|
|
my $writer = Mojo::MemoryMap::Writer->new({map => $map}); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Construct a new L object. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 store |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $bool = $writer->store({foo => 123}); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Store data, replacing all existing data. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head1 SEE ALSO |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
L, L, L. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |