uuid7 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b24060fdf8c607bf359e96657d4f160151726fffd5c08a34c72f2bd1043ac0d
4
- data.tar.gz: d767c79de58c110731b009c74952a14b4ba15c42daaa8efaa761eb7a6e79ce51
3
+ metadata.gz: ef7855cbafb96446c9a4acbd94485d8d5a9fa9af5c399d8781f02931a4fb36c1
4
+ data.tar.gz: 1dad8f699065d560d66e2cb9ccd231a885a7b293057539faf11ccfce9032532e
5
5
  SHA512:
6
- metadata.gz: c36b7c1219d9f57c8e5dc1a5a9b8d314731daff3ac173e8225710508d86ec653a91d4f1b30461ee1955638179ba3919a3dd38df875b4400d73bf4d24fd4b1eff
7
- data.tar.gz: fe600303dfb8942c46f2fb978f8037786d9c6214a485032d40f95249924bd2813f41c4c69c72b39292d08e85519bea836a05f8daf82350ab18e9ffca544e071d
6
+ metadata.gz: 50048c0238cefa15be0b36029bc77c373bc52b6a3795d8c84f379cb45f54706abaf9e71a9558973cdae998878de2023e5f69388788f2fdc8356358d086f24248
7
+ data.tar.gz: 37a2c55d277e58ea1d24f362758c193fbfbc30a7103a66997416518c5759f4d0b067d72ef62d37b1ae2fad949a65d1d6d15a2e9afc9876398a19a7eb9e080ceb
@@ -5,9 +5,8 @@ module UUID7
5
5
  #
6
6
  # @api private
7
7
  #
8
- # 32 bits for unix timestamp seconds
9
- # 12 bits for millisecond precision
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 and sequence count
19
- # UUID::V7.generate(timestamp, sequence)
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, seq)
25
- unixts = timestamp / 1_000 # seconds of unix timestamp
26
- unixts1 = (unixts >> 4) & 0xffffffff # take 32 most significant bits of 36
27
- unixts2 = (unixts & 0xf) << 12 # take 4 least significant bits of 36
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
- msec = timestamp % 1_000 # milliseconds of unix timestamp
30
- msec &= 0xfff # take 12 bits of 16
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
- rand1, rand2, rand3 = SecureRandom.gen_random(8).unpack("nnN")
33
- rand1 &= 0x3fff # take 14 bits of 16
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
- # | unixts1 |
35
+ # | unix_ts_ms |
40
36
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41
- # |unixts2| msec | ver | seq |
37
+ # | unix_ts_ms | ver | rand_a |
42
38
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43
- # |var| rand1 | rand2 |
39
+ # |var| rand_b |
44
40
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45
- # | rand3 |
41
+ # | rand_b |
46
42
  # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43
+
44
+ # layout in bits [32, 16, 16, 16, 16, 32]
47
45
  [
48
- unixts1,
49
- (unixts2 | msec),
50
- (VERSION_7 | seq),
51
- (VARIANT_RFC4122 | rand1),
52
- rand2,
53
- rand3
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UUID7
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
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(*Sequencer.sequence(timestamp)))
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.1.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: 2021-09-25 00:00:00.000000000 Z
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.5.0
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.2.15
60
+ rubygems_version: 3.3.7
62
61
  signing_key:
63
62
  specification_version: 4
64
63
  summary: UUID v7 generator
@@ -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