line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package UUID::Generator::PurePerl; |
2
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
5125
|
use strict; |
|
9
|
|
|
|
|
12
|
|
|
9
|
|
|
|
|
230
|
|
4
|
9
|
|
|
9
|
|
29
|
use warnings; |
|
9
|
|
|
|
|
9
|
|
|
9
|
|
|
|
|
164
|
|
5
|
9
|
|
|
9
|
|
132
|
use 5.006; |
|
9
|
|
|
|
|
20
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.04_02'; |
8
|
|
|
|
|
|
|
|
9
|
9
|
|
|
9
|
|
33
|
use Carp; |
|
9
|
|
|
|
|
12
|
|
|
9
|
|
|
|
|
507
|
|
10
|
9
|
|
|
9
|
|
3911
|
use Digest; |
|
9
|
|
|
|
|
3792
|
|
|
9
|
|
|
|
|
205
|
|
11
|
9
|
|
|
9
|
|
4152
|
use Time::HiRes; |
|
9
|
|
|
|
|
9126
|
|
|
9
|
|
|
|
|
29
|
|
12
|
9
|
|
|
9
|
|
4430
|
use UUID::Object; |
|
9
|
|
|
|
|
109136
|
|
|
9
|
|
|
|
|
539
|
|
13
|
9
|
|
|
9
|
|
3490
|
use UUID::Generator::PurePerl::RNG; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
228
|
|
14
|
9
|
|
|
9
|
|
2960
|
use UUID::Generator::PurePerl::NodeID; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
215
|
|
15
|
9
|
|
|
9
|
|
40
|
use UUID::Generator::PurePerl::Util; |
|
9
|
|
|
|
|
8
|
|
|
9
|
|
|
|
|
5396
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
18
|
7
|
|
|
7
|
0
|
953
|
my $class = shift; |
19
|
7
|
|
|
|
|
19
|
my $self = bless {}, $class; |
20
|
|
|
|
|
|
|
|
21
|
7
|
|
|
|
|
20
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub rng { |
25
|
20
|
|
|
20
|
0
|
14
|
my ($self) = @_; |
26
|
|
|
|
|
|
|
|
27
|
20
|
100
|
|
|
|
48
|
if (! defined $self->{rng}) { |
28
|
1
|
|
|
|
|
10
|
$self->{rng} = UUID::Generator::PurePerl::RNG->singleton(); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
20
|
|
|
|
|
55
|
return $self->{rng}; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub node_getter { |
35
|
14
|
|
|
14
|
0
|
25
|
my ($self) = @_; |
36
|
|
|
|
|
|
|
|
37
|
14
|
100
|
|
|
|
37
|
if (! defined $self->{node_getter}) { |
38
|
3
|
|
|
|
|
21
|
$self->{node_getter} = UUID::Generator::PurePerl::NodeID->singleton(); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
14
|
|
|
|
|
40
|
return $self->{node_getter}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub get_timestamp { |
45
|
16
|
|
|
16
|
0
|
27
|
return Time::HiRes::time(); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub get_clk_seq { |
49
|
14
|
|
|
14
|
0
|
16
|
my $self = shift; |
50
|
14
|
|
|
|
|
13
|
my $node_id = shift; |
51
|
|
|
|
|
|
|
|
52
|
14
|
|
|
|
|
7
|
my $inc_seq = 0; |
53
|
|
|
|
|
|
|
|
54
|
14
|
|
|
|
|
16
|
my $ts = $self->get_timestamp(); |
55
|
14
|
100
|
100
|
|
|
74
|
if (! defined $self->{last_ts} || $ts <= $self->{last_ts}) { |
56
|
5
|
|
|
|
|
3
|
$inc_seq ++; |
57
|
|
|
|
|
|
|
} |
58
|
14
|
|
|
|
|
13
|
$self->{last_ts} = $ts; |
59
|
|
|
|
|
|
|
|
60
|
14
|
100
|
|
|
|
25
|
if (! defined $self->{last_node}) { |
61
|
3
|
50
|
|
|
|
8
|
if (defined $node_id) { |
62
|
3
|
|
|
|
|
3
|
$inc_seq ++; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
11
|
50
|
33
|
|
|
53
|
if (! defined $node_id || $node_id ne $self->{last_node}) { |
67
|
0
|
|
|
|
|
0
|
$inc_seq ++; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
14
|
|
|
|
|
13
|
$self->{last_node} = $node_id; |
71
|
|
|
|
|
|
|
|
72
|
14
|
100
|
|
|
|
24
|
if (! defined $self->{clk_seq}) { |
73
|
3
|
|
|
|
|
5
|
$self->{clk_seq} = $self->_generate_clk_seq(); |
74
|
3
|
|
|
|
|
29
|
return $self->{clk_seq} & 0x03ff; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
11
|
100
|
|
|
|
17
|
if ($inc_seq) { |
78
|
2
|
|
|
|
|
3
|
$self->{clk_seq} = ($self->{clk_seq} + 1) % 65536; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
11
|
|
|
|
|
63
|
return $self->{clk_seq} & 0x03ff; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _generate_clk_seq { |
85
|
3
|
|
|
3
|
|
4
|
my $self = shift; |
86
|
|
|
|
|
|
|
|
87
|
3
|
|
|
|
|
4
|
my @data; |
88
|
3
|
|
|
|
|
10
|
push @data, q{} . $$; |
89
|
3
|
|
|
|
|
31
|
push @data, q{:} . Time::HiRes::time(); |
90
|
|
|
|
|
|
|
|
91
|
3
|
|
|
|
|
14
|
return digest_as_16bit(@data); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub generate_v1 { |
95
|
14
|
|
|
14
|
1
|
1463
|
my $self = shift; |
96
|
|
|
|
|
|
|
|
97
|
14
|
|
|
|
|
25
|
my $node = $self->node_getter->node_id(); |
98
|
14
|
|
|
|
|
26
|
my $ts = $self->get_timestamp(); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return |
101
|
14
|
|
|
|
|
39
|
UUID::Object->create_from_hash({ |
102
|
|
|
|
|
|
|
variant => 2, |
103
|
|
|
|
|
|
|
version => 1, |
104
|
|
|
|
|
|
|
node => $node, |
105
|
|
|
|
|
|
|
time => $ts, |
106
|
|
|
|
|
|
|
clk_seq => $self->get_clk_seq($node), |
107
|
|
|
|
|
|
|
}); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub generate_v1mc { |
111
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
0
|
my $node = $self->node_getter->random_node_id(); |
114
|
0
|
|
|
|
|
0
|
my $ts = $self->get_timestamp(); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
return |
117
|
0
|
|
|
|
|
0
|
UUID::Object->create_from_hash({ |
118
|
|
|
|
|
|
|
variant => 2, |
119
|
|
|
|
|
|
|
version => 1, |
120
|
|
|
|
|
|
|
node => $node, |
121
|
|
|
|
|
|
|
time => $ts, |
122
|
|
|
|
|
|
|
clk_seq => $self->get_clk_seq(undef), |
123
|
|
|
|
|
|
|
}); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub generate_v4 { |
127
|
5
|
|
|
5
|
1
|
1083
|
my ($self) = @_; |
128
|
|
|
|
|
|
|
|
129
|
5
|
|
|
|
|
8
|
my $b = q{}; |
130
|
5
|
|
|
|
|
7
|
for (1 .. 4) { |
131
|
20
|
|
|
|
|
30
|
$b .= pack 'I', $self->rng->rand_32bit; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
5
|
|
|
|
|
19
|
my $u = UUID::Object->create_from_binary($b); |
135
|
|
|
|
|
|
|
|
136
|
5
|
|
|
|
|
37
|
$u->variant(2); |
137
|
5
|
|
|
|
|
133
|
$u->version(4); |
138
|
|
|
|
|
|
|
|
139
|
5
|
|
|
|
|
48
|
return $u; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub generate_v3 { |
143
|
22
|
|
|
22
|
1
|
4829
|
my ($self, $ns, $data) = @_; |
144
|
|
|
|
|
|
|
|
145
|
22
|
|
|
|
|
44
|
return $self->_generate_digest(3, 'MD5', $ns, $data); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub generate_v5 { |
149
|
1
|
|
|
1
|
1
|
85
|
my ($self, $ns, $data) = @_; |
150
|
|
|
|
|
|
|
|
151
|
1
|
|
|
|
|
4
|
return $self->_generate_digest(5, 'SHA-1', $ns, $data); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub _generate_digest { |
155
|
23
|
|
|
23
|
|
32
|
my ($self, $version, $digest, $ns, $data) = @_; |
156
|
|
|
|
|
|
|
|
157
|
23
|
|
|
|
|
51
|
$ns = UUID::Object->new($ns)->as_binary; |
158
|
|
|
|
|
|
|
|
159
|
23
|
|
|
|
|
553
|
my $dg = Digest->new($digest); |
160
|
|
|
|
|
|
|
|
161
|
23
|
|
|
|
|
464
|
$dg->reset(); |
162
|
|
|
|
|
|
|
|
163
|
23
|
|
|
|
|
49
|
$dg->add($ns); |
164
|
|
|
|
|
|
|
|
165
|
23
|
|
|
|
|
36
|
$dg->add($data); |
166
|
|
|
|
|
|
|
|
167
|
23
|
|
|
|
|
101
|
my $u = UUID::Object->create_from_binary($dg->digest); |
168
|
23
|
|
|
|
|
128
|
$u->variant(2); |
169
|
23
|
|
|
|
|
444
|
$u->version($version); |
170
|
|
|
|
|
|
|
|
171
|
23
|
|
|
|
|
363
|
return $u; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
1; |
175
|
|
|
|
|
|
|
__END__ |