line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Katsubushi::Converter; |
2
|
1
|
|
|
1
|
|
51038
|
use strict; |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
36
|
|
3
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
4
|
no warnings "portable"; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
125
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# see https://github.com/kayac/go-katsubushi/blob/master/generator.go#L12-L20 |
8
|
|
|
|
|
|
|
use constant { |
9
|
1
|
|
|
|
|
263
|
WORKER_ID_BITS => 10, |
10
|
|
|
|
|
|
|
SEQUENCE_BITS => 12, |
11
|
|
|
|
|
|
|
TIMESTAMP_SINCE => 1420070400000, |
12
|
|
|
|
|
|
|
TIMESTAMP_MASK => 0x7FFFFFFFFFC00000, |
13
|
1
|
|
|
1
|
|
6
|
}; |
|
1
|
|
|
|
|
2
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub id_to_epoch { |
16
|
2
|
|
|
2
|
1
|
507
|
my ($id) = @_; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
13
|
return int(id_to_epoch_msec($id) / 1000); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub id_to_epoch_msec { |
22
|
4
|
|
|
4
|
1
|
1172
|
my ($id) = @_; |
23
|
|
|
|
|
|
|
|
24
|
4
|
|
|
|
|
12
|
return ((($id & TIMESTAMP_MASK) >> (WORKER_ID_BITS + SEQUENCE_BITS)) + TIMESTAMP_SINCE); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub epoch_to_id { |
28
|
2
|
|
|
2
|
1
|
900
|
my ($epoch) = @_; |
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
|
|
5
|
return epoch_msec_to_id($epoch * 1000); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub epoch_msec_to_id { |
34
|
4
|
|
|
4
|
1
|
846
|
my ($epoch_msec) = @_; |
35
|
|
|
|
|
|
|
|
36
|
4
|
|
|
|
|
7
|
return ($epoch_msec - TIMESTAMP_SINCE) << (WORKER_ID_BITS + SEQUENCE_BITS); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
1; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head NAME |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Katsubushi::Converter - id converter issued by katsubushi |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 SYNOPSIS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
use Katsubushi::Converter |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $epoch = Katsubushi::Converter::id_to_epoch($id); |
50
|
|
|
|
|
|
|
print "id=${id} issued at ${epoch} in unix epoch time" |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 DESCRIPTION |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
This module provides methods to convert id issued by katsubushi. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
katsubushi is id generator written in Go. |
57
|
|
|
|
|
|
|
github.com/kayac/go-katsubushi |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 METHODS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over 4 |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item C<< $epoch = id_to_epoch($id) >> |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=item C<< $epoch_msec = id_to_epoch_msec($id) >> |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Convert id to epoch in seconds/milliseconds. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item C<< $id = epoch_to_id($epoch) >> |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item C<< $id = epoch_msec_to_id($epoch_msec) >> |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Convert epoch seconds/milliseconds to id. |
74
|
|
|
|
|
|
|
This method assume that WorkerID and Sequense are both 0. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=back |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 AUTHOR |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
NAGATA Hiroaki |