static_uid 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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +12 -0
- data/lib/static_uid.rb +5 -0
- data/lib/static_uid/lookup_tables/million +1113024 -0
- data/lib/static_uid/million.rb +38 -0
- data/lib/static_uid/version.rb +3 -0
- data/spec/million_spec.rb +57 -0
- data/spec/spec_helper.rb +2 -0
- data/static_uid.gemspec +22 -0
- metadata +86 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
module StaticUID
|
2
|
+
class Million
|
3
|
+
|
4
|
+
def self.generate(i)
|
5
|
+
ensure_capacity!(i)
|
6
|
+
|
7
|
+
lookup_table[i]
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.capacity
|
11
|
+
lookup_table.size
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.fetch_lookup_table
|
15
|
+
capacity
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def self.ensure_capacity!(i)
|
21
|
+
if i >= capacity
|
22
|
+
raise RuntimeError, "Capacity (#{capacity}) exceeded with #{i}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.lookup_table
|
27
|
+
@lookup_table ||= build_lookup_table
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.build_lookup_table
|
31
|
+
File.open(lookup_table_file).to_a.map(&:strip)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.lookup_table_file
|
35
|
+
File.expand_path File.join('..', 'lookup_tables', 'million'), __FILE__
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
require 'static_uid/million'
|
5
|
+
|
6
|
+
describe StaticUID::Million do
|
7
|
+
let(:generator) { StaticUID::Million }
|
8
|
+
|
9
|
+
def generate(i)
|
10
|
+
generator.generate(i)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'generate' do
|
14
|
+
it 'emits a static uid for an integer' do
|
15
|
+
assert_kind_of String, generator.generate(22)
|
16
|
+
refute_predicate generate(22), :empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'emits different uids for different integers' do
|
20
|
+
refute_equal generate(1), generate(44)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'emits the same uid for equal input' do
|
24
|
+
assert_equal generate(332366), generate(332366)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'does not contain control chars at the end' do
|
28
|
+
refute_match(/\n$/, generate(22))
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'is free of collisions in the promised range' do
|
32
|
+
known = Set.new
|
33
|
+
|
34
|
+
1.upto(1_000_000).each do |i|
|
35
|
+
uid = generate(i)
|
36
|
+
refute_includes known, uid, "Collision on #{i}"
|
37
|
+
known << uid
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'fails if range is exceeded' do
|
42
|
+
assert_raises RuntimeError do
|
43
|
+
generate(generator.capacity)
|
44
|
+
end
|
45
|
+
|
46
|
+
assert_raises RuntimeError do
|
47
|
+
generate(2_000_000)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'capacity' do
|
53
|
+
it 'is over one million' do
|
54
|
+
assert_operator generator.capacity, :>, 1_000_000
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/static_uid.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'static_uid/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "static_uid"
|
8
|
+
spec.version = StaticUid::VERSION
|
9
|
+
spec.authors = ["Jakob Holderbaum"]
|
10
|
+
spec.email = ["jh@neopoly.de"]
|
11
|
+
spec.summary = %q{Generates static and readable UIDs}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: static_uid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakob Holderbaum
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-08 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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
|
+
description:
|
42
|
+
email:
|
43
|
+
- jh@neopoly.de
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/static_uid.rb
|
54
|
+
- lib/static_uid/lookup_tables/million
|
55
|
+
- lib/static_uid/million.rb
|
56
|
+
- lib/static_uid/version.rb
|
57
|
+
- spec/million_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
- static_uid.gemspec
|
60
|
+
homepage: ''
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.0.0
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Generates static and readable UIDs
|
84
|
+
test_files:
|
85
|
+
- spec/million_spec.rb
|
86
|
+
- spec/spec_helper.rb
|