groonga 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/NEWS.ja.rdoc +11 -0
  2. data/NEWS.rdoc +11 -0
  3. data/README.ja.rdoc +4 -3
  4. data/README.rdoc +4 -3
  5. data/Rakefile +1 -1
  6. data/TUTORIAL.ja.rdoc +168 -44
  7. data/benchmark/common.rb +49 -0
  8. data/benchmark/read-write-small-many-items.rb +156 -0
  9. data/benchmark/write-small-many-items.rb +145 -0
  10. data/example/bookmark.rb +68 -20
  11. data/ext/rb-grn-array-cursor.c +8 -0
  12. data/ext/rb-grn-array.c +40 -11
  13. data/ext/rb-grn-column.c +38 -209
  14. data/ext/rb-grn-context.c +203 -56
  15. data/ext/rb-grn-database.c +119 -5
  16. data/ext/rb-grn-encoding-support.c +64 -0
  17. data/ext/rb-grn-encoding.c +58 -1
  18. data/ext/rb-grn-fix-size-column.c +220 -0
  19. data/ext/rb-grn-hash-cursor.c +8 -0
  20. data/ext/rb-grn-hash.c +244 -2
  21. data/ext/rb-grn-index-column.c +474 -0
  22. data/ext/rb-grn-object.c +143 -265
  23. data/ext/rb-grn-patricia-trie.c +148 -2
  24. data/ext/rb-grn-query.c +5 -3
  25. data/ext/rb-grn-record.c +3 -2
  26. data/ext/rb-grn-snippet.c +5 -3
  27. data/ext/rb-grn-table-cursor-key-support.c +3 -3
  28. data/ext/rb-grn-table-cursor.c +106 -112
  29. data/ext/rb-grn-table-key-support.c +220 -118
  30. data/ext/rb-grn-table.c +336 -80
  31. data/ext/rb-grn-type.c +5 -4
  32. data/ext/rb-grn-utils.c +62 -63
  33. data/ext/rb-grn.h +215 -14
  34. data/ext/rb-groonga.c +7 -16
  35. data/extconf.rb +3 -1
  36. data/html/favicon.ico +0 -0
  37. data/html/favicon.xcf +0 -0
  38. data/html/index.html +1 -7
  39. data/lib/groonga/record.rb +6 -1
  40. data/test/groonga-test-utils.rb +1 -0
  41. data/test/test-array.rb +81 -0
  42. data/test/test-column.rb +22 -12
  43. data/test/test-context.rb +1 -29
  44. data/test/test-database.rb +30 -0
  45. data/test/test-hash.rb +194 -0
  46. data/test/test-index-column.rb +57 -0
  47. data/test/test-patricia-trie.rb +82 -0
  48. data/test/test-record.rb +10 -10
  49. data/test/test-table.rb +37 -130
  50. data/test/test-type.rb +4 -3
  51. metadata +15 -4
  52. data/benchmark/small-many-items.rb +0 -175
@@ -18,95 +18,188 @@
18
18
 
19
19
  #include "rb-grn.h"
20
20
 
21
- #define SELF(object, context) (RVAL2GRNTABLE(object, context))
21
+ #define SELF(object) ((RbGrnTableKeySupport *)DATA_PTR(object))
22
22
 
23
23
  VALUE rb_mGrnTableKeySupport;
24
24
 
25
- /* FIXME */
26
- grn_rc grn_table_get_info(grn_ctx *ctx, grn_obj *table, grn_obj_flags *flags,
27
- grn_encoding *encoding, grn_obj **tokenizer);
25
+ void
26
+ rb_grn_table_key_support_deconstruct (RbGrnTableKeySupport *rb_grn_table_key_support,
27
+ grn_obj **table_key_support,
28
+ grn_ctx **context,
29
+ grn_obj **key,
30
+ grn_id *domain_id,
31
+ grn_obj **domain,
32
+ grn_obj **value,
33
+ grn_id *range_id,
34
+ grn_obj **range)
35
+ {
36
+ RbGrnTable *rb_grn_table;
28
37
 
29
- static grn_id
30
- rb_grn_table_key_support_add_raw (VALUE self, VALUE rb_key,
31
- grn_ctx *context, grn_obj *table)
38
+ rb_grn_table = RB_GRN_TABLE(rb_grn_table_key_support);
39
+
40
+ rb_grn_table_deconstruct(rb_grn_table, table_key_support, context,
41
+ domain_id, domain,
42
+ value, range_id, range);
43
+
44
+ if (key)
45
+ *key = rb_grn_table_key_support->key;
46
+ }
47
+
48
+ static void
49
+ rb_grn_table_key_support_free (void *object)
32
50
  {
33
- VALUE exception;
34
- grn_id id;
35
- grn_obj key;
36
- grn_search_flags flags;
37
-
38
- RVAL2GRNKEY(rb_key, context, &key, table->header.domain, self);
39
- flags = GRN_SEARCH_EXACT | GRN_TABLE_ADD;
40
- id = grn_table_lookup(context, table,
41
- GRN_BULK_HEAD(&key), GRN_BULK_VSIZE(&key),
42
- &flags);
43
- exception = rb_grn_context_to_exception(context, self);
44
- grn_obj_close(context, &key);
45
- if (!NIL_P(exception))
46
- rb_exc_raise(exception);
51
+ RbGrnObject *rb_grn_object = object;
52
+ RbGrnTable *rb_grn_table = object;
53
+ RbGrnTableKeySupport *rb_grn_table_key_support = object;
54
+ grn_ctx *context;
47
55
 
48
- return id;
56
+ context = rb_grn_object->context;
57
+ if (context)
58
+ grn_obj_close(context, rb_grn_table_key_support->key);
59
+
60
+ rb_grn_table_unbind(rb_grn_table);
61
+
62
+ xfree(rb_grn_table_key_support);
63
+ }
64
+
65
+ VALUE
66
+ rb_grn_table_key_support_alloc (VALUE klass)
67
+ {
68
+ return Data_Wrap_Struct(klass, NULL, rb_grn_table_key_support_free, NULL);
69
+ }
70
+
71
+ void
72
+ rb_grn_table_key_support_bind (RbGrnTableKeySupport *rb_grn_table_key_support,
73
+ grn_ctx *context,
74
+ grn_obj *table_key_support,
75
+ rb_grn_boolean owner)
76
+ {
77
+ RbGrnObject *rb_grn_object;
78
+ RbGrnTable *rb_grn_table;
79
+
80
+ rb_grn_object = RB_GRN_OBJECT(rb_grn_table_key_support);
81
+
82
+ rb_grn_table = RB_GRN_TABLE(rb_grn_table_key_support);
83
+ rb_grn_table_bind(rb_grn_table, context, table_key_support, owner);
84
+
85
+ rb_grn_table_key_support->key =
86
+ grn_obj_open(context, GRN_BULK, 0, rb_grn_object->domain_id);
87
+ }
88
+
89
+ void
90
+ rb_grn_table_key_support_assign (VALUE self, VALUE rb_context,
91
+ grn_ctx *context,
92
+ grn_obj *table_key_support,
93
+ rb_grn_boolean owner)
94
+ {
95
+ RbGrnTableKeySupport *rb_grn_table_key_support;
96
+
97
+ rb_grn_table_key_support = ALLOC(RbGrnTableKeySupport);
98
+ DATA_PTR(self) = rb_grn_table_key_support;
99
+
100
+ rb_grn_table_key_support_bind(rb_grn_table_key_support,
101
+ context, table_key_support, owner);
102
+
103
+ rb_iv_set(self, "context", rb_context);
49
104
  }
50
105
 
51
106
  static VALUE
52
- rb_grn_table_key_support_add (VALUE self, VALUE rb_key)
107
+ rb_grn_table_key_support_initialize (int argc, VALUE *argv, VALUE self)
53
108
  {
54
109
  grn_ctx *context = NULL;
55
110
  grn_obj *table;
56
- grn_id id;
111
+ VALUE rb_context;
112
+
113
+ table = rb_grn_table_open_raw(argc, argv, &context, &rb_context);
114
+ rb_grn_context_check(context, self);
115
+ rb_grn_table_key_support_assign(self, rb_context, context,
116
+ table, RB_GRN_TRUE);
117
+
118
+ return Qnil;
119
+ }
120
+
121
+ static grn_id
122
+ rb_grn_table_key_support_add_raw (VALUE self, VALUE rb_key)
123
+ {
124
+ grn_ctx *context;
125
+ grn_obj *table;
126
+ grn_id id, domain_id;
127
+ grn_obj *key, *domain;
57
128
 
58
- table = SELF(self, &context);
129
+ rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
130
+ &key, &domain_id, &domain,
131
+ NULL, NULL, NULL);
59
132
 
60
- id = rb_grn_table_key_support_add_raw(self, rb_key, context, table);
133
+ GRN_BULK_REWIND(key);
134
+ RVAL2GRNKEY(rb_key, context, key, domain_id, domain, self);
135
+ id = grn_table_add(context, table,
136
+ GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key), NULL);
137
+ rb_grn_context_check(context, self);
138
+
139
+ return id;
140
+ }
141
+
142
+ static VALUE
143
+ rb_grn_table_key_support_add (int argc, VALUE *argv, VALUE self)
144
+ {
145
+ grn_id id;
146
+ VALUE key, values;
147
+
148
+ rb_scan_args(argc, argv, "11", &key, &values);
149
+ id = rb_grn_table_key_support_add_raw(self, key);
61
150
  if (GRN_ID_NIL == id)
62
151
  return Qnil;
63
152
  else
64
- return rb_grn_record_new(self, id);
153
+ return rb_grn_record_new(self, id, values);
65
154
  }
66
155
 
67
156
  static VALUE
68
157
  rb_grn_table_key_support_get_key (VALUE self, VALUE rb_id)
69
158
  {
70
- grn_ctx *context = NULL;
159
+ grn_ctx *context;
160
+ grn_obj *table, *key;
71
161
  grn_id id;
72
- grn_obj *table, key;
73
- int key_size = 0;
162
+ int key_size;
74
163
  VALUE rb_key;
75
164
 
76
- table = SELF(self, &context);
165
+ rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
166
+ &key, NULL, NULL,
167
+ NULL, NULL, NULL);
77
168
 
78
169
  id = NUM2UINT(rb_id);
79
- key_size = grn_table_get_key(context, table, id, NULL, 0);
170
+ GRN_BULK_REWIND(key);
171
+ key_size = grn_table_get_key(context, table, id,
172
+ GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key));
80
173
  if (key_size == 0)
81
174
  return Qnil;
82
175
 
83
- GRN_OBJ_INIT(&key, GRN_BULK, 0);
84
- grn_bulk_space(context, &key, key_size);
85
- grn_table_get_key(context, table, id, GRN_BULK_HEAD(&key), key_size);
86
- rb_key = GRNKEY2RVAL(context, GRN_BULK_HEAD(&key), key_size, table, self);
87
- grn_obj_close(context, &key);
176
+ if (GRN_BULK_VSIZE(key) < key_size) {
177
+ grn_bulk_reserve(context, key, key_size);
178
+ grn_table_get_key(context, table, id, GRN_BULK_HEAD(key), key_size);
179
+ }
88
180
 
181
+ rb_key = GRNKEY2RVAL(context, GRN_BULK_HEAD(key), key_size, table, self);
89
182
  return rb_key;
90
183
  }
91
184
 
92
185
  static VALUE
93
186
  rb_grn_table_key_support_delete_by_key (VALUE self, VALUE rb_key)
94
187
  {
95
- VALUE exception;
96
- grn_ctx *context = NULL;
188
+ grn_ctx *context;
97
189
  grn_obj *table;
98
- grn_obj key;
190
+ grn_id domain_id;
191
+ grn_obj *key, *domain;
99
192
  grn_rc rc;
100
193
 
101
- table = SELF(self, &context);
194
+ rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
195
+ &key, &domain_id, &domain,
196
+ NULL, NULL, NULL);
102
197
 
103
- RVAL2GRNKEY(rb_key, context, &key, table->header.domain, self);
198
+ GRN_BULK_REWIND(key);
199
+ RVAL2GRNKEY(rb_key, context, key, domain_id, domain, self);
104
200
  rc = grn_table_delete(context, table,
105
- GRN_BULK_HEAD(&key), GRN_BULK_VSIZE(&key));
106
- exception = rb_grn_context_to_exception(context, self);
107
- grn_obj_close(context, &key);
108
- if (!NIL_P(exception))
109
- rb_exc_raise(exception);
201
+ GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key));
202
+ rb_grn_context_check(context, self);
110
203
  rb_grn_rc_check(rc, self);
111
204
 
112
205
  return Qnil;
@@ -116,88 +209,110 @@ static VALUE
116
209
  rb_grn_table_key_support_delete (VALUE self, VALUE rb_id_or_key)
117
210
  {
118
211
  if (FIXNUM_P(rb_id_or_key)) {
119
- int argc = 1;
120
- VALUE argv[1];
121
- argv[0] = rb_id_or_key;
122
- return rb_call_super(argc, argv);
212
+ return rb_grn_table_delete(self, rb_id_or_key);
123
213
  } else {
124
214
  return rb_grn_table_key_support_delete_by_key(self, rb_id_or_key);
125
215
  }
126
216
  }
127
217
 
218
+ grn_id
219
+ rb_grn_table_key_support_get (VALUE self, VALUE rb_key)
220
+ {
221
+ grn_ctx *context;
222
+ grn_obj *table, *key, *domain;
223
+ grn_id id, domain_id;
224
+
225
+ rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
226
+ &key, &domain_id, &domain,
227
+ NULL, NULL, NULL);
228
+
229
+ GRN_BULK_REWIND(key);
230
+ RVAL2GRNKEY(rb_key, context, key, domain_id, domain, self);
231
+ id = grn_table_get(context, table,
232
+ GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key));
233
+ rb_grn_context_check(context, self);
234
+
235
+ return id;
236
+ }
237
+
128
238
  static VALUE
129
- rb_grn_table_key_support_array_reference_by_key (VALUE self, VALUE rb_key)
239
+ rb_grn_table_key_support_find (VALUE self, VALUE rb_key)
130
240
  {
131
- VALUE exception;
132
- grn_ctx *context = NULL;
133
- grn_obj *table;
134
- grn_obj key;
135
241
  grn_id id;
136
- grn_search_flags flags = 0;
137
242
 
138
- table = SELF(self, &context);
243
+ id = rb_grn_table_key_support_get(self, rb_key);
139
244
 
140
- RVAL2GRNKEY(rb_key, context, &key, table->header.domain, self);
141
- flags = GRN_SEARCH_EXACT;
142
- id = grn_table_lookup(context, table,
143
- GRN_BULK_HEAD(&key), GRN_BULK_VSIZE(&key),
144
- &flags);
145
- exception = rb_grn_context_to_exception(context, self);
146
- grn_obj_close(context, &key);
147
- if (!NIL_P(exception))
148
- rb_exc_raise(exception);
245
+ if (id == GRN_ID_NIL)
246
+ return Qnil;
247
+ else
248
+ return rb_grn_record_new(self, id, Qnil);
249
+ }
250
+
251
+ static VALUE
252
+ rb_grn_table_key_support_array_reference_by_key (VALUE self, VALUE rb_key)
253
+ {
254
+ grn_ctx *context;
255
+ grn_obj *table, *value, *range;
256
+ grn_id id;
257
+
258
+ id = rb_grn_table_key_support_get(self, rb_key);
149
259
 
150
260
  if (id == GRN_ID_NIL)
151
261
  return Qnil;
262
+
263
+ rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
264
+ NULL, NULL, NULL,
265
+ &value, NULL, &range);
266
+ GRN_BULK_REWIND(value);
267
+ grn_obj_get_value(context, table, id, value);
268
+ rb_grn_context_check(context, self);
269
+
270
+ if (GRN_BULK_EMPTYP(value))
271
+ return Qnil;
152
272
  else
153
- return rb_grn_record_new(self, id);
273
+ return rb_str_new(GRN_BULK_HEAD(value), GRN_BULK_VSIZE(value));
154
274
  }
155
275
 
156
276
  static VALUE
157
277
  rb_grn_table_key_support_array_reference (VALUE self, VALUE rb_id_or_key)
158
278
  {
159
279
  if (FIXNUM_P(rb_id_or_key)) {
160
- int argc = 1;
161
- VALUE argv[1];
162
- argv[0] = rb_id_or_key;
163
- return rb_call_super(argc, argv);
280
+ return rb_grn_table_array_reference(self, rb_id_or_key);
164
281
  } else {
165
282
  return rb_grn_table_key_support_array_reference_by_key(self,
166
283
  rb_id_or_key);
167
284
  }
168
285
  }
169
286
 
170
-
171
287
  static VALUE
172
288
  rb_grn_table_key_support_array_set_by_key (VALUE self,
173
289
  VALUE rb_key, VALUE rb_value)
174
290
  {
175
- VALUE exception;
176
- grn_ctx *context = NULL;
291
+ grn_ctx *context;
177
292
  grn_obj *table;
178
293
  grn_id id;
179
- grn_obj value;
294
+ grn_obj *value;
180
295
  grn_rc rc;
181
296
 
182
- table = SELF(self, &context);
183
-
184
297
  if (NIL_P(rb_key))
185
298
  rb_raise(rb_eArgError, "key should not be nil: <%s>",
186
299
  rb_grn_inspect(self));
187
300
 
188
- id = rb_grn_table_key_support_add_raw(self, rb_key, context, table);
301
+ rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
302
+ NULL, NULL, NULL,
303
+ &value, NULL, NULL);
304
+
305
+ id = rb_grn_table_key_support_add_raw(self, rb_key);
189
306
  if (GRN_ID_NIL == id)
190
307
  rb_raise(rb_eGrnError,
191
308
  "failed to add new record with key: <%s>: <%s>",
192
309
  rb_grn_inspect(rb_key),
193
310
  rb_grn_inspect(self));
194
311
 
195
- RVAL2GRNBULK(rb_value, context, &value);
196
- rc = grn_obj_set_value(context, table, id, &value, GRN_OBJ_SET);
197
- exception = rb_grn_context_to_exception(context, self);
198
- grn_obj_close(context, &value);
199
- if (!NIL_P(exception))
200
- rb_exc_raise(exception);
312
+ GRN_BULK_REWIND(value);
313
+ RVAL2GRNBULK(rb_value, context, value);
314
+ rc = grn_obj_set_value(context, table, id, value, GRN_OBJ_SET);
315
+ rb_grn_context_check(context, self);
201
316
  rb_grn_rc_check(rc, self);
202
317
 
203
318
  return rb_value;
@@ -208,11 +323,7 @@ rb_grn_table_key_support_array_set (VALUE self,
208
323
  VALUE rb_id_or_key, VALUE rb_value)
209
324
  {
210
325
  if (FIXNUM_P(rb_id_or_key)) {
211
- int argc = 2;
212
- VALUE argv[2];
213
- argv[0] = rb_id_or_key;
214
- argv[1] = rb_value;
215
- return rb_call_super(argc, argv);
326
+ return rb_grn_table_array_set(self, rb_id_or_key, rb_value);
216
327
  } else {
217
328
  return rb_grn_table_key_support_array_set_by_key(self,
218
329
  rb_id_or_key,
@@ -220,34 +331,20 @@ rb_grn_table_key_support_array_set (VALUE self,
220
331
  }
221
332
  }
222
333
 
223
- static VALUE
224
- rb_grn_table_key_support_get_encoding (VALUE self)
225
- {
226
- grn_ctx *context = NULL;
227
- grn_obj *table;
228
- grn_encoding encoding;
229
- grn_rc rc;
230
-
231
- table = SELF(self, &context);
232
- rc = grn_table_get_info(context, table, NULL, &encoding, NULL);
233
- rb_grn_context_check(context, self);
234
- rb_grn_rc_check(rc, self);
235
-
236
- return GRNENCODING2RVAL(encoding);
237
- }
238
-
239
334
  static VALUE
240
335
  rb_grn_table_key_support_get_default_tokenizer (VALUE self)
241
336
  {
242
337
  grn_ctx *context = NULL;
243
338
  grn_obj *table;
244
- grn_obj *tokenizer;
245
- grn_rc rc;
339
+ grn_obj *tokenizer = NULL;
340
+
341
+ rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
342
+ NULL, NULL, NULL,
343
+ NULL, NULL, NULL);
246
344
 
247
- table = SELF(self, &context);
248
- rc = grn_table_get_info(context, table, NULL, NULL, &tokenizer);
345
+ tokenizer = grn_obj_get_info(context, table, GRN_INFO_DEFAULT_TOKENIZER,
346
+ NULL);
249
347
  rb_grn_context_check(context, self);
250
- rb_grn_rc_check(rc, self);
251
348
 
252
349
  return GRNOBJECT2RVAL(Qnil, context, tokenizer, RB_GRN_FALSE);
253
350
  }
@@ -255,12 +352,15 @@ rb_grn_table_key_support_get_default_tokenizer (VALUE self)
255
352
  static VALUE
256
353
  rb_grn_table_key_support_set_default_tokenizer (VALUE self, VALUE rb_tokenizer)
257
354
  {
258
- grn_ctx *context = NULL;
355
+ grn_ctx *context;
259
356
  grn_obj *table;
260
357
  grn_obj *tokenizer;
261
358
  grn_rc rc;
262
359
 
263
- table = SELF(self, &context);
360
+ rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
361
+ NULL, NULL, NULL,
362
+ NULL, NULL, NULL);
363
+
264
364
  tokenizer = RVAL2GRNOBJECT(rb_tokenizer, &context);
265
365
  rc = grn_obj_set_info(context, table,
266
366
  GRN_INFO_DEFAULT_TOKENIZER, tokenizer);
@@ -274,26 +374,28 @@ void
274
374
  rb_grn_init_table_key_support (VALUE mGrn)
275
375
  {
276
376
  rb_mGrnTableKeySupport = rb_define_module_under(rb_cGrnTable, "KeySupport");
377
+ rb_include_module(rb_mGrnTableKeySupport, rb_mGrnEncodingSupport);
378
+
379
+ rb_define_method(rb_mGrnTableKeySupport, "initialize",
380
+ rb_grn_table_key_support_initialize, -1);
277
381
 
278
382
  rb_define_method(rb_mGrnTableKeySupport, "add",
279
- rb_grn_table_key_support_add, 1);
383
+ rb_grn_table_key_support_add, -1);
280
384
  rb_define_method(rb_mGrnTableKeySupport, "key",
281
385
  rb_grn_table_key_support_get_key, 1);
282
386
 
283
387
  rb_define_method(rb_mGrnTableKeySupport, "delete",
284
388
  rb_grn_table_key_support_delete, 1);
285
389
 
390
+ rb_define_method(rb_mGrnTableKeySupport, "find",
391
+ rb_grn_table_key_support_find, 1);
286
392
  rb_define_method(rb_mGrnTableKeySupport, "[]",
287
393
  rb_grn_table_key_support_array_reference, 1);
288
394
  rb_define_method(rb_mGrnTableKeySupport, "[]=",
289
395
  rb_grn_table_key_support_array_set, 2);
290
396
 
291
- rb_define_method(rb_mGrnTableKeySupport, "encoding",
292
- rb_grn_table_key_support_get_encoding, 0);
293
-
294
397
  rb_define_method(rb_mGrnTableKeySupport, "default_tokenizer",
295
398
  rb_grn_table_key_support_get_default_tokenizer, 0);
296
-
297
399
  rb_define_method(rb_mGrnTableKeySupport, "default_tokenizer=",
298
400
  rb_grn_table_key_support_set_default_tokenizer, 1);
299
401
  }