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.
330 331 332 333 334 |
# File 'lib/bson/object_id.rb', line 330 def initialize @counter = 0 @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.
321 322 323 |
# File 'lib/bson/object_id.rb', line 321 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.
368 369 370 |
# File 'lib/bson/object_id.rb', line 368 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.
347 348 349 350 351 352 353 354 355 |
# File 'lib/bson/object_id.rb', line 347 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 |