Reader/Role/HasMetadata.pm

filter: Statementstmt 100% 49 / 49 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 100% 62 / 62 CC 2 SCAR 1.5
CC
2
Cov
100%
_build_metadata
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« Reader/Role/Reader.pm ↑ Summary Reader/Data/Container.pm »
1 package MaxMind::DB::Reader::Role::HasMetadata;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 require bytes;
9  
10 21 our $VERSION = '1.000014';
11 21 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use List::AllUtils qw( min );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use MaxMind::DB::Reader::Decoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use MaxMind::DB::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use MaxMind::DB::Types qw( Int Metadata );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 21 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 21 with 'MaxMind::DB::Reader::Role::Sysreader';
20  
21 21 has metadata => (
22 is => 'ro',
23 isa => Metadata,
24 init_arg => undef,
25 lazy => 1,
26 builder => '_build_metadata',
27 handles => [
28 'binary_format_major_version',
29 'binary_format_minor_version',
30 'build_epoch',
31 'database_type',
32 'description',
33 'ip_version',
34 'languages',
35 'node_count',
36 'record_size',
37 ],
38 );
39  
40 21 has _data_section_end => (
41 is => 'ro',
42 writer => '_set_data_section_end',
43 isa => Int,
44 init_arg => undef,
45 );
46  
47 21 my $MetadataStartMarker = "\xab\xcd\xefMaxMind.com";
48  
49 21 ▶sub _build_metadata {
Subroutine _build_metadata: called CC=2 SCAR=6.9
50 21 my $self = shift;
51  
52 # We need to make sure that whatever chunk we read will have the metadata
53 # in it. The description metadata key is a hash of descriptions, one per
54 # language. The description could be something verbose like "GeoIP 2.0
55 # City Database, Multilingual - English, Chinese (Taiwan), Chinese
56 # (China), French, German, Portuguese" (but with c. 20 languages). That
57 # comes out to about 250 bytes _per key_. Multiply that by 20 languages,
58 # and the description alon ecould use up about 5k. The other keys in the
59 # metadata are very, very tiny.
60 #
61 # Given all this, reading 20k seems fairly future-proof. We'd have to have
62 # extremely long descriptions or descriptions in 80 languages before this
63 # became too long.
64  
65 21 my $size = $self->_data_source_size;
66  
67 21 my $last_bytes = min( $size, 20 * 1024 );
68 21 my $last_block = q{};
69 21 $self->_read( \$last_block, -$last_bytes, $last_bytes, 'seek from end' );
70  
71 21 my $start = ( $size - $last_bytes )
72 + rindex( $last_block, $MetadataStartMarker );
73  
74 21 ▶ confess 'Error opening database file "'
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 1 20 21
75 . $self->file
76 . q{": The MaxMind DB file contains invalid metadata.}
77 if $start < 0;
78  
79 # XXX - this is really gross but I couldn't come up with a better way to
80 # factor this out that doesn't involve either looking for the metadata
81 # marker multiple times _or_ storing the whole metadata raw chunk in
82 # memory so we can calculate this later
83 20 $self->_set_data_section_end( $size - ( $last_bytes - $start ) );
84  
85 ## no critic (Subroutines::ProhibitCallsToUnexportedSubs)
86 20 $start += bytes::length($MetadataStartMarker);
87 ## use critic
88  
89 20 my $raw = MaxMind::DB::Reader::Decoder->new(
90 data_source => $self->data_source,
91 pointer_base => $start,
92 )->decode($start);
93  
94 20 my $metadata = MaxMind::DB::Metadata->new($raw);
95  
96 20 return $metadata;
97 }
98  
99 21 1;
« Reader/Role/Reader.pm ↑ Summary Reader/Data/Container.pm »