| 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
| |||||||||||||||||||||||||||||
| 27 | 223 | ▶ | return $HAVE_ZSTD = eval { require Compress::Zstd; Compress::Zstd->VERSION('0.20'); 1 } ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 36 | unless _have_zstd(); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 15 | ▶ | $self->{+LEVEL} //= DEFAULT_LEVEL; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"level"} //= 3
MC/DC100%1/1
$self->{"level"} //= 3
$self->{"level"}
Truth table100%2/2
A
A= $self->{"level"}
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 15 | ▶ | if (defined $self->{+DICTIONARY}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | unless _have_zstd(); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 698 | my $json = $invocant->SUPER::serialize($obj); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 698 | ▶ | if (blessed $invocant) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | if ($invocant->{+CCTX}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 85 | unless _have_zstd(); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 433 | my $json; | |||||||||||||||||||||||||||
| 88 | 433 | ▶ | if (blessed($invocant) && $invocant->{+DCTX}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($invocant) and $invocant->{"dctx"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $self->{"level"} and $self->{"level"} != 3
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
| |||||||||||||||||||||||||||||
| 104 | 4 | ▶ | push @args, dictionary => $self->{+DICTIONARY} if defined $self->{+DICTIONARY}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 4 | return [ref($self), @args]; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 224 | 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||