groonga 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,30 @@
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
+ VALUE rb_cGrnPatriciaTrieCursor;
22
+
23
+ void
24
+ rb_grn_init_patricia_trie_cursor (VALUE mGrn)
25
+ {
26
+ rb_cGrnPatriciaTrieCursor =
27
+ rb_define_class_under(mGrn, "PatriciaTrieCursor", rb_cGrnTableCursor);
28
+
29
+ rb_include_module(rb_cGrnHashCursor, rb_mGrnTableCursorKeySupport);
30
+ }
@@ -0,0 +1,40 @@
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) (RVAL2GRNTABLE(object))
22
+
23
+ VALUE rb_cGrnPatriciaTrie;
24
+
25
+ static VALUE
26
+ rb_grn_patricia_trie_s_create (int argc, VALUE *argv, VALUE self)
27
+ {
28
+ return rb_grn_table_s_create(argc, argv, self, GRN_TABLE_PAT_KEY);
29
+ }
30
+
31
+ void
32
+ rb_grn_init_patricia_trie (VALUE mGrn)
33
+ {
34
+ rb_cGrnPatriciaTrie =
35
+ rb_define_class_under(mGrn, "PatriciaTrie", rb_cGrnTable);
36
+
37
+ rb_include_module(rb_cGrnPatriciaTrie, rb_mGrnTableKeySupport);
38
+ rb_define_singleton_method(rb_cGrnPatriciaTrie, "create",
39
+ rb_grn_patricia_trie_s_create, -1);
40
+ }
@@ -0,0 +1,52 @@
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) (RVAL2GRNPROCEDURE(object))
22
+
23
+ VALUE rb_cGrnProcedure;
24
+
25
+ grn_obj *
26
+ rb_grn_procedure_from_ruby_object (VALUE object)
27
+ {
28
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnProcedure))) {
29
+ rb_raise(rb_eTypeError, "not a groonga procedure");
30
+ }
31
+
32
+ return RVAL2GRNOBJECT(object, NULL);
33
+ }
34
+
35
+ VALUE
36
+ rb_grn_procedure_to_ruby_object (grn_ctx *context, grn_obj *procedure,
37
+ rb_grn_boolean owner)
38
+ {
39
+ return GRNOBJECT2RVAL(rb_cGrnProcedure, context, procedure, owner);
40
+ }
41
+
42
+ void
43
+ rb_grn_init_procedure (VALUE mGrn)
44
+ {
45
+ rb_cGrnProcedure = rb_define_class_under(mGrn, "Procedure", rb_cGrnObject);
46
+
47
+ rb_define_const(rb_cGrnProcedure, "DELIMIT", INT2NUM(GRN_DB_DELIMIT));
48
+ rb_define_const(rb_cGrnProcedure, "UNIGRAM", INT2NUM(GRN_DB_UNIGRAM));
49
+ rb_define_const(rb_cGrnProcedure, "BIGRAM", INT2NUM(GRN_DB_BIGRAM));
50
+ rb_define_const(rb_cGrnProcedure, "TRIGRAM", INT2NUM(GRN_DB_TRIGRAM));
51
+ rb_define_const(rb_cGrnProcedure, "MECAB", INT2NUM(GRN_DB_MECAB));
52
+ }
@@ -0,0 +1,207 @@
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) (rb_rb_grn_query_from_ruby_object(object))
22
+
23
+ typedef struct _RbGrnQuery RbGrnQuery;
24
+ struct _RbGrnQuery
25
+ {
26
+ grn_ctx *context;
27
+ grn_query *query;
28
+ rb_grn_boolean owner;
29
+ };
30
+
31
+ VALUE rb_cGrnQuery;
32
+
33
+ static RbGrnQuery *
34
+ rb_rb_grn_query_from_ruby_object (VALUE object)
35
+ {
36
+ RbGrnQuery *rb_grn_query;
37
+
38
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnQuery))) {
39
+ rb_raise(rb_eTypeError, "not a groonga query");
40
+ }
41
+
42
+ Data_Get_Struct(object, RbGrnQuery, rb_grn_query);
43
+ if (!rb_grn_query)
44
+ rb_raise(rb_eGrnError, "groonga query is NULL");
45
+
46
+ return rb_grn_query;
47
+ }
48
+
49
+ grn_query *
50
+ rb_grn_query_from_ruby_object (VALUE object)
51
+ {
52
+ if (NIL_P(object))
53
+ return NULL;
54
+
55
+ return SELF(object)->query;
56
+ }
57
+
58
+ static void
59
+ rb_rb_grn_query_free (void *object)
60
+ {
61
+ RbGrnQuery *rb_grn_query = object;
62
+
63
+ if (rb_grn_query->context && rb_grn_query->query && rb_grn_query->owner)
64
+ /* grn_query_close(rb_grn_query->context, rb_grn_query->query) */;
65
+
66
+ xfree(object);
67
+ }
68
+
69
+ VALUE
70
+ rb_grn_query_to_ruby_object (grn_ctx *context, grn_query *query)
71
+ {
72
+ RbGrnQuery *rb_grn_query;
73
+
74
+ if (!query)
75
+ return Qnil;
76
+
77
+ rb_grn_query = ALLOC(RbGrnQuery);
78
+ rb_grn_query->context = context;
79
+ rb_grn_query->query = query;
80
+ rb_grn_query->owner = RB_GRN_FALSE;
81
+
82
+ return Data_Wrap_Struct(rb_cGrnQuery, NULL,
83
+ rb_rb_grn_query_free, rb_grn_query);
84
+ }
85
+
86
+ static VALUE
87
+ rb_grn_query_alloc (VALUE klass)
88
+ {
89
+ return Data_Wrap_Struct(klass, NULL, rb_rb_grn_query_free, NULL);
90
+ }
91
+
92
+ grn_sel_operator
93
+ rb_grn_select_operator_from_ruby_object (VALUE rb_operator)
94
+ {
95
+ grn_sel_operator operator = GRN_SEL_OR;
96
+
97
+ if (NIL_P(rb_operator) ||
98
+ rb_grn_equal_option(rb_operator, "or") ||
99
+ rb_grn_equal_option(rb_operator, "||")) {
100
+ operator = GRN_SEL_OR;
101
+ } else if (rb_grn_equal_option(rb_operator, "and") ||
102
+ rb_grn_equal_option(rb_operator, "+") ||
103
+ rb_grn_equal_option(rb_operator, "&&")) {
104
+ operator = GRN_SEL_AND;
105
+ } else if (rb_grn_equal_option(rb_operator, "but") ||
106
+ rb_grn_equal_option(rb_operator, "not") ||
107
+ rb_grn_equal_option(rb_operator, "-")) {
108
+ operator = GRN_SEL_BUT;
109
+ } else if (rb_grn_equal_option(rb_operator, "adjust") ||
110
+ rb_grn_equal_option(rb_operator, ">")) {
111
+ operator = GRN_SEL_ADJUST;
112
+ } else {
113
+ rb_raise(rb_eArgError,
114
+ "operator should be one of "
115
+ "[:or, :||, :and, :+, :&&, :but, :not, :-, :adjust, :>]: <%s>",
116
+ rb_grn_inspect(rb_operator));
117
+ }
118
+
119
+ return operator;
120
+ }
121
+
122
+ static VALUE
123
+ rb_grn_query_initialize (int argc, VALUE *argv, VALUE self)
124
+ {
125
+ RbGrnQuery *rb_grn_query;
126
+ grn_ctx *context = NULL;
127
+ grn_query *query;
128
+ char *query_string;
129
+ unsigned int query_string_length;
130
+ grn_sel_operator default_operator;
131
+ int max_expressions = RB_GRN_QUERY_DEFAULT_MAX_EXPRESSIONS;
132
+ VALUE rb_query_string, options, rb_context, rb_default_operator;
133
+ VALUE rb_max_expressions;
134
+
135
+ rb_scan_args(argc, argv, "11", &rb_query_string, &options);
136
+
137
+ query_string = StringValuePtr(rb_query_string);
138
+ query_string_length = RSTRING_LEN(rb_query_string);
139
+
140
+ rb_grn_scan_options(options,
141
+ "context", &rb_context,
142
+ "default_operator", &rb_default_operator,
143
+ "max_expressions", &rb_max_expressions,
144
+ NULL);
145
+
146
+ context = rb_grn_context_ensure(rb_context);
147
+
148
+ default_operator = RVAL2GRNSELECTOPERATOR(rb_default_operator);
149
+
150
+ if (!NIL_P(rb_max_expressions))
151
+ max_expressions = NUM2INT(rb_max_expressions);
152
+
153
+ query = grn_query_open(context, query_string, query_string_length,
154
+ default_operator, max_expressions);
155
+ rb_grn_context_check(context, rb_ary_new4(argc, argv));
156
+
157
+ rb_grn_query = ALLOC(RbGrnQuery);
158
+ DATA_PTR(self) = rb_grn_query;
159
+ rb_grn_query->context = context;
160
+ rb_grn_query->query = query;
161
+ rb_grn_query->owner = RB_GRN_TRUE;
162
+
163
+ return Qnil;
164
+ }
165
+
166
+ static VALUE
167
+ rb_grn_query_close (VALUE self)
168
+ {
169
+ RbGrnQuery *rb_grn_query;
170
+
171
+ rb_grn_query = SELF(self);
172
+ if (rb_grn_query->context && rb_grn_query->query) {
173
+ grn_rc rc;
174
+
175
+ rc = grn_query_close(rb_grn_query->context, rb_grn_query->query);
176
+ rb_grn_query->context = NULL;
177
+ rb_grn_query->query = NULL;
178
+ rb_grn_rc_check(rc, self);
179
+ }
180
+ return Qnil;
181
+ }
182
+
183
+ static VALUE
184
+ rb_grn_query_closed_p (VALUE self)
185
+ {
186
+ RbGrnQuery *rb_grn_query;
187
+
188
+ rb_grn_query = SELF(self);
189
+ if (rb_grn_query->context && rb_grn_query->query)
190
+ return Qfalse;
191
+ else
192
+ return Qtrue;
193
+ }
194
+
195
+ void
196
+ rb_grn_init_query (VALUE mGrn)
197
+ {
198
+ rb_cGrnQuery = rb_define_class_under(mGrn, "Query", rb_cObject);
199
+ rb_define_alloc_func(rb_cGrnQuery, rb_grn_query_alloc);
200
+
201
+ rb_define_method(rb_cGrnQuery, "initialize",
202
+ rb_grn_query_initialize, -1);
203
+ rb_define_method(rb_cGrnQuery, "close",
204
+ rb_grn_query_close, 0);
205
+ rb_define_method(rb_cGrnQuery, "closed?",
206
+ rb_grn_query_closed_p, 0);
207
+ }
@@ -0,0 +1,33 @@
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
+ VALUE rb_cGrnRecord;
22
+
23
+ VALUE
24
+ rb_grn_record_new (VALUE table, grn_id id)
25
+ {
26
+ return rb_funcall(rb_cGrnRecord, rb_intern("new"), 2, table, UINT2NUM(id));
27
+ }
28
+
29
+ void
30
+ rb_grn_init_record (VALUE mGrn)
31
+ {
32
+ rb_cGrnRecord = rb_const_get(mGrn, rb_intern("Record"));
33
+ }
@@ -0,0 +1,274 @@
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
+ #ifdef HAVE_RUBY_ENCODING_H
22
+ # include <ruby/encoding.h>
23
+ #endif
24
+
25
+ #define SELF(object) (rb_rb_grn_snippet_from_ruby_object(object))
26
+
27
+ typedef struct _RbGrnSnippet RbGrnSnippet;
28
+ struct _RbGrnSnippet
29
+ {
30
+ grn_ctx *context;
31
+ grn_snip *snippet;
32
+ rb_grn_boolean owner;
33
+ };
34
+
35
+ VALUE rb_cGrnSnippet;
36
+
37
+ static RbGrnSnippet *
38
+ rb_rb_grn_snippet_from_ruby_object (VALUE object)
39
+ {
40
+ RbGrnSnippet *rb_grn_snippet;
41
+
42
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnSnippet))) {
43
+ rb_raise(rb_eTypeError, "not a groonga snippet");
44
+ }
45
+
46
+ Data_Get_Struct(object, RbGrnSnippet, rb_grn_snippet);
47
+ if (!rb_grn_snippet)
48
+ rb_raise(rb_eGrnError, "groonga snippet is NULL");
49
+
50
+ return rb_grn_snippet;
51
+ }
52
+
53
+ grn_snip *
54
+ rb_grn_snippet_from_ruby_object (VALUE object)
55
+ {
56
+ if (NIL_P(object))
57
+ return NULL;
58
+
59
+ return SELF(object)->snippet;
60
+ }
61
+
62
+ static void
63
+ rb_rb_grn_snippet_free (void *object)
64
+ {
65
+ RbGrnSnippet *rb_grn_snippet = object;
66
+
67
+ if (rb_grn_snippet->context && rb_grn_snippet->snippet &&
68
+ rb_grn_snippet->owner)
69
+ grn_snip_close(rb_grn_snippet->context,
70
+ rb_grn_snippet->snippet);
71
+
72
+ xfree(object);
73
+ }
74
+
75
+ VALUE
76
+ rb_grn_snippet_to_ruby_object (grn_ctx *context, grn_snip *snippet)
77
+ {
78
+ RbGrnSnippet *rb_grn_snippet;
79
+
80
+ if (!snippet)
81
+ return Qnil;
82
+
83
+ rb_grn_snippet = ALLOC(RbGrnSnippet);
84
+ rb_grn_snippet->context = context;
85
+ rb_grn_snippet->snippet = snippet;
86
+ rb_grn_snippet->owner = RB_GRN_FALSE;
87
+
88
+ return Data_Wrap_Struct(rb_cGrnSnippet, NULL,
89
+ rb_rb_grn_snippet_free, rb_grn_snippet);
90
+ }
91
+
92
+ static VALUE
93
+ rb_grn_snippet_alloc (VALUE klass)
94
+ {
95
+ return Data_Wrap_Struct(klass, NULL, rb_rb_grn_snippet_free, NULL);
96
+ }
97
+
98
+ static VALUE
99
+ rb_grn_snippet_initialize (int argc, VALUE *argv, VALUE self)
100
+ {
101
+ RbGrnSnippet *rb_grn_snippet;
102
+ grn_ctx *context = NULL;
103
+ grn_snip *snippet = NULL;
104
+ VALUE options;
105
+ VALUE rb_context, rb_normalize, rb_skip_leading_spaces;
106
+ VALUE rb_width, rb_max_results, rb_default_open_tag, rb_default_close_tag;
107
+ VALUE rb_html_escape;
108
+ int flags = GRN_SNIP_COPY_TAG;
109
+ unsigned int width = 100;
110
+ unsigned int max_results = 3;
111
+ char *default_open_tag = NULL;
112
+ unsigned int default_open_tag_length = 0;
113
+ char *default_close_tag = NULL;
114
+ unsigned int default_close_tag_length = 0;
115
+ grn_snip_mapping *mapping = NULL;
116
+
117
+ rb_scan_args(argc, argv, "01", &options);
118
+
119
+ rb_grn_scan_options(options,
120
+ "context", &rb_context,
121
+ "normalize", &rb_normalize,
122
+ "skip_leading_spaces", &rb_skip_leading_spaces,
123
+ "width", &rb_width,
124
+ "max_results", &rb_max_results,
125
+ "default_open_tag", &rb_default_open_tag,
126
+ "default_close_tag", &rb_default_close_tag,
127
+ "html_escape", &rb_html_escape,
128
+ NULL);
129
+
130
+ context = rb_grn_context_ensure(rb_context);
131
+
132
+ if (RVAL2CBOOL(rb_normalize))
133
+ flags |= GRN_SNIP_NORMALIZE;
134
+ if (RVAL2CBOOL(rb_skip_leading_spaces))
135
+ flags |= GRN_SNIP_SKIP_LEADING_SPACES;
136
+
137
+ if (!NIL_P(rb_width))
138
+ width = NUM2UINT(rb_width);
139
+
140
+ if (!NIL_P(rb_max_results))
141
+ max_results = NUM2UINT(rb_max_results);
142
+
143
+ if (!NIL_P(rb_default_open_tag)) {
144
+ default_open_tag = StringValuePtr(rb_default_open_tag);
145
+ default_open_tag_length = RSTRING_LEN(rb_default_open_tag);
146
+ }
147
+
148
+ if (!NIL_P(rb_default_close_tag)) {
149
+ default_close_tag = StringValuePtr(rb_default_close_tag);
150
+ default_close_tag_length = RSTRING_LEN(rb_default_close_tag);
151
+ }
152
+
153
+ if (RVAL2CBOOL(rb_html_escape))
154
+ mapping = (grn_snip_mapping *)-1;
155
+
156
+ snippet = grn_snip_open(context, flags, width, max_results,
157
+ default_open_tag, default_open_tag_length,
158
+ default_close_tag, default_close_tag_length,
159
+ mapping);
160
+ rb_grn_context_check(context, rb_ary_new4(argc, argv));
161
+
162
+ rb_grn_snippet = ALLOC(RbGrnSnippet);
163
+ DATA_PTR(self) = rb_grn_snippet;
164
+ rb_grn_snippet->context = context;
165
+ rb_grn_snippet->snippet = snippet;
166
+ rb_grn_snippet->owner = RB_GRN_TRUE;
167
+
168
+ return Qnil;
169
+ }
170
+
171
+ static VALUE
172
+ rb_grn_snippet_add_keyword (int argc, VALUE *argv, VALUE self)
173
+ {
174
+ RbGrnSnippet *rb_grn_snippet;
175
+ grn_rc rc;
176
+ VALUE rb_keyword, options;
177
+ VALUE rb_open_tag, rb_close_tag;
178
+ char *keyword, *open_tag = NULL, *close_tag = NULL;
179
+ unsigned int keyword_length, open_tag_length = 0, close_tag_length = 0;
180
+
181
+ rb_scan_args(argc, argv, "11", &rb_keyword, &options);
182
+
183
+ rb_grn_snippet = SELF(self);
184
+
185
+ keyword = StringValuePtr(rb_keyword);
186
+ keyword_length = RSTRING_LEN(rb_keyword);
187
+
188
+ rb_grn_scan_options(options,
189
+ "open_tag", &rb_open_tag,
190
+ "close_tag", &rb_close_tag,
191
+ NULL);
192
+
193
+ if (!NIL_P(rb_open_tag)) {
194
+ open_tag = StringValuePtr(rb_open_tag);
195
+ open_tag_length = RSTRING_LEN(rb_open_tag);
196
+ }
197
+
198
+ if (!NIL_P(rb_close_tag)) {
199
+ close_tag = StringValuePtr(rb_close_tag);
200
+ close_tag_length = RSTRING_LEN(rb_close_tag);
201
+ }
202
+
203
+ rc = grn_snip_add_cond(rb_grn_snippet->context,
204
+ rb_grn_snippet->snippet,
205
+ keyword, keyword_length,
206
+ open_tag, open_tag_length,
207
+ close_tag, close_tag_length);
208
+ rb_grn_rc_check(rc, self);
209
+
210
+ return Qnil;
211
+ }
212
+
213
+ static VALUE
214
+ rb_grn_snippet_execute (VALUE self, VALUE rb_string)
215
+ {
216
+ RbGrnSnippet *rb_grn_snippet;
217
+ grn_rc rc;
218
+ grn_ctx *context;
219
+ grn_snip *snippet;
220
+ char *string;
221
+ unsigned int string_length;
222
+ unsigned int i, n_results, max_tagged_length;
223
+ VALUE rb_results;
224
+ char *result;
225
+ #ifdef HAVE_RUBY_ENCODING_H
226
+ rb_encoding *encoding;
227
+ #endif
228
+
229
+ rb_grn_snippet = SELF(self);
230
+ context = rb_grn_snippet->context;
231
+ snippet = rb_grn_snippet->snippet;
232
+
233
+ string = StringValuePtr(rb_string);
234
+ string_length = RSTRING_LEN(rb_string);
235
+ #ifdef HAVE_RUBY_ENCODING_H
236
+ encoding = rb_enc_get(rb_string);
237
+ #endif
238
+
239
+ rc = grn_snip_exec(context, snippet, string, string_length,
240
+ &n_results, &max_tagged_length);
241
+ rb_grn_rc_check(rc, self);
242
+
243
+ rb_results = rb_ary_new2(n_results);
244
+ result = ALLOCA_N(char, max_tagged_length);
245
+ for (i = 0; i < n_results; i++) {
246
+ VALUE rb_result;
247
+ unsigned result_length;
248
+
249
+ rc = grn_snip_get_result(context, snippet, i, result, &result_length);
250
+ rb_grn_rc_check(rc, self);
251
+ #ifdef HAVE_RUBY_ENCODING_H
252
+ rb_result = rb_enc_str_new(result, result_length, encoding);
253
+ #else
254
+ rb_result = rb_str_new(result, result_length);
255
+ #endif
256
+ rb_ary_push(rb_results, rb_result);
257
+ }
258
+
259
+ return rb_results;
260
+ }
261
+
262
+ void
263
+ rb_grn_init_snippet (VALUE mGrn)
264
+ {
265
+ rb_cGrnSnippet = rb_define_class_under(mGrn, "Snippet", rb_cObject);
266
+ rb_define_alloc_func(rb_cGrnSnippet, rb_grn_snippet_alloc);
267
+
268
+ rb_define_method(rb_cGrnSnippet, "initialize",
269
+ rb_grn_snippet_initialize, -1);
270
+ rb_define_method(rb_cGrnSnippet, "add_keyword",
271
+ rb_grn_snippet_add_keyword, -1);
272
+ rb_define_method(rb_cGrnSnippet, "execute",
273
+ rb_grn_snippet_execute, 1);
274
+ }