Class: Moped::BSON::ObjectId::Generator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/moped/bson/object_id.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initializeGenerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Generator.



90
91
92
93
94
95
96
97
# File 'lib/moped/bson/object_id.rb', line 90

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

Instance Method Details

#generate(time, counter = 0) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate object id data for a given time using the provided counter.



113
114
115
116
# File 'lib/moped/bson/object_id.rb', line 113

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

#nextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return object id data based on the current time, incrementing the object id counter.



101
102
103
104
105
106
107
108
109
110
# File 'lib/moped/bson/object_id.rb', line 101

def next
  @mutex.lock
  begin
    counter = @counter = (@counter + 1) % 0xFFFFFF
  ensure
    @mutex.unlock rescue nil
  end

  generate(Time.new.to_i, counter)
end