Class: Tarantool::SpaceArray

Inherits:
Object
  • Object
show all
Includes:
CommonSpace, CommonSpaceBlockMethods, Request, Util::Array
Defined in:
lib/tarantool/space_array.rb

Constant Summary

Constants included from Request

Request::BINARY, Request::BOX_ADD, Request::BOX_REPLACE, Request::BOX_RETURN_TUPLE, Request::CALL_HEADER, Request::DELETE_HEADER, Request::EMPTY, Request::INSERT_HEADER, Request::INT32_0, Request::INT32_1, Request::LEST_INT32, Request::MAX_BYTE_SIZE, Request::ONE, Request::PACK_STRING, Request::REQUEST_CALL, Request::REQUEST_DELETE, Request::REQUEST_INSERT, Request::REQUEST_PING, Request::REQUEST_SELECT, Request::REQUEST_UPDATE, Request::SELECT_HEADER, Request::TYPES_AUTO, Request::TYPES_FALLBACK, Request::TYPES_STR_AUTO, Request::TYPES_STR_STR, Request::UPDATE_FIELDNO_OP, Request::UPDATE_HEADER, Request::UPDATE_OPS, Request::ZERO

Constants included from Serializers

Tarantool::Serializers::MAP

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

Attributes included from Request

#insert_with_shards_count, #previous_shards_count, #shard_proc, #shards_count

Attributes included from CommonSpace

#space_no, #tarantool

Instance Method Summary collapse

Methods included from CommonSpaceBlockMethods

#all_by_pks_blk, #by_pk_blk, #call_blk, #delete_blk, #insert_blk, #invoke_blk, #ping_blk, #replace_blk, #store_blk, #update_blk

Methods included from Util::Array

#frozen_array

Methods included from Request

#_all_shards, #_call, #_delete, #_detect_shard, #_detect_shard_for_insert, #_detect_shards, #_detect_shards_for_insert, #_detect_shards_for_key, #_detect_shards_for_keys, #_detect_type, #_detect_types, #_flat_uniq, #_get_shard_nums, #_init_shard_vars, #_insert, #_modify_request, #_pack_operations, #_parse_hash_definition, #_ping, #_raise_integer_overflow, #_raise_or_return, #_select, #_send_request, #_space_call_fix_values, #_update, #all_shards, #detect_shard, #detect_shard_for_insert, #detect_shards, #detect_shards_for_insert, #pack_field, #pack_tuple, #shard

Methods included from Serializers

#check_type, #get_serializer

Constructor Details

#initialize(tarantool, space_no, fields, indexes, shard_fields = nil, shard_proc = nil) ⇒ SpaceArray

Returns a new instance of SpaceArray.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tarantool/space_array.rb', line 13

def initialize(tarantool, space_no, fields, indexes, shard_fields = nil, shard_proc = nil)
  @tarantool = tarantool
  @space_no = space_no

  fields = ([*fields].empty? ? TYPES_AUTO : fields).dup
  tail_size = Integer === fields.last ? fields.pop : 1
  fields.map!{|type| check_type(type)}
  fields << tail_size
  @fields = fields

  indexes = [*indexes].map{|ind| frozen_array(ind) }.freeze
  if !indexes.empty?
    @index_fields = indexes
    @indexes = _map_indexes(indexes)
  else
    @index_fields = []
    @indexes = [TYPES_FALLBACK]
  end

  shard_fields ||= @index_fields[0]
  unless shard_fields || @tarantool.shards_count == 1
    raise ArgumentError, "You could not use space without specifying primary key or shard fields when shards count is greater than 1"
  else
    @shard_fields = [*shard_fields]
    @shard_positions = @shard_fields
    _init_shard_vars(shard_proc)
  end
end

Instance Method Details

#_fix_index_fields(index_fields, keys) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/tarantool/space_array.rb', line 73

def _fix_index_fields(index_fields, keys)
  sorted = index_fields.sort
  if index_no = @index_fields.index{|fields| fields.take(index_fields.size).sort == sorted}
    real_fields = @index_fields[index_no]
    permutation = index_fields.map{|i| real_fields.index(i)}
    keys = [*keys].map{|v| [*v].values_at(*permutation)}
    [index_no, keys]
  end
end

#_map_indexes(indexes) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/tarantool/space_array.rb', line 42

def _map_indexes(indexes)
  indexes.map do |index|
    (index.map do |i|
      unless (field = @fields[i]) && !field.is_a?(Integer)
        raise ArgumentError, "Wrong index field number: #{index} #{i}"
      end
      field
    end << :error).freeze
  end.freeze
end

#all_by_key_blk(index_no, key, opts = {}, &block) ⇒ Object

callback with block api



162
163
164
# File 'lib/tarantool/space_array.rb', line 162

def all_by_key_blk(index_no, key, opts={}, &block)
  all_by_key_cb(index_no, key, block, opts)
end

#all_by_key_cb(index_no, key, cb, opts = {}) ⇒ Object



61
62
63
# File 'lib/tarantool/space_array.rb', line 61

def all_by_key_cb(index_no, key, cb, opts={})
  select_cb(index_no, [key], opts[:offset] || 0, opts[:limit] || -1, cb)
end

#all_by_keys_blk(index_no, keys, opts = {}, &block) ⇒ Object



170
171
172
# File 'lib/tarantool/space_array.rb', line 170

def all_by_keys_blk(index_no, keys, opts={}, &block)
  all_by_keys_cb(index_no, keys, block, opts)
end

#all_by_keys_cb(index_no, keys, cb, opts = {}) ⇒ Object



69
70
71
# File 'lib/tarantool/space_array.rb', line 69

def all_by_keys_cb(index_no, keys, cb, opts = {})
  select_cb(index_no, keys, opts[:offset] || 0, opts[:limit] || -1, cb)
end

#all_by_pks_cb(keys, cb, opts = {}) ⇒ Object



53
54
55
# File 'lib/tarantool/space_array.rb', line 53

def all_by_pks_cb(keys, cb, opts={})
  all_by_keys_cb(0, keys, cb, opts)
end

#by_pk_cb(pk, cb) ⇒ Object



57
58
59
# File 'lib/tarantool/space_array.rb', line 57

def by_pk_cb(pk, cb)
  first_by_key_cb(0, pk, cb)
end

#call_cb(func_name, values, cb, opts = {}) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/tarantool/space_array.rb', line 151

def call_cb(func_name, values, cb, opts = {})
  values, opts = _space_call_fix_values(values, @space_no, opts)

  opts[:return_tuple] = true  if opts[:return_tuple].nil?
  opts[:returns] ||= @fields   if opts[:return_tuple]

  _call(func_name, values, cb, opts)
end

#delete_cb(pk, cb, opts = {}) ⇒ Object



139
140
141
142
143
144
# File 'lib/tarantool/space_array.rb', line 139

def delete_cb(pk, cb, opts = {})
  shard_nums = _get_shard_nums{ _detect_shards_for_key(pk, 0) }
  _delete(@space_no, pk, @fields,
          @indexes[0] || _detect_types([*pk]),
          cb, opts[:return_tuple], shard_nums)
end

#first_by_key_blk(index_no, key, &block) ⇒ Object



166
167
168
# File 'lib/tarantool/space_array.rb', line 166

def first_by_key_blk(index_no, key, &block)
  first_by_key_cb(index_no, key, block)
end

#first_by_key_cb(index_no, key, cb) ⇒ Object



65
66
67
# File 'lib/tarantool/space_array.rb', line 65

def first_by_key_cb(index_no, key, cb)
  select_cb(index_no, [key], 0, :first, cb)
end

#insert_cb(tuple, cb, opts = {}) ⇒ Object



109
110
111
112
# File 'lib/tarantool/space_array.rb', line 109

def insert_cb(tuple, cb, opts = {})
  shard_nums = detect_shard_for_insert(tuple.values_at(*@shard_positions))
  _insert(@space_no, BOX_ADD, tuple, @fields, cb, opts[:return_tuple], shard_nums)
end

#invoke_cb(func_name, values, cb, opts = {}) ⇒ Object



146
147
148
149
# File 'lib/tarantool/space_array.rb', line 146

def invoke_cb(func_name, values, cb, opts = {})
  values, opts = _space_call_fix_values(values, @space_no, opts)
  _call(func_name, values, cb, opts)
end

#replace_cb(tuple, cb, opts = {}) ⇒ Object



114
115
116
117
118
# File 'lib/tarantool/space_array.rb', line 114

def replace_cb(tuple, cb, opts = {})
  shard_nums = detect_shard(tuple.values_at(*@shard_positions))
  _insert(@space_no, BOX_REPLACE, tuple, @fields,
          cb, opts[:return_tuple], shard_nums, opts.fetch(:in_any_shard, true))
end

#select_blk(index_no, keys, offset = 0, limit = -1,, &block) ⇒ Object



174
175
176
# File 'lib/tarantool/space_array.rb', line 174

def select_blk(index_no, keys, offset=0, limit=-1, &block)
  select_cb(index_no, keys, offset, limit, block)
end

#select_cb(index_no, keys, offset, limit, cb) ⇒ Object



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
# File 'lib/tarantool/space_array.rb', line 83

def select_cb(index_no, keys, offset, limit, cb)
  if Array === index_no
    raise ArgumentError, "Has no defined indexes to search index #{index_no}"  if @index_fields.empty?
    index_fields = index_no
    index_no = @index_fields.index{|fields| fields.take(index_fields.size) == index_fields}
    unless index_no || index_fields.size == 1
      index_no, keys = _fix_index_fields(index_fields, keys)
      unless index_no
         raise(ArgumentError, "Not found index with field numbers #{index_no}, " +
                           "(defined indexes #{@index_fields})")
      end
    end
  end
  unless @index_fields.empty?
    unless index_types = @indexes[index_no]
      raise ArgumentError, "No index ##{index_no}"
    end
  else
    index_types = _detect_types([*[*keys][0]])
  end

  shard_nums = _get_shard_nums { _detect_shards_for_keys(keys, index_no) }

  _select(@space_no, index_no, offset, limit, keys, cb, @fields, index_types, shard_nums)
end

#store_cb(tuple, cb, opts = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/tarantool/space_array.rb', line 120

def store_cb(tuple, cb, opts = {})
  shard_nums = _get_shard_nums{
    if opts.fetch(:to_insert_shard, true)
      _detect_shard_for_insert(tuple.values_at(*@shard_positions))
    else
      _detect_shard(tuple.values_at(*@shard_positions))
    end
  }
  _insert(@space_no, 0, tuple, @fields, cb, opts[:return_tuple], shard_nums)
end

#update_cb(pk, operations, cb, opts = {}) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/tarantool/space_array.rb', line 131

def update_cb(pk, operations, cb, opts = {})
  shard_nums = _get_shard_nums{ _detect_shards_for_key(pk, 0) }
  _update(@space_no, pk, operations, @fields,
          @indexes[0] || _detect_types([*pk]),
          cb, opts[:return_tuple],
          shard_nums)
end