blib/lib/Katsubushi/Converter.pm

filter: Statementstmt 100% 21 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 100% 33 / 33 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ Summary
1 package Katsubushi::Converter;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶no warnings "portable";
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # see https://github.com/kayac/go-katsubushi/blob/master/generator.go#L12-L20
8 use constant {
9 1 WORKER_ID_BITS => 10,
10 SEQUENCE_BITS => 12,
11 TIMESTAMP_SINCE => 1420070400000,
12 TIMESTAMP_MASK => 0x7FFFFFFFFFC00000,
13 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub id_to_epoch {
16 2 ▶ my ($id) = @_;
Subroutine id_to_epoch: called CC=1 SCAR=0.0
Pod documented
17  
18 2 return int(id_to_epoch_msec($id) / 1000);
19 }
20  
21 sub id_to_epoch_msec {
22 4 ▶ my ($id) = @_;
Subroutine id_to_epoch_msec: called CC=1 SCAR=0.0
Pod documented
23  
24 4 return ((($id & TIMESTAMP_MASK) >> (WORKER_ID_BITS + SEQUENCE_BITS)) + TIMESTAMP_SINCE);
25 }
26  
27 sub epoch_to_id {
28 2 ▶ my ($epoch) = @_;
Subroutine epoch_to_id: called CC=1 SCAR=0.0
Pod documented
29  
30 2 return epoch_msec_to_id($epoch * 1000);
31 }
32  
33 sub epoch_msec_to_id {
34 4 ▶ my ($epoch_msec) = @_;
Subroutine epoch_msec_to_id: called CC=1 SCAR=0.0
Pod documented
35  
36 4 return ($epoch_msec - TIMESTAMP_SINCE) << (WORKER_ID_BITS + SEQUENCE_BITS);
37 }
38  
39 1 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 <handlename>
« Build ↑ Summary