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
Converts a BSON::Document to a ClusterTime
.
doc
can be nil, in which case nil is returned.
# File lib/mongo/cluster_time.rb, line 94 def [](doc) if doc.nil? || doc.is_a?(ClusterTime) doc else ClusterTime.new(doc) end end
# File lib/mongo/cluster_time.rb, line 27 def initialize(elements = nil) super if Lint.enabled? && !self['clusterTime'] raise ArgumentError, 'Creating a cluster time without clusterTime field' end end
Public Instance Methods
# File lib/mongo/cluster_time.rb, line 75 def <(other) (self <=> other) == -1 end
# File lib/mongo/cluster_time.rb, line 72 def <=(other) (self <=> other) != 1 end
Compares two ClusterTime
instances by comparing their timestamps.
# File lib/mongo/cluster_time.rb, line 53 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
Compares two ClusterTime
instances by comparing their timestamps.
# File lib/mongo/cluster_time.rb, line 80 def ==(other) if self['clusterTime'] && other['clusterTime'] && self['clusterTime'] == other['clusterTime'] then true else false end end
# File lib/mongo/cluster_time.rb, line 69 def >(other) (self <=> other) == 1 end
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 66 def >=(other) (self <=> other) != -1 end
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 42 def advance(other) if self['clusterTime'] && other['clusterTime'] && other['clusterTime'] > self['clusterTime'] then ClusterTime[other] else self end end