Class: Mongo::ClusterTime Private

Inherits:
BSON::Document
  • Object
show all
Includes:
Comparable
Defined in:
lib/mongo/cluster_time.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.

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.

Defined Under Namespace

Modules: Consumer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements = nil) ⇒ ClusterTime

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.

Returns a new instance of ClusterTime.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
# File 'lib/mongo/cluster_time.rb', line 31

def initialize(elements = nil)
  super

  return unless Lint.enabled? && !self['clusterTime']

  raise ArgumentError, 'Creating a cluster time without clusterTime field'
end

Class Method Details

.[](doc) ⇒ 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.

Converts a BSON::Document to a ClusterTime.

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



81
82
83
84
85
86
87
# File 'lib/mongo/cluster_time.rb', line 81

def [](doc)
  if doc.nil? || doc.is_a?(ClusterTime)
    doc
  else
    ClusterTime.new(doc)
  end
end

Instance Method Details

#<=>(other) ⇒ 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.

Compares two ClusterTime instances by comparing their timestamps.



56
57
58
59
60
61
62
63
64
65
# File 'lib/mongo/cluster_time.rb', line 56

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) ⇒ 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.

Compares two ClusterTime instances by comparing their timestamps.



68
69
70
71
72
73
74
75
# File 'lib/mongo/cluster_time.rb', line 68

def ==(other)
  if self['clusterTime'] && other['clusterTime'] &&
     self['clusterTime'] == other['clusterTime']
    true
  else
    false
  end
end

#advance(other) ⇒ 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.

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.



46
47
48
49
50
51
52
53
# File 'lib/mongo/cluster_time.rb', line 46

def advance(other)
  if self['clusterTime'] && other['clusterTime'] &&
     other['clusterTime'] > self['clusterTime']
    ClusterTime[other]
  else
    self
  end
end