Class: BSON::Timestamp

Inherits:
Object
  • Object
show all
Includes:
JSON, Comparable
Defined in:
lib/bson/timestamp.rb

Overview

Represents a timestamp type, which is predominately used for sharding.

See Also:

Since:

  • 2.0.0

Constant Summary collapse

BSON_TYPE =

A timestamp is type 0x11 in the BSON spec.

Since:

  • 2.0.0

::String.new(17.chr, encoding: BINARY).freeze
COMPARISON_ERROR_MESSAGE =

Error message if an object other than a Timestamp is compared with this object.

Since:

  • 4.3.0

'comparison of %s with Timestamp failed'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSON

#to_json

Constructor Details

#initialize(seconds, increment) ⇒ Timestamp

Instantiate the new timestamp.

Examples:

Instantiate the timestamp.

BSON::Timestamp.new(5, 30)

Parameters:

  • seconds (Integer)

    The number of seconds.

  • increment (Integer)

    The increment value.

Since:

  • 2.0.0



114
115
116
# File 'lib/bson/timestamp.rb', line 114

def initialize(seconds, increment)
  @seconds, @increment = seconds, increment
end

Instance Attribute Details

#incrementObject

Since:

  • 2.0.0



46
# File 'lib/bson/timestamp.rb', line 46

attr_reader :seconds, :increment

#secondsInteger

Returns The number of seconds.

Returns:

  • (Integer)

    The number of seconds.

Since:

  • 2.0.0



46
47
48
# File 'lib/bson/timestamp.rb', line 46

def seconds
  @seconds
end

Class Method Details

.from_bson(buffer, **options) ⇒ Timestamp

Deserialize timestamp from BSON.

Parameters:

  • buffer (ByteBuffer)

    The byte buffer.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :mode (nil | :bson)

    Decoding mode to use.

Returns:

See Also:

Since:

  • 2.0.0



144
145
146
147
148
# File 'lib/bson/timestamp.rb', line 144

def self.from_bson(buffer, **options)
  increment = buffer.get_uint32
  seconds = buffer.get_uint32
  new(seconds, increment)
end

Instance Method Details

#<=>(other) ⇒ true, false

Determine if this timestamp is greater or less than another object.

Examples:

Compare the timestamp.

timestamp < other

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (true, false)

    The result of the comparison.

Raises:

  • (ArgumentError)

Since:

  • 4.3.0



73
74
75
76
77
78
79
# File 'lib/bson/timestamp.rb', line 73

def <=>(other)
  raise ArgumentError.new(COMPARISON_ERROR_MESSAGE % other.class) unless other.is_a?(Timestamp)
  return 0 if self == other
  a = [ seconds, increment ]
  b = [ other.seconds, other.increment ]
  [ a, b ].sort[0] == a ? -1 : 1
end

#==(other) ⇒ true, false

Determine if this timestamp is equal to another object.

Examples:

Check the timestamp equality.

timestamp == other

Parameters:

  • other (Object)

    The object to compare against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



58
59
60
61
# File 'lib/bson/timestamp.rb', line 58

def ==(other)
  return false unless other.is_a?(Timestamp)
  seconds == other.seconds && increment == other.increment
end

#as_extended_json(**options) ⇒ Hash

Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md).

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0



101
102
103
# File 'lib/bson/timestamp.rb', line 101

def as_extended_json(**options)
  { "$timestamp" => { "t" => seconds, "i" => increment } }
end

#as_json(*args) ⇒ Hash

Deprecated.

Use as_extended_json instead.

Get the timestamp as JSON hash data.

Examples:

Get the timestamp as a JSON hash.

timestamp.as_json

Returns:

  • (Hash)

    The timestamp as a JSON hash.

Since:

  • 2.0.0



90
91
92
# File 'lib/bson/timestamp.rb', line 90

def as_json(*args)
  as_extended_json
end

#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer

Get the timestamp as its encoded raw BSON bytes.

Examples:

Get the timestamp as BSON.

timestamp.to_bson

Returns:

See Also:

Since:

  • 2.0.0



128
129
130
131
# File 'lib/bson/timestamp.rb', line 128

def to_bson(buffer = ByteBuffer.new)
  buffer.put_uint32(increment)
  buffer.put_uint32(seconds)
end