/* * Executes a prefix-search operation. Prefix-serach operation checks * whether `text` starts with `prefix` or not. * * @example Executes prefix-search operations with the default context * Groonga::Operator::PREFIX.exec("Hello Rroonga", "Hello") # => true * Groonga::Operator::PREFIX.exec("Hello Rroonga", "Rroonga") # => false * * @example Executes prefix-search operations with the specified context * context = Groonga::Context.new * Groonga::Operator::PREFIX.exec("Hello Rroonga", "Hello", * :context => context) # => true * Groonga::Operator::PREFIX.exec("Hello Rroonga", "Rroonga", * :context => context) # => false * * @overload exec(text, prefix, options={}) * @param text [String] The text to be searched. * @param prefix [String] The prefix to be contained. * @param options [::Hash] The options. * @option options [Groonga::Context] (Groonga::Context.default) * The context to executes the operation. * @return [Boolean] `true` if `text` starts with `prefix`, `false` * otherwise. */ static VALUE rb_grn_prefix_operator_exec (int argc, VALUE *argv, VALUE self) { grn_bool have_prefix; VALUE rb_text; VALUE rb_prefix; VALUE rb_options; VALUE rb_context; grn_ctx *context; grn_obj text; grn_obj prefix; rb_scan_args(argc, argv, "21", &rb_text, &rb_prefix, &rb_options); rb_grn_scan_options(rb_options, "context", &rb_context, NULL); context = rb_grn_context_ensure(&rb_context); GRN_VOID_INIT(&text); GRN_VOID_INIT(&prefix); RVAL2GRNBULK(rb_text, context, &text); RVAL2GRNBULK(rb_prefix, context, &prefix); have_prefix = grn_operator_exec_prefix(context, &text, &prefix); GRN_OBJ_FIN(context, &text); GRN_OBJ_FIN(context, &prefix); return CBOOL2RVAL(have_prefix); }
void grn_ctx_impl_mrb_init(grn_ctx *ctx) { if (!grn_ctx_impl_mrb_mruby_enabled) { ctx->impl->mrb.state = NULL; ctx->impl->mrb.base_directory[0] = '\0'; ctx->impl->mrb.module = NULL; ctx->impl->mrb.object_class = NULL; ctx->impl->mrb.checked_procs = NULL; ctx->impl->mrb.registered_plugins = NULL; ctx->impl->mrb.builtin.time_class = NULL; ctx->impl->mrb.groonga.operator_class = NULL; } else { mrb_state *mrb; mrb = mrb_open(); ctx->impl->mrb.state = mrb; ctx->impl->mrb.base_directory[0] = '\0'; grn_ctx_impl_mrb_init_bindings(ctx); /* TODO: Implement better error handling on init. */ if (ctx->impl->mrb.state->exc) { mrb_print_error(mrb); } ctx->impl->mrb.checked_procs = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); ctx->impl->mrb.registered_plugins = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.from)); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.to)); ctx->impl->mrb.builtin.time_class = mrb_class_get(mrb, "Time"); } }
/* * Executes a less operation. * * @example Executes less operations with the default context * Groonga::Operator::LESS.exec(1, 2) # => true * Groonga::Operator::LESS.exec(2, 1) # => false * * @example Executes less operations with the specified context * context = Groonga::Context.new * Groonga::Operator::LESS.exec(1, 2, * :context => context) # => true * Groonga::Operator::LESS.exec(2, 1, * :context => context) # => false * * @overload exec(x, y, options={}) * @param x [::Object] The left hand side value. * @param y [::Object] The right hand side value. * @param options [::Hash] The options. * @option options [Groonga::Context] (Groonga::Context.default) * The context to executes the operation. * @return [Boolean] `true` if `x` is less than `y`, `false` * otherwise. */ static VALUE rb_grn_less_operator_exec (int argc, VALUE *argv, VALUE self) { grn_bool less; VALUE rb_x; VALUE rb_y; VALUE rb_options; VALUE rb_context; grn_ctx *context; grn_obj x; grn_obj y; rb_scan_args(argc, argv, "21", &rb_x, &rb_y, &rb_options); rb_grn_scan_options(rb_options, "context", &rb_context, NULL); context = rb_grn_context_ensure(&rb_context); GRN_VOID_INIT(&x); GRN_VOID_INIT(&y); RVAL2GRNBULK(rb_x, context, &x); RVAL2GRNBULK(rb_y, context, &y); less = grn_operator_exec_less(context, &x, &y); GRN_OBJ_FIN(context, &x); GRN_OBJ_FIN(context, &y); return CBOOL2RVAL(less); }
void grn_mrb_value_to_raw_data_buffer_init(mrb_state *mrb, grn_mrb_value_to_raw_data_buffer *buffer) { GRN_VOID_INIT(&(buffer->from)); GRN_VOID_INIT(&(buffer->to)); }
void cut_setup(void) { logger = setup_grn_logger(); grn_ctx_init(&context, 0); GRN_VOID_INIT(&src); GRN_VOID_INIT(&dest); }
static grn_bool exec_match_vector_bulk(grn_ctx *ctx, grn_obj *vector, grn_obj *query) { grn_bool matched = GRN_FALSE; unsigned int i, size; grn_obj element; size = grn_vector_size(ctx, vector); GRN_VOID_INIT(&element); for (i = 0; i < size; i++) { const char *content; unsigned int content_size; grn_id domain_id; content_size = grn_vector_get_element(ctx, vector, i, &content, NULL, &domain_id); grn_obj_reinit(ctx, &element, domain_id, 0); grn_bulk_write(ctx, &element, content, content_size); if (grn_operator_exec_equal(ctx, &element, query)) { matched = GRN_TRUE; break; } } GRN_OBJ_FIN(ctx, &element); return matched; }
grn_obj * rb_grn_vector_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *vector) { VALUE *values; grn_obj value; int i, n; if (vector) GRN_OBJ_INIT(vector, GRN_VECTOR, 0, GRN_ID_NIL); else vector = grn_obj_open(context, GRN_VECTOR, 0, 0); if (NIL_P(object)) return vector; GRN_VOID_INIT(&value); n = RARRAY_LEN(object); values = RARRAY_PTR(object); for (i = 0; i < n; i++) { grn_obj *_value = &value; RVAL2GRNOBJ(values[i], context, &_value); grn_vector_add_element(context, vector, GRN_BULK_HEAD(&value), GRN_BULK_VSIZE(&value), 0, value.header.domain); } GRN_OBJ_FIN(context, &value); return vector; }
void setup (void) { grn_ctx_init(&context, 0); database = grn_db_create(&context, NULL, NULL); GRN_VOID_INIT(&buffer); }
VALUE rb_grn_vector_to_ruby_object (grn_ctx *context, grn_obj *vector) { VALUE array; grn_obj value; unsigned int i, n; if (!vector) return Qnil; GRN_VOID_INIT(&value); n = grn_vector_size(context, vector); array = rb_ary_new2(n); for (i = 0; i < n; i++) { const char *_value; unsigned int weight, length; grn_id domain; length = grn_vector_get_element(context, vector, i, &_value, &weight, &domain); grn_obj_reinit(context, &value, domain, 0); grn_bulk_write(context, &value, _value, length); rb_ary_push(array, GRNOBJ2RVAL(Qnil, context, &value, Qnil)); } GRN_OBJ_FIN(context, &value); return array; }
static int rb_grn_hash_from_ruby_hash_body (VALUE rb_key, VALUE rb_value, VALUE user_data) { RbGrnHashFromRubyHashData *data = (RbGrnHashFromRubyHashData *)user_data; grn_obj *value; int added; rb_key = rb_grn_convert_to_string(rb_key); grn_hash_add(data->context, data->hash, RSTRING_PTR(rb_key), RSTRING_LEN(rb_key), (void **)&value, &added); rb_grn_context_check(data->context, data->rb_hash); if (added) { GRN_VOID_INIT(value); } RVAL2GRNBULK(rb_value, data->context, value); return ST_CONTINUE; }
ConditionConverter::ConditionConverter(grn_ctx *ctx, grn_obj *table, bool is_storage_mode) : ctx_(ctx), table_(table), is_storage_mode_(is_storage_mode) { GRN_TEXT_INIT(&column_name_, 0); GRN_VOID_INIT(&value_); }
void grn_ctx_impl_mrb_init(grn_ctx *ctx) { if (!grn_ctx_impl_mrb_mruby_enabled) { ctx->impl->mrb.state = NULL; ctx->impl->mrb.base_directory[0] = '\0'; ctx->impl->mrb.module = NULL; ctx->impl->mrb.object_class = NULL; ctx->impl->mrb.checked_procs = NULL; ctx->impl->mrb.registered_plugins = NULL; ctx->impl->mrb.builtin.time_class = NULL; ctx->impl->mrb.groonga.operator_class = NULL; } else { mrb_state *mrb; mrb = mrb_open_allocf(grn_ctx_impl_mrb_allocf, ctx); ctx->impl->mrb.state = mrb; ctx->impl->mrb.base_directory[0] = '\0'; grn_ctx_impl_mrb_init_bindings(ctx); if (ctx->impl->mrb.state->exc) { mrb_value reason; reason = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0); ERR(GRN_UNKNOWN_ERROR, "failed to initialize mruby: %.*s", RSTRING_LEN(reason), RSTRING_PTR(reason)); mrb_close(ctx->impl->mrb.state); ctx->impl->mrb.state = NULL; } else { ctx->impl->mrb.checked_procs = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); ctx->impl->mrb.registered_plugins = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.from)); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.to)); ctx->impl->mrb.builtin.time_class = mrb_class_get(mrb, "Time"); } } }
static void output_result(grn_ctx *ctx, mrb_value result) { grn_obj grn_result; GRN_OUTPUT_MAP_OPEN("result", 1); GRN_OUTPUT_CSTR("value"); GRN_VOID_INIT(&grn_result); if (grn_mrb_to_grn(ctx, result, &grn_result) == GRN_SUCCESS) { GRN_OUTPUT_OBJ(&grn_result, NULL); } else { GRN_OUTPUT_CSTR("unsupported return value"); } grn_obj_unlink(ctx, &grn_result); GRN_OUTPUT_MAP_CLOSE(); }
void cut_setup(void) { remove_tmp_directory(); g_mkdir_with_parents(tmp_directory, 0700); context = NULL; logger = setup_grn_logger(); context = g_new0(grn_ctx, 1); grn_ctx_init(context, 0); database = grn_db_create(context, cut_build_path(tmp_directory, "table.db", NULL), NULL); table = NULL; column = NULL; GRN_VOID_INIT(&buffer); }
static void construct_object(gconstpointer data, grn_builtin_type type, grn_obj *object) { switch (type) { case GRN_DB_VOID: GRN_VOID_INIT(object); break; case GRN_DB_BOOL: GRN_BOOL_INIT(object, 0); GRN_BOOL_SET(&context, object, gcut_data_get_boolean(data, "value")); break; case GRN_DB_INT8: GRN_INT8_INIT(object, 0); GRN_INT8_SET(&context, object, gcut_data_get_int(data, "value")); break; case GRN_DB_UINT8: GRN_UINT8_INIT(object, 0); GRN_UINT8_SET(&context, object, gcut_data_get_uint(data, "value")); break; case GRN_DB_INT16: GRN_INT16_INIT(object, 0); GRN_INT16_SET(&context, object, gcut_data_get_int(data, "value")); break; case GRN_DB_UINT16: GRN_UINT16_INIT(object, 0); GRN_UINT16_SET(&context, object, gcut_data_get_uint(data, "value")); break; case GRN_DB_INT32: GRN_INT32_INIT(object, 0); GRN_INT32_SET(&context, object, gcut_data_get_int(data, "value")); break; case GRN_DB_UINT32: GRN_UINT32_INIT(object, 0); GRN_UINT32_SET(&context, object, gcut_data_get_uint(data, "value")); break; case GRN_DB_INT64: GRN_INT64_INIT(object, 0); GRN_INT64_SET(&context, object, gcut_data_get_int64(data, "value")); break; case GRN_DB_UINT64: GRN_UINT64_INIT(object, 0); GRN_UINT64_SET(&context, object, gcut_data_get_uint64(data, "value")); break; case GRN_DB_FLOAT: GRN_FLOAT_INIT(object, 0); GRN_FLOAT_SET(&context, object, gcut_data_get_double(data, "value")); break; case GRN_DB_TIME: GRN_TIME_INIT(object, 0); GRN_TIME_SET(&context, object, gcut_data_get_int64(data, "value")); break; case GRN_DB_SHORT_TEXT: GRN_SHORT_TEXT_INIT(object, 0); GRN_TEXT_SETS(&context, object, gcut_data_get_string(data, "value")); break; case GRN_DB_TEXT: GRN_TEXT_INIT(object, 0); GRN_TEXT_SETS(&context, object, gcut_data_get_string(data, "value")); break; case GRN_DB_LONG_TEXT: GRN_LONG_TEXT_INIT(object, 0); GRN_TEXT_SETS(&context, object, gcut_data_get_string(data, "value")); break; case GRN_DB_TOKYO_GEO_POINT: GRN_TOKYO_GEO_POINT_INIT(object, 0); GRN_GEO_POINT_SET(&context, object, gcut_data_get_int(data, "latitude"), gcut_data_get_int(data, "longitude")); break; case GRN_DB_WGS84_GEO_POINT: GRN_WGS84_GEO_POINT_INIT(object, 0); GRN_GEO_POINT_SET(&context, object, gcut_data_get_int(data, "latitude"), gcut_data_get_int(data, "longitude")); break; default: cut_fail("unknown type: %d", type); break; } }
static mrb_value mrb_grn_expression_append_constant(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_obj *expr; mrb_value mrb_constant; mrb_value mrb_op; grn_operator op; int n_args; expr = DATA_PTR(self); mrb_get_args(mrb, "ooi", &mrb_constant, &mrb_op, &n_args); op = grn_mrb_value_to_operator(mrb, mrb_op); switch (mrb_type(mrb_constant)) { case MRB_TT_FALSE : if (mrb_nil_p(mrb_constant)) { grn_obj constant; GRN_VOID_INIT(&constant); grn_expr_append_const(ctx, expr, &constant, op, n_args); GRN_OBJ_FIN(ctx, &constant); } else { grn_obj constant; GRN_BOOL_INIT(&constant, 0); GRN_BOOL_SET(ctx, &constant, GRN_FALSE); grn_expr_append_const(ctx, expr, &constant, op, n_args); GRN_OBJ_FIN(ctx, &constant); } break; case MRB_TT_TRUE : { grn_obj constant; GRN_BOOL_INIT(&constant, 0); GRN_BOOL_SET(ctx, &constant, GRN_TRUE); grn_expr_append_const(ctx, expr, &constant, op, n_args); GRN_OBJ_FIN(ctx, &constant); } break; case MRB_TT_FIXNUM : grn_expr_append_const_int(ctx, expr, mrb_fixnum(mrb_constant), op, n_args); break; case MRB_TT_SYMBOL : { const char *value; mrb_int value_length; value = mrb_sym2name_len(mrb, mrb_symbol(mrb_constant), &value_length); grn_expr_append_const_str(ctx, expr, value, value_length, op, n_args); } break; case MRB_TT_FLOAT : { grn_obj constant; GRN_FLOAT_INIT(&constant, 0); GRN_FLOAT_SET(ctx, &constant, mrb_float(mrb_constant)); grn_expr_append_const(ctx, expr, &constant, op, n_args); GRN_OBJ_FIN(ctx, &constant); } break; case MRB_TT_STRING : grn_expr_append_const_str(ctx, expr, RSTRING_PTR(mrb_constant), RSTRING_LEN(mrb_constant), op, n_args); break; default : { struct RClass *klass; klass = mrb_class(mrb, mrb_constant); if (klass == ctx->impl->mrb.builtin.time_class) { grn_obj constant; mrb_value mrb_sec; mrb_value mrb_usec; mrb_sec = mrb_funcall(mrb, mrb_constant, "to_i", 0); mrb_usec = mrb_funcall(mrb, mrb_constant, "usec", 0); GRN_TIME_INIT(&constant, 0); GRN_TIME_SET(ctx, &constant, GRN_TIME_PACK(mrb_fixnum(mrb_sec), mrb_fixnum(mrb_usec))); grn_expr_append_const(ctx, expr, &constant, op, n_args); GRN_OBJ_FIN(ctx, &constant); } else { mrb_raisef(mrb, E_ARGUMENT_ERROR, "unsupported constant to append to expression: %S", mrb_constant); } } break; } grn_mrb_ctx_check(mrb); return mrb_nil_value(); }
void setup (void) { grn_ctx_init(&context, GRN_CTX_USE_QL); GRN_VOID_INIT(&buffer); }
static grn_bool exec_equal(grn_ctx *ctx, grn_obj *x, grn_obj *y) { switch (x->header.type) { case GRN_BULK : if (y->header.type == GRN_BULK) { grn_bool is_equal = GRN_FALSE; DO_EQ(x, y, is_equal); return is_equal; } else { return GRN_FALSE; } break; case GRN_VECTOR : if (y->header.type == GRN_VECTOR) { grn_bool is_equal = GRN_TRUE; unsigned int x_size = grn_vector_size(ctx, x); unsigned int y_size = grn_vector_size(ctx, y); unsigned int i; grn_obj x_element; grn_obj y_element; if (x_size != y_size) { return GRN_FALSE; } GRN_VOID_INIT(&x_element); GRN_VOID_INIT(&y_element); for (i = 0; i < x_size; i++) { const char *x_value; unsigned int x_size; unsigned int x_weight; grn_id x_domain; const char *y_value; unsigned int y_size; unsigned int y_weight; grn_id y_domain; x_size = grn_vector_get_element(ctx, x, i, &x_value, &x_weight, &x_domain); y_size = grn_vector_get_element(ctx, y, i, &y_value, &y_weight, &y_domain); if (x_weight != y_weight) { is_equal = GRN_FALSE; break; } grn_obj_reinit(ctx, &x_element, x_domain, 0); grn_bulk_write(ctx, &x_element, x_value, x_size); grn_obj_reinit(ctx, &y_element, y_domain, 0); grn_bulk_write(ctx, &y_element, y_value, y_size); DO_EQ((&x_element), (&y_element), is_equal); if (!is_equal) { break; } } GRN_OBJ_FIN(ctx, &x_element); GRN_OBJ_FIN(ctx, &y_element); return is_equal; } else { return GRN_FALSE; } break; case GRN_UVECTOR : if (y->header.type == GRN_UVECTOR) { grn_bool is_equal = GRN_TRUE; unsigned int x_size = grn_vector_size(ctx, x); unsigned int y_size = grn_vector_size(ctx, y); unsigned int i; grn_obj x_element; grn_obj y_element; unsigned int x_element_size = grn_uvector_element_size(ctx, x); unsigned int y_element_size = grn_uvector_element_size(ctx, y); if (x_size != y_size) { return GRN_FALSE; } GRN_OBJ_INIT(&x_element, GRN_BULK, 0, x->header.domain); GRN_OBJ_INIT(&y_element, GRN_BULK, 0, y->header.domain); for (i = 0; i < x_size; i++) { const char *x_value; const char *y_value; x_value = GRN_BULK_HEAD(x) + (x_element_size * i); y_value = GRN_BULK_HEAD(y) + (y_element_size * i); GRN_BULK_REWIND(&x_element); GRN_BULK_REWIND(&y_element); grn_bulk_write(ctx, &x_element, x_value, x_element_size); grn_bulk_write(ctx, &y_element, y_value, y_element_size); DO_EQ((&x_element), (&y_element), is_equal); if (!is_equal) { break; } } GRN_OBJ_FIN(ctx, &x_element); GRN_OBJ_FIN(ctx, &y_element); return is_equal; } else { return GRN_FALSE; } break; default : return GRN_FALSE; } }
void grn_output_obj(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type, grn_obj *obj, grn_obj_format *format) { grn_obj buf; GRN_TEXT_INIT(&buf, 0); switch (obj->header.type) { case GRN_BULK : switch (obj->header.domain) { case GRN_DB_VOID : grn_output_void(ctx, outbuf, output_type, GRN_BULK_HEAD(obj), GRN_BULK_VSIZE(obj)); break; case GRN_DB_SHORT_TEXT : case GRN_DB_TEXT : case GRN_DB_LONG_TEXT : grn_output_str(ctx, outbuf, output_type, GRN_BULK_HEAD(obj), GRN_BULK_VSIZE(obj)); break; case GRN_DB_BOOL : grn_output_bool(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_UINT8_VALUE(obj) : 0); break; case GRN_DB_INT8 : grn_output_int32(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_INT8_VALUE(obj) : 0); break; case GRN_DB_UINT8 : grn_output_int32(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_UINT8_VALUE(obj) : 0); break; case GRN_DB_INT16 : grn_output_int32(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_INT16_VALUE(obj) : 0); break; case GRN_DB_UINT16 : grn_output_int32(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_UINT16_VALUE(obj) : 0); break; case GRN_DB_INT32 : grn_output_int32(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_INT32_VALUE(obj) : 0); break; case GRN_DB_UINT32 : grn_output_int64(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_UINT32_VALUE(obj) : 0); break; case GRN_DB_INT64 : grn_output_int64(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_INT64_VALUE(obj) : 0); break; case GRN_DB_UINT64 : grn_output_uint64(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_UINT64_VALUE(obj) : 0); break; case GRN_DB_FLOAT : grn_output_float(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_FLOAT_VALUE(obj) : 0); break; case GRN_DB_TIME : grn_output_time(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? GRN_INT64_VALUE(obj) : 0); break; case GRN_DB_TOKYO_GEO_POINT : case GRN_DB_WGS84_GEO_POINT : grn_output_geo_point(ctx, outbuf, output_type, GRN_BULK_VSIZE(obj) ? (grn_geo_point *)GRN_BULK_HEAD(obj) : NULL); break; default : if (format) { int j; int ncolumns = GRN_BULK_VSIZE(&format->columns)/sizeof(grn_obj *); grn_obj **columns = (grn_obj **)GRN_BULK_HEAD(&format->columns); if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { grn_output_array_open(ctx, outbuf, output_type, "COLUMNS", ncolumns); for (j = 0; j < ncolumns; j++) { grn_id range_id; grn_output_array_open(ctx, outbuf, output_type, "COLUMN", 2); GRN_BULK_REWIND(&buf); grn_column_name_(ctx, columns[j], &buf); grn_output_obj(ctx, outbuf, output_type, &buf, NULL); /* column range */ range_id = grn_obj_get_range(ctx, columns[j]); if (range_id == GRN_ID_NIL) { GRN_TEXT_PUTS(ctx, outbuf, "null"); } else { int name_len; grn_obj *range_obj; char name_buf[GRN_TABLE_MAX_KEY_SIZE]; range_obj = grn_ctx_at(ctx, range_id); name_len = grn_obj_name(ctx, range_obj, name_buf, GRN_TABLE_MAX_KEY_SIZE); GRN_BULK_REWIND(&buf); GRN_TEXT_PUT(ctx, &buf, name_buf, name_len); grn_output_obj(ctx, outbuf, output_type, &buf, NULL); } grn_output_array_close(ctx, outbuf, output_type); } grn_output_array_close(ctx, outbuf, output_type); } grn_output_array_open(ctx, outbuf, output_type, "HIT", ncolumns); for (j = 0; j < ncolumns; j++) { grn_text_atoj_o(ctx, outbuf, output_type, columns[j], obj); } grn_output_array_close(ctx, outbuf, output_type); } else { grn_obj *table = grn_ctx_at(ctx, obj->header.domain); grn_id id = *((grn_id *)GRN_BULK_HEAD(obj)); if (table && table->header.type != GRN_TABLE_NO_KEY) { grn_obj *accessor = grn_obj_column(ctx, table, "_key", 4); if (accessor) { grn_obj_get_value(ctx, accessor, id, &buf); grn_obj_unlink(ctx, accessor); } grn_output_obj(ctx, outbuf, output_type, &buf, format); } else { grn_output_int64(ctx, outbuf, output_type, id); } } break; } break; case GRN_UVECTOR : if (format) { int i, j; grn_id *v = (grn_id *)GRN_BULK_HEAD(obj), *ve = (grn_id *)GRN_BULK_CURR(obj); int ncolumns = GRN_BULK_VSIZE(&format->columns) / sizeof(grn_obj *); grn_obj **columns = (grn_obj **)GRN_BULK_HEAD(&format->columns); grn_output_array_open(ctx, outbuf, output_type, "RESULTSET", -1); grn_output_array_open(ctx, outbuf, output_type, "NHITS", 1); grn_text_itoa(ctx, outbuf, ve - v); grn_output_array_close(ctx, outbuf, output_type); if (v < ve) { if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { grn_output_array_open(ctx, outbuf, output_type, "COLUMNS", -1); for (j = 0; j < ncolumns; j++) { grn_id range_id; grn_output_array_open(ctx, outbuf, output_type, "COLUMN", -1); GRN_BULK_REWIND(&buf); grn_column_name_(ctx, columns[j], &buf); grn_output_obj(ctx, outbuf, output_type, &buf, NULL); /* column range */ range_id = grn_obj_get_range(ctx, columns[j]); if (range_id == GRN_ID_NIL) { GRN_TEXT_PUTS(ctx, outbuf, "null"); } else { int name_len; grn_obj *range_obj; char name_buf[GRN_TABLE_MAX_KEY_SIZE]; range_obj = grn_ctx_at(ctx, range_id); name_len = grn_obj_name(ctx, range_obj, name_buf, GRN_TABLE_MAX_KEY_SIZE); GRN_BULK_REWIND(&buf); GRN_TEXT_PUT(ctx, &buf, name_buf, name_len); grn_output_obj(ctx, outbuf, output_type, &buf, NULL); } grn_output_array_close(ctx, outbuf, output_type); } grn_output_array_close(ctx, outbuf, output_type); } for (i = 0;; i++) { grn_output_array_open(ctx, outbuf, output_type, "HITS", -1); for (j = 0; j < ncolumns; j++) { GRN_BULK_REWIND(&buf); grn_obj_get_value(ctx, columns[j], *v, &buf); grn_output_obj(ctx, outbuf, output_type, &buf, NULL); } grn_output_array_close(ctx, outbuf, output_type); v++; if (v < ve) { } else { break; } } } grn_output_array_close(ctx, outbuf, output_type); } else { grn_obj *range = grn_ctx_at(ctx, obj->header.domain); if (range && range->header.type == GRN_TYPE) { int value_size = ((struct _grn_type *)range)->obj.range; char *v = (char *)GRN_BULK_HEAD(obj), *ve = (char *)GRN_BULK_CURR(obj); grn_output_array_open(ctx, outbuf, output_type, "VECTOR", -1); if (v < ve) { for (;;) { grn_obj value; GRN_OBJ_INIT(&value, GRN_BULK, 0, obj->header.domain); grn_bulk_write_from(ctx, &value, v, 0, value_size); grn_output_obj(ctx, outbuf, output_type, &value, NULL); v += value_size; if (v < ve) { } else { break; } } } grn_output_array_close(ctx, outbuf, output_type); } else { grn_id *v = (grn_id *)GRN_BULK_HEAD(obj), *ve = (grn_id *)GRN_BULK_CURR(obj); grn_output_array_open(ctx, outbuf, output_type, "VECTOR", ve - v); if (v < ve) { grn_obj key; GRN_OBJ_INIT(&key, GRN_BULK, 0, range->header.domain); for (;;) { if (range->header.type != GRN_TABLE_NO_KEY) { grn_table_get_key2(ctx, range, *v, &key); grn_output_obj(ctx, outbuf, output_type, &key, NULL); GRN_BULK_REWIND(&key); } else { grn_obj id; GRN_UINT32_INIT(&id, 0); GRN_UINT32_SET(ctx, &id, *v); grn_output_obj(ctx, outbuf, output_type, &id, NULL); GRN_OBJ_FIN(ctx, &id); } v++; if (v < ve) { } else { break; } } GRN_OBJ_FIN(ctx, &key); } grn_output_array_close(ctx, outbuf, output_type); } } break; case GRN_VECTOR : if (obj->header.domain == GRN_DB_VOID) { ERR(GRN_INVALID_ARGUMENT, "invalid obj->header.domain"); } if (format) { ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "cannot print GRN_VECTOR using grn_obj_format"); } else { unsigned int i, n; grn_obj value; GRN_VOID_INIT(&value); n = grn_vector_size(ctx, obj); grn_output_array_open(ctx, outbuf, output_type, "VECTOR", -1); for (i = 0; i < n; i++) { const char *_value; unsigned int weight, length; grn_id domain; length = grn_vector_get_element(ctx, obj, i, &_value, &weight, &domain); if (domain != GRN_DB_VOID) { grn_obj_reinit(ctx, &value, domain, 0); } else { grn_obj_reinit(ctx, &value, obj->header.domain, 0); } grn_bulk_write(ctx, &value, _value, length); grn_output_obj(ctx, outbuf, output_type, &value, NULL); } grn_output_array_close(ctx, outbuf, output_type); GRN_OBJ_FIN(ctx, &value); } break; case GRN_PVECTOR : if (format) { ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "cannot print GRN_PVECTOR using grn_obj_format"); } else { unsigned int i, n; grn_output_array_open(ctx, outbuf, output_type, "VECTOR", -1); n = GRN_BULK_VSIZE(obj) / sizeof(grn_obj *); for (i = 0; i < n; i++) { grn_obj *value; value = GRN_PTR_VALUE_AT(obj, i); grn_output_obj(ctx, outbuf, output_type, value, NULL); } grn_output_array_close(ctx, outbuf, output_type); } break; case GRN_TABLE_HASH_KEY : case GRN_TABLE_PAT_KEY : case GRN_TABLE_NO_KEY : case GRN_TABLE_VIEW : if (format) { int i, j; int ncolumns = GRN_BULK_VSIZE(&format->columns)/sizeof(grn_obj *); grn_obj **columns = (grn_obj **)GRN_BULK_HEAD(&format->columns); grn_table_cursor *tc = grn_table_cursor_open(ctx, obj, NULL, 0, NULL, 0, format->offset, format->limit, GRN_CURSOR_ASCENDING); int resultset_size = -1; if (!tc) { ERRCLR(ctx); } #ifdef HAVE_MESSAGE_PACK resultset_size = 1; /* [NHITS, (COLUMNS), (HITS)] */ if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { resultset_size++; } resultset_size += format->limit; #endif grn_output_array_open(ctx, outbuf, output_type, "RESULTSET", resultset_size); grn_output_array_open(ctx, outbuf, output_type, "NHITS", 1); if (output_type == GRN_CONTENT_XML) { grn_text_itoa(ctx, outbuf, format->nhits); } else { grn_output_int32(ctx, outbuf, output_type, format->nhits); } grn_output_array_close(ctx, outbuf, output_type); if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) { grn_output_array_open(ctx, outbuf, output_type, "COLUMNS", ncolumns); for (j = 0; j < ncolumns; j++) { grn_id range_id; grn_output_array_open(ctx, outbuf, output_type, "COLUMN", 2); GRN_BULK_REWIND(&buf); grn_column_name_(ctx, columns[j], &buf); grn_output_obj(ctx, outbuf, output_type, &buf, NULL); /* column range */ range_id = grn_obj_get_range(ctx, columns[j]); if (range_id == GRN_ID_NIL) { GRN_TEXT_PUTS(ctx, outbuf, "null"); } else { int name_len; grn_obj *range_obj; char name_buf[GRN_TABLE_MAX_KEY_SIZE]; range_obj = grn_ctx_at(ctx, range_id); name_len = grn_obj_name(ctx, range_obj, name_buf, GRN_TABLE_MAX_KEY_SIZE); GRN_BULK_REWIND(&buf); GRN_TEXT_PUT(ctx, &buf, name_buf, name_len); grn_output_obj(ctx, outbuf, output_type, &buf, NULL); } grn_output_array_close(ctx, outbuf, output_type); } grn_output_array_close(ctx, outbuf, output_type); } if (tc) { grn_obj id; GRN_TEXT_INIT(&id, 0); for (i = 0; !grn_table_cursor_next_o(ctx, tc, &id); i++) { grn_output_array_open(ctx, outbuf, output_type, "HIT", ncolumns); for (j = 0; j < ncolumns; j++) { grn_text_atoj_o(ctx, outbuf, output_type, columns[j], &id); } grn_output_array_close(ctx, outbuf, output_type); } GRN_OBJ_FIN(ctx, &id); grn_table_cursor_close(ctx, tc); } grn_output_array_close(ctx, outbuf, output_type); } else { int i; grn_obj *column = grn_obj_column(ctx, obj, "_key", 4); grn_table_cursor *tc = grn_table_cursor_open(ctx, obj, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_ASCENDING); grn_output_array_open(ctx, outbuf, output_type, "HIT", -1); if (tc) { grn_obj id; GRN_TEXT_INIT(&id, 0); for (i = 0; !grn_table_cursor_next_o(ctx, tc, &id); i++) { /* todo: grn_text_atoj_o(ctx, outbuf, output_type, column, &id); */ GRN_BULK_REWIND(&buf); grn_obj_get_value_o(ctx, column, &id, &buf); grn_text_esc(ctx, outbuf, GRN_BULK_HEAD(&buf), GRN_BULK_VSIZE(&buf)); } GRN_OBJ_FIN(ctx, &id); grn_table_cursor_close(ctx, tc); } grn_output_array_close(ctx, outbuf, output_type); grn_obj_unlink(ctx, column); } break; } GRN_OBJ_FIN(ctx, &buf); }