Class: Tarantool16::SchemaSpace

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

Defined Under Namespace

Classes: CallbackWrapper, Field, Index

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sid, name, fields) ⇒ SchemaSpace

Returns a new instance of SchemaSpace.



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

def initialize(sid, name, fields)
  @sid = sid
  @name = name
  @has_tail = false
  self.fields = fields
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



5
6
7
# File 'lib/tarantool16/schema.rb', line 5

def fields
  @fields
end

#indicesObject

Returns the value of attribute indices.



5
6
7
# File 'lib/tarantool16/schema.rb', line 5

def indices
  @indices
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/tarantool16/schema.rb', line 5

def name
  @name
end

#sidObject (readonly)

Returns the value of attribute sid.



5
6
7
# File 'lib/tarantool16/schema.rb', line 5

def sid
  @sid
end

Instance Method Details

#dataObject



15
# File 'lib/tarantool16/schema.rb', line 15

def data; self; end

#get_ino(ino, key, _iter, cb) ⇒ Object



80
81
82
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/tarantool16/schema.rb', line 80

def get_ino(ino, key, _iter, cb)
  if ino.nil?
    unless key.is_a?(Hash)
      opt = Option.error(SchemaError, "Could not detect index without field names and iterator: #{key.inspect} in #{name_sid}")
      return cb.call(opt)
    end
    # key should be Hash here
    keys = key.keys
    keys << _iter
    _ino = @_fields_2_ino[keys]
    if _ino
      ind = @indices[_ino]
      return yield(_ino, ind.map_key(key))
    elsif _ino == false
      opt = Option.error(SchemaError, "Could not detect index for fields #{key.keys} in #{name_sid}")
      return cb.call(opt)
    end
    keys.pop
    iter = _iter.is_a?(Integer) ? _iter : ::Tarantool16.iter(iter)

    fields = keys.map{|fld|
      case fld
      when Integer
        fld
      when Symbol, String
        @field_names[fld].pos
      else
        return cb.call(Option.error(SchemaError, "Unknown field #{fld.inspect} in query key #{key.inspect}"))
      end
    }

    index = nil
    for ind in @indices
      next unless ind
      first_fields = ind.parts[0,fields.size]
      if ind.can_iterator?(iter, fields.size)
        if fields == first_fields
          index = ind
          break
        elsif (fields - first_fields).empty?
          index = ind
        end
      end
    end
    keys << _iter
    if index
      @_fields_2_ino[keys.freeze] = index.pos
      yield index.pos, index.map_key(key)
    else
      @_fields_2_ino[keys.freeze] = false
      cb.call(Option.error(SchemaError, "Could not detect index for fields #{key.keys} in #{name_sid}"))
    end
  elsif index = @index_names[ino]
    yield index.pos, index.map_key(key)
  else
    cb.call(Option.error(SchemaError, "Could not find index #{ino} for fields #{Hash===key ? key.keys : key.inspect} in #{name_sid}"))
  end
end

#indices?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/tarantool16/schema.rb', line 76

def indices?
  @indices && !@indices.empty?
end

#map_ops(ops) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/tarantool16/schema.rb', line 190

def map_ops(ops)
  ops.map do |op|
    case op1 = op[1]
    when Integer
      op
    when Symbol, String
      _op = op.dup
      _op[1] = @field_names[op1].pos
      _op
    when Array
      fld_pos = case op[0]
            when Integer
              op[0]
            when Symbol, String
              @field_names[op[0]].pos
            else
              raise "No field #{op[0].inspect} in #{name_sid}"
            end
      op1.dup.insert(1, fld_pos)
    end
  end
end

#map_tuple(tuple) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/tarantool16/schema.rb', line 164

def map_tuple(tuple)
  row = []
  unless @has_tail
    tuple.each_key do |k|
      field = @field_names[k]
      row[field.pos] = tuple[k]
    end
  else
    tail = @fields.last
    tuple.each do |k|
      field = @field_names[k]
      val = tuple[k]
      if field.equal? tail
        unless tail.type.is_a?(Array)
          row[field.pos,0] = val
        else
          row[field.pos,0] = val.flatten(1)
        end
      else
        row[field.pos] = tuple[k]
      end
    end
  end
  row
end

#name_sidObject



160
161
162
# File 'lib/tarantool16/schema.rb', line 160

def name_sid
  @_np ||= "space #{@name}:#{@sid}"
end

#ok?Boolean

imitate Option

Returns:

  • (Boolean)


14
# File 'lib/tarantool16/schema.rb', line 14

def ok?; true; end

#tuple2hash(ar) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tarantool16/schema.rb', line 139

def tuple2hash(ar)
  raise "No fields defined for #{name_sid}" unless @fields && !@fields.empty?
  res = {}
  i = 0
  flds = @fields
  s = flds.size - (@has_tail ? 1 : 0)
  while i < s
    res[flds[i].name] = ar[i]
    i += 1
  end
  if @has_tail
    tail = flds[s]
    unless tail.type.is_a?(Array)
      res[tail.name] = ar[s..-1]
    else
      res[tail.name] = ar[s..-1].each_slice(tail.type.size).to_a
    end
  end
  res
end

#wrap_cb(cb) ⇒ Object



213
214
215
# File 'lib/tarantool16/schema.rb', line 213

def wrap_cb(cb)
  CallbackWrapper.new(self, cb)
end