Class: Groonga::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/record.rb

Defined Under Namespace

Classes: AttributeHashBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, id, values = nil) ⇒ Record

tableid に対応するレコードを作成する。 values には各カラムに設定する値を以下のような形式で指定する。

<pre> !!!ruby [

["カラム名", 値],
["カラム名", 値],
...,

] </pre>

Each value is set by #[]=. See #[]= how to set weight for a value.



39
40
41
42
43
44
45
46
47
48
# File 'lib/groonga/record.rb', line 39

def initialize(table, id, values=nil)
  @table = table
  @id = id
  @added = false
  if values
    values.each do |name, value|
      self[name] = value
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/groonga/record.rb', line 380

def method_missing(name, *args, &block)
  if /=\z/ =~ name.to_s
    base_name = $PREMATCH
    is_setter = true
  else
    base_name = name.to_s
    is_setter = false
  end
  _column = @table.column(base_name)
  if _column
    if is_setter
      _column.send("[]=", @id, *args, &block)
    else
      _column.send("[]", @id, *args, &block)
    end
  else
    super
  end
end

Instance Attribute Details

#tableObject (readonly)

レコードが所属するテーブル



24
25
26
# File 'lib/groonga/record.rb', line 24

def table
  @table
end

Instance Method Details

#==(other) ⇒ Object

recordother が同じテーブルに属していて、さらに、同じレコードIDを持つなら true を返し、そうでなければfalse を返す。



53
54
55
56
# File 'lib/groonga/record.rb', line 53

def ==(other)
  self.class == other.class and
    [table, id] == [other.table, other.id]
end

#[](column_name) ⇒ Object

このレコードの column_name で指定されたカラムの値を返す。



70
71
72
# File 'lib/groonga/record.rb', line 70

def [](column_name)
  @table.column_value(@id, column_name, :id => true)
end

#[]=(column_name, value) ⇒ Object #[]=(column_name, value_with_weight) ⇒ Object

Sets column value of the record.

Overloads:

  • #[]=(column_name, value) ⇒ Object

    Examples:

    Set a new value

    user["age"] = 29

    Parameters:

    • column_name (String)

      The column name.

    • value (Object)

      The column value. Weight of the value is 0.

  • #[]=(column_name, value_with_weight) ⇒ Object

    Examples:

    Set a new value with weight “2”

    user["tags"] = [{:value => "groonga", :weight => 2}]

    Parameters:

    • column_name (String)

      The column name.

    • value_with_weight (::Hash)

      The column value with weight.

    Options Hash (value_with_weight):

    • :value (Object) — default: nil

      The column value.

    • :weight (Integer or nil) — default: nil

      The weight for the value. You need to use vector column and weight supported index column for weight. See Groonga;:Table#set_column_value for details.

See Also:

  • Table#set_column_value


96
97
98
# File 'lib/groonga/record.rb', line 96

def []=(column_name, value)
  @table.set_column_value(@id, column_name, value, :id => true)
end

#added=(added) ⇒ Object



336
337
338
# File 'lib/groonga/record.rb', line 336

def added=(added)
  @added = added
end

#added?Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/groonga/record.rb', line 331

def added?
  @added
end

#append(column_name, value) ⇒ Object

このレコードの column_name で指定されたカラムの値の最後にvalue を追加する。



102
103
104
# File 'lib/groonga/record.rb', line 102

def append(column_name, value)
  column(column_name).append(@id, value)
end

#as_jsonObject



257
258
259
260
261
262
263
264
265
266
# File 'lib/groonga/record.rb', line 257

def as_json
  accessor = AttributeHashBuilder.new(self) do |value|
    if value.is_a?(Time)
      value.iso8601
    else
      value
    end
  end
  accessor.build
end

#attributesObject

レコードが所属しているテーブルで定義されているインデックス型のカラムでない全カラムを対象とし、カラムの名前をキーとしたこのレコードのカラムの値のハッシュを返す。

return same attributes object if duplicate records exist.



252
253
254
255
# File 'lib/groonga/record.rb', line 252

def attributes
  accessor = AttributeHashBuilder.new(self)
  accessor.build
end

#clear_lock(options = {}) ⇒ Object

レコードが所属するテーブルのロックを強制的に解除する。

利用可能なオプションは現在は無い。



303
304
305
# File 'lib/groonga/record.rb', line 303

def clear_lock(options={})
  @table.clear_lock(options.merge(:id => @id))
end

#columnsObject

レコードが所属するテーブルの全てのカラムを返す。



243
244
245
# File 'lib/groonga/record.rb', line 243

def columns
  @table.columns
end

#decrement!(name, delta = nil) ⇒ Object

このレコードの name で指定されたカラムの値を delta だけ減少する。 deltanil の場合は1減少する。



238
239
240
# File 'lib/groonga/record.rb', line 238

def decrement!(name, delta=nil)
  column(name).decrement!(@id, delta)
end

#deleteObject

レコードを削除する。



274
275
276
# File 'lib/groonga/record.rb', line 274

def delete
  @table.delete(@id)
end

#eql?(other) ⇒ Boolean

#== と同じ。

Returns:

  • (Boolean)


59
60
61
# File 'lib/groonga/record.rb', line 59

def eql?(other)
  self == other
end

#hashObject

同じテーブルの同じIDのレコードに対しては常に同じハッシュ値を返す。



65
66
67
# File 'lib/groonga/record.rb', line 65

def hash
  @table.hash ^ @id.hash
end

#have_column?(name) ⇒ Boolean

名前が name のカラムがレコードの所属するテーブルで定義されているなら true を返す。

Returns:

  • (Boolean)


120
121
122
# File 'lib/groonga/record.rb', line 120

def have_column?(name)
  not @table.column(normalize_column_name(name)).nil?
end

#increment!(name, delta = nil) ⇒ Object

このレコードの name で指定されたカラムの値を delta だけ増加する。 deltanil の場合は1増加する。



232
233
234
# File 'lib/groonga/record.rb', line 232

def increment!(name, delta=nil)
  column(name).increment!(@id, delta)
end

#index_column?(name) ⇒ Boolean

名前が name のカラムが索引カラム( IndexColumn )であるなら true を返す。

Returns:

  • (Boolean)


131
132
133
# File 'lib/groonga/record.rb', line 131

def index_column?(name)
  column(name).index?
end

#keyObject

レコードの主キーを返す。

record が所属するテーブルが Array の場合は常に nil を返す。



160
161
162
163
164
165
166
# File 'lib/groonga/record.rb', line 160

def key
  if support_key?
    @key ||= @table.key(@id)
  else
    nil
  end
end

#lock(options = {}, &block) ⇒ Object

レコードが所属するテーブルをロックする。ロックに失敗した場合は Groonga::ResourceDeadlockAvoided 例外が発生する。

ブロックを指定した場合はブロックを抜けたときにunlockする。

利用可能な option は以下の通り。

Parameters:

  • options (Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :timeout (Integer)

    The timeout ロックを獲得できなかった場合は :timeout 秒間ロックの獲得を試みる。:timeout 秒以内にロックを獲得できなかった場合は例外が発生する。



289
290
291
# File 'lib/groonga/record.rb', line 289

def lock(options={}, &block)
  @table.lock(options.merge(:id => @id), &block)
end

#locked?(options = {}) ⇒ Boolean

レコードが所属するテーブルがロックされていれば true を返す。

利用可能なオプションは現在は無い。

Returns:

  • (Boolean)


310
311
312
# File 'lib/groonga/record.rb', line 310

def locked?(options={})
  @table.locked?(options.merge(:id => @id))
end

#methods(include_inherited = true) ⇒ Object



320
321
322
323
324
# File 'lib/groonga/record.rb', line 320

def methods(include_inherited=true)
  original_methods = super
  return original_methods unless include_inherited
  (original_methods + dynamic_methods).uniq
end

#n_sub_recordsObject

主キーの値が同一であったレコードの件数を返す。検索結果として生成されたテーブルのみに定義される。

#support_sub_records? でこの値を利用できるかがわかる。



210
211
212
# File 'lib/groonga/record.rb', line 210

def n_sub_records
  self["_nsubrecs"]
end

#prepend(column_name, value) ⇒ Object

このレコードの column_name で指定されたカラムの値の最初にvalue を追加する。



108
109
110
# File 'lib/groonga/record.rb', line 108

def prepend(column_name, value)
  column(column_name).prepend(@id, value)
end

#record_idObject

レコードを一意に識別するための情報を返す。

record が所属するテーブルが Array の場合はID を返し、それ以外の場合は主キーを返す。



172
173
174
175
176
177
178
# File 'lib/groonga/record.rb', line 172

def record_id
  if support_key?
    key
  else
    id
  end
end

#record_raw_idObject Also known as: id

レコードのIDを返す。



181
182
183
# File 'lib/groonga/record.rb', line 181

def record_raw_id
  @id
end

#reference_column?(name) ⇒ Boolean

名前が name のカラムが参照カラムであるなら true を返す。

Returns:

  • (Boolean)


125
126
127
# File 'lib/groonga/record.rb', line 125

def reference_column?(name)
  column(name).reference?
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
# File 'lib/groonga/record.rb', line 327

def respond_to?(name)
  super or !@table.column(name.to_s.sub(/=\z/, '')).nil?
end

#scalar_column?(name) ⇒ Boolean

名前が name のカラムの値がスカラーであるなら true を返す。

Returns:

  • (Boolean)

Since:

  • 1.0.5



145
146
147
# File 'lib/groonga/record.rb', line 145

def scalar_column?(name)
  column(name).scalar?
end

#scoreObject

レコードのスコア値を返す。検索結果として生成されたテーブルのみに定義される。



188
189
190
# File 'lib/groonga/record.rb', line 188

def score
  self["_score"]
end

#score=(new_score) ⇒ Object

Sets score. Score column exists only search result table.

Parameters:

  • new_score (Integer)

    The new score.



195
196
197
# File 'lib/groonga/record.rb', line 195

def score=(new_score)
  self["_score"] = new_score
end

#search(name, query, options = {}) ⇒ Object

名前が nameIndexColumn の search メソッドを呼ぶ。queryoptions はそのメソッドにそのまま渡される。詳しくは IndexColumn#search を参照。



152
153
154
# File 'lib/groonga/record.rb', line 152

def search(name, query, options={})
  column(name).search(query, options)
end

#support_key?Boolean

record が所属するテーブルで主キーを使える場合は true を返し、使えない場合は false を返す。

Returns:

  • (Boolean)


114
115
116
# File 'lib/groonga/record.rb', line 114

def support_key?
  @table.support_key?
end

#support_score?Boolean

#score が利用できる場合は true を返す。

Returns:

  • (Boolean)


201
202
203
# File 'lib/groonga/record.rb', line 201

def support_score?
  @table.have_column?("_score") # TODO delegate to Table
end

#support_sub_records?Boolean

#n_sub_records が利用できる場合は true を返す。

Returns:

  • (Boolean)


216
217
218
# File 'lib/groonga/record.rb', line 216

def support_sub_records?
  @table.support_sub_records?
end

#to_json(*args) ⇒ String

Returns the record formatted as JSON.

Returns:

  • (String)

    the record formatted as JSON.



269
270
271
# File 'lib/groonga/record.rb', line 269

def to_json(*args)
  as_json.to_json(*args)
end

#unlock(options = {}) ⇒ Object

レコードが所属するテーブルのロックを解除する。

利用可能なオプションは現在は無い。



296
297
298
# File 'lib/groonga/record.rb', line 296

def unlock(options={})
  @table.unlock(options.merge(:id => @id))
end

#valid_id?Boolean

レコードが持つIDが有効なIDであれば true を返す。

Returns:

  • (Boolean)


315
316
317
# File 'lib/groonga/record.rb', line 315

def valid_id?
  @table.exist?(@id)
end

#valueObject

レコードの値を返す。



221
222
223
# File 'lib/groonga/record.rb', line 221

def value
  @table.value(@id, :id => true)
end

#value=(value) ⇒ Object

レコードの値を設定する。既存の値は上書きされる。



226
227
228
# File 'lib/groonga/record.rb', line 226

def value=(value)
  @table.set_value(@id, value, :id => true)
end

#vector_column?(name) ⇒ Boolean

名前が name のカラムの値がベクターであるなら true を返す。

Returns:

  • (Boolean)

Since:

  • 1.0.5



138
139
140
# File 'lib/groonga/record.rb', line 138

def vector_column?(name)
  column(name).vector?
end