blib/lib/Kafka/Int64.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 69 SCAR 40.3
CC
7
Cov
0%
packq
20.0
unpackq
12.0
intsum
6.0
CRAP 56.0
1 package Kafka::Int64;
2  
3 =head1 NAME
4  
5 Kafka::Int64 - Functions to work with 64 bit elements of the
6 protocol on 32 bit systems.
7  
8 =head1 VERSION
9  
10 This documentation refers to C<Kafka::Int64> version 1.07 .
11  
12 =cut
13  
14  
15  
16 use 5.010;
17 use strict;
18 use warnings;
19  
20 use bigint; # this allows integers of practially any size at the cost of significant performance drop
21  
22  
23  
24 our $VERSION = '1.07';
25  
26 use Exporter qw(
27 import
28 );
29  
30 our @EXPORT_OK = qw(
31 intsum
32 packq
33 unpackq
34 );
35  
36  
37  
38 use Carp;
39  
40 use Kafka qw(
41 %ERROR
42 $ERROR_MISMATCH_ARGUMENT
43 );
44 use Kafka::Exceptions;
45  
46  
47  
48 =head1 SYNOPSIS
49  
50 use 5.010;
51 use strict;
52 use warnings;
53  
54 use Scalar::Util qw(
55 blessed
56 );
57 use Try::Tiny;
58  
59 use Kafka qw(
60 $BITS64
61 );
62  
63 try {
64  
65 # Apache Kafka Protocol: FetchOffset, Time
66  
67 my $offset = 123;
68  
69 my $encoded = $BITS64
70 ? pack( 'q>', $offset )
71 : Kafka::Int64::packq( $offset );
72  
73 my $response = chr( 0 ) x 8;
74  
75 $offset = $BITS64
76 ? unpack( 'q>', substr( $response, 0, 8 ) )
77 : Kafka::Int64::unpackq( substr( $response, 0, 8 ) );
78  
79 my $next_offset = $BITS64
80 ? $offset + 1
81 : Kafka::Int64::intsum( $offset, 1 );
82  
83 } catch {
84 my $error = $_;
85 if ( blessed( $error ) && $error->isa( 'Kafka::Exception' ) ) {
86 warn 'Error: (', $error->code, ') ', $error->message, "\n";
87 exit;
88 } else {
89 die $error;
90 }
91 };
92  
93 =head1 DESCRIPTION
94  
95 This module is not intended to be used by end user.
96  
97 In order to achieve better performance, functions of this module do not perform
98 validation of arguments.
99  
100 Transparent L<BigInteger|bigint> support on 32-bit platforms where native
101 integer type is limited to 32 bits and slow bigint must be used instead.
102 Use L<functions|/FUNCTIONS> from this module in such case.
103  
104 The main features of the C<Kafka::Int64> module are:
105  
106 =over 3
107  
108 =item *
109  
110 Support for working with 64 bit elements of the Kafka protocol
111 on 32 bit systems.
112  
113 =back
114  
115 =cut
116  
117 #-- public functions -----------------------------------------------------------
118  
119 =head2 FUNCTIONS
120  
121 The following functions are available for the C<Kafka::Int64> module.
122  
123 =cut
124  
125 =head3 C<intsum( $bint, $int )>
126  
127 Adds two numbers to emulate bigint adding 64-bit integers in 32-bit systems.
128  
129 The both arguments must be a number. That is, it is defined and Perl thinks
130 it's a number. Arguments may be a L<Math::BigInt|Math::BigInt>
131 integers.
132  
133 Returns the value as a L<Math::BigInt|Math::BigInt> integer.
134  
135 =cut
136 sub intsum {
137 my ( $frst, $scnd ) = @_;
138  
139 my $ret = $frst + $scnd + 0; # bigint coercion
140 Kafka::Exception::Int64->throw( throw_args( $ERROR_MISMATCH_ARGUMENT, 'intsum' ) )
141 if $ret->is_nan();
142  
143 return $ret;
144 }
145  
146 =head3 C<packq( $bint )>
147  
148 Emulates C<pack( q{qE<gt>}, $bint )> to 32-bit systems - assumes decimal string
149 or integer input.
150  
151 An argument must be a positive number. That is, it is defined and Perl thinks
152 it's a number. The argument may be a L<Math::BigInt|Math::BigInt> integer.
153  
154 The special values -1, -2 are allowed
155 (C<$Kafka::RECEIVE_LATEST_OFFSET>, C<$Kafka::RECEIVE_EARLIEST_OFFSETS>).
156  
157 Returns the value as a packed binary string.
158  
159 =cut
160 sub packq {
161 my ( $n ) = @_;
162  
163 if ( $n == -1 ) { return pack q{C8}, ( 255 ) x 8; }
164 elsif ( $n == -2 ) { return pack q{C8}, ( 255 ) x 7, 254; }
165 elsif ( $n < 0 ) { Kafka::Exception::Int64->throw( throw_args( $ERROR_MISMATCH_ARGUMENT, 'packq' ) ); }
166  
167 return pack q{H16}, substr( '00000000000000000000000000000000'.substr( ( $n + 0 )->as_hex(), 2 ), -16 );
168 }
169  
170 =head3 C<unpackq( $bstr )>
171  
172 Emulates C<unpack( q{qE<gt>}, $bstr )> to 32-bit systems - assumes binary input.
173  
174 The argument must be a binary string of 8 bytes length.
175  
176 Returns the value as a L<Math::BigInt|Math::BigInt> integer.
177  
178 =cut
179 sub unpackq {
180 my ( $s ) = @_;
181  
182 my $ret = Math::BigInt->from_hex( '0x'.unpack( q{H16}, $s ) );
183 $ret = -1 if $ret == 18446744073709551615;
184 $ret = -2 if $ret == 18446744073709551614;
185  
186 return $ret;
187 }
188  
189 #-- private functions ----------------------------------------------------------
190  
191 1;
192  
193 __END__