Mojo/MemoryMap.pm

filter: Statementstmt 100% 25 / 25 Branchbran n/a Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 93.3% 42 / 45 CC 3 SCAR 3.4
CC
3
Cov
97%
writer
2.0
new
2.0
BEGIN
1.0
CRAP 1.4
« Mojo/MemoryMap/Writer.pm ↑ Summary
1 package Mojo::MemoryMap;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use File::Map qw(map_anonymous);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::File qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::MemoryMap::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
9 4 my ($class, $size) = @_;
10 4 ▶ my $self = $class->SUPER::new(size => $size // 52428800, usage => 0);
Condition100%2/2
$size // 52428800
l!l
13
MC/DC100%1/1
$size // 52428800
$size
Truth table100%2/2
A
A= $size
Aresult
11
00
11  
12 4 $self->{tempfile} = tempfile->touch;
13 4 map_anonymous my $map, $self->{size}, 'shared';
14 4 $self->{map} = \$map;
15 4 $self->writer->store({});
16  
17 4 return $self;
18 }
19  
20 5 ▶sub size { shift->{size} }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
21  
22 4 ▶sub usage { shift->{usage} }
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
23  
24 288 ▶sub writer {
Subroutine writer: called CC=2 SCAR=7.0
Pod documented
25 288 my $self = shift;
26  
27 288 ▶ my $fh = $self->{fh}{$$} ||= $self->{tempfile}->open('>');
Condition66%2/3
$self->{"fh"}{$$} ||= $self->{"tempfile"}->open(">")
l!l&&r!l&&!r
28440
MC/DC0%0/2
$self->{"fh"}{$$} ||= $self->{"tempfile"}->open(">")
$self->{"fh"}{$$} $self->{"tempfile"}->open(">")
Truth table66%2/3
A ||= B
A= $self->{"fh"}{$$}B= $self->{"tempfile"}->open(">")
ABresult
1X1
011
000
28 288 return Mojo::MemoryMap::Writer->new(fh => $fh, map => $self->{map}, usage => \$self->{usage});
29 }
30  
31 2 1;
32  
33 =encoding utf8
34  
35 =head1 NAME
36  
37 Mojo::MemoryMap - Safely use anonymous memory mapped segments
38  
39 =head1 SYNOPSIS
40  
41 use Mojo::MemoryMap;
42  
43 my $map = Mojo::MemoryMap->new(4096);
44 say $map->usage;
45 $map->writer->store({foo => 123});
46 say $map->writer->fetch->{foo};
47 say $map->writer->change(sub { delete $_->{foo} });
48 say $map->usage;
49  
50 =head1 DESCRIPTION
51  
52 L<Mojo::MemoryMap> uses L<File::Map> to allow you to safely cache mutable data structures in anonymous mapped memory
53 segments, and share it between multiple processes.
54  
55 =head1 METHODS
56  
57 L<Mojo::MemoryMap> inherits all methods from L<Mojo::Base> and implements the following new ones.
58  
59 =head2 new
60  
61 my $map = Mojo::MemoryMap->new;
62 my $map = Mojo::MemoryMap->new(4096);
63  
64 Construct a new L<Mojo::MemoryMap> object, defaults to a L</"size"> of C<52428800> bytes (50 MiB).
65  
66 =head2 size
67  
68 my $size = $map->size;
69  
70 Size of anonymous memory segment in bytes.
71  
72 =head2 usage
73  
74 my $usage = $map->usage;
75  
76 Current usage of anonymous memory segment in bytes.
77  
78 =head2 writer
79  
80 my $writer = $map->writer;
81  
82 Acquire exclusive lock and return L<Mojo::MemoryMap::Writer> object. Allowing the shared data structure to be retrieved
83 and modified safely. The lock is released when the writer object is destroyed.
84  
85 # Retrieve data
86 my $data = $map->writer->fetch;
87  
88 # Modify data safely
89 my $writer = $map->writer;
90 my $data = $writer->fetch;
91 $data->{foo} += 23;
92 $writer->store($data);
93 undef $writer;
94  
95 # Modify data safely (with less code)
96 $map->writer->change(sub { $_->{foo} += 23 });
97  
98 =head1 SEE ALSO
99  
100 L<Mojolicious::Plugin::Status>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
101  
102 =cut
« Mojo/MemoryMap/Writer.pm ↑ Summary