This is an automated email from the ASF dual-hosted git repository.
rskraba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 6a1d0f3 AVRO-3190: Error when trying to read 0 bytes at eof (#1314)
6a1d0f3 is described below
commit 6a1d0f3a1c3b39350a27802bb1df18434985fe4b
Author: RyanSkraba <[email protected]>
AuthorDate: Fri Aug 27 11:39:19 2021 +0200
AVRO-3190: Error when trying to read 0 bytes at eof (#1314)
* fix error when trying to read 0 bytes at eof
When reading an avro file record for record each time when reading the last
record DataFileReader tries to get the next block but (of course) fails. This
fixes the issue.
* AVRO-3190: Return quickly on eof
* AVRO-3190: Unit test for EOF.
* AVRO-3190: Fix unit test to demonstrate problem
Co-authored-by: Stephan Hradek <[email protected]>
---
lang/perl/lib/Avro/DataFileReader.pm | 4 ++--
lang/perl/t/04_datafile.t | 46 ++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/lang/perl/lib/Avro/DataFileReader.pm
b/lang/perl/lib/Avro/DataFileReader.pm
index 0ce9181..e6341cc 100644
--- a/lang/perl/lib/Avro/DataFileReader.pm
+++ b/lang/perl/lib/Avro/DataFileReader.pm
@@ -133,12 +133,12 @@ sub next {
my @objs;
- $datafile->read_block_header if $datafile->eob;
return () if $datafile->eof;
+ $datafile->read_block_header if $datafile->eob;
my $block_count = $datafile->{object_count};
- if ($block_count <= $count) {
+ if ($block_count < $count) {
push @objs, $datafile->read_to_block_end;
croak "Didn't read as many objects than expected"
unless scalar @objs == $block_count;
diff --git a/lang/perl/t/04_datafile.t b/lang/perl/t/04_datafile.t
index a22efc8..281c18f 100644
--- a/lang/perl/t/04_datafile.t
+++ b/lang/perl/t/04_datafile.t
@@ -175,4 +175,50 @@ is_deeply $all[0], $data, "Our data is intact!";
is_deeply $all[0], $data, "Our data is intact!";
}
+## Test on a slightly larger file with 100 records
+{
+ my $tmpfh_write100 = File::Temp->new(UNLINK => 1);
+ my $write100_file = Avro::DataFileWriter->new(
+ fh => $tmpfh_write100,
+ writer_schema => $schema,
+ codec => 'bzip2'
+ );
+ foreach (1..100) {
+ $write100_file->print($data);
+ }
+ $write100_file->flush;
+
+ seek $tmpfh_write100, 0, 0;
+ my $read100_file = Avro::DataFileReader->new(
+ fh => $tmpfh_write100,
+ );
+
+ # Read the first instance.
+ my @next = $read100_file->next(1);
+ is scalar @next, 1, "first object back";
+ is_deeply $all[0], $data, "Our data is intact!";
+ is scalar $read100_file->eob, 0, "not end-of-block";
+ is scalar $read100_file->eof, 0, "not end-of-file";
+ is scalar $read100_file->{object_count}, 99, "remainder in this block
count";
+
+ # Scan over the next 98
+ @next = $read100_file->next(98);
+ is scalar @next, 98, "ninety-eigth objects back";
+ is_deeply $all[0], $data, "Our data is intact!";
+ is scalar $read100_file->eob, 0, "not end-of-block";
+ is scalar $read100_file->eof, 0, "not end-of-file";
+
+ # Read the last instance.
+ @next = $read100_file->next(1);
+ is scalar @next, 1, "last object back";
+ is_deeply $all[0], $data, "Our data is intact!";
+ is scalar $read100_file->eob, 1, "end-of-block";
+ is scalar $read100_file->eof, 1, "end-of-file";
+ is scalar $read100_file->{object_count}, 0, "no blocks remaining";
+
+ # One more instance
+ @next = $read100_file->next(1);
+ is scalar @next, 0, "no more objects back";
+}
+
done_testing;