Merge pull request #49 from bernerdschaefer/master
Use pure Ruby hostname digest optimization for ObjectId and use java.bson.types.ObjectId for JRuby ObjectId generation.
This commit is contained in:
commit
6b8d19b3fb
@ -30,9 +30,6 @@ module BSON
|
|||||||
#
|
#
|
||||||
# @core objectids
|
# @core objectids
|
||||||
class ObjectId
|
class ObjectId
|
||||||
@@lock = Mutex.new
|
|
||||||
@@index = 0
|
|
||||||
|
|
||||||
attr_accessor :data
|
attr_accessor :data
|
||||||
|
|
||||||
# Create a new object id. If no parameter is given, an id corresponding
|
# Create a new object id. If no parameter is given, an id corresponding
|
||||||
@ -126,7 +123,7 @@ module BSON
|
|||||||
#
|
#
|
||||||
# @return [Mongo::ObjectId]
|
# @return [Mongo::ObjectId]
|
||||||
def self.from_string(str)
|
def self.from_string(str)
|
||||||
raise InvalidObjectId, "illegal ObjectId format" unless legal?(str)
|
raise InvalidObjectId, "illegal ObjectId format: #{str}" unless legal?(str)
|
||||||
data = []
|
data = []
|
||||||
12.times do |i|
|
12.times do |i|
|
||||||
data[i] = str[i * 2, 2].to_i(16)
|
data[i] = str[i * 2, 2].to_i(16)
|
||||||
@ -170,8 +167,31 @@ module BSON
|
|||||||
Time.at(@data.pack("C4").unpack("N")[0]).utc
|
Time.at(@data.pack("C4").unpack("N")[0]).utc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.machine_id
|
||||||
|
@@machine_id
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
if RUBY_PLATFORM =~ /java/
|
||||||
|
@@generator = Java::OrgBsonTypes::ObjectId
|
||||||
|
@@machine_id = [@@generator.genMachineId].pack("N")[0,3]
|
||||||
|
|
||||||
|
def generate(oid_time=nil)
|
||||||
|
data = (oid_time ? @@generator.new(oid_time) : @@generator.new)
|
||||||
|
|
||||||
|
oid = ''
|
||||||
|
oid += [data.timeSecond].pack("N")
|
||||||
|
oid += [data._machine].pack("N")
|
||||||
|
oid += [data._inc].pack("N")
|
||||||
|
oid.unpack("C12")
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
@@lock = Mutex.new
|
||||||
|
@@index = 0
|
||||||
|
@@machine_id = Digest::MD5.digest(Socket.gethostname)[0, 3]
|
||||||
|
|
||||||
# This gets overwritten by the C extension if it loads.
|
# This gets overwritten by the C extension if it loads.
|
||||||
def generate(oid_time=nil)
|
def generate(oid_time=nil)
|
||||||
oid = ''
|
oid = ''
|
||||||
@ -185,7 +205,7 @@ module BSON
|
|||||||
oid += [t].pack("N")
|
oid += [t].pack("N")
|
||||||
|
|
||||||
# 3 bytes machine
|
# 3 bytes machine
|
||||||
oid += Digest::MD5.digest(Socket.gethostname)[0, 3]
|
oid += @@machine_id
|
||||||
|
|
||||||
# 2 bytes pid
|
# 2 bytes pid
|
||||||
oid += [Process.pid % 0xFFFF].pack("n")
|
oid += [Process.pid % 0xFFFF].pack("n")
|
||||||
@ -203,3 +223,4 @@ module BSON
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
@ -134,8 +134,7 @@ class ObjectIdTest < Test::Unit::TestCase
|
|||||||
time = Time.now.utc
|
time = Time.now.utc
|
||||||
id = ObjectId.from_time(time, :unique => true)
|
id = ObjectId.from_time(time, :unique => true)
|
||||||
|
|
||||||
mac_id = Digest::MD5.digest(Socket.gethostname)[0, 3].unpack("C3")
|
assert_equal id.to_a[4, 3], ObjectId.machine_id.unpack("C3")
|
||||||
assert_equal id.to_a[4, 3], mac_id
|
|
||||||
assert_equal time.to_i, id.generation_time.to_i
|
assert_equal time.to_i, id.generation_time.to_i
|
||||||
|
|
||||||
id2 = ObjectId.new(nil, time)
|
id2 = ObjectId.new(nil, time)
|
||||||
|
Loading…
Reference in New Issue
Block a user