class Mongo::ClusterTime

ClusterTime encapsulates cluster time storage and operations.

The primary operation performed on the cluster time is advancing it: given another cluster time, pick the newer of the two.

This class provides comparison methods that are used to figure out which cluster time is newer, and provides diagnostics in lint mode when the actual time is missing from a cluster time document.

@api private

Public Class Methods

[](doc) click to toggle source

Converts a BSON::Document to a ClusterTime.

doc can be nil, in which case nil is returned.

# File lib/mongo/cluster_time.rb, line 96
def [](doc)
  if doc.nil? || doc.is_a?(ClusterTime)
    doc
  else
    ClusterTime.new(doc)
  end
end
new(elements = nil) click to toggle source
Calls superclass method
# File lib/mongo/cluster_time.rb, line 29
def initialize(elements = nil)
  super

  if Lint.enabled? && !self['clusterTime']
    raise ArgumentError, 'Creating a cluster time without clusterTime field'
  end
end

Public Instance Methods

<(other) click to toggle source
# File lib/mongo/cluster_time.rb, line 77
def <(other)
  (self <=> other) == -1
end
<=(other) click to toggle source
# File lib/mongo/cluster_time.rb, line 74
def <=(other)
  (self <=> other) != 1
end
<=>(other) click to toggle source

Compares two ClusterTime instances by comparing their timestamps.

# File lib/mongo/cluster_time.rb, line 55
def <=>(other)
  if self['clusterTime'] && other['clusterTime']
    self['clusterTime'] <=> other['clusterTime']
  elsif !self['clusterTime']
    raise ArgumentError, "Cannot compare cluster times when receiver is missing clusterTime key: #{inspect}"
  else other['clusterTime']
    raise ArgumentError, "Cannot compare cluster times when other is missing clusterTime key: #{other.inspect}"
  end
end
==(other) click to toggle source

Compares two ClusterTime instances by comparing their timestamps.

# File lib/mongo/cluster_time.rb, line 82
def ==(other)
  if self['clusterTime'] && other['clusterTime'] &&
    self['clusterTime'] == other['clusterTime']
  then
    true
  else
    false
  end
end
>(other) click to toggle source
# File lib/mongo/cluster_time.rb, line 71
def >(other)
  (self <=> other) == 1
end
>=(other) click to toggle source

Older Rubies do not implement other logical operators through <=>. TODO revise whether these methods are needed when jira.mongodb.org/browse/RUBY-1622 is implemented.

# File lib/mongo/cluster_time.rb, line 68
def >=(other)
  (self <=> other) != -1
end
advance(other) click to toggle source

Advances the cluster time in the receiver to the cluster time in other.

other can be nil or be behind the cluster time in the receiver; in these cases the receiver is returned unmodified. If receiver is advanced, a new ClusterTime object is returned.

Return value is nil or a ClusterTime instance.

# File lib/mongo/cluster_time.rb, line 44
def advance(other)
  if self['clusterTime'] && other['clusterTime'] &&
    other['clusterTime'] > self['clusterTime']
  then
    ClusterTime[other]
  else
    self
  end
end