Class: Cassandra::Types::Duration

Inherits:
Cassandra::Type show all
Includes:
CustomData
Defined in:
lib/cassandra/duration.rb

Constant Summary collapse

@@four_byte_max =
2 ** 32
@@eight_byte_max =
2 ** 64

Instance Attribute Summary

Attributes inherited from Cassandra::Type

#kind

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cql_typeObject



77
78
79
# File 'lib/cassandra/duration.rb', line 77

def self.cql_type
  Type.new(@kind)
end

.deserialize(bytestr) ⇒ Object

Requirements for CustomData module



82
83
84
85
# File 'lib/cassandra/duration.rb', line 82

def self.deserialize(bytestr)
  buffer = Cassandra::Protocol::CqlByteBuffer.new.append(bytestr)
  Cassandra::Types::Duration.new(buffer.read_signed_vint,buffer.read_signed_vint,buffer.read_signed_vint)
end

.typeObject



87
88
89
# File 'lib/cassandra/duration.rb', line 87

def self.type
  Cassandra::Types::Custom.new('org.apache.cassandra.db.marshal.DurationType')
end

Instance Method Details

#assert(value, message = nil, &block) ⇒ Object



50
51
52
# File 'lib/cassandra/duration.rb', line 50

def assert(value, message = nil, &block)
  Util.assert_instance_of(Duration, value, message, &block)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:



68
69
70
71
72
73
# File 'lib/cassandra/duration.rb', line 68

def eql?(other)
  other.is_a?(Duration) &&
    @months == other.months &&
    @days == other.days &&
    @nanos == other.nanos
end

#hashObject



58
59
60
61
62
63
64
65
66
# File 'lib/cassandra/duration.rb', line 58

def hash
  @hash ||= begin
    h = 17
    h = 31 * h + @months.hash
    h = 31 * h + @days.hash
    h = 31 * h + @nanos.hash
    h
  end
end

#new(*values) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cassandra/duration.rb', line 38

def new(*values)
  Util.assert_size(3, values, "Duration type expects three values, #{values.size} were provided")
  values.each { |v| Util.assert_type(Int, v) }
  Util.assert (Util.encode_zigzag32(values[0]) < @@four_byte_max), "Months value must be a valid 32-bit integer"
  Util.assert (Util.encode_zigzag32(values[1]) < @@four_byte_max), "Days value must be a valid 32-bit integer"
  Util.assert (Util.encode_zigzag64(values[2]) < @@eight_byte_max), "Nanos value must be a valid 64-bit integer"
  all_positive = values.all? {|i| i >= 0 }
  all_negative = values.all? {|i| i <= 0 }
  Util.assert (all_positive or all_negative), "Values in a duration must be uniformly positive or negative"
  Duration.new *values
end

#serializeObject



91
92
93
94
95
96
97
# File 'lib/cassandra/duration.rb', line 91

def serialize
  rv = Cassandra::Protocol::CqlByteBuffer.new
  rv.append_signed_vint32(@months)
  rv.append_signed_vint32(@days)
  rv.append_signed_vint64(@nanos)
  rv
end

#to_sObject



54
55
56
# File 'lib/cassandra/duration.rb', line 54

def to_s
  "Duration: months => #{@months}, days => #{@days}, nanos => #{@nanos}"
end