Files

Class/Module Index [+]

Quicksearch

Tarantool::Request

Attributes

insert_with_shards_count[R]
previous_shards_count[R]
shard_proc[R]
shards_count[R]

Public Instance Methods

_all_shards() click to toggle source
# File lib/tarantool/shards_support.rb, line 157
def _all_shards
  (0...@shards_count).to_a
end
_call(func_name, values, cb, opts={}) click to toggle source
# File lib/tarantool/request.rb, line 344
def _call(func_name, values, cb, opts={})
  return_tuple = opts[:return_tuple] && :all
  flags = return_tuple ? BOX_RETURN_TUPLE : 0
  
  values = [*values]
  value_types = opts[:types] ? [*opts[:types]] :
                              _detect_types(values)
  return_types = [*opts[:returns] || TYPES_AUTO]

  func_name = func_name.to_s
  body = ::BinUtils.append_int32_le!(nil, flags)
  ::BinUtils.append_bersize_string!(body, func_name)
  pack_tuple(body, values, value_types, :func_call)

  shard_nums = opts[:shards] || all_shards
  read_write = case opts[:readonly]
               when nil, false, :write
                 :write
               when true, :read
                 :read
               when :replace
                 :replace
               else
                 raise ArgumentError, "space#call :readonly options accepts nil, false, :write, true, :read and :replace, but #{opts[:readonly].inspect} were sent"
               end

  _modify_request(REQUEST_CALL, body, return_types, return_tuple, cb, shard_nums, read_write, opts[:translators] || [])
end
_delete(space_no, pk, fields, pk_fields, cb, ret_tuple, shard_nums, translators = []) click to toggle source
# File lib/tarantool/request.rb, line 308
def _delete(space_no, pk, fields, pk_fields, cb, ret_tuple, shard_nums, translators = [])
  flags = ret_tuple ? BOX_RETURN_TUPLE : 0

  body = ::BinUtils.append_int32_le!(nil, space_no, flags)
  pack_tuple(body, pk, pk_fields, 0)

  _modify_request(REQUEST_DELETE, body, fields, ret_tuple, cb, shard_nums, :write, translators)
end
_detect_shard(shard_values) click to toggle source
# File lib/tarantool/shards_support.rb, line 143
def _detect_shard(shard_values)
  shard_values = [shard_values]  unless Array === shard_values
  shards = @shard_proc.call(shard_values, @shards_count, self) || _all_shards
  if @previous_shards_count
    prev_shards = @shard_proc.call(shard_values, @previous_shards_count, self) || _all_shards
    shards = [*shards, *prev_shards].uniq
  end
  shards
end
_detect_shard_for_insert(shard_values) click to toggle source
# File lib/tarantool/shards_support.rb, line 138
def _detect_shard_for_insert(shard_values)
  shard_values = [shard_values]  unless Array === shard_values
  @shard_proc.call(shard_values, @insert_with_shards_count, self) || _all_shards
end
_detect_shards(keys) click to toggle source
# File lib/tarantool/shards_support.rb, line 118
def _detect_shards(keys)
  _flat_uniq keys.map{|key| _detect_shard(key)}
end
_detect_shards_for_insert(keys) click to toggle source
# File lib/tarantool/shards_support.rb, line 122
def _detect_shards_for_insert(keys)
  _flat_uniq keys.map{|key| _detect_shard_for_insert(key)}
end
_detect_shards_for_key(key, index_no) click to toggle source
# File lib/tarantool/shards_support.rb, line 108
def _detect_shards_for_key(key, index_no)
  if index_no == @shard_by_index
    _detect_shard(key)
  elsif pos = @shard_for_index[index_no]
    _detect_shard(key.values_at(*pos))
  else
    _all_shards
  end
end
_detect_shards_for_keys(keys, index_no) click to toggle source
# File lib/tarantool/shards_support.rb, line 94
def _detect_shards_for_keys(keys, index_no)
  return _detect_shards_for_key(keys, index_no)  unless Array === keys
  if index_no == @shard_by_index && (
        @index_fields.size == 1 ||
        keys.all?{|key| Array === key && key.size == @index_fields.size}
      )
    _flat_uniq keys.map{|key| _detect_shard(key)}
  elsif pos = @shard_for_index[index_no]
    _flat_uniq keys.map{|key| _detect_shard([*key].values_at(*pos)) }
  else
    _all_shards
  end
end
_detect_type(value) click to toggle source
# File lib/tarantool/request.rb, line 393
def _detect_type(value)
  Integer === v ? :varint :
  Util::AutoType === v ? :auto :
  :string
end
_detect_types(values) click to toggle source
# File lib/tarantool/request.rb, line 389
def _detect_types(values)
  values.map{|v| Integer === v ? :varint : :string}
end
_flat_uniq(array) click to toggle source
# File lib/tarantool/shards_support.rb, line 126
def _flat_uniq(array)
  hsh = {}
  array.each do |v|
    if v.respond_to?(:each)
      v.each{|vv| hsh[vv] = true}
    else
      hsh[v] = true
    end
  end
  hsh.keys
end
_get_shard_nums() click to toggle source
# File lib/tarantool/shards_support.rb, line 153
def _get_shard_nums
  @shards_count == 1 ? @default_shard : yield
end
_init_shard_vars(shard_proc, init_shard_for_index = true) click to toggle source
# File lib/tarantool/shards_support.rb, line 61
def _init_shard_vars(shard_proc, init_shard_for_index = true)
  if init_shard_for_index
    @shard_by_index = @index_fields.index{|index| index == @shard_fields}
    @shard_for_index = @index_fields.map{|index|
        if (pos = @shard_fields.map{|name| index.index(name)}).any?
          pos.map{|i| i || 2**30}
        end
    }
    @shard_proc = case shard_proc
                  when nil, :default
                    DefaultShardProc.new
                  when :sumbur_murmur_fmix
                    SumburMurmurFmix.new
                  when :sumbur_murmur_int64
                    SumburMurmurInt64.new
                  when :sumbur_murmur_str
                    SumburMurmurStr.new
                  when :modulo, :module
                    ModuloShardProc.new
                  else
                    unless shard_proc.respond_to?(:call)
                      raise ArgumentError, "Wrong sharding proc object #{shard_proc.inspect}"
                    end
                    shard_proc
                  end
  end

  @shards_count = @tarantool.shards_count
  @previous_shards_count = @tarantool.previous_shards_count
  @insert_with_shards_count = @tarantool.insert_with_shards_count
  @default_shard = 0
end
_insert(space_no, flags, tuple, fields, cb, ret_tuple, shard_nums, in_any_shard = nil, translators = []) click to toggle source
# File lib/tarantool/request.rb, line 190
def _insert(space_no, flags, tuple, fields, cb, ret_tuple, shard_nums, in_any_shard = nil, translators = [])
  flags |= BOX_RETURN_TUPLE  if ret_tuple
  fields = [*fields]

  tuple = [*tuple]
  body = ::BinUtils.append_int32_le!(nil, space_no, flags)
  pack_tuple(body, tuple, fields, :space)

  _modify_request(REQUEST_INSERT, body, fields, ret_tuple, cb, shard_nums,
                  in_any_shard ? :replace : :write, translators)
end
_modify_request(type, body, fields, ret_tuple, cb, shard_nums, read_write, translators) click to toggle source
# File lib/tarantool/request.rb, line 183
def _modify_request(type, body, fields, ret_tuple, cb, shard_nums, read_write, translators)
  response = Response.new(cb, type, body, ret_tuple && (ret_tuple != :all ? :first : :all),
                        fields, translators)
  _send_request(shard_nums, read_write, response)
end
_pack_operations(body, operations, fields) click to toggle source
# File lib/tarantool/request.rb, line 218
def _pack_operations(body, operations, fields)
  if Integer === fields.last
    *fields, tail = fields
  else
    tail = 1
  end
  for operation in operations
    if Array === operation[0]
      operation = operation[0] + operation.drop(1)
    elsif Array === operation[1]
      if operation.size == 2
        operation = [operation[0]] + operation[1]
      else
        raise ArgumentError, "Could not understand operation #{operation}"
      end
    end

    field_no = operation[0]
    if operation.size == 2
      if (Integer === field_no || field_no =~ /\A\d/)
        unless Symbol === operation[1] && UPDATE_OPS[operation[1]] == 6
          ::BinUtils.append_int32_int8_le!(body, field_no, 0)
          type = fields[field_no] || get_tail_item(fields, field_no, tail) ||
            _detect_type(operation[1])
          pack_field(body, type, operation[1])
          next
        end
      elsif String === field_no
        operation.insert(1, field_no.slice(0, 1))
        field_no = field_no.slice(1..-1).to_i
      else
        raise ArgumentError, "Could not understand field number #{field_no.inspect}"
      end
    end

    op = operation[1]
    op = UPDATE_OPS[op]  unless Integer === op
    raise ArgumentError, "Unknown operation #{operation[1]}" unless op
    ::BinUtils.append_int32_int8_le!(body, field_no, op)
    case op
    when 0
      if (type = fields[field_no]).nil?
        if operation.size == 4 && Symbol === operation.last
          *operation, type = operation
        else
          type = get_tail_item(fields, field_no, tail) || _detect_type(operation[2])
        end
      end
      unless operation.size == 3
        raise ArgumentError, "wrong arguments for set or insert operation #{operation.inspect}"
      end
      pack_field(body, type, operation[2])
    when 1, 2, 3, 4
      unless operation.size == 3 && !operation[2].nil?
        raise ArgumentError, "wrong arguments for integer operation #{operation.inspect}"
      end
      pack_field(body, :sint, operation[2])
    when 5
      unless operation.size == 5 && !operation[2].nil? && !operation[3].nil?
        raise ArgumentError, "wrong arguments for slice operation #{operation.inspect}"
      end

      str = operation[4].to_s
      ::BinUtils.append_ber!(body, 10 + ber_size(str.bytesize) + str.bytesize)
      ::BinUtils.append_bersize_int32_le!(body, operation[2].to_i)
      ::BinUtils.append_bersize_int32_le!(body, operation[3].to_i)
      ::BinUtils.append_bersize_string!(body, str.to_s)
    when 7
      old_field_no = field_no + 
        (inserted ||= []).count{|i| i <= field_no} -
        (deleted ||= []).count{|i| i <= field_no}
      inserted << field_no
      if (type = fields[old_field_no]).nil?
        if operation.size == 4 && Symbol === operation.last
          *operation, type = operation
        else
          type = get_tail_item(fields, old_field_no, tail)
        end
      end
      unless operation.size == 3
        raise ArgumentError, "wrong arguments for set or insert operation #{operation.inspect}"
      end
      pack_field(body, type, operation[2])
    when 6
      body << ZERO
      # pass
    end
  end
end
_parse_hash_definition(returns) click to toggle source
# File lib/tarantool/request.rb, line 399
def _parse_hash_definition(returns)
  field_names = []
  field_types = []
  returns.each{|name, type|
    field_names << name
    field_types << type
  }
  field_types << if field_names.include?(:_tail)
      unless field_names.last == :_tail
        raise ArgumentError, "_tail should be de declared last"
      end
      field_names.pop
      [*field_types.last].size
    else
      1
    end
  field_types.flatten!
  [field_types, TranslateToHash.new(field_names, field_types.last)]
end
_ping(cb) click to toggle source
# File lib/tarantool/request.rb, line 384
def _ping(cb)
  _send_request(all_shards, :write, REQUEST_PING, EMPTY, WrapPing.new(cb))
end
Also aliased as: ping_cb
_raise_integer_overflow(value, min, max) click to toggle source
# File lib/tarantool/request.rb, line 179
def _raise_integer_overflow(value, min, max)
    raise IntegerFieldOverflow, "#{value} not in (#{min}..#{max})"
end
_raise_or_return(res) click to toggle source
# File lib/tarantool/request.rb, line 419
def _raise_or_return(res)
  raise res  if Exception === res
  res
end
_select(space_no, index_no, offset, limit, keys, cb, fields, index_fields, shard_nums, translators = []) click to toggle source
# File lib/tarantool/request.rb, line 63
def _select(space_no, index_no, offset, limit, keys, cb, fields, index_fields, shard_nums, translators = [])
  get_tuples = limit == :first ? (limit = 1; :first) : :all
  keys = [*keys]
  body = ::BinUtils.append_int32_le!(nil, space_no, index_no, offset, limit, keys.size)

  for key in keys
    pack_tuple(body, key, index_fields, index_no)
  end
  response = Response.new(cb, REQUEST_SELECT, body, get_tuples, fields, translators)
  _send_request(shard_nums, :read, response)
end
_send_request(shard_numbers, read_write, cb) click to toggle source
# File lib/tarantool/request.rb, line 58
def _send_request(shard_numbers, read_write, cb)
  @tarantool._send_request(shard_numbers, read_write, cb)
end
_space_call_fix_values(values, space_no, opts) click to toggle source
# File lib/tarantool/request.rb, line 317
def _space_call_fix_values(values, space_no, opts)
  opts = opts.dup
  space_no = opts[:space_no]  if opts.has_key?(:space_no)
  if space_no
    values = [space_no].concat([*values])
    if opts[:types]
      opts[:types] = [:string].concat([*opts[:types]]) # cause lua could convert it to integer by itself
    else
      opts[:types] = TYPES_STR_STR
    end
  end

  # scheck for shards hints
  opts[:shards] ||= _get_shard_nums do
      if opts[:shard_for_insert]
        opts[:shard_keys] ? _detect_shards_for_insert(opts[:shard_keys]) :
        opts[:shard_key]  ? _detect_shard_for_insert( opts[:shard_key]) :
                             _all_shards
      else
        opts[:shard_keys] ? _detect_shards(opts[:shard_keys]) :
        opts[:shard_key]  ? _detect_shard( opts[:shard_key]) :
                             _all_shards
      end
    end
  [values, opts]
end
_update(space_no, pk, operations, fields, pk_fields, cb, ret_tuple, shard_nums, translators = []) click to toggle source
# File lib/tarantool/request.rb, line 202
def _update(space_no, pk, operations, fields, pk_fields, cb, ret_tuple, shard_nums, translators = [])
  flags = ret_tuple ? BOX_RETURN_TUPLE : 0

  if Array === operations && !(Array === operations.first)
    operations = [operations]
  end

  body = ::BinUtils.append_int32_le!(nil, space_no, flags)
  pack_tuple(body, pk, pk_fields, 0)
  ::BinUtils.append_int32_le!(body, operations.size)

  _pack_operations(body, operations, fields)

  _modify_request(REQUEST_UPDATE, body, fields, ret_tuple, cb, shard_nums, :write, translators)
end
all_shards() click to toggle source
# File lib/tarantool/shards_support.rb, line 177
def all_shards
  @shards_count == 1 ? @default_shard : (0...@shards_count).to_a
end
detect_shard(shard_values) click to toggle source
# File lib/tarantool/shards_support.rb, line 161
def detect_shard(shard_values)
  @shards_count == 1 ? @default_shard : _detect_shard(shard_values)
end
detect_shard_for_insert(shard_values) click to toggle source
# File lib/tarantool/shards_support.rb, line 165
def detect_shard_for_insert(shard_values)
  @shards_count == 1 ? @default_shard : _detect_shard_for_insert(shard_values)
end
detect_shards(shard_values) click to toggle source
# File lib/tarantool/shards_support.rb, line 169
def detect_shards(shard_values)
  @shards_count == 1 ? @default_shard : _detect_shards(shard_values)
end
detect_shards_for_insert(shard_values) click to toggle source
# File lib/tarantool/shards_support.rb, line 173
def detect_shards_for_insert(shard_values)
  @shards_count == 1 ? @default_shard : _detect_shards_for_insert(shard_values)
end
pack_field(body, field_kind, value) click to toggle source
# File lib/tarantool/request.rb, line 105
def pack_field(body, field_kind, value)
  if value.nil?
    body << ZERO
    return
  end
  case field_kind
  when :int, :integer
    value = value.to_i
    _raise_integer_overflow(value, MIN_INT, MAX_INT32)  if value > MAX_INT32 or value < 0
    ::BinUtils.append_bersize_int32_le!(body, value)
  when :string, :bytes, :str
    value = value.to_s
    value = ZERO + value  if value < ONE
    raise StringTooLong  if value.bytesize >= MAX_BYTE_SIZE
    ::BinUtils.append_bersize_string!(body, value)
  when :bytes
    value = value.to_s
    raise StringTooLong  if value.bytesize >= MAX_BYTE_SIZE
    ::BinUtils.append_bersize_string!(body, value)
  when :int64
    value = value.to_i
    _raise_integer_overflow(value, MIN_INT, MAX_INT64)  if value > MAX_INT64 or value < 0
    ::BinUtils.append_bersize_int64_le!(body, value)
  when :int16
    value = value.to_i
    _raise_integer_overflow(value, MIN_INT, MAX_INT16)  if value > MAX_INT16 or value < 0
    ::BinUtils.append_bersize_int16_le!(body, value)
  when :int8
    value = value.to_i
    _raise_integer_overflow(value, MIN_INT, MAX_INT8)  if value > MAX_INT8 or value < 0
    ::BinUtils.append_bersize_int8!(body, value)
  when :sint
    value = value.to_i
    _raise_integer_overflow(value, MIN_SINT32, MAX_SINT32)  if value > MAX_SINT32 or value < MIN_SINT32
    ::BinUtils.append_bersize_int32_le!(body, value)
  when :sint64
    value = value.to_i
    _raise_integer_overflow(value, MIN_SINT64, MAX_SINT64)  if value > MAX_SINT64 or value < MIN_SINT64
    ::BinUtils.append_bersize_int64_le!(body, value)
  when :sint16
    value = value.to_i
    _raise_integer_overflow(value, MIN_SINT16, MAX_SINT16)  if value > MAX_SINT16 or value < MIN_SINT16
    ::BinUtils.append_bersize_int16_le!(body, value)
  when :sint8
    value = value.to_i
    _raise_integer_overflow(value, MIN_SINT8, MAX_SINT8)  if value > MAX_SINT8 or value < MIN_SINT8
    ::BinUtils.append_bersize_int8!(body, value)
  when :varint
    value = value.to_i
    if 0 <= value && value < MAX_INT32
      ::BinUtils.append_bersize_int32_le!(body, value)
    else
      ::BinUtils.append_bersize_int64_le!(body, value)
    end
  when :error
    raise IndexIndexError
  when :auto
    case value
    when Integer
      pack_field(body, :varint, value)
    when String
      pack_field(body, :bytes, value)
    when Util::AutoType
      pack_field(body, :bytes, value.data)
    else
      raise ArgumentError, "Could auto detect only Integer and String"
    end
  else
    value = get_serializer(field_kind).encode(value).to_s
    raise StringTooLong  if value.bytesize > MAX_BYTE_SIZE
    ::BinUtils.append_bersize_string!(body, value)
  end
end
pack_tuple(body, key, types, index_no = 0) click to toggle source
# File lib/tarantool/request.rb, line 76
def pack_tuple(body, key, types, index_no = 0)
  if Integer === types.last
    *types, tail = types
  else
    tail = 1
  end
  case key
  when Array
    if index_no != :space && nili = key.index(nil)
      key = key.slice(0, nili)
    end
    ::BinUtils.append_int32_le!(body, key_size = key.size)
    i = 0
    while i < key_size
      field = types[i] || get_tail_item(types, i, tail)
      pack_field(body, field, key[i])
      i += 1
    end
  when nil
    body << INT32_0
  else
    body << INT32_1
    pack_field(body, types[0], key)
  end
rescue IndexIndexError
  raise ArgumentError, "tuple #{key} has more entries than index #{index_no}"
end
ping_cb(cb) click to toggle source
Alias for: _ping
shard(shard_number) click to toggle source
# File lib/tarantool/shards_support.rb, line 181
def shard(shard_number)
  case shard_number
  when Integer
    if shard_number >= @shards_count
      raise ArgumentError, "There is no shard #{shard_number}, amount of shards is #{@shards_count}"
    end
  when Array
    shard_number.each do|i|
      if i >= @shards_count
        raise ArgumentError, "There is no shard #{i}, amount of shards is #{@shards_count}"
      end
    end
  else
    raise ArgumentError, "Shard number should be integer or array of integers"
  end
  (@_fixed_shards ||= {})[shard_number] ||=
      clone.instance_exec do
        @shards_count = 1
        @default_shard = shard_number
        self
      end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.