rroonga 0.9.2-x86-mingw32

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 (199) hide show
  1. data/AUTHORS +5 -0
  2. data/NEWS.ja.rdoc +114 -0
  3. data/NEWS.rdoc +116 -0
  4. data/README.ja.rdoc +65 -0
  5. data/README.rdoc +66 -0
  6. data/Rakefile +206 -0
  7. data/benchmark/common.rb +49 -0
  8. data/benchmark/read-write-many-small-items.rb +144 -0
  9. data/benchmark/write-many-small-items.rb +135 -0
  10. data/example/bookmark.rb +161 -0
  11. data/example/index-html.rb +89 -0
  12. data/example/search/config.ru +230 -0
  13. data/example/search/public/css/groonga.css +122 -0
  14. data/ext/.gitignore +2 -0
  15. data/ext/groonga/extconf.rb +93 -0
  16. data/ext/groonga/rb-grn-accessor.c +52 -0
  17. data/ext/groonga/rb-grn-array-cursor.c +36 -0
  18. data/ext/groonga/rb-grn-array.c +210 -0
  19. data/ext/groonga/rb-grn-column.c +573 -0
  20. data/ext/groonga/rb-grn-context.c +662 -0
  21. data/ext/groonga/rb-grn-database.c +472 -0
  22. data/ext/groonga/rb-grn-encoding-support.c +64 -0
  23. data/ext/groonga/rb-grn-encoding.c +257 -0
  24. data/ext/groonga/rb-grn-exception.c +1110 -0
  25. data/ext/groonga/rb-grn-expression-builder.c +75 -0
  26. data/ext/groonga/rb-grn-expression.c +731 -0
  27. data/ext/groonga/rb-grn-fix-size-column.c +166 -0
  28. data/ext/groonga/rb-grn-hash-cursor.c +38 -0
  29. data/ext/groonga/rb-grn-hash.c +294 -0
  30. data/ext/groonga/rb-grn-index-column.c +488 -0
  31. data/ext/groonga/rb-grn-logger.c +504 -0
  32. data/ext/groonga/rb-grn-object.c +1369 -0
  33. data/ext/groonga/rb-grn-operation.c +198 -0
  34. data/ext/groonga/rb-grn-patricia-trie-cursor.c +39 -0
  35. data/ext/groonga/rb-grn-patricia-trie.c +488 -0
  36. data/ext/groonga/rb-grn-procedure.c +52 -0
  37. data/ext/groonga/rb-grn-query.c +260 -0
  38. data/ext/groonga/rb-grn-record.c +40 -0
  39. data/ext/groonga/rb-grn-snippet.c +334 -0
  40. data/ext/groonga/rb-grn-table-cursor-key-support.c +69 -0
  41. data/ext/groonga/rb-grn-table-cursor.c +247 -0
  42. data/ext/groonga/rb-grn-table-key-support.c +714 -0
  43. data/ext/groonga/rb-grn-table.c +1977 -0
  44. data/ext/groonga/rb-grn-type.c +181 -0
  45. data/ext/groonga/rb-grn-utils.c +769 -0
  46. data/ext/groonga/rb-grn-variable-size-column.c +36 -0
  47. data/ext/groonga/rb-grn-variable.c +108 -0
  48. data/ext/groonga/rb-grn-view-accessor.c +53 -0
  49. data/ext/groonga/rb-grn-view-cursor.c +35 -0
  50. data/ext/groonga/rb-grn-view-record.c +41 -0
  51. data/ext/groonga/rb-grn-view.c +421 -0
  52. data/ext/groonga/rb-grn.h +698 -0
  53. data/ext/groonga/rb-groonga.c +107 -0
  54. data/extconf.rb +130 -0
  55. data/html/bar.svg +153 -0
  56. data/html/developer.html +117 -0
  57. data/html/developer.svg +469 -0
  58. data/html/download.svg +253 -0
  59. data/html/favicon.ico +0 -0
  60. data/html/favicon.xcf +0 -0
  61. data/html/footer.html.erb +28 -0
  62. data/html/head.html.erb +4 -0
  63. data/html/header.html.erb +17 -0
  64. data/html/index.html +147 -0
  65. data/html/install.svg +636 -0
  66. data/html/logo.xcf +0 -0
  67. data/html/ranguba.css +250 -0
  68. data/html/tutorial.svg +559 -0
  69. data/lib/1.8/groonga.so +0 -0
  70. data/lib/1.9/groonga.so +0 -0
  71. data/lib/groonga.rb +90 -0
  72. data/lib/groonga/context.rb +184 -0
  73. data/lib/groonga/expression-builder.rb +324 -0
  74. data/lib/groonga/patricia-trie.rb +85 -0
  75. data/lib/groonga/record.rb +311 -0
  76. data/lib/groonga/schema.rb +1191 -0
  77. data/lib/groonga/view-record.rb +56 -0
  78. data/license/GPL +340 -0
  79. data/license/LGPL +504 -0
  80. data/license/RUBY +59 -0
  81. data/misc/grnop2ruby.rb +49 -0
  82. data/pkg-config.rb +333 -0
  83. data/rroonga-build.rb +57 -0
  84. data/test-unit/Rakefile +40 -0
  85. data/test-unit/TODO +5 -0
  86. data/test-unit/bin/testrb +5 -0
  87. data/test-unit/html/classic.html +15 -0
  88. data/test-unit/html/index.html +25 -0
  89. data/test-unit/html/index.html.ja +27 -0
  90. data/test-unit/lib/test/unit.rb +323 -0
  91. data/test-unit/lib/test/unit/assertionfailederror.rb +25 -0
  92. data/test-unit/lib/test/unit/assertions.rb +1230 -0
  93. data/test-unit/lib/test/unit/attribute.rb +125 -0
  94. data/test-unit/lib/test/unit/autorunner.rb +360 -0
  95. data/test-unit/lib/test/unit/collector.rb +36 -0
  96. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  97. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  98. data/test-unit/lib/test/unit/collector/load.rb +144 -0
  99. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  100. data/test-unit/lib/test/unit/color-scheme.rb +102 -0
  101. data/test-unit/lib/test/unit/color.rb +96 -0
  102. data/test-unit/lib/test/unit/diff.rb +724 -0
  103. data/test-unit/lib/test/unit/error.rb +130 -0
  104. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  105. data/test-unit/lib/test/unit/failure.rb +136 -0
  106. data/test-unit/lib/test/unit/fixture.rb +176 -0
  107. data/test-unit/lib/test/unit/notification.rb +129 -0
  108. data/test-unit/lib/test/unit/omission.rb +191 -0
  109. data/test-unit/lib/test/unit/pending.rb +150 -0
  110. data/test-unit/lib/test/unit/priority.rb +180 -0
  111. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  112. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  113. data/test-unit/lib/test/unit/runner/tap.rb +8 -0
  114. data/test-unit/lib/test/unit/testcase.rb +476 -0
  115. data/test-unit/lib/test/unit/testresult.rb +89 -0
  116. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  117. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  118. data/test-unit/lib/test/unit/ui/console/testrunner.rb +466 -0
  119. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +63 -0
  120. data/test-unit/lib/test/unit/ui/tap/testrunner.rb +92 -0
  121. data/test-unit/lib/test/unit/ui/testrunner.rb +28 -0
  122. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  123. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  124. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  125. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  126. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  127. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  128. data/test-unit/lib/test/unit/version.rb +7 -0
  129. data/test-unit/sample/adder.rb +13 -0
  130. data/test-unit/sample/subtracter.rb +12 -0
  131. data/test-unit/sample/test_adder.rb +20 -0
  132. data/test-unit/sample/test_subtracter.rb +20 -0
  133. data/test-unit/sample/test_user.rb +23 -0
  134. data/test-unit/test/collector/test-descendant.rb +133 -0
  135. data/test-unit/test/collector/test-load.rb +442 -0
  136. data/test-unit/test/collector/test_dir.rb +406 -0
  137. data/test-unit/test/collector/test_objectspace.rb +100 -0
  138. data/test-unit/test/run-test.rb +15 -0
  139. data/test-unit/test/test-attribute.rb +86 -0
  140. data/test-unit/test/test-color-scheme.rb +67 -0
  141. data/test-unit/test/test-color.rb +47 -0
  142. data/test-unit/test/test-diff.rb +518 -0
  143. data/test-unit/test/test-emacs-runner.rb +60 -0
  144. data/test-unit/test/test-fixture.rb +287 -0
  145. data/test-unit/test/test-notification.rb +33 -0
  146. data/test-unit/test/test-omission.rb +81 -0
  147. data/test-unit/test/test-pending.rb +70 -0
  148. data/test-unit/test/test-priority.rb +119 -0
  149. data/test-unit/test/test-testcase.rb +544 -0
  150. data/test-unit/test/test_assertions.rb +1151 -0
  151. data/test-unit/test/test_error.rb +26 -0
  152. data/test-unit/test/test_failure.rb +33 -0
  153. data/test-unit/test/test_testresult.rb +113 -0
  154. data/test-unit/test/test_testsuite.rb +129 -0
  155. data/test-unit/test/testunit-test-util.rb +14 -0
  156. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  157. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  158. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  159. data/test-unit/test/util/test_observable.rb +102 -0
  160. data/test-unit/test/util/test_procwrapper.rb +36 -0
  161. data/test/.gitignore +1 -0
  162. data/test/groonga-test-utils.rb +134 -0
  163. data/test/run-test.rb +58 -0
  164. data/test/test-array.rb +90 -0
  165. data/test/test-column.rb +316 -0
  166. data/test/test-context-select.rb +93 -0
  167. data/test/test-context.rb +73 -0
  168. data/test/test-database.rb +113 -0
  169. data/test/test-encoding.rb +33 -0
  170. data/test/test-exception.rb +93 -0
  171. data/test/test-expression-builder.rb +217 -0
  172. data/test/test-expression.rb +134 -0
  173. data/test/test-fix-size-column.rb +65 -0
  174. data/test/test-gqtp.rb +72 -0
  175. data/test/test-hash.rb +305 -0
  176. data/test/test-index-column.rb +81 -0
  177. data/test/test-logger.rb +37 -0
  178. data/test/test-patricia-trie.rb +205 -0
  179. data/test/test-procedure.rb +37 -0
  180. data/test/test-query.rb +22 -0
  181. data/test/test-record.rb +243 -0
  182. data/test/test-remote.rb +54 -0
  183. data/test/test-schema-view.rb +90 -0
  184. data/test/test-schema.rb +459 -0
  185. data/test/test-snippet.rb +130 -0
  186. data/test/test-table-cursor.rb +153 -0
  187. data/test/test-table-offset-and-limit.rb +102 -0
  188. data/test/test-table-select-normalize.rb +53 -0
  189. data/test/test-table-select.rb +150 -0
  190. data/test/test-table.rb +594 -0
  191. data/test/test-type.rb +71 -0
  192. data/test/test-variable-size-column.rb +98 -0
  193. data/test/test-variable.rb +28 -0
  194. data/test/test-vector-column.rb +76 -0
  195. data/test/test-version.rb +31 -0
  196. data/test/test-view.rb +72 -0
  197. data/text/TUTORIAL.ja.rdoc +392 -0
  198. data/text/expression.rdoc +284 -0
  199. metadata +276 -0
@@ -0,0 +1,662 @@
1
+ /* -*- c-file-style: "ruby" -*- */
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
+ #include "rb-grn.h"
20
+
21
+ #define SELF(object) (RVAL2GRNCONTEXT(object))
22
+
23
+ static VALUE cGrnContext;
24
+
25
+ /*
26
+ * Document-class: Groonga::Context
27
+ *
28
+ * groonga全体に渡る情報を管理するオブジェクト。通常のアプリ
29
+ * ケーションでは1つのコンテキストを作成し、それを利用する。
30
+ * 複数のコンテキストを利用する必要はない。
31
+ *
32
+ * デフォルトで使用されるコンテキストは
33
+ * Groonga::Context#defaultでアクセスできる。コンテキストを
34
+ * 指定できる箇所でコンテキストの指定を省略したり+nil+を指定
35
+ * した場合はGroonga::Context.defaultが利用される。
36
+ *
37
+ * また、デフォルトのコンテキストは必要になると暗黙のうちに
38
+ * 作成される。そのため、コンテキストを意識することは少ない。
39
+ *
40
+ * 暗黙のうちに作成されるコンテキストにオプションを指定する
41
+ * 場合はGroonga::Context.default_options=を使用する。
42
+ */
43
+
44
+ grn_ctx *
45
+ rb_grn_context_from_ruby_object (VALUE object)
46
+ {
47
+ RbGrnContext *rb_grn_context;
48
+
49
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(object, cGrnContext))) {
50
+ rb_raise(rb_eTypeError, "not a groonga context");
51
+ }
52
+
53
+ Data_Get_Struct(object, RbGrnContext, rb_grn_context);
54
+ if (!rb_grn_context)
55
+ rb_raise(rb_eGrnError, "groonga context is NULL");
56
+ return rb_grn_context->context;
57
+ }
58
+
59
+ void
60
+ rb_grn_context_fin (grn_ctx *context)
61
+ {
62
+ grn_obj *database;
63
+
64
+ if (context->stat == GRN_CTX_FIN)
65
+ return;
66
+
67
+ database = grn_ctx_db(context);
68
+ debug("context:database: %p:%p\n", context, database);
69
+ if (database && database->header.type == GRN_DB) {
70
+ grn_obj_unlink(context, database);
71
+ }
72
+ grn_ctx_fin(context);
73
+ }
74
+
75
+ static void
76
+ rb_grn_context_free (void *pointer)
77
+ {
78
+ RbGrnContext *rb_grn_context = pointer;
79
+ grn_ctx *context;
80
+
81
+ context = rb_grn_context->context;
82
+ debug("context-free: %p\n", context);
83
+ if (!rb_grn_exited)
84
+ rb_grn_context_fin(context);
85
+ debug("context-free: %p: done\n", context);
86
+ xfree(rb_grn_context);
87
+ }
88
+
89
+ static VALUE
90
+ rb_grn_context_alloc (VALUE klass)
91
+ {
92
+ return Data_Wrap_Struct(klass, NULL, rb_grn_context_free, NULL);
93
+ }
94
+
95
+ VALUE
96
+ rb_grn_context_to_exception (grn_ctx *context, VALUE related_object)
97
+ {
98
+ VALUE exception, exception_class;
99
+ const char *message;
100
+ grn_obj bulk;
101
+
102
+ if (context->rc == GRN_SUCCESS)
103
+ return Qnil;
104
+
105
+ exception_class = rb_grn_rc_to_exception(context->rc);
106
+ message = rb_grn_rc_to_message(context->rc);
107
+
108
+ GRN_OBJ_INIT(&bulk, GRN_BULK, 0, GRN_ID_NIL);
109
+ GRN_TEXT_PUTS(context, &bulk, message);
110
+ GRN_TEXT_PUTS(context, &bulk, ": ");
111
+ GRN_TEXT_PUTS(context, &bulk, context->errbuf);
112
+ if (!NIL_P(related_object)) {
113
+ GRN_TEXT_PUTS(context, &bulk, ": ");
114
+ GRN_TEXT_PUTS(context, &bulk, rb_grn_inspect(related_object));
115
+ }
116
+ GRN_TEXT_PUTS(context, &bulk, "\n");
117
+ GRN_TEXT_PUTS(context, &bulk, context->errfile);
118
+ GRN_TEXT_PUTS(context, &bulk, ":");
119
+ grn_text_itoa(context, &bulk, context->errline);
120
+ GRN_TEXT_PUTS(context, &bulk, ": ");
121
+ GRN_TEXT_PUTS(context, &bulk, context->errfunc);
122
+ GRN_TEXT_PUTS(context, &bulk, "()");
123
+ exception = rb_funcall(exception_class, rb_intern("new"), 1,
124
+ rb_str_new(GRN_BULK_HEAD(&bulk),
125
+ GRN_BULK_VSIZE(&bulk)));
126
+ grn_obj_unlink(context, &bulk);
127
+
128
+ return exception;
129
+ }
130
+
131
+ void
132
+ rb_grn_context_check (grn_ctx *context, VALUE related_object)
133
+ {
134
+ VALUE exception;
135
+
136
+ exception = rb_grn_context_to_exception(context, related_object);
137
+ if (NIL_P(exception))
138
+ return;
139
+
140
+ rb_exc_raise(exception);
141
+ }
142
+
143
+ grn_ctx *
144
+ rb_grn_context_ensure (VALUE *context)
145
+ {
146
+ if (NIL_P(*context))
147
+ *context = rb_grn_context_get_default();
148
+ return SELF(*context);
149
+ }
150
+
151
+ VALUE
152
+ rb_grn_context_rb_string_new (grn_ctx *context, const char *string, long length)
153
+ {
154
+ if (length < 0)
155
+ length = strlen(string);
156
+ #ifdef HAVE_RUBY_ENCODING_H
157
+ return rb_enc_str_new(string, length,
158
+ rb_grn_encoding_to_ruby_encoding(context->encoding));
159
+ #else
160
+ return rb_str_new(string, length);
161
+ #endif
162
+ }
163
+
164
+ VALUE
165
+ rb_grn_context_rb_string_encode (grn_ctx *context, VALUE rb_string)
166
+ {
167
+ #ifdef HAVE_RUBY_ENCODING_H
168
+ rb_encoding *encoding, *to_encode;
169
+
170
+ encoding = rb_enc_get(rb_string);
171
+ to_encode = rb_grn_encoding_to_ruby_encoding(context->encoding);
172
+ if (rb_enc_to_index(encoding) != rb_enc_to_index(to_encode))
173
+ rb_string = rb_str_encode(rb_string, rb_enc_from_encoding(to_encode),
174
+ 0, Qnil);
175
+ #endif
176
+ return rb_string;
177
+ }
178
+
179
+ void
180
+ rb_grn_context_text_set (grn_ctx *context, grn_obj *bulk, VALUE rb_string)
181
+ {
182
+ rb_string = rb_grn_context_rb_string_encode(context, rb_string);
183
+ GRN_TEXT_SET(context, bulk, RSTRING_PTR(rb_string), RSTRING_LEN(rb_string));
184
+ }
185
+
186
+ /*
187
+ * call-seq:
188
+ * Groonga::Context.default -> Groonga::Context
189
+ *
190
+ * デフォルトのコンテキストを返す。デフォルトのコンテキスト
191
+ * が作成されていない場合は暗黙のうちに作成し、それを返す。
192
+ *
193
+ * 暗黙のうちにコンテキストを作成する場合は、
194
+ * Groonga::Context.default_optionsに設定されているオプショ
195
+ * ンを利用する。
196
+ */
197
+ static VALUE
198
+ rb_grn_context_s_get_default (VALUE self)
199
+ {
200
+ VALUE context;
201
+
202
+ context = rb_cv_get(self, "@@default");
203
+ if (NIL_P(context)) {
204
+ context = rb_funcall(cGrnContext, rb_intern("new"), 0);
205
+ rb_cv_set(self, "@@default", context);
206
+ }
207
+ return context;
208
+ }
209
+
210
+ VALUE
211
+ rb_grn_context_get_default (void)
212
+ {
213
+ return rb_grn_context_s_get_default(cGrnContext);
214
+ }
215
+
216
+ /*
217
+ * call-seq:
218
+ * Groonga::Context.default=(context)
219
+ *
220
+ * デフォルトのコンテキストを設定する。+nil+を指定すると、デ
221
+ * フォルトのコンテキストをリセットする。リセットすると、次
222
+ * 回Groonga::Context.defaultを呼び出したときに新しくコンテ
223
+ * キストが作成される。
224
+ */
225
+ static VALUE
226
+ rb_grn_context_s_set_default (VALUE self, VALUE context)
227
+ {
228
+ rb_cv_set(self, "@@default", context);
229
+ return Qnil;
230
+ }
231
+
232
+ /*
233
+ * call-seq:
234
+ * Groonga::Context.default_options -> Hash or nil
235
+ *
236
+ * コンテキストを作成する時に利用するデフォルトのオプション
237
+ * を返す。
238
+ */
239
+ static VALUE
240
+ rb_grn_context_s_get_default_options (VALUE self)
241
+ {
242
+ return rb_cv_get(self, "@@default_options");
243
+ }
244
+
245
+ /*
246
+ * call-seq:
247
+ * Groonga::Context.default_options=(options)
248
+ *
249
+ * コンテキストを作成する時に利用するデフォルトのオプション
250
+ * を設定する。利用可能なオプションは
251
+ * Groonga::Context.newを参照。
252
+ */
253
+ static VALUE
254
+ rb_grn_context_s_set_default_options (VALUE self, VALUE options)
255
+ {
256
+ rb_cv_set(self, "@@default_options", options);
257
+ return Qnil;
258
+ }
259
+
260
+ /*
261
+ * call-seq:
262
+ * Groonga::Context.new(options=nil)
263
+ *
264
+ * コンテキストを作成する。_options_に指定可能な値は以下の通
265
+ * り。
266
+ *
267
+ * [+:encoding+]
268
+ * エンコーディングを指定する。エンコーディングの指定方法
269
+ * はGroonga::Encodingを参照。
270
+ */
271
+ static VALUE
272
+ rb_grn_context_initialize (int argc, VALUE *argv, VALUE self)
273
+ {
274
+ RbGrnContext *rb_grn_context;
275
+ grn_ctx *context;
276
+ int flags = 0;
277
+ VALUE options, default_options;
278
+ VALUE rb_encoding;
279
+
280
+ rb_scan_args(argc, argv, "01", &options);
281
+ default_options = rb_grn_context_s_get_default_options(rb_obj_class(self));
282
+ if (NIL_P(default_options))
283
+ default_options = rb_hash_new();
284
+
285
+ if (NIL_P(options))
286
+ options = rb_hash_new();
287
+ options = rb_funcall(default_options, rb_intern("merge"), 1, options);
288
+
289
+ rb_grn_scan_options(options,
290
+ "encoding", &rb_encoding,
291
+ NULL);
292
+
293
+ rb_grn_context = ALLOC(RbGrnContext);
294
+ DATA_PTR(self) = rb_grn_context;
295
+ rb_grn_context->self = self;
296
+ context = rb_grn_context->context = grn_ctx_open(flags);
297
+ rb_grn_context_check(context, self);
298
+
299
+ GRN_CTX_USER_DATA(context)->ptr = rb_grn_context;
300
+
301
+ if (!NIL_P(rb_encoding)) {
302
+ grn_encoding encoding;
303
+
304
+ encoding = RVAL2GRNENCODING(rb_encoding, NULL);
305
+ GRN_CTX_SET_ENCODING(context, encoding);
306
+ }
307
+
308
+ debug("context new: %p\n", context);
309
+
310
+ return Qnil;
311
+ }
312
+
313
+ /*
314
+ * call-seq:
315
+ * context.inspect -> String
316
+ *
317
+ * コンテキストの中身を人に見やすい文字列で返す。
318
+ */
319
+ static VALUE
320
+ rb_grn_context_inspect (VALUE self)
321
+ {
322
+ VALUE inspected;
323
+ grn_ctx *context;
324
+ grn_obj *database;
325
+ VALUE rb_database;
326
+
327
+ context = SELF(self);
328
+
329
+ inspected = rb_str_new2("#<");
330
+ rb_str_concat(inspected, rb_inspect(rb_obj_class(self)));
331
+ rb_str_cat2(inspected, " ");
332
+
333
+ rb_str_cat2(inspected, "encoding: <");
334
+ rb_str_concat(inspected, rb_inspect(GRNENCODING2RVAL(context->encoding)));
335
+ rb_str_cat2(inspected, ">, ");
336
+
337
+ rb_str_cat2(inspected, "database: <");
338
+ database = grn_ctx_db(context);
339
+ rb_database = GRNDB2RVAL(context, database, RB_GRN_FALSE);
340
+ rb_str_concat(inspected, rb_inspect(rb_database));
341
+ rb_str_cat2(inspected, ">");
342
+
343
+ rb_str_cat2(inspected, ">");
344
+ return inspected;
345
+ }
346
+
347
+ /*
348
+ * call-seq:
349
+ * context.encoding -> Groonga::Encoding
350
+ *
351
+ * コンテキストが使うエンコーディングを返す。
352
+ */
353
+ static VALUE
354
+ rb_grn_context_get_encoding (VALUE self)
355
+ {
356
+ return GRNENCODING2RVAL(GRN_CTX_GET_ENCODING(SELF(self)));
357
+ }
358
+
359
+ /*
360
+ * call-seq:
361
+ * context.encoding=(encoding)
362
+ *
363
+ * コンテキストが使うエンコーディングを設定する。エンコーディ
364
+ * ングの指定のしかたはGroonga::Encodingを参照。
365
+ */
366
+ static VALUE
367
+ rb_grn_context_set_encoding (VALUE self, VALUE rb_encoding)
368
+ {
369
+ grn_ctx *context;
370
+ grn_encoding encoding;
371
+
372
+ context = SELF(self);
373
+ encoding = RVAL2GRNENCODING(rb_encoding, NULL);
374
+ GRN_CTX_SET_ENCODING(context, encoding);
375
+
376
+ return rb_encoding;
377
+ }
378
+
379
+ /*
380
+ * call-seq:
381
+ * context.database -> Groonga::Database
382
+ *
383
+ * コンテキストが使うデータベースを返す。
384
+ */
385
+ static VALUE
386
+ rb_grn_context_get_database (VALUE self)
387
+ {
388
+ grn_ctx *context;
389
+
390
+ context = SELF(self);
391
+ return GRNDB2RVAL(context, grn_ctx_db(context), RB_GRN_FALSE);
392
+ }
393
+
394
+ /*
395
+ * call-seq:
396
+ * context.connect(options=nil)
397
+ *
398
+ * groongaサーバに接続する。_options_に指定可能な値は以下の通
399
+ * り。
400
+ *
401
+ * [+:host+]
402
+ * groongaサーバのホスト名。またはIPアドレス。省略すると
403
+ * "localhost"に接続する。
404
+ *
405
+ * [+:port+]
406
+ * groongaサーバのポート番号。省略すると10041番ポートに接
407
+ * 続する。
408
+ */
409
+ static VALUE
410
+ rb_grn_context_connect (int argc, VALUE *argv, VALUE self)
411
+ {
412
+ grn_ctx *context;
413
+ char *host;
414
+ int port;
415
+ int flags = 0;
416
+ grn_rc rc;
417
+ VALUE options, rb_host, rb_port;
418
+
419
+ rb_scan_args(argc, argv, "01", &options);
420
+ rb_grn_scan_options(options,
421
+ "host", &rb_host,
422
+ "port", &rb_port,
423
+ NULL);
424
+
425
+ context = SELF(self);
426
+
427
+ if (NIL_P(rb_host)) {
428
+ host = "localhost";
429
+ } else {
430
+ host = StringValueCStr(rb_host);
431
+ }
432
+
433
+ if (NIL_P(rb_port)) {
434
+ port = 10041;
435
+ } else {
436
+ port = NUM2INT(rb_port);
437
+ }
438
+
439
+ rc = grn_ctx_connect(context, host, port, flags);
440
+ rb_grn_context_check(context, self);
441
+ rb_grn_rc_check(rc, self);
442
+
443
+ return Qnil;
444
+ }
445
+
446
+ /*
447
+ * call-seq:
448
+ * context.send(string) -> ID
449
+ *
450
+ * groongaサーバにクエリ文字列を送信する。
451
+ */
452
+ static VALUE
453
+ rb_grn_context_send (VALUE self, VALUE rb_string)
454
+ {
455
+ grn_ctx *context;
456
+ char *string;
457
+ unsigned int string_size;
458
+ int flags = 0;
459
+ unsigned int query_id;
460
+
461
+ context = SELF(self);
462
+ string = StringValuePtr(rb_string);
463
+ string_size = RSTRING_LEN(rb_string);
464
+ query_id = grn_ctx_send(context, string, string_size, flags);
465
+ rb_grn_context_check(context, self);
466
+
467
+ return UINT2NUM(query_id);
468
+ }
469
+
470
+ /*
471
+ * call-seq:
472
+ * context.receive -> [ID, String]
473
+ *
474
+ * groongaサーバからクエリ実行結果文字列を受信する。
475
+ */
476
+ static VALUE
477
+ rb_grn_context_receive (VALUE self)
478
+ {
479
+ grn_ctx *context;
480
+ char *string;
481
+ unsigned string_size;
482
+ int flags = 0;
483
+ unsigned int query_id;
484
+
485
+ context = SELF(self);
486
+ query_id = grn_ctx_recv(context, &string, &string_size, &flags);
487
+ rb_grn_context_check(context, self);
488
+
489
+ return rb_ary_new3(2, UINT2NUM(query_id), rb_str_new(string, string_size));
490
+ }
491
+
492
+ static const char *
493
+ grn_type_name_old_to_new (const char *name, unsigned int name_size)
494
+ {
495
+ unsigned int i;
496
+
497
+ for (i = 0; i < name_size; i++) {
498
+ if (name[i] == '\0')
499
+ return NULL;
500
+ }
501
+
502
+ if (strcmp(name, "<int>") == 0) {
503
+ return "Int32";
504
+ } else if (strcmp(name, "<uint>") == 0) {
505
+ return "UInt32";
506
+ } else if (strcmp(name, "<int64>") == 0) {
507
+ return "Int64";
508
+ } else if (strcmp(name, "<uint64>") == 0) {
509
+ return "UInt64";
510
+ } else if (strcmp(name, "<float>") == 0) {
511
+ return "Float";
512
+ } else if (strcmp(name, "<time>") == 0) {
513
+ return "Time";
514
+ } else if (strcmp(name, "<shorttext>") == 0) {
515
+ return "ShortText";
516
+ } else if (strcmp(name, "<text>") == 0) {
517
+ return "Text";
518
+ } else if (strcmp(name, "<longtext>") == 0) {
519
+ return "LongText";
520
+ } else if (strcmp(name, "<token:delimit>") == 0) {
521
+ return "TokenDelimit";
522
+ } else if (strcmp(name, "<token:unigram>") == 0) {
523
+ return "TokenUnigram";
524
+ } else if (strcmp(name, "<token:bigram>") == 0) {
525
+ return "TokenBigram";
526
+ } else if (strcmp(name, "<token:trigram>") == 0) {
527
+ return "TokenTrigram";
528
+ } else if (strcmp(name, "<token:mecab>") == 0) {
529
+ return "TokenMecab";
530
+ }
531
+
532
+ return NULL;
533
+ }
534
+
535
+ grn_obj *
536
+ rb_grn_context_get_backward_compatibility (grn_ctx *context,
537
+ const char *name,
538
+ unsigned int name_size)
539
+ {
540
+ grn_obj *object;
541
+
542
+ object = grn_ctx_get(context, name, name_size);
543
+ if (!object) {
544
+ const char *new_type_name;
545
+
546
+ new_type_name = grn_type_name_old_to_new(name, name_size);
547
+ if (new_type_name) {
548
+ object = grn_ctx_get(context, new_type_name, strlen(new_type_name));
549
+ #if 0
550
+ if (object) {
551
+ rb_warn("deprecated old data type name <%s> is used. "
552
+ "Use new data type name <%s> instead.",
553
+ name, new_type_name);
554
+ }
555
+ #endif
556
+ }
557
+ }
558
+
559
+ return object;
560
+ }
561
+
562
+
563
+ /*
564
+ * call-seq:
565
+ * context[name] -> Groonga::Object or nil
566
+ * context[id] -> Groonga::Object or nil
567
+ *
568
+ * コンテキスト管理下にあるオブジェクトを返す。
569
+ *
570
+ * _name_として文字列を指定した場合はオブジェクト名でオブジェ
571
+ * クトを検索する。
572
+ *
573
+ * _id_として数値を指定した場合はオブジェクトIDでオブジェク
574
+ * トを検索する。
575
+ */
576
+ static VALUE
577
+ rb_grn_context_array_reference (VALUE self, VALUE name_or_id)
578
+ {
579
+ grn_ctx *context;
580
+ grn_obj *object;
581
+ const char *name;
582
+ unsigned int name_size;
583
+ grn_id id;
584
+
585
+ context = SELF(self);
586
+ switch (TYPE(name_or_id)) {
587
+ case T_SYMBOL:
588
+ name = rb_id2name(SYM2ID(name_or_id));
589
+ name_size = strlen(name);
590
+ object = rb_grn_context_get_backward_compatibility(context,
591
+ name, name_size);
592
+ break;
593
+ case T_STRING:
594
+ name = StringValuePtr(name_or_id);
595
+ name_size = RSTRING_LEN(name_or_id);
596
+ object = rb_grn_context_get_backward_compatibility(context,
597
+ name, name_size);
598
+ break;
599
+ case T_FIXNUM:
600
+ id = NUM2UINT(name_or_id);
601
+ object = grn_ctx_at(context, id);
602
+ break;
603
+ default:
604
+ rb_raise(rb_eArgError,
605
+ "should be String, Symbol or unsigned integer: %s",
606
+ rb_grn_inspect(name_or_id));
607
+ break;
608
+ }
609
+
610
+ return GRNOBJECT2RVAL(Qnil, context, object, RB_GRN_FALSE);
611
+ }
612
+
613
+ /*
614
+ * call-seq:
615
+ * context.pop -> 値
616
+ *
617
+ * コンテキスト内にあるスタックから値を取り出す。このスタッ
618
+ * クにはGroonga::Expression#executeの実行結果が格納される。
619
+ */
620
+ static VALUE
621
+ rb_grn_context_pop (VALUE self)
622
+ {
623
+ grn_ctx *context;
624
+ context = SELF(self);
625
+ return GRNOBJ2RVAL(Qnil, context, grn_ctx_pop(context), self);
626
+ }
627
+
628
+ void
629
+ rb_grn_init_context (VALUE mGrn)
630
+ {
631
+ cGrnContext = rb_define_class_under(mGrn, "Context", rb_cObject);
632
+ rb_define_alloc_func(cGrnContext, rb_grn_context_alloc);
633
+
634
+ rb_cv_set(cGrnContext, "@@default", Qnil);
635
+ rb_cv_set(cGrnContext, "@@default_options", Qnil);
636
+
637
+ rb_define_singleton_method(cGrnContext, "default",
638
+ rb_grn_context_s_get_default, 0);
639
+ rb_define_singleton_method(cGrnContext, "default=",
640
+ rb_grn_context_s_set_default, 1);
641
+ rb_define_singleton_method(cGrnContext, "default_options",
642
+ rb_grn_context_s_get_default_options, 0);
643
+ rb_define_singleton_method(cGrnContext, "default_options=",
644
+ rb_grn_context_s_set_default_options, 1);
645
+
646
+ rb_define_method(cGrnContext, "initialize", rb_grn_context_initialize, -1);
647
+
648
+ rb_define_method(cGrnContext, "inspect", rb_grn_context_inspect, 0);
649
+
650
+ rb_define_method(cGrnContext, "encoding", rb_grn_context_get_encoding, 0);
651
+ rb_define_method(cGrnContext, "encoding=", rb_grn_context_set_encoding, 1);
652
+
653
+ rb_define_method(cGrnContext, "database", rb_grn_context_get_database, 0);
654
+
655
+ rb_define_method(cGrnContext, "[]", rb_grn_context_array_reference, 1);
656
+
657
+ rb_define_method(cGrnContext, "pop", rb_grn_context_pop, 0);
658
+
659
+ rb_define_method(cGrnContext, "connect", rb_grn_context_connect, -1);
660
+ rb_define_method(cGrnContext, "send", rb_grn_context_send, 1);
661
+ rb_define_method(cGrnContext, "receive", rb_grn_context_receive, 0);
662
+ }