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   14 use Mojo::Base -base;
  2         6  
  2         14  
3              
4 2     2   442 use Fcntl qw(:flock);
  2         5  
  2         322  
5 2     2   1025 use Sereal qw(decode_sereal encode_sereal);
  2         1845  
  2         1000  
6              
7 288 50   288   7560 sub DESTROY { flock(shift->{fh}, LOCK_UN) or die "Couldn't flock: $!" }
8              
9             sub change {
10 200     200 1 483 my ($self, $cb) = @_;
11 200         417 my $data = $self->fetch;
12 200         1110 $cb->($_) for $data;
13 200         1743 return $self->store($data);
14             }
15              
16             sub fetch {
17 279     279 1 542 my $self = shift;
18 279         486 my $len = unpack 'N', substr(${$self->{map}}, 0, 4);
  279         1235  
19 279         513 return decode_sereal(substr(${$self->{map}}, 4, $len));
  279         12388  
20             }
21              
22             sub new {
23 288     288 1 968 my $self = shift->SUPER::new(@_);
24 288 50       5637 flock($self->{fh}, LOCK_EX) or die "Couldn't flock: $!";
25 288         262426 return $self;
26             }
27              
28             sub store {
29 209     209 1 476 my ($self, $data) = @_;
30              
31 209         12493 my $bin = encode_sereal($data);
32 209         8889 my $bytes = pack('N', length $bin) . $bin;
33              
34 209         428 ${$self->{usage}} = my $usage = length $bytes;
  209         544  
35 209 100       345 return undef if $usage > length ${$self->{map}};
  209         715  
36 208         356 substr ${$self->{map}}, 0, $usage, $bytes;
  208         880  
37              
38 208         2906 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