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

filter: Statementstmt 96.3% 182 / 189 Branchbran 78.5% 44 / 56 Conditioncond 55.5% 15 / 27 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 94.5% 35 / 37 Podpod 77.7% 7 / 9 totaltotal 87.0% 288 / 331 CC 40 SCAR 16.7
CC
40
Cov
89%
__ANON__
10.9
new_from_frozen_tree
10.7
_merge_strategy
9.2
CRAP 5.3
↑ Summary blib/lib/MaxMind/DB/Writer/Serializer.pm »
1 package MaxMind::DB::Writer::Tree;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 our $VERSION = '0.300004';
9  
10 18 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Math::Int64 0.51;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Math::Int128 0.21 qw( uint128 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use MaxMind::DB::Common 0.031003 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 DATA_SECTION_SEPARATOR
15 METADATA_MARKER
16 18 );
17 18 ▶use MaxMind::DB::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use MaxMind::DB::Writer::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use MaxMind::DB::Writer::Util qw( key_for_data );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use MooseX::Params::Validate qw( validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use Sereal::Decoder qw( decode_sereal );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use Sereal::Encoder qw( encode_sereal );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 18 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 ## no critic (ProhibitCallsToUnexportedSubs)
31 18 XSLoader::load( __PACKAGE__, $VERSION );
32 ## use critic
33  
34 18 warnings::warnif(
35 'deprecated',
36 'MaxMind::DB::Writer::Tree is deprecated and should no longer be used',
37 );
38  
39 18 has ip_version => (
40 is => 'ro',
41 isa => 'Int',
42 required => 1,
43 );
44  
45 #<<<
46 my $RecordSizeType = subtype
47 as 'Int',
48 208 ▶ where { ( $_ % 4 == 0 ) && $_ >= 24 && $_ <= 128 },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
$_ % 4 == 0 and $_ >= 24 and $_ <= 128 208 0 208
Condition33%1/3
$_ % 4 == 0 and $_ >= 24
!ll&&!rl&&r
00208
MC/DC0%0/2
$_ % 4 == 0 and $_ >= 24
$_ % 4 == 0 $_ >= 24
Truth table33%1/3
A and B
A= $_ % 4 == 0B= $_ >= 24
ABresult
0X0
100
111
49 0 ▶ message {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 'The record size must be a number from 24-128 that is divisible by 4';
51 18 };
52 #>>>
53  
54 18 has record_size => (
55 is => 'ro',
56 isa => $RecordSizeType,
57 required => 1,
58 );
59  
60 18 my $MergeStrategyEnum
61 = enum( [qw( add-only-if-parent-exists none recurse toplevel )] );
62  
63 18 has merge_strategy => (
64 is => 'ro',
65 isa => $MergeStrategyEnum,
66 lazy => 1,
67 default => 'none',
68 );
69  
70 18 has remove_reserved_networks => (
71 is => 'ro',
72 isa => 'Bool',
73 default => 1,
74 );
75  
76 18 has _tree => (
77 is => 'ro',
78 lazy => 1,
79 builder => '_build_tree',
80 predicate => '_has_tree',
81 );
82  
83 # All records in the tree will point to a value of this type in the data
84 # section.
85 18 has _root_data_type => (
86 is => 'ro',
87 isa => 'Str', # XXX - should make sure it's valid type
88 init_arg => 'root_data_type',
89 default => 'map',
90 );
91  
92 18 has map_key_type_callback => (
93 is => 'ro',
94 isa => 'CodeRef',
95 required => 1,
96 );
97  
98 18 has database_type => (
99 is => 'ro',
100 isa => 'Str',
101 required => 1,
102 );
103  
104 has languages => (
105 is => 'ro',
106 isa => 'ArrayRef[Str]',
107 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 18 );
109  
110 18 has description => (
111 is => 'ro',
112 isa => 'HashRef[Str]',
113 required => 1,
114 );
115  
116 18 has alias_ipv6_to_ipv4 => (
117 is => 'ro',
118 isa => 'Bool',
119 default => 0,
120 );
121  
122 18 has _serializer => (
123 is => 'ro',
124 isa => 'MaxMind::DB::Writer::Serializer',
125 init_arg => undef,
126 lazy => 1,
127 builder => '_build_serializer',
128 );
129  
130 # This is an attribute so that we can explicitly set it from test code when we
131 # want to compare the binary output for two trees that we want to be
132 # identical.
133 has _build_epoch => (
134 is => 'rw',
135 writer => '_set_build_epoch',
136 isa => 'Int',
137 lazy => 1,
138 39 ▶ default => sub { time() },
Subroutine __ANON__: called CC=1 SCAR=0.0
139 18 );
140  
141 203 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=8 SCAR=23.9
142 203 my $orig = shift;
143 203 my $class = shift;
144 203 my $args = $class->$orig(@_);
145  
146 203 ▶ if ( exists $args->{merge_record_collisions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"merge_record_collisions"}) 2 201 203
147 2 warn
148 'merge_record_collisions is deprecated. Use merge_strategy instead.';
149 }
150  
151 203 ▶ return $args unless exists $args->{merge_record_collisions};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"merge_record_collisions"} 201 2 203
152  
153 2 my $merge_record_collisions = delete $args->{merge_record_collisions};
154  
155 2 ▶ if ( $args->{merge_strategy}
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"merge_strategy"} and not $args->{"merge_strategy"} eq "none" ^^ $merge_record_collisions) 0 2 2
Condition14%1/7
$args->{"merge_strategy"} and not $args->{"merge_strategy"} eq "none" ^^ $merge_record_collisions
!ll&&!rl&&r
200
$args->{"merge_strategy"} eq "none" ^^ $merge_record_collisions
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/3
$args->{"merge_strategy"} and not $args->{"merge_strategy"} eq "none" ^^ $merge_record_collisions
$args->{"merge_strategy"} $args->{"merge_strategy"} eq "none" $merge_record_collisions
Truth table20%1/5
A and not(B ^^ C)
A= $args->{"merge_strategy"}B= $args->{"merge_strategy"} eq "none"C= $merge_record_collisions
ABCresult
0XX0
1100
1010
1111
1001
156 && !( $args->{merge_strategy} eq 'none' xor $merge_record_collisions )
157 ) {
158 die sprintf(
159 'merge_strategy cannot be "%s" if merge_record_collisions is "%s"',
160 $args->{merge_strategy}, $args->{merge_record_collisions}
161 0 );
162 }
163  
164 $args->{merge_strategy} //=
165 2 ▶ $merge_record_collisions ? 'toplevel' : 'none';
Branch: 1 branch
BranchTrueFalseTotal
$merge_record_collisions ? : 2 0 2
Condition50%1/2
$args->{"merge_strategy"} //= $merge_record_collisions ? "toplevel" : "none"
l!l
02
MC/DC0%0/1
$args->{"merge_strategy"} //= $merge_record_collisions ? "toplevel" : "none"
$args->{"merge_strategy"}
Truth table50%1/2
A
A= $args->{"merge_strategy"}
Aresult
11
00
166  
167 2 return $args;
168 18 };
169  
170 # The XS code expects $self->{_tree} to be populated.
171 203 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
172 203 $_[0]->_tree();
173 }
174  
175 174 ▶sub _build_tree {
Subroutine _build_tree: called CC=1 SCAR=0.0
176 174 my $self = shift;
177  
178 174 return _create_tree(
179 $self->ip_version,
180 $self->record_size,
181 $self->merge_strategy,
182 $self->alias_ipv6_to_ipv4,
183 $self->remove_reserved_networks,
184 );
185 }
186  
187 2 ▶sub merge_record_collisions {
Subroutine merge_record_collisions: called CC=1 SCAR=0.0
Pod documented
188 2 warn
189 'merge_record_collisions is deprecated and will be removed in a future release';
190  
191 2 return $_[0]->merge_strategy ne 'none';
192 }
193  
194 105270 ▶sub insert_network {
Subroutine insert_network: called CC=7 SCAR=19.6
Pod documented
195 105270 my $self = shift;
196 105270 my $network = shift;
197 105270 my $data = shift;
198 105270 ▶ my $args = shift // {};
Condition100%2/2
shift() // {}
l!l
9105261
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
199  
200 105270 my ( $ip_address, $prefix_length ) = split qr{/}, $network, 2;
201  
202 105270 ▶ if ( !defined $prefix_length
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prefix_length or int $prefix_length != $prefix_length or $prefix_length < 0 or $prefix_length > 128) 2 105268 105270
Condition66%6/9
not defined $prefix_length or int $prefix_length != $prefix_length or $prefix_length < 0 or $prefix_length > 128
l!l&&r!l&&!r
11105268
not defined $prefix_length or int $prefix_length != $prefix_length or $prefix_length < 0
l!l&&r!l&&!r
01105269
not defined $prefix_length or int $prefix_length != $prefix_length
l!l&&r!l&&!r
00105270
MC/DC50%2/4
not defined $prefix_length or int $prefix_length != $prefix_length or $prefix_length < 0 or $prefix_length > 128
not defined $prefix_length int $prefix_length != $prefix_length $prefix_length < 0 $prefix_length > 128
Truth table60%3/5
not(A) or B or C or D
A= not defined $prefix_lengthB= int $prefix_length != $prefix_lengthC= $prefix_length < 0D= $prefix_length > 128
ABCDresult
1XXX1
01XX1
001X1
00011
00000
203 || int($prefix_length) != $prefix_length
204 || $prefix_length < 0
205 || $prefix_length > 128 ) {
206 2 die "Invalid network inserted: $network";
207 }
208  
209 105268 ▶ my $merge_strategy = %{$args} ? $self->_merge_strategy($args) : q{};
Branch: 1 branch
BranchTrueFalseTotal
%{$args;} ? : 9 105259 105268
210  
211 105268 $self->_insert_network(
212 $ip_address,
213 $prefix_length,
214 key_for_data($data),
215 $data,
216 $merge_strategy,
217 );
218 105254 return;
219 }
220  
221 849 ▶sub insert_range {
Subroutine insert_range: called CC=3 SCAR=11.0
Pod documented
222 849 my $self = shift;
223 849 my $start_ip_address = shift;
224 849 my $end_ip_address = shift;
225 849 my $data = shift;
226 849 ▶ my $args = shift // {};
Condition100%2/2
shift() // {}
l!l
10839
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
227  
228 849 ▶ my $merge_strategy = %{$args} ? $self->_merge_strategy($args) : q{};
Branch: 1 branch
BranchTrueFalseTotal
%{$args;} ? : 10 839 849
229  
230 849 $self->_insert_range(
231 $start_ip_address,
232 $end_ip_address,
233 key_for_data($data),
234 $data,
235 $merge_strategy,
236 );
237 846 return;
238 }
239  
240 19 ▶sub _merge_strategy {
Subroutine _merge_strategy: called CC=9 SCAR=22.2
241 19 my $self = shift;
242 19 my $args = shift;
243  
244 19 ▶ if ( ( $args->{insert_only_if_parent_exists} ? 1 : 0 )
Branch: 4 branches
BranchTrueFalseTotal
if (($args->{"insert_only_if_parent_exists"} ? 1 : 0) + ($args->{"force_overwrite"} ? 1 : 0) + ($args->{"merge_strategy"} ? 1 : 0) > 1) 0 19 19
$args->{'insert_only_if_parent_exists'} ? : 2 17 19
$args->{'force_overwrite'} ? : 2 17 19
$args->{'merge_strategy'} ? : 15 4 19
245 + ( $args->{force_overwrite} ? 1 : 0 )
246 + ( $args->{merge_strategy} ? 1 : 0 ) > 1 ) {
247 0 die 'Only one of merge_strategy, force_overwrite, or '
248 . 'insert_only_if_parent_exists can be set at a time';
249 }
250  
251 19 ▶ if ( $args->{insert_only_if_parent_exists} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"insert_only_if_parent_exists"}) 2 17 19
252 2 warn 'The argument insert_only_if_parent_exists is deprecated. '
253 . 'Use the merge_strategy "add-if-parent-exists" instead.';
254 2 return 'add-only-if-parent-exists';
255 }
256  
257 17 ▶ if ( $args->{force_overwrite} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"force_overwrite"}) 2 15 17
258 2 warn 'The argument force_overwrite is deprecated. '
259 . 'Use the merge_strategy "none" instead.';
260 2 return 'none';
261 }
262  
263 15 ▶ return undef unless $args->{merge_strategy};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"merge_strategy"} 0 15 15
264  
265 die "Unknown merge strategy: $args->{merge_strategy}"
266 15 ▶ unless $MergeStrategyEnum->check( $args->{merge_strategy} );
Branch: 1 branch
BranchTrueFalseTotal
unless $MergeStrategyEnum->check($args->{"merge_strategy"}) 0 15 15
267  
268 15 return $args->{merge_strategy};
269 }
270  
271 2 ▶sub remove_network {
Subroutine remove_network: called CC=1 SCAR=0.0
Pod documented
272 2 my $self = shift;
273 2 my $network = shift;
274 2 my ( $ip_address, $prefix_length ) = split qr{/}, $network, 2;
275  
276 2 $self->_remove_network(
277 $ip_address,
278 $prefix_length,
279 );
280  
281 2 return;
282 }
283  
284 35 ▶sub _build_serializer {
Subroutine _build_serializer: called CC=1 SCAR=0.0
285 35 my $self = shift;
286  
287 35 return MaxMind::DB::Writer::Serializer->new(
288 map_key_type_callback => $self->map_key_type_callback(),
289 );
290 }
291  
292 37 ▶sub write_tree {
Subroutine write_tree: called CC=1 SCAR=0.0
Pod documented
293 37 my $self = shift;
294 37 my $output = shift;
295  
296 37 $self->_write_search_tree(
297 $output,
298 $self->_root_data_type(),
299 $self->_serializer(),
300 );
301  
302 $output->print(
303 DATA_SECTION_SEPARATOR,
304 37 ${ $self->_serializer()->buffer() },
305 METADATA_MARKER,
306 $self->_encoded_metadata(),
307 );
308 }
309  
310 {
311 18 my %key_types = (
312 binary_format_major_version => 'uint16',
313 binary_format_minor_version => 'uint16',
314 build_epoch => 'uint64',
315 database_type => 'utf8_string',
316 description => 'map',
317 ip_version => 'uint16',
318 languages => [ 'array', 'utf8_string' ],
319 node_count => 'uint32',
320 record_size => 'uint16',
321 );
322  
323 382 ▶ my $type_callback = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
324 382 ▶ return $key_types{ $_[0] } || 'utf8_string';
Condition100%2/2
$key_types{$_[0]} || 'utf8_string'
l!l
33349
MC/DC100%1/1
$key_types{$_[0]} || 'utf8_string'
$key_types{$_[0]}
Truth table100%2/2
A
A= $key_types{$_[0]}
Aresult
11
00
325 18 };
326  
327 37 ▶ sub _encoded_metadata {
Subroutine _encoded_metadata: called CC=1 SCAR=0.0
328 37 my $self = shift;
329  
330 37 my $metadata = MaxMind::DB::Metadata->new(
331 binary_format_major_version => 2,
332 binary_format_minor_version => 0,
333 build_epoch => uint128( $self->_build_epoch() ),
334 database_type => $self->database_type(),
335 description => $self->description(),
336 ip_version => $self->ip_version(),
337 languages => $self->languages(),
338 node_count => $self->node_count(),
339 record_size => $self->record_size(),
340 );
341  
342 37 my $serializer = MaxMind::DB::Writer::Serializer->new(
343 map_key_type_callback => $type_callback,
344 );
345  
346 37 $serializer->store_data( 'map', $metadata->metadata_to_encode() );
347  
348 37 return ${ $serializer->buffer() };
349 }
350 }
351  
352 {
353 18 my %do_not_freeze = map { $_ => 1 } qw(
354 map_key_type_callback
355 _tree
356 );
357  
358 30 ▶ sub freeze_tree {
Subroutine freeze_tree: called CC=4 SCAR=13.9
Pod documented
359 30 my $self = shift;
360 30 my $filename = shift;
361  
362 30 my %constructor_params;
363 30 for my $attr ( $self->meta()->get_all_attributes() ) {
364 390 ▶ next unless $attr->init_arg();
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->init_arg 30 360 390
365 360 ▶ next if $do_not_freeze{ $attr->name() };
Branch: 1 branch
BranchTrueFalseTotal
if $do_not_freeze{$attr->name} 60 300 360
366  
367 300 my $reader = $attr->get_read_method();
368 300 $constructor_params{ $attr->init_arg() } = $self->$reader();
369 }
370  
371 30 my $frozen = encode_sereal( \%constructor_params );
372 30 $self->_freeze_tree( $filename, $frozen, length $frozen );
373  
374 30 return;
375 }
376 }
377  
378 18 ▶sub new_from_frozen_tree {
Subroutine new_from_frozen_tree: called CC=10 SCAR=23.7
Pod documented
379 29 my $class = shift;
380 my (
381 29 $filename, $callback, $database_type, $description, $merge_strategy,
382 $record_size
383 )
384 = validated_list(
385 \@_,
386 filename => { isa => 'Str' },
387 map_key_type_callback => { isa => 'CodeRef' },
388 database_type => { isa => 'Str', optional => 1 },
389 description => { isa => 'HashRef[Str]', optional => 1 },
390 merge_strategy => { isa => $MergeStrategyEnum, optional => 1 },
391 record_size => { isa => $RecordSizeType, optional => 1 },
392 );
393  
394 ## no critic (InputOutput::RequireBriefOpen)
395 29 ▶ open my $fh, '<:raw', $filename or die $!;
Branch: 1 branch
BranchTrueFalseTotal
&MaxMind::DB::Writer::Tree::open(my $fh, "<:raw", $filename) or die $! 29 0 29
396 29 my $packed_params_size;
397 29 ▶ unless ( read( $fh, $packed_params_size, 4 ) == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&MaxMind::DB::Writer::Tree::read($fh, \$packed_params_size, 4) == 4) 0 29 29
398 0 die "Could not read 4 bytes from $filename: $!";
399 }
400  
401 29 my $params_size = unpack( 'L', $packed_params_size );
402 29 my $frozen_params;
403 29 ▶ unless ( read( $fh, $frozen_params, $params_size ) == $params_size ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&MaxMind::DB::Writer::Tree::read($fh, \$frozen_params, $params_size) == $params_size) 0 29 29
404 0 die "Could not read $params_size bytes from $filename: $!";
405 }
406 29 ▶ close $fh or die $!;
Branch: 1 branch
BranchTrueFalseTotal
&MaxMind::DB::Writer::Tree::close($fh) or die $! 29 0 29
407  
408 29 my $params = decode_sereal($frozen_params);
409  
410 29 ▶ $params->{database_type} = $database_type if defined $database_type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $database_type 4 25 29
411 29 ▶ $params->{description} = $description if defined $description;
Branch: 1 branch
BranchTrueFalseTotal
if defined $description 4 25 29
412 29 ▶ $params->{record_size} = $record_size if defined $record_size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $record_size 5 24 29
413  
414 29 ▶ if ( defined $merge_strategy ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $merge_strategy) { } 12 17 29
elsif ($params->{'merge_record_collisions'}) { } 0 17 17
415 12 $params->{merge_strategy} = $merge_strategy;
416 }
417 elsif ( $params->{merge_record_collisions} ) {
418  
419 # This is for backwards compatibility with frozen trees created before
420 # "merge_strategy" was added.
421 0 $params->{merge_strategy} = 'toplevel';
422 }
423  
424 my $tree = _thaw_tree(
425 $filename,
426 $params_size + 4,
427 29 @{$params}{
428 29 qw(
429 ip_version
430 record_size
431 merge_strategy
432 alias_ipv6_to_ipv4
433 remove_reserved_networks
434 )
435 },
436 );
437  
438 return $class->new(
439 29 %{$params},
440 map_key_type_callback => $callback,
441 _tree => $tree,
442 );
443 }
444  
445 203 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.1
Pod undocumented
446 203 my $self = shift;
447  
448 203 ▶ $self->_free_tree()
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_tree 203 0 203
449 if $self->_has_tree();
450  
451 203 return;
452 }
453  
454 18 __PACKAGE__->meta()->make_immutable();
455  
456 18 1;
457  
458 # ABSTRACT: DEPRECATED Tree representing a MaxMind DB database in memory - then write it to a file
459  
460 __END__
↑ Summary blib/lib/MaxMind/DB/Writer/Serializer.pm »