CouchLike/IdGenerator.pm

filter: Statementstmt 97.7% 85 / 87 Branchbran 64.2% 9 / 14 Conditioncond 42.8% 3 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 2 totaltotal 88.2% 120 / 136 CC 11 SCAR 9.3
CC
11
Cov
90%
new
6.7
get_id
4.0
_sync
3.0
CRAP 2.5
« CouchLike/Sth.pm ↑ Summary
1 package DBIx::CouchLike::IdGenerator;
2 # original code from Data::YUID::Generator
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Math::BigInt try => 'GMP';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶no warnings qw(deprecated); # for fields
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $VERSION = "0.02";
12  
13 1 ▶use fields qw(host_id start_time current_time min_id max_id ids);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use constant EPOCH_OFFSET => 946684800; # Sat, Jan 1 2000 00:00 GMT
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use constant HOST_ID_BITS => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant TIME_BITS => 36;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant SERIAL_BITS => 64 - HOST_ID_BITS - TIME_BITS;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use constant TIME_SHIFT => HOST_ID_BITS + SERIAL_BITS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant SERIAL_SHIFT => HOST_ID_BITS;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use constant SERIAL_INCREMENT => Math::BigInt->new(1) << SERIAL_SHIFT;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use constant HOST_ID_MAX => (Math::BigInt->new(1) << HOST_ID_BITS) - 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant TIME_MAX => (Math::BigInt->new(1) << TIME_BITS) - 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant TIME_MAX_SHIFTED => TIME_MAX << TIME_SHIFT;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant SERIAL_MAX => (Math::BigInt->new(1) << SERIAL_BITS) - 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant SERIAL_MAX_SHIFTED => SERIAL_MAX << SERIAL_SHIFT;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32  
33 1 ▶sub new {
Subroutine new: called CC=5 SCAR=19.0
Pod undocumented
34 1 my $self = shift;
35 1 ▶ $self = fields::new( $self ) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 0 1
36  
37 1 my $host_id = shift;
38 1 ▶ if( !$host_id ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $host_id) { } 1 0 1
elsif ($host_id < 0 or $host_id > {sign => '+', value => [65535]}) { } 0 0 0
39 1 $host_id = int( rand( HOST_ID_MAX ) );
40 ▶ } elsif( $host_id < 0 || $host_id > HOST_ID_MAX ) {
Condition0%0/3
$host_id < 0 or $host_id > bless( {"sign" => "+","value" => bless( [65535], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
l!l&&r!l&&!r
000
MC/DC0%0/2
$host_id < 0 or $host_id > bless( {"sign" => "+","value" => bless( [65535], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
$host_id < 0 $host_id > bless( {"sign" => "+","value" => bless( [65535], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
Truth table0%0/3
A or B
A= $host_id < 0B= $host_id > bless( {"sign" => "+","value" => bless( [65535], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
ABresult
1X1
011
000
41 0 warn __PACKAGE__ . ": host ID $host_id is not in range of [0," . HOST_ID_MAX . "]\n";
42 0 return;
43 }
44  
45 1 $self->{ host_id } = $host_id;
46 1 $self->{ start_time } = time;
47 1 $self->{ current_time } = 0;
48 1 $self->{ ids } = {};
49 1 $self->_sync();
50  
51 1 return $self;
52 }
53  
54  
55 2001 ▶sub _sync {
Subroutine _sync: called CC=3 SCAR=11.0
56 2001 my $self = shift;
57 2001 my $time = time;
58 2001 ▶ return if( $self->{ current_time } == $time ); # FIXME: check for clock skew
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"current_time"} == $time 1998 3 2001
59 3 $self->{ current_time } = $time;
60 3 ▶ $self->{ min_id } = $self->_make_id( 0 ) unless( $self->{ min_id } );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"min_id"} 1 2 3
61 3 $self->{ max_id } = $self->_make_id( SERIAL_MAX );
62 }
63  
64  
65 4 ▶sub _make_id {
Subroutine _make_id: called CC=2 SCAR=6.9
66 4 my $self = shift;
67 4 ▶ my $serial = shift || 0;
Condition100%2/2
shift() || 0
l!l
31
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
68 return
69 ((Math::BigInt->new( $self->{ current_time } - EPOCH_OFFSET )) << TIME_SHIFT)
70 4 | (Math::BigInt->new($serial) << SERIAL_SHIFT) | $self->{ host_id };
71 }
72  
73  
74 2000 ▶sub get_id {
Subroutine get_id: called CC=4 SCAR=14.0
Pod undocumented
75 2000 my $self = shift;
76 2000 ▶ my $key = shift || "_";
Condition50%1/2
shift() || '_'
l!l
02000
MC/DC0%0/1
shift() || '_'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
77 2000 $self->_sync();
78  
79 2000 ▶ if( !exists $self->{ ids }->{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"ids"}{$key}) 1 1999 2000
80 1 $self->{ ids }->{ $key } = $self->{ min_id };
81 1 return $self->{ ids }->{ $key }->bstr;
82 }
83  
84 1999 ▶ return if( $self->{ ids }->{ $key } >= $self->{ max_id } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ids"}{$key} >= $self->{"max_id"} 0 1999 1999
85  
86 1999 $self->{ ids }->{ $key } += SERIAL_INCREMENT;
87 1999 return $self->{ ids }->{ $key }->bstr;
88 }
89  
90  
91 1 1;
« CouchLike/Sth.pm ↑ Summary