Class: Tarantool::Select

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tarantool/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Select

Returns a new instance of Select.



7
8
9
# File 'lib/tarantool/record.rb', line 7

def initialize(record)
  @record = record
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



6
7
8
# File 'lib/tarantool/record.rb', line 6

def record
  @record
end

Instance Method Details

#allObject



68
69
70
# File 'lib/tarantool/record.rb', line 68

def all
  to_a
end

#batches_each(&blk) ⇒ Object

def _batch_exec

Tarantool.call proc_name: 'box.select_range', args: [space_no.to_s, @index_no.to_s, count.to_s] + @tuples.first.map(&:to_s), return_tuple: true

end



64
65
66
# File 'lib/tarantool/record.rb', line 64

def batches_each(&blk)
  batches { |records| records.each(&blk) }
end

#detect_index_no(keys) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tarantool/record.rb', line 76

def detect_index_no(keys)
  index_no = nil
  record.indexes.each_with_index do |v, i|
    keys_inst = keys.dup
    v.each do |index_part|
      unless keys_inst.delete(index_part)
        break
      end
      if keys_inst.size == 0
        index_no = i
      end
    end
    break if index_no
  end
  index_no
end

#each(&blk) ⇒ Object



15
16
17
18
19
20
# File 'lib/tarantool/record.rb', line 15

def each(&blk)
  res = record.space.select(*@tuples, index_no: @index_no, limit: @limit, offset: @offset).tuples
  res.each do |tuple|
    blk.call record.from_server(tuple)
  end
end

#firstObject



72
73
74
# File 'lib/tarantool/record.rb', line 72

def first
  limit(1).all.first
end

#limit(limit) ⇒ Object



22
23
24
25
# File 'lib/tarantool/record.rb', line 22

def limit(limit)
  @limit = limit
  self
end

#offset(offset) ⇒ Object



27
28
29
30
# File 'lib/tarantool/record.rb', line 27

def offset(offset)
  @offset = offset
  self
end

#space_noObject



11
12
13
# File 'lib/tarantool/record.rb', line 11

def space_no
  record.space_no
end

#where(params) ⇒ Object

id: 1 id: [1, 2, 3]

{ name: ‘a’, email: ‘a’}, { name: ‘b’, email: ‘b’}

Raises:

  • (SelectError)


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

def where(params)
  raise SelectError.new('Where condition already setted') if @index_no # todo?
  keys, @tuples = case params
  when Hash
    ordered_keys = record.ordered_keys params.keys
    # name: ['a', 'b'], email: ['c', 'd'] => [['a', 'c'], ['b', 'd']]
    if params.values.first.is_a?(Array)          
      [ordered_keys, params[ordered_keys.first].zip(*ordered_keys[1, ordered_keys.size].map { |k| params[k] })]
    else
      [ordered_keys, [record.hash_to_tuple(params)]]
    end
  when Array
    [params.first.keys, params.map { |v| record.hash_to_tuple(v) }]
  end
  @index_no = detect_index_no keys
  raise ArgumentError.new("Undefined index for keys #{keys}") unless @index_no
  self
end