blib/lib/Sereal/Splitter.pm

filter: Statementstmt 100% 54 / 54 Branchbran 62.5% 5 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 91.2% 73 / 80 CC 6 SCAR 7.7
CC
6
Cov
92%
create_header_data_template
4.1
new
3.0
BEGIN
1.0
CRAP 2.2
« blib/lib/Sereal/Splitter/Constants.pm ↑ Summary const-c.inc »
1 package Sereal::Splitter;
2  
3 =head1 NAME
4  
5 Sereal::Splitter - splits a Sereal blob in chunks of roughly the same size
6  
7 =head1 SYNOPSIS
8  
9 use Sereal::Splitter qw(SRL_ZLIB create_header_data_template);
10  
11 my $splitter = Sereal::Splitter->new(
12 { input => $data, chunk_size => 1, compress => SRL_ZLIB,
13 header_data_template => create_header_data_template(
14 { date => time(), elements_count => '__$CNT__' }
15 )
16 }
17 );
18 while (defined( my $chunk = $splitter->next_chunk())) {
19 # do stuff with $chunk;
20 }
21  
22 =head1 DESCRIPTION
23  
24 This library implements an efficient way of splitting a Sereal blob into
25 smaller chunks. Currently, it only works with ArrayRefs Sereal blobs, like
26 this:
27  
28 [ $element_1, $element_2, ..., $element_n ]
29  
30 In the future, it may also work with HashRefs.
31  
32  
33 =head1 CONSTRUCTOR
34  
35 =head2 new
36  
37 Takes a HashRef with options:
38  
39 =head3 input
40  
41 Mandatory, String, the Sereal blob to split
42  
43 =head3 chunk_size
44  
45 Mandatory, positive Int, the approximate size of the B<uncompressed> chunk
46  
47 =head3 compress
48  
49 Optional, Int, one of SRL_UNCOMPRESSED, SRL_SNAPPY or SRL_ZLIB. These constant
50 can be exported at use time. If set, indicates how chunks must be compressed.
51 Defaults to SRL_UNCOMPRESSED.
52  
53 =head3 header_data_template
54  
55 Optional, Str, the header_data to inject in each chunk. This header_data can
56 contain special scalar values, that will be replaced by values. Special scalar values are:
57  
58 =over
59  
60 =item '__$CNT__'
61  
62 This will be replaced by the number of elements that the chunks contains. It
63 must be encoded as SHORT_BINARY_08. It'll be replaced by a VARINT.
64  
65 REMARK: In theory, it should be a string of lentgh 11, because varint max size
66 are 11 bytes. However, the Sereal decoder code, can't cope with varint bigger
67 than 8 bytes, because of a bug, and even if the varint is forged like
68 0x8180808080808080808080.
69  
70 =back
71  
72 To make things easier, you can use C<create_header_data_template> (see below)
73 to create it for you.
74  
75 =head1 METHODS
76  
77 =head2 next_chunk
78  
79 returns the next chunk as a String, or Undef if it was the last chunk
80  
81 =cut
82  
83 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 3 our $VERSION = '0.810';
88  
89 3 ▶use constant SRL_UNCOMPRESSED => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 3 ▶use constant SRL_SNAPPY => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 3 ▶use constant SRL_ZLIB => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
96 3 our @EXPORT_OK = qw(
97 SRL_UNCOMPRESSED
98 SRL_SNAPPY
99 SRL_ZLIB
100 create_header_data_template
101 );
102 3 our %EXPORT_TAGS = (all => \@EXPORT_OK);
103  
104 =head1 EXPORTED FUNCTIONS
105  
106 =head2 create_header_data_template
107  
108 Given a structure, will return a Sereal *body*, that can be used as value for
109 the C<header_data_template> constructor option.
110  
111 This function loads C<Sereal::Encoder> if it's not already loaded.
112  
113 =cut
114  
115 sub create_header_data_template {
116 1 ▶ require Sereal::Encoder;
Subroutine create_header_data_template: called CC=4 SCAR=14.2
Pod documented
117 1 require Sereal::Encoder::Constants;
118 1 my ($struct) = @_;
119 1 my $blob = Sereal::Encoder::encode_sereal_with_header_data(1, $struct);
120  
121  
122 1 ▶ my $fh = IO::File->new(\$blob, 'r')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new(\$blob, "r") or croak("failed to open blob") 0 1 1
123 or croak "failed to open blob";
124 1 $fh->binmode(':raw');
125  
126 1 my $length = 0;
127  
128 # magic
129 1 $length += $fh->read(my $magic, Sereal::Encoder::Constants::SRL_MAGIC_STRLEN());
130 1 ▶ $magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING() || $magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING_HIGHBIT()
Branch: 1 branch
BranchTrueFalseTotal
$magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING() or $magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING_HIGHBIT() or croak("invalid magic") 0 1 1
Condition33%1/3
$magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING() or $magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING_HIGHBIT()
l!l&&r!l&&!r
010
MC/DC0%0/2
$magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING() or $magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING_HIGHBIT()
$magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING() $magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING_HIGHBIT()
Truth table33%1/3
A or B
A= $magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING()B= $magic eq Sereal::Encoder::Constants::SRL_MAGIC_STRING_HIGHBIT()
ABresult
1X1
011
000
131 or croak "invalid magic";
132  
133 # version-type
134 1 $length += $fh->read(my $version_type, 1);
135  
136 1 $blob = substr $blob, $length, -1;
137
138 1 return $blob;
139 }
140  
141 sub new {
142 7 ▶ my ($class, $args) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
143
144 7 ▶ if ( my $header_data_template = $args->{header_data_template} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $header_data_template = $args->{"header_data_template"}) 1 6 7
145 1 my $str_to_replace = chr(0x68) . '__$CNT__';
146 1 ▶ if ( (my $where = index($header_data_template, $str_to_replace)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $where = index($header_data_template, $str_to_replace)) >= 0) 1 0 1
147 1 my $l = length $str_to_replace;
148 1 my $copy = $header_data_template;
149 1 substr($copy, $where, $l, chr(0x20) . chr(0) x ($l-1) );
150 1 $args = { %{$args},
151 header_data_template => $copy,
152 header_count_idx => $where + 1,
153 };
154 }
155 }
156  
157 7 $class->new_xs($args);
158 }
159  
160 3 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 3 XSLoader::load(__PACKAGE__, $Sereal::Splitter::VERSION);
163  
164 3 1;
« blib/lib/Sereal/Splitter/Constants.pm ↑ Summary const-c.inc »