Mango/GridFS/Reader.pm

filter: Statementstmt 14.2% 8 / 56 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 10.0% 2 / 20 Podpod 100% 12 / 12 totaltotal 17.8% 22 / 123 CC 15 SCAR 25.5
CC
15
Cov
9%
read
42.0
open
12.0
__ANON__
12.0
CRAP 12.8
« Mango/Cursor.pm ↑ Summary Mango/GridFS/Writer.pm »
1 package Mango::GridFS::Reader;
2 9 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 has 'gridfs';
7  
8 0 ▶sub chunk_size { shift->{meta}{chunkSize} }
Subroutine chunk_size: not called CC=1 SCAR=6.9
Pod documented
9 0 ▶sub content_type { shift->{meta}{contentType} }
Subroutine content_type: not called CC=1 SCAR=6.9
Pod documented
10 0 ▶sub filename { shift->{meta}{filename} }
Subroutine filename: not called CC=1 SCAR=6.9
Pod documented
11 0 ▶sub md5 { shift->{meta}{md5} }
Subroutine md5: not called CC=1 SCAR=6.9
Pod documented
12 0 ▶sub metadata { shift->{meta}{metadata} }
Subroutine metadata: not called CC=1 SCAR=6.9
Pod documented
13  
14 0 ▶sub open {
Subroutine open: not called CC=3 SCAR=24.8
Pod documented
15 0 my ($self, $oid, $cb) = @_;
16  
17 # Non-blocking
18 return $self->gridfs->files->find_one(
19 0 ▶ $oid => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
20 0 my ($collection, $err, $doc) = @_;
21 0 ▶ $err //= "$oid does not exist" unless $self->{meta} = $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"meta"} = $doc 0 0 0
Condition0%0/2
$err //= "$oid does not exist"
l!l
00
MC/DC0%0/1
$err //= "$oid does not exist"
$err
Truth table0%0/2
A
A= $err
Aresult
11
00
22 0 $self->$cb($err);
23 }
24 0 ▶ ) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
25  
26 # Blocking
27 croak "$oid does not exist"
28 0 ▶ unless $self->{meta} = $self->gridfs->files->find_one($oid);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"meta"} = $self->gridfs->files->find_one($oid) 0 0 0
29 0 return $self;
30 }
31  
32 0 ▶sub read {
Subroutine read: not called CC=6 SCAR=37.4
Pod documented
33 0 my ($self, $cb) = @_;
34  
35 0 ▶ $self->{pos} //= 0;
Condition0%0/2
$self->{"pos"} //= 0
l!l
00
MC/DC0%0/1
$self->{"pos"} //= 0
$self->{"pos"}
Truth table0%0/2
A
A= $self->{"pos"}
Aresult
11
00
36  
37 # EOF
38 0 ▶ if ($self->{pos} >= ($self->size // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pos"} >= ($self->size // 0)) 0 0 0
Condition0%0/2
$self->size // 0
l!l
00
MC/DC0%0/1
$self->size // 0
$self->size
Truth table0%0/2
A
A= $self->size
Aresult
11
00
39 0 ▶ return undef unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
40 0 ▶ return Mojo::IOLoop->next_tick(sub { $self->$cb(undef, undef) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 }
42  
43 # Blocking
44 0 my $n = int($self->{pos} / $self->chunk_size);
45 0 my $query = {files_id => $self->{meta}{_id}, n => $n};
46 0 my $fields = {_id => 0, data => 1};
47 return $self->_slice($n,
48 $self->gridfs->chunks->find_one($query, $fields)->{data})
49 0 ▶ unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
50  
51 # Non-blocking
52 $self->gridfs->chunks->find_one(
53 0 ▶ ($query, $fields) => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 my ($collection, $err, $doc) = @_;
55 0 $self->$cb($err, $self->_slice($n, $doc->{data}));
56 }
57 0 );
58 }
59  
60 0 ▶sub seek {
Subroutine seek: not called CC=1 SCAR=6.9
Pod documented
61 0 my ($self, $pos) = @_;
62 0 $self->{pos} = $pos;
63 0 return $self;
64 }
65  
66 0 ▶sub slurp {
Subroutine slurp: not called CC=3 SCAR=24.8
Pod documented
67 0 my ($self, $cb) = @_;
68  
69 # Blocking
70 0 my $data;
71 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
72 0 ▶ while (defined(my $chunk = $self->read)) { $data .= $chunk }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = $self->read)) 0 0 0
73 0 return $data;
74 }
75  
76 # Non-blocking
77 0 $self->_chunk(\$data, $cb);
78 }
79  
80 0 ▶sub size { shift->{meta}{length} }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
81  
82 0 ▶sub tell { shift->{pos} // 0 }
Subroutine tell: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"pos"} // 0
l!l
00
MC/DC0%0/1
+(shift())->{"pos"} // 0
+(shift())->{"pos"}
Truth table0%0/2
A
A= +(shift())->{"pos"}
Aresult
11
00
83  
84 0 ▶sub upload_date { shift->{meta}{uploadDate} }
Subroutine upload_date: not called CC=1 SCAR=6.9
Pod documented
85  
86 0 ▶sub _chunk {
Subroutine _chunk: not called CC=1 SCAR=6.9
87 0 my ($self, $dataref, $cb) = @_;
88  
89 $self->read(
90 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
91 0 my ($self, $err, $chunk) = @_;
92 0 ▶ return $self->$cb($err, $$dataref) if $err || !defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if $err or not defined $chunk 0 0 0
Condition0%0/3
$err or not defined $chunk
l!l&&r!l&&!r
000
MC/DC0%0/2
$err or not defined $chunk
$err not defined $chunk
Truth table0%0/3
A or not(B)
A= $errB= not defined $chunk
ABresult
1X1
011
000
93 0 $$dataref .= $chunk;
94 0 $self->_chunk($dataref, $cb);
95 }
96 0 );
97 }
98  
99 0 ▶sub _slice {
Subroutine _slice: not called CC=1 SCAR=6.9
100 0 my ($self, $n, $chunk) = @_;
101 0 my $offset = $self->{pos} - ($n * $self->chunk_size);
102 0 $self->{pos} += length $chunk;
103 0 return substr $chunk, $offset;
104 }
105  
106 9 1;
107  
108 =encoding utf8
109  
110 =head1 NAME
111  
112 Mango::GridFS::Reader - GridFS reader
113  
114 =head1 SYNOPSIS
115  
116 use Mango::GridFS::Reader;
117  
118 my $reader = Mango::GridFS::Reader->new(gridfs => $gridfs);
119  
120 =head1 DESCRIPTION
121  
122 L<Mango::GridFS::Reader> reads files from GridFS.
123  
124 =head1 ATTRIBUTES
125  
126 L<Mango::GridFS::Reader> implements the following attributes.
127  
128 =head2 gridfs
129  
130 my $gridfs = $reader->gridfs;
131 $reader = $reader->gridfs(Mango::GridFS->new);
132  
133 L<Mango::GridFS> object this reader belongs to.
134  
135 =head1 METHODS
136  
137 L<Mango::GridFS::Reader> inherits all methods from L<Mojo::Base> and
138 implements the following new ones.
139  
140 =head2 chunk_size
141  
142 my $size = $reader->chunk_size;
143  
144 Chunk size in bytes.
145  
146 =head2 content_type
147  
148 my $type = $reader->content_type;
149  
150 Content type of file.
151  
152 =head2 filename
153  
154 my $name = $reader->filename;
155  
156 Name of file.
157  
158 =head2 md5
159  
160 my $checksum = $reader->md5;
161  
162 MD5 checksum for file.
163  
164 =head2 metadata
165  
166 my $data = $reader->metadata;
167  
168 Additional information.
169  
170 =head2 open
171  
172 $reader = $reader->open($oid);
173  
174 Open file. You can also append a callback to perform operation non-blocking.
175  
176 $reader->open($oid => sub {
177 my ($reader, $err) = @_;
178 ...
179 });
180 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
181  
182 =head2 read
183  
184 my $chunk = $reader->read;
185  
186 Read chunk. You can also append a callback to perform operation non-blocking.
187  
188 $reader->read(sub {
189 my ($reader, $err, $chunk) = @_;
190 ...
191 });
192 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
193  
194 =head2 seek
195  
196 $reader = $reader->seek(13);
197  
198 Change current position.
199  
200 =head2 size
201  
202 my $size = $reader->size;
203  
204 Size of entire file in bytes.
205  
206 =head2 slurp
207  
208 my $data = $reader->slurp;
209  
210 Slurp all remaining data from file. You can also append a callback to perform
211 operation non-blocking.
212  
213 $reader->slurp(sub {
214 my ($reader, $err, $data) = @_;
215 ...
216 });
217 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
218  
219 =head2 tell
220  
221 my $pos = $reader->tell;
222  
223 Current position.
224  
225 =head2 upload_date
226  
227 my $time = $reader->upload_date;
228  
229 Date file was uploaded.
230  
231 =head1 SEE ALSO
232  
233 L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
234  
235 =cut
« Mango/Cursor.pm ↑ Summary Mango/GridFS/Writer.pm »