Class: BSON::ObjectId::Generator Private
- Inherits:
-
Object
- Object
- BSON::ObjectId::Generator
- Defined in:
- lib/bson/object_id.rb
Overview
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.
Inner class that encapsulates the behaviour of actually generating each part of the ObjectId.
Instance Attribute Summary collapse
- #machine_id ⇒ Object private
Instance Method Summary collapse
-
#generate(time, counter = 0) ⇒ String
private
Generate object id data for a given time using the provided counter.
-
#initialize ⇒ Generator
constructor
private
Instantiate the new object id generator.
-
#next_object_id(time = nil) ⇒ String
private
Return object id data based on the current time, incrementing the object id counter.
Constructor Details
#initialize ⇒ Generator
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.
Instantiate the new object id generator. Will set the machine id once on the initial instantiation.
333 334 335 336 337 |
# File 'lib/bson/object_id.rb', line 333 def initialize @counter = rand(0x1000000) @machine_id = Digest::MD5.digest(Socket.gethostname).unpack("N")[0] @mutex = Mutex.new end |
Instance Attribute Details
#machine_id ⇒ 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.
324 325 326 |
# File 'lib/bson/object_id.rb', line 324 def machine_id @machine_id end |
Instance Method Details
#generate(time, counter = 0) ⇒ String
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.
371 372 373 |
# File 'lib/bson/object_id.rb', line 371 def generate(time, counter = 0) [ time, machine_id, process_id, counter << 8 ].pack("N NX lXX NX") end |
#next_object_id(time = nil) ⇒ String
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. Will use the provided time if not nil.
350 351 352 353 354 355 356 357 358 |
# File 'lib/bson/object_id.rb', line 350 def next_object_id(time = nil) @mutex.lock begin count = @counter = (@counter + 1) % 0xFFFFFF ensure @mutex.unlock rescue nil end generate(time || ::Time.new.to_i, count) end |