class Moped::BSON::ObjectId::Generator
@api private
Public Class Methods
new()
click to toggle source
# File lib/moped/bson/object_id.rb, line 269 def initialize # Generate and cache 3 bytes of identifying information from the current # machine. @machine_id = Digest::MD5.digest(Socket.gethostname).unpack("N")[0] @mutex = Mutex.new @counter = 0 end
Public Instance Methods
generate(time, counter = 0)
click to toggle source
Generate object id data for a given time using the provided
counter
.
# File lib/moped/bson/object_id.rb, line 292 def generate(time, counter = 0) process_thread_id = "#{Process.pid}#{Thread.current.object_id}".hash % 0xFFFF [time, @machine_id, process_thread_id, counter << 8].pack("N NX lXX NX") end
next(time = nil)
click to toggle source
Return object id data based on the current time, incrementing the object id counter.
# File lib/moped/bson/object_id.rb, line 280 def next(time = nil) @mutex.lock begin counter = @counter = (@counter + 1) % 0xFFFFFF ensure @mutex.unlock rescue nil end generate(time || Time.new.to_i, counter) end