Manager/Serializer/JSON/Zstd.pm

filter: Statementstmt 100% 70 / 70 Branchbran 70.5% 24 / 34 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 13 / 13 Podpod 60.0% 3 / 5 totaltotal 89.6% 121 / 135 CC 21 SCAR 14.8
CC
21
Cov
90%
serialize
7.2
init
6.1
deserialize
5.0
CRAP 4.4
« Manager/Role/Service/Select.pm ↑ Summary Manager/Base/FS.pm »
1 package IPC::Manager::Serializer::JSON::Zstd;
2 224 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 224 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 224 our $VERSION = '0.000035';
6  
7 224 ▶use parent 'IPC::Manager::Serializer::JSON';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 224 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 224 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 224 use Object::HashBase qw{
13 <level
14 <dictionary
15 +cctx
16 +dctx
17 +cdict
18 +ddict
19 224 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 224 ▶use constant DEFAULT_LEVEL => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 224 my $HAVE_ZSTD;
24  
25 sub _have_zstd {
26 1365 ▶ return $HAVE_ZSTD if defined $HAVE_ZSTD;
Subroutine _have_zstd: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if defined $HAVE_ZSTD 1142 223 1365
27 223 ▶ return $HAVE_ZSTD = eval { require Compress::Zstd; Compress::Zstd->VERSION('0.20'); 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Compress::Zstd; 'Compress::Zstd'->VERSION('0.20'); 1 } } ? : 223 0 223
28 }
29  
30 219 ▶sub viable { _have_zstd() }
Subroutine viable: called CC=1 SCAR=0.0
Pod documented
31  
32 sub init {
33 15 ▶ my $self = shift;
Subroutine init: called CC=6 SCAR=18.0
Pod undocumented
34  
35 15 ▶ croak "Compress::Zstd 0.20 or newer is required for IPC::Manager::Serializer::JSON::Zstd"
Branch: 1 branch
BranchTrueFalseTotal
unless _have_zstd() 0 15 15
36 unless _have_zstd();
37  
38 15 ▶ $self->{+LEVEL} //= DEFAULT_LEVEL;
Condition100%2/2
$self->{"level"} //= 3
l!l
132
MC/DC100%1/1
$self->{"level"} //= 3
$self->{"level"}
Truth table100%2/2
A
A= $self->{"level"}
Aresult
11
00
39  
40 15 ▶ if (defined $self->{+DICTIONARY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"dictionary"}) 1 14 15
41 1 require Compress::Zstd::CompressionContext;
42 1 require Compress::Zstd::DecompressionContext;
43 1 require Compress::Zstd::CompressionDictionary;
44 1 require Compress::Zstd::DecompressionDictionary;
45  
46 1 ▶ $self->{+CDICT} = Compress::Zstd::CompressionDictionary->new_from_file($self->{+DICTIONARY}, $self->{+LEVEL})
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cdict"} = "Compress::Zstd::CompressionDictionary"->new_from_file($self->{"dictionary"}, $self->{"level"}) or croak(qq[Could not load zstd compression dictionary from '$self->{"dictionary"}']) 0 1 1
47 or croak "Could not load zstd compression dictionary from '$self->{+DICTIONARY}'";
48 1 ▶ $self->{+DDICT} = Compress::Zstd::DecompressionDictionary->new_from_file($self->{+DICTIONARY})
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ddict"} = "Compress::Zstd::DecompressionDictionary"->new_from_file($self->{"dictionary"}) or croak(qq[Could not load zstd decompression dictionary from '$self->{"dictionary"}']) 0 1 1
49 or croak "Could not load zstd decompression dictionary from '$self->{+DICTIONARY}'";
50 1 $self->{+CCTX} = Compress::Zstd::CompressionContext->new;
51 1 $self->{+DCTX} = Compress::Zstd::DecompressionContext->new;
52 }
53  
54 15 return $self;
55 }
56  
57 sub serialize {
58 698 ▶ my ($invocant, $obj) = @_;
Subroutine serialize: called CC=7 SCAR=19.7
Pod documented
59  
60 698 ▶ croak "Compress::Zstd 0.20 or newer is required for IPC::Manager::Serializer::JSON::Zstd"
Branch: 1 branch
BranchTrueFalseTotal
unless _have_zstd() 0 698 698
61 unless _have_zstd();
62  
63 698 my $json = $invocant->SUPER::serialize($obj);
64  
65 698 ▶ if (blessed $invocant) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($invocant)) 4 694 698
66 4 ▶ if ($invocant->{+CCTX}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invocant->{"cctx"}) 1 3 4
67 1 my $bytes = $invocant->{+CCTX}->compress_using_dict($json, $invocant->{+CDICT});
68 1 ▶ croak "Failed to compress payload with zstd dictionary" unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 1 1
69 1 return $bytes;
70 }
71 3 my $bytes = Compress::Zstd::compress($json, $invocant->{+LEVEL});
72 3 ▶ croak "Failed to compress payload with zstd" unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 3 3
73 3 return $bytes;
74 }
75  
76 694 my $bytes = Compress::Zstd::compress($json, DEFAULT_LEVEL);
77 694 ▶ croak "Failed to compress payload with zstd" unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 694 694
78 694 return $bytes;
79 }
80  
81 sub deserialize {
82 433 ▶ my ($invocant, $bytes) = @_;
Subroutine deserialize: called CC=5 SCAR=16.1
Pod documented
83  
84 433 ▶ croak "Compress::Zstd 0.20 or newer is required for IPC::Manager::Serializer::JSON::Zstd"
Branch: 1 branch
BranchTrueFalseTotal
unless _have_zstd() 0 433 433
85 unless _have_zstd();
86  
87 433 my $json;
88 433 ▶ if (blessed($invocant) && $invocant->{+DCTX}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($invocant) and $invocant->{'dctx'}) { } 1 432 433
Condition100%3/3
builtin::blessed($invocant) and $invocant->{"dctx"}
!ll&&!rl&&r
42931
MC/DC100%2/2
builtin::blessed($invocant) and $invocant->{"dctx"}
builtin::blessed($invocant) $invocant->{"dctx"}
Truth table100%3/3
A and B
A= builtin::blessed($invocant)B= $invocant->{"dctx"}
ABresult
0X0
100
111
89 1 $json = $invocant->{+DCTX}->decompress_using_dict($bytes, $invocant->{+DDICT});
90 }
91 else {
92 432 $json = Compress::Zstd::decompress($bytes);
93 }
94  
95 433 ▶ croak "Failed to decompress zstd payload" unless defined $json;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 2 431 433
96  
97 431 return $invocant->SUPER::deserialize($json);
98 }
99  
100 sub TO_JSON {
101 4 ▶ my $self = shift;
Subroutine TO_JSON: called CC=4 SCAR=14.0
Pod undocumented
102 4 my @args;
103 4 ▶ push @args, level => $self->{+LEVEL} if defined $self->{+LEVEL} && $self->{+LEVEL} != DEFAULT_LEVEL;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"level"} and $self->{"level"} != 3 3 1 4
Condition66%2/3
defined $self->{"level"} and $self->{"level"} != 3
!ll&&!rl&&r
013
MC/DC50%1/2
defined $self->{"level"} and $self->{"level"} != 3
defined $self->{"level"} $self->{"level"} != 3
Truth table66%2/3
A and B
A= defined $self->{"level"}B= $self->{"level"} != 3
ABresult
0X0
100
111
104 4 ▶ push @args, dictionary => $self->{+DICTIONARY} if defined $self->{+DICTIONARY};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dictionary"} 0 4 4
105 4 return [ref($self), @args];
106 }
107  
108 224 1;
109  
110 __END__
« Manager/Role/Service/Select.pm ↑ Summary Manager/Base/FS.pm »