Mojo/Asset/Memory.pm

filter: Statementstmt 100% 41 / 41 Branchbran 93.7% 15 / 16 Conditioncond 100% 21 / 21 MC/DCmcdc 100% 12 / 12 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 99.0% 109 / 110 CC 18 SCAR 12.5
CC
18
Cov
99%
contains
6.0
get_chunk
5.0
add_chunk
4.0
CRAP 3.5
« ojo.pm ↑ Summary Mojo/Message/Response.pm »
1 package Mojo::Asset::Memory;
2 32 ▶use Mojo::Base 'Mojo::Asset';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 32 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 32 has 'auto_upgrade';
9 302 ▶has max_memory_size => sub { $ENV{MOJO_MAX_MEMORY_SIZE} || 262144 };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MOJO_MAX_MEMORY_SIZE"} or 262144 3 299 302
10 7 ▶has mtime => sub {$^T};
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 699 ▶sub add_chunk {
Subroutine add_chunk: called CC=4 SCAR=13.9
Pod documented
13 699 my ($self, $chunk) = @_;
14  
15 699 ▶ croak 'Asset has been upgraded and is now frozen' if $self->{frozen};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"frozen"} 1 698 699
16 698 ▶ if ($self->auto_upgrade && ($self->size + length $chunk) > $self->max_memory_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_upgrade and $self->size + length($chunk) > $self->max_memory_size) 10 688 698
Condition100%3/3
$self->auto_upgrade and $self->size + length($chunk) > $self->max_memory_size
!ll&&!rl&&r
24744110
MC/DC100%2/2
$self->auto_upgrade and $self->size + length($chunk) > $self->max_memory_size
$self->auto_upgrade $self->size + length($chunk) > $self->max_memory_size
Truth table100%3/3
A and B
A= $self->auto_upgradeB= $self->size + length($chunk) > $self->max_memory_size
ABresult
0X0
100
111
17 10 $self->emit(upgrade => my $file = Mojo::Asset::File->new)->{frozen} = 1;
18 10 return $file->add_chunk($self->slurp . $chunk);
19 }
20  
21 688 $self->{content} .= $chunk;
22 688 return $self;
23 }
24  
25 59 ▶sub contains {
Subroutine contains: called CC=6 SCAR=17.9
Pod documented
26 59 my ($self, $str) = @_;
27  
28 59 my $start = $self->start_range;
29 59 ▶ my $pos = index $self->{content} // '', $str, $start;
Condition100%2/2
$self->{"content"} // ""
l!l
572
MC/DC100%1/1
$self->{"content"} // ""
$self->{"content"}
Truth table100%2/2
A
A= $self->{"content"}
Aresult
11
00
30 59 ▶ $pos -= $start if $start && $pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start and $pos >= 0 16 43 59
Condition100%3/3
$start and $pos >= 0
!ll&&!rl&&r
39416
MC/DC100%2/2
$start and $pos >= 0
$start $pos >= 0
Truth table100%3/3
A and B
A= $startB= $pos >= 0
ABresult
0X0
100
111
31 59 my $end = $self->end_range;
32  
33 59 ▶ return $end && ($pos + length $str) >= $end ? -1 : $pos;
Branch: 1 branch
BranchTrueFalseTotal
$end && $pos + length($str) >= $end ? : 2 57 59
Condition100%3/3
$end and $pos + length($str) >= $end
!ll&&!rl&&r
47102
MC/DC100%2/2
$end and $pos + length($str) >= $end
$end $pos + length($str) >= $end
Truth table100%3/3
A and B
A= $endB= $pos + length($str) >= $end
ABresult
0X0
100
111
34 }
35  
36 386 ▶sub get_chunk {
Subroutine get_chunk: called CC=5 SCAR=16.1
Pod documented
37 386 my ($self, $offset, $max) = @_;
38 386 ▶ $max //= 131072;
Condition100%2/2
$max //= 131072
l!l
8378
MC/DC100%1/1
$max //= 131072
$max
Truth table100%2/2
A
A= $max
Aresult
11
00
39  
40 386 $offset += $self->start_range;
41 386 ▶ if (defined(my $end = $self->end_range)) { $max = $end + 1 - $offset if ($offset + $max) > $end }
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $end = $self->end_range)) 18 368 386
if $offset + $max > $end 12 6 18
42  
43 386 ▶ return substr shift->{content} // '', $offset, $max;
Condition100%2/2
+(shift())->{"content"} // ""
l!l
213173
MC/DC100%1/1
+(shift())->{"content"} // ""
+(shift())->{"content"}
Truth table100%2/2
A
A= +(shift())->{"content"}
Aresult
11
00
44 }
45  
46 6 ▶sub move_to { path($_[1])->spew($_[0]{content} // '') and return $_[0] }
Subroutine move_to: called CC=3 SCAR=11.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
path($_[1])->spew($_[0]{"content"} // "") and return $_[0] 6 0 6
Condition100%2/2
$_[0]{"content"} // ""
l!l
42
MC/DC100%1/1
$_[0]{"content"} // ""
$_[0]{"content"}
Truth table100%2/2
A
A= $_[0]{"content"}
Aresult
11
00
47  
48 832 ▶sub size { length(shift->{content} // '') }
Subroutine size: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"content"} // ""
l!l
344488
MC/DC100%1/1
+(shift())->{"content"} // ""
+(shift())->{"content"}
Truth table100%2/2
A
A= +(shift())->{"content"}
Aresult
11
00
49  
50 443 ▶sub slurp { shift->{content} // '' }
Subroutine slurp: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"content"} // ""
l!l
38063
MC/DC100%1/1
+(shift())->{"content"} // ""
+(shift())->{"content"}
Truth table100%2/2
A
A= +(shift())->{"content"}
Aresult
11
00
51  
52 6 ▶sub to_file { Mojo::Asset::File->new->add_chunk(shift->slurp) }
Subroutine to_file: called CC=1 SCAR=0.0
Pod documented
53  
54 32 1;
55  
56 =encoding utf8
57  
58 =head1 NAME
59  
60 Mojo::Asset::Memory - In-memory storage for HTTP content
61  
62 =head1 SYNOPSIS
63  
64 use Mojo::Asset::Memory;
65  
66 my $mem = Mojo::Asset::Memory->new;
67 $mem->add_chunk('foo bar baz');
68 say $mem->slurp;
69  
70 =head1 DESCRIPTION
71  
72 L<Mojo::Asset::Memory> is an in-memory storage backend for HTTP content.
73  
74 =head1 EVENTS
75  
76 L<Mojo::Asset::Memory> inherits all events from L<Mojo::Asset> and can emit the following new ones.
77  
78 =head2 upgrade
79  
80 $mem->on(upgrade => sub ($mem, $file) {...});
81  
82 Emitted when asset gets upgraded to a L<Mojo::Asset::File> object.
83  
84 $mem->on(upgrade => sub ($mem, $file) { $file->tmpdir('/tmp') });
85  
86 =head1 ATTRIBUTES
87  
88 L<Mojo::Asset::Memory> inherits all attributes from L<Mojo::Asset> and implements the following new ones.
89  
90 =head2 auto_upgrade
91  
92 my $bool = $mem->auto_upgrade;
93 $mem = $mem->auto_upgrade($bool);
94  
95 Try to detect if content size exceeds L</"max_memory_size"> limit and automatically upgrade to a L<Mojo::Asset::File>
96 object.
97  
98 =head2 max_memory_size
99  
100 my $size = $mem->max_memory_size;
101 $mem = $mem->max_memory_size(1024);
102  
103 Maximum size in bytes of data to keep in memory before automatically upgrading to a L<Mojo::Asset::File> object,
104 defaults to the value of the C<MOJO_MAX_MEMORY_SIZE> environment variable or C<262144> (256KiB).
105  
106 =head2 mtime
107  
108 my $mtime = $mem->mtime;
109 $mem = $mem->mtime(1408567500);
110  
111 Modification time of asset, defaults to the value of C<$^T>.
112  
113 =head1 METHODS
114  
115 L<Mojo::Asset::Memory> inherits all methods from L<Mojo::Asset> and implements the following new ones.
116  
117 =head2 add_chunk
118  
119 $mem = $mem->add_chunk('foo bar baz');
120 my $file = $mem->add_chunk('abc' x 262144);
121  
122 Add chunk of data and upgrade to L<Mojo::Asset::File> object if necessary.
123  
124 =head2 contains
125  
126 my $position = $mem->contains('bar');
127  
128 Check if asset contains a specific string.
129  
130 =head2 get_chunk
131  
132 my $bytes = $mem->get_chunk($offset);
133 my $bytes = $mem->get_chunk($offset, $max);
134  
135 Get chunk of data starting from a specific position, defaults to a maximum chunk size of C<131072> bytes (128KiB).
136  
137 =head2 move_to
138  
139 $mem = $mem->move_to('/home/sri/foo.txt');
140  
141 Move asset data into a specific file.
142  
143 =head2 size
144  
145 my $size = $mem->size;
146  
147 Size of asset data in bytes.
148  
149 =head2 slurp
150  
151 my $bytes = $mem->slurp;
152  
153 Read all asset data at once.
154  
155 =head2 to_file
156  
157 my $file = $mem->to_file;
158  
159 Convert asset to L<Mojo::Asset::File> object.
160  
161 =head1 SEE ALSO
162  
163 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
164  
165 =cut
« ojo.pm ↑ Summary Mojo/Message/Response.pm »