Class: GorgonAMQ::Protocol::Table

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

Defined Under Namespace

Classes: InvalidTableError

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

.decode(data) ⇒ Object



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
82
83
84
85
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.rb', line 54

def self.decode(data)
  table        = Hash.new
  table_length = data.unpack(PACK_UINT32).first

  return table if table_length.zero?

  offset       = 4
  while offset <= table_length
    key, offset  = decode_table_key(data, offset)
    type, offset = TableValueDecoder.decode_value_type(data, offset)

    table[key] = case type
                 when TYPE_STRING
                   v, offset = TableValueDecoder.decode_string(data, offset)
                   v
                 when TYPE_INTEGER
                   v, offset = TableValueDecoder.decode_integer(data, offset)
                   v
                 when TYPE_DECIMAL
                   v, offset = TableValueDecoder.decode_big_decimal(data, offset)
                   v
                 when TYPE_TIME
                   v, offset = TableValueDecoder.decode_time(data, offset)
                   v
                 when TYPE_HASH
                   v, offset = TableValueDecoder.decode_hash(data, offset)
                   v
                 when TYPE_BOOLEAN
                   v, offset = TableValueDecoder.decode_boolean(data, offset)
                   v
                 when TYPE_SIGNED_8BIT  then
                   v, offset = TableValueDecoder.decode_short_short(data, offset)
                   v
                 when TYPE_SIGNED_16BIT then
                   v, offset = TableValueDecoder.decode_short(data, offset)
                   v
                 when TYPE_SIGNED_64BIT then
                   v, offset = TableValueDecoder.decode_long(data, offset)
                   v
                 when TYPE_32BIT_FLOAT then
                   v, offset = TableValueDecoder.decode_32bit_float(data, offset)
                   v
                 when TYPE_64BIT_FLOAT then
                   v, offset = TableValueDecoder.decode_64bit_float(data, offset)
                   v
                 when TYPE_VOID
                   nil
                 when TYPE_ARRAY
                   v, offset = TableValueDecoder.decode_array(data, offset)
                   v
                 else
                   raise ArgumentError, "Not a valid type: #{type.inspect}\nData: #{data.inspect}\nUnprocessed data: #{data[offset..-1].inspect}\nOffset: #{offset}\nTotal size: #{table_length}\nProcessed data: #{table.inspect}"
                 end
  end

  table
end

.decode_table_key(data, offset) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/protocol/table.rb', line 129

def self.decode_table_key(data, offset)
  key_length = data.slice(offset, 1).unpack(PACK_CHAR).first
  offset += 1
  key = data.slice(offset, key_length)
  offset += key_length

  [key, offset]
end

.encode(table) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/protocol/table.rb', line 30

def self.encode(table)
  buffer = String.new

  table ||= {}

  table.each do |key, value|
    key = key.to_s # it can be a symbol as well
    buffer << key.bytesize.chr + key

    case value
    when Hash then
      buffer << TYPE_HASH
      buffer << self.encode(value)
    else
      buffer << TableValueEncoder.encode(value)
    end
  end

  [buffer.bytesize].pack(PACK_UINT32) + buffer
end

.hash_size(value) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/protocol/table.rb', line 118

def self.hash_size(value)
  acc = 0
  value.each do |k, v|
    acc += (1 + k.to_s.bytesize)
    acc += TableValueEncoder.field_value_size(v)
  end

  acc
end

.length(data) ⇒ Object



113
114
115
# File 'lib/gorgon_amq-protocol/lib/gorgon_amq/protocol/table.rb', line 113

def self.length(data)
  data.unpack(PACK_UINT32).first
end