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.



269
270
271
272
273
274
275
276
# 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

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.



292
293
294
295
# 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) ⇒ 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.

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



280
281
282
283
284
285
286
287
288
289
# 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