blib/lib/MaxMind/DB/Writer/Serializer.pm

filter: Statementstmt 94.1% 240 / 255 Branchbran 88.3% 53 / 60 Conditioncond 91.6% 11 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 95.4% 42 / 44 Podpod 0.0% 0 / 1 totaltotal 92.6% 352 / 380 CC 39 SCAR 13.6
CC
39
Cov
93%
_require_x_bits_unsigned_integer
10.0
_control_bytes
7.0
__ANON__
6.0
CRAP 3.9
« blib/lib/MaxMind/DB/Writer/Tree.pm ↑ Summary blib/lib/MaxMind/DB/Writer/Tree/Processor/VisualizeTree.pm »
1 package MaxMind::DB::Writer::Serializer;
2  
3 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 37 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 37 our $VERSION = '0.300004';
8  
9 37 require bytes;
10 37 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 37 ▶use Data::IEEE754 qw( pack_double_be pack_float_be );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 37 ▶use Encode qw( encode is_utf8 FB_CROAK );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 37 ▶use Math::Int128 qw( uint128_to_net );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 37 ▶use MaxMind::DB::Common 0.031000 qw( %TypeNameToNum );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 37 ▶use MaxMind::DB::Writer::Util qw( key_for_data );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 37 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 37 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 37 with 'MaxMind::DB::Role::Debugs';
21  
22 ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
23 37 ▶use constant DEBUG => $ENV{MAXMIND_DB_SERIALIZER_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
24 37 ▶use constant VERIFY => $ENV{MAXMIND_DB_SERIALIZER_VERIFY};
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 37 if (VERIFY) {
27 require MaxMind::DB::Reader::Decoder;
28 require Test::Deep::NoTest;
29 Test::Deep::NoTest->import(qw( cmp_details deep_diag ));
30 }
31  
32 37 if (DEBUG) {
33 binmode STDERR, ':encoding(UTF-8)' or die $!;
34 }
35  
36 has buffer => (
37 is => 'ro',
38 isa => 'ScalarRef[Str]',
39 init_arg => undef,
40 lazy => 1,
41 197 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 197 my $buffer = q{};
43 197 return \$buffer;
44 },
45 37 );
46  
47 37 has _map_key_type_callback => (
48 is => 'ro',
49 isa => 'CodeRef',
50 init_arg => 'map_key_type_callback',
51 required => 1,
52 );
53  
54 # This is settable so we can more easily test the encoding portion of the code
55 # without letting the deduplication interfere and turn a data item into a
56 # pointer. In normal use this should always be true.
57 37 has _deduplicate_data => (
58 is => 'ro',
59 isa => 'Bool',
60 default => 1,
61 );
62  
63 has _cache => (
64 traits => ['Hash'],
65 is => 'ro',
66 isa => 'HashRef',
67 init_arg => undef,
68 lazy => 1,
69 75 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 37 handles => {
71 _save_position => 'set',
72 _position_for_data => 'get',
73 },
74 );
75  
76 37 has _decoder => (
77 is => 'ro',
78 isa => 'MaxMind::DB::Reader::Decoder',
79 init_arg => undef,
80 lazy => 1,
81 builder => '_build_decoder',
82 );
83  
84 37 my $MinimumCacheableSize = 4;
85  
86 11911 ▶sub store_data {
Subroutine store_data: called CC=5 SCAR=16.1
Pod undocumented
87 11911 my $self = shift;
88 11911 my $type = shift;
89 11911 my $data = shift;
90 11911 my $member_type = shift;
91 11911 my $key_for_data = shift;
92  
93 11911 ▶ confess 'Cannot store an undef as data'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 11911 11911
94 unless defined $data;
95  
96 11911 $self->_debug_newline()
97 if DEBUG;
98 11911 $self->_debug_string( 'Storing type', $type )
99 if DEBUG;
100  
101 11911 ▶ return $self->_store_data( $type, $data, $member_type )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_should_cache_value($type, $data) 5166 6745 11911
102 unless $self->_should_cache_value( $type, $data );
103  
104 6745 ▶ $key_for_data //= key_for_data($data);
Condition66%2/3
$key_for_data //= key_for_data($data)
l!l&&r!l&&!r
169050550
MC/DC0%0/2
$key_for_data //= key_for_data($data)
$key_for_data key_for_data($data)
Truth table66%2/3
A //= B
A= $key_for_dataB= key_for_data($data)
ABresult
1X1
011
000
105  
106 6745 $self->_debug_string( 'Cache key', $key_for_data )
107 if DEBUG;
108  
109 6745 my $position = $self->_position_for_data($key_for_data);
110  
111 6745 ▶ if ( defined $position ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) { } 1289 5456 6745
112 1289 if (DEBUG) {
113 $self->_debug_string( 'Found data at position', $position );
114 $self->_debug_string( 'Storing pointer to', $position );
115 }
116  
117 1289 return $self->_store_data( pointer => $position );
118 }
119 else {
120 5456 my $stored_position
121 = $self->_store_data( $type, $data, $member_type );
122 5456 $self->_debug_string( 'Stored data at position', $stored_position )
123 if DEBUG;
124 5456 $self->_save_position( $key_for_data => $stored_position );
125  
126 5456 return $stored_position;
127 }
128 }
129  
130 37 if (VERIFY) {
131 0 ▶ around store_data => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
132 0 my $orig = shift;
133 0 my $self = shift;
134 0 my $type = shift;
135 0 my $data = shift;
136 0 my $member_type = shift;
137  
138 0 my $position = $self->$orig( $type, $data, $member_type );
139  
140 0 my $stored_data = $self->_decoder()->decode($position);
141 0 my ( $ok, $stack ) = cmp_details( $data, $stored_data );
142  
143 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
144 0 my $diag = deep_diag($stack);
145 0 die
146 "Data we just stored does not decode to value we expected:\n$diag\n";
147 }
148  
149 0 return $position;
150 };
151 }
152  
153 # These types never take more than 4 bytes to store.
154 37 my %NeverCache = map { $_ => 1 } qw(
155 int32
156 uint16
157 uint32
158 );
159  
160 11911 ▶sub _should_cache_value {
Subroutine _should_cache_value: called CC=6 SCAR=17.9
161 11911 my $self = shift;
162 11911 my $type = shift;
163 11911 my $data = shift;
164  
165 11911 ▶ return 0 unless $self->_deduplicate_data();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_deduplicate_data 131 11780 11911
166  
167 11780 ▶ if ( $NeverCache{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($NeverCache{$type}) 2796 8984 11780
168 2796 $self->_debug_string( 'Never cache type', $type )
169 if DEBUG;
170 2796 return 0;
171 }
172  
173 8984 ▶ if ( $type eq 'uint64' || $type eq 'uint128' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'uint64' or $type eq 'uint128') { } 241 8743 8984
elsif (ref $data) { } 2205 6538 8743
Condition100%3/3
$type eq "uint64" or $type eq "uint128"
l!l&&r!l&&!r
1381038743
MC/DC100%2/2
$type eq "uint64" or $type eq "uint128"
$type eq "uint64" $type eq "uint128"
Truth table100%3/3
A or B
A= $type eq "uint64"B= $type eq "uint128"
ABresult
1X1
011
000
174 241 ( my $non_zero = $data ) =~ s/^0+//;
175  
176 241 my $stored_bytes = ( length($non_zero) / 4 );
177  
178 241 $self->_debug_string( "Space needed for $type $data", $stored_bytes )
179 if DEBUG;
180  
181 # We can store four hex digits per byte. Once we strip leading zeros,
182 # we know how much space this number will take to store.
183 241 return $stored_bytes >= $MinimumCacheableSize;
184 }
185 elsif ( ref $data ) {
186 2205 $self->_debug_message('Always cache references')
187 if DEBUG;
188 2205 return 1;
189 }
190 else {
191 ## no critic (ProhibitCallsToUnexportedSubs)
192 6538 $self->_debug_string(
193 "Space needed for $type $data",
194 bytes::length $data
195 ) if DEBUG;
196  
197 6538 return bytes::length($data) >= $MinimumCacheableSize;
198 }
199 }
200  
201 11911 ▶sub _store_data {
Subroutine _store_data: called CC=1 SCAR=0.0
202 11911 my $self = shift;
203 11911 my $type = shift;
204 11911 my $data = shift;
205 11911 my $member_type = shift;
206  
207 ## no critic (ProhibitCallsToUnexportedSubs)
208 11911 my $current_position = bytes::length ${ $self->buffer() };
209  
210 11911 my $method = '_encode_' . $type;
211 11911 $self->$method( $data, $member_type );
212  
213 # We don't add 1 byte because the first byte we can point to is byte 0
214 # (not 1).
215 11910 return $current_position;
216 }
217  
218 37 my @pointer_thresholds = (
219 {
220 cutoff => 2**11,
221 offset => 0,
222 }
223 );
224 push @pointer_thresholds,
225 {
226 cutoff => 2**19 + $pointer_thresholds[-1]{cutoff},
227 offset => $pointer_thresholds[-1]{cutoff},
228 37 };
229 push @pointer_thresholds,
230 {
231 cutoff => 2**27 + $pointer_thresholds[-1]{cutoff},
232 offset => $pointer_thresholds[-1]{cutoff},
233 37 };
234 37 push @pointer_thresholds,
235 {
236 cutoff => 2**32,
237 offset => 0,
238 };
239  
240 ## no critic (ProhibitUnusedPrivateSubroutines)
241 1303 ▶sub _encode_pointer {
Subroutine _encode_pointer: called CC=4 SCAR=13.9
242 1303 my $self = shift;
243 1303 my $value = shift;
244  
245 1303 $self->_require_x_bits_unsigned_integer( 32, $value );
246  
247 my $ctrl_byte
248 1299 = ord( $self->_control_bytes( $TypeNameToNum{pointer}, 0 ) );
249  
250 1299 my @value_bytes;
251 1299 for my $n ( 0 .. 3 ) {
252 1312 ▶ if ( $value < $pointer_thresholds[$n]{cutoff} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $pointer_thresholds[$n]{"cutoff"}) 1299 13 1312
253  
254 1299 my $pack_method = '_pack_' . ( $n + 1 ) . '_byte_pointer';
255 @value_bytes = split //,
256 $self->$pack_method(
257 1299 $value - $pointer_thresholds[$n]{offset} );
258  
259 1299 ▶ if ( $n == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 3) { } 2 1297 1299
260 2 $ctrl_byte |= ( 3 << 3 );
261 }
262 else {
263 1297 $ctrl_byte |= ( $n << 3 ) | ord( shift @value_bytes );
264 }
265  
266 1299 last;
267 }
268 }
269  
270 1299 $self->_write_encoded_data( pack( 'C', $ctrl_byte ), @value_bytes );
271 }
272  
273 1292 ▶sub _pack_1_byte_pointer {
Subroutine _pack_1_byte_pointer: called CC=1 SCAR=0.0
274 1292 return pack( n => $_[1] );
275 }
276  
277 3 ▶sub _pack_2_byte_pointer {
Subroutine _pack_2_byte_pointer: called CC=1 SCAR=0.0
278 3 return substr( pack( N => $_[1] ), 1, 3 );
279 }
280  
281 2 ▶sub _pack_3_byte_pointer {
Subroutine _pack_3_byte_pointer: called CC=1 SCAR=0.0
282 2 return pack( N => $_[1] );
283 }
284  
285 2 ▶sub _pack_4_byte_pointer {
Subroutine _pack_4_byte_pointer: called CC=1 SCAR=0.0
286 2 return pack( N => $_[1] );
287 }
288  
289 4897 ▶sub _encode_utf8_string {
Subroutine _encode_utf8_string: called CC=1 SCAR=0.0
290 4897 my $self = shift;
291  
292 4897 my $string = shift;
293  
294 4897 $self->_simple_encode(
295 utf8_string => encode( 'UTF-8', $string, FB_CROAK ) );
296 }
297  
298 104 ▶sub _encode_double {
Subroutine _encode_double: called CC=1 SCAR=0.0
299 104 my $self = shift;
300  
301 $self->_write_encoded_data(
302 104 $self->_control_bytes( $TypeNameToNum{double}, 8, ),
303 pack_double_be(shift)
304 );
305 }
306  
307 105 ▶sub _encode_float {
Subroutine _encode_float: called CC=1 SCAR=0.0
308 105 my $self = shift;
309  
310 $self->_write_encoded_data(
311 105 $self->_control_bytes( $TypeNameToNum{float}, 4, ),
312 pack_float_be(shift)
313 );
314 }
315  
316 107 ▶sub _encode_bytes {
Subroutine _encode_bytes: called CC=2 SCAR=6.9
317 107 my $self = shift;
318  
319 107 my $bytes = shift;
320 107 ▶ die "You attempted to store a characters string ($bytes) as bytes"
Branch: 1 branch
BranchTrueFalseTotal
if &is_utf8($bytes) 1 106 107
321 if is_utf8($bytes);
322  
323 106 $self->_simple_encode( bytes => $bytes );
324 }
325  
326 254 ▶sub _encode_uint16 {
Subroutine _encode_uint16: called CC=1 SCAR=0.0
327 254 my $self = shift;
328  
329 254 $self->_encode_unsigned_int( 16 => @_ );
330 }
331  
332 2462 ▶sub _encode_uint32 {
Subroutine _encode_uint32: called CC=1 SCAR=0.0
333 2462 my $self = shift;
334  
335 2462 $self->_encode_unsigned_int( 32 => @_ );
336 }
337  
338 1977 ▶sub _encode_map {
Subroutine _encode_map: called CC=3 SCAR=11.0
339 1977 my $self = shift;
340 1977 my $map = shift;
341  
342 $self->_write_encoded_data(
343 1977 $self->_control_bytes( $TypeNameToNum{map}, scalar keys %{$map} ) );
344  
345 # We sort to make testing possible.
346 1977 for my $k ( sort keys %{$map} ) {
347 3450 $self->store_data( utf8_string => $k );
348  
349 3450 my $value_type = $self->_type_for_key( $k, $map->{$k} );
350 3450 my $array_value_type;
351 3450 ▶ if ( ref $value_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value_type) 232 3218 3450
352 232 ( $value_type, $array_value_type ) = @{$value_type};
353 }
354  
355 3450 $self->store_data( $value_type, $map->{$k}, $array_value_type );
356 }
357 }
358  
359 235 ▶sub _encode_array {
Subroutine _encode_array: called CC=3 SCAR=11.0
360 235 my $self = shift;
361 235 my $array = shift;
362 235 my $value_type = shift;
363  
364 235 ▶ die 'No value type for array!' unless defined $value_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_type 0 235 235
365  
366 $self->_write_encoded_data(
367 235 $self->_control_bytes( $TypeNameToNum{array}, scalar @{$array} ) );
368  
369 235 $self->store_data( $value_type, $_ ) for @{$array};
370 }
371  
372 3451 ▶sub _type_for_key {
Subroutine _type_for_key: called CC=2 SCAR=6.9
373 3451 my $self = shift;
374 3451 my $key = shift;
375 3451 my $value = shift;
376  
377 3451 my $type = $self->_map_key_type_callback->( $key, $value );
378  
379 3451 ▶ die qq{Could not determine the type for map key "$key"}
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 3450 3451
380 unless $type;
381  
382 3450 return $type;
383 }
384  
385 108 ▶sub _encode_int32 {
Subroutine _encode_int32: called CC=1 SCAR=0.0
386 108 my $self = shift;
387 108 my $value = shift;
388  
389 108 my $encoded_value = pack( 'N!' => $value );
390 108 $encoded_value =~ s/^\x00+//;
391  
392 $self->_write_encoded_data(
393 $self->_control_bytes(
394 108 $TypeNameToNum{int32}, length($encoded_value)
395 ),
396 $encoded_value,
397 );
398 }
399  
400 153 ▶sub _encode_uint64 {
Subroutine _encode_uint64: called CC=1 SCAR=0.0
401 153 my $self = shift;
402  
403 153 $self->_encode_unsigned_int( 64 => @_ );
404 }
405  
406 121 ▶sub _encode_uint128 {
Subroutine _encode_uint128: called CC=1 SCAR=0.0
407 121 my $self = shift;
408  
409 121 $self->_encode_unsigned_int( 128 => @_ );
410 }
411  
412 98 ▶sub _encode_boolean {
Subroutine _encode_boolean: called CC=2 SCAR=6.9
413 98 my $self = shift;
414 98 my $value = shift;
415  
416 $self->_write_encoded_data(
417 98 ▶ $self->_control_bytes( $TypeNameToNum{boolean}, $value ? 1 : 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 49 49 98
418 }
419  
420 1 ▶sub _encode_end_marker {
Subroutine _encode_end_marker: called CC=1 SCAR=0.0
421 1 my $self = shift;
422  
423 1 $self->_simple_encode( 'end_marker', q{} );
424 }
425  
426 5004 ▶sub _simple_encode {
Subroutine _simple_encode: called CC=1 SCAR=0.0
427 5004 my $self = shift;
428 5004 my $type = shift;
429 5004 my $value = shift;
430  
431 $self->_write_encoded_data(
432 5004 $self->_control_bytes( $TypeNameToNum{$type}, length($value) ),
433 $value,
434 );
435 }
436  
437 2990 ▶sub _encode_unsigned_int {
Subroutine _encode_unsigned_int: called CC=2 SCAR=6.9
438 2990 my $self = shift;
439 2990 my $bits = shift;
440 2990 my $value = shift;
441  
442 2990 $self->_require_x_bits_unsigned_integer( $bits, $value );
443  
444 2982 my $encoded_value;
445 2982 ▶ if ( $bits >= 64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= 64) { } 270 2712 2982
446 270 $encoded_value = uint128_to_net($value);
447 }
448 else {
449 2712 $encoded_value = pack( N => $value );
450 }
451  
452 2982 $encoded_value =~ s/^\x00+//;
453  
454 $self->_write_encoded_data(
455 $self->_control_bytes(
456 2982 $TypeNameToNum{ 'uint' . $bits },
457 length($encoded_value)
458 ),
459 $encoded_value,
460 );
461 }
462 ## use critic
463  
464 {
465 37 my %Max = (
466 16 => ( 2**16 ) - 1,
467 32 => ( 2**32 ) - 1,
468 );
469  
470 4293 ▶ sub _require_x_bits_unsigned_integer {
Subroutine _require_x_bits_unsigned_integer: called CC=10 SCAR=23.0
471 4293 my $self = shift;
472 4293 my $bits = shift;
473 4293 my $value = shift;
474  
475 4293 my $type_description = "unsigned $bits-bit integer";
476  
477 4293 ▶ die "You cannot encode undef as an $type_description."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 4290 4293
478 unless defined $value;
479  
480 4290 ▶ if ( $bits >= 64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits >= 64) { } 273 4017 4290
481 273 ▶ if ( blessed $value && $value->isa('Math::UInt128') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa('Math::UInt128')) { } 231 42 273
Condition100%3/3
builtin::blessed($value) and $value->isa("Math::UInt128")
!ll&&!rl&&r
240231
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Math::UInt128")
builtin::blessed($value) $value->isa("Math::UInt128")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Math::UInt128")
ABresult
0X0
100
111
482 231 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
if $bits != 128 and $value / 2 ** $bits > 1 1 230 231
Condition100%3/3
$bits != 128 and $value / 2 ** $bits > 1
!ll&&!rl&&r
971331
MC/DC100%2/2
$bits != 128 and $value / 2 ** $bits > 1
$bits != 128 $value / 2 ** $bits > 1
Truth table100%3/3
A and B
A= $bits != 128B= $value / 2 ** $bits > 1
ABresult
0X0
100
111
483 "You cannot encode $value as an $type_description. It is too big."
484 if $bits != 128 && $value / ( 2**$bits ) > 1;
485 }
486 else {
487 42 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^[0-9]+$/ 2 40 42
488 "You cannot encode $value as an $type_description. It is not an unsigned integer number."
489 unless $value =~ /^[0-9]+$/;
490 }
491 }
492 else {
493 4017 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^[0-9]+$/ 4 4013 4017
494 "You cannot encode $value as an $type_description. It is not an unsigned integer number."
495 unless $value =~ /^[0-9]+$/;
496  
497 die
498 "You cannot encode $value as an $type_description. It is too big."
499 4013 ▶ if $value > $Max{$bits};
Branch: 1 branch
BranchTrueFalseTotal
if $value > $Max{$bits} 2 4011 4013
500  
501 }
502 }
503 }
504  
505 {
506 # The value is the threshold for needing another byte to store the size
507 # value. In other words, a size of 28 fits in one byte, a size of 29 needs
508 # two bytes.
509 37 my %ThresholdSize = (
510 1 => 29,
511 2 => 29 + 256,
512 3 => 29 + 256 + 2**16,
513 4 => 29 + 256 + 2**16 + 2**24,
514 );
515  
516 11912 ▶ sub _control_bytes {
Subroutine _control_bytes: called CC=7 SCAR=19.5
517 11912 my $self = shift;
518 11912 my $type = shift;
519 11912 my $size = shift;
520  
521 11912 ▶ if ( $size >= $ThresholdSize{4} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size >= $ThresholdSize{4}) 1 11911 11912
522 die "Cannot store $size bytes - max size is "
523 1 . ( $ThresholdSize{4} - 1 )
524 . ' bytes';
525 }
526  
527 11911 my $template = 'C';
528  
529 11911 my $first_byte;
530 my $second_byte;
531 11911 ▶ if ( $type < 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type < 8) { } 11094 817 11911
532 11094 $first_byte = ( $type << 5 );
533 }
534 else {
535 817 $first_byte = ( $TypeNameToNum{extended} << 5 );
536 817 $second_byte = $type - 7;
537 817 $template .= 'C';
538 }
539  
540 11911 my $leftover_size;
541 ## no critic (ControlStructures::ProhibitCascadingIfElse)
542 11911 ▶ if ( $size < $ThresholdSize{1} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($size < $ThresholdSize{'1'}) { } 9844 2067 11911
elsif ($size <= $ThresholdSize{'2'}) { } 4 2063 2067
elsif ($size <= $ThresholdSize{'3'}) { } 2060 3 2063
elsif ($size <= $ThresholdSize{'4'}) { } 3 0 3
543 9844 $first_byte |= $size;
544 }
545 elsif ( $size <= $ThresholdSize{2} ) {
546 4 $first_byte |= 29;
547 4 $leftover_size = $size - $ThresholdSize{1};
548 4 $template .= 'C';
549 }
550 elsif ( $size <= $ThresholdSize{3} ) {
551 2060 $first_byte |= 30;
552 2060 $leftover_size = $size - $ThresholdSize{2};
553 2060 $template .= 'n';
554 }
555 elsif ( $size <= $ThresholdSize{4} ) {
556 3 $first_byte |= 31;
557  
558 # There's no nice way to express "pack an integer into 24 bits"
559 # using a pack template, so we'll just pack it here and then chop
560 # off the first byte.
561 $leftover_size
562 3 = substr( pack( N => $size - $ThresholdSize{3} ), 1 );
563 3 $template .= 'a3';
564 }
565  
566 return pack(
567 11911 $template => grep { defined } (
568 $first_byte,
569 $second_byte,
570 $leftover_size,
571 )
572 );
573 }
574 }
575  
576 37 ▶sub _write_encoded_data {
Subroutine _write_encoded_data: called CC=2 SCAR=6.9
577 11911 my $self = shift;
578  
579 11911 ${ $self->buffer() } .= $_ for @_;
580  
581 11911 $self->_debug_binary( 'Wrote', join q{}, @_ )
582 if DEBUG;
583  
584 11911 return;
585 }
586  
587 0 ▶sub _build_decoder {
Subroutine _build_decoder: not called CC=2 SCAR=17.9
588 0 my $self = shift;
589  
590 ## no critic (InputOutput::RequireBriefOpen)
591 0 ▶ open my $fh, '<:raw', $self->buffer() or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $self->buffer or die $! 0 0 0
592  
593 0 return MaxMind::DB::Reader::Decoder->new(
594 data_source => $fh,
595 );
596 }
597  
598 37 __PACKAGE__->meta()->make_immutable();
599  
600 37 1;
« blib/lib/MaxMind/DB/Writer/Tree.pm ↑ Summary blib/lib/MaxMind/DB/Writer/Tree/Processor/VisualizeTree.pm »