| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||||||||||||||||||||
| 2 | # or more contributor license agreements. See the NOTICE file | ||||||||||||||||||||
| 3 | # distributed with this work for additional information | ||||||||||||||||||||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||||||||||||||||||||
| 5 | # to you under the Apache License, Version 2.0 (the | ||||||||||||||||||||
| 6 | # "License"); you may not use this file except in compliance | ||||||||||||||||||||
| 7 | # with the License. You may obtain a copy of the License at | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | # https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||
| 10 | # | ||||||||||||||||||||
| 11 | # Unless required by applicable law or agreed to in writing, | ||||||||||||||||||||
| 12 | # software distributed under the License is distributed on an | ||||||||||||||||||||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||||||||||||||
| 14 | # KIND, either express or implied. See the License for the | ||||||||||||||||||||
| 15 | # specific language governing permissions and limitations | ||||||||||||||||||||
| 16 | # under the License. | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | package Avro::DataFileWriter; | ||||||||||||||||||||
| 19 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | ▶ | use constant DEFAULT_BLOCK_MAX_SIZE => 1024 * 64; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | ▶ | use Object::Tiny qw{ | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | fh | ||||||||||||||||||||
| 26 | writer_schema | ||||||||||||||||||||
| 27 | codec | ||||||||||||||||||||
| 28 | metadata | ||||||||||||||||||||
| 29 | block_max_size | ||||||||||||||||||||
| 30 | sync_marker | ||||||||||||||||||||
| 31 | 1 | }; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | ▶ | use Avro::BinaryEncoder; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 1 | ▶ | use Avro::BinaryDecoder; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 1 | ▶ | use Avro::DataFile; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 1 | ▶ | use Avro::Schema; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 1 | ▶ | use Compress::Zstd; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 1 | ▶ | use Error::Simple; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 1 | ▶ | use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | 1 | ▶ | use IO::Compress::RawDeflate qw(rawdeflate $RawDeflateError); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1 | our $VERSION = '1.12.2'; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 6 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||
| 46 | 6 | my $class = shift; | |||||||||||||||||||
| 47 | 6 | my $datafile = $class->SUPER::new(@_); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | ## default values | ||||||||||||||||||||
| 50 | 6 | ▶ | $datafile->{block_max_size} ||= DEFAULT_BLOCK_MAX_SIZE; | ||||||||||||||||||
|
Condition50%1/2
$datafile->{"block_max_size"} ||= 65536
MC/DC0%0/1
$datafile->{"block_max_size"} ||= 65536
$datafile->{"block_max_size"}
Truth table50%1/2
A
A= $datafile->{"block_max_size"}
| |||||||||||||||||||||
| 51 | 6 | ▶ | $datafile->{sync_marker} ||= $class->random_sync_marker; | ||||||||||||||||||
|
Condition33%1/3
$datafile->{"sync_marker"} ||= $class->random_sync_marker
MC/DC0%0/2
$datafile->{"sync_marker"} ||= $class->random_sync_marker
$datafile->{"sync_marker"}
$class->random_sync_marker
Truth table33%1/3
A ||= B
A= $datafile->{"sync_marker"}B= $class->random_sync_marker
| |||||||||||||||||||||
| 52 | 6 | ▶ | $datafile->{metadata} ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$datafile->{"metadata"} ||= {}
MC/DC100%1/1
$datafile->{"metadata"} ||= {}
$datafile->{"metadata"}
Truth table100%2/2
A
A= $datafile->{"metadata"}
| |||||||||||||||||||||
| 53 | 6 | ▶ | $datafile->{codec} ||= 'null'; | ||||||||||||||||||
|
Condition100%2/2
$datafile->{"codec"} ||= "null"
MC/DC100%1/1
$datafile->{"codec"} ||= "null"
$datafile->{"codec"}
Truth table100%2/2
A
A= $datafile->{"codec"}
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 6 | $datafile->{_current_size} = 0; | |||||||||||||||||||
| 56 | 6 | $datafile->{_serialized_objects} = []; | |||||||||||||||||||
| 57 | 6 | $datafile->{_compressed_block} = ''; | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 6 | ▶ | croak "Please specify a writer schema" unless $datafile->{writer_schema}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | croak "writer_schema is invalid" | ||||||||||||||||||||
| 61 | 6 | ▶ | unless eval { $datafile->{writer_schema}->isa("Avro::Schema") }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | throw Avro::DataFile::Error::InvalidCodec($datafile->{codec}) | ||||||||||||||||||||
| 64 | 6 | ▶ | unless Avro::DataFile->is_codec_valid($datafile->{codec}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 5 | return $datafile; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | ## it's not really good random, but it should be good enough | ||||||||||||||||||||
| 70 | 6 | ▶ | sub random_sync_marker { | ||||||||||||||||||
|
Subroutine
random_sync_marker: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 71 | 6 | my $class = shift; | |||||||||||||||||||
| 72 | 6 | my @r; | |||||||||||||||||||
| 73 | 6 | for (1..16) { | |||||||||||||||||||
| 74 | 96 | push @r, int rand(1<<8); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | 6 | my $marker = pack "C16", @r; | |||||||||||||||||||
| 77 | 6 | return $marker; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 104 | ▶ | sub print { | ||||||||||||||||||
|
Subroutine
print: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 81 | 104 | my $datafile = shift; | |||||||||||||||||||
| 82 | 104 | my $data = shift; | |||||||||||||||||||
| 83 | 104 | my $writer_schema = $datafile->{writer_schema}; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 104 | my $enc_ref = ''; | |||||||||||||||||||
| 86 | Avro::BinaryEncoder->encode( | ||||||||||||||||||||
| 87 | schema => $writer_schema, | ||||||||||||||||||||
| 88 | data => $data, | ||||||||||||||||||||
| 89 | 3120 | ▶ | emit_cb => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 90 | 3120 | $enc_ref .= ${ $_[0] }; | |||||||||||||||||||
| 91 | }, | ||||||||||||||||||||
| 92 | 104 | ); | |||||||||||||||||||
| 93 | 104 | $datafile->buffer_or_print(\$enc_ref); | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 104 | ▶ | sub buffer_or_print { | ||||||||||||||||||
|
Subroutine
buffer_or_print: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||
| 97 | 104 | my $datafile = shift; | |||||||||||||||||||
| 98 | 104 | my $string_ref = shift; | |||||||||||||||||||
| 99 | 104 | my $codec = $datafile->codec; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 104 | my $ser_objects = $datafile->{_serialized_objects}; | |||||||||||||||||||
| 102 | 104 | push @$ser_objects, $string_ref; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 104 | ▶ | if ($codec eq 'deflate') { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 105 | 1 | my $uncompressed = join('', map { $$_ } @$ser_objects); | |||||||||||||||||||
| 106 | rawdeflate \$uncompressed => \$datafile->{_compressed_block} | ||||||||||||||||||||
| 107 | 1 | ▶ | or croak "rawdeflate failed: $RawDeflateError"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | $datafile->{_current_size} = | ||||||||||||||||||||
| 109 | 1 | bytes::length($datafile->{_compressed_block}); | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | elsif ($codec eq 'bzip2') { | ||||||||||||||||||||
| 112 | 101 | my $uncompressed = join('', map { $$_ } @$ser_objects); | |||||||||||||||||||
| 113 | 101 | my $compressed; | |||||||||||||||||||
| 114 | 101 | ▶ | bzip2 \$uncompressed => \$compressed | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | or croak "bzip2 failed: $Bzip2Error"; | ||||||||||||||||||||
| 116 | 101 | $datafile->{_compressed_block} = $compressed; | |||||||||||||||||||
| 117 | 101 | $datafile->{_current_size} = bytes::length($datafile->{_compressed_block}); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | elsif ($codec eq 'zstandard') { | ||||||||||||||||||||
| 120 | 1 | my $uncompressed = join('', map { $$_ } @$ser_objects); | |||||||||||||||||||
| 121 | 1 | $datafile->{_compressed_block} = compress(\$uncompressed); | |||||||||||||||||||
| 122 | 1 | $datafile->{_current_size} = bytes::length($datafile->{_compressed_block}); | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||
| 125 | 1 | $datafile->{_current_size} += bytes::length($$string_ref); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | 104 | ▶ | if ($datafile->{_current_size} > $datafile->{block_max_size}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | ## ok, time to flush! | ||||||||||||||||||||
| 129 | 0 | $datafile->_print_block; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 104 | return; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 5 | ▶ | sub header { | ||||||||||||||||||
|
Subroutine
header: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 135 | 5 | my $datafile = shift; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 5 | my $metadata = $datafile->metadata; | |||||||||||||||||||
| 138 | 5 | my $schema = $datafile->writer_schema; | |||||||||||||||||||
| 139 | 5 | my $codec = $datafile->codec; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 5 | for (keys %$metadata) { | |||||||||||||||||||
| 142 | 4 | ▶ | warn "metadata '$_' is reserved" if /^avro\./; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 5 | my $encoded_header = ''; | |||||||||||||||||||
| 146 | Avro::BinaryEncoder->encode( | ||||||||||||||||||||
| 147 | schema => $Avro::DataFile::HEADER_SCHEMA, | ||||||||||||||||||||
| 148 | data => { | ||||||||||||||||||||
| 149 | magic => Avro::DataFile->AVRO_MAGIC, | ||||||||||||||||||||
| 150 | meta => { | ||||||||||||||||||||
| 151 | %$metadata, | ||||||||||||||||||||
| 152 | 'avro.schema' => $schema->to_string, | ||||||||||||||||||||
| 153 | 'avro.codec' => $codec, | ||||||||||||||||||||
| 154 | }, | ||||||||||||||||||||
| 155 | sync => $datafile->{sync_marker}, | ||||||||||||||||||||
| 156 | }, | ||||||||||||||||||||
| 157 | 76 | ▶ | emit_cb => sub { $encoded_header .= ${ $_[0] } }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 158 | 5 | ); | |||||||||||||||||||
| 159 | 5 | return $encoded_header; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 5 | ▶ | sub _print_header { | ||||||||||||||||||
|
Subroutine
_print_header: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 163 | 5 | my $datafile = shift; | |||||||||||||||||||
| 164 | 5 | $datafile->{_header_printed} = 1; | |||||||||||||||||||
| 165 | 5 | my $fh = $datafile->{fh}; | |||||||||||||||||||
| 166 | 5 | print $fh $datafile->header; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 5 | return 1; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 5 | ▶ | sub _print_block { | ||||||||||||||||||
|
Subroutine
_print_block: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 172 | 5 | my $datafile = shift; | |||||||||||||||||||
| 173 | 5 | ▶ | unless ($datafile->{_header_printed}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | 5 | $datafile->_print_header; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | 5 | my $ser_objects = $datafile->{_serialized_objects}; | |||||||||||||||||||
| 177 | 5 | my $object_count = scalar @$ser_objects; | |||||||||||||||||||
| 178 | 5 | my $length = $datafile->{_current_size}; | |||||||||||||||||||
| 179 | 5 | my $prefix = ''; | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 5 | for ($object_count, $length) { | |||||||||||||||||||
| 182 | Avro::BinaryEncoder->encode_long( | ||||||||||||||||||||
| 183 | 10 | ▶ | undef, $_, sub { $prefix .= ${ $_[0] } }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 184 | 10 | ); | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 5 | my $sync_marker = $datafile->{sync_marker}; | |||||||||||||||||||
| 188 | 5 | my $fh = $datafile->{fh}; | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | ## alternatively here, we could do n calls to print | ||||||||||||||||||||
| 191 | ## but we'll say that this all write block thing is here to overcome | ||||||||||||||||||||
| 192 | ## any memory issues we could have with deferencing the ser_objects | ||||||||||||||||||||
| 193 | 5 | ▶ | if ($datafile->codec ne 'null') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | 4 | print $fh $prefix, $datafile->{_compressed_block}, $sync_marker; | |||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | else { | ||||||||||||||||||||
| 197 | 1 | print $fh $prefix, (map { $$_ } @$ser_objects), $sync_marker; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | ## now reset our internal buffer | ||||||||||||||||||||
| 201 | 5 | $datafile->{_serialized_objects} = []; | |||||||||||||||||||
| 202 | 5 | $datafile->{_current_size} = 0; | |||||||||||||||||||
| 203 | 5 | $datafile->{_compressed_block} = ''; | |||||||||||||||||||
| 204 | 5 | return 1; | |||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 11 | ▶ | sub flush { | ||||||||||||||||||
|
Subroutine
flush: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 208 | 11 | my $datafile = shift; | |||||||||||||||||||
| 209 | 11 | ▶ | $datafile->_print_block if $datafile->{_current_size}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 0 | ▶ | sub close { | ||||||||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 213 | 0 | my $datafile = shift; | |||||||||||||||||||
| 214 | 0 | $datafile->flush; | |||||||||||||||||||
| 215 | 0 | ▶ | my $fh = $datafile->{fh} or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | 0 | close $fh; | |||||||||||||||||||
| 217 | } | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 6 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 220 | 6 | my $datafile = shift; | |||||||||||||||||||
| 221 | 6 | $datafile->flush; | |||||||||||||||||||
| 222 | 6 | return 1; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | package Avro::DataFile::Error::InvalidCodec; | ||||||||||||||||||||
| 226 | 1 | ▶ | use parent 'Error::Simple'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 1 | 1; | |||||||||||||||||||