File Coverage

blib/lib/Mojo/MemoryMap.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition 4 5 80.0
subroutine 8 8 100.0
pod 4 4 100.0
total 40 41 97.5


line stmt bran cond sub pod time code
1             package Mojo::MemoryMap;
2 2     2   451190 use Mojo::Base -base;
  2         6  
  2         14  
3              
4 2     2   1476 use File::Map qw(map_anonymous);
  2         13519  
  2         9  
5 2     2   204 use Mojo::File qw(tempfile);
  2         5  
  2         104  
6 2     2   1075 use Mojo::MemoryMap::Writer;
  2         7  
  2         33  
7              
8             sub new {
9 4     4 1 9021 my ($class, $size) = @_;
10 4   100     54 my $self = $class->SUPER::new(size => $size // 52428800, usage => 0);
11              
12 4         65 $self->{tempfile} = tempfile->touch;
13 4         328520 map_anonymous my $map, $self->{size}, 'shared';
14 4         289 $self->{map} = \$map;
15 4         44 $self->writer->store({});
16              
17 4         31 return $self;
18             }
19              
20 5     5 1 58 sub size { shift->{size} }
21              
22 4     4 1 32 sub usage { shift->{usage} }
23              
24             sub writer {
25 288     288 1 3033185 my $self = shift;
26              
27 288   66     1531 my $fh = $self->{fh}{$$} ||= $self->{tempfile}->open('>');
28 288         2482 return Mojo::MemoryMap::Writer->new(fh => $fh, map => $self->{map}, usage => \$self->{usage});
29             }
30              
31             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 uses L 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 inherits all methods from L 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 object, defaults to a L 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 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, L, L.
101              
102             =cut