rroonga 3.0.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,6 +18,16 @@
18
18
 
19
19
  #include "rb-grn.h"
20
20
 
21
+ /* GRN_TABLE_* should be defined in groonga.h. If they are defined in
22
+ * groonga.h, the following defines are removed. */
23
+ #ifndef GRN_TABLE_GROUPED
24
+ # define GRN_TABLE_GROUPED (0x01<<0)
25
+ #endif
26
+ #ifndef GRN_TABLE_IS_GROUPED
27
+ # define GRN_TABLE_IS_GROUPED(table) \
28
+ ((table)->header.impl_flags & GRN_TABLE_GROUPED)
29
+ #endif
30
+
21
31
  grn_obj *grn_table_open(grn_ctx *ctx,
22
32
  const char *name, unsigned name_size, const char *path);
23
33
  grn_obj *grn_column_open(grn_ctx *ctx, grn_obj *table,
@@ -2242,6 +2252,42 @@ rb_grn_table_support_sub_records_p (VALUE self)
2242
2252
  {
2243
2253
  grn_obj *table;
2244
2254
 
2255
+ rb_grn_table_deconstruct(SELF(self), &table, NULL,
2256
+ NULL, NULL,
2257
+ NULL, NULL, NULL,
2258
+ NULL);
2259
+ return CBOOL2RVAL(GRN_TABLE_IS_GROUPED(table));
2260
+ }
2261
+
2262
+ /*
2263
+ * {Groonga::Table#group} returns a table that contains grouped
2264
+ * records. If grouped table has a space to store the number of
2265
+ * records for each group, the number of records is stored to
2266
+ * it. Records for each group are called as "sub records".
2267
+ *
2268
+ * Normally, you don't need to care about the space because rroonga
2269
+ * creates a table with the space automatically. Normal tables
2270
+ * (persistent tables) don't have the space because they don't need
2271
+ * it.
2272
+ *
2273
+ * @example A normal table don't have the space
2274
+ * users = Groonga["Users"] # A normal table
2275
+ * users.have_n_sub_records_space? # => false
2276
+ *
2277
+ * @example A grouped table has the space
2278
+ * users = Groonga["Users"] # A normal table
2279
+ * grouped_users = users.group("_key") # A grouped table
2280
+ * grouped_users.have_n_sub_records_space? # => true
2281
+ *
2282
+ * @overload have_n_sub_records_space?
2283
+ * @return [Boolean] @true@ if the table has a space for storing
2284
+ * the number of sub records, @false@ otherwise.
2285
+ */
2286
+ static VALUE
2287
+ rb_grn_table_have_n_sub_records_space_p (VALUE self)
2288
+ {
2289
+ grn_obj *table;
2290
+
2245
2291
  rb_grn_table_deconstruct(SELF(self), &table, NULL,
2246
2292
  NULL, NULL,
2247
2293
  NULL, NULL, NULL,
@@ -2413,6 +2459,8 @@ rb_grn_init_table (VALUE mGrn)
2413
2459
  rb_grn_table_support_key_p, 0);
2414
2460
  rb_define_method(rb_cGrnTable, "support_sub_records?",
2415
2461
  rb_grn_table_support_sub_records_p, 0);
2462
+ rb_define_method(rb_cGrnTable, "have_n_sub_records_space?",
2463
+ rb_grn_table_have_n_sub_records_space_p, 0);
2416
2464
 
2417
2465
  rb_define_method(rb_cGrnTable, "exist?", rb_grn_table_exist_p, 1);
2418
2466
 
@@ -102,13 +102,13 @@ rb_grn_scan_options (VALUE options, ...)
102
102
 
103
103
  options = rb_grn_check_convert_to_hash(options);
104
104
  if (NIL_P(options)) {
105
- if (NIL_P(original_options)) {
106
- options = rb_hash_new();
107
- } else {
108
- rb_raise(rb_eArgError,
109
- "options must be Hash: %s",
110
- rb_grn_inspect(original_options));
111
- }
105
+ if (NIL_P(original_options)) {
106
+ options = rb_hash_new();
107
+ } else {
108
+ rb_raise(rb_eArgError,
109
+ "options must be Hash: %s",
110
+ rb_grn_inspect(original_options));
111
+ }
112
112
  } else if (options == original_options) {
113
113
  options = rb_funcall(options, rb_intern("dup"), 0);
114
114
  }
@@ -144,11 +144,11 @@ rb_grn_equal_option (VALUE option, const char *key)
144
144
 
145
145
  key_string = rb_str_new2(key);
146
146
  if (RVAL2CBOOL(rb_funcall(option, rb_intern("=="), 1, key_string)))
147
- return GRN_TRUE;
147
+ return GRN_TRUE;
148
148
 
149
149
  key_symbol = rb_str_intern(key_string);
150
150
  if (RVAL2CBOOL(rb_funcall(option, rb_intern("=="), 1, key_symbol)))
151
- return GRN_TRUE;
151
+ return GRN_TRUE;
152
152
 
153
153
  return GRN_FALSE;
154
154
  }
@@ -193,69 +193,69 @@ rb_grn_check_convert_to_hash (VALUE object)
193
193
 
194
194
  static VALUE
195
195
  rb_grn_bulk_to_ruby_object_by_range_id (grn_ctx *context, grn_obj *bulk,
196
- grn_id range_id,
197
- VALUE related_object, VALUE *rb_value)
196
+ grn_id range_id,
197
+ VALUE related_object, VALUE *rb_value)
198
198
  {
199
199
  grn_bool success = GRN_TRUE;
200
200
 
201
201
  switch (range_id) {
202
202
  case GRN_DB_VOID:
203
- *rb_value = rb_str_new(GRN_TEXT_VALUE(bulk), GRN_TEXT_LEN(bulk));
204
- break;
203
+ *rb_value = rb_str_new(GRN_TEXT_VALUE(bulk), GRN_TEXT_LEN(bulk));
204
+ break;
205
205
  case GRN_DB_BOOL:
206
- *rb_value = GRN_BOOL_VALUE(bulk) ? Qtrue : Qfalse;
207
- break;
206
+ *rb_value = GRN_BOOL_VALUE(bulk) ? Qtrue : Qfalse;
207
+ break;
208
208
  case GRN_DB_INT32:
209
- *rb_value = INT2NUM(GRN_INT32_VALUE(bulk));
210
- break;
209
+ *rb_value = INT2NUM(GRN_INT32_VALUE(bulk));
210
+ break;
211
211
  case GRN_DB_UINT32:
212
- *rb_value = UINT2NUM(GRN_UINT32_VALUE(bulk));
213
- break;
212
+ *rb_value = UINT2NUM(GRN_UINT32_VALUE(bulk));
213
+ break;
214
214
  case GRN_DB_INT64:
215
- *rb_value = LL2NUM(GRN_INT64_VALUE(bulk));
216
- break;
215
+ *rb_value = LL2NUM(GRN_INT64_VALUE(bulk));
216
+ break;
217
217
  case GRN_DB_UINT64:
218
- *rb_value = ULL2NUM(GRN_UINT64_VALUE(bulk));
219
- break;
218
+ *rb_value = ULL2NUM(GRN_UINT64_VALUE(bulk));
219
+ break;
220
220
  case GRN_DB_FLOAT:
221
- *rb_value = rb_float_new(GRN_FLOAT_VALUE(bulk));
222
- break;
221
+ *rb_value = rb_float_new(GRN_FLOAT_VALUE(bulk));
222
+ break;
223
223
  case GRN_DB_TIME:
224
- {
225
- int64_t time_value, sec, usec;
226
-
227
- time_value = GRN_TIME_VALUE(bulk);
228
- GRN_TIME_UNPACK(time_value, sec, usec);
229
- *rb_value = rb_funcall(rb_cTime, rb_intern("at"), 2,
230
- LL2NUM(sec), LL2NUM(usec));
231
- }
232
- break;
224
+ {
225
+ int64_t time_value, sec, usec;
226
+
227
+ time_value = GRN_TIME_VALUE(bulk);
228
+ GRN_TIME_UNPACK(time_value, sec, usec);
229
+ *rb_value = rb_funcall(rb_cTime, rb_intern("at"), 2,
230
+ LL2NUM(sec), LL2NUM(usec));
231
+ }
232
+ break;
233
233
  case GRN_DB_SHORT_TEXT:
234
234
  case GRN_DB_TEXT:
235
235
  case GRN_DB_LONG_TEXT:
236
- *rb_value = rb_grn_context_rb_string_new(context,
237
- GRN_TEXT_VALUE(bulk),
238
- GRN_TEXT_LEN(bulk));
239
- break;
236
+ *rb_value = rb_grn_context_rb_string_new(context,
237
+ GRN_TEXT_VALUE(bulk),
238
+ GRN_TEXT_LEN(bulk));
239
+ break;
240
240
  case GRN_DB_TOKYO_GEO_POINT:
241
- {
242
- int latitude, longitude;
241
+ {
242
+ int latitude, longitude;
243
243
 
244
- GRN_GEO_POINT_VALUE(bulk, latitude, longitude);
245
- *rb_value = rb_grn_tokyo_geo_point_new(latitude, longitude);
246
- }
247
- break;
244
+ GRN_GEO_POINT_VALUE(bulk, latitude, longitude);
245
+ *rb_value = rb_grn_tokyo_geo_point_new(latitude, longitude);
246
+ }
247
+ break;
248
248
  case GRN_DB_WGS84_GEO_POINT:
249
- {
250
- int latitude, longitude;
249
+ {
250
+ int latitude, longitude;
251
251
 
252
- GRN_GEO_POINT_VALUE(bulk, latitude, longitude);
253
- *rb_value = rb_grn_wgs84_geo_point_new(latitude, longitude);
254
- }
255
- break;
252
+ GRN_GEO_POINT_VALUE(bulk, latitude, longitude);
253
+ *rb_value = rb_grn_wgs84_geo_point_new(latitude, longitude);
254
+ }
255
+ break;
256
256
  default:
257
- success = GRN_FALSE;
258
- break;
257
+ success = GRN_FALSE;
258
+ break;
259
259
  }
260
260
 
261
261
  return success;
@@ -263,62 +263,62 @@ rb_grn_bulk_to_ruby_object_by_range_id (grn_ctx *context, grn_obj *bulk,
263
263
 
264
264
  static VALUE
265
265
  rb_grn_bulk_to_ruby_object_by_range_type (grn_ctx *context, grn_obj *bulk,
266
- grn_obj *range, grn_id range_id,
267
- VALUE related_object, VALUE *rb_value)
266
+ grn_obj *range, grn_id range_id,
267
+ VALUE related_object, VALUE *rb_value)
268
268
  {
269
269
  grn_bool success = GRN_TRUE;
270
270
 
271
271
  if (!range && range_id != GRN_ID_NIL) {
272
- range = grn_ctx_at(context, range_id);
272
+ range = grn_ctx_at(context, range_id);
273
273
  }
274
274
 
275
275
  if (!range)
276
- return GRN_FALSE;
276
+ return GRN_FALSE;
277
277
 
278
278
  switch (range->header.type) {
279
279
  case GRN_TABLE_HASH_KEY:
280
280
  case GRN_TABLE_PAT_KEY:
281
281
  case GRN_TABLE_DAT_KEY:
282
282
  case GRN_TABLE_NO_KEY:
283
- {
284
- grn_id id;
285
-
286
- id = *((grn_id *)GRN_BULK_HEAD(bulk));
287
- if (id == GRN_ID_NIL) {
288
- *rb_value = Qnil;
289
- } else {
290
- VALUE rb_range;
291
-
292
- rb_range = GRNOBJECT2RVAL(Qnil, context, range, GRN_FALSE);
293
- *rb_value = rb_grn_record_new(rb_range, id, Qnil);
294
- }
295
- }
296
- break;
283
+ {
284
+ grn_id id;
285
+
286
+ id = *((grn_id *)GRN_BULK_HEAD(bulk));
287
+ if (id == GRN_ID_NIL) {
288
+ *rb_value = Qnil;
289
+ } else {
290
+ VALUE rb_range;
291
+
292
+ rb_range = GRNOBJECT2RVAL(Qnil, context, range, GRN_FALSE);
293
+ *rb_value = rb_grn_record_new(rb_range, id, Qnil);
294
+ }
295
+ }
296
+ break;
297
297
  case GRN_TYPE:
298
- if (range->header.flags & GRN_OBJ_KEY_VAR_SIZE) {
299
- *rb_value = rb_grn_context_rb_string_new(context,
300
- GRN_BULK_HEAD(bulk),
301
- GRN_BULK_VSIZE(bulk));
302
- } else {
303
- switch (range->header.flags & GRN_OBJ_KEY_MASK) {
304
- case GRN_OBJ_KEY_UINT:
305
- *rb_value = INT2NUM(GRN_UINT32_VALUE(bulk));
306
- break;
307
- case GRN_OBJ_KEY_INT:
308
- *rb_value = INT2NUM(GRN_INT32_VALUE(bulk));
309
- break;
310
- case GRN_OBJ_KEY_FLOAT:
311
- *rb_value = rb_float_new(GRN_FLOAT_VALUE(bulk));
312
- break;
313
- default:
314
- success = GRN_FALSE;
315
- }
316
- break;
317
- }
318
- break;
298
+ if (range->header.flags & GRN_OBJ_KEY_VAR_SIZE) {
299
+ *rb_value = rb_grn_context_rb_string_new(context,
300
+ GRN_BULK_HEAD(bulk),
301
+ GRN_BULK_VSIZE(bulk));
302
+ } else {
303
+ switch (range->header.flags & GRN_OBJ_KEY_MASK) {
304
+ case GRN_OBJ_KEY_UINT:
305
+ *rb_value = INT2NUM(GRN_UINT32_VALUE(bulk));
306
+ break;
307
+ case GRN_OBJ_KEY_INT:
308
+ *rb_value = INT2NUM(GRN_INT32_VALUE(bulk));
309
+ break;
310
+ case GRN_OBJ_KEY_FLOAT:
311
+ *rb_value = rb_float_new(GRN_FLOAT_VALUE(bulk));
312
+ break;
313
+ default:
314
+ success = GRN_FALSE;
315
+ }
316
+ break;
317
+ }
318
+ break;
319
319
  default:
320
- success = GRN_FALSE;
321
- break;
320
+ success = GRN_FALSE;
321
+ break;
322
322
  }
323
323
 
324
324
  return success;
@@ -326,27 +326,27 @@ rb_grn_bulk_to_ruby_object_by_range_type (grn_ctx *context, grn_obj *bulk,
326
326
 
327
327
  VALUE
328
328
  rb_grn_bulk_to_ruby_object (grn_ctx *context, grn_obj *bulk,
329
- grn_obj *range, VALUE related_object)
329
+ grn_obj *range, VALUE related_object)
330
330
  {
331
331
  grn_id range_id;
332
332
  VALUE rb_value = Qnil;
333
333
 
334
334
  if (GRN_BULK_EMPTYP(bulk))
335
- return Qnil;
335
+ return Qnil;
336
336
 
337
337
  range_id = bulk->header.domain;
338
338
  if (rb_grn_bulk_to_ruby_object_by_range_id(context, bulk, range_id,
339
- related_object, &rb_value))
340
- return rb_value;
339
+ related_object, &rb_value))
340
+ return rb_value;
341
341
 
342
342
  if (rb_grn_bulk_to_ruby_object_by_range_type(context, bulk,
343
- range, range_id,
344
- related_object, &rb_value))
345
- return rb_value;
343
+ range, range_id,
344
+ related_object, &rb_value))
345
+ return rb_value;
346
346
 
347
347
  return rb_grn_context_rb_string_new(context,
348
- GRN_BULK_HEAD(bulk),
349
- GRN_BULK_VSIZE(bulk));
348
+ GRN_BULK_HEAD(bulk),
349
+ GRN_BULK_VSIZE(bulk));
350
350
  }
351
351
 
352
352
  grn_obj *
@@ -354,88 +354,88 @@ rb_grn_bulk_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *bulk)
354
354
  {
355
355
  if (bulk && bulk->header.domain == GRN_DB_TIME)
356
356
  return RVAL2GRNBULK_WITH_TYPE(object, context, bulk,
357
- bulk->header.domain,
358
- grn_ctx_at(context, bulk->header.domain));
357
+ bulk->header.domain,
358
+ grn_ctx_at(context, bulk->header.domain));
359
359
 
360
360
  if (!bulk) {
361
- bulk = grn_obj_open(context, GRN_BULK, 0, GRN_ID_NIL);
362
- rb_grn_context_check(context, object);
361
+ bulk = grn_obj_open(context, GRN_BULK, 0, GRN_ID_NIL);
362
+ rb_grn_context_check(context, object);
363
363
  }
364
364
 
365
365
  switch (TYPE(object)) {
366
366
  case T_NIL:
367
- grn_obj_reinit(context, bulk, GRN_DB_VOID, 0);
368
- break;
367
+ grn_obj_reinit(context, bulk, GRN_DB_VOID, 0);
368
+ break;
369
369
  case T_SYMBOL:
370
- object = rb_funcall(object, rb_intern("to_s"), 0);
370
+ object = rb_funcall(object, rb_intern("to_s"), 0);
371
371
  case T_STRING:
372
- grn_obj_reinit(context, bulk, GRN_DB_TEXT, 0);
373
- rb_grn_context_text_set(context, bulk, object);
374
- break;
372
+ grn_obj_reinit(context, bulk, GRN_DB_TEXT, 0);
373
+ rb_grn_context_text_set(context, bulk, object);
374
+ break;
375
375
  case T_FIXNUM:
376
- grn_obj_reinit(context, bulk, GRN_DB_INT32, 0);
377
- GRN_INT32_SET(context, bulk, NUM2INT(object));
378
- break;
376
+ grn_obj_reinit(context, bulk, GRN_DB_INT32, 0);
377
+ GRN_INT32_SET(context, bulk, NUM2INT(object));
378
+ break;
379
379
  case T_BIGNUM:
380
- {
381
- int64_t int64_value;
382
- int64_value = NUM2LL(object);
383
- if (int64_value <= INT32_MAX) {
384
- grn_obj_reinit(context, bulk, GRN_DB_INT32, 0);
385
- GRN_INT32_SET(context, bulk, int64_value);
386
- } else {
387
- grn_obj_reinit(context, bulk, GRN_DB_INT64, 0);
388
- GRN_INT64_SET(context, bulk, int64_value);
389
- }
390
- }
391
- break;
380
+ {
381
+ int64_t int64_value;
382
+ int64_value = NUM2LL(object);
383
+ if (int64_value <= INT32_MAX) {
384
+ grn_obj_reinit(context, bulk, GRN_DB_INT32, 0);
385
+ GRN_INT32_SET(context, bulk, int64_value);
386
+ } else {
387
+ grn_obj_reinit(context, bulk, GRN_DB_INT64, 0);
388
+ GRN_INT64_SET(context, bulk, int64_value);
389
+ }
390
+ }
391
+ break;
392
392
  case T_FLOAT:
393
- grn_obj_reinit(context, bulk, GRN_DB_FLOAT, 0);
394
- GRN_FLOAT_SET(context, bulk, NUM2DBL(object));
395
- break;
393
+ grn_obj_reinit(context, bulk, GRN_DB_FLOAT, 0);
394
+ GRN_FLOAT_SET(context, bulk, NUM2DBL(object));
395
+ break;
396
396
  case T_TRUE:
397
- grn_obj_reinit(context, bulk, GRN_DB_BOOL, 0);
398
- GRN_BOOL_SET(context, bulk, GRN_TRUE);
399
- break;
397
+ grn_obj_reinit(context, bulk, GRN_DB_BOOL, 0);
398
+ GRN_BOOL_SET(context, bulk, GRN_TRUE);
399
+ break;
400
400
  case T_FALSE:
401
- grn_obj_reinit(context, bulk, GRN_DB_BOOL, 0);
402
- GRN_BOOL_SET(context, bulk, GRN_FALSE);
403
- break;
401
+ grn_obj_reinit(context, bulk, GRN_DB_BOOL, 0);
402
+ GRN_BOOL_SET(context, bulk, GRN_FALSE);
403
+ break;
404
404
  default:
405
- if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cTime))) {
406
- VALUE sec, usec;
407
- int64_t time_value;
408
-
409
- sec = rb_funcall(object, rb_intern("to_i"), 0);
410
- usec = rb_funcall(object, rb_intern("usec"), 0);
411
- time_value = GRN_TIME_PACK(NUM2LL(sec), NUM2LL(usec));
412
- grn_obj_reinit(context, bulk, GRN_DB_TIME, 0);
413
- GRN_TIME_SET(context, bulk, time_value);
414
- } else if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnObject))) {
415
- grn_obj *grn_object;
416
- grn_id id_value;
417
-
418
- grn_object = RVAL2GRNOBJECT(object, &context);
419
- grn_obj_reinit(context, bulk, grn_object->header.domain, 0);
420
- id_value = grn_obj_id(context, grn_object);
421
- GRN_RECORD_SET(context, bulk, id_value);
422
- } else if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnRecord))) {
423
- grn_obj *table;
424
- grn_id id_value;
425
-
426
- table = RVAL2GRNOBJECT(rb_funcall(object, rb_intern("table"), 0),
427
- &context);
428
- id_value = NUM2UINT(rb_funcall(object, rb_intern("id"), 0));
429
- grn_obj_reinit(context, bulk, grn_obj_id(context, table), 0);
430
- GRN_RECORD_SET(context, bulk, id_value);
431
- } else {
432
- rb_raise(rb_eTypeError,
433
- "bulked object should be one of "
434
- "[nil, true, false, String, Symbol, Integer, Float, Time, "
435
- "Groonga::Object, Groonga::Record]: %s",
436
- rb_grn_inspect(object));
437
- }
438
- break;
405
+ if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cTime))) {
406
+ VALUE sec, usec;
407
+ int64_t time_value;
408
+
409
+ sec = rb_funcall(object, rb_intern("to_i"), 0);
410
+ usec = rb_funcall(object, rb_intern("usec"), 0);
411
+ time_value = GRN_TIME_PACK(NUM2LL(sec), NUM2LL(usec));
412
+ grn_obj_reinit(context, bulk, GRN_DB_TIME, 0);
413
+ GRN_TIME_SET(context, bulk, time_value);
414
+ } else if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnObject))) {
415
+ grn_obj *grn_object;
416
+ grn_id id_value;
417
+
418
+ grn_object = RVAL2GRNOBJECT(object, &context);
419
+ grn_obj_reinit(context, bulk, grn_object->header.domain, 0);
420
+ id_value = grn_obj_id(context, grn_object);
421
+ GRN_RECORD_SET(context, bulk, id_value);
422
+ } else if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnRecord))) {
423
+ grn_obj *table;
424
+ grn_id id_value;
425
+
426
+ table = RVAL2GRNOBJECT(rb_funcall(object, rb_intern("table"), 0),
427
+ &context);
428
+ id_value = NUM2UINT(rb_funcall(object, rb_intern("id"), 0));
429
+ grn_obj_reinit(context, bulk, grn_obj_id(context, table), 0);
430
+ GRN_RECORD_SET(context, bulk, id_value);
431
+ } else {
432
+ rb_raise(rb_eTypeError,
433
+ "bulked object should be one of "
434
+ "[nil, true, false, String, Symbol, Integer, Float, Time, "
435
+ "Groonga::Object, Groonga::Record]: %s",
436
+ rb_grn_inspect(object));
437
+ }
438
+ break;
439
439
  }
440
440
 
441
441
  return bulk;
@@ -443,8 +443,8 @@ rb_grn_bulk_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *bulk)
443
443
 
444
444
  grn_obj *
445
445
  rb_grn_bulk_from_ruby_object_with_type (VALUE object, grn_ctx *context,
446
- grn_obj *bulk,
447
- grn_id type_id, grn_obj *type)
446
+ grn_obj *bulk,
447
+ grn_id type_id, grn_obj *type)
448
448
  {
449
449
  const char *string;
450
450
  unsigned int size;
@@ -462,40 +462,46 @@ rb_grn_bulk_from_ruby_object_with_type (VALUE object, grn_ctx *context,
462
462
 
463
463
  string_p = rb_type(object) == T_STRING;
464
464
  table_type_p = (GRN_TABLE_HASH_KEY <= type->header.type &&
465
- type->header.type <= GRN_TABLE_NO_KEY);
465
+ type->header.type <= GRN_TABLE_NO_KEY);
466
466
 
467
467
  switch (type_id) {
468
468
  case GRN_DB_INT32:
469
- int32_value = NUM2INT(object);
470
- string = (const char *)&int32_value;
471
- size = sizeof(int32_value);
472
- break;
469
+ int32_value = NUM2INT(object);
470
+ string = (const char *)&int32_value;
471
+ size = sizeof(int32_value);
472
+ break;
473
473
  case GRN_DB_UINT32:
474
- uint32_value = NUM2UINT(object);
475
- string = (const char *)&uint32_value;
476
- size = sizeof(uint32_value);
477
- break;
474
+ uint32_value = NUM2UINT(object);
475
+ string = (const char *)&uint32_value;
476
+ size = sizeof(uint32_value);
477
+ break;
478
478
  case GRN_DB_INT64:
479
- int64_value = NUM2LL(object);
480
- string = (const char *)&int64_value;
481
- size = sizeof(int64_value);
482
- break;
479
+ int64_value = NUM2LL(object);
480
+ string = (const char *)&int64_value;
481
+ size = sizeof(int64_value);
482
+ break;
483
483
  case GRN_DB_UINT64:
484
- uint64_value = NUM2ULL(object);
485
- string = (const char *)&uint64_value;
486
- size = sizeof(uint64_value);
487
- break;
484
+ uint64_value = NUM2ULL(object);
485
+ string = (const char *)&uint64_value;
486
+ size = sizeof(uint64_value);
487
+ break;
488
488
  case GRN_DB_FLOAT:
489
- double_value = NUM2DBL(object);
490
- string = (const char *)&double_value;
491
- size = sizeof(double_value);
492
- break;
489
+ double_value = NUM2DBL(object);
490
+ string = (const char *)&double_value;
491
+ size = sizeof(double_value);
492
+ break;
493
493
  case GRN_DB_TIME:
494
- {
495
- VALUE rb_sec, rb_usec;
494
+ {
495
+ VALUE rb_sec, rb_usec;
496
496
  int64_t sec;
497
497
  int32_t usec;
498
498
 
499
+ if (string_p) {
500
+ ID id_parse;
501
+ CONST_ID(id_parse, "parse");
502
+ object = rb_funcall(rb_cTime, id_parse, 1, object);
503
+ }
504
+
499
505
  switch (TYPE(object)) {
500
506
  case T_FIXNUM:
501
507
  case T_BIGNUM:
@@ -505,88 +511,88 @@ rb_grn_bulk_from_ruby_object_with_type (VALUE object, grn_ctx *context,
505
511
  case T_FLOAT:
506
512
  rb_sec = rb_funcall(object, rb_intern("to_i"), 0);
507
513
  rb_usec = rb_funcall(object, rb_intern("remainder"), 1,
508
- INT2NUM(1));
514
+ INT2NUM(1));
509
515
 
510
516
  sec = NUM2LL(rb_sec);
511
517
  usec = (int32_t)(NUM2DBL(rb_usec) * 1000000);
512
518
  break;
513
- case T_NIL:
514
- sec = 0;
515
- usec = 0;
516
- break;
519
+ case T_NIL:
520
+ sec = 0;
521
+ usec = 0;
522
+ break;
517
523
  default:
518
524
  sec = NUM2LL(rb_funcall(object, rb_intern("to_i"), 0));
519
525
  usec = NUM2INT(rb_funcall(object, rb_intern("usec"), 0));
520
526
  break;
521
527
  }
522
528
 
523
- time_value = GRN_TIME_PACK(sec, usec);
524
- }
525
- string = (const char *)&time_value;
526
- size = sizeof(time_value);
527
- break;
529
+ time_value = GRN_TIME_PACK(sec, usec);
530
+ }
531
+ string = (const char *)&time_value;
532
+ size = sizeof(time_value);
533
+ break;
528
534
  case GRN_DB_SHORT_TEXT:
529
535
  case GRN_DB_TEXT:
530
536
  case GRN_DB_LONG_TEXT:
531
- string = StringValuePtr(object);
532
- size = RSTRING_LEN(object);
533
- range = grn_obj_get_range(context, type);
534
- if (size > range)
535
- rb_raise(rb_eArgError,
536
- "string is too large: expected: %u <= %u",
537
- size, range);
538
- flags |= GRN_OBJ_DO_SHALLOW_COPY;
539
- break;
537
+ string = StringValuePtr(object);
538
+ size = RSTRING_LEN(object);
539
+ range = grn_obj_get_range(context, type);
540
+ if (size > range)
541
+ rb_raise(rb_eArgError,
542
+ "string is too large: expected: %u <= %u",
543
+ size, range);
544
+ flags |= GRN_OBJ_DO_SHALLOW_COPY;
545
+ break;
540
546
  case GRN_DB_TOKYO_GEO_POINT:
541
547
  case GRN_DB_WGS84_GEO_POINT:
542
- {
543
- VALUE rb_geo_point;
544
- VALUE rb_latitude, rb_longitude;
545
- if (type_id == GRN_DB_TOKYO_GEO_POINT) {
546
- rb_geo_point = rb_funcall(rb_cGrnTokyoGeoPoint,
547
- rb_intern("new"), 1, object);
548
- } else {
549
- rb_geo_point = rb_funcall(rb_cGrnWGS84GeoPoint,
550
- rb_intern("new"), 1, object);
551
- }
552
- rb_geo_point = rb_funcall(rb_geo_point, rb_intern("to_msec"), 0);
553
- rb_latitude = rb_funcall(rb_geo_point, rb_intern("latitude"), 0);
554
- rb_longitude = rb_funcall(rb_geo_point, rb_intern("longitude"), 0);
555
- geo_point_value.latitude = NUM2INT(rb_latitude);
556
- geo_point_value.longitude = NUM2INT(rb_longitude);
557
- string = (const char *)&geo_point_value;
558
- size = sizeof(geo_point_value);
559
- }
560
- break;
548
+ {
549
+ VALUE rb_geo_point;
550
+ VALUE rb_latitude, rb_longitude;
551
+ if (type_id == GRN_DB_TOKYO_GEO_POINT) {
552
+ rb_geo_point = rb_funcall(rb_cGrnTokyoGeoPoint,
553
+ rb_intern("new"), 1, object);
554
+ } else {
555
+ rb_geo_point = rb_funcall(rb_cGrnWGS84GeoPoint,
556
+ rb_intern("new"), 1, object);
557
+ }
558
+ rb_geo_point = rb_funcall(rb_geo_point, rb_intern("to_msec"), 0);
559
+ rb_latitude = rb_funcall(rb_geo_point, rb_intern("latitude"), 0);
560
+ rb_longitude = rb_funcall(rb_geo_point, rb_intern("longitude"), 0);
561
+ geo_point_value.latitude = NUM2INT(rb_latitude);
562
+ geo_point_value.longitude = NUM2INT(rb_longitude);
563
+ string = (const char *)&geo_point_value;
564
+ size = sizeof(geo_point_value);
565
+ }
566
+ break;
561
567
  case GRN_DB_VOID:
562
568
  case GRN_DB_DELIMIT:
563
569
  case GRN_DB_UNIGRAM:
564
570
  case GRN_DB_BIGRAM:
565
571
  case GRN_DB_TRIGRAM:
566
572
  case GRN_DB_MECAB:
567
- rb_type_object = GRNOBJECT2RVAL(Qnil, context, type, GRN_FALSE);
568
- rb_raise(rb_eArgError,
569
- "unbulkable type: %s",
570
- rb_grn_inspect(rb_type_object));
571
- break;
573
+ rb_type_object = GRNOBJECT2RVAL(Qnil, context, type, GRN_FALSE);
574
+ rb_raise(rb_eArgError,
575
+ "unbulkable type: %s",
576
+ rb_grn_inspect(rb_type_object));
577
+ break;
572
578
  default:
573
- if (table_type_p &&
574
- (NIL_P(object) || (string_p && RSTRING_LEN(object) == 0))) {
575
- record_id = GRN_ID_NIL;
576
- string = (const char *)&record_id;
577
- size = sizeof(record_id);
578
- if (bulk && bulk->header.domain != type_id) {
579
- grn_obj_reinit(context, bulk, type_id, 0);
580
- }
581
- } else {
582
- return RVAL2GRNBULK(object, context, bulk);
583
- }
584
- break;
579
+ if (table_type_p &&
580
+ (NIL_P(object) || (string_p && RSTRING_LEN(object) == 0))) {
581
+ record_id = GRN_ID_NIL;
582
+ string = (const char *)&record_id;
583
+ size = sizeof(record_id);
584
+ if (bulk && bulk->header.domain != type_id) {
585
+ grn_obj_reinit(context, bulk, type_id, 0);
586
+ }
587
+ } else {
588
+ return RVAL2GRNBULK(object, context, bulk);
589
+ }
590
+ break;
585
591
  }
586
592
 
587
593
  if (!bulk) {
588
- bulk = grn_obj_open(context, GRN_BULK, flags, GRN_ID_NIL);
589
- rb_grn_context_check(context, object);
594
+ bulk = grn_obj_open(context, GRN_BULK, flags, GRN_ID_NIL);
595
+ rb_grn_context_check(context, object);
590
596
  }
591
597
  GRN_TEXT_SET(context, bulk, string, size);
592
598
 
@@ -602,22 +608,22 @@ rb_grn_vector_to_ruby_object (grn_ctx *context, grn_obj *vector)
602
608
  unsigned int i, n;
603
609
 
604
610
  if (!vector)
605
- return Qnil;
611
+ return Qnil;
606
612
 
607
613
  GRN_VOID_INIT(&value);
608
614
  n = grn_vector_size(context, vector);
609
615
  array = rb_ary_new2(n);
610
616
  for (i = 0; i < n; i++) {
611
- const char *_value;
612
- unsigned int weight, length;
613
- grn_id domain;
614
-
615
- length = grn_vector_get_element(context, vector, i,
616
- &_value, &weight, &domain);
617
- grn_obj_reinit(context, &value, domain, 0);
618
- grn_bulk_write(context, &value, _value, length);
619
- rb_ary_push(array, GRNOBJ2RVAL(Qnil, context, &value, Qnil));
620
- /* UINT2NUM(weight); */ /* TODO: How handle weight? */
617
+ const char *_value;
618
+ unsigned int weight, length;
619
+ grn_id domain;
620
+
621
+ length = grn_vector_get_element(context, vector, i,
622
+ &_value, &weight, &domain);
623
+ grn_obj_reinit(context, &value, domain, 0);
624
+ grn_bulk_write(context, &value, _value, length);
625
+ rb_ary_push(array, GRNOBJ2RVAL(Qnil, context, &value, Qnil));
626
+ /* UINT2NUM(weight); */ /* TODO: How handle weight? */
621
627
  }
622
628
  GRN_OBJ_FIN(context, &value);
623
629
 
@@ -697,12 +703,15 @@ rb_grn_vector_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *vector)
697
703
  VectorFromRubyData data;
698
704
 
699
705
  if (NIL_P(object))
700
- return vector;
706
+ return vector;
701
707
 
702
708
  data.array = rb_grn_convert_to_array(object);
703
709
  data.context = context;
704
710
  data.vector = vector;
705
- GRN_VOID_INIT(&(data.value_buffer));
711
+ GRN_OBJ_INIT(&(data.value_buffer),
712
+ GRN_BULK,
713
+ 0,
714
+ vector->header.domain);
706
715
  rb_ensure(rb_grn_vector_from_ruby_object_body, (VALUE)(&data),
707
716
  rb_grn_vector_from_ruby_object_ensure, (VALUE)(&data));
708
717
 
@@ -710,64 +719,238 @@ rb_grn_vector_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *vector)
710
719
  }
711
720
 
712
721
  VALUE
713
- rb_grn_uvector_to_ruby_object (grn_ctx *context, grn_obj *uvector)
722
+ rb_grn_uvector_to_ruby_object (grn_ctx *context, grn_obj *uvector,
723
+ grn_obj *range, VALUE related_object)
714
724
  {
715
- VALUE array;
716
- grn_id *current, *end;
725
+ VALUE array = Qnil;
717
726
 
718
727
  if (!uvector)
719
- return Qnil;
720
-
721
- array = rb_ary_new();
722
- current = (grn_id *)GRN_BULK_HEAD(uvector);
723
- end = (grn_id *)GRN_BULK_CURR(uvector);
724
- while (current < end) {
725
- rb_ary_push(array, UINT2NUM(*current));
726
- current++;
728
+ return Qnil;
729
+
730
+ if (!range) {
731
+ rb_raise(rb_eTypeError,
732
+ "unknown range uvector can't be converted: <%s>",
733
+ rb_grn_inspect(related_object));
734
+ }
735
+
736
+ switch (range->header.type) {
737
+ case GRN_TYPE:
738
+ {
739
+ const char *current, *end;
740
+ grn_id range_id;
741
+ grn_obj value;
742
+ int value_size;
743
+ value_size = grn_obj_get_range(context, range);
744
+ array = rb_ary_new();
745
+ current = GRN_BULK_HEAD(uvector);
746
+ end = GRN_BULK_CURR(uvector);
747
+ range_id = grn_obj_id(context, range);
748
+ GRN_OBJ_INIT(&value, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, range_id);
749
+ while (current < end) {
750
+ VALUE rb_value;
751
+ GRN_TEXT_SET(context, &value, current, value_size);
752
+ rb_value = GRNBULK2RVAL(context, &value, range, related_object);
753
+ rb_ary_push(array, rb_value);
754
+ current += value_size;
755
+ }
756
+ GRN_OBJ_FIN(context, &value);
757
+ }
758
+ break;
759
+ case GRN_TABLE_HASH_KEY:
760
+ case GRN_TABLE_PAT_KEY:
761
+ case GRN_TABLE_DAT_KEY:
762
+ case GRN_TABLE_NO_KEY:
763
+ {
764
+ grn_id *current, *end;
765
+ VALUE rb_range = Qnil;
766
+ array = rb_ary_new();
767
+ rb_range = GRNTABLE2RVAL(context, range, GRN_FALSE);
768
+ current = (grn_id *)GRN_BULK_HEAD(uvector);
769
+ end = (grn_id *)GRN_BULK_CURR(uvector);
770
+ while (current < end) {
771
+ VALUE record = Qnil;
772
+ if (*current != GRN_ID_NIL) {
773
+ record = rb_grn_record_new(rb_range, *current, Qnil);
774
+ }
775
+ rb_ary_push(array, record);
776
+ current++;
777
+ }
778
+ }
779
+ break;
780
+ default:
781
+ rb_raise(rb_eTypeError,
782
+ "unknown range uvector can't be converted: %s(%#x): <%s>",
783
+ rb_grn_inspect_type(range->header.type),
784
+ range->header.type,
785
+ rb_grn_inspect(related_object));
786
+ break;
727
787
  }
728
788
 
729
789
  return array;
730
790
  }
731
791
 
732
- grn_obj *
733
- rb_grn_uvector_from_ruby_object (VALUE object, grn_ctx *context,
734
- grn_obj *uvector, VALUE related_object)
792
+ typedef struct {
793
+ VALUE object;
794
+ grn_ctx *context;
795
+ grn_obj *uvector;
796
+ VALUE related_object;
797
+
798
+ grn_obj element_buffer;
799
+ grn_obj *domain;
800
+ grn_bool succeeded;
801
+ } UVectorFromRubyData;
802
+
803
+ static void
804
+ rb_grn_uvector_from_ruby_object_type (UVectorFromRubyData *data)
735
805
  {
736
- VALUE *values;
806
+ VALUE object;
807
+ grn_ctx *context;
808
+ grn_obj *uvector;
809
+ grn_obj *type;
810
+ VALUE *rb_values;
737
811
  int i, n;
812
+ grn_obj *grn_value;
813
+ int value_size;
738
814
 
739
- if (NIL_P(object))
740
- return NULL;
815
+ object = data->object;
816
+ context = data->context;
817
+ uvector = data->uvector;
818
+ type = data->domain;
819
+ grn_value = &(data->element_buffer);
820
+
821
+ n = RARRAY_LEN(object);
822
+ rb_values = RARRAY_PTR(object);
823
+ value_size = grn_obj_get_range(context, type);
824
+ for (i = 0; i < n; i++) {
825
+ GRN_BULK_REWIND(grn_value);
826
+ RVAL2GRNBULK(rb_values[i], context, grn_value);
827
+ grn_bulk_write(context, uvector, GRN_BULK_HEAD(grn_value), value_size);
828
+ }
829
+
830
+ data->succeeded = GRN_TRUE;
831
+ }
832
+
833
+ static void
834
+ rb_grn_uvector_from_ruby_object_reference (UVectorFromRubyData *data)
835
+ {
836
+ VALUE object;
837
+ grn_ctx *context;
838
+ grn_obj *uvector;
839
+ VALUE related_object;
840
+ VALUE *rb_values;
841
+ int i, n;
842
+
843
+ object = data->object;
844
+ context = data->context;
845
+ uvector = data->uvector;
846
+ related_object = data->related_object;
741
847
 
742
848
  n = RARRAY_LEN(object);
743
- values = RARRAY_PTR(object);
849
+ rb_values = RARRAY_PTR(object);
744
850
  for (i = 0; i < n; i++) {
745
- VALUE value;
746
- grn_id id;
747
- void *grn_value;
748
-
749
- value = values[i];
750
- switch (TYPE(value)) {
751
- case T_FIXNUM:
752
- id = NUM2UINT(value);
753
- break;
754
- default:
755
- if (rb_respond_to(value, rb_intern("record_raw_id"))) {
756
- id = NUM2UINT(rb_funcall(value, rb_intern("record_raw_id"), 0));
757
- } else {
758
- grn_obj_unlink(context, uvector);
759
- rb_raise(rb_eArgError,
760
- "uvector value should be one of "
761
- "[Fixnum or object that has #record_raw_id]: "
762
- "%s (%s): %s",
763
- rb_grn_inspect(value),
764
- rb_grn_inspect(object),
765
- rb_grn_inspect(related_object));
766
- }
767
- break;
768
- }
769
- grn_value = &id;
770
- grn_bulk_write(context, uvector, grn_value, sizeof(grn_id));
851
+ VALUE rb_value;
852
+ grn_id id;
853
+ void *grn_value;
854
+ ID id_record_raw_id;
855
+
856
+ rb_value = rb_values[i];
857
+ switch (TYPE(rb_value)) {
858
+ case T_FIXNUM:
859
+ id = NUM2UINT(rb_value);
860
+ break;
861
+ default:
862
+ CONST_ID(id_record_raw_id, "record_raw_id");
863
+ if (rb_respond_to(rb_value, id_record_raw_id)) {
864
+ id = NUM2UINT(rb_funcall(rb_value, id_record_raw_id, 0));
865
+ } else {
866
+ rb_raise(rb_eArgError,
867
+ "uvector value should be one of "
868
+ "[Fixnum or object that has #record_raw_id]: "
869
+ "%s (%s): %s",
870
+ rb_grn_inspect(rb_value),
871
+ rb_grn_inspect(object),
872
+ rb_grn_inspect(related_object));
873
+ }
874
+ break;
875
+ }
876
+ grn_value = &id;
877
+ grn_bulk_write(context, uvector, grn_value, sizeof(grn_id));
878
+ }
879
+
880
+ data->succeeded = GRN_TRUE;
881
+ }
882
+
883
+ static VALUE
884
+ rb_grn_uvector_from_ruby_object_body (VALUE user_data)
885
+ {
886
+ UVectorFromRubyData *data = (UVectorFromRubyData *)user_data;
887
+ grn_obj *domain;
888
+
889
+ domain = data->domain;
890
+ switch (domain->header.type) {
891
+ case GRN_TYPE:
892
+ rb_grn_uvector_from_ruby_object_type(data);
893
+ break;
894
+ case GRN_TABLE_HASH_KEY:
895
+ case GRN_TABLE_PAT_KEY:
896
+ case GRN_TABLE_DAT_KEY:
897
+ case GRN_TABLE_NO_KEY:
898
+ rb_grn_uvector_from_ruby_object_reference(data);
899
+ break;
900
+ default:
901
+ rb_raise(rb_eTypeError,
902
+ "can't convert to unknown domain uvector: %s(%#x): <%s>",
903
+ rb_grn_inspect_type(domain->header.type),
904
+ domain->header.type,
905
+ rb_grn_inspect(data->related_object));
906
+ break;
907
+ }
908
+
909
+ return Qnil;
910
+ }
911
+
912
+ static VALUE
913
+ rb_grn_uvector_from_ruby_object_ensure (VALUE user_data)
914
+ {
915
+ UVectorFromRubyData *data = (UVectorFromRubyData *)user_data;
916
+
917
+ if (data->domain) {
918
+ grn_obj_unlink(data->context, data->domain);
919
+ }
920
+ GRN_OBJ_FIN(data->context, &(data->element_buffer));
921
+
922
+ return Qnil;
923
+ }
924
+
925
+ grn_obj *
926
+ rb_grn_uvector_from_ruby_object (VALUE object, grn_ctx *context,
927
+ grn_obj *uvector, VALUE related_object)
928
+ {
929
+ UVectorFromRubyData data;
930
+
931
+ if (NIL_P(object))
932
+ return NULL;
933
+
934
+ data.domain = grn_ctx_at(context, uvector->header.domain);
935
+ if (!data.domain) {
936
+ rb_raise(rb_eArgError,
937
+ "unknown domain uvector can't be converted: <%s>",
938
+ rb_grn_inspect(related_object));
939
+ }
940
+
941
+ GRN_OBJ_INIT(&(data.element_buffer), GRN_BULK, 0, uvector->header.domain);
942
+
943
+ data.object = object;
944
+ data.context = context;
945
+ data.uvector = uvector;
946
+ data.related_object = related_object;
947
+ data.succeeded = GRN_FALSE;
948
+
949
+ rb_ensure(rb_grn_uvector_from_ruby_object_body, (VALUE)(&data),
950
+ rb_grn_uvector_from_ruby_object_ensure, (VALUE)(&data));
951
+
952
+ if (!data.succeeded) {
953
+ return NULL;
771
954
  }
772
955
 
773
956
  return uvector;
@@ -775,100 +958,84 @@ rb_grn_uvector_from_ruby_object (VALUE object, grn_ctx *context,
775
958
 
776
959
  VALUE
777
960
  rb_grn_value_to_ruby_object (grn_ctx *context,
778
- grn_obj *value,
779
- grn_obj *range,
780
- VALUE related_object)
961
+ grn_obj *value,
962
+ grn_obj *range,
963
+ VALUE related_object)
781
964
  {
782
965
  if (!value)
783
- return Qnil;
966
+ return Qnil;
784
967
 
785
968
  switch (value->header.type) {
786
969
  case GRN_VOID:
787
- return Qnil;
788
- break;
970
+ return Qnil;
971
+ break;
789
972
  case GRN_BULK:
790
- if (GRN_BULK_EMPTYP(value))
791
- return Qnil;
792
- if (value->header.domain == GRN_ID_NIL && range)
793
- value->header.domain = grn_obj_id(context, range);
794
- return GRNBULK2RVAL(context, value, range, related_object);
795
- break;
973
+ if (GRN_BULK_EMPTYP(value))
974
+ return Qnil;
975
+ if (value->header.domain == GRN_ID_NIL && range)
976
+ value->header.domain = grn_obj_id(context, range);
977
+ return GRNBULK2RVAL(context, value, range, related_object);
978
+ break;
796
979
  case GRN_UVECTOR:
797
- {
798
- VALUE rb_value, rb_range = Qnil;
799
- grn_id *uvector, *uvector_end;
800
-
801
- rb_value = rb_ary_new();
802
- if (range)
803
- rb_range = GRNTABLE2RVAL(context, range, GRN_FALSE);
804
- uvector = (grn_id *)GRN_BULK_HEAD(value);
805
- uvector_end = (grn_id *)GRN_BULK_CURR(value);
806
- for (; uvector < uvector_end; uvector++) {
807
- VALUE record = Qnil;
808
- if (*uvector != GRN_ID_NIL)
809
- record = rb_grn_record_new(rb_range, *uvector, Qnil);
810
- rb_ary_push(rb_value, record);
811
- }
812
- return rb_value;
813
- }
814
- break;
980
+ return GRNUVECTOR2RVAL(context, value, range, related_object);
981
+ break;
815
982
  case GRN_VECTOR:
816
- return GRNVECTOR2RVAL(context, value);
817
- break;
983
+ return GRNVECTOR2RVAL(context, value);
984
+ break;
818
985
  default:
819
- rb_raise(rb_eGrnError,
820
- "unsupported value type: %s(%#x): %s",
821
- rb_grn_inspect_type(value->header.type),
822
- value->header.type,
823
- rb_grn_inspect(related_object));
824
- break;
986
+ rb_raise(rb_eGrnError,
987
+ "unsupported value type: %s(%#x): %s",
988
+ rb_grn_inspect_type(value->header.type),
989
+ value->header.type,
990
+ rb_grn_inspect(related_object));
991
+ break;
825
992
  }
826
993
 
827
994
  if (!range)
828
- return GRNOBJECT2RVAL(Qnil, context, value, GRN_FALSE);
995
+ return GRNOBJECT2RVAL(Qnil, context, value, GRN_FALSE);
829
996
 
830
997
  return Qnil;
831
998
  }
832
999
 
833
1000
  grn_id
834
1001
  rb_grn_id_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *table,
835
- VALUE related_object)
1002
+ VALUE related_object)
836
1003
  {
837
1004
  VALUE rb_id;
838
1005
 
839
1006
  if (NIL_P(object))
840
- return Qnil;
1007
+ return Qnil;
841
1008
 
842
1009
  if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnRecord))) {
843
- VALUE rb_table;
844
- rb_table = rb_funcall(object, rb_intern("table"), 0);
845
- if (table && RVAL2GRNOBJECT(rb_table, &context) != table) {
846
- VALUE rb_expected_table;
847
-
848
- rb_expected_table =
849
- GRNOBJECT2RVAL(Qnil, context, table, GRN_FALSE);
850
- rb_raise(rb_eGrnError,
851
- "wrong table: expected <%s>: actual <%s>",
852
- rb_grn_inspect(rb_expected_table),
853
- rb_grn_inspect(rb_table));
854
- }
855
- rb_id = rb_funcall(object, rb_intern("id"), 0);
1010
+ VALUE rb_table;
1011
+ rb_table = rb_funcall(object, rb_intern("table"), 0);
1012
+ if (table && RVAL2GRNOBJECT(rb_table, &context) != table) {
1013
+ VALUE rb_expected_table;
1014
+
1015
+ rb_expected_table =
1016
+ GRNOBJECT2RVAL(Qnil, context, table, GRN_FALSE);
1017
+ rb_raise(rb_eGrnError,
1018
+ "wrong table: expected <%s>: actual <%s>",
1019
+ rb_grn_inspect(rb_expected_table),
1020
+ rb_grn_inspect(rb_table));
1021
+ }
1022
+ rb_id = rb_funcall(object, rb_intern("id"), 0);
856
1023
  } else {
857
- rb_id = object;
1024
+ rb_id = object;
858
1025
  }
859
1026
 
860
1027
  if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_id, rb_cInteger)))
861
- rb_raise(rb_eGrnError,
862
- "should be unsigned integer or Groogna::Record: <%s>: <%s>",
863
- rb_grn_inspect(object),
864
- rb_grn_inspect(related_object));
1028
+ rb_raise(rb_eGrnError,
1029
+ "should be unsigned integer or Groogna::Record: <%s>: <%s>",
1030
+ rb_grn_inspect(object),
1031
+ rb_grn_inspect(related_object));
865
1032
 
866
1033
  return NUM2UINT(rb_id);
867
1034
  }
868
1035
 
869
1036
  VALUE
870
1037
  rb_grn_key_to_ruby_object (grn_ctx *context, const void *key, int key_size,
871
- grn_obj *table, VALUE related_object)
1038
+ grn_obj *table, VALUE related_object)
872
1039
  {
873
1040
  grn_obj bulk;
874
1041
 
@@ -880,33 +1047,33 @@ rb_grn_key_to_ruby_object (grn_ctx *context, const void *key, int key_size,
880
1047
 
881
1048
  grn_obj *
882
1049
  rb_grn_key_from_ruby_object (VALUE rb_key, grn_ctx *context,
883
- grn_obj *key, grn_id domain_id, grn_obj *domain,
884
- VALUE related_object)
1050
+ grn_obj *key, grn_id domain_id, grn_obj *domain,
1051
+ VALUE related_object)
885
1052
  {
886
1053
  grn_id id;
887
1054
 
888
1055
  if (!domain)
889
- return RVAL2GRNBULK(rb_key, context, key);
1056
+ return RVAL2GRNBULK(rb_key, context, key);
890
1057
 
891
1058
  switch (domain->header.type) {
892
1059
  case GRN_TYPE:
893
- return RVAL2GRNBULK_WITH_TYPE(rb_key, context, key, domain_id, domain);
894
- break;
1060
+ return RVAL2GRNBULK_WITH_TYPE(rb_key, context, key, domain_id, domain);
1061
+ break;
895
1062
  case GRN_TABLE_HASH_KEY:
896
1063
  case GRN_TABLE_PAT_KEY:
897
1064
  case GRN_TABLE_DAT_KEY:
898
1065
  case GRN_TABLE_NO_KEY:
899
- id = RVAL2GRNID(rb_key, context, domain, related_object);
900
- break;
1066
+ id = RVAL2GRNID(rb_key, context, domain, related_object);
1067
+ break;
901
1068
  default:
902
- if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_key, rb_cInteger)))
903
- rb_raise(rb_eGrnError,
904
- "should be unsigned integer: <%s>: <%s>",
905
- rb_grn_inspect(rb_key),
906
- rb_grn_inspect(related_object));
907
-
908
- id = NUM2UINT(rb_key);
909
- break;
1069
+ if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_key, rb_cInteger)))
1070
+ rb_raise(rb_eGrnError,
1071
+ "should be unsigned integer: <%s>: <%s>",
1072
+ rb_grn_inspect(rb_key),
1073
+ rb_grn_inspect(related_object));
1074
+
1075
+ id = NUM2UINT(rb_key);
1076
+ break;
910
1077
  }
911
1078
 
912
1079
  GRN_TEXT_SET(context, key, &id, sizeof(id));
@@ -915,28 +1082,28 @@ rb_grn_key_from_ruby_object (VALUE rb_key, grn_ctx *context,
915
1082
 
916
1083
  grn_obj *
917
1084
  rb_grn_value_from_ruby_object (VALUE object, grn_ctx *context,
918
- grn_obj *value, grn_id type_id, grn_obj *type)
1085
+ grn_obj *value, grn_id type_id, grn_obj *type)
919
1086
  {
920
1087
  grn_bool string_p, table_type_p;
921
1088
 
922
1089
  string_p = rb_type(object) == T_STRING;
923
1090
  table_type_p = (GRN_TABLE_HASH_KEY <= type->header.type &&
924
- type->header.type <= GRN_TABLE_NO_KEY);
1091
+ type->header.type <= GRN_TABLE_NO_KEY);
925
1092
  if (!string_p) {
926
- return RVAL2GRNBULK_WITH_TYPE(object, context, value, type_id, type);
1093
+ return RVAL2GRNBULK_WITH_TYPE(object, context, value, type_id, type);
927
1094
  }
928
1095
 
929
1096
  if (table_type_p && RSTRING_LEN(object) == 0) {
930
- if (value) {
931
- if (value->header.domain != type_id) {
932
- grn_obj_reinit(context, value, type_id, 0);
933
- }
934
- } else {
935
- value = grn_obj_open(context, GRN_BULK, 0, type_id);
936
- rb_grn_context_check(context, object);
937
- }
938
- GRN_RECORD_SET(context, value, GRN_ID_NIL);
939
- return value;
1097
+ if (value) {
1098
+ if (value->header.domain != type_id) {
1099
+ grn_obj_reinit(context, value, type_id, 0);
1100
+ }
1101
+ } else {
1102
+ value = grn_obj_open(context, GRN_BULK, 0, type_id);
1103
+ rb_grn_context_check(context, object);
1104
+ }
1105
+ GRN_RECORD_SET(context, value, GRN_ID_NIL);
1106
+ return value;
940
1107
  }
941
1108
 
942
1109
  return RVAL2GRNBULK(object, context, value);
@@ -946,12 +1113,12 @@ grn_obj *
946
1113
  rb_grn_obj_from_ruby_object (VALUE rb_object, grn_ctx *context, grn_obj **_obj)
947
1114
  {
948
1115
  if (RVAL2CBOOL(rb_obj_is_kind_of(rb_object, rb_cGrnObject))) {
949
- if (*_obj) {
950
- grn_obj_unlink(context, *_obj); /* TODO: reduce memory allocation */
951
- }
952
- *_obj = RVAL2GRNOBJECT(rb_object, &context);
1116
+ if (*_obj) {
1117
+ grn_obj_unlink(context, *_obj); /* TODO: reduce memory allocation */
1118
+ }
1119
+ *_obj = RVAL2GRNOBJECT(rb_object, &context);
953
1120
  } else {
954
- *_obj = RVAL2GRNBULK(rb_object, context, *_obj);
1121
+ *_obj = RVAL2GRNBULK(rb_object, context, *_obj);
955
1122
  }
956
1123
 
957
1124
  return *_obj;
@@ -959,24 +1126,24 @@ rb_grn_obj_from_ruby_object (VALUE rb_object, grn_ctx *context, grn_obj **_obj)
959
1126
 
960
1127
  VALUE
961
1128
  rb_grn_obj_to_ruby_object (VALUE klass, grn_ctx *context,
962
- grn_obj *obj, VALUE related_object)
1129
+ grn_obj *obj, VALUE related_object)
963
1130
  {
964
1131
  if (!obj)
965
- return Qnil;
1132
+ return Qnil;
966
1133
 
967
1134
  /* if (NIL_P(klass)) */
968
- /* klass = GRNOBJECT2RCLASS(obj); */
1135
+ /* klass = GRNOBJECT2RCLASS(obj); */
969
1136
 
970
1137
  switch (obj->header.type) {
971
1138
  case GRN_VOID:
972
- if (GRN_BULK_VSIZE(obj) > 0)
973
- return rb_str_new(GRN_BULK_HEAD(obj), GRN_BULK_VSIZE(obj));
974
- else
975
- return Qnil;
976
- break;
1139
+ if (GRN_BULK_VSIZE(obj) > 0)
1140
+ return rb_str_new(GRN_BULK_HEAD(obj), GRN_BULK_VSIZE(obj));
1141
+ else
1142
+ return Qnil;
1143
+ break;
977
1144
  case GRN_BULK:
978
- return GRNBULK2RVAL(context, obj, NULL, related_object);
979
- break;
1145
+ return GRNBULK2RVAL(context, obj, NULL, related_object);
1146
+ break;
980
1147
  /* case GRN_PTR: */
981
1148
  /* case GRN_UVECTOR: */
982
1149
  /* case GRN_PVECTOR: */
@@ -1002,12 +1169,12 @@ rb_grn_obj_to_ruby_object (VALUE klass, grn_ctx *context,
1002
1169
  /* case GRN_COLUMN_VAR_SIZE: */
1003
1170
  /* case GRN_COLUMN_INDEX: */
1004
1171
  default:
1005
- rb_raise(rb_eTypeError,
1006
- "unsupported groonga object: %s(%#x): <%s>",
1007
- rb_grn_inspect_type(obj->header.type),
1008
- obj->header.type,
1009
- rb_grn_inspect(related_object));
1010
- break;
1172
+ rb_raise(rb_eTypeError,
1173
+ "unsupported groonga object: %s(%#x): <%s>",
1174
+ rb_grn_inspect_type(obj->header.type),
1175
+ obj->header.type,
1176
+ rb_grn_inspect(related_object));
1177
+ break;
1011
1178
  }
1012
1179
 
1013
1180
  return Qnil;