anyflake 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +78 -0
- data/Rakefile +6 -0
- data/anyflake.gemspec +25 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/anyflake.rb +73 -0
- data/lib/anyflake/version.rb +3 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c2e808de118e495bfe5dc94867dcdb11696803a3
|
4
|
+
data.tar.gz: be42bfbfc7146616482a0629142b2041b969ff6c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a1439b1c3b36673710abe927f30f2c41493c2cd16d73fcbd437cec151209d0aa22c8e82d3c4c97a297550f998ca6c48fcefad76182214f24cdd6a68d1bef201f
|
7
|
+
data.tar.gz: 8d658b804098729134e9d36abad338187f9f478437eb69178c8007c327753e6dbedfd2e1bbbd8aefd50b75188cd5d386f0fa669bee91e343203eaa26737c4d99
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Joshimasa KISHIMOTO
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
Pure ruby independent ID generator like the SnowFlake, ChiliFlake
|
2
|
+
=================================================================
|
3
|
+
|
4
|
+
This is ID generator like the SnowFlake and ChiliFlake that implemented by pure ruby.
|
5
|
+
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem "anyflake"
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install anyflake
|
21
|
+
|
22
|
+
|
23
|
+
QuickStart
|
24
|
+
----------
|
25
|
+
|
26
|
+
**Genenate ID:**
|
27
|
+
|
28
|
+
```
|
29
|
+
service_epoch = Time.new(2016, 7, 1, 0, 0, 0).strftime('%s%L').to_i
|
30
|
+
node_id = 1
|
31
|
+
af = AnyFlake.new(service_epoch, node_id)
|
32
|
+
af.next_id
|
33
|
+
=> 8697308774404097
|
34
|
+
```
|
35
|
+
|
36
|
+
**Parse for the Flaked ID:**
|
37
|
+
|
38
|
+
```
|
39
|
+
af.parse(8697308774404097)
|
40
|
+
|
41
|
+
or
|
42
|
+
|
43
|
+
AnyFlake.parse(8697308774404097, service_epoch)
|
44
|
+
=> {:epoch_time=>2073600000, :time=>2016-07-25 00:00:00 +0900, :node_id=>1, :sequence=>1}
|
45
|
+
```
|
46
|
+
|
47
|
+
|
48
|
+
Overview
|
49
|
+
--------
|
50
|
+
|
51
|
+
ID structure
|
52
|
+
|
53
|
+
```
|
54
|
+
upper bit(always 0) 1 bit
|
55
|
+
epoch timestamp 41 bits
|
56
|
+
node id 10 bits
|
57
|
+
sequence per node 12 bits
|
58
|
+
```
|
59
|
+
|
60
|
+
|
61
|
+
Notice:
|
62
|
+
|
63
|
+
1. Duplicate when generate the ID of 4096 or more per node within 1ms.
|
64
|
+
=> Workaround: Unique check in the user side. (e.g. DB Pkey, unique index...)
|
65
|
+
|
66
|
+
see https://github.com/twitter/snowflake
|
67
|
+
see https://github.com/ma2shita/chiliflake
|
68
|
+
|
69
|
+
Contributing
|
70
|
+
------------
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/anyflake.
|
73
|
+
|
74
|
+
|
75
|
+
## License
|
76
|
+
|
77
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
78
|
+
|
data/Rakefile
ADDED
data/anyflake.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'anyflake/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'anyflake'
|
8
|
+
spec.version = Anyflake::VERSION
|
9
|
+
spec.authors = ['Joshimasa KISHIMOTO']
|
10
|
+
spec.email = ['joshimasa.kishimoto@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Pure ruby independent ID generator like the SnowFlake, ChiliFlake}
|
13
|
+
spec.description = %q{Pure ruby independent ID generator like the SnowFlake, ChiliFlake}
|
14
|
+
spec.homepage = 'https://github.com/jkishimoto/anyflake'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "anyflake"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/anyflake.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require "anyflake/version"
|
2
|
+
|
3
|
+
# Pure ruby independent ID generator like the SnowFlake, ChiliFlake.
|
4
|
+
# @see https://github.com/twitter/snowflake
|
5
|
+
# @see https://github.com/ma2shita/chiliflake
|
6
|
+
class AnyFlake
|
7
|
+
TIMESTAMP_BITS = 41
|
8
|
+
NODE_ID_BITS = 10
|
9
|
+
SEQUENCE_BITS = 12
|
10
|
+
|
11
|
+
MAX_NODE_ID = (1 << NODE_ID_BITS) # 1024
|
12
|
+
MAX_SEQUENCE = (1 << SEQUENCE_BITS) # 4096
|
13
|
+
|
14
|
+
@target_epoch = nil
|
15
|
+
@node_id = nil
|
16
|
+
@sequence = nil
|
17
|
+
@last_time = nil
|
18
|
+
|
19
|
+
def initialize(target_epoch, node_id, sequence = 0)
|
20
|
+
raise OverflowError, "invalid node_id (#{node_id} >= #{MAX_NODE_ID})" if node_id >= MAX_NODE_ID
|
21
|
+
raise OverflowError, "invalid sequence (#{sequence} >= #{MAX_SEQUENCE})" if sequence >= MAX_SEQUENCE
|
22
|
+
@target_epoch = target_epoch
|
23
|
+
@node_id = node_id % MAX_NODE_ID
|
24
|
+
@sequence = sequence % MAX_SEQUENCE
|
25
|
+
@last_time = current_time
|
26
|
+
end
|
27
|
+
|
28
|
+
def next_id
|
29
|
+
time = current_time
|
30
|
+
|
31
|
+
raise InvalidSystemClockError, "(#{time} < #{@last_time})" if time < @last_time
|
32
|
+
|
33
|
+
if time == @last_time
|
34
|
+
@sequence = (@sequence + 1) % MAX_SEQUENCE
|
35
|
+
# NOTE: Distributed in node_id so if more than MAX_SEQUENCE, or wait until the next time.
|
36
|
+
# time = till_next_time if @sequence == 0
|
37
|
+
else
|
38
|
+
@sequence = 0
|
39
|
+
end
|
40
|
+
|
41
|
+
@last_time = time
|
42
|
+
|
43
|
+
compose(@last_time, @node_id, @sequence)
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse(flake_id)
|
47
|
+
AnyFlake.parse(flake_id, @target_epoch)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.parse(flake_id, target_epoch)
|
51
|
+
hash = {}
|
52
|
+
hash[:epoch_time] = flake_id >> (SEQUENCE_BITS + NODE_ID_BITS)
|
53
|
+
hash[:time] = Time.at((hash[:epoch_time] + target_epoch) / 1000.0)
|
54
|
+
hash[:node_id] = (flake_id >> SEQUENCE_BITS).to_s(2)[-NODE_ID_BITS, NODE_ID_BITS].to_i(2)
|
55
|
+
hash[:sequence] = flake_id.to_s(2)[-SEQUENCE_BITS, SEQUENCE_BITS].to_i(2)
|
56
|
+
hash
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def compose(last_time, node_id, sequence)
|
62
|
+
((last_time - @target_epoch) << (SEQUENCE_BITS + NODE_ID_BITS)) +
|
63
|
+
(node_id << SEQUENCE_BITS) +
|
64
|
+
sequence
|
65
|
+
end
|
66
|
+
|
67
|
+
def current_time
|
68
|
+
Time.now.strftime('%s%L').to_i
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class AnyFlake::OverflowError < StandardError; end
|
73
|
+
class AnyFlake::InvalidSystemClockError < StandardError; end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: anyflake
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshimasa KISHIMOTO
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Pure ruby independent ID generator like the SnowFlake, ChiliFlake
|
56
|
+
email:
|
57
|
+
- joshimasa.kishimoto@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- anyflake.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/anyflake.rb
|
73
|
+
- lib/anyflake/version.rb
|
74
|
+
homepage: https://github.com/jkishimoto/anyflake
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.5.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Pure ruby independent ID generator like the SnowFlake, ChiliFlake
|
98
|
+
test_files: []
|