Class: Tarantool16::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/tarantool16/db.rb

Direct Known Subclasses

DumbDB

Constant Summary collapse

UNDEF =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, opts = {}) ⇒ DB

Returns a new instance of DB.



6
7
8
9
10
11
12
13
# File 'lib/tarantool16/db.rb', line 6

def initialize(host, opts = {})
  @host = host
  @opts = opts.dup
  @future = nil
  @spaces = nil
  @defined_fields = {}
  @conn = self.class::Connection.new(@host, @opts)
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



4
5
6
# File 'lib/tarantool16/db.rb', line 4

def conn
  @conn
end

Instance Method Details

#_call(name, args, cb) ⇒ Object



210
211
212
# File 'lib/tarantool16/db.rb', line 210

def _call(name, args, cb)
  conn._call(name, args, cb)
end

#_call16(name, args, cb) ⇒ Object



214
215
216
# File 'lib/tarantool16/db.rb', line 214

def _call16(name, args, cb)
  conn._call16(name, args, cb)
end

#_delete(sno, ino, key, need_hash, cb) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/tarantool16/db.rb', line 145

def _delete(sno, ino, key, need_hash, cb)
  ino = 0 if ino.nil? && key.is_a?(Array)
  if !need_hash && sno.is_a?(Integer) && ino.is_a?(Integer) && key.is_a?(Array)
    return conn._delete(sno, ino, key, cb)
  end
  _with_space(sno, cb) do |sp|
    sp.get_ino(ino, key, ITERATOR_EQ, cb) do |_ino, _key|
      _cb = need_hash ? sp.wrap_cb(cb) : cb
      conn._delete(sp.sid, _ino, _key, _cb)
    end
  end
end

#_eval(expr, args, cb) ⇒ Object



218
219
220
# File 'lib/tarantool16/db.rb', line 218

def _eval(expr, args, cb)
  conn._eval(expr, args, cb)
end

#_fill_indices(spaces, rows) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tarantool16/db.rb', line 106

def _fill_indices(spaces, rows)
  rows.
    map{|row|
      if row[4].is_a? Hash
        # new format
        [row[0], [row[2], row[1], row[3], row[5].map(&:first)]]
      else
        [row[0], [row[2], row[1], row[3], 6.step(row.size-1, 2).map{|i| row[i]}]]
      end
    }.
    group_by{|sid, _| sid}.
    each do |sid, inds|
      sp = spaces[sid]
      sp.indices = inds.map{|_sid, ind| ind}
    end 
end

#_fill_spaces(rows) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/tarantool16/db.rb', line 95

def _fill_spaces(rows)
  @spaces = {}
  rows.each do |row|
    fields = @defined_fields[row[0]] || @defined_fields[row[2]] || row[6]
    sp = SchemaSpace.new(row[0], row[2], fields)
    @spaces[row[0]] = sp
    @spaces[sp.name] = sp
    @spaces[sp.name.to_sym] = sp
  end
end

#_insert(sno, tuple, need_hash, cb) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/tarantool16/db.rb', line 123

def _insert(sno, tuple, need_hash, cb)
  if !need_hash && sno.is_a?(Integer) && tuple.is_a?(Array)
    return conn._insert(sno, tuple, cb)
  end
  _with_space(sno, cb) do |sp|
    _tuple = tuple.is_a?(Hash) ? sp.map_tuple(tuple) : tuple
    _cb = need_hash ? sp.wrap_cb(cb) : cb
    conn._insert(sp.sid, _tuple, _cb)
  end
end

#_ping(cb) ⇒ Object



222
223
224
# File 'lib/tarantool16/db.rb', line 222

def _ping(cb)
  conn._ping(cb)
end

#_replace(sno, tuple, need_hash, cb) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/tarantool16/db.rb', line 134

def _replace(sno, tuple, need_hash, cb)
  if !need_hash && sno.is_a?(Integer) && tuple.is_a?(Array)
    return conn._replace(sno, tuple, cb)
  end
  _with_space(sno, cb) do |sp|
    _tuple = tuple.is_a?(Hash) ? sp.map_tuple(tuple) : tuple
    _cb = need_hash ? sp.wrap_cb(cb) : cb
    conn._replace(sp.sid, _tuple, _cb)
  end
end

#_select(sno, ino, key, offset, limit, iterator, need_hash, cb) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/tarantool16/db.rb', line 158

def _select(sno, ino, key, offset, limit, iterator, need_hash, cb)
  key = [] if key.nil?
  ino = 0 if ino.nil? && key.is_a?(Array)
  unless iterator.is_a?(Integer)
    if key.empty? && (Array === key || Hash === key)
      iterator = ITERATOR_ALL
    else
      iterator = ::Tarantool16.iter(iterator)
    end
  end
  if sno.is_a?(Integer) && ino.is_a?(Integer) && (key.is_a?(Array) || key.nil?)
    return conn._select(sno, ino, key, offset, limit, iterator, cb)
  end
  _with_space(sno, cb) do |sp|
    sp.get_ino(ino, key, iterator, cb) do |_ino, _key|
      _cb = need_hash ? sp.wrap_cb(cb) : cb
      conn._select(sp.sid, _ino, _key, offset, limit, iterator, _cb)
    end
  end
end

#_space_futureObject



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
90
91
92
93
# File 'lib/tarantool16/db.rb', line 56

def _space_future
  _synchronized do
    return @future if @future
    future = @future = self.class::SchemaFuture.new
    fill_indexes = nil
    spaces = nil
    fill_spaces = lambda do|r|
      unless r.ok?
        future.set r
        _synchronized do
          @future = nil
        end
      else
        _synchronized do
          _fill_spaces(r.data)
          spaces = @spaces
          _select(SPACE_VINDEX, 0, [], 0, 2**30, :all, false, fill_indexes)
        end
      end
    end
    fill_indexes = lambda do |r|
      unless r.ok?
        future.set r
        _synchronized do
          @future = nil
          @spaces = nil
        end
      else
        _synchronized do
          _fill_indices(spaces, r.data)
          future.set Option.ok(spaces)
        end
      end
    end
    _select(SPACE_VSPACE, 0, [], 0, 2**30, :all, false, fill_spaces)
    return future
  end
end

#_synchronizedObject



30
31
32
# File 'lib/tarantool16/db.rb', line 30

def _synchronized
  raise "Override #_synchronized"
end

#_update(sno, ino, key, ops, need_hash, cb) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/tarantool16/db.rb', line 179

def _update(sno, ino, key, ops, need_hash, cb)
  ino = 0 if ino.nil? && key.is_a?(Array)
  ops_good = ops.is_a?(Array) && ops.all?{|a| a[1].is_a?(Integer)}
  if sno.is_a?(Integer) && ino.is_a?(Integer) && key.is_a?(Array) && ops_good
    return conn._update(sno, ino, key, ops, cb)
  end
  _with_space(sno, cb) do |sp|
    sp.get_ino(ino, key, ITERATOR_EQ, cb) do |_ino, _key|
      _ops = ops_good ? ops : sp.map_ops(ops)
      _cb = need_hash ? sp.wrap_cb(cb) : cb
      conn._update(sp.sid, _ino, _key, _ops, _cb)
    end
  end
end

#_upsert(sno, ino, tuple_key, ops, need_hash, cb) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/tarantool16/db.rb', line 194

def _upsert(sno, ino, tuple_key, ops, need_hash, cb)
  ino = 0 if ino.nil?
  ops_good = ops.is_a?(Array) && ops.all?{|a| a[1].is_a?(Integer)}
  if sno.is_a?(Integer) && ino.is_a?(Integer) && tuple_key.is_a?(Array) && ops_good
    return conn._upsert(sno, ino, tuple_key, ops, cb)
  end
  _with_space(sno, cb) do |sp|
    _tuple = tuple_key.is_a?(Hash) ? sp.map_tuple(tuple_key) : tuple_key
    sp.get_ino(ino, nil, ITERATOR_EQ, cb) do |_ino, _key|
      _ops = ops_good ? ops : sp.map_ops(ops)
      _cb = need_hash ? sp.wrap_cb(cb) : cb
      conn._upsert(sp.sid, _ino, tuple_key, _ops, _cb)
    end
  end
end

#_with_space(name, cb) ⇒ Object



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

def _with_space(name, cb)
  future = @future || _space_future
  future.then_blk do |r|
    unless r.ok?
      cb.call r
    else
      sps = r.data
      sp = sps[name]
      if sp.nil? && Symbol == name
        sp = sps[name.to_s]
        sps[name] = sp unless sp.nil?
      end
      if sp.nil?
        cb.call Option.error(SchemaError, "space #{name} not found")
      else
        yield sp
      end
    end
  end
end

#define_fields(sid, fields) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tarantool16/db.rb', line 15

def define_fields(sid, fields)
  sid = sid.to_s if sid.is_a?(Symbol)
  @defined_fields[sid] = fields
  if @spaces && (sp = @spaces[sid])
    if sp.sid && sp.name && !sp.name.empty?
      rf1 = @defined_fields[sp.sid]
      rf2 = @defined_fields[sp.name]
      if rf1 && rf2 && rf1 != rf2
        raise "Misconfigured defined fields for #{sp.name_sid}"
      end
    end
    sp.fields = fields
  end
end