Module: Tarantool::BaseRecord::ClassMethods

Included in:
Tarantool::BaseRecord
Defined in:
lib/tarantool/base_record.rb

Instance Method Summary collapse

Instance Method Details

#_tail(*types) ⇒ Object

Raises:



78
79
80
81
82
83
84
85
# File 'lib/tarantool/base_record.rb', line 78

def _tail(*types)
  types = types.map{|type| Serializers.check_type(type)}

  raise ArgumentError, "double _tail declaration"  if fields.include?(:_tail)
  self.fields = fields.merge(:_tail => types)

  define_field_accessor(:_tail, types)
end

#all(cond, opts = {}) ⇒ Object



184
185
186
187
188
# File 'lib/tarantool/base_record.rb', line 184

def all(cond, opts={})
  res = space.all(cond, opts)
  res.map!{|hash| from_fetched(hash)}
  res
end

#auto_spaceObject

space that will return records as results for calls it is useful, if you wish to use callback interface



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

def auto_space
  @auto_space ||= begin
      space.with_translator(method(:from_fetched))
    end
end

#by_pk(pk) ⇒ Object



156
157
158
159
160
# File 'lib/tarantool/base_record.rb', line 156

def by_pk(pk)
  if Hash === (res = space.by_pk(pk))
    from_fetched(res)
  end
end

#by_pks(pks) ⇒ Object



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

def by_pks(pks)
  space.all_by_pks(pks).map{|hash| from_fetched(hash)}
end

#call(proc_name, *args) ⇒ Object

Call stored procedure. By default, it prepends space_no to arguments. To avoid prepending, set space_no: nil in options.

MyRecord.call('box.select_range', offset, limit)
MyRecord.call('myfunction', arg1, arg2, space_no: nil)

You could recieve arbitarry arrays or hashes instead of instances of record, if you pass :returns argument. See documentation for SpaceHash for this.



220
221
222
223
224
225
226
227
228
# File 'lib/tarantool/base_record.rb', line 220

def call(proc_name, *args)
  opts = Hash === args.last ? args.pop : {}
  res = space.call(proc_name, args, opts)
  if Array === res && !opts[:returns]
    res.map{|hash| from_fetched(hash) }
  else
    res
  end
end

#create(attributes = {}) ⇒ Object



194
195
196
197
198
# File 'lib/tarantool/base_record.rb', line 194

def create(attributes = {})
  r = new(attributes)
  r.save
  r
end

#delete(pk, ret_tuple = false) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/tarantool/base_record.rb', line 269

def delete(pk, ret_tuple=false)
  if ret_tuple
    from_fetched space.delete(pk, return_tuple: true)
  else
    space.delete(pk)
  end
end

#field(name, type, params = {}) ⇒ Object

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tarantool/base_record.rb', line 64

def field(name, type, params = {})
  type = Serializers.check_type(type)

  raise ArgumentError, "_tail should be last declaration"  if fields.include?(:_tail)
  self.fields = fields.merge(name => type)
  index name  if indexes.empty?

  if params[:default]
    self.default_values = default_values.merge name => params[:default]
  end

  define_field_accessor(name, type)
end

#find(*args) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/tarantool/base_record.rb', line 166

def find(*args)
  if args.size == 1
    by_pk(args[0])
  else
    by_pks(args)
  end
end

#first(cond) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/tarantool/base_record.rb', line 174

def first(cond)
  if Hash === cond
    if Hash === (res = space.first(cond))
      from_fetched(res)
    end
  else
    by_pk(cond)
  end
end

#from_fetched(hash) ⇒ Object



230
231
232
# File 'lib/tarantool/base_record.rb', line 230

def from_fetched(hash)
  hash && allocate.__fetched(hash)
end

#index(*fields) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/tarantool/base_record.rb', line 87

def index(*fields)
  options = Hash === fields.last ? fields.pop : {}
  if options[:primary]
    self.indexes = indexes.dup.tap{|ind| ind[0] = fields}
  else
    self.indexes += [fields]
  end
end

#insert(hash, ret_tuple = false) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/tarantool/base_record.rb', line 234

def insert(hash, ret_tuple = false)
  hash = default_values.merge(hash)
  if ret_tuple
    from_fetched space.insert(hash, return_tuple: true)
  else
    space.insert(hash)
  end
end

#invoke(proc_name, *args) ⇒ Object

Call stored procedure without returning tuples. By default, it prepends space_no to arguments. To avoid prepending, set space_no: nil in options.

MyRecord.call('box.select_range', offset, limit)
MyRecord.call('myfunction', arg1, arg2, space_no: nil)


206
207
208
209
# File 'lib/tarantool/base_record.rb', line 206

def invoke(proc_name, *args)
  opts = Hash === args.last ? args.pop : {}
  space.invoke(proc_name, args, opts)
end

#primary_indexObject



113
114
115
# File 'lib/tarantool/base_record.rb', line 113

def primary_index
  indexes[0]
end

#replace(hash, ret_tuple = false) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/tarantool/base_record.rb', line 243

def replace(hash, ret_tuple = false)
  hash = default_values.merge(hash)
  if ret_tuple
    from_fetched space.replace(hash, return_tuple: true)
  else
    space.replace(hash)
  end
end

#reset_space!Object



151
152
153
154
# File 'lib/tarantool/base_record.rb', line 151

def reset_space!
  @space = nil
  @auto_space = nil
end

#select(cond = nil, opts = {}) ⇒ Object



190
191
192
# File 'lib/tarantool/base_record.rb', line 190

def select(cond=nil, opts={})
  cond.nil? ? Select.new(self) : all(cond, opts)
end

#shard_fields(*args) ⇒ Object Also known as: set_shard_fields



104
105
106
107
108
109
110
# File 'lib/tarantool/base_record.rb', line 104

def shard_fields(*args)
  if args.empty?
    _shard_fields
  else
    self._shard_fields = args
  end
end

#shard_proc(cb = nil, &block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/tarantool/base_record.rb', line 96

def shard_proc(cb = nil, &block)
  if cb ||= block
    self._shard_proc = cb
  else
    _shard_proc
  end
end

#spaceObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tarantool/base_record.rb', line 117

def space
  @space ||= begin
      shard_fields = _shard_fields || primary_index
      shard_proc = _shard_proc ||
        if shard_fields.size == 1
          case fields[shard_fields[0]]
          when :int, :int16, :int8
            :sumbur_murmur_fmix
          when :int64
            :sumbur_murmur_int64
          when :string
            :sumbur_murmur_str
          else
            :default
          end
        else
          :default
        end
      _tarantool.space_hash(_space_no, fields.dup,
                           keys: indexes,
                           shard_fields: shard_fields,
                           shard_proc: shard_proc
                          )
    end
end

#space_no(v = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/tarantool/base_record.rb', line 50

def space_no(v=nil)
  unless v
    _space_no
  else
    self.space_no = v
  end
end

#space_no=(v) ⇒ Object Also known as: set_space_no



58
59
60
61
# File 'lib/tarantool/base_record.rb', line 58

def space_no=(v)
  reset_space!
  self._space_no = v
end

#store(hash, ret_tuple = false) ⇒ Object



252
253
254
255
256
257
258
259
# File 'lib/tarantool/base_record.rb', line 252

def store(hash, ret_tuple = false)
  hash = default_values.merge(hash)
  if ret_tuple
    from_fetched space.store(hash, return_tuple: true)
  else
    space.store(hash)
  end
end

#tarantool(v = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/tarantool/base_record.rb', line 33

def tarantool(v=nil)
  unless v
    _tarantool
  else
    self.tarantool = v
  end
end

#tarantool=(v) ⇒ Object Also known as: set_tarantool



41
42
43
44
45
46
47
# File 'lib/tarantool/base_record.rb', line 41

def tarantool=(v)
  reset_space!
  unless ::Tarantool::DB === v && v.primary_interface == :synchronous
    raise ArgumentError, "you may assing to record's tarantool only instances of Tarantool::BlockDB or Tarantool::FiberDB"
  end
  self._tarantool= v
end

#update(pk, ops, ret_tuple = false) ⇒ Object



261
262
263
264
265
266
267
# File 'lib/tarantool/base_record.rb', line 261

def update(pk, ops, ret_tuple=false)
  if ret_tuple
    from_fetched space.update(pk, ops, return_tuple: true)
  else
    space.update(pk, ops)
  end
end