global_uid 1.2.6 → 1.3.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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/ben/src/global_uid
3
3
  specs:
4
- global_uid (1.2.3)
4
+ global_uid (1.2.6)
5
5
  activerecord
6
6
  activesupport
7
7
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/ben/src/global_uid
3
3
  specs:
4
- global_uid (1.2.3)
4
+ global_uid (1.2.6)
5
5
  activerecord
6
6
  activesupport
7
7
 
data/global_uid.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'global_uid'
16
- s.version = '1.2.6'
16
+ s.version = '1.3.0'
17
17
  s.date = '2012-03-13'
18
18
  s.rubyforge_project = 'global_uid'
19
19
 
@@ -10,10 +10,13 @@ module GlobalUid
10
10
  return if GlobalUid::Base.global_uid_options[:disabled]
11
11
  return if self.class.global_uid_disabled
12
12
 
13
- global_uid = nil
14
- realtime = Benchmark::realtime do
15
- global_uid = self.class.generate_uid
13
+ global_uid = self.class.get_reserved_global_uid
14
+ if !global_uid
15
+ realtime = Benchmark::realtime do
16
+ global_uid = self.class.generate_uid
17
+ end
16
18
  end
19
+
17
20
  if GlobalUid::Base.global_uid_options[:dry_run]
18
21
  ActiveRecord::Base.logger.info("GlobalUid dry-run: #{self.class.name}\t#{global_uid}\t#{"%.4f" % realtime}")
19
22
  return
@@ -58,6 +61,28 @@ module GlobalUid
58
61
  end
59
62
  @global_uid_table_exists = true
60
63
  end
64
+
65
+ def with_reserved_global_uids(n_to_reserve)
66
+ old_should_reserve = @should_reserve_global_uids
67
+ @should_reserve_global_uids = n_to_reserve
68
+ yield
69
+ ensure
70
+ @should_reserve_global_uids = old_should_reserve
71
+ end
72
+
73
+ def get_reserved_global_uid
74
+ @reserved_global_uids ||= []
75
+ id = @reserved_global_uids.shift
76
+ return id if id
77
+
78
+ if @should_reserve_global_uids
79
+ @reserved_global_uids += GlobalUid::Base.get_many_uids_for_class(self, @should_reserve_global_uids)
80
+ @reserved_global_uids.shift
81
+ else
82
+ nil
83
+ end
84
+ end
85
+
61
86
  end
62
87
  end
63
88
  end
@@ -187,7 +187,6 @@ module GlobalUid
187
187
 
188
188
  def self.get_uid_for_class(klass, options = {})
189
189
  with_connections do |connection|
190
- timeout = self.global_uid_options[:query_timeout]
191
190
  GlobalUidTimer.timeout(self.global_uid_options[:query_timeout], TimeoutException) do
192
191
  id = connection.insert("REPLACE INTO #{klass.global_uid_table} (stub) VALUES ('a')")
193
192
  return id
@@ -196,6 +195,22 @@ module GlobalUid
196
195
  raise NoServersAvailableException, "All global UID servers are gone!"
197
196
  end
198
197
 
198
+ def self.get_many_uids_for_class(klass, n, options = {})
199
+ with_connections do |connection|
200
+ GlobalUidTimer.timeout(self.global_uid_options[:query_timeout], TimeoutException) do
201
+ connection.transaction do
202
+ start_id = connection.select_value("SELECT id from #{klass.global_uid_table} where stub='a' FOR UPDATE").to_i
203
+ connection.execute("update #{klass.global_uid_table} set id = id + #{n} * @@auto_increment_increment where stub='a'")
204
+ end_res = connection.select_one("SELECT id, @@auto_increment_increment as inc from #{klass.global_uid_table} where stub='a'")
205
+ increment_by = end_res['inc'].to_i
206
+ end_id = end_res['id'].to_i
207
+ return (start_id + increment_by).step(end_id, increment_by).to_a
208
+ end
209
+ end
210
+ end
211
+ raise NoServersAvailableException, "All global UID servers are gone!"
212
+ end
213
+
199
214
  def self.global_uid_options=(options)
200
215
  @global_uid_options = GLOBAL_UID_DEFAULTS.merge(options.symbolize_keys)
201
216
  end
@@ -242,6 +242,28 @@ class GlobalUIDTest < ActiveSupport::TestCase
242
242
  should "get a unique id" do
243
243
  test_unique_ids
244
244
  end
245
+
246
+ should "get bulk ids" do
247
+ res = GlobalUid::Base.get_many_uids_for_class(WithGlobalUID, 10)
248
+ assert res.size == 10
249
+ res += GlobalUid::Base.get_many_uids_for_class(WithGlobalUID, 10)
250
+ assert res.uniq.size == 20
251
+ end
252
+ end
253
+
254
+ context "reserving ids" do
255
+ should "get 10 in bulk" do
256
+ WithGlobalUID.with_reserved_global_uids(10) do
257
+ WithGlobalUID.create!
258
+ # now we should be able to run without ever touching the cx again
259
+ GlobalUid::Base.get_connections.each.expects(:insert).never
260
+ GlobalUid::Base.get_connections.each.expects(:select_value).never
261
+ 9.times { WithGlobalUID.create! }
262
+ end
263
+
264
+ GlobalUid::Base.get_connections.first.expects(:insert).once.returns(50)
265
+ WithGlobalUID.create!
266
+ end
245
267
  end
246
268
 
247
269
  context "With a timing out server" do
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: 1.2.6
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: