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.



83
84
85
86
87
88
89
90
# File 'lib/moped/bson/object_id.rb', line 83

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.



106
107
108
# File 'lib/moped/bson/object_id.rb', line 106

def generate(time, counter = 0)
  [time, @machine_id, Process.pid, 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.



94
95
96
97
98
99
100
101
102
103
# File 'lib/moped/bson/object_id.rb', line 94

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

  generate(Time.new.to_i, counter)
end