groonga 0.9.1 → 1.0.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 (186) hide show
  1. metadata +38 -227
  2. data/AUTHORS +0 -5
  3. data/NEWS.ja.rdoc +0 -114
  4. data/NEWS.rdoc +0 -116
  5. data/README.ja.rdoc +0 -63
  6. data/README.rdoc +0 -64
  7. data/Rakefile +0 -216
  8. data/benchmark/common.rb +0 -49
  9. data/benchmark/read-write-many-small-items.rb +0 -144
  10. data/benchmark/write-many-small-items.rb +0 -135
  11. data/example/bookmark.rb +0 -161
  12. data/example/index-html.rb +0 -89
  13. data/example/search/config.ru +0 -211
  14. data/example/search/public/css/groonga.css +0 -122
  15. data/ext/.gitignore +0 -2
  16. data/ext/rb-grn-accessor.c +0 -52
  17. data/ext/rb-grn-array-cursor.c +0 -36
  18. data/ext/rb-grn-array.c +0 -210
  19. data/ext/rb-grn-column.c +0 -570
  20. data/ext/rb-grn-context.c +0 -655
  21. data/ext/rb-grn-database.c +0 -415
  22. data/ext/rb-grn-encoding-support.c +0 -64
  23. data/ext/rb-grn-encoding.c +0 -257
  24. data/ext/rb-grn-exception.c +0 -1110
  25. data/ext/rb-grn-expression-builder.c +0 -75
  26. data/ext/rb-grn-expression.c +0 -732
  27. data/ext/rb-grn-fix-size-column.c +0 -166
  28. data/ext/rb-grn-hash-cursor.c +0 -38
  29. data/ext/rb-grn-hash.c +0 -294
  30. data/ext/rb-grn-index-column.c +0 -488
  31. data/ext/rb-grn-logger.c +0 -325
  32. data/ext/rb-grn-object.c +0 -1335
  33. data/ext/rb-grn-operation.c +0 -198
  34. data/ext/rb-grn-patricia-trie-cursor.c +0 -39
  35. data/ext/rb-grn-patricia-trie.c +0 -488
  36. data/ext/rb-grn-procedure.c +0 -52
  37. data/ext/rb-grn-query.c +0 -260
  38. data/ext/rb-grn-record.c +0 -40
  39. data/ext/rb-grn-snippet.c +0 -328
  40. data/ext/rb-grn-table-cursor-key-support.c +0 -69
  41. data/ext/rb-grn-table-cursor.c +0 -246
  42. data/ext/rb-grn-table-key-support.c +0 -731
  43. data/ext/rb-grn-table.c +0 -2038
  44. data/ext/rb-grn-type.c +0 -181
  45. data/ext/rb-grn-utils.c +0 -769
  46. data/ext/rb-grn-variable-size-column.c +0 -36
  47. data/ext/rb-grn-variable.c +0 -108
  48. data/ext/rb-grn.h +0 -684
  49. data/ext/rb-groonga.c +0 -113
  50. data/extconf.rb +0 -216
  51. data/html/bar.svg +0 -153
  52. data/html/developer.html +0 -121
  53. data/html/developer.svg +0 -469
  54. data/html/download.svg +0 -253
  55. data/html/favicon.ico +0 -0
  56. data/html/favicon.xcf +0 -0
  57. data/html/footer.html.erb +0 -28
  58. data/html/head.html.erb +0 -4
  59. data/html/header.html.erb +0 -17
  60. data/html/index.html +0 -147
  61. data/html/install.svg +0 -636
  62. data/html/logo.xcf +0 -0
  63. data/html/ranguba.css +0 -248
  64. data/html/tutorial.svg +0 -559
  65. data/lib/groonga.rb +0 -83
  66. data/lib/groonga/expression-builder.rb +0 -285
  67. data/lib/groonga/patricia-trie.rb +0 -53
  68. data/lib/groonga/record.rb +0 -276
  69. data/lib/groonga/schema.rb +0 -916
  70. data/license/GPL +0 -340
  71. data/license/LGPL +0 -504
  72. data/license/RUBY +0 -59
  73. data/misc/grnop2ruby.rb +0 -49
  74. data/pkg-config.rb +0 -333
  75. data/src/rb-grn-table-cursor.c +0 -296
  76. data/test-unit/Rakefile +0 -40
  77. data/test-unit/TODO +0 -5
  78. data/test-unit/bin/testrb +0 -5
  79. data/test-unit/html/classic.html +0 -15
  80. data/test-unit/html/index.html +0 -25
  81. data/test-unit/html/index.html.ja +0 -27
  82. data/test-unit/lib/test/unit.rb +0 -323
  83. data/test-unit/lib/test/unit/assertionfailederror.rb +0 -25
  84. data/test-unit/lib/test/unit/assertions.rb +0 -1230
  85. data/test-unit/lib/test/unit/attribute.rb +0 -125
  86. data/test-unit/lib/test/unit/autorunner.rb +0 -353
  87. data/test-unit/lib/test/unit/collector.rb +0 -36
  88. data/test-unit/lib/test/unit/collector/descendant.rb +0 -23
  89. data/test-unit/lib/test/unit/collector/dir.rb +0 -108
  90. data/test-unit/lib/test/unit/collector/load.rb +0 -136
  91. data/test-unit/lib/test/unit/collector/objectspace.rb +0 -34
  92. data/test-unit/lib/test/unit/color-scheme.rb +0 -102
  93. data/test-unit/lib/test/unit/color.rb +0 -96
  94. data/test-unit/lib/test/unit/diff.rb +0 -724
  95. data/test-unit/lib/test/unit/error.rb +0 -130
  96. data/test-unit/lib/test/unit/exceptionhandler.rb +0 -39
  97. data/test-unit/lib/test/unit/failure.rb +0 -136
  98. data/test-unit/lib/test/unit/fixture.rb +0 -176
  99. data/test-unit/lib/test/unit/notification.rb +0 -129
  100. data/test-unit/lib/test/unit/omission.rb +0 -191
  101. data/test-unit/lib/test/unit/pending.rb +0 -150
  102. data/test-unit/lib/test/unit/priority.rb +0 -181
  103. data/test-unit/lib/test/unit/runner/console.rb +0 -52
  104. data/test-unit/lib/test/unit/runner/emacs.rb +0 -8
  105. data/test-unit/lib/test/unit/runner/tap.rb +0 -8
  106. data/test-unit/lib/test/unit/testcase.rb +0 -476
  107. data/test-unit/lib/test/unit/testresult.rb +0 -89
  108. data/test-unit/lib/test/unit/testsuite.rb +0 -110
  109. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +0 -14
  110. data/test-unit/lib/test/unit/ui/console/testrunner.rb +0 -464
  111. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +0 -63
  112. data/test-unit/lib/test/unit/ui/tap/testrunner.rb +0 -92
  113. data/test-unit/lib/test/unit/ui/testrunner.rb +0 -28
  114. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +0 -77
  115. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +0 -41
  116. data/test-unit/lib/test/unit/util/backtracefilter.rb +0 -41
  117. data/test-unit/lib/test/unit/util/method-owner-finder.rb +0 -28
  118. data/test-unit/lib/test/unit/util/observable.rb +0 -90
  119. data/test-unit/lib/test/unit/util/procwrapper.rb +0 -48
  120. data/test-unit/lib/test/unit/version.rb +0 -7
  121. data/test-unit/sample/adder.rb +0 -13
  122. data/test-unit/sample/subtracter.rb +0 -12
  123. data/test-unit/sample/test_adder.rb +0 -20
  124. data/test-unit/sample/test_subtracter.rb +0 -20
  125. data/test-unit/sample/test_user.rb +0 -23
  126. data/test-unit/test/collector/test-descendant.rb +0 -133
  127. data/test-unit/test/collector/test-load.rb +0 -329
  128. data/test-unit/test/collector/test_dir.rb +0 -406
  129. data/test-unit/test/collector/test_objectspace.rb +0 -100
  130. data/test-unit/test/run-test.rb +0 -15
  131. data/test-unit/test/test-attribute.rb +0 -86
  132. data/test-unit/test/test-color-scheme.rb +0 -67
  133. data/test-unit/test/test-color.rb +0 -47
  134. data/test-unit/test/test-diff.rb +0 -518
  135. data/test-unit/test/test-emacs-runner.rb +0 -60
  136. data/test-unit/test/test-fixture.rb +0 -287
  137. data/test-unit/test/test-notification.rb +0 -33
  138. data/test-unit/test/test-omission.rb +0 -81
  139. data/test-unit/test/test-pending.rb +0 -70
  140. data/test-unit/test/test-priority.rb +0 -119
  141. data/test-unit/test/test-testcase.rb +0 -554
  142. data/test-unit/test/test_assertions.rb +0 -1151
  143. data/test-unit/test/test_error.rb +0 -26
  144. data/test-unit/test/test_failure.rb +0 -33
  145. data/test-unit/test/test_testresult.rb +0 -113
  146. data/test-unit/test/test_testsuite.rb +0 -129
  147. data/test-unit/test/testunit-test-util.rb +0 -14
  148. data/test-unit/test/ui/test_testrunmediator.rb +0 -20
  149. data/test-unit/test/util/test-method-owner-finder.rb +0 -38
  150. data/test-unit/test/util/test_backtracefilter.rb +0 -41
  151. data/test-unit/test/util/test_observable.rb +0 -102
  152. data/test-unit/test/util/test_procwrapper.rb +0 -36
  153. data/test/.gitignore +0 -1
  154. data/test/groonga-test-utils.rb +0 -106
  155. data/test/run-test.rb +0 -58
  156. data/test/test-array.rb +0 -97
  157. data/test/test-column.rb +0 -298
  158. data/test/test-context.rb +0 -73
  159. data/test/test-database.rb +0 -113
  160. data/test/test-encoding.rb +0 -33
  161. data/test/test-exception.rb +0 -93
  162. data/test/test-expression-builder.rb +0 -156
  163. data/test/test-expression.rb +0 -133
  164. data/test/test-fix-size-column.rb +0 -65
  165. data/test/test-gqtp.rb +0 -70
  166. data/test/test-hash.rb +0 -312
  167. data/test/test-index-column.rb +0 -81
  168. data/test/test-patricia-trie.rb +0 -189
  169. data/test/test-procedure.rb +0 -37
  170. data/test/test-query.rb +0 -22
  171. data/test/test-record.rb +0 -268
  172. data/test/test-remote.rb +0 -53
  173. data/test/test-schema.rb +0 -416
  174. data/test/test-snippet.rb +0 -121
  175. data/test/test-table-cursor.rb +0 -153
  176. data/test/test-table-offset-and-limit.rb +0 -102
  177. data/test/test-table-select-normalize.rb +0 -48
  178. data/test/test-table-select.rb +0 -145
  179. data/test/test-table.rb +0 -642
  180. data/test/test-type.rb +0 -61
  181. data/test/test-variable-size-column.rb +0 -98
  182. data/test/test-variable.rb +0 -28
  183. data/test/test-vector-column.rb +0 -76
  184. data/test/test-version.rb +0 -31
  185. data/text/TUTORIAL.ja.rdoc +0 -392
  186. data/text/expression.rdoc +0 -284
@@ -1,916 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- #
3
- # Copyright (C) 2009 Kouhei Sutou <kou@clear-code.com>
4
- #
5
- # This library is free software; you can redistribute it and/or
6
- # modify it under the terms of the GNU Lesser General Public
7
- # License version 2.1 as published by the Free Software Foundation.
8
- #
9
- # This library is distributed in the hope that it will be useful,
10
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- # Lesser General Public License for more details.
13
- #
14
- # You should have received a copy of the GNU Lesser General Public
15
- # License along with this library; if not, write to the Free Software
16
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
-
18
-
19
- module Groonga
20
-
21
- # groongaのスキーマ(データ構造)を管理するクラス。
22
- #
23
- # Groonga::Schemaを使うことにより簡単にテーブルやカラムを
24
- # 追加・削除することができる。
25
- #
26
- # http://qwik.jp/senna/senna2.files/rect4605.png
27
- # のようなスキーマを定義する場合は以下のようになる。
28
- #
29
- # Groonga::Schema.define do |schema|
30
- # schema.create_table("Items") do |table|
31
- # table.short_text("title")
32
- # end
33
- #
34
- # schema.create_table("Users") do |table|
35
- # table.short_text("name")
36
- # end
37
- #
38
- # schema.create_table("comments") do |table|
39
- # table.reference("item", "Items")
40
- # table.reference("author", "Users")
41
- # table.text("content")
42
- # table.time("issued")
43
- # end
44
- # end
45
- class Schema
46
- class << self
47
-
48
- # call-seq:
49
- # Groonga::Schema.define(options={}) {|schema| ...}
50
- #
51
- # スキーマを定義する。ブロックにはGroonga::Schemaオブ
52
- # ジェクトがわたるので、そのオブジェクトを利用してスキー
53
- # マを定義する。以下の省略形。
54
- #
55
- # schema = Groonga::Scheme.new(options)
56
- # ...
57
- # schema.define
58
- #
59
- # _options_に指定可能な値は以下の通り。
60
- #
61
- # [+:context+]
62
- # スキーマ定義時に使用するGroonga::Contextを指定する。
63
- # 省略した場合はGroonga::Context.defaultを使用する。
64
- def define(options={})
65
- schema = new(options)
66
- yield(schema)
67
- schema.define
68
- end
69
-
70
- # call-seq:
71
- # Groonga::Schema.create_table(name, options={}) {|table| ...}
72
- #
73
- # 名前が_name_のテーブルを作成する。以下の省略形。
74
- #
75
- # Groonga::Schema.define do |schema|
76
- # schema.create_table(name, options) do |table|
77
- # ...
78
- # end
79
- # end
80
- #
81
- # ブロックにはGroonga::Schema::TableDefinitionオブジェ
82
- # クトがわたるので、そのオブジェクトを利用してテーブル
83
- # の詳細を定義する。
84
- #
85
- # _options_に指定可能な値は以下の通り。
86
- #
87
- # [+:force+]
88
- # +true+を指定すると既存の同名のテーブルが存在してい
89
- # ても、強制的にテーブルを作成する。
90
- #
91
- # [+:type+]
92
- # テーブルの型を指定する。+:array+, +:hash+,
93
- # +:patricia_trie+のいずれかを指定する。デフォルトで
94
- # は+:array+になる。
95
- #
96
- # [+:context+]
97
- # スキーマ定義時に使用するGroonga::Contextを指定する。
98
- # 省略した場合はGroonga::Context.defaultを使用する。
99
- #
100
- # [+:path+]
101
- # テーブルを保存するパスを指定する。パスを指定すると
102
- # 永続テーブルになる。
103
- #
104
- # [+:persistent+]
105
- # テーブルを永続テーブルとする。+:path:+を省略した場
106
- # 合はパス名は自動的に作成される。デフォルトでは永続
107
- # テーブルとなる。
108
- #
109
- # [+:value_type+]
110
- # 値の型を指定する。省略すると値のための領域を確保しない。
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
- # 登録される。
155
- def create_table(name, options={}, &block)
156
- define do |schema|
157
- schema.create_table(name, options, &block)
158
- end
159
- end
160
-
161
- # 名前が_name_のテーブルを削除する。
162
- #
163
- # _options_に指定可能な値は以下の通り。
164
- #
165
- # [+:context+]
166
- # スキーマ定義時に使用するGroonga::Contextを指定する。
167
- # 省略した場合はGroonga::Context.defaultを使用する。
168
- def remove_table(name, options={})
169
- define do |schema|
170
- schema.remove_table(name, options)
171
- end
172
- end
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を使用する。
194
- def change_table(name, options={}, &block)
195
- define do |schema|
196
- schema.change_table(name, options, &block)
197
- end
198
- end
199
-
200
- # スキーマの内容を文字列で返す。返された値は
201
- # Groonga::Schema.restoreすることによりスキーマ内に組
202
- # み込むことができる。
203
- #
204
- # dump.rb:
205
- # File.open("/tmp/groonga-schema.rb", "w") do |schema|
206
- # dumped_text = Groonga::Schema.dump
207
- # end
208
- #
209
- # restore.rb:
210
- # dumped_text = Groonga::Schema.dump
211
- # Groonga::Database.create(:path => "/tmp/new-db.grn")
212
- # Groonga::Schema.restore(dumped_text)
213
- #
214
- # _options_に指定可能な値は以下の通り。
215
- #
216
- # [+:context+]
217
- # スキーマ定義時に使用するGroonga::Contextを指定する。
218
- # 省略した場合はGroonga::Context.defaultを使用する。
219
- def dump(options={})
220
- Dumper.new(options).dump
221
- end
222
-
223
- # Groonga::Schema.dumpで文字列化したスキーマを組み込む。
224
- def restore(dumped_text, options={})
225
- define(options) do |schema|
226
- schema.load(dumped_text)
227
- end
228
- end
229
-
230
- def normalize_type(type) # :nodoc:
231
- return type if type.nil?
232
- return type if type.is_a?(Groonga::Object)
233
- case type.to_s
234
- when "string"
235
- "ShortText"
236
- when "text"
237
- "Text"
238
- when "int", "integer"
239
- "Int32"
240
- when "float"
241
- "Float"
242
- when "decimal"
243
- "Int64"
244
- when "datetime", "timestamp", "time", "date"
245
- "Time"
246
- when "binary"
247
- "LongText"
248
- when "boolean"
249
- "Bool"
250
- else
251
- type
252
- end
253
- end
254
- end
255
-
256
- # スキーマ定義を開始する。
257
- #
258
- # _options_に指定可能な値は以下の通り。
259
- #
260
- # [+:context+]
261
- # スキーマ定義時に使用するGroonga::Contextを指定する。
262
- # 省略した場合はGroonga::Context.defaultを使用する。
263
- def initialize(options={})
264
- @options = (options || {}).dup
265
- @definitions = []
266
- end
267
-
268
- # 定義されたスキーマ定義を実際に実行する。
269
- def define
270
- @definitions.each do |definition|
271
- definition.define
272
- end
273
- end
274
-
275
- # Groonga::Schema.dumpで返されたスキーマの内容を読み込む。
276
- #
277
- # 読み込まれた内容は#defineを呼び出すまでは実行されない
278
- # ことに注意すること。
279
- def load(dumped_text)
280
- instance_eval(dumped_text)
281
- end
282
-
283
- # call-seq:
284
- # schema.create_table(name, options={}) {|table| ...}
285
- #
286
- # 名前が_name_のテーブルを作成する。
287
- #
288
- # テーブルの作成は#defineを呼び出すまでは実行されないこ
289
- # とに注意すること。
290
- #
291
- # _options_に指定可能な値は以下の通り。
292
- #
293
- # [+:force+]
294
- # +true+を指定すると既存の同名のテーブルが存在してい
295
- # ても、強制的にテーブルを作成する。
296
- #
297
- # [+:type+]
298
- # テーブルの型を指定する。+:array+, +:hash+,
299
- # +:patricia_trie+のいずれかを指定する。デフォルトで
300
- # は+:array+になる。
301
- #
302
- # [+:context+]
303
- # スキーマ定義時に使用するGroonga::Contextを指定する。
304
- # 省略した場合はGroonga::Schema.newで指定した
305
- # Groonga::Contextを使用する。Groonga::Schema.newで指
306
- # 定していない場合はGroonga::Context.defaultを使用する。
307
- #
308
- # [+:path+]
309
- # テーブルを保存するパスを指定する。パスを指定すると
310
- # 永続テーブルになる。
311
- #
312
- # [+:persistent+]
313
- # テーブルを永続テーブルとする。+:path:+を省略した場
314
- # 合はパス名は自動的に作成される。デフォルトでは永続
315
- # テーブルとなる。
316
- #
317
- # [+:value_type+]
318
- # 値の型を指定する。省略すると値のための領域を確保しな
319
- # い。値を保存したい場合は必ず指定すること。
320
- #
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
- # 登録される。
365
- def create_table(name, options={})
366
- definition = TableDefinition.new(name, @options.merge(options || {}))
367
- yield(definition)
368
- @definitions << definition
369
- end
370
-
371
- # 名前が_name_のテーブルを削除する。
372
- #
373
- # テーブルの削除は#defineを呼び出すまでは実行されないこ
374
- # とに注意すること。
375
- #
376
- # _options_に指定可能な値は以下の通り。
377
- #
378
- # [+:context+]
379
- # スキーマ定義時に使用するGroonga::Contextを指定する。
380
- # 省略した場合はGroonga::Context.defaultを使用する。
381
- def remove_table(name, options={})
382
- definition = TableRemoveDefinition.new(name, @options.merge(options || {}))
383
- @definitions << definition
384
- end
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を使用する。
399
- def change_table(name, options={})
400
- options = @options.merge(options || {}).merge(:change => true)
401
- definition = TableDefinition.new(name, options)
402
- yield(definition)
403
- @definitions << definition
404
- end
405
-
406
- # スキーマ定義時にGroonga::Schema.create_tableや
407
- # Groonga::Schema#create_tableからブロックに渡されてくる
408
- # オブジェクト
409
- class TableDefinition
410
- # テーブルの名前
411
- attr_reader :name
412
-
413
- def initialize(name, options) # :nodoc:
414
- @name = name
415
- @name = @name.to_s if @name.is_a?(Symbol)
416
- @definitions = []
417
- validate_options(options)
418
- @options = options
419
- @table_type = table_type
420
- end
421
-
422
- def define # :nodoc:
423
- table = context[@name]
424
- if @options[:change]
425
- raise ArgumentError, "table doesn't exist: #{@name}" if table.nil?
426
- else
427
- if table and @options[:force]
428
- table.remove
429
- table = nil
430
- end
431
- table ||= @table_type.create(create_options)
432
- end
433
- @definitions.each do |definition|
434
- definition.define(self, table)
435
- end
436
- table
437
- end
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圧縮して格納する。
472
- def column(name, type, options={})
473
- definition = self[name, ColumnDefinition]
474
- if definition.nil?
475
- definition = ColumnDefinition.new(name, options)
476
- update_definition(name, ColumnDefinition, definition)
477
- end
478
- definition.type = type
479
- definition.options.merge!(column_options.merge(options))
480
- self
481
- end
482
-
483
- # 名前が_name_のカラムを削除する。
484
- #
485
- # _options_に指定可能な値はない(TODO _options_は不要?)。
486
- #
487
- def remove_column(name, options={})
488
- definition = self[name, ColumnRemoveDefinition]
489
- if definition.nil?
490
- definition = ColumnRemoveDefinition.new(name, options)
491
- update_definition(name, ColumnRemoveDefinition, definition)
492
- end
493
- definition.options.merge!(options)
494
- self
495
- end
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
- # 転置索引に出現位置情報を合わせて格納する。
524
- def index(target_column, options={})
525
- name = options.delete(:name)
526
- if name.nil?
527
- target_column_name = nil
528
- if target_column.is_a?(Groonga::Column)
529
- target_column_name = target_column.name
530
- else
531
- target_column_name = target_column
532
- end
533
- name = target_column_name.gsub(/\./, "_")
534
- end
535
-
536
- definition = self[name, IndexColumnDefinition]
537
- if definition.nil?
538
- definition = IndexColumnDefinition.new(name, options)
539
- update_definition(name, IndexColumnDefinition, definition)
540
- end
541
- definition.target = target_column
542
- definition.options.merge!(column_options.merge(options))
543
- self
544
- end
545
-
546
- # 名前が_name_の32bit符号付き整数のカラムを作成する。
547
- #
548
- # _options_に指定可能な値は
549
- # Groonga::Schema::TableDefinition#columnを参照。
550
- def integer32(name, options={})
551
- column(name, "Int32", options)
552
- end
553
- alias_method :integer, :integer32
554
- alias_method :int32, :integer32
555
-
556
- # 名前が_name_の64bit符号付き整数のカラムを作成する。
557
- #
558
- # _options_に指定可能な値は
559
- # Groonga::Schema::TableDefinition#columnを参照。
560
- def integer64(name, options={})
561
- column(name, "Int64", options)
562
- end
563
- alias_method :int64, :integer64
564
-
565
- # 名前が_name_の32bit符号なし整数のカラムを作成する。
566
- #
567
- # _options_に指定可能な値は
568
- # Groonga::Schema::TableDefinition#columnを参照。
569
- def unsigned_integer32(name, options={})
570
- column(name, "UInt32", options)
571
- end
572
- alias_method :unsigned_integer, :unsigned_integer32
573
- alias_method :uint32, :unsigned_integer32
574
-
575
- # 名前が_name_の64bit符号なし整数のカラムを作成する。
576
- #
577
- # _options_に指定可能な値は
578
- # Groonga::Schema::TableDefinition#columnを参照。
579
- def unsigned_integer64(name, options={})
580
- column(name, "UInt64", options)
581
- end
582
- alias_method :uint64, :unsigned_integer64
583
-
584
- # 名前が_name_のieee754形式の64bit浮動小数点数のカラム
585
- # を作成する。
586
- #
587
- # _options_に指定可能な値は
588
- # Groonga::Schema::TableDefinition#columnを参照。
589
- def float(name, options={})
590
- column(name, "Float", options)
591
- end
592
-
593
- # 名前が_name_の64bit符号付き整数で1970年1月1日0時0分
594
- # 0秒からの経過マイクロ秒数を格納するカラムを作成する。
595
- #
596
- # _options_に指定可能な値は
597
- # Groonga::Schema::TableDefinition#columnを参照。
598
- def time(name, options={})
599
- column(name, "Time", options)
600
- end
601
-
602
- # 名前が_name_の4Kbyte以下の文字列を格納できるカラムを
603
- # 作成する。
604
- #
605
- # _options_に指定可能な値は
606
- # Groonga::Schema::TableDefinition#columnを参照。
607
- def short_text(name, options={})
608
- column(name, "ShortText", options)
609
- end
610
- alias_method :string, :short_text
611
-
612
- # 名前が_name_の64Kbyte以下の文字列を格納できるカラムを
613
- # 作成する。
614
- #
615
- # _options_に指定可能な値は
616
- # Groonga::Schema::TableDefinition#columnを参照。
617
- def text(name, options={})
618
- column(name, "Text", options)
619
- end
620
-
621
- # 名前が_name_の2Gbyte以下の文字列を格納できるカラムを
622
- # 作成する。
623
- #
624
- # _options_に指定可能な値は
625
- # Groonga::Schema::TableDefinition#columnを参照。
626
- def long_text(name, options={})
627
- column(name, "LongText", options)
628
- end
629
-
630
- # 名前が_name_で_table_のレコードIDを格納する参照カラ
631
- # ムを作成する。
632
- #
633
- # _options_に指定可能な値は
634
- # Groonga::Schema::TableDefinition#columnを参照。
635
- def reference(name, table, options={})
636
- column(name, table, options)
637
- end
638
-
639
- def [](name, definition_class=nil) # :nodoc:
640
- @definitions.find do |definition|
641
- definition.name.to_s == name.to_s and
642
- (definition_class.nil? or definition.is_a?(definition_class))
643
- end
644
- end
645
-
646
- def context # :nodoc:
647
- @options[:context] || Groonga::Context.default
648
- end
649
-
650
- private
651
- def update_definition(name, definition_class, definition) # :nodoc:
652
- old_definition = self[name, definition_class]
653
- if old_definition
654
- index = @definitions.index(old_definition)
655
- @definitions[index] = definition
656
- else
657
- @definitions << definition
658
- end
659
- end
660
-
661
- AVAILABLE_OPTION_KEYS = [:context, :change, :force,
662
- :type, :path, :persistent,
663
- :key_type, :value_type, :sub_records,
664
- :default_tokenizer,
665
- :key_normalize, :key_with_sis] # :nodoc:
666
- def validate_options(options) # :nodoc:
667
- return if options.nil?
668
- unknown_keys = options.keys - AVAILABLE_OPTION_KEYS
669
- unless unknown_keys.empty?
670
- message = "unknown keys are specified: #{unknown_keys.inspect}"
671
- message << ": available keys: #{AVAILABLE_OPTION_KEYS.inspect}"
672
- raise ArgumentError, message
673
- end
674
- end
675
-
676
- def table_type # :nodoc:
677
- type = @options[:type]
678
- case type
679
- when :array, nil
680
- Groonga::Array
681
- when :hash
682
- Groonga::Hash
683
- when :patricia_trie
684
- Groonga::PatriciaTrie
685
- else
686
- raise ArgumentError, "unknown table type: #{type.inspect}"
687
- end
688
- end
689
-
690
- def create_options # :nodoc:
691
- common = {
692
- :name => @name,
693
- :path => @options[:path],
694
- :persistent => persistent?,
695
- :value_type => @options[:value_type],
696
- :context => context,
697
- :sub_records => @options[:sub_records],
698
- }
699
- key_support_table_common = {
700
- :key_type => Schema.normalize_type(@options[:key_type]),
701
- :default_tokenizer => @options[:default_tokenizer],
702
- }
703
-
704
- if @table_type == Groonga::Array
705
- common
706
- elsif @table_type == Groonga::Hash
707
- common.merge(key_support_table_common)
708
- elsif @table_type == Groonga::PatriciaTrie
709
- options = {
710
- :key_normalize => @options[:key_normalize],
711
- :key_with_sis => @options[:key_with_sis],
712
- }
713
- common.merge(key_support_table_common).merge(options)
714
- else
715
- raise ArgumentError, "unknown table type: #{@table_type.inspect}"
716
- end
717
- end
718
-
719
- def column_options # :nodoc:
720
- {:persistent => persistent?}
721
- end
722
-
723
- def persistent? # :nodoc:
724
- @options[:persistent].nil? ? true : @options[:persistent]
725
- end
726
- end
727
-
728
- class TableRemoveDefinition # :nodoc:
729
- def initialize(name, options={})
730
- @name = name
731
- @options = options
732
- end
733
-
734
- def define
735
- context = @options[:context] || Groonga::Context.default
736
- context[@name].remove
737
- end
738
- end
739
-
740
- class ColumnDefinition # :nodoc:
741
- attr_accessor :name, :type
742
- attr_reader :options
743
-
744
- def initialize(name, options={})
745
- @name = name
746
- @name = @name.to_s if @name.is_a?(Symbol)
747
- @options = (options || {}).dup
748
- @type = nil
749
- end
750
-
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
764
- table.define_column(@name,
765
- Schema.normalize_type(@type),
766
- @options)
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
775
- end
776
-
777
- class ColumnRemoveDefinition # :nodoc:
778
- attr_accessor :name
779
- attr_reader :options
780
-
781
- def initialize(name, options={})
782
- @name = name
783
- @name = @name.to_s if @name.is_a?(Symbol)
784
- @options = (options || {}).dup
785
- end
786
-
787
- def define(table_definition, table)
788
- table.column(@name).remove
789
- end
790
- end
791
-
792
- class IndexColumnDefinition # :nodoc:
793
- attr_accessor :name, :target
794
- attr_reader :options
795
-
796
- def initialize(name, options={})
797
- @name = name
798
- @name = @name.to_s if @name.is_a?(Symbol)
799
- @options = (options || {}).dup
800
- @target = nil
801
- end
802
-
803
- def define(table_definition, table)
804
- target = @target
805
- unless target.is_a?(Groonga::Object)
806
- target = table_definition.context[target]
807
- end
808
- if target.nil?
809
- raise ArgumentError, "Unknown index target: #{@target.inspect}"
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
824
- index = table.define_index_column(@name,
825
- target.table,
826
- @options)
827
- index.source = target
828
- index
829
- end
830
-
831
- private
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]
836
- end
837
- end
838
-
839
- class Dumper # :nodoc:
840
- def initialize(options={})
841
- @options = (options || {}).dup
842
- end
843
-
844
- def dump
845
- context = @options[:context] || Groonga::Context.default
846
- database = context.database
847
- return nil if database.nil?
848
-
849
- reference_columns = []
850
- definitions = []
851
- database.each do |object|
852
- next unless object.is_a?(Groonga::Table)
853
- schema = "create_table(#{object.name.inspect}) do |table|\n"
854
- object.columns.sort_by {|column| column.local_name}.each do |column|
855
- if column.range.is_a?(Groonga::Table)
856
- reference_columns << column
857
- else
858
- type = column_method(column)
859
- name = column.local_name
860
- schema << " table.#{type}(#{name.inspect})\n"
861
- end
862
- end
863
- schema << "end"
864
- definitions << schema
865
- end
866
-
867
- reference_columns.group_by do |column|
868
- column.table
869
- end.each do |table, columns|
870
- schema = "change_table(#{table.name.inspect}) do |table|\n"
871
- columns.each do |column|
872
- name = column.local_name
873
- reference = column.range
874
- schema << " table.reference(#{name.inspect}, " +
875
- "#{reference.name.inspect})\n"
876
- end
877
- schema << "end"
878
- definitions << schema
879
- end
880
-
881
- if definitions.empty?
882
- ""
883
- else
884
- definitions.join("\n\n") + "\n"
885
- end
886
- end
887
-
888
- private
889
- def column_method(column)
890
- range = column.range
891
- case range.name
892
- when "Int32"
893
- "integer32"
894
- when "Int64"
895
- "integer64"
896
- when "UInt32"
897
- "unsigned_integer32"
898
- when "UInt64"
899
- "unsigned_integer64"
900
- when "Float"
901
- "float"
902
- when "Time"
903
- "time"
904
- when "ShortText"
905
- "short_text"
906
- when "Text"
907
- "text"
908
- when "LongText"
909
- "long_text"
910
- else
911
- raise ArgumentError, "unsupported column: #{column.inspect}"
912
- end
913
- end
914
- end
915
- end
916
- end