lsuuid 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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +24 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/lsuuid/version.rb +5 -0
- data/lib/lsuuid.rb +52 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cc6dd220066b3a608eeaaccf05b8d2e5004c24f587bb34748d74af42a030a1e0
|
4
|
+
data.tar.gz: 85dfc3291c452dec71a775744599b45da7e4d3ce3626f1352becd92269c688fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 804a0e9517a4505fd41b8b4f111ed185051ede0b21e72a0eeda35b80ddaa900d94a68e81b2ee88b2866c3ef5dce73903931a3d3d86d9cce0d4be0be61c1018c5
|
7
|
+
data.tar.gz: e5a03f3d374062219cf11681661f9dd5a88259a0f7b82fdca2bd6d3373ae5e1364e60f01bd4b1843f8897e864f2afa3de5f28a4948d38399c051930c679d3e49
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Style/StringLiterals:
|
6
|
+
Enabled: true
|
7
|
+
EnforcedStyle: single_quotes
|
8
|
+
|
9
|
+
Style/StringLiteralsInInterpolation:
|
10
|
+
Enabled: true
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
Style/ClassAndModuleChildren:
|
14
|
+
Enabled: true
|
15
|
+
EnforcedStyle: compact
|
16
|
+
|
17
|
+
Style/RedundantSelf:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Layout/LineLength:
|
21
|
+
Max: 120
|
22
|
+
|
23
|
+
Metrics/AbcSize:
|
24
|
+
Max: 20
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Karthick
|
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,113 @@
|
|
1
|
+
# LSUUID (Lexicographically Sortable Universally Unique Identifier)
|
2
|
+
|
3
|
+
LSUUID is a lexicographically sortable identifier with format similar to UUIDv4.
|
4
|
+
UUID has some limitations which are already listed and addressed by [ULID](https://github.com/ulid/spec).
|
5
|
+
|
6
|
+
LSUUID provides the following improvements over ulid :-
|
7
|
+
- The timestamp component in ulid is in milliseconds whereas LSUUID uses nanoseconds so that chance of collision is reduced 1000 times
|
8
|
+
- Ability to bucket the generated identifiers with a prefix is provided by LSUUID
|
9
|
+
- LSUUID adheres to the UUIDv4 format there by allowing the storage of the identifier in the UUID datatype of data stores which is more efficient than a string which is required for ulid.
|
10
|
+
|
11
|
+
*Note: For use cases where format of the identifier is not a matter of concern, using ULID over LSUUID might be a better choice.*
|
12
|
+
|
13
|
+
## Specification
|
14
|
+
Below is the structure of LSUUID.
|
15
|
+
|
16
|
+
**Without Prefix:**
|
17
|
+
```
|
18
|
+
Sample LSUUID: 619bd0ef-2a04-5068-d3c4-f6fac6809edb
|
19
|
+
|
20
|
+
619bd0ef-2a04-5068- d3c4-f6fac6809edb
|
21
|
+
|
22
|
+
|------------------| |-----------------|
|
23
|
+
Timestamp Randomness
|
24
|
+
64bits 64bits
|
25
|
+
```
|
26
|
+
|
27
|
+
**With Prefix:**
|
28
|
+
```
|
29
|
+
Sample LSUUID: 619bd0ef-2a04-5068-d3c4-f6fac6809edb
|
30
|
+
|
31
|
+
619bd0 ef-2a04-5068-d3c4-f6 fac6809edb
|
32
|
+
|
33
|
+
|------| |--------------------| |----------|
|
34
|
+
Prefix Timestamp Randomness
|
35
|
+
24bits 64bits 40bits
|
36
|
+
```
|
37
|
+
|
38
|
+
### Components:
|
39
|
+
|
40
|
+
**Timestamp**
|
41
|
+
- 64 bit integer
|
42
|
+
- UNIX-time in nanoseconds
|
43
|
+
|
44
|
+
**Randomness**
|
45
|
+
- 64 bits (40 bits in case a prefix is specified)
|
46
|
+
- Cryptographically secure source of randomness, if possible
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
Add this line to your application's Gemfile:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gem 'lsuuid'
|
54
|
+
```
|
55
|
+
|
56
|
+
And then execute:
|
57
|
+
|
58
|
+
$ bundle install
|
59
|
+
|
60
|
+
Or install it yourself as:
|
61
|
+
|
62
|
+
$ gem install lsuuid
|
63
|
+
|
64
|
+
## Usage
|
65
|
+
|
66
|
+
### Generate a UUID:
|
67
|
+
```ruby
|
68
|
+
irb(main):001:0> LSUUID.generate
|
69
|
+
=> "619bd0ef-2a04-5068-d3c4-f6fac6809edb"
|
70
|
+
```
|
71
|
+
Generates a UUID based on the method call time.
|
72
|
+
### Timestamp specific UUIDs:
|
73
|
+
```ruby
|
74
|
+
irb(main):001:0> t = Time.now
|
75
|
+
=> 2021-11-22 22:49:02 +0530
|
76
|
+
irb(main):002:0> LSUUID.generate(time: t)
|
77
|
+
=> "619bd106-0c6b-ba28-7811-f5787dab02d4"
|
78
|
+
```
|
79
|
+
### Modes:
|
80
|
+
#### **ceil**
|
81
|
+
```ruby
|
82
|
+
irb(main):001:0> LSUUID.generate(time: t, mode: :ceil)
|
83
|
+
=> "619bd106-0c6b-ba28-ffff-ffffffffffff"
|
84
|
+
irb(main):002:0> LSUUID.generate(time: t, mode: :ceil, prefix: 0)
|
85
|
+
=> "00000061-9bd1-060c-6bba-28ffffffffff"
|
86
|
+
```
|
87
|
+
|
88
|
+
#### **floor**
|
89
|
+
```ruby
|
90
|
+
irb(main):001:0> LSUUID.generate(time: t, mode: :floor)
|
91
|
+
=> "619bd106-0c6b-ba28-0000-000000000000"
|
92
|
+
irb(main):002:0> LSUUID.generate(time: t, mode: :ceil, prefix: 0)
|
93
|
+
=> "00000061-9bd1-060c-6bba-280000000000"
|
94
|
+
```
|
95
|
+
|
96
|
+
### Prefix
|
97
|
+
```ruby
|
98
|
+
irb(main):001:0> LSUUID.generate(prefix: 0, time: t)
|
99
|
+
=> "00000061-9bd1-060c-6bba-28428c7e196d"
|
100
|
+
irb(main):002:0> LSUUID.generate(prefix: 1000, time: t)
|
101
|
+
=> "0003e861-9bd1-060c-6bba-28d9cb973352"
|
102
|
+
irb(main):003:0> LSUUID.generate(prefix: 16_777_215, time: t)
|
103
|
+
=> "ffffff61-9bd1-060c-6bba-2826264c51e0"
|
104
|
+
```
|
105
|
+
**Note:** Prefix can be an integer between 0 and 16,777,215
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hwslabs/lsuuid.
|
110
|
+
|
111
|
+
## License
|
112
|
+
|
113
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'lsuuid'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require 'pry'
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/lsuuid.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lsuuid/version'
|
4
|
+
|
5
|
+
if RUBY_VERSION >= '2.5'
|
6
|
+
require 'securerandom'
|
7
|
+
else
|
8
|
+
require 'sysrandom/securerandom'
|
9
|
+
end
|
10
|
+
|
11
|
+
module LSUUID # :nodoc:
|
12
|
+
PREFIX_LEN = 6
|
13
|
+
PAD_CHAR = '0'
|
14
|
+
FLOOR_CHAR = '0'
|
15
|
+
CEIL_CHAR = 'f'
|
16
|
+
DEFAULT_RAND_LEN = 16
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# We allow 6 chars (24 bits) of UUID to be customizable as per requirement.
|
20
|
+
def generate(prefix: nil, time: Time.now, mode: :random)
|
21
|
+
prefix_hex = validate_prefix_and_get_hex(prefix)
|
22
|
+
result = prefix_hex +
|
23
|
+
time.to_i.to_s(16).rjust(8, PAD_CHAR) + time.nsec.to_s(16).rjust(8, PAD_CHAR) +
|
24
|
+
random_hex(prefix_hex, mode)
|
25
|
+
[result[0..7], result[8..11], result[12..15], result[16..19], result[20..31]].join('-')
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def random_hex(prefix_hex, mode)
|
31
|
+
rand_len = DEFAULT_RAND_LEN - prefix_hex.size
|
32
|
+
case mode
|
33
|
+
when :random then SecureRandom.hex(rand_len / 2)
|
34
|
+
when :ceil then ''.rjust(rand_len, CEIL_CHAR)
|
35
|
+
when :floor then ''.rjust(rand_len, FLOOR_CHAR)
|
36
|
+
else raise "Unknown mode [#{mode}]"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def validate_prefix_and_get_hex(prefix)
|
41
|
+
return '' if prefix.nil?
|
42
|
+
|
43
|
+
raise "Expecting integer Got #{prefix.class.name}" unless prefix.is_a? Integer
|
44
|
+
|
45
|
+
if (prefix > 16_777_215) || prefix.negative?
|
46
|
+
raise "Integer prefix out of range. Allowed: min - 0, max - 16,777,215 (hex: ffffff). Got: #{prefix}."
|
47
|
+
end
|
48
|
+
|
49
|
+
prefix.to_s(16).rjust(PREFIX_LEN, PAD_CHAR)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lsuuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hypto Engineering Team
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Universally Unique Lexicographically Sortable Identifier
|
14
|
+
email:
|
15
|
+
- engineering@hypto.in
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/lsuuid.rb
|
30
|
+
- lib/lsuuid/version.rb
|
31
|
+
homepage: https://github.com/hwslabs/lsuuid
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/hwslabs/lsuuid
|
36
|
+
source_code_uri: https://github.com/hwslabs/lsuuid
|
37
|
+
changelog_uri: https://github.com/hwslabs/lsuuid/blob/main/CHANGELOG.md
|
38
|
+
rubygems_mfa_required: 'true'
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.4.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.0.3
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Universally Unique Lexicographically Sortable Identifier
|
58
|
+
test_files: []
|