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

def initialize(node)
  super(node)

  @valid = true
  @mutex = Mutex.new

  self
end

Instance Method Details

#parse_group(receive_size) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aerospike/command/multi_command.rb', line 55

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" and "not found".
    # If other return codes are received, then abort the batch.
    if result_code != 0 && result_code != Aerospike::ResultCode::KEY_NOT_FOUND_ERROR
      raise Aerospike::Exceptions::Aerospike.new(result_code)
    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



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

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.



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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/aerospike/command/multi_command.rb', line 35

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
    # Read header.
    read_bytes(8)

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

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

#read_bytes(length) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/aerospike/command/multi_command.rb', line 143

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

  @conn.read(@data_buffer, length)
  @data_offset += length
end

#stopObject



157
158
159
160
161
# File 'lib/aerospike/command/multi_command.rb', line 157

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

#valid?Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
# File 'lib/aerospike/command/multi_command.rb', line 163

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

  res
end