line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::MoCo::MUID; |
2
|
1
|
|
|
1
|
|
59213
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
3
|
1
|
|
|
1
|
|
5
|
use Exporter qw(import); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
4
|
|
|
|
|
|
|
our @EXPORT = qw(create_muid); |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
503
|
use Net::Address::Ethernet qw(get_addresses); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Math::BigInt::Lite; |
8
|
|
|
|
|
|
|
use Time::HiRes; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my ($ser, $addr); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
BEGIN { |
13
|
|
|
|
|
|
|
$addr = (get_addresses)[0]->{sIP}; |
14
|
|
|
|
|
|
|
$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 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 |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 AUTHOR |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Junya Kondo, Ejkondo@hatena.comE |
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 |