Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
PERL-536 GridFS: don't error if an empty file has no chunks
  • Loading branch information
xdg committed May 26, 2015
1 parent 9a3cfd5 commit 7007095
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Changes
Expand Up @@ -8,6 +8,11 @@
MongoDB::BulkWriteResult to ease detection of when that attributes is
supported by a server or not.

[Bug fixes]

- PERL-536 fix GridFS to stop throwing an error when a known empty file has
no chunks; errors will still be thrown if a non-empty file has no chunks.

[Changes]

- PERL-127 integers that fit in 32-bits are now encoded as BSON Int32;
Expand Down
2 changes: 1 addition & 1 deletion lib/MongoDB/GridFS/File.pm
Expand Up @@ -71,7 +71,7 @@ sub print {

my $cursor = $self->_grid->chunks->query({"files_id" => $self->info->{"_id"}})->sort({"n" => 1});

if ( !$cursor->has_next ) {
if ( $self->info->{length} && !$cursor->has_next ) {
MongoDB::GridFSError->throw(
sprintf( "GridFS file corrupt: no chunks found for file ID '%s'",
$self->info->{_id} )
Expand Down
23 changes: 22 additions & 1 deletion t/gridfs.t
Expand Up @@ -293,7 +293,28 @@ $grid->drop;
is($file, undef);
}

# no chunks asserts
subtest "empty file" => sub {
$grid->drop;
is( $grid->chunks->count, 0, "0 chunks exist" );

my $txt = "";

my $basicfh;
open( $basicfh, '<', \$txt );

my $fh = FileHandle->new;
$fh->fdopen( $basicfh, 'r' );
ok( $grid->insert( $fh, { filename => 'hello.txt' } ), "inserted" );

is( $grid->chunks->count, 0, "0 chunks still" );

$file = $grid->find_one;
is( $file->info->{filename}, 'hello.txt', "filename" );
is( $file->info->{length}, 0, "length" );
is( $file->slurp, "", "slurp" );
};

# no chunks for file with length asserts
{
$grid->drop;

Expand Down

0 comments on commit 7007095

Please sign in to comment.