MoCo/MUID.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« MoCo/Cache/Dummy.pm ↑ Summary MoCo/Join.pm »
1 package DBIx::MoCo::MUID;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 our @EXPORT = qw(create_muid);
5  
6 1 ▶use Net::Address::Ethernet qw(get_addresses);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Math::BigInt::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 my ($ser, $addr);
11  
12 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=2.6
13 1 $addr = (get_addresses)[0]->{sIP};
14 0 $addr = substr(join('', map {sprintf('%08b', $_)} split(/\./, $addr)), -20);
15 }
16  
17 sub create_muid {
18 unless (defined $ser) {
19 $ser = int(rand(256));
20 }
21 # my $time = sprintf('%032b', time());
22 Math::BigInt::Lite->new(int(Time::HiRes::time() * 1000))->as_bin =~ /([01]{36})$/o;
23 my $time = $1;
24 my $serial = sprintf('%08b', $ser++ % 256);
25 my $muid = $addr . $time . $serial;
26 $muid = Math::BigInt::Lite->new("0b$muid");
27 # warn $muid->bstr();
28 # warn $muid->as_bin();
29 return $muid->bstr();
30 }
31  
32 1;
33  
34 =head1 NAME
35  
36 DBIx::MoCo::MUID - MUID generator for muid fields
37  
38 =head1 SYNOPSIS
39  
40 my $muid = DBIx::MoCo::MUID->create_muid();
41  
42 =head1 DESCRIPTION
43  
44 I<DBIx::MoCo::MUID> provides "almost unique" id for MoCo Unique ID
45 (muid) fields.
46 They are less unique than UUIDs because they only have 64bits long.
47  
48 They are generated as set of next 3 parts.
49  
50 20 bits of ip address (last 20 bits)
51 36 bits of epoch time (lower 36 bits of msec.) (2.179 years)
52 8 bits of serial
53  
54 =head1 SEE ALSO
55  
56 L<DBIx::MoCo>
57  
58 =head1 AUTHOR
59  
60 Junya Kondo, E<lt>jkondo@hatena.comE<gt>
61  
62 =head1 COPYRIGHT AND LICENSE
63  
64 Copyright (C) Hatena Inc. All Rights Reserved.
65  
66 This library is free software; you may redistribute it and/or modify
67 it under the same terms as Perl itself.
68  
69 =cut
« MoCo/Cache/Dummy.pm ↑ Summary MoCo/Join.pm »