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

17.chr.force_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'.freeze

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


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

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

Instance Attribute Details

#incrementObject

Since:

  • 2.0.0


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

attr_reader :seconds, :increment

#secondsInteger

Returns The number of seconds.

Returns:

  • (Integer)

    The number of seconds.

Since:

  • 2.0.0


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

def seconds
  @seconds
end

Class Method Details

.from_bson(buffer) ⇒ Timestamp

Deserialize timestamp from BSON.

Parameters:

  • buffer (ByteBuffer)

    The byte buffer.

Returns:

See Also:

Since:

  • 2.0.0


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

def self.from_bson(buffer)
  increment = buffer.get_int32
  seconds = buffer.get_int32
  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


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

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


56
57
58
59
# File 'lib/bson/timestamp.rb', line 56

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

#as_json(*args) ⇒ Hash

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


87
88
89
# File 'lib/bson/timestamp.rb', line 87

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

#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer

Get the timestamp as its encoded raw BSON bytes.

Examples:

Get the timestamp as BSON.

timestamp.to_bson

Returns:

  • (BSON::ByteBuffer)

    The buffer with the encoded object.

See Also:

Since:

  • 2.0.0


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

def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  buffer.put_int32(increment)
  buffer.put_int32(seconds)
end