Class: Tarantool::Response
- Inherits:
-
Struct
- Object
- Struct
- Tarantool::Response
- Includes:
- Serializers, Util::Packer, Util::TailGetter
- Defined in:
- lib/tarantool/response.rb
Constant Summary collapse
- UTF8 =
'utf-8'.freeze
Constants included from Serializers
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
-
#body ⇒ Object
Returns the value of attribute body.
-
#cb ⇒ Object
Returns the value of attribute cb.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#get_tuples ⇒ Object
Returns the value of attribute get_tuples.
-
#request_type ⇒ Object
Returns the value of attribute request_type.
-
#translators ⇒ Object
Returns the value of attribute translators.
Instance Method Summary collapse
- #call(data) ⇒ Object
- #call_callback(result) ⇒ Object
- #parse_response(data) ⇒ Object
- #return_code(data) ⇒ Object
- #unpack_tuples(data) ⇒ Object
Methods included from Serializers
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
20 21 22 |
# File 'lib/tarantool/response.rb', line 20 def body @body end |
#cb ⇒ Object
Returns the value of attribute cb
20 21 22 |
# File 'lib/tarantool/response.rb', line 20 def cb @cb end |
#fields ⇒ Object
Returns the value of attribute fields
20 21 22 |
# File 'lib/tarantool/response.rb', line 20 def fields @fields end |
#get_tuples ⇒ Object
Returns the value of attribute get_tuples
20 21 22 |
# File 'lib/tarantool/response.rb', line 20 def get_tuples @get_tuples end |
#request_type ⇒ Object
Returns the value of attribute request_type
20 21 22 |
# File 'lib/tarantool/response.rb', line 20 def request_type @request_type end |
#translators ⇒ Object
Returns the value of attribute translators
20 21 22 |
# File 'lib/tarantool/response.rb', line 20 def translators @translators end |
Instance Method Details
#call(data) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tarantool/response.rb', line 26 def call(data) if Exception === data cb.call(data) else if (ret = return_code(data)) == 0 call_callback parse_response(data) else data.gsub!("\x00", "") cb.call CODE_TO_EXCEPTION[ret].new(ret, data) end end end |
#call_callback(result) ⇒ Object
43 44 45 |
# File 'lib/tarantool/response.rb', line 43 def call_callback(result) cb.call(Exception === result || get_tuples != :first ? result : result.first) end |
#parse_response(data) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tarantool/response.rb', line 47 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 |
#return_code(data) ⇒ Object
161 162 163 |
# File 'lib/tarantool/response.rb', line 161 def return_code(data) ::BinUtils.slice_int32_le!(data) end |
#unpack_tuples(data) ⇒ Object
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 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 |
# File 'lib/tarantool/response.rb', line 62 def unpack_tuples(data) tuples_affected = ::BinUtils.slice_int32_le!(data) tuples = [] fields = fields() if Integer === fields.last *fields, tail = fields else tail = 1 end 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 |