File Coverage

blib/lib/MaxMind/DB/Writer/Util.pm
Criterion Covered Total %
statement 20 20 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 29 30 96.6


line stmt bran cond sub pod time code
1             package MaxMind::DB::Writer::Util;
2              
3 38     38   6641 use strict;
  38         77  
  38         1715  
4 38     38   204 use warnings;
  38         69  
  38         6982  
5              
6             our $VERSION = '0.300004';
7              
8 38     38   31181 use Digest::SHA qw( sha1_base64 );
  38         162729  
  38         4443  
9 38     38   960 use Encode qw( encode );
  38         22068  
  38         3216  
10 38     38   253 use Sereal::Encoder 3.002 qw( sereal_encode_with_object );
  38         1077  
  38         4195  
11              
12 38     38   272 use Exporter qw( import );
  38         117  
  38         9680  
13             our @EXPORT_OK = qw( key_for_data );
14              
15             {
16             # Although this mostly works fine when canonical and canonical_refs are
17             # enabled, it is still somewhat broken. See:
18             #
19             # https://metacpan.org/pod/distribution/Sereal-Encoder/lib/Sereal/Encoder.pm#CANONICAL-REPRESENTATION
20             #
21             # The arrays in the example, for instance, would have distinct keys
22             # despite being structurally equivalent. Requires Sereal 3.002.
23             my $Encoder = Sereal::Encoder->new(
24             {
25             canonical => 1,
26             canonical_refs => 1,
27             }
28             );
29              
30             sub key_for_data {
31              
32             # We need to use sha1 because the Sereal structure has \0 bytes which
33             # confuse the C code. As a bonus, this makes the keys smaller so they
34             # take up less space. As an un-bonus, this makes the code a little
35             # slower.
36 111272 100   111272 0 2676630 my $key
37             = ref $_[0]
38             ? sereal_encode_with_object( $Encoder, $_[0] )
39             : encode( 'UTF-8', $_[0] );
40              
41 111272         4082101 return sha1_base64($key);
42             }
43             }
44              
45             1;