ruby_xid 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruby_xid.rb +48 -56
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f11215b569bbc9b0acc216e30731fcd0dc84a3a26d7e771c131820ede1e0f8d
|
4
|
+
data.tar.gz: 44dc8ffb01a7a9409699af142df483c30056afa5d981925deb35b9476dbb433c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1b5be1db1fc6c817d78ba1fff9229c69c7d9d5a5ba83a40c1302f5f23bf8adea0837e53a03eb93faf7505b5d4e581aab43351c73dbefc27fbfd763bd3eb2c67
|
7
|
+
data.tar.gz: 6ae7fe4347442396c7ccc7fb6ae8240329f0ef9b9290ca14da7cf899a9befbfa1e96143abf7fb9857f931982b95f3af4e2dc52799392cf46769c66ae1c5afe8f
|
data/lib/ruby_xid.rb
CHANGED
@@ -1,32 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# Xid implementatin in Ruby
|
3
|
+
require 'socket'
|
4
|
+
require 'securerandom'
|
5
|
+
require 'date'
|
6
|
+
|
2
7
|
class Xid
|
3
|
-
require 'socket'
|
4
|
-
require 'securerandom'
|
5
|
-
require 'date'
|
6
8
|
|
7
9
|
RAW_LEN = 12
|
8
10
|
TRIM_LEN = 20
|
9
11
|
|
10
|
-
|
12
|
+
@@generator = nil
|
11
13
|
|
12
14
|
def initialize(id = nil)
|
13
|
-
|
14
|
-
|
15
|
-
@pid = Process.pid
|
16
|
-
@machine_id = real_machine_id
|
17
|
-
unless id.nil?
|
18
|
-
# Decoded array
|
19
|
-
@value = id
|
20
|
-
else
|
21
|
-
generate_xid
|
22
|
-
end
|
15
|
+
@@generator ||= Generator.new(init_rand_int, real_machine_id)
|
16
|
+
@byte_str = id ? id.map(&:chr).join('') : @@generator.next_xid
|
23
17
|
end
|
24
18
|
|
25
|
-
def
|
26
|
-
@value =
|
19
|
+
def next
|
20
|
+
@string = @value = nil
|
21
|
+
@byte_str = @@generator.next_xid
|
27
22
|
string
|
28
23
|
end
|
29
24
|
|
25
|
+
def value
|
26
|
+
@value ||= @byte_str.chars.map(&:ord)
|
27
|
+
end
|
28
|
+
|
30
29
|
def pid
|
31
30
|
# type: () -> int
|
32
31
|
(value[7] << 8 | value[8])
|
@@ -51,37 +50,49 @@ class Xid
|
|
51
50
|
value[0] << 24 | value[1] << 16 | value[2] << 8 | value[3]
|
52
51
|
end
|
53
52
|
|
53
|
+
def inspect
|
54
|
+
"Xid('#{string}')"
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_s
|
58
|
+
string
|
59
|
+
end
|
60
|
+
|
54
61
|
def string
|
55
62
|
# type: () -> str
|
56
|
-
Base32.b32encode(value)[0..TRIM_LEN - 1]
|
63
|
+
@string ||= Base32.b32encode(value)[0..TRIM_LEN - 1]
|
57
64
|
end
|
58
65
|
|
59
66
|
def bytes
|
60
67
|
# type: () -> str
|
61
|
-
|
68
|
+
@byte_str
|
62
69
|
end
|
63
70
|
|
64
71
|
def init_rand_int
|
65
72
|
# type: () -> int
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
73
|
+
SecureRandom.random_number(16_777_215)
|
74
|
+
end
|
75
|
+
|
76
|
+
def real_machine_id
|
77
|
+
# type: () -> int
|
78
|
+
Digest::MD5.digest(Socket.gethostname).unpack('N')[0]
|
79
|
+
rescue
|
80
|
+
init_rand_int
|
70
81
|
end
|
71
82
|
|
72
83
|
def ==(other_xid)
|
73
84
|
# type: (Xid) -> bool
|
74
|
-
|
85
|
+
to_s == other_xid.to_s
|
75
86
|
end
|
76
87
|
|
77
88
|
def <(other_xid)
|
78
89
|
# type: (Xid) -> bool
|
79
|
-
|
90
|
+
to_s < other_xid.to_s
|
80
91
|
end
|
81
92
|
|
82
93
|
def >(other_xid)
|
83
94
|
# type: (Xid) -> bool
|
84
|
-
|
95
|
+
to_s > other_xid.to_s
|
85
96
|
end
|
86
97
|
|
87
98
|
def self.from_string(str)
|
@@ -97,44 +108,25 @@ class Xid
|
|
97
108
|
Object.const_get(name).new(val)
|
98
109
|
end
|
99
110
|
|
100
|
-
|
111
|
+
# Xid Generator
|
112
|
+
class Generator
|
113
|
+
attr_accessor :value
|
101
114
|
|
102
|
-
def
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
rescue
|
108
|
-
SecureRandom.hex(3).scan(/.{2}/m).map(&:hex)
|
115
|
+
def initialize(rand_val = nil, machine_id = 0)
|
116
|
+
@mutex = Mutex.new
|
117
|
+
@rand_int = rand_val || rand(65_535)
|
118
|
+
@pid = Process.pid
|
119
|
+
@machine_id = machine_id
|
109
120
|
end
|
110
121
|
|
111
|
-
def
|
112
|
-
#
|
113
|
-
now = Time.now.to_i
|
114
|
-
@value = Array.new(RAW_LEN, 0)
|
115
|
-
|
116
|
-
@value[0] = (now >> 24) & 0xff
|
117
|
-
@value[1] = (now >> 16) & 0xff
|
118
|
-
@value[2] = (now >> 8) & 0xff
|
119
|
-
@value[3] = now & 0xff
|
120
|
-
|
121
|
-
@value[4] = @machine_id[0]
|
122
|
-
@value[5] = @machine_id[1]
|
123
|
-
@value[6] = @machine_id[2]
|
124
|
-
|
125
|
-
@value[7] = (@pid >> 8) & 0xff
|
126
|
-
@value[8] = @pid & 0xff
|
127
|
-
|
122
|
+
def next_xid
|
123
|
+
# () -> str
|
128
124
|
@mutex.synchronize do
|
129
125
|
@rand_int += 1
|
130
126
|
end
|
131
|
-
|
132
|
-
@value[9] = (@rand_int >> 16) & 0xff
|
133
|
-
@value[10] = (@rand_int >> 8) & 0xff
|
134
|
-
@value[11] = @rand_int & 0xff
|
135
|
-
|
136
|
-
@value
|
127
|
+
[::Time.new.to_i, @machine_id, @pid, @rand_int << 8].pack('N NX n NX')
|
137
128
|
end
|
129
|
+
end
|
138
130
|
end
|
139
131
|
|
140
132
|
require_relative 'xid/base32'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_xid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valarpiraichandran
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: a.valarpirai@gmail.com
|
@@ -30,7 +30,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.9.3
|
34
34
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
35
|
requirements:
|
36
36
|
- - ">="
|