/* * call-seq: * expression[name] -> 変数の値 * expression[offset] -> 変数の値 * * _expression_で使用可能な変数のうち、名前が_name_または * _offset_番目にExpression#append_objectされた変数の値を返 * す。 */ static VALUE rb_grn_expression_array_reference (VALUE self, VALUE rb_name_or_offset) { grn_ctx *context = NULL; grn_obj *expression, *variable, *value; char *name = NULL; unsigned name_size = 0; int offset; rb_grn_expression_deconstruct(SELF(self), &expression, &context, NULL, NULL, NULL, NULL, NULL); switch (TYPE(rb_name_or_offset)) { case T_STRING: name = RSTRING_PTR(rb_name_or_offset); name_size = RSTRING_LEN(rb_name_or_offset); variable = grn_expr_get_var(context, expression, name, name_size); return GRNBULK2RVAL(context, variable, NULL, self); break; case T_FIXNUM: offset = NUM2INT(rb_name_or_offset); value = grn_expr_get_var_by_offset(context, expression, offset); return GRNBULK2RVAL(context, value, NULL, self); break; default: rb_raise(rb_eArgError, "xxx"); break; } return Qnil; }
/* * Extracts keywords from _expression_. The keywords order isn't * guaranteed. * * @example * expression.parse("Ruby OR Groonga") * expression.keywords #=> ["Groonga", "Ruby"] * * @overload keywords * @return [::Array<String>] the extracted keywords * * @since 4.0.6 */ static VALUE rb_grn_expression_get_keywords (VALUE self) { grn_ctx *context = NULL; grn_obj *expression; grn_obj keywords; VALUE rb_keywords = rb_ary_new(); rb_grn_expression_deconstruct(SELF(self), &expression, &context, NULL, NULL, NULL, NULL, NULL); GRN_PTR_INIT(&keywords, GRN_OBJ_VECTOR, GRN_ID_NIL); grn_expr_get_keywords(context, expression, &keywords); { int i, n_keywords; n_keywords = GRN_BULK_VSIZE(&keywords) / sizeof(grn_obj *); for (i = 0; i < n_keywords; i++) { grn_obj *keyword = GRN_PTR_VALUE_AT(&keywords, i); rb_ary_push(rb_keywords, GRNBULK2RVAL(context, keyword, NULL, self)); } } GRN_OBJ_FIN(context, &keywords); return rb_keywords; }
/* * @overload [](id) * @param id [Integer, Groonga::Record] The record ID for the * column value. * * @return [Object] The value for the record ID. */ static VALUE rb_grn_column_cache_array_reference (VALUE self, VALUE rb_id) { RbGrnColumnCache *rb_grn_column_cache; grn_id id; void *value; size_t value_size = 0; TypedData_Get_Struct(self, RbGrnColumnCache, &data_type, rb_grn_column_cache); if (!rb_grn_column_cache->column_cache) { return Qnil; } id = rb_grn_id_from_ruby_object(rb_id, rb_grn_column_cache->context, rb_grn_column_cache->table, self); value = grn_column_cache_ref(rb_grn_column_cache->context, rb_grn_column_cache->column_cache, id, &value_size); rb_grn_context_check(rb_grn_column_cache->context, self); GRN_TEXT_SET_REF(&(rb_grn_column_cache->buffer), value, value_size); return GRNBULK2RVAL(rb_grn_column_cache->context, &(rb_grn_column_cache->buffer), rb_grn_column_cache->range, self); }
VALUE rb_grn_obj_to_ruby_object (VALUE klass, grn_ctx *context, grn_obj *obj, VALUE related_object) { if (!obj) return Qnil; /* if (NIL_P(klass)) */ /* klass = GRNOBJECT2RCLASS(obj); */ switch (obj->header.type) { case GRN_VOID: if (GRN_BULK_VSIZE(obj) > 0) return rb_str_new(GRN_BULK_HEAD(obj), GRN_BULK_VSIZE(obj)); else return Qnil; break; case GRN_BULK: return GRNBULK2RVAL(context, obj, NULL, related_object); break; /* case GRN_PTR: */ /* case GRN_UVECTOR: */ /* case GRN_PVECTOR: */ case GRN_VECTOR: return GRNVECTOR2RVAL(context, obj); break; /* case GRN_MSG: */ /* case GRN_QUERY: */ /* case GRN_ACCESSOR: */ /* case GRN_SNIP: */ /* case GRN_PATSNIP: */ /* case GRN_CURSOR_TABLE_HASH_KEY: */ /* case GRN_CURSOR_TABLE_PAT_KEY: */ /* case GRN_CURSOR_TABLE_NO_KEY: */ /* case GRN_CURSOR_COLUMN_INDEX: */ /* case GRN_TYPE: */ /* case GRN_PROC: */ /* case GRN_EXPR: */ /* case GRN_TABLE_HASH_KEY: */ /* case GRN_TABLE_PAT_KEY: */ /* case GRN_TABLE_DAT_KEY: */ /* case GRN_TABLE_NO_KEY: */ /* case GRN_DB: */ /* case GRN_COLUMN_FIX_SIZE: */ /* case GRN_COLUMN_VAR_SIZE: */ /* case GRN_COLUMN_INDEX: */ default: rb_raise(rb_eTypeError, "unsupported groonga object: %s(%#x): <%s>", rb_grn_inspect_type(obj->header.type), obj->header.type, rb_grn_inspect(related_object)); break; } return Qnil; }
VALUE rb_grn_value_to_ruby_object (grn_ctx *context, grn_obj *value, grn_obj *range, VALUE related_object) { if (!value) return Qnil; switch (value->header.type) { case GRN_VOID: return Qnil; break; case GRN_BULK: if (GRN_BULK_EMPTYP(value)) return Qnil; if (value->header.domain == GRN_ID_NIL && range) value->header.domain = grn_obj_id(context, range); return GRNBULK2RVAL(context, value, range, related_object); break; case GRN_UVECTOR: { VALUE rb_value, rb_range = Qnil; grn_id *uvector, *uvector_end; rb_value = rb_ary_new(); if (range) rb_range = GRNTABLE2RVAL(context, range, GRN_FALSE); uvector = (grn_id *)GRN_BULK_HEAD(value); uvector_end = (grn_id *)GRN_BULK_CURR(value); for (; uvector < uvector_end; uvector++) { VALUE record = Qnil; if (*uvector != GRN_ID_NIL) record = rb_grn_record_new(rb_range, *uvector, Qnil); rb_ary_push(rb_value, record); } return rb_value; } break; case GRN_VECTOR: return GRNVECTOR2RVAL(context, value); break; default: rb_raise(rb_eGrnError, "unsupported value type: %s(%#x): %s", rb_grn_inspect_type(value->header.type), value->header.type, rb_grn_inspect(related_object)); break; } if (!range) return GRNOBJECT2RVAL(Qnil, context, value, GRN_FALSE); return Qnil; }
VALUE rb_grn_key_to_ruby_object (grn_ctx *context, const void *key, int key_size, grn_obj *table, VALUE related_object) { grn_obj bulk; GRN_OBJ_INIT(&bulk, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, table->header.domain); GRN_TEXT_SET(context, &bulk, key, key_size); return GRNBULK2RVAL(context, &bulk, NULL, related_object); }
/* * Dump execution plan of the `expression` in string. * * @overload dump_plan * @since 4.0.7 */ static VALUE rb_grn_expression_dump_plan (VALUE self) { grn_ctx *context = NULL; grn_obj *expression; grn_obj dumped_plan; VALUE rb_dumped_plan; rb_grn_expression_deconstruct(SELF(self), &expression, &context, NULL, NULL, NULL, NULL, NULL); GRN_TEXT_INIT(&dumped_plan, 0); grn_expr_dump_plan(context, expression, &dumped_plan); rb_dumped_plan = GRNBULK2RVAL(context, &dumped_plan, NULL, self); grn_obj_unlink(context, &dumped_plan); return rb_dumped_plan; }
VALUE rb_grn_value_to_ruby_object (grn_ctx *context, grn_obj *value, grn_obj *range, VALUE related_object) { if (!value) return Qnil; switch (value->header.type) { case GRN_VOID: return Qnil; break; case GRN_BULK: if (GRN_BULK_EMPTYP(value)) return Qnil; if (value->header.domain == GRN_ID_NIL && range) value->header.domain = grn_obj_id(context, range); return GRNBULK2RVAL(context, value, range, related_object); break; case GRN_UVECTOR: return GRNUVECTOR2RVAL(context, value, range, related_object); break; case GRN_VECTOR: return GRNVECTOR2RVAL(context, value); break; default: rb_raise(rb_eGrnError, "unsupported value type: %s(%#x): %s", rb_grn_inspect_type(value->header.type), value->header.type, rb_grn_inspect(related_object)); break; } if (!range) return GRNOBJECT2RVAL(Qnil, context, value, GRN_FALSE); return Qnil; }
static VALUE rb_grn_table_key_support_get_value_by_key (VALUE self, VALUE rb_key) { grn_ctx *context; grn_obj *table, *value, *range; grn_id id; id = rb_grn_table_key_support_get(self, rb_key); if (id == GRN_ID_NIL) return Qnil; rb_grn_table_key_support_deconstruct(SELF(self), &table, &context, NULL, NULL, NULL, &value, NULL, &range, NULL); GRN_BULK_REWIND(value); grn_obj_get_value(context, table, id, value); rb_grn_context_check(context, self); return GRNBULK2RVAL(context, value, range, self); }
VALUE rb_grn_uvector_to_ruby_object (grn_ctx *context, grn_obj *uvector, grn_obj *range, VALUE related_object) { VALUE array = Qnil; if (!uvector) return Qnil; if (!range) { rb_raise(rb_eTypeError, "unknown range uvector can't be converted: <%s>", rb_grn_inspect(related_object)); } switch (range->header.type) { case GRN_TYPE: { const char *current, *end; grn_id range_id; grn_obj value; int value_size; value_size = grn_obj_get_range(context, range); array = rb_ary_new(); current = GRN_BULK_HEAD(uvector); end = GRN_BULK_CURR(uvector); range_id = grn_obj_id(context, range); GRN_OBJ_INIT(&value, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, range_id); while (current < end) { VALUE rb_value; GRN_TEXT_SET(context, &value, current, value_size); rb_value = GRNBULK2RVAL(context, &value, range, related_object); rb_ary_push(array, rb_value); current += value_size; } GRN_OBJ_FIN(context, &value); break; } case GRN_TABLE_HASH_KEY: case GRN_TABLE_PAT_KEY: case GRN_TABLE_DAT_KEY: case GRN_TABLE_NO_KEY: { grn_id *current, *end; VALUE rb_range = Qnil; array = rb_ary_new(); rb_range = GRNTABLE2RVAL(context, range, GRN_FALSE); current = (grn_id *)GRN_BULK_HEAD(uvector); end = (grn_id *)GRN_BULK_CURR(uvector); while (current < end) { VALUE record = Qnil; if (*current != GRN_ID_NIL) { record = rb_grn_record_new(rb_range, *current, Qnil); } rb_ary_push(array, record); current++; } break; } default: rb_raise(rb_eTypeError, "unknown range uvector can't be converted: %s(%#x): <%s>", rb_grn_inspect_type(range->header.type), range->header.type, rb_grn_inspect(related_object)); break; } return array; }