global_uid 1.2.6 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/gemfiles/rails2.gemfile.lock +1 -1
- data/gemfiles/rails3.gemfile.lock +1 -1
- data/global_uid.gemspec +1 -1
- data/lib/global_uid/active_record_extension.rb +28 -3
- data/lib/global_uid/base.rb +16 -1
- data/test/global_uid_test.rb +22 -0
- metadata +1 -1
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.
|
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 =
|
14
|
-
|
15
|
-
|
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
|
data/lib/global_uid/base.rb
CHANGED
@@ -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
|
data/test/global_uid_test.rb
CHANGED
@@ -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
|