|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +#-- |
| 4 | +# Copyright DataStax, Inc. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +#++ |
| 18 | +module Cassandra |
| 19 | + module Types |
| 20 | + |
| 21 | + class Duration < Type |
| 22 | + include CustomData |
| 23 | + |
| 24 | + @@four_byte_max = 2 ** 32 |
| 25 | + @@eight_byte_max = 2 ** 64 |
| 26 | + |
| 27 | + # @private |
| 28 | + attr_reader :months, :days, :nanos |
| 29 | + |
| 30 | + # @private |
| 31 | + def initialize(months, days, nanos) |
| 32 | + super(:duration) |
| 33 | + @months = months |
| 34 | + @days = days |
| 35 | + @nanos = nanos |
| 36 | + end |
| 37 | + |
| 38 | + def new(*values) |
| 39 | + Util.assert_size(3, values, "Duration type expects three values, #{values.size} were provided") |
| 40 | + values.each { |v| Util.assert_type(Int, v) } |
| 41 | + Util.assert (Util.encode_zigzag32(values[0]) < @@four_byte_max), "Months value must be a valid 32-bit integer" |
| 42 | + Util.assert (Util.encode_zigzag32(values[1]) < @@four_byte_max), "Days value must be a valid 32-bit integer" |
| 43 | + Util.assert (Util.encode_zigzag64(values[2]) < @@eight_byte_max), "Nanos value must be a valid 64-bit integer" |
| 44 | + all_positive = values.all? {|i| i >= 0 } |
| 45 | + all_negative = values.all? {|i| i <= 0 } |
| 46 | + Util.assert (all_positive or all_negative), "Values in a duration must be uniformly positive or negative" |
| 47 | + Duration.new *values |
| 48 | + end |
| 49 | + |
| 50 | + def assert(value, message = nil, &block) |
| 51 | + Util.assert_instance_of(Duration, value, message, &block) |
| 52 | + end |
| 53 | + |
| 54 | + def to_s |
| 55 | + "Duration: months => #{@months}, days => #{@days}, nanos => #{@nanos}" |
| 56 | + end |
| 57 | + |
| 58 | + def hash |
| 59 | + @hash ||= begin |
| 60 | + h = 17 |
| 61 | + h = 31 * h + @months.hash |
| 62 | + h = 31 * h + @days.hash |
| 63 | + h = 31 * h + @nanos.hash |
| 64 | + h |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def eql?(other) |
| 69 | + other.is_a?(Duration) && |
| 70 | + @months == other.months && |
| 71 | + @days == other.days && |
| 72 | + @nanos == other.nanos |
| 73 | + end |
| 74 | + |
| 75 | + alias == eql? |
| 76 | + |
| 77 | + def self.cql_type |
| 78 | + Type.new(@kind) |
| 79 | + end |
| 80 | + |
| 81 | + # Requirements for CustomData module |
| 82 | + def self.deserialize(bytestr) |
| 83 | + buffer = Cassandra::Protocol::CqlByteBuffer.new.append(bytestr) |
| 84 | + Cassandra::Types::Duration.new(buffer.read_signed_vint,buffer.read_signed_vint,buffer.read_signed_vint) |
| 85 | + end |
| 86 | + |
| 87 | + def self.type |
| 88 | + Cassandra::Types::Custom.new('org.apache.cassandra.db.marshal.DurationType') |
| 89 | + end |
| 90 | + |
| 91 | + def serialize |
| 92 | + rv = Cassandra::Protocol::CqlByteBuffer.new |
| 93 | + rv.append_signed_vint32(@months) |
| 94 | + rv.append_signed_vint32(@days) |
| 95 | + rv.append_signed_vint64(@nanos) |
| 96 | + rv |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments