global_uid 4.0.0.beta2 → 4.0.0.rc1

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: f572a1f8e4c4e113bbe071eedc10d0311a89827adfa318dd577e3ec0b0fc12bd
4
- data.tar.gz: 5075c514fbbb2f7fef0c38534ae094779d98b536924dcb5e19bb031f9273877e
3
+ metadata.gz: dd050a7d1631dd7ab255069ec2b83a54df5f667660ed99666f183cc221d5a03f
4
+ data.tar.gz: 1209ad46e05ec878ee018d1fbb7c51d97b300db054551771b7df178aae9ae9ff
5
5
  SHA512:
6
- metadata.gz: 7e2cdf576215f37dfdf18e132b86d80f088d6c40f7a751bdf0ed95965596389cfc61df046fa1aa0136b7f0f56c091a9cf8d869a3c2f06395bb44a4bf4362d5f1
7
- data.tar.gz: c8f1c21a6259983780c860298b9ab119aeea9f9d4c5541940a78b5433dab9df6a01b26ca0567fb4b395cf375a84fe027a8c0efaaeb9feb77dc3ca9f63631fe84
6
+ metadata.gz: 390c50c8f7a67d6693d45a029746c0cedddb52ec60f90f867388bbff4e9c1177d96827da3e0c9e0923a9fe6f98914e27adfc34c4f5ecc1097f332175354f4916
7
+ data.tar.gz: 6c37206cf9942f59fb07b2932799debd9f6d58a013e7af7035be252a9e6365c55353da711963ad5722d202080ce5cb8c26b36766a8f4b66ac95632672d66feb2
@@ -48,7 +48,7 @@ module GlobalUid
48
48
  end
49
49
 
50
50
  def global_uid_table
51
- GlobalUid::Base.id_table_from_name(self.table_name)
51
+ @_global_uid_table ||= GlobalUid::Base.id_table_from_name(self.table_name)
52
52
  end
53
53
  end
54
54
  end
@@ -1,26 +1,26 @@
1
1
  module GlobalUid
2
2
  class Allocator
3
- attr_reader :recent_allocations, :max_window_size, :incrementing_by, :connection
3
+ attr_reader :recent_allocations, :max_window_size, :incrementing_by, :connection, :table_name
4
4
 
5
- def initialize(incrementing_by:, connection:)
5
+ def initialize(incrementing_by:, connection:, table_name:)
6
6
  @recent_allocations = []
7
7
  @max_window_size = 5
8
8
  @incrementing_by = incrementing_by
9
9
  @connection = connection
10
- validate_connection_increment
10
+ @table_name = table_name
11
11
  end
12
12
 
13
- def allocate_one(table)
14
- identifier = connection.insert("REPLACE INTO #{table} (stub) VALUES ('a')")
13
+ def allocate_one
14
+ identifier = connection.insert("REPLACE INTO #{table_name} (stub) VALUES ('a')")
15
15
  allocate(identifier)
16
16
  end
17
17
 
18
- def allocate_many(table, count:)
18
+ def allocate_many(count:)
19
19
  return [] unless count > 0
20
20
 
21
- increment_by = validate_connection_increment
21
+ increment_by = connection.select_value("SELECT @@auto_increment_increment")
22
22
 
23
- start_id = connection.insert("REPLACE INTO #{table} (stub) VALUES " + (["('a')"] * count).join(','))
23
+ start_id = connection.insert("REPLACE INTO #{table_name} (stub) VALUES " + (["('a')"] * count).join(','))
24
24
  identifiers = start_id.step(start_id + (count - 1) * increment_by, increment_by).to_a
25
25
  identifiers.each { |identifier| allocate(identifier) }
26
26
  identifiers
@@ -34,8 +34,8 @@ module GlobalUid
34
34
 
35
35
  if !valid_allocation?
36
36
  db_increment = connection.select_value("SELECT @@auto_increment_increment")
37
- message = "Configured: '#{incrementing_by}', Found: '#{db_increment}' on '#{connection.current_database}'. Recently allocated IDs: #{recent_allocations}"
38
- alert(InvalidIncrementException.new(message))
37
+ message = "Configured: '#{incrementing_by}', Found: '#{db_increment}' on '#{connection.current_database}'. Recently allocated IDs: #{recent_allocations} using table '#{table_name}'"
38
+ GlobalUid::Base.alert(InvalidIncrementException.new(message))
39
39
  end
40
40
 
41
41
  identifier
@@ -47,23 +47,5 @@ module GlobalUid
47
47
  (identifier - recent_allocations[0]) % incrementing_by == 0
48
48
  end
49
49
  end
50
-
51
- def validate_connection_increment
52
- db_increment = connection.select_value("SELECT @@auto_increment_increment")
53
-
54
- if db_increment != incrementing_by
55
- alert(InvalidIncrementException.new("Configured: '#{incrementing_by}', Found: '#{db_increment}' on '#{connection.current_database}'"))
56
- end
57
-
58
- db_increment
59
- end
60
-
61
- def alert(exception)
62
- if GlobalUid.configuration.suppress_increment_exceptions?
63
- GlobalUid.configuration.notifier.call(exception)
64
- else
65
- raise exception
66
- end
67
- end
68
50
  end
69
51
  end
@@ -68,5 +68,13 @@ module GlobalUid
68
68
  def self.id_table_from_name(name)
69
69
  "#{name}_ids".to_sym
70
70
  end
71
+
72
+ def self.alert(exception)
73
+ if GlobalUid.configuration.suppress_increment_exceptions?
74
+ GlobalUid.configuration.notifier.call(exception)
75
+ else
76
+ raise exception
77
+ end
78
+ end
71
79
  end
72
80
  end
@@ -7,7 +7,7 @@ module GlobalUid
7
7
  @connection = nil
8
8
  @name = name
9
9
  @retry_at = nil
10
- @allocator = nil
10
+ @allocators = {}
11
11
  @increment_by = increment_by
12
12
  @connection_retry = connection_retry
13
13
  @connection_timeout = connection_timeout
@@ -19,7 +19,7 @@ module GlobalUid
19
19
  @connection = mysql2_connection(name)
20
20
 
21
21
  begin
22
- @allocator = Allocator.new(incrementing_by: increment_by, connection: @connection) if active?
22
+ validate_connection_increment if active?
23
23
  rescue InvalidIncrementException => e
24
24
  GlobalUid.configuration.notifier.call(e)
25
25
  disconnect!
@@ -42,7 +42,7 @@ module GlobalUid
42
42
 
43
43
  def disconnect!
44
44
  @connection = nil
45
- @allocator = nil
45
+ @allocators = {}
46
46
  end
47
47
 
48
48
  def create_uid_table!(name:, uid_type: nil, start_id: nil)
@@ -69,16 +69,21 @@ module GlobalUid
69
69
  # Timeout.timeout is unpredictable
70
70
  Timeout.timeout(query_timeout, TimeoutException) do
71
71
  if count == 1
72
- allocator.allocate_one(klass.global_uid_table)
72
+ allocator(klass).allocate_one
73
73
  else
74
- allocator.allocate_many(klass.global_uid_table, count: count)
74
+ allocator(klass).allocate_many(count: count)
75
75
  end
76
76
  end
77
77
  end
78
78
 
79
79
  private
80
80
 
81
- attr_accessor :connection_retry, :connection_timeout, :retry_at, :increment_by, :query_timeout, :allocator
81
+ attr_accessor :connection_retry, :connection_timeout, :retry_at, :increment_by, :query_timeout, :allocators
82
+
83
+ def allocator(klass)
84
+ table_name = klass.global_uid_table
85
+ @allocators[table_name] ||= Allocator.new(incrementing_by: increment_by, connection: connection, table_name: table_name)
86
+ end
82
87
 
83
88
  def retry_connection?
84
89
  return Time.now > retry_at if retry_at
@@ -105,5 +110,12 @@ module GlobalUid
105
110
  nil
106
111
  end
107
112
 
113
+ def validate_connection_increment
114
+ db_increment = connection.select_value("SELECT @@auto_increment_increment")
115
+
116
+ if db_increment != increment_by
117
+ GlobalUid::Base.alert(InvalidIncrementException.new("Configured: '#{increment_by}', Found: '#{db_increment}' on '#{connection.current_database}'"))
118
+ end
119
+ end
108
120
  end
109
121
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: global_uid
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.beta2
4
+ version: 4.0.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Quorning
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2020-04-30 00:00:00.000000000 Z
14
+ date: 2020-05-11 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord