File Coverage

blib/lib/Mojo/MemoryMap/Writer.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 6 66.6
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 48 50 96.0


line stmt bran cond sub pod time code
1             package Mojo::MemoryMap::Writer;
2 2     2   15 use Mojo::Base -base;
  2         4  
  2         19  
3              
4 2     2   365 use Fcntl qw(:flock);
  2         4  
  2         250  
5 2     2   912 use Sereal qw(decode_sereal encode_sereal);
  2         1622  
  2         932  
6              
7 288 50   288   7068 sub DESTROY { flock(shift->{fh}, LOCK_UN) or die "Couldn't flock: $!" }
8              
9             sub change {
10 200     200 1 375 my ($self, $cb) = @_;
11 200         390 my $data = $self->fetch;
12 200         824 $cb->($_) for $data;
13 200         1280 return $self->store($data);
14             }
15              
16             sub fetch {
17 279     279 1 430 my $self = shift;
18 279         347 my $len = unpack 'N', substr(${$self->{map}}, 0, 4);
  279         895  
19 279         480 return decode_sereal(substr(${$self->{map}}, 4, $len));
  279         8893  
20             }
21              
22             sub new {
23 288     288 1 730 my $self = shift->SUPER::new(@_);
24 288 50       4063 flock($self->{fh}, LOCK_EX) or die "Couldn't flock: $!";
25 288         261636 return $self;
26             }
27              
28             sub store {
29 209     209 1 367 my ($self, $data) = @_;
30              
31 209         11268 my $bin = encode_sereal($data);
32 209         8498 my $bytes = pack('N', length $bin) . $bin;
33              
34 209         313 ${$self->{usage}} = my $usage = length $bytes;
  209         336  
35 209 100       272 return undef if $usage > length ${$self->{map}};
  209         495  
36 208         279 substr ${$self->{map}}, 0, $usage, $bytes;
  208         635  
37              
38 208         2206 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