Class: Tarantool::Response

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

Constant Summary

Constants included from UnpackTuples

UnpackTuples::UTF8

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

Constants included from Serializers

Serializers::MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UnpackTuples

#_unpack_field

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



99
100
101
# File 'lib/tarantool/response.rb', line 99

def body
  @body
end

#cbObject

Returns the value of attribute cb

Returns:

  • (Object)

    the current value of cb



99
100
101
# File 'lib/tarantool/response.rb', line 99

def cb
  @cb
end

#fieldsObject

Returns the value of attribute fields

Returns:

  • (Object)

    the current value of fields



99
100
101
# File 'lib/tarantool/response.rb', line 99

def fields
  @fields
end

#get_tuplesObject

Returns the value of attribute get_tuples

Returns:

  • (Object)

    the current value of get_tuples



99
100
101
# File 'lib/tarantool/response.rb', line 99

def get_tuples
  @get_tuples
end

#request_typeObject

Returns the value of attribute request_type

Returns:

  • (Object)

    the current value of request_type



99
100
101
# File 'lib/tarantool/response.rb', line 99

def request_type
  @request_type
end

#translatorsObject

Returns the value of attribute translators

Returns:

  • (Object)

    the current value of translators



99
100
101
# File 'lib/tarantool/response.rb', line 99

def translators
  @translators
end

Instance Method Details

#call(data) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tarantool/response.rb', line 103

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



120
121
122
# File 'lib/tarantool/response.rb', line 120

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

#parse_response(data) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/tarantool/response.rb', line 130

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



124
125
126
127
128
# File 'lib/tarantool/response.rb', line 124

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

#return_code(data) ⇒ Object



179
180
181
# File 'lib/tarantool/response.rb', line 179

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

#unpack_tuples(data) ⇒ Object



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
# File 'lib/tarantool/response.rb', line 145

def unpack_tuples(data)
  tuples_affected = ::BinUtils.slice_int32_le!(data)
  ta = tuples_affected
  fields = fields()
  if Integer === fields.last
    *fields, tail = fields
  else
    tail = 1
  end
  orig_data = data.dup
  begin
    tuples = []
    serializers = []
    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 = fields[fieldno = i] || fields[fieldno = get_tail_no(fields, i, tail)]
        tuple << _unpack_field(tuple_str, field, i, fieldno, serializers)
        i += 1
      end
      tuples << tuple
      tuples_affected -= 1
    end
    tuples
  rescue ValueError => e
    $stderr.puts "Value Error: tuples=#{ta} now=#{ta-tuples_affected}, remains=#{data.bytesize} remains_data='#{data.unpack('H*')[0].gsub(/../,'\& ')}' orig_size=#{orig_data.size} orig_data='#{orig_data.unpack('H*')[0].gsub(/../,'\& ')}'"
    raise e
  end
end