groonga 0.0.1

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 (148) hide show
  1. data/AUTHORS +1 -0
  2. data/NEWS.ja.rdoc +5 -0
  3. data/NEWS.rdoc +5 -0
  4. data/README.ja.rdoc +53 -0
  5. data/README.rdoc +54 -0
  6. data/Rakefile +209 -0
  7. data/TUTORIAL.ja.rdoc +160 -0
  8. data/benchmark/small-many-items.rb +175 -0
  9. data/example/bookmark.rb +38 -0
  10. data/ext/.gitignore +2 -0
  11. data/ext/rb-grn-accessor.c +52 -0
  12. data/ext/rb-grn-array-cursor.c +28 -0
  13. data/ext/rb-grn-array.c +168 -0
  14. data/ext/rb-grn-column.c +273 -0
  15. data/ext/rb-grn-context.c +333 -0
  16. data/ext/rb-grn-database.c +128 -0
  17. data/ext/rb-grn-encoding.c +163 -0
  18. data/ext/rb-grn-exception.c +1014 -0
  19. data/ext/rb-grn-hash-cursor.c +30 -0
  20. data/ext/rb-grn-hash.c +40 -0
  21. data/ext/rb-grn-logger.c +277 -0
  22. data/ext/rb-grn-object.c +985 -0
  23. data/ext/rb-grn-patricia-trie-cursor.c +30 -0
  24. data/ext/rb-grn-patricia-trie.c +40 -0
  25. data/ext/rb-grn-procedure.c +52 -0
  26. data/ext/rb-grn-query.c +207 -0
  27. data/ext/rb-grn-record.c +33 -0
  28. data/ext/rb-grn-snippet.c +274 -0
  29. data/ext/rb-grn-table-cursor-key-support.c +55 -0
  30. data/ext/rb-grn-table-cursor.c +294 -0
  31. data/ext/rb-grn-table-key-support.c +299 -0
  32. data/ext/rb-grn-table.c +706 -0
  33. data/ext/rb-grn-type.c +114 -0
  34. data/ext/rb-grn-utils.c +578 -0
  35. data/ext/rb-grn.h +346 -0
  36. data/ext/rb-groonga.c +98 -0
  37. data/extconf.rb +171 -0
  38. data/html/bar.svg +153 -0
  39. data/html/developer.html +121 -0
  40. data/html/developer.svg +469 -0
  41. data/html/download.svg +253 -0
  42. data/html/footer.html.erb +28 -0
  43. data/html/head.html.erb +4 -0
  44. data/html/header.html.erb +17 -0
  45. data/html/index.html +153 -0
  46. data/html/install.svg +636 -0
  47. data/html/logo.xcf +0 -0
  48. data/html/ranguba.css +248 -0
  49. data/html/tutorial.svg +559 -0
  50. data/lib/groonga.rb +50 -0
  51. data/lib/groonga/record.rb +98 -0
  52. data/license/GPL +340 -0
  53. data/license/LGPL +504 -0
  54. data/license/RUBY +59 -0
  55. data/pkg-config.rb +328 -0
  56. data/test-unit/Rakefile +35 -0
  57. data/test-unit/TODO +5 -0
  58. data/test-unit/bin/testrb +5 -0
  59. data/test-unit/html/classic.html +15 -0
  60. data/test-unit/html/index.html +25 -0
  61. data/test-unit/html/index.html.ja +27 -0
  62. data/test-unit/lib/test/unit.rb +342 -0
  63. data/test-unit/lib/test/unit/assertionfailederror.rb +14 -0
  64. data/test-unit/lib/test/unit/assertions.rb +1149 -0
  65. data/test-unit/lib/test/unit/attribute.rb +125 -0
  66. data/test-unit/lib/test/unit/autorunner.rb +306 -0
  67. data/test-unit/lib/test/unit/collector.rb +43 -0
  68. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  69. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  70. data/test-unit/lib/test/unit/collector/load.rb +135 -0
  71. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  72. data/test-unit/lib/test/unit/color-scheme.rb +86 -0
  73. data/test-unit/lib/test/unit/color.rb +96 -0
  74. data/test-unit/lib/test/unit/diff.rb +538 -0
  75. data/test-unit/lib/test/unit/error.rb +124 -0
  76. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  77. data/test-unit/lib/test/unit/failure.rb +110 -0
  78. data/test-unit/lib/test/unit/fixture.rb +176 -0
  79. data/test-unit/lib/test/unit/notification.rb +125 -0
  80. data/test-unit/lib/test/unit/omission.rb +143 -0
  81. data/test-unit/lib/test/unit/pending.rb +146 -0
  82. data/test-unit/lib/test/unit/priority.rb +161 -0
  83. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  84. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  85. data/test-unit/lib/test/unit/testcase.rb +360 -0
  86. data/test-unit/lib/test/unit/testresult.rb +89 -0
  87. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  88. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  89. data/test-unit/lib/test/unit/ui/console/testrunner.rb +220 -0
  90. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +49 -0
  91. data/test-unit/lib/test/unit/ui/testrunner.rb +20 -0
  92. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  93. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  94. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  95. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  96. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  97. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  98. data/test-unit/lib/test/unit/version.rb +7 -0
  99. data/test-unit/sample/adder.rb +13 -0
  100. data/test-unit/sample/subtracter.rb +12 -0
  101. data/test-unit/sample/tc_adder.rb +18 -0
  102. data/test-unit/sample/tc_subtracter.rb +18 -0
  103. data/test-unit/sample/test_user.rb +22 -0
  104. data/test-unit/sample/ts_examples.rb +7 -0
  105. data/test-unit/test/collector/test-descendant.rb +135 -0
  106. data/test-unit/test/collector/test-load.rb +333 -0
  107. data/test-unit/test/collector/test_dir.rb +406 -0
  108. data/test-unit/test/collector/test_objectspace.rb +98 -0
  109. data/test-unit/test/run-test.rb +13 -0
  110. data/test-unit/test/test-attribute.rb +86 -0
  111. data/test-unit/test/test-color-scheme.rb +56 -0
  112. data/test-unit/test/test-color.rb +47 -0
  113. data/test-unit/test/test-diff.rb +477 -0
  114. data/test-unit/test/test-emacs-runner.rb +60 -0
  115. data/test-unit/test/test-fixture.rb +287 -0
  116. data/test-unit/test/test-notification.rb +33 -0
  117. data/test-unit/test/test-omission.rb +81 -0
  118. data/test-unit/test/test-pending.rb +70 -0
  119. data/test-unit/test/test-priority.rb +119 -0
  120. data/test-unit/test/test_assertions.rb +1082 -0
  121. data/test-unit/test/test_error.rb +26 -0
  122. data/test-unit/test/test_failure.rb +33 -0
  123. data/test-unit/test/test_testcase.rb +478 -0
  124. data/test-unit/test/test_testresult.rb +113 -0
  125. data/test-unit/test/test_testsuite.rb +129 -0
  126. data/test-unit/test/testunit-test-util.rb +14 -0
  127. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  128. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  129. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  130. data/test-unit/test/util/test_observable.rb +102 -0
  131. data/test-unit/test/util/test_procwrapper.rb +36 -0
  132. data/test/.gitignore +1 -0
  133. data/test/groonga-test-utils.rb +90 -0
  134. data/test/run-test.rb +54 -0
  135. data/test/test-column.rb +190 -0
  136. data/test/test-context.rb +90 -0
  137. data/test/test-database.rb +62 -0
  138. data/test/test-encoding.rb +33 -0
  139. data/test/test-exception.rb +85 -0
  140. data/test/test-procedure.rb +35 -0
  141. data/test/test-query.rb +22 -0
  142. data/test/test-record.rb +188 -0
  143. data/test/test-snippet.rb +121 -0
  144. data/test/test-table-cursor.rb +51 -0
  145. data/test/test-table.rb +447 -0
  146. data/test/test-type.rb +52 -0
  147. data/test/test-version.rb +31 -0
  148. metadata +213 -0
@@ -0,0 +1,273 @@
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, context) (RVAL2GRNCOLUMN(object, context))
22
+
23
+ VALUE rb_cGrnColumn;
24
+ VALUE rb_cGrnFixSizeColumn;
25
+ VALUE rb_cGrnVarSizeColumn;
26
+ VALUE rb_cGrnIndexColumn;
27
+
28
+ grn_obj *
29
+ rb_grn_column_from_ruby_object (VALUE object, grn_ctx **context)
30
+ {
31
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnColumn))) {
32
+ rb_raise(rb_eTypeError, "not a groonga column");
33
+ }
34
+
35
+ return RVAL2GRNOBJECT(object, context);
36
+ }
37
+
38
+ VALUE
39
+ rb_grn_column_to_ruby_object (VALUE klass, grn_ctx *context, grn_obj *column,
40
+ rb_grn_boolean owner)
41
+ {
42
+ return GRNOBJECT2RVAL(klass, context, column, owner);
43
+ }
44
+
45
+ static VALUE
46
+ rb_grn_fix_size_column_array_set (VALUE self, VALUE rb_id, VALUE rb_value)
47
+ {
48
+ grn_ctx *context = NULL;
49
+ grn_obj *column;
50
+ grn_id range;
51
+ grn_obj *range_object = NULL;
52
+ grn_rc rc;
53
+ grn_id id;
54
+ grn_obj value;
55
+
56
+ column = SELF(self, &context);
57
+ id = NUM2UINT(rb_id);
58
+
59
+ range = grn_obj_get_range(context, column);
60
+ if (range != GRN_ID_NIL)
61
+ range_object = grn_ctx_get(context, range);
62
+
63
+ if (RVAL2CBOOL(rb_obj_is_kind_of(rb_value, rb_cGrnRecord))) {
64
+ VALUE rb_id, rb_table;
65
+ grn_obj *table;
66
+
67
+ if (!range_object)
68
+ rb_raise(rb_eArgError,
69
+ "%s isn't associated with any table: %s",
70
+ rb_grn_inspect(self), rb_grn_inspect(rb_value));
71
+
72
+ rb_id = rb_funcall(rb_value, rb_intern("id"), 0);
73
+ rb_table = rb_funcall(rb_value, rb_intern("table"), 0);
74
+ table = RVAL2GRNTABLE(rb_table, &context);
75
+ if (grn_obj_id(context, table) != range)
76
+ rb_raise(rb_eArgError,
77
+ "%s isn't associated with passed record's table: %s",
78
+ rb_grn_inspect(self),
79
+ rb_grn_inspect(rb_value));
80
+
81
+ RVAL2GRNUVECTOR(rb_ary_new3(1, rb_id), context, &value);
82
+ } else if (range_object &&
83
+ RVAL2CBOOL(rb_obj_is_kind_of(rb_value, rb_cInteger))) {
84
+ switch (range_object->header.type) {
85
+ case GRN_TABLE_PAT_KEY:
86
+ case GRN_TABLE_HASH_KEY:
87
+ case GRN_TABLE_NO_KEY:
88
+ RVAL2GRNUVECTOR(rb_ary_new3(1, rb_value), context, &value);
89
+ break;
90
+ default:
91
+ RVAL2GRNBULK(rb_value, context, &value);
92
+ break;
93
+ }
94
+ } else {
95
+ RVAL2GRNBULK(rb_value, context, &value);
96
+ }
97
+
98
+ rc = grn_obj_set_value(context, column, id, &value, GRN_OBJ_SET);
99
+ grn_obj_close(context, &value);
100
+ rb_grn_rc_check(rc, self);
101
+
102
+ return Qnil;
103
+ }
104
+
105
+ static VALUE
106
+ rb_grn_index_column_array_set (VALUE self, VALUE rb_id, VALUE rb_value)
107
+ {
108
+ grn_ctx *context = NULL;
109
+ grn_obj *column;
110
+ grn_rc rc;
111
+ grn_id id;
112
+ unsigned int section;
113
+ grn_obj old_value, new_value;
114
+ VALUE rb_section, rb_old_value, rb_new_value;
115
+
116
+ column = SELF(self, &context);
117
+
118
+ id = NUM2UINT(rb_id);
119
+
120
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_value, rb_cHash))) {
121
+ VALUE hash_value;
122
+ hash_value = rb_hash_new();
123
+ rb_hash_aset(hash_value, RB_GRN_INTERN("value"), rb_value);
124
+ rb_value = hash_value;
125
+ }
126
+
127
+ rb_grn_scan_options(rb_value,
128
+ "section", &rb_section,
129
+ "old_value", &rb_old_value,
130
+ "value", &rb_new_value,
131
+ NULL);
132
+
133
+ if (NIL_P(rb_section))
134
+ section = 1;
135
+ else
136
+ section = NUM2UINT(rb_section);
137
+
138
+ RVAL2GRNBULK(rb_old_value, context, &old_value);
139
+ RVAL2GRNBULK(rb_new_value, context, &new_value);
140
+
141
+ rc = grn_column_index_update(context, column,
142
+ id, section, &old_value, &new_value);
143
+ grn_obj_close(context, &old_value);
144
+ grn_obj_close(context, &new_value);
145
+
146
+ rb_grn_rc_check(rc, self);
147
+
148
+ return Qnil;
149
+ }
150
+
151
+ static VALUE
152
+ rb_grn_column_get_table (VALUE self)
153
+ {
154
+ grn_ctx *context = NULL;
155
+ grn_obj *column;
156
+ grn_obj *table;
157
+
158
+ column = SELF(self, &context);
159
+ table = grn_column_table(context, column);
160
+ rb_grn_context_check(context, self);
161
+
162
+ return GRNOBJECT2RVAL(Qnil, context, table, RB_GRN_FALSE);
163
+ }
164
+
165
+ static VALUE
166
+ rb_grn_index_column_get_sources (VALUE self)
167
+ {
168
+ grn_ctx *context = NULL;
169
+ grn_obj *column;
170
+ grn_obj sources;
171
+ grn_id *source_ids;
172
+ VALUE rb_sources;
173
+ int i, n;
174
+
175
+ column = SELF(self, &context);
176
+
177
+ grn_obj_get_info(context, column, GRN_INFO_SOURCE, &sources);
178
+ rb_grn_context_check(context, self);
179
+
180
+ n = GRN_BULK_VSIZE(&sources) / sizeof(grn_id);
181
+ source_ids = (grn_id *)GRN_BULK_HEAD(&sources);
182
+ rb_sources = rb_ary_new2(n);
183
+ for (i = 0; i < n; i++) {
184
+ grn_obj *source;
185
+ VALUE rb_source;
186
+
187
+ source = grn_ctx_get(context, *source_ids);
188
+ rb_source = GRNOBJECT2RVAL(Qnil, context, source, RB_GRN_FALSE);
189
+ rb_ary_push(rb_sources, rb_source);
190
+ source_ids++;
191
+ }
192
+
193
+ return rb_sources;
194
+ }
195
+
196
+ static VALUE
197
+ rb_grn_index_column_set_sources (VALUE self, VALUE rb_sources)
198
+ {
199
+ grn_ctx *context = NULL;
200
+ grn_obj *column;
201
+ int i, n;
202
+ VALUE *rb_source_values;
203
+ grn_id *sources;
204
+ grn_rc rc;
205
+
206
+ column = SELF(self, &context);
207
+
208
+ n = RARRAY_LEN(rb_sources);
209
+ rb_source_values = RARRAY_PTR(rb_sources);
210
+ sources = ALLOCA_N(grn_id, n);
211
+ for (i = 0; i < n; i++) {
212
+ VALUE rb_source_id;
213
+ grn_obj *source;
214
+ grn_id source_id;
215
+
216
+ rb_source_id = rb_source_values[i];
217
+ if (CBOOL2RVAL(rb_obj_is_kind_of(rb_source_id, rb_cInteger))) {
218
+ source_id = NUM2UINT(rb_source_id);
219
+ } else {
220
+ source = RVAL2GRNOBJECT(rb_source_id, &context);
221
+ rb_grn_context_check(context, rb_source_id);
222
+ source_id = grn_obj_id(context, source);
223
+ }
224
+ sources[i] = source_id;
225
+ }
226
+
227
+ {
228
+ grn_obj source;
229
+ GRN_OBJ_INIT(&source, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY);
230
+ GRN_BULK_SET(context, &source, sources, n * sizeof(grn_id));
231
+ rc = grn_obj_set_info(context, column, GRN_INFO_SOURCE, &source);
232
+ }
233
+
234
+ rb_grn_context_check(context, self);
235
+ rb_grn_rc_check(rc, self);
236
+
237
+ return Qnil;
238
+ }
239
+
240
+ static VALUE
241
+ rb_grn_index_column_set_source (VALUE self, VALUE rb_source)
242
+ {
243
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_source, rb_cArray)))
244
+ rb_source = rb_ary_new3(1, rb_source);
245
+
246
+ return rb_grn_index_column_set_sources(self, rb_source);
247
+ }
248
+
249
+ void
250
+ rb_grn_init_column (VALUE mGrn)
251
+ {
252
+ rb_cGrnColumn = rb_define_class_under(mGrn, "Column", rb_cGrnObject);
253
+ rb_cGrnFixSizeColumn =
254
+ rb_define_class_under(mGrn, "FixSizeColumn", rb_cGrnColumn);
255
+ rb_cGrnVarSizeColumn =
256
+ rb_define_class_under(mGrn, "VarSizeColumn", rb_cGrnColumn);
257
+ rb_cGrnIndexColumn =
258
+ rb_define_class_under(mGrn, "IndexColumn", rb_cGrnColumn);
259
+
260
+ rb_define_method(rb_cGrnFixSizeColumn, "[]=",
261
+ rb_grn_fix_size_column_array_set, 2);
262
+ rb_define_method(rb_cGrnIndexColumn, "[]=",
263
+ rb_grn_index_column_array_set, 2);
264
+
265
+ rb_define_method(rb_cGrnColumn, "table", rb_grn_column_get_table, 0);
266
+
267
+ rb_define_method(rb_cGrnIndexColumn, "sources",
268
+ rb_grn_index_column_get_sources, 0);
269
+ rb_define_method(rb_cGrnIndexColumn, "sources=",
270
+ rb_grn_index_column_set_sources, 1);
271
+ rb_define_method(rb_cGrnIndexColumn, "source=",
272
+ rb_grn_index_column_set_source, 1);
273
+ }
@@ -0,0 +1,333 @@
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
+ grn_ctx *
26
+ rb_grn_context_from_ruby_object (VALUE object)
27
+ {
28
+ grn_ctx *context;
29
+
30
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(object, cGrnContext))) {
31
+ rb_raise(rb_eTypeError, "not a groonga context");
32
+ }
33
+
34
+ Data_Get_Struct(object, grn_ctx, context);
35
+ if (!context)
36
+ rb_raise(rb_eGrnError, "groonga context is NULL");
37
+ return context;
38
+ }
39
+
40
+ static void
41
+ rb_grn_context_free (void *pointer)
42
+ {
43
+ grn_ctx *context = pointer;
44
+
45
+ if (context->stat != GRN_CTX_FIN)
46
+ grn_ctx_fin(context);
47
+ xfree(context);
48
+ }
49
+
50
+ static VALUE
51
+ rb_grn_context_alloc (VALUE klass)
52
+ {
53
+ return Data_Wrap_Struct(klass, NULL, rb_grn_context_free, NULL);
54
+ }
55
+
56
+ VALUE
57
+ rb_grn_context_to_exception (grn_ctx *context, VALUE related_object)
58
+ {
59
+ VALUE exception, exception_class;
60
+ const char *message;
61
+ grn_obj bulk;
62
+
63
+ if (context->rc == GRN_SUCCESS)
64
+ return Qnil;
65
+
66
+ exception_class = rb_grn_rc_to_exception(context->rc);
67
+ message = rb_grn_rc_to_message(context->rc);
68
+
69
+ grn_bulk_init(context, &bulk, 0);
70
+ GRN_BULK_PUTS(context, &bulk, message);
71
+ GRN_BULK_PUTS(context, &bulk, ": ");
72
+ GRN_BULK_PUTS(context, &bulk, context->errbuf);
73
+ if (!NIL_P(related_object)) {
74
+ GRN_BULK_PUTS(context, &bulk, ": ");
75
+ GRN_BULK_PUTS(context, &bulk, rb_grn_inspect(related_object));
76
+ }
77
+ GRN_BULK_PUTS(context, &bulk, "\n");
78
+ GRN_BULK_PUTS(context, &bulk, context->errfile);
79
+ GRN_BULK_PUTS(context, &bulk, ":");
80
+ grn_bulk_itoa(context, &bulk, context->errline);
81
+ GRN_BULK_PUTS(context, &bulk, ": ");
82
+ GRN_BULK_PUTS(context, &bulk, context->errfunc);
83
+ GRN_BULK_PUTS(context, &bulk, "()");
84
+ exception = rb_funcall(exception_class, rb_intern("new"), 1,
85
+ rb_str_new(GRN_BULK_HEAD(&bulk),
86
+ GRN_BULK_VSIZE(&bulk)));
87
+ grn_obj_close(context, &bulk);
88
+
89
+ return exception;
90
+ }
91
+
92
+ void
93
+ rb_grn_context_check (grn_ctx *context, VALUE related_object)
94
+ {
95
+ VALUE exception;
96
+
97
+ exception = rb_grn_context_to_exception(context, related_object);
98
+ if (NIL_P(exception))
99
+ return;
100
+
101
+ rb_exc_raise(exception);
102
+ }
103
+
104
+ grn_ctx *
105
+ rb_grn_context_ensure (VALUE context)
106
+ {
107
+ if (NIL_P(context))
108
+ context = rb_grn_context_get_default();
109
+ return SELF(context);
110
+ }
111
+
112
+ static VALUE
113
+ rb_grn_context_s_get_default (VALUE self)
114
+ {
115
+ VALUE context;
116
+
117
+ context = rb_cv_get(self, "@@default");
118
+ if (NIL_P(context)) {
119
+ context = rb_funcall(cGrnContext, rb_intern("new"), 0);
120
+ rb_cv_set(self, "@@default", context);
121
+ }
122
+ return context;
123
+ }
124
+
125
+ VALUE
126
+ rb_grn_context_get_default (void)
127
+ {
128
+ return rb_grn_context_s_get_default(cGrnContext);
129
+ }
130
+
131
+ static VALUE
132
+ rb_grn_context_s_set_default (VALUE self, VALUE context)
133
+ {
134
+ rb_cv_set(self, "@@default", context);
135
+ return Qnil;
136
+ }
137
+
138
+ static VALUE
139
+ rb_grn_context_s_get_default_options (VALUE self)
140
+ {
141
+ return rb_cv_get(self, "@@default_options");
142
+ }
143
+
144
+ static VALUE
145
+ rb_grn_context_s_set_default_options (VALUE self, VALUE options)
146
+ {
147
+ rb_cv_set(self, "@@default_options", options);
148
+ return Qnil;
149
+ }
150
+
151
+ static VALUE
152
+ rb_grn_context_initialize (int argc, VALUE *argv, VALUE self)
153
+ {
154
+ grn_ctx *context;
155
+ int flags = 0;
156
+ grn_rc rc;
157
+ VALUE options, default_options;
158
+ VALUE use_ql, batch_mode, rb_encoding;
159
+
160
+ rb_scan_args(argc, argv, "01", &options);
161
+ default_options = rb_grn_context_s_get_default_options(rb_obj_class(self));
162
+ if (NIL_P(default_options))
163
+ default_options = rb_hash_new();
164
+
165
+ if (NIL_P(options))
166
+ options = rb_hash_new();
167
+ options = rb_funcall(default_options, rb_intern("merge"), 1, options);
168
+
169
+ rb_grn_scan_options(options,
170
+ "use_ql", &use_ql,
171
+ "batch_mode", &batch_mode,
172
+ "encoding", &rb_encoding,
173
+ NULL);
174
+
175
+ if (RVAL2CBOOL(use_ql))
176
+ flags |= GRN_CTX_USE_QL;
177
+ if (RVAL2CBOOL(batch_mode))
178
+ flags |= GRN_CTX_BATCH_MODE;
179
+
180
+ context = ALLOC(grn_ctx);
181
+ DATA_PTR(self) = context;
182
+ rc = grn_ctx_init(context, flags);
183
+ rb_grn_context_check(context, self);
184
+
185
+ if (!NIL_P(rb_encoding)) {
186
+ grn_encoding encoding;
187
+
188
+ encoding = RVAL2GRNENCODING(rb_encoding, NULL);
189
+ GRN_CTX_SET_ENCODING(context, encoding);
190
+ }
191
+
192
+ return Qnil;
193
+ }
194
+
195
+ static VALUE
196
+ rb_grn_context_inspect (VALUE self)
197
+ {
198
+ VALUE inspected;
199
+ grn_ctx *context;
200
+ grn_obj *database;
201
+ VALUE rb_database;
202
+
203
+ context = SELF(self);
204
+
205
+ inspected = rb_str_new2("#<");
206
+ rb_str_concat(inspected, rb_inspect(rb_obj_class(self)));
207
+ rb_str_cat2(inspected, " ");
208
+
209
+ rb_str_cat2(inspected, "use_ql: <");
210
+ if (context->flags & GRN_CTX_USE_QL)
211
+ rb_str_cat2(inspected, "true");
212
+ else
213
+ rb_str_cat2(inspected, "false");
214
+ rb_str_cat2(inspected, ">, ");
215
+
216
+ rb_str_cat2(inspected, "batch_mode: <");
217
+ if (context->flags & GRN_CTX_BATCH_MODE)
218
+ rb_str_cat2(inspected, "true");
219
+ else
220
+ rb_str_cat2(inspected, "false");
221
+ rb_str_cat2(inspected, ">, ");
222
+
223
+ rb_str_cat2(inspected, "encoding: <");
224
+ rb_str_concat(inspected, rb_inspect(GRNENCODING2RVAL(context->encoding)));
225
+ rb_str_cat2(inspected, ">, ");
226
+
227
+ rb_str_cat2(inspected, "database: <");
228
+ database = grn_ctx_db(context);
229
+ rb_database = GRNDB2RVAL(context, database, RB_GRN_FALSE);
230
+ rb_str_concat(inspected, rb_inspect(rb_database));
231
+ rb_str_cat2(inspected, ">");
232
+
233
+ rb_str_cat2(inspected, ">");
234
+ return inspected;
235
+ }
236
+
237
+ static VALUE
238
+ rb_grn_context_use_ql_p (VALUE self)
239
+ {
240
+ return CBOOL2RVAL(SELF(self)->flags & GRN_CTX_USE_QL);
241
+ }
242
+
243
+ static VALUE
244
+ rb_grn_context_batch_mode_p (VALUE self)
245
+ {
246
+ return CBOOL2RVAL(SELF(self)->flags & GRN_CTX_BATCH_MODE);
247
+ }
248
+
249
+ static VALUE
250
+ rb_grn_context_get_encoding (VALUE self)
251
+ {
252
+ return GRNENCODING2RVAL(GRN_CTX_GET_ENCODING(SELF(self)));
253
+ }
254
+
255
+ static VALUE
256
+ rb_grn_context_set_encoding (VALUE self, VALUE rb_encoding)
257
+ {
258
+ grn_ctx *context;
259
+ grn_encoding encoding;
260
+
261
+ context = SELF(self);
262
+ encoding = RVAL2GRNENCODING(rb_encoding, NULL);
263
+ GRN_CTX_SET_ENCODING(context, encoding);
264
+
265
+ return rb_encoding;
266
+ }
267
+
268
+ static VALUE
269
+ rb_grn_context_get_database (VALUE self)
270
+ {
271
+ grn_ctx *context;
272
+
273
+ context = SELF(self);
274
+ return GRNDB2RVAL(context, grn_ctx_db(context), RB_GRN_FALSE);
275
+ }
276
+
277
+ static VALUE
278
+ rb_grn_context_array_reference (VALUE self, VALUE name_or_id)
279
+ {
280
+ grn_ctx *context;
281
+ grn_obj *object;
282
+
283
+ context = SELF(self);
284
+ if (RVAL2CBOOL(rb_obj_is_kind_of(name_or_id, rb_cString))) {
285
+ const char *name;
286
+ unsigned name_size;
287
+
288
+ name = StringValuePtr(name_or_id);
289
+ name_size = RSTRING_LEN(name_or_id);
290
+ object = grn_ctx_lookup(context, name, name_size);
291
+ } else if (RVAL2CBOOL(rb_obj_is_kind_of(name_or_id, rb_cInteger))) {
292
+ unsigned id;
293
+ id = NUM2UINT(name_or_id);
294
+ object = grn_ctx_get(context, id);
295
+ } else {
296
+ rb_raise(rb_eArgError, "should be string or unsigned integer: %s",
297
+ rb_grn_inspect(name_or_id));
298
+ }
299
+
300
+ return GRNOBJECT2RVAL(Qnil, context, object, RB_GRN_FALSE);
301
+ }
302
+
303
+ void
304
+ rb_grn_init_context (VALUE mGrn)
305
+ {
306
+ cGrnContext = rb_define_class_under(mGrn, "Context", rb_cObject);
307
+ rb_define_alloc_func(cGrnContext, rb_grn_context_alloc);
308
+
309
+ rb_cv_set(cGrnContext, "@@default", Qnil);
310
+ rb_cv_set(cGrnContext, "@@default_options", Qnil);
311
+
312
+ rb_define_singleton_method(cGrnContext, "default",
313
+ rb_grn_context_s_get_default, 0);
314
+ rb_define_singleton_method(cGrnContext, "default=",
315
+ rb_grn_context_s_set_default, 1);
316
+ rb_define_singleton_method(cGrnContext, "default_options",
317
+ rb_grn_context_s_get_default_options, 0);
318
+ rb_define_singleton_method(cGrnContext, "default_options=",
319
+ rb_grn_context_s_set_default_options, 1);
320
+
321
+ rb_define_method(cGrnContext, "initialize", rb_grn_context_initialize, -1);
322
+
323
+ rb_define_method(cGrnContext, "inspect", rb_grn_context_inspect, 0);
324
+
325
+ rb_define_method(cGrnContext, "use_ql?", rb_grn_context_use_ql_p, 0);
326
+ rb_define_method(cGrnContext, "batch_mode?", rb_grn_context_batch_mode_p, 0);
327
+ rb_define_method(cGrnContext, "encoding", rb_grn_context_get_encoding, 0);
328
+ rb_define_method(cGrnContext, "encoding=", rb_grn_context_set_encoding, 1);
329
+
330
+ rb_define_method(cGrnContext, "database", rb_grn_context_get_database, 0);
331
+
332
+ rb_define_method(cGrnContext, "[]", rb_grn_context_array_reference, 1);
333
+ }