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
14
# File 'lib/tarantool16/db.rb', line 6

def initialize(host, opts = {})
  @host = host
  @opts = opts.dup
  @future = nil
  @spaces = nil
  @defined_fields = {}
  _fill_standard_spaces
  @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



190
191
192
# File 'lib/tarantool16/db.rb', line 190

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

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



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

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

#_fill_indices(spaces, rows) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/tarantool16/db.rb', line 115

def _fill_indices(spaces, rows)
  rows.
    map{|row| [row[0], [row[2], row[1], row[3], 6.step(row.size-1, 2).map{|i| row[i]}]]}.
    group_by{|sid, _| sid}.
    each do |sid, inds|
      sp = spaces[sid]
      sp.indices = inds.map{|_sid, ind| ind}
    end 
end

#_fill_spaces(rows) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/tarantool16/db.rb', line 104

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

#_fill_standard_spacesObject



31
32
33
34
35
36
37
# File 'lib/tarantool16/db.rb', line 31

def _fill_standard_spaces
  rf = @defined_fields
  rf[SPACE_INDEX] =
    [%w{sid num}, %w{iid num}, %w{name str},
     %w{type str}, %w{unique num}, %w{part_count num},
     {name: 'parts', type: [:num, :str], tail: true}]
end

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



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

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



194
195
196
# File 'lib/tarantool16/db.rb', line 194

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

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



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

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.insert(sp.sid, _tuple, _cb)
  end
end

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



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

def _select(sno, ino, key, offset, limit, iterator, need_hash, cb)
  key = [] if key.nil?
  ino = 0 if ino.nil? && key.is_a?(Array)
  iterator = ::Tarantool16.iter(iterator) unless iterator.is_a?(Integer)
  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



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
94
95
96
97
98
99
100
101
102
# File 'lib/tarantool16/db.rb', line 65

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_INDEX, 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_SPACE, 0, [], 0, 2**30, :all, false, fill_spaces)
    return future
  end
end

#_synchronizedObject



39
40
41
# File 'lib/tarantool16/db.rb', line 39

def _synchronized
  raise "Override #_synchronized"
end

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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/tarantool16/db.rb', line 175

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| ops[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

#_with_space(name, cb) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tarantool16/db.rb', line 44

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



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

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