snowflakes 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80ad868878f31b343d0e8c5c923e34d522a3cf35
4
+ data.tar.gz: b0edc0f6abce38e7a69c7464fff5c50953453a16
5
+ SHA512:
6
+ metadata.gz: 81f8391c160f8380cd33a9b8ff39eb07f3ecf99825055fd125214fbeefb68c3c2a9e2e7d5321c3bf1ed3330bc43439309e519e280695caee227fa1512db73a65
7
+ data.tar.gz: 6d80c750027a844a0e4130317581d2f5233960024cc73c212f9b66953649a80aa8fe6b3d15f5b8c9718b0f311195b87273d79a52f8356d46d08275f55af07610
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Yang Sheng Han
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,62 @@
1
+ # Snowflakes
2
+
3
+ Snowflakes is a [Ruby](https://www.ruby-lang.org/) gem that provides:
4
+ * A simple 128-bit id generator.
5
+ * Methods to convert snowflakes ID into string
6
+
7
+ Snowflakes produces 128-bit and time-ordered ids. They run one on each node in infrastructure and will generate conflict-free ids on-demand without coordination.
8
+
9
+ The project is inspired by [Twitter's Snowflake](https://github.com/twitter/snowflake) and [Go implementation of Discord](https://github.com/bwmarrin/snowflake) but extended to 128-bit and not compatible with Twitter's Snowflake. This library provides a basis for id generation but not a service for handing out ids nor node id coordination.
10
+
11
+ ## Getting Started
12
+
13
+ ### Installation
14
+
15
+ ```sh
16
+ gem install snowflakes
17
+ ```
18
+
19
+ Or via bundler:
20
+ ```
21
+ source 'https://rubygems.org'
22
+
23
+ gem 'snowflakes'
24
+ ```
25
+
26
+ ### Usage
27
+
28
+ ```ruby
29
+ node = Snowflakes::Node.new(ENV['SNOWFLAKE_ID'])
30
+
31
+ some_id = node.next.to_hex
32
+ ```
33
+
34
+ ### Format
35
+
36
+ Snowflakes ids are 128-bits wide described here from most significant to least significant bits:
37
+ * timestamp (64-bit) - milliseconds since the epoch (Jan 1 1970)
38
+ * node id(48-bit) - a configurable node id
39
+ * sequence (16-bit) - usually 0, increasing when more than one request in the same millisecond and reset to 0 when clock ticks forward
40
+
41
+ #### System Clock Dependency
42
+
43
+ You should use NTP to keep your system clock accurate. Snowflakes holds requests when non-monotonic clock detected. To run in a mode where NTP not move the clock backwards, see http://wiki.dovecot.org/TimeMovedBackwards#Time_synchronization for tips on how to do this.
44
+
45
+ ## Related Projects
46
+
47
+ * [progamesigner/snowflakes-php](https://github.com/progamesigner/snowflakes-php)
48
+
49
+ ## Maintainers
50
+
51
+ [@progamesigner](https://github.com/progamesigner).
52
+
53
+ ## Contribute
54
+
55
+ 1. **Fork** the repo on GitHub
56
+ 2. **Clone** the project to your own machine
57
+ 3. **Commit** changes to your own branch
58
+ 4. **Push** your work back up to your fork
59
+ 5. Submit a **Pull request** so that I can review your changes
60
+
61
+ ## License
62
+ [MIT](LICENSE) © Yang Sheng Han
@@ -0,0 +1,125 @@
1
+ module Snowflakes
2
+ class OverflowError < StandardError
3
+ def initialize(topic, current, maximum)
4
+ formats = {
5
+ topic: topic,
6
+ current: current,
7
+ maximum: maximum
8
+ }
9
+
10
+ if current < 0
11
+ super('Invalid %{topic} (${current} < 0)' % formats)
12
+ else
13
+ super('Invalid %{topic} (${current} > ${maximum})' % formats)
14
+ end
15
+ end
16
+ end
17
+
18
+ class InvalidSystemClockError < StandardError
19
+ def initialize(time, last)
20
+ super('Time goes backward! (%{time} < %{last})' % { time: time, last: last })
21
+ end
22
+ end
23
+
24
+ class Snowflake
25
+ def initialize(u, l)
26
+ @u = u
27
+ @l = l
28
+ end
29
+
30
+ def to_hex
31
+ '%016x%016x' % [@u, @l]
32
+ end
33
+ end
34
+
35
+ class Node
36
+ STEP_BITS = 16
37
+ NODE_BITS = 48
38
+
39
+ MAX_STEPS = (1 << STEP_BITS)
40
+ MAX_NODES = (1 << NODE_BITS)
41
+
42
+ def initialize(id, step = 0, since = 0)
43
+ if not id.is_a? Integer or id < 0 or id >= MAX_NODES
44
+ raise OverflowError.new('id', id, MAX_NODES)
45
+ end
46
+
47
+ if not step.is_a? Integer or step < 0 or step >= MAX_STEPS
48
+ raise OverflowError.new('step', step, MAX_STEPS)
49
+ end
50
+
51
+ @id = id
52
+ @last = 0
53
+ @step = step
54
+ @since = since
55
+ end
56
+
57
+ def next
58
+ time = now
59
+
60
+ if time < @last
61
+ raise InvalidSystemClockError.new(time, @last)
62
+ end
63
+
64
+ if time == @last
65
+ @step = (@step + 1) % MAX_STEPS
66
+ if @step == 0
67
+ time = waitUntilNextMillisecond(time)
68
+ end
69
+ else
70
+ @step = 0
71
+ end
72
+
73
+ @last = time
74
+
75
+ generate(time, @id, @step)
76
+ end
77
+
78
+ def now
79
+ (Time.now.to_f * 1000).to_i - @since
80
+ end
81
+
82
+ def parse
83
+ end
84
+
85
+ private
86
+
87
+ def generate(time, id, step)
88
+ Snowflake.new(time, id << (STEP_BITS) + step)
89
+ end
90
+
91
+ def waitUntilNextMillisecond(time)
92
+ t = time
93
+ while t < time
94
+ t = now
95
+ end
96
+ t
97
+ end
98
+ end
99
+ end
100
+
101
+ # node = Snowflakes::Node.new(10)
102
+
103
+ # puts node.next.to_hex
104
+
105
+ # require 'benchmark'
106
+
107
+ # puts Benchmark.measure {
108
+ # 65_536.times do
109
+ # node.next.to_hex
110
+ # end
111
+ # }
112
+
113
+ # # (1/65535)*1e6 = 15.2590218967
114
+ # puts '%.12f nanoseconds' % (1_000_000_000 * Benchmark.realtime {
115
+ # 65_536.times do
116
+ # node.next.to_hex
117
+ # end
118
+ # } / 65_536)
119
+
120
+ # require 'ruby-prof'
121
+ # RubyProf::FlatPrinter.new(RubyProf.profile do
122
+ # 65_536.times do
123
+ # node.next
124
+ # end
125
+ # end).print(STDOUT)
@@ -0,0 +1,14 @@
1
+ module Snowflakes
2
+ def self.version
3
+ Gem::Version.new VERSION::STRING
4
+ end
5
+
6
+ module VERSION
7
+ MAJOR = 0
8
+ MINOR = 1
9
+ MICRO = 0
10
+ SURFIX = nil
11
+
12
+ STRING = [MAJOR, MINOR, MICRO, SURFIX].compact.join('.')
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path("../lib/snowflakes/version", __FILE__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'snowflakes'
5
+ spec.version = Snowflakes.version
6
+ spec.license = 'MIT'
7
+
8
+ spec.authors = [
9
+ 'Yang Sheng Han'
10
+ ]
11
+ spec.email = [
12
+ 'progamesigner@gmail.com'
13
+ ]
14
+ spec.homepage = 'https://github.com/progamesigner/snowflakes-gem'
15
+
16
+ spec.summary = 'A decentralized, k-ordered UUID generator in Ruby'
17
+ spec.description = 'Snowflakes produces 128-bit and time-ordered ids. They run one on each node in infrastructure and will generate conflict-free ids on-demand without coordination.'
18
+
19
+ spec.files = Dir[
20
+ 'README.md',
21
+ 'LICENSE',
22
+ 'snowflakes.gemspec',
23
+ 'lib/**/*.rb'
24
+ ]
25
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snowflakes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yang Sheng Han
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Snowflakes produces 128-bit and time-ordered ids. They run one on each
14
+ node in infrastructure and will generate conflict-free ids on-demand without coordination.
15
+ email:
16
+ - progamesigner@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - lib/snowflakes.rb
24
+ - lib/snowflakes/version.rb
25
+ - snowflakes.gemspec
26
+ homepage: https://github.com/progamesigner/snowflakes-gem
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.6.11
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A decentralized, k-ordered UUID generator in Ruby
50
+ test_files: []