Parent

Class/Module Index [+]

Quicksearch

Moped::BSON::ObjectId::Generator

@api private

Public Class Methods

new() click to toggle source
# 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

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 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
next() 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 101
def next
  @mutex.lock
  begin
    counter = @counter = (@counter + 1) % 0xFFFFFF
  ensure
    @mutex.unlock rescue nil
  end

  generate(Time.new.to_i, counter)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.