Mojo/MemoryMap/Writer.pm

filter: Statementstmt 100% 33 / 33 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 96.0% 49 / 51 CC 5 SCAR 5.3
CC
5
Cov
95%
DESTROY
2.1
new
2.0
change
2.0
CRAP 1.7
« Mojolicious/Plugin/Status.pm ↑ Summary Mojo/MemoryMap.pm »
1 package Mojo::MemoryMap::Writer;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Sereal qw(decode_sereal encode_sereal);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 288 ▶sub DESTROY { flock(shift->{fh}, LOCK_UN) or die "Couldn't flock: $!" }
Subroutine DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
flock +(shift())->{"fh"}, 8 or die "Couldn't flock: $!" 288 0 288
8  
9 200 ▶sub change {
Subroutine change: called CC=2 SCAR=6.9
Pod documented
10 200 my ($self, $cb) = @_;
11 200 my $data = $self->fetch;
12 200 $cb->($_) for $data;
13 200 return $self->store($data);
14 }
15  
16 279 ▶sub fetch {
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
17 279 my $self = shift;
18 279 my $len = unpack 'N', substr(${$self->{map}}, 0, 4);
19 279 return decode_sereal(substr(${$self->{map}}, 4, $len));
20 }
21  
22 288 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
23 288 my $self = shift->SUPER::new(@_);
24 288 ▶ flock($self->{fh}, LOCK_EX) or die "Couldn't flock: $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $self->{"fh"}, 2 or die "Couldn't flock: $!" 288 0 288
25 288 return $self;
26 }
27  
28 209 ▶sub store {
Subroutine store: called CC=2 SCAR=6.9
Pod documented
29 209 my ($self, $data) = @_;
30  
31 209 my $bin = encode_sereal($data);
32 209 my $bytes = pack('N', length $bin) . $bin;
33  
34 209 ${$self->{usage}} = my $usage = length $bytes;
35 209 ▶ return undef if $usage > length ${$self->{map}};
Branch: 1 branch
BranchTrueFalseTotal
if $usage > length ${$self->{"map"};} 1 208 209
36 208 substr ${$self->{map}}, 0, $usage, $bytes;
37  
38 208 return 1;
39 }
40  
41 2 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<Mojo::MemoryMap::Writer> is a scope guard for L<Mojo::MemoryMap> that allows you to write safely to anonymous mapped
60 memory segments.
61  
62 =head1 METHODS
63  
64 L<Mojo::MemoryMap::Writer> inherits all methods from L<Mojo::Base> 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<Mojo::MemoryMap::Writer> 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<Mojolicious::Plugin::Status>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
98  
99 =cut
« Mojolicious/Plugin/Status.pm ↑ Summary Mojo/MemoryMap.pm »