uuid7 0.1.0 → 0.2.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 +4 -4
- data/lib/uuid7/generator.rb +25 -27
- data/lib/uuid7/version.rb +1 -1
- data/lib/uuid7.rb +1 -1
- metadata +4 -5
- data/lib/uuid7/sequencer.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef7855cbafb96446c9a4acbd94485d8d5a9fa9af5c399d8781f02931a4fb36c1
|
4
|
+
data.tar.gz: 1dad8f699065d560d66e2cb9ccd231a885a7b293057539faf11ccfce9032532e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50048c0238cefa15be0b36029bc77c373bc52b6a3795d8c84f379cb45f54706abaf9e71a9558973cdae998878de2023e5f69388788f2fdc8356358d086f24248
|
7
|
+
data.tar.gz: 37a2c55d277e58ea1d24f362758c193fbfbc30a7103a66997416518c5759f4d0b067d72ef62d37b1ae2fad949a65d1d6d15a2e9afc9876398a19a7eb9e080ceb
|
data/lib/uuid7/generator.rb
CHANGED
@@ -5,9 +5,8 @@ module UUID7
|
|
5
5
|
#
|
6
6
|
# @api private
|
7
7
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# 62 bits for random data
|
8
|
+
# 48 bits for unix timestamp with millisecond precision
|
9
|
+
# 74 bits for random data
|
11
10
|
#
|
12
11
|
module Generator
|
13
12
|
VERSION_7 = 0x7000
|
@@ -15,42 +14,41 @@ module UUID7
|
|
15
14
|
|
16
15
|
# Instantiates a new layout
|
17
16
|
#
|
18
|
-
# @example Generate a new UUID v7 layout for the millisecond
|
19
|
-
# UUID::V7.generate(timestamp
|
17
|
+
# @example Generate a new UUID v7 layout for the millisecond
|
18
|
+
# UUID::V7.generate(timestamp)
|
20
19
|
#
|
21
20
|
# @param timestamp [Integer] the timestamp to use for the layout
|
22
|
-
# @param seq [Integer] the sequence to use for the layout
|
23
21
|
# @return [Array<Integer>] the generated UUID v7 layout
|
24
|
-
def self.generate(timestamp
|
25
|
-
|
26
|
-
|
27
|
-
|
22
|
+
def self.generate(timestamp)
|
23
|
+
unix_ts_ms = timestamp & 0xffffffffffff # take 48 least significant bits of timestamp
|
24
|
+
unix_ts_ms1 = (unix_ts_ms >> 16) & 0xffffffff # take 32 most significant bits of timestamp
|
25
|
+
unix_ts_ms2 = (unix_ts_ms & 0xffff) # take 16 least significant bits of timestamp
|
28
26
|
|
29
|
-
|
30
|
-
|
27
|
+
rand_a, rand_b1, rand_b2, rand_b3 = SecureRandom.gen_random(10).unpack("nnnN")
|
28
|
+
rand_a &= 0xfff # take 12 bits of 16
|
29
|
+
rand_b1 &= 0x3fff # take 14 bits of 16
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
# https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-01#section-4.4.4.1
|
36
|
-
#
|
31
|
+
# https://www.ietf.org/id/draft-peabody-dispatch-new-uuid-format-03.txt#section-5.2
|
32
|
+
# 0 1 2 3
|
37
33
|
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
38
34
|
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
39
|
-
# |
|
35
|
+
# | unix_ts_ms |
|
40
36
|
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
41
|
-
# |
|
37
|
+
# | unix_ts_ms | ver | rand_a |
|
42
38
|
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
43
|
-
# |var|
|
39
|
+
# |var| rand_b |
|
44
40
|
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
45
|
-
# |
|
41
|
+
# | rand_b |
|
46
42
|
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
43
|
+
|
44
|
+
# layout in bits [32, 16, 16, 16, 16, 32]
|
47
45
|
[
|
48
|
-
|
49
|
-
|
50
|
-
(VERSION_7 |
|
51
|
-
(VARIANT_RFC4122 |
|
52
|
-
|
53
|
-
|
46
|
+
unix_ts_ms1,
|
47
|
+
unix_ts_ms2,
|
48
|
+
(VERSION_7 | rand_a),
|
49
|
+
(VARIANT_RFC4122 | rand_b1),
|
50
|
+
rand_b2,
|
51
|
+
rand_b3
|
54
52
|
]
|
55
53
|
end
|
56
54
|
end
|
data/lib/uuid7/version.rb
CHANGED
data/lib/uuid7.rb
CHANGED
@@ -18,6 +18,6 @@ module UUID7
|
|
18
18
|
# @param timestamp [Integer] the timestamp to use for UUID v7
|
19
19
|
# @return [String] the generated UUID v7 string
|
20
20
|
def self.generate(timestamp: Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond))
|
21
|
-
format("%08x-%04x-%04x-%04x-%04x%08x", *Generator.generate(
|
21
|
+
format("%08x-%04x-%04x-%04x-%04x%08x", *Generator.generate(timestamp))
|
22
22
|
end
|
23
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uuid7
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Obukhov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|
@@ -35,7 +35,6 @@ files:
|
|
35
35
|
- README.md
|
36
36
|
- lib/uuid7.rb
|
37
37
|
- lib/uuid7/generator.rb
|
38
|
-
- lib/uuid7/sequencer.rb
|
39
38
|
- lib/uuid7/version.rb
|
40
39
|
homepage: https://github.com/sprql/uuid7-ruby
|
41
40
|
licenses:
|
@@ -51,14 +50,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
52
51
|
- - ">="
|
53
52
|
- !ruby/object:Gem::Version
|
54
|
-
version: 2.
|
53
|
+
version: 2.6.0
|
55
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
55
|
requirements:
|
57
56
|
- - ">="
|
58
57
|
- !ruby/object:Gem::Version
|
59
58
|
version: '0'
|
60
59
|
requirements: []
|
61
|
-
rubygems_version: 3.
|
60
|
+
rubygems_version: 3.3.7
|
62
61
|
signing_key:
|
63
62
|
specification_version: 4
|
64
63
|
summary: UUID v7 generator
|
data/lib/uuid7/sequencer.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module UUID7
|
4
|
-
# Monotonic clock sequence counter
|
5
|
-
class Sequencer
|
6
|
-
def self.sequence(time)
|
7
|
-
@sequencer ||= new
|
8
|
-
@sequencer.sequence(time)
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@sequence = 0
|
13
|
-
@last_timestamp = 0
|
14
|
-
@mutex = Mutex.new
|
15
|
-
end
|
16
|
-
|
17
|
-
def sequence(timestamp)
|
18
|
-
@mutex.synchronize do
|
19
|
-
@sequence = if timestamp <= @last_timestamp
|
20
|
-
# TODO: handle overflow
|
21
|
-
((@sequence + 1) & 0xfff) # 12 bits
|
22
|
-
else
|
23
|
-
0
|
24
|
-
end
|
25
|
-
@last_timestamp = timestamp
|
26
|
-
end
|
27
|
-
|
28
|
-
[timestamp, @sequence]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|