Class: Aerospike::ReadCommand

Inherits:
SingleCommand show all
Defined in:
lib/aerospike/command/read_command.rb

Overview

:nodoc:

Direct Known Subclasses

ExecuteCommand, OperateCommand

Constant Summary collapse

BIN_NAME_ENCODING =
'utf-8'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#execute, #set_delete, #set_exists, #set_operate, #set_query, #set_read, #set_read_for_key_only, #set_read_header, #set_scan, #set_touch, #set_udf, #set_write, #write_bins

Constructor Details

#initialize(cluster, policy, key, bin_names) ⇒ ReadCommand

Returns a new instance of ReadCommand.



35
36
37
38
39
40
41
42
# File 'lib/aerospike/command/read_command.rb', line 35

def initialize(cluster, policy, key, bin_names)
  super(cluster, key)

  @bin_names = bin_names
  @policy = policy

  self
end

Instance Attribute Details

#policyObject (readonly)

Returns the value of attribute policy.



33
34
35
# File 'lib/aerospike/command/read_command.rb', line 33

def policy
  @policy
end

#recordObject (readonly)

Returns the value of attribute record.



33
34
35
# File 'lib/aerospike/command/read_command.rb', line 33

def record
  @record
end

Instance Method Details

#get_nodeObject



44
45
46
# File 'lib/aerospike/command/read_command.rb', line 44

def get_node
  @cluster.read_node(@partition, @policy.replica, @sequence)
end

#handle_udf_error(result_code) ⇒ Object



147
148
149
150
151
# File 'lib/aerospike/command/read_command.rb', line 147

def handle_udf_error(result_code)
  ret = @record.bins['FAILURE']
  raise Aerospike::Exceptions::Aerospike.new(result_code, ret, [@node]) if ret
  raise Aerospike::Exceptions::Aerospike.new(result_code, nil, [@node])
end

#parse_record(op_count, field_count, generation, expiration) ⇒ Object



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
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/aerospike/command/read_command.rb', line 153

def parse_record(op_count, field_count, generation, expiration)
  bins = op_count > 0 ? {} : nil
  receive_offset = 0
  single_bin_value = !policy.is_a?(OperatePolicy) || policy.record_bin_multiplicity == RecordBinMultiplicity::SINGLE

  # There can be fields in the response (setname etc).
  # But for now, ignore them. Expose them to the API if needed in the future.
  if field_count > 0
    # Just skip over all the fields
    i = 0
    while i < field_count
      field_size = @data_buffer.read_int32(receive_offset)
      receive_offset += (4 + field_size)
      i = i.succ
    end
  end

  i = 0
  while i < op_count
    op_size = @data_buffer.read_int32(receive_offset)
    particle_type = @data_buffer.read(receive_offset+5).ord
    name_size = @data_buffer.read(receive_offset+7).ord
    name = @data_buffer.read(receive_offset+8, name_size).force_encoding(BIN_NAME_ENCODING)
    receive_offset += 4 + 4 + name_size

    particle_bytes_size = op_size - (4 + name_size)
    value = Aerospike.bytes_to_particle(particle_type, @data_buffer, receive_offset, particle_bytes_size)
    receive_offset += particle_bytes_size

    if single_bin_value || !bins.has_key?(name)
      bins[name] = value
    elsif (prev = bins[name]).is_a?(OpResults)
      prev << value
    else
      bins[name] = OpResults.new << prev << value
    end

    i = i.succ
  end

  Record.new(@node, @key, bins, generation, expiration)
end

#parse_resultObject



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
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
# File 'lib/aerospike/command/read_command.rb', line 52

def parse_result
  # Read header.
  begin
    @conn.read(@data_buffer, 8)
  rescue => e
    Aerospike.logger.error("parse result error: #{e}")
    raise e
  end

  # inflate if compressed
  compressed_sz = compressed_size
  if compressed_sz
    begin
      # waste 8 size bytes
      @conn.read(@data_buffer, 8)

      # read compressed message
      @conn.read(@data_buffer, compressed_sz - 8)

      # inflate the results
      # TODO: reuse the current buffer
      uncompressed = Zlib.inflate(@data_buffer.buf)

      @data_buffer = Buffer.new(-1, uncompressed)
    rescue => e
      Aerospike.logger.error("parse result error: #{e}")
      raise e
    end
  else
    begin
      bytes_read = @conn.read(@data_buffer, MSG_TOTAL_HEADER_SIZE - 8, 8)
    rescue => e
      Aerospike.logger.error("parse result error: #{e}")
      raise e
    end
  end

  # A number of these are commented out because we just don't care enough to read
  # that section of the header. If we do care, uncomment and check!
  sz = @data_buffer.read_int64(0)
  header_length = @data_buffer.read(8).ord
  result_code = @data_buffer.read(13).ord & 0xFF
  generation = @data_buffer.read_int32(14)
  expiration = @data_buffer.read_int32(18)
  field_count = @data_buffer.read_int16(26) # almost certainly 0
  op_count = @data_buffer.read_int16(28)
  receive_size = (sz & 0xFFFFFFFFFFFF) - header_length

  # Read remaining message bytes.
  if compressed_sz
    @data_buffer.eat!(MSG_TOTAL_HEADER_SIZE)
  elsif receive_size > 0
    size_buffer_sz(receive_size)

    begin
      @conn.read(@data_buffer, receive_size)
    rescue => e
      Aerospike.logger.error("parse result error: #{e}")
      raise e
    end

  end

  if result_code == 0
    if op_count == 0
      @record = Record.new(@node, @key, nil, generation, expiration)
      return
    end

    @record = parse_record(op_count, field_count, generation, expiration)
    return
  end

  return nil if result_code == Aerospike::ResultCode::KEY_NOT_FOUND_ERROR

  if result_code == Aerospike::ResultCode::FILTERED_OUT
    if @policy.fail_on_filtered_out
      raise Aerospike::Exceptions::Aerospike.new(result_code, nil, [@node])
    end
    return
  end

  if result_code == Aerospike::ResultCode::UDF_BAD_RESPONSE
    begin
      @record = parse_record(op_count, field_count, generation, expiration)
      handle_udf_error(result_code)
    rescue => e
      Aerospike.logger.error("UDF execution error: #{e}")
      raise e
    end
  end

  raise Aerospike::Exceptions::Aerospike.new(result_code, nil, [@node])
end

#write_bufferObject



48
49
50
# File 'lib/aerospike/command/read_command.rb', line 48

def write_buffer
  set_read(@policy, @key, @bin_names)
end