Class: Tarantool::Response

Inherits:
Struct
  • Object
show all
Includes:
Serializers, UnpackTuples, Util::Packer, Util::TailGetter
Defined in:
lib/tarantool/response.rb

Constant Summary collapse

UTF8 =
'utf-8'.freeze
X02 =
'%02x'.freeze

Constants included from Serializers

Serializers::MAP

Constants included from Util::Packer

Util::Packer::INT16, Util::Packer::INT32, Util::Packer::INT64, Util::Packer::INT8, Util::Packer::MAX_INT16, Util::Packer::MAX_INT32, Util::Packer::MAX_INT64, Util::Packer::MAX_INT8, Util::Packer::MAX_SINT16, Util::Packer::MAX_SINT32, Util::Packer::MAX_SINT64, Util::Packer::MAX_SINT8, Util::Packer::MIN_INT, Util::Packer::MIN_SINT16, Util::Packer::MIN_SINT32, Util::Packer::MIN_SINT64, Util::Packer::MIN_SINT8, Util::Packer::SINT16, Util::Packer::SINT32, Util::Packer::SINT64, Util::Packer::SINT8

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Serializers

#check_type, #get_serializer

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



27
28
29
# File 'lib/tarantool/response.rb', line 27

def body
  @body
end

#cbObject

Returns the value of attribute cb

Returns:

  • (Object)

    the current value of cb



27
28
29
# File 'lib/tarantool/response.rb', line 27

def cb
  @cb
end

#fieldsObject

Returns the value of attribute fields

Returns:

  • (Object)

    the current value of fields



27
28
29
# File 'lib/tarantool/response.rb', line 27

def fields
  @fields
end

#get_tuplesObject

Returns the value of attribute get_tuples

Returns:

  • (Object)

    the current value of get_tuples



27
28
29
# File 'lib/tarantool/response.rb', line 27

def get_tuples
  @get_tuples
end

#request_typeObject

Returns the value of attribute request_type

Returns:

  • (Object)

    the current value of request_type



27
28
29
# File 'lib/tarantool/response.rb', line 27

def request_type
  @request_type
end

#translatorsObject

Returns the value of attribute translators

Returns:

  • (Object)

    the current value of translators



27
28
29
# File 'lib/tarantool/response.rb', line 27

def translators
  @translators
end

Instance Method Details

#_unpack_tuples(data, fields, tail, tuples_affected) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tarantool/response.rb', line 94

def _unpack_tuples(data, fields, tail, tuples_affected)
  tuples = []
  while tuples_affected > 0
    byte_size = ::BinUtils.slice_int32_le!(data)
    fields_num = ::BinUtils.slice_int32_le!(data)
    tuple_str = data.slice!(0, byte_size)
    i = 0
    tuple = []
    while i < fields_num
      field_size = ::BinUtils.slice_ber!(tuple_str)

      field = fields[i] || get_tail_item(fields, i, tail)

      tuple << (field_size == 0 ? nil :
        case field
        when :int, :integer
          if field_size != 4
            raise ValueError, "Bad field size #{field_size} for integer field ##{i}"
          end
          ::BinUtils.slice_int32_le!(tuple_str)
        when :string, :str
          str = tuple_str.slice!(0, field_size)
          str[0,1] = EMPTY  if str < ONE
          str.force_encoding(UTF8)
        when :int64
          if field_size != 8
            raise ValueError, "Bad field size #{field_size} for 64bit integer field ##{i}"
          end
          ::BinUtils.slice_int64_le!(tuple_str)
        when :bytes
          tuple_str.slice!(0, field_size)
        when :int16
          if field_size != 2
            raise ValueError, "Bad field size #{field_size} for 16bit integer field ##{i}"
          end
          ::BinUtils.slice_int16_le!(tuple_str)
        when :int8
          if field_size != 1
            raise ValueError, "Bad field size #{field_size} for 8bit integer field ##{i}"
          end
          ::BinUtils.slice_int8!(tuple_str)
        when :sint
          if field_size != 4
            raise ValueError, "Bad field size #{field_size} for integer field ##{i}"
          end
          ::BinUtils.slice_sint32_le!(tuple_str)
        when :sint64
          if field_size != 8
            raise ValueError, "Bad field size #{field_size} for 64bit integer field ##{i}"
          end
          ::BinUtils.slice_sint64_le!(tuple_str)
        when :sint16
          if field_size != 2
            raise ValueError, "Bad field size #{field_size} for 16bit integer field ##{i}"
          end
          ::BinUtils.slice_sint16_le!(tuple_str)
        when :sint8
          if field_size != 1
            raise ValueError, "Bad field size #{field_size} for 8bit integer field ##{i}"
          end
          ::BinUtils.slice_sint8!(tuple_str)
        when :varint
          case field_size
          when 8
            ::BinUtils.slice_int64_le!(tuple_str)
          when 4
            ::BinUtils.slice_int32_le!(tuple_str)
          when 2
            ::BinUtils.slice_int16_le!(tuple_str)
          else
            raise ValueError, "Bad field size #{field_size} for integer field ##{i}"
          end
        when :auto
          str = tuple_str.slice!(0, field_size).force_encoding('utf-8')
          case field_size
          when 8, 4, 2
            Util::AutoType.new(str)
          else
            str
          end
        else
          get_serializer(field).decode(tuple_str.slice!(0, field_size))
        end)
      i += 1
    end
    tuples << tuple
    tuples_affected -= 1
  end
  tuples
end

#call(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tarantool/response.rb', line 34

def call(data)
  if Exception === data
    cb.call(data)
  else
    if (ret = return_code(data)) == 0
      call_callback parse_response_for_cb(data)
    else
      data.gsub!("\x00", "")
      cb.call CODE_TO_EXCEPTION[ret].new(ret, data)
    end
  end
end

#call_callback(result) ⇒ Object



51
52
53
# File 'lib/tarantool/response.rb', line 51

def call_callback(result)
  cb.call(Exception === result || get_tuples != :first ? result : result.first)
end

#parse_response(data) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tarantool/response.rb', line 61

def parse_response(data)
  return data  if Exception === data
  unless get_tuples
    ::BinUtils.get_int32_le(data)
  else
    tuples = unpack_tuples(data)
    if translators
      translators.each{|trans|
        tuples.map!{|tuple| trans.call(tuple)}
      }
    end
    tuples
  end
end

#parse_response_for_cb(data) ⇒ Object



55
56
57
58
59
# File 'lib/tarantool/response.rb', line 55

def parse_response_for_cb(data)
  parse_response data
rescue StandardError => e
  e
end

#return_code(data) ⇒ Object



185
186
187
# File 'lib/tarantool/response.rb', line 185

def return_code(data)
  ::BinUtils.slice_int32_le!(data)
end

#unpack_tuples(data) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tarantool/response.rb', line 77

def unpack_tuples(data)
  tuples_affected = ::BinUtils.slice_int32_le!(data)
  fields = fields()
  if Integer === fields.last
    *fields, tail = fields
  else
    tail = 1
  end
  orig_data = data.dup
  begin
    _unpack_tuples(data, fields, tail, tuples_affected)
  rescue ValueError => e
    $stderr.puts "Value Error: tuples=#{tuples_affected}, data='#{orig_data.each_byte.map{|b| format(X02, b)}.join(' ')}'"
    raise e
  end
end