groonga 0.0.7 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. data/NEWS.ja.rdoc +56 -0
  2. data/NEWS.rdoc +58 -0
  3. data/Rakefile +2 -3
  4. data/benchmark/read-write-many-small-items.rb +16 -32
  5. data/benchmark/write-many-small-items.rb +14 -28
  6. data/example/bookmark.rb +19 -17
  7. data/example/index-html.rb +11 -1
  8. data/example/search/config.ru +14 -9
  9. data/ext/rb-grn-array.c +6 -6
  10. data/ext/rb-grn-column.c +348 -18
  11. data/ext/rb-grn-context.c +8 -4
  12. data/ext/rb-grn-database.c +6 -7
  13. data/ext/rb-grn-exception.c +101 -5
  14. data/ext/rb-grn-expression.c +206 -23
  15. data/ext/rb-grn-fix-size-column.c +6 -39
  16. data/ext/rb-grn-hash.c +24 -24
  17. data/ext/rb-grn-index-column.c +74 -19
  18. data/ext/rb-grn-logger.c +48 -0
  19. data/ext/rb-grn-object.c +281 -67
  20. data/ext/rb-grn-operation.c +1 -1
  21. data/ext/rb-grn-patricia-trie-cursor.c +10 -1
  22. data/ext/rb-grn-patricia-trie.c +268 -7
  23. data/ext/rb-grn-query.c +52 -1
  24. data/ext/rb-grn-record.c +8 -2
  25. data/ext/rb-grn-snippet.c +63 -1
  26. data/ext/rb-grn-table-cursor-key-support.c +15 -1
  27. data/ext/rb-grn-table-cursor.c +57 -0
  28. data/ext/rb-grn-table-key-support.c +382 -46
  29. data/ext/rb-grn-table.c +729 -192
  30. data/ext/rb-grn-type.c +63 -12
  31. data/ext/rb-grn-utils.c +156 -158
  32. data/ext/rb-grn-variable.c +18 -0
  33. data/ext/rb-grn.h +85 -21
  34. data/ext/rb-groonga.c +13 -3
  35. data/extconf.rb +19 -4
  36. data/html/developer.html +1 -1
  37. data/html/header.html.erb +1 -1
  38. data/html/index.html +4 -4
  39. data/lib/groonga.rb +10 -0
  40. data/lib/groonga/expression-builder.rb +81 -42
  41. data/lib/groonga/patricia-trie.rb +13 -0
  42. data/lib/groonga/record.rb +158 -13
  43. data/lib/groonga/schema.rb +339 -33
  44. data/pkg-config.rb +6 -1
  45. data/test-unit/lib/test/unit.rb +23 -42
  46. data/test-unit/lib/test/unit/assertionfailederror.rb +11 -0
  47. data/test-unit/lib/test/unit/assertions.rb +87 -9
  48. data/test-unit/lib/test/unit/autorunner.rb +20 -11
  49. data/test-unit/lib/test/unit/collector.rb +1 -8
  50. data/test-unit/lib/test/unit/collector/load.rb +2 -3
  51. data/test-unit/lib/test/unit/color-scheme.rb +13 -1
  52. data/test-unit/lib/test/unit/diff.rb +223 -37
  53. data/test-unit/lib/test/unit/error.rb +4 -0
  54. data/test-unit/lib/test/unit/failure.rb +31 -5
  55. data/test-unit/lib/test/unit/notification.rb +8 -4
  56. data/test-unit/lib/test/unit/omission.rb +51 -3
  57. data/test-unit/lib/test/unit/pending.rb +4 -0
  58. data/test-unit/lib/test/unit/testcase.rb +55 -4
  59. data/test-unit/lib/test/unit/ui/console/testrunner.rb +190 -4
  60. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +14 -0
  61. data/test-unit/lib/test/unit/ui/testrunner.rb +8 -0
  62. data/test-unit/lib/test/unit/version.rb +1 -1
  63. data/test-unit/sample/{tc_adder.rb → test_adder.rb} +3 -1
  64. data/test-unit/sample/{tc_subtracter.rb → test_subtracter.rb} +3 -1
  65. data/test-unit/sample/test_user.rb +1 -0
  66. data/test-unit/test/collector/test-descendant.rb +2 -4
  67. data/test-unit/test/collector/test_objectspace.rb +7 -5
  68. data/test-unit/test/run-test.rb +2 -0
  69. data/test-unit/test/test-color-scheme.rb +7 -0
  70. data/test-unit/test/test-diff.rb +48 -7
  71. data/test-unit/test/test-omission.rb +1 -1
  72. data/test-unit/test/test-testcase.rb +47 -0
  73. data/test-unit/test/test_assertions.rb +79 -10
  74. data/test/groonga-test-utils.rb +6 -1
  75. data/test/test-array.rb +29 -14
  76. data/test/test-column.rb +107 -55
  77. data/test/test-context.rb +5 -0
  78. data/test/test-database.rb +2 -37
  79. data/test/test-exception.rb +9 -1
  80. data/test/test-expression-builder.rb +23 -5
  81. data/test/test-expression.rb +44 -8
  82. data/test/test-fix-size-column.rb +16 -5
  83. data/test/test-gqtp.rb +70 -0
  84. data/test/test-hash.rb +142 -43
  85. data/test/test-index-column.rb +9 -9
  86. data/test/test-patricia-trie.rb +79 -20
  87. data/test/test-procedure.rb +4 -2
  88. data/test/test-record.rb +32 -20
  89. data/test/test-remote.rb +3 -2
  90. data/test/test-schema.rb +226 -92
  91. data/test/test-table-cursor.rb +103 -1
  92. data/test/test-table-offset-and-limit.rb +102 -0
  93. data/test/test-table-select-normalize.rb +4 -4
  94. data/test/test-table-select.rb +52 -8
  95. data/test/test-table.rb +235 -116
  96. data/test/test-type.rb +2 -2
  97. data/test/test-variable-size-column.rb +21 -5
  98. data/test/test-vector-column.rb +76 -0
  99. data/{TUTORIAL.ja.rdoc → text/TUTORIAL.ja.rdoc} +52 -52
  100. data/text/expression.rdoc +284 -0
  101. metadata +11 -7
  102. data/test-unit/sample/ts_examples.rb +0 -7
@@ -27,17 +27,17 @@ module Groonga
27
27
  # のようなスキーマを定義する場合は以下のようになる。
28
28
  #
29
29
  # Groonga::Schema.define do |schema|
30
- # schema.create_table("items") do |table|
30
+ # schema.create_table("Items") do |table|
31
31
  # table.short_text("title")
32
32
  # end
33
33
  #
34
- # schema.create_table("users") do |table|
34
+ # schema.create_table("Users") do |table|
35
35
  # table.short_text("name")
36
36
  # end
37
37
  #
38
38
  # schema.create_table("comments") do |table|
39
- # table.reference("item", "items")
40
- # table.reference("author", "users")
39
+ # table.reference("item", "Items")
40
+ # table.reference("author", "Users")
41
41
  # table.text("content")
42
42
  # table.time("issued")
43
43
  # end
@@ -50,7 +50,11 @@ module Groonga
50
50
  #
51
51
  # スキーマを定義する。ブロックにはGroonga::Schemaオブ
52
52
  # ジェクトがわたるので、そのオブジェクトを利用してスキー
53
- # マを定義する。
53
+ # マを定義する。以下の省略形。
54
+ #
55
+ # schema = Groonga::Scheme.new(options)
56
+ # ...
57
+ # schema.define
54
58
  #
55
59
  # _options_に指定可能な値は以下の通り。
56
60
  #
@@ -80,6 +84,15 @@ module Groonga
80
84
  #
81
85
  # _options_に指定可能な値は以下の通り。
82
86
  #
87
+ # [+:force+]
88
+ # +true+を指定すると既存の同名のテーブルが存在してい
89
+ # ても、強制的にテーブルを作成する。
90
+ #
91
+ # [+:type+]
92
+ # テーブルの型を指定する。+:array+, +:hash+,
93
+ # +:patricia_trie+のいずれかを指定する。デフォルトで
94
+ # は+:array+になる。
95
+ #
83
96
  # [+:context+]
84
97
  # スキーマ定義時に使用するGroonga::Contextを指定する。
85
98
  # 省略した場合はGroonga::Context.defaultを使用する。
@@ -96,6 +109,49 @@ module Groonga
96
109
  # [+:value_type+]
97
110
  # 値の型を指定する。省略すると値のための領域を確保しない。
98
111
  # 値を保存したい場合は必ず指定すること。
112
+ #
113
+ # [+:sub_records+]
114
+ # +true+を指定するとGroonga::Table#groupでグループ化
115
+ # したときに、Groonga::Record#n_sub_recordsでグルー
116
+ # プに含まれるレコードの件数を取得できる。
117
+ #
118
+ # 以下は+:type+に+:hash+あるいは+:patricia_trie+を指定
119
+ # した時に指定可能。
120
+ #
121
+ # [+:key_type+]
122
+ # キーの種類を示すオブジェクトを指定する。キーの種類
123
+ # には型名("Int32"や"ShortText"など)または
124
+ # Groonga::Typeまたはテーブル(Groonga::Array、
125
+ # Groonga::Hash、Groonga::PatriciaTrieのどれか)を指
126
+ # 定する。
127
+ #
128
+ # Groonga::Typeを指定した場合は、その型が示す範囲の
129
+ # 値をキーとして使用する。ただし、キーの最大サイズは
130
+ # 4096バイトであるため、Groonga::Type::TEXTや
131
+ # Groonga::Type::LONG_TEXTは使用できない。
132
+ #
133
+ # テーブルを指定した場合はレコードIDをキーとして使用
134
+ # する。指定したテーブルのGroonga::Recordをキーとし
135
+ # て使用することもでき、その場合は自動的に
136
+ # Groonga::RecordからレコードIDを取得する。
137
+ #
138
+ # 省略した場合は文字列をキーとして使用する。この場合、
139
+ # 4096バイトまで使用可能である。
140
+ #
141
+ # [+:default_tokenizer+]
142
+ # Groonga::IndexColumnで使用するトークナイザを指定す
143
+ # る。デフォルトでは何も設定されていないので、テーブ
144
+ # ルにGroonga::IndexColumnを定義する場合は
145
+ # <tt>"TokenBigram"</tt>などを指定する必要がある。
146
+ #
147
+ # 以下は+:type+に+:patricia_trie+を指定した時に指定可能。
148
+ #
149
+ # [+:key_normalize+]
150
+ # +true+を指定するとキーを正規化する。
151
+ #
152
+ # [+:key_with_sis+]
153
+ # +true+を指定するとキーの文字列の全suffixが自動的に
154
+ # 登録される。
99
155
  def create_table(name, options={}, &block)
100
156
  define do |schema|
101
157
  schema.create_table(name, options, &block)
@@ -103,6 +159,7 @@ module Groonga
103
159
  end
104
160
 
105
161
  # 名前が_name_のテーブルを削除する。
162
+ #
106
163
  # _options_に指定可能な値は以下の通り。
107
164
  #
108
165
  # [+:context+]
@@ -114,6 +171,26 @@ module Groonga
114
171
  end
115
172
  end
116
173
 
174
+ # call-seq:
175
+ # Groonga::Schema.change_table(name, options={}) {|table| ...}
176
+ #
177
+ # 名前が_name_のテーブルを変更する。以下の省略形。
178
+ #
179
+ # Groonga::Schema.define do |schema|
180
+ # schema.change_table(name, options) do |table|
181
+ # ...
182
+ # end
183
+ # end
184
+ #
185
+ # ブロックにはGroonga::Schema::TableDefinitionオブジェ
186
+ # クトがわたるので、そのオブジェクトを利用してテーブル
187
+ # の詳細を定義する。
188
+ #
189
+ # _options_に指定可能な値は以下の通り。
190
+ #
191
+ # [+:context+]
192
+ # スキーマ定義時に使用するGroonga::Contextを指定する。
193
+ # 省略した場合はGroonga::Context.defaultを使用する。
117
194
  def change_table(name, options={}, &block)
118
195
  define do |schema|
119
196
  schema.change_table(name, options, &block)
@@ -203,13 +280,25 @@ module Groonga
203
280
  instance_eval(dumped_text)
204
281
  end
205
282
 
283
+ # call-seq:
284
+ # schema.create_table(name, options={}) {|table| ...}
285
+ #
206
286
  # 名前が_name_のテーブルを作成する。
207
287
  #
208
- # 作成したテーブルは#defineを呼び出すまでは実行されない
209
- # ことに注意すること。
288
+ # テーブルの作成は#defineを呼び出すまでは実行されないこ
289
+ # とに注意すること。
210
290
  #
211
291
  # _options_に指定可能な値は以下の通り。
212
292
  #
293
+ # [+:force+]
294
+ # +true+を指定すると既存の同名のテーブルが存在してい
295
+ # ても、強制的にテーブルを作成する。
296
+ #
297
+ # [+:type+]
298
+ # テーブルの型を指定する。+:array+, +:hash+,
299
+ # +:patricia_trie+のいずれかを指定する。デフォルトで
300
+ # は+:array+になる。
301
+ #
213
302
  # [+:context+]
214
303
  # スキーマ定義時に使用するGroonga::Contextを指定する。
215
304
  # 省略した場合はGroonga::Schema.newで指定した
@@ -230,17 +319,83 @@ module Groonga
230
319
  # い。値を保存したい場合は必ず指定すること。
231
320
  #
232
321
  # 参考: Groonga::Type.new
322
+ #
323
+ # [+:sub_records+]
324
+ # +true+を指定するとGroonga::Table#groupでグループ化
325
+ # したときに、Groonga::Record#n_sub_recordsでグルー
326
+ # プに含まれるレコードの件数を取得できる。
327
+ #
328
+ # 以下は+:type+に+:hash+あるいは+:patricia_trie+を指定
329
+ # した時に指定可能。
330
+ #
331
+ # [+:key_type+]
332
+ # キーの種類を示すオブジェクトを指定する。キーの種類
333
+ # には型名("Int32"や"ShortText"など)または
334
+ # Groonga::Typeまたはテーブル(Groonga::Array、
335
+ # Groonga::Hash、Groonga::PatriciaTrieのどれか)を指
336
+ # 定する。
337
+ #
338
+ # Groonga::Typeを指定した場合は、その型が示す範囲の
339
+ # 値をキーとして使用する。ただし、キーの最大サイズは
340
+ # 4096バイトであるため、Groonga::Type::TEXTや
341
+ # Groonga::Type::LONG_TEXTは使用できない。
342
+ #
343
+ # テーブルを指定した場合はレコードIDをキーとして使用
344
+ # する。指定したテーブルのGroonga::Recordをキーとし
345
+ # て使用することもでき、その場合は自動的に
346
+ # Groonga::RecordからレコードIDを取得する。
347
+ #
348
+ # 省略した場合は文字列をキーとして使用する。この場合、
349
+ # 4096バイトまで使用可能である。
350
+ #
351
+ # [+:default_tokenizer+]
352
+ # Groonga::IndexColumnで使用するトークナイザを指定す
353
+ # る。デフォルトでは何も設定されていないので、テーブ
354
+ # ルにGroonga::IndexColumnを定義する場合は
355
+ # <tt>"TokenBigram"</tt>などを指定する必要がある。
356
+ #
357
+ # 以下は+:type+に+:patricia_trie+を指定した時に指定可能。
358
+ #
359
+ # [+:key_normalize+]
360
+ # +true+を指定するとキーを正規化する。
361
+ #
362
+ # [+:key_with_sis+]
363
+ # +true+を指定するとキーの文字列の全suffixが自動的に
364
+ # 登録される。
233
365
  def create_table(name, options={})
234
366
  definition = TableDefinition.new(name, @options.merge(options || {}))
235
367
  yield(definition)
236
368
  @definitions << definition
237
369
  end
238
370
 
371
+ # 名前が_name_のテーブルを削除する。
372
+ #
373
+ # テーブルの削除は#defineを呼び出すまでは実行されないこ
374
+ # とに注意すること。
375
+ #
376
+ # _options_に指定可能な値は以下の通り。
377
+ #
378
+ # [+:context+]
379
+ # スキーマ定義時に使用するGroonga::Contextを指定する。
380
+ # 省略した場合はGroonga::Context.defaultを使用する。
239
381
  def remove_table(name, options={})
240
382
  definition = TableRemoveDefinition.new(name, @options.merge(options || {}))
241
383
  @definitions << definition
242
384
  end
243
385
 
386
+ # call-seq:
387
+ # schema.change_table(name, options={}) {|table| ...}
388
+ #
389
+ # 名前が_name_のテーブルを変更する。
390
+ #
391
+ # テーブルの変更は#defineを呼び出すまでは実行されないこ
392
+ # とに注意すること。
393
+ #
394
+ # _options_に指定可能な値は以下の通り。
395
+ #
396
+ # [+:context+]
397
+ # スキーマ定義時に使用するGroonga::Contextを指定する。
398
+ # 省略した場合はGroonga::Context.defaultを使用する。
244
399
  def change_table(name, options={})
245
400
  options = @options.merge(options || {}).merge(:change => true)
246
401
  definition = TableDefinition.new(name, options)
@@ -248,10 +403,14 @@ module Groonga
248
403
  @definitions << definition
249
404
  end
250
405
 
406
+ # スキーマ定義時にGroonga::Schema.create_tableや
407
+ # Groonga::Schema#create_tableからブロックに渡されてくる
408
+ # オブジェクト
251
409
  class TableDefinition
410
+ # テーブルの名前
252
411
  attr_reader :name
253
412
 
254
- def initialize(name, options)
413
+ def initialize(name, options) # :nodoc:
255
414
  @name = name
256
415
  @name = @name.to_s if @name.is_a?(Symbol)
257
416
  @definitions = []
@@ -260,22 +419,56 @@ module Groonga
260
419
  @table_type = table_type
261
420
  end
262
421
 
263
- def define
422
+ def define # :nodoc:
423
+ table = context[@name]
264
424
  if @options[:change]
265
- table = context[@name]
425
+ raise ArgumentError, "table doesn't exist: #{@name}" if table.nil?
266
426
  else
267
- table = context[@name]
268
427
  if table and @options[:force]
269
428
  table.remove
429
+ table = nil
270
430
  end
271
- table = @table_type.create(create_options)
431
+ table ||= @table_type.create(create_options)
272
432
  end
273
433
  @definitions.each do |definition|
274
- definition.define(table)
434
+ definition.define(self, table)
275
435
  end
276
436
  table
277
437
  end
278
438
 
439
+ # 名前が_name_で型が_type_のカラムを作成する。
440
+ #
441
+ # _options_に指定可能な値は以下の通り。
442
+ #
443
+ # [+:force+]
444
+ # +true+を指定すると既存の同名のカラムが存在してい
445
+ # ても、強制的に新しいカラムを作成する。
446
+ #
447
+ # [+:path+]
448
+ # カラムを保存するパス。
449
+ #
450
+ # [+:persistent+]
451
+ # +true+を指定すると永続カラムとなる。+:path+を省略
452
+ # した場合は自動的にパスが付加される。
453
+ #
454
+ # [+:type+]
455
+ # カラムの値の格納方法について指定する。省略した場合は、
456
+ # +:scalar+になる。
457
+ #
458
+ # [+:scalar+]
459
+ # スカラ値(単独の値)を格納する。
460
+ #
461
+ # [+:vector+]
462
+ # 値の配列を格納する。
463
+ #
464
+ # [+:compress+]
465
+ # 値の圧縮方法を指定する。省略した場合は、圧縮しない。
466
+ #
467
+ # [+:zlib+]
468
+ # 値をzlib圧縮して格納する。
469
+ #
470
+ # [+:lzo+]
471
+ # 値をlzo圧縮して格納する。
279
472
  def column(name, type, options={})
280
473
  definition = self[name, ColumnDefinition]
281
474
  if definition.nil?
@@ -287,6 +480,10 @@ module Groonga
287
480
  self
288
481
  end
289
482
 
483
+ # 名前が_name_のカラムを削除する。
484
+ #
485
+ # _options_に指定可能な値はない(TODO _options_は不要?)。
486
+ #
290
487
  def remove_column(name, options={})
291
488
  definition = self[name, ColumnRemoveDefinition]
292
489
  if definition.nil?
@@ -297,6 +494,33 @@ module Groonga
297
494
  self
298
495
  end
299
496
 
497
+ # _taget_column_を対象とするインデックスカラムを作成す
498
+ # る。
499
+ #
500
+ # _options_に指定可能な値は以下の通り。
501
+ #
502
+ # [+:name+]
503
+ # インデックスカラムのカラム名を任意に指定する。
504
+ #
505
+ # [+:force+]
506
+ # +true+を指定すると既存の同名のカラムが存在してい
507
+ # ても、強制的に新しいカラムを作成する。
508
+ #
509
+ # [+:path+]
510
+ # カラムを保存するパス。
511
+ #
512
+ # [+:persistent+]
513
+ # +true+を指定すると永続カラムとなる。+:path+を省略
514
+ # した場合は自動的にパスが付加される。
515
+ #
516
+ # [+:with_section+]
517
+ # 転置索引にsection(段落情報)を合わせて格納する。
518
+ #
519
+ # [+:with_weight+]
520
+ # 転置索引にweight情報を合わせて格納する。
521
+ #
522
+ # [+:with_position+]
523
+ # 転置索引に出現位置情報を合わせて格納する。
300
524
  def index(target_column, options={})
301
525
  name = options.delete(:name)
302
526
  if name.nil?
@@ -319,66 +543,112 @@ module Groonga
319
543
  self
320
544
  end
321
545
 
546
+ # 名前が_name_の32bit符号付き整数のカラムを作成する。
547
+ #
548
+ # _options_に指定可能な値は
549
+ # Groonga::Schema::TableDefinition#columnを参照。
322
550
  def integer32(name, options={})
323
551
  column(name, "Int32", options)
324
552
  end
325
553
  alias_method :integer, :integer32
326
554
  alias_method :int32, :integer32
327
555
 
556
+ # 名前が_name_の64bit符号付き整数のカラムを作成する。
557
+ #
558
+ # _options_に指定可能な値は
559
+ # Groonga::Schema::TableDefinition#columnを参照。
328
560
  def integer64(name, options={})
329
561
  column(name, "Int64", options)
330
562
  end
331
563
  alias_method :int64, :integer64
332
564
 
565
+ # 名前が_name_の32bit符号なし整数のカラムを作成する。
566
+ #
567
+ # _options_に指定可能な値は
568
+ # Groonga::Schema::TableDefinition#columnを参照。
333
569
  def unsigned_integer32(name, options={})
334
570
  column(name, "UInt32", options)
335
571
  end
336
572
  alias_method :unsigned_integer, :unsigned_integer32
337
573
  alias_method :uint32, :unsigned_integer32
338
574
 
575
+ # 名前が_name_の64bit符号なし整数のカラムを作成する。
576
+ #
577
+ # _options_に指定可能な値は
578
+ # Groonga::Schema::TableDefinition#columnを参照。
339
579
  def unsigned_integer64(name, options={})
340
580
  column(name, "UInt64", options)
341
581
  end
342
582
  alias_method :uint64, :unsigned_integer64
343
583
 
584
+ # 名前が_name_のieee754形式の64bit浮動小数点数のカラム
585
+ # を作成する。
586
+ #
587
+ # _options_に指定可能な値は
588
+ # Groonga::Schema::TableDefinition#columnを参照。
344
589
  def float(name, options={})
345
590
  column(name, "Float", options)
346
591
  end
347
592
 
593
+ # 名前が_name_の64bit符号付き整数で1970年1月1日0時0分
594
+ # 0秒からの経過マイクロ秒数を格納するカラムを作成する。
595
+ #
596
+ # _options_に指定可能な値は
597
+ # Groonga::Schema::TableDefinition#columnを参照。
348
598
  def time(name, options={})
349
599
  column(name, "Time", options)
350
600
  end
351
601
 
602
+ # 名前が_name_の4Kbyte以下の文字列を格納できるカラムを
603
+ # 作成する。
604
+ #
605
+ # _options_に指定可能な値は
606
+ # Groonga::Schema::TableDefinition#columnを参照。
352
607
  def short_text(name, options={})
353
608
  column(name, "ShortText", options)
354
609
  end
355
610
  alias_method :string, :short_text
356
611
 
612
+ # 名前が_name_の64Kbyte以下の文字列を格納できるカラムを
613
+ # 作成する。
614
+ #
615
+ # _options_に指定可能な値は
616
+ # Groonga::Schema::TableDefinition#columnを参照。
357
617
  def text(name, options={})
358
618
  column(name, "Text", options)
359
619
  end
360
620
 
621
+ # 名前が_name_の2Gbyte以下の文字列を格納できるカラムを
622
+ # 作成する。
623
+ #
624
+ # _options_に指定可能な値は
625
+ # Groonga::Schema::TableDefinition#columnを参照。
361
626
  def long_text(name, options={})
362
627
  column(name, "LongText", options)
363
628
  end
364
629
 
630
+ # 名前が_name_で_table_のレコードIDを格納する参照カラ
631
+ # ムを作成する。
632
+ #
633
+ # _options_に指定可能な値は
634
+ # Groonga::Schema::TableDefinition#columnを参照。
365
635
  def reference(name, table, options={})
366
636
  column(name, table, options)
367
637
  end
368
638
 
369
- def [](name, definition_class=nil)
639
+ def [](name, definition_class=nil) # :nodoc:
370
640
  @definitions.find do |definition|
371
641
  definition.name.to_s == name.to_s and
372
642
  (definition_class.nil? or definition.is_a?(definition_class))
373
643
  end
374
644
  end
375
645
 
376
- def context
646
+ def context # :nodoc:
377
647
  @options[:context] || Groonga::Context.default
378
648
  end
379
649
 
380
650
  private
381
- def update_definition(name, definition_class, definition)
651
+ def update_definition(name, definition_class, definition) # :nodoc:
382
652
  old_definition = self[name, definition_class]
383
653
  if old_definition
384
654
  index = @definitions.index(old_definition)
@@ -392,8 +662,8 @@ module Groonga
392
662
  :type, :path, :persistent,
393
663
  :key_type, :value_type, :sub_records,
394
664
  :default_tokenizer,
395
- :key_normalize, :key_with_sis]
396
- def validate_options(options)
665
+ :key_normalize, :key_with_sis] # :nodoc:
666
+ def validate_options(options) # :nodoc:
397
667
  return if options.nil?
398
668
  unknown_keys = options.keys - AVAILABLE_OPTION_KEYS
399
669
  unless unknown_keys.empty?
@@ -403,7 +673,7 @@ module Groonga
403
673
  end
404
674
  end
405
675
 
406
- def table_type
676
+ def table_type # :nodoc:
407
677
  type = @options[:type]
408
678
  case type
409
679
  when :array, nil
@@ -417,7 +687,7 @@ module Groonga
417
687
  end
418
688
  end
419
689
 
420
- def create_options
690
+ def create_options # :nodoc:
421
691
  common = {
422
692
  :name => @name,
423
693
  :path => @options[:path],
@@ -446,16 +716,16 @@ module Groonga
446
716
  end
447
717
  end
448
718
 
449
- def column_options
719
+ def column_options # :nodoc:
450
720
  {:persistent => persistent?}
451
721
  end
452
722
 
453
- def persistent?
723
+ def persistent? # :nodoc:
454
724
  @options[:persistent].nil? ? true : @options[:persistent]
455
725
  end
456
726
  end
457
727
 
458
- class TableRemoveDefinition
728
+ class TableRemoveDefinition # :nodoc:
459
729
  def initialize(name, options={})
460
730
  @name = name
461
731
  @options = options
@@ -467,7 +737,7 @@ module Groonga
467
737
  end
468
738
  end
469
739
 
470
- class ColumnDefinition
740
+ class ColumnDefinition # :nodoc:
471
741
  attr_accessor :name, :type
472
742
  attr_reader :options
473
743
 
@@ -478,14 +748,33 @@ module Groonga
478
748
  @type = nil
479
749
  end
480
750
 
481
- def define(table)
751
+ def define(table_definition, table)
752
+ column = table.column(@name)
753
+ if column
754
+ return column if same_column?(table_definition, column)
755
+ if @options.delete(:force)
756
+ column.remove
757
+ else
758
+ raise ArgumentError,
759
+ "the same name column with different type is " +
760
+ "already defined: #{@type.inspect}(#{@options.inspect}): " +
761
+ "#{column.inspect}"
762
+ end
763
+ end
482
764
  table.define_column(@name,
483
765
  Schema.normalize_type(@type),
484
766
  @options)
485
767
  end
768
+
769
+ private
770
+ def same_column?(table_definition, column)
771
+ context = table_definition.context
772
+ # TODO: should check column type and other options.
773
+ column.range == context[Schema.normalize_type(@type)]
774
+ end
486
775
  end
487
776
 
488
- class ColumnRemoveDefinition
777
+ class ColumnRemoveDefinition # :nodoc:
489
778
  attr_accessor :name
490
779
  attr_reader :options
491
780
 
@@ -495,12 +784,12 @@ module Groonga
495
784
  @options = (options || {}).dup
496
785
  end
497
786
 
498
- def define(table)
787
+ def define(table_definition, table)
499
788
  table.column(@name).remove
500
789
  end
501
790
  end
502
791
 
503
- class IndexColumnDefinition
792
+ class IndexColumnDefinition # :nodoc:
504
793
  attr_accessor :name, :target
505
794
  attr_reader :options
506
795
 
@@ -511,12 +800,27 @@ module Groonga
511
800
  @target = nil
512
801
  end
513
802
 
514
- def define(table)
803
+ def define(table_definition, table)
515
804
  target = @target
516
- target = context[target] unless target.is_a?(Groonga::Object)
805
+ unless target.is_a?(Groonga::Object)
806
+ target = table_definition.context[target]
807
+ end
517
808
  if target.nil?
518
809
  raise ArgumentError, "Unknown index target: #{@target.inspect}"
519
810
  end
811
+ index = table.column(@name)
812
+ if index
813
+ return index if same_index?(table_definition, index, target)
814
+ if @options.delete(:force)
815
+ index.remove
816
+ else
817
+ raise ArgumentError,
818
+ "the same name index column with " +
819
+ "different target or options is " +
820
+ "already defined: #{target.inspect}(#{@options.inspect}): " +
821
+ "#{index.inspect}"
822
+ end
823
+ end
520
824
  index = table.define_index_column(@name,
521
825
  target.table,
522
826
  @options)
@@ -525,12 +829,14 @@ module Groonga
525
829
  end
526
830
 
527
831
  private
528
- def context
529
- @options[:context] || Groonga::Context.default
832
+ def same_index?(table_definition, index, target)
833
+ context = table_definition.context
834
+ # TODO: should check column type and other options.
835
+ index.range == target.table and index.sources == [target]
530
836
  end
531
837
  end
532
838
 
533
- class Dumper
839
+ class Dumper # :nodoc:
534
840
  def initialize(options={})
535
841
  @options = (options || {}).dup
536
842
  end