Class: Aerospike::MultiCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/aerospike/command/multi_command.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from Command

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

Constructor Details

#initialize(node) ⇒ MultiCommand

Returns a new instance of MultiCommand.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aerospike/command/multi_command.rb', line 26

def initialize(node)
  super(node)

  @valid = true
  @mutex = Mutex.new

  @compressed_data_buffer = nil
  @compressed_data_offset = nil

  self
end

Instance Method Details

#compressed?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/aerospike/command/multi_command.rb', line 217

def compressed?
  @compressed_data_buffer != nil
end

#get_nodeObject



38
39
40
# File 'lib/aerospike/command/multi_command.rb', line 38

def get_node
  @node
end

#parse_group(receive_size) ⇒ Object



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

def parse_group(receive_size)
  @data_offset = 0

  while @data_offset < receive_size
    read_bytes(MSG_REMAINING_HEADER_SIZE)
    result_code = @data_buffer.read(5).ord & 0xFF

    # The only valid server return codes are "ok", "not found" and "filtered out".
    # If other return codes are received, then abort the batch.
    if result_code != 0 
        if result_code == Aerospike::ResultCode::KEY_NOT_FOUND_ERROR || result_code == Aerospike::ResultCode::FILTERED_OUT
        else
          raise Aerospike::Exceptions::Aerospike.new(result_code)
        end
    end

    # If cmd is the end marker of the response, do not proceed further
    info3 = @data_buffer.read(3).ord
    return false if (info3 & INFO3_LAST) == INFO3_LAST

    parse_row(result_code)
  end

  true
end

#parse_key(field_count) ⇒ Object



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

def parse_key(field_count)
  # in Stream queries, there are no keys
  return unless field_count > 0

  digest = nil
  namespace = nil
  set_name = nil
  user_key = nil

  i = 0
  while i < field_count
    read_bytes(4)

    fieldlen = @data_buffer.read_int32(0)
    read_bytes(fieldlen)

    fieldtype = @data_buffer.read(0).ord
    size = fieldlen - 1

    case fieldtype
    when Aerospike::FieldType::DIGEST_RIPE
      digest = @data_buffer.read(1, size)
    when Aerospike::FieldType::NAMESPACE
      namespace = @data_buffer.read(1, size).force_encoding('utf-8')
    when Aerospike::FieldType::TABLE
      set_name = @data_buffer.read(1, size).force_encoding('utf-8')
    when Aerospike::FieldType::KEY
      user_key = Aerospike::bytes_to_key_value(@data_buffer.read(1).ord, @data_buffer, 2, size-1)
    end

    i = i.succ
  end

  Aerospike::Key.new(namespace, set_name, user_key, digest)
end

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

Parses the given byte buffer and populate the result object. Returns the number of bytes that were parsed from the given buffer.



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

def parse_record(key, op_count, generation, expiration)
  bins = op_count > 0 ? {} : nil
  i = 0
  while i < op_count
    raise Aerospike::Exceptions::QueryTerminated.new unless valid?

    read_bytes(8)

    op_size = @data_buffer.read_int32(0).ord
    particle_type = @data_buffer.read(5).ord
    name_size = @data_buffer.read(7).ord

    read_bytes(name_size)
    name = @data_buffer.read(0, name_size).force_encoding('utf-8')

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

    bins[name] = value

    i = i.succ
  end

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

#parse_resultObject



42
43
44
45
46
47
48
49
50
51
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
# File 'lib/aerospike/command/multi_command.rb', line 42

def parse_result
  # Read socket into receive buffer one record at a time.  Do not read entire receive size
  # because the receive buffer would be too big.
  status = true

  while status
    @data_offset = 0
    @compressed_data_buffer = nil

    # Read header.
    read_bytes(8)

    size = @data_buffer.read_int64(0)
    receive_size = size & 0xFFFFFFFFFFFF

    # inflate if compressed
    compressed_sz = compressed_size
    if compressed_sz
      begin
        # read compressed msg header
        @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)
        receive_size = uncompressed.size - 8

        @compressed_data_buffer = Buffer.new(-1, uncompressed)
        @compressed_data_offset = 0

        # waste the first 8 header bytes
        @compressed_data_buffer.eat!(8)
      rescue => e
        Aerospike.logger.error("parse result error: #{e}")
        raise e
      end
    end

    if receive_size > 0
      status = parse_group(receive_size)
    else
      status = false
    end
  end
end

#read_bytes(length) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/aerospike/command/multi_command.rb', line 182

def read_bytes(length)
  if length > @data_buffer.length
    # Corrupted data streams can result in a huge length.
    # Do a sanity check here.
    if length > Aerospike::Buffer::MAX_BUFFER_SIZE
      raise Aerospike::Exceptions::Parse.new("Invalid read_bytes length: #{length}")
    end
    @data_buffer = Buffer.new(length)
  end

  if compressed?
    @data_buffer.write_binary(@compressed_data_buffer.buf[@compressed_data_offset...@compressed_data_offset+length], 0)
    @compressed_data_offset += length
  else
    @conn.read(@data_buffer, length)
  end

  @data_offset += length
end

#stopObject



202
203
204
205
206
# File 'lib/aerospike/command/multi_command.rb', line 202

def stop
  @mutex.synchronize do
    @valid = false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
# File 'lib/aerospike/command/multi_command.rb', line 208

def valid?
  res = nil
  @mutex.synchronize do
    res = @valid
  end

  res
end