| 1 | package Mango::GridFS; | ||||||||||||||||||||
| 2 | 9 | ▶ | use Mojo::Base -base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 9 | ▶ | use Mango::GridFS::Reader; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 9 | ▶ | use Mango::GridFS::Writer; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 0 | ▶ | has chunks => sub { $_[0]->db->collection($_[0]->prefix . '.chunks') }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 8 | 9 | has 'db'; | |||||||||||||||||||
| 9 | 0 | ▶ | has files => sub { $_[0]->db->collection($_[0]->prefix . '.files') }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 10 | 9 | has prefix => 'fs'; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | sub delete { | ||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 13 | 0 | my ($self, $oid, $cb) = @_; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # Non-blocking | ||||||||||||||||||||
| 16 | return Mojo::IOLoop->delay( | ||||||||||||||||||||
| 17 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 18 | 0 | my $delay = shift; | |||||||||||||||||||
| 19 | 0 | $self->files->remove({_id => $oid} => $delay->begin); | |||||||||||||||||||
| 20 | 0 | $self->chunks->remove({files_id => $oid} => $delay->begin); | |||||||||||||||||||
| 21 | }, | ||||||||||||||||||||
| 22 | 0 | ▶ | sub { $self->$cb($_[1] || $_[3]) } | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$_[1] || $_[3]
MC/DC0%0/2
$_[1] || $_[3]
$_[1]
$_[3]
Truth table0%0/3
A || B
A= $_[1]B= $_[3]
| |||||||||||||||||||||
| 23 | 0 | ▶ | ) if $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # Blocking | ||||||||||||||||||||
| 26 | 0 | $self->files->remove({_id => $oid}); | |||||||||||||||||||
| 27 | 0 | $self->chunks->remove({files_id => $oid}); | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | ▶ | sub find_version { | ||||||||||||||||||
|
Subroutine
find_version: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 31 | 0 | my ($self, $name, $version, $cb) = @_; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # Positive numbers are absolute and negative ones relative | ||||||||||||||||||||
| 34 | 0 | my $cursor = $self->files->find({filename => $name}, {_id => 1})->limit(-1); | |||||||||||||||||||
| 35 | 0 | ▶ | $cursor->sort({uploadDate => $version < 0 ? -1 : 1}) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 36 | ->skip($version < 0 ? abs($version) - 1 : $version); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | # Non-blocking | ||||||||||||||||||||
| 39 | return $cursor->next( | ||||||||||||||||||||
| 40 | 0 | ▶ | sub { shift; $self->$cb(shift, $_[0] ? $_[0]{_id} : undef) }) | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | ▶ | if $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # Blocking | ||||||||||||||||||||
| 44 | 0 | my $doc = $cursor->next; | |||||||||||||||||||
| 45 | 0 | ▶ | return $doc ? $doc->{_id} : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | sub list { | ||||||||||||||||||
|
Subroutine
list: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 49 | 0 | my ($self, $cb) = @_; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | # Blocking | ||||||||||||||||||||
| 52 | 0 | ▶ | return $self->files->find->distinct('filename') unless $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # Non-blocking | ||||||||||||||||||||
| 55 | 0 | ▶ | $self->files->find->distinct('filename' => sub { shift; $self->$cb(@_) }); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | ▶ | sub reader { Mango::GridFS::Reader->new(gridfs => shift) } | ||||||||||||||||||
|
Subroutine
reader: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 59 | 0 | ▶ | sub writer { Mango::GridFS::Writer->new(gridfs => shift) } | ||||||||||||||||||
|
Subroutine
writer: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 9 | 1; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =encoding utf8 | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head1 NAME | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Mango::GridFS - GridFS | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head1 SYNOPSIS | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | use Mango::GridFS; | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | my $gridfs = Mango::GridFS->new(db => $db); | ||||||||||||||||||||
| 74 | my $reader = $gridfs->reader; | ||||||||||||||||||||
| 75 | my $writer = $gridfs->writer; | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head1 DESCRIPTION | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | L<Mango::GridFS> is an interface for MongoDB GridFS access. | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | L<Mango::GridFS> implements the following attributes. | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =head2 chunks | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | my $chunks = $gridfs->chunks; | ||||||||||||||||||||
| 88 | $gridfs = $gridfs->chunks(Mango::Collection->new); | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | L<Mango::Collection> object for C<chunks> collection, defaults to one based on | ||||||||||||||||||||
| 91 | L</"prefix">. | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =head2 db | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | my $db = $gridfs->db; | ||||||||||||||||||||
| 96 | $gridfs = $gridfs->db(Mango::Database->new); | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | L<Mango::Database> object GridFS belongs to. | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =head2 files | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | my $files = $gridfs->files; | ||||||||||||||||||||
| 103 | $gridfs = $gridfs->files(Mango::Collection->new); | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | L<Mango::Collection> object for C<files> collection, defaults to one based on | ||||||||||||||||||||
| 106 | L</"prefix">. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head2 prefix | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | my $prefix = $gridfs->prefix; | ||||||||||||||||||||
| 111 | $gridfs = $gridfs->prefix('foo'); | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | Prefix for GridFS collections, defaults to C<fs>. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head1 METHODS | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | L<Mango::GridFS> inherits all methods from L<Mojo::Base> and implements the | ||||||||||||||||||||
| 118 | following new ones. | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =head2 delete | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | $gridfs->delete($oid); | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Delete file. You can also append a callback to perform operation non-blocking. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | $gridfs->delete($oid => sub { | ||||||||||||||||||||
| 127 | my ($gridfs, $err) = @_; | ||||||||||||||||||||
| 128 | ... | ||||||||||||||||||||
| 129 | }); | ||||||||||||||||||||
| 130 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head2 find_version | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | my $oid = $gridfs->find_version('test.txt', 1); | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | Find versions of files, positive numbers from C<0> and upwards always point to | ||||||||||||||||||||
| 137 | a specific version, negative ones start with C<-1> for the most recently added | ||||||||||||||||||||
| 138 | version. You can also append a callback to perform operation non-blocking. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | $gridfs->find_version(('test.txt', 1) => sub { | ||||||||||||||||||||
| 141 | my ($gridfs, $err, $oid) = @_; | ||||||||||||||||||||
| 142 | ... | ||||||||||||||||||||
| 143 | }); | ||||||||||||||||||||
| 144 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =head2 list | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | my $names = $gridfs->list; | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | List files. You can also append a callback to perform operation non-blocking. | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | $gridfs->list(sub { | ||||||||||||||||||||
| 153 | my ($gridfs, $err, $names) = @_; | ||||||||||||||||||||
| 154 | ... | ||||||||||||||||||||
| 155 | }); | ||||||||||||||||||||
| 156 | Mojo::IOLoop->start unless Mojo::IOLoop->is_running; | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head2 reader | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | my $reader = $gridfs->reader; | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Build L<Mango::GridFS::Reader> object. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | # Read all data at once from newest version of file | ||||||||||||||||||||
| 165 | my $oid = $gridfs->find_version('test.txt', -1); | ||||||||||||||||||||
| 166 | my $data = $gridfs->reader->open($oid)->slurp; | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | # Read all data in chunks from file | ||||||||||||||||||||
| 169 | my $reader = $gridfs->reader->open($oid); | ||||||||||||||||||||
| 170 | while (defined(my $chunk = $reader->read)) { say "Chunk: $chunk" } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head2 writer | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | my $writer = $gridfs->writer; | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Build L<Mango::GridFS::Writer> object. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | # Write all data at once to file with name | ||||||||||||||||||||
| 179 | my $oid = $gridfs->writer->filename('test.txt')->write('Hello!')->close; | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | # Write data in chunks to file | ||||||||||||||||||||
| 182 | my $writer = $gridfs->writer; | ||||||||||||||||||||
| 183 | $writer->write($_) for 1 .. 100; | ||||||||||||||||||||
| 184 | my $oid = $writer->close; | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head1 SEE ALSO | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>. | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||