Class: GorgonAMQ::Protocol::TableValueEncoder

Inherits:
Object
  • Object
show all
Includes:
TypeConstants
Defined in:
lib/gorgon_amq-protocol/lib/gorgon_amq/protocol/table_value_encoder.rb

Constant Summary

Constants included from TypeConstants

GorgonAMQ::Protocol::TypeConstants::BOOLEAN_FALSE, GorgonAMQ::Protocol::TypeConstants::BOOLEAN_TRUE, GorgonAMQ::Protocol::TypeConstants::TEN, GorgonAMQ::Protocol::TypeConstants::TYPE_32BIT_FLOAT, GorgonAMQ::Protocol::TypeConstants::TYPE_64BIT_FLOAT, GorgonAMQ::Protocol::TypeConstants::TYPE_ARRAY, GorgonAMQ::Protocol::TypeConstants::TYPE_BOOLEAN, GorgonAMQ::Protocol::TypeConstants::TYPE_BYTE_ARRAY, GorgonAMQ::Protocol::TypeConstants::TYPE_DECIMAL, GorgonAMQ::Protocol::TypeConstants::TYPE_HASH, GorgonAMQ::Protocol::TypeConstants::TYPE_INTEGER, GorgonAMQ::Protocol::TypeConstants::TYPE_SIGNED_16BIT, GorgonAMQ::Protocol::TypeConstants::TYPE_SIGNED_64BIT, GorgonAMQ::Protocol::TypeConstants::TYPE_SIGNED_8BIT, GorgonAMQ::Protocol::TypeConstants::TYPE_STRING, GorgonAMQ::Protocol::TypeConstants::TYPE_TIME, GorgonAMQ::Protocol::TypeConstants::TYPE_VOID

Class Method Summary collapse

Class Method Details

.array_size(value) ⇒ Object



113
114
115
116
117
118
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/protocol/table_value_encoder.rb', line 113

def self.array_size(value)
  acc = 0
  value.each { |v| acc += self.field_value_size(v) }

  acc
end

.encode(value) ⇒ Object

API



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/protocol/table_value_encoder.rb', line 25

def self.encode(value)
  accumulator = String.new

  case value
  when String then
    accumulator << TYPE_STRING
    accumulator << [value.bytesize].pack(PACK_UINT32)
    accumulator << value
  when Symbol then
    v = value.to_s
    accumulator << TYPE_STRING
    accumulator << [v.bytesize].pack(PACK_UINT32)
    accumulator << v
  when Integer then
    accumulator << TYPE_INTEGER
    accumulator << [value].pack(PACK_UINT32)
  when GorgonAMQ::Protocol::Float32Bit then
    accumulator << TYPE_32BIT_FLOAT
    accumulator << [value.value].pack(PACK_32BIT_FLOAT)
  when Float then
    accumulator << TYPE_64BIT_FLOAT
    accumulator << [value].pack(PACK_64BIT_FLOAT)
  when true, false then
    accumulator << TYPE_BOOLEAN
    accumulator << (value ? BOOLEAN_TRUE : BOOLEAN_FALSE)
  when Time then
    accumulator << TYPE_TIME
    accumulator << [value.to_i].pack(PACK_INT64).reverse # FIXME: there has to be a more efficient way
  when nil then
    accumulator << TYPE_VOID
  when Array then
    accumulator << TYPE_ARRAY
    accumulator << [self.array_size(value)].pack(PACK_UINT32)

    value.each { |v| accumulator << self.encode(v) }
  when Hash then
    accumulator << TYPE_HASH
    accumulator << GorgonAMQ::Protocol::Table.encode(value)
  else
    # We don't want to require these libraries.
    if defined?(BigDecimal) && value.is_a?(BigDecimal)
      accumulator << TYPE_DECIMAL
      if value.exponent < 0
        decimals = -value.exponent
        raw = (value * (decimals ** 10)).to_i
        accumulator << [decimals + 1, raw].pack(PACK_UCHAR_UINT32) # somewhat like floating point
      else
        # per spec, the "decimals" octet is unsigned (!)
        accumulator << [0, value.to_i].pack(PACK_UCHAR_UINT32)
      end
    else
      raise ArgumentError.new("Unsupported value #{value.inspect} of type #{value.class.name}")
    end # if
  end # case

  accumulator
end

.field_value_size(value) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/protocol/table_value_encoder.rb', line 86

def self.field_value_size(value)
  # the type tag takes 1 byte
  acc = 1

  case value
  when String then
    acc += (value.bytesize + 4)
  when Integer then
    acc += 4
  when Float then
    acc += 8
  when Time, DateTime then
    acc += 8
  when true, false then
    acc += 1
  when nil then
    # nothing, type tag alone is enough
  when Hash then
    acc += (4 + Table.hash_size(value))
  when Array then
    acc += (4 + self.array_size(value))
  end

  acc
end