void test_text_to_float(void) { grn_obj_reinit(&context, &dest, GRN_DB_FLOAT, 0); cast_text("29.029"); cut_assert_equal_double(29.029, 0.001, GRN_FLOAT_VALUE(&dest)); }
void test_uint64_to_float(void) { grn_obj_reinit(&context, &dest, GRN_DB_FLOAT, 0); cast_uint64(29); cut_assert_equal_double(29, 0.1, GRN_FLOAT_VALUE(&dest)); }
void grn_rset_recinfo_update_calc_values(grn_ctx *ctx, grn_rset_recinfo *ri, grn_obj *table, grn_obj *value) { grn_table_group_flags flags; byte *values; grn_obj value_int64; grn_obj value_float; flags = DB_OBJ(table)->flags.group; values = (((byte *)ri->subrecs) + GRN_RSET_SUBRECS_SIZE(DB_OBJ(table)->subrec_size, DB_OBJ(table)->max_n_subrecs)); GRN_INT64_INIT(&value_int64, 0); GRN_FLOAT_INIT(&value_float, 0); if (flags & (GRN_TABLE_GROUP_CALC_MAX | GRN_TABLE_GROUP_CALC_MIN | GRN_TABLE_GROUP_CALC_SUM)) { grn_obj_cast(ctx, value, &value_int64, GRN_FALSE); } if (flags & GRN_TABLE_GROUP_CALC_AVG) { grn_obj_cast(ctx, value, &value_float, GRN_FALSE); } if (flags & GRN_TABLE_GROUP_CALC_MAX) { int64_t current_max = *((int64_t *)values); int64_t value_raw = GRN_INT64_VALUE(&value_int64); if (ri->n_subrecs == 1 || value_raw > current_max) { *((int64_t *)values) = value_raw; } values += GRN_RSET_MAX_SIZE; } if (flags & GRN_TABLE_GROUP_CALC_MIN) { int64_t current_min = *((int64_t *)values); int64_t value_raw = GRN_INT64_VALUE(&value_int64); if (ri->n_subrecs == 1 || value_raw < current_min) { *((int64_t *)values) = value_raw; } values += GRN_RSET_MIN_SIZE; } if (flags & GRN_TABLE_GROUP_CALC_SUM) { int64_t value_raw = GRN_INT64_VALUE(&value_int64); *((int64_t *)values) += value_raw; values += GRN_RSET_SUM_SIZE; } if (flags & GRN_TABLE_GROUP_CALC_AVG) { double current_average = *((double *)values); double value_raw = GRN_FLOAT_VALUE(&value_float); *((double *)values) += (value_raw - current_average) / ri->n_subrecs; values += GRN_RSET_AVG_SIZE; } GRN_OBJ_FIN(ctx, &value_float); GRN_OBJ_FIN(ctx, &value_int64); }
static VALUE rb_grn_bulk_to_ruby_object_by_range_id (grn_ctx *context, grn_obj *bulk, grn_id range_id, VALUE related_object, VALUE *rb_value) { grn_bool success = GRN_TRUE; switch (range_id) { case GRN_DB_VOID: *rb_value = rb_str_new(GRN_TEXT_VALUE(bulk), GRN_TEXT_LEN(bulk)); break; case GRN_DB_BOOL: *rb_value = GRN_BOOL_VALUE(bulk) ? Qtrue : Qfalse; break; case GRN_DB_INT32: *rb_value = INT2NUM(GRN_INT32_VALUE(bulk)); break; case GRN_DB_UINT32: *rb_value = UINT2NUM(GRN_UINT32_VALUE(bulk)); break; case GRN_DB_INT64: *rb_value = LL2NUM(GRN_INT64_VALUE(bulk)); break; case GRN_DB_UINT64: *rb_value = ULL2NUM(GRN_UINT64_VALUE(bulk)); break; case GRN_DB_FLOAT: *rb_value = rb_float_new(GRN_FLOAT_VALUE(bulk)); break; case GRN_DB_TIME: { int64_t time_value, sec, usec; time_value = GRN_TIME_VALUE(bulk); GRN_TIME_UNPACK(time_value, sec, usec); *rb_value = rb_funcall(rb_cTime, rb_intern("at"), 2, LL2NUM(sec), LL2NUM(usec)); } break; case GRN_DB_SHORT_TEXT: case GRN_DB_TEXT: case GRN_DB_LONG_TEXT: *rb_value = rb_grn_context_rb_string_new(context, GRN_TEXT_VALUE(bulk), GRN_TEXT_LEN(bulk)); break; default: success = GRN_FALSE; break; } return success; }
grn_bool grn_obj_is_true(grn_ctx *ctx, grn_obj *obj) { if (!obj) { return GRN_FALSE; } switch (obj->header.type) { case GRN_BULK : switch (obj->header.domain) { case GRN_DB_BOOL : return GRN_BOOL_VALUE(obj); break; case GRN_DB_INT32 : return GRN_INT32_VALUE(obj) != 0; break; case GRN_DB_UINT32 : return GRN_UINT32_VALUE(obj) != 0; break; case GRN_DB_FLOAT : { double float_value; float_value = GRN_FLOAT_VALUE(obj); return (float_value < -DBL_EPSILON || DBL_EPSILON < float_value); break; } case GRN_DB_SHORT_TEXT : case GRN_DB_TEXT : case GRN_DB_LONG_TEXT : return GRN_TEXT_LEN(obj) != 0; break; default : return GRN_FALSE; break; } break; case GRN_VECTOR : return GRN_TRUE; break; default : return GRN_FALSE; break; } }
static void bench_teardown(gpointer user_data) { BenchmarkData *data = user_data; if (data->report_result) { grn_obj *result; result = grn_expr_get_value(data->context, data->expression, 0); g_print("result: %g\n", GRN_FLOAT_VALUE(result)); /* http://vldb.gsi.go.jp/ says '38820.79' in WGS84 and '38816.42' in Tokyo geodetic system for a distance between '127980000x502560000' and '128880000x503640000'. */ } grn_obj_unlink(data->context, data->end_point); grn_obj_unlink(data->context, data->start_point); grn_obj_unlink(data->context, data->expression); grn_obj_unlink(data->context, data->database); grn_ctx_fin(data->context); }
void test_aton(gconstpointer data) { const gchar *input, *input_end, *rest; grn_builtin_type type; grn_rc rc; type = gcut_data_get_int(data, "type"); input = gcut_data_get_string(data, "input"); input_end = strchr(input, '\0'); rc = grn_aton(&context, input, input_end, &rest, &buffer); grn_test_assert(rc); cut_assert_equal_string(input_end, rest); cut_assert_equal_int(type, buffer.header.domain); switch (type) { case GRN_DB_INT32 : cut_assert_equal_int(gcut_data_get_int(data, "expected"), GRN_INT32_VALUE(&buffer)); break; case GRN_DB_UINT32 : cut_assert_equal_uint(gcut_data_get_uint(data, "expected"), GRN_UINT32_VALUE(&buffer)); break; case GRN_DB_INT64 : gcut_assert_equal_int64(gcut_data_get_int64(data, "expected"), GRN_INT64_VALUE(&buffer)); break; case GRN_DB_UINT64 : gcut_assert_equal_uint64(gcut_data_get_uint64(data, "expected"), GRN_UINT64_VALUE(&buffer)); break; case GRN_DB_FLOAT : cut_assert_equal_double(gcut_data_get_double(data, "expected"), 0.000001, GRN_FLOAT_VALUE(&buffer)); break; default : cut_error("unknown type: %d", type); break; } }
static VALUE rb_grn_bulk_to_ruby_object_by_range_type (grn_ctx *context, grn_obj *bulk, grn_obj *range, grn_id range_id, VALUE related_object, VALUE *rb_value) { grn_bool success = GRN_TRUE; if (!range && range_id != GRN_ID_NIL) { range = grn_ctx_at(context, range_id); } if (!range) return GRN_FALSE; switch (range->header.type) { case GRN_TABLE_HASH_KEY: case GRN_TABLE_PAT_KEY: case GRN_TABLE_DAT_KEY: case GRN_TABLE_NO_KEY: { grn_id id; id = *((grn_id *)GRN_BULK_HEAD(bulk)); if (id == GRN_ID_NIL) { *rb_value = Qnil; } else { VALUE rb_range; rb_range = GRNOBJECT2RVAL(Qnil, context, range, GRN_FALSE); *rb_value = rb_grn_record_new(rb_range, id, Qnil); } break; } case GRN_TYPE: if (range->header.flags & GRN_OBJ_KEY_VAR_SIZE) { *rb_value = rb_grn_context_rb_string_new(context, GRN_BULK_HEAD(bulk), GRN_BULK_VSIZE(bulk)); } else { switch (range->header.flags & GRN_OBJ_KEY_MASK) { case GRN_OBJ_KEY_UINT: *rb_value = INT2NUM(GRN_UINT32_VALUE(bulk)); break; case GRN_OBJ_KEY_INT: *rb_value = INT2NUM(GRN_INT32_VALUE(bulk)); break; case GRN_OBJ_KEY_FLOAT: *rb_value = rb_float_new(GRN_FLOAT_VALUE(bulk)); break; default: success = GRN_FALSE; } break; } break; default: success = GRN_FALSE; break; } return success; }
void test_setoperation(gconstpointer data) { grn_operator operator; grn_obj *entries; grn_obj *result1; grn_obj *result2; const char *dump; operator = gcut_data_get_int(data, "operator"); assert_send_command("table_create Entries TABLE_HASH_KEY ShortText"); send_command( "load " "--table Entries " "--values '[{\"_key\": \"a\"}, {\"_key\": \"b\"}, {\"_key\": \"c\"}]'"); entries = grn_ctx_get(context, "Entries", -1); { const char *condition = "_id < 3"; grn_obj *expr; grn_obj *variable; GRN_EXPR_CREATE_FOR_QUERY(context, entries, expr, variable); grn_expr_parse(context, expr, condition, strlen(condition), NULL, GRN_OP_AND, GRN_OP_MATCH, GRN_EXPR_SYNTAX_SCRIPT); result1 = grn_table_select(context, entries, expr, NULL, GRN_OP_OR); grn_obj_unlink(context, expr); } { const char *condition = "_id > 1"; grn_obj *expr; grn_obj *variable; GRN_EXPR_CREATE_FOR_QUERY(context, entries, expr, variable); grn_expr_parse(context, expr, condition, strlen(condition), NULL, GRN_OP_AND, GRN_OP_MATCH, GRN_EXPR_SYNTAX_SCRIPT); result2 = grn_table_select(context, entries, expr, NULL, GRN_OP_OR); grn_obj_unlink(context, expr); } grn_table_setoperation(context, result1, result2, result1, operator); { grn_bool first_record = GRN_TRUE; grn_obj buffer; grn_obj *score_accessor; grn_obj score; GRN_TEXT_INIT(&buffer, 0); GRN_TEXT_PUTS(context, &buffer, "["); score_accessor = grn_obj_column(context, result1, GRN_COLUMN_NAME_SCORE, GRN_COLUMN_NAME_SCORE_LEN); GRN_FLOAT_INIT(&score, 0); GRN_TABLE_EACH_BEGIN(context, result1, cursor, id) { void *result_key; grn_id entry_id; char entry_key[GRN_TABLE_MAX_KEY_SIZE]; int entry_key_size; if (first_record) { first_record = GRN_FALSE; } else { GRN_TEXT_PUTS(context, &buffer, ", "); } GRN_TEXT_PUTS(context, &buffer, "["); grn_table_cursor_get_key(context, cursor, &result_key); entry_id = *((grn_id *)result_key); entry_key_size = grn_table_get_key(context, entries, entry_id, entry_key, GRN_TABLE_MAX_KEY_SIZE); GRN_TEXT_PUT(context, &buffer, entry_key, entry_key_size); GRN_TEXT_PUTS(context, &buffer, ", "); GRN_BULK_REWIND(&score); grn_obj_get_value(context, score_accessor, id, &score); grn_text_printf(context, &buffer, "%.1f", GRN_FLOAT_VALUE(&score)); GRN_TEXT_PUTS(context, &buffer, "]"); } GRN_TABLE_EACH_END(context, cursor); GRN_OBJ_FIN(context, &score); grn_obj_unlink(context, score_accessor); GRN_TEXT_PUTS(context, &buffer, "]"); dump = cut_take_strndup(GRN_TEXT_VALUE(&buffer), GRN_TEXT_LEN(&buffer)); GRN_OBJ_FIN(context, &buffer); }
void test_mroonga_index_score(void) { grn_obj *t1,*c1,*lc,*ft; grn_obj buff; grn_id r1,r2,r3,r4; const gchar *mrn_dir; mrn_dir = cut_build_path(tmp_directory, "mrn", NULL); g_mkdir_with_parents(mrn_dir, 0700); grn_obj_close(context, db); db = grn_db_create(context, cut_build_path(mrn_dir, "mroonga.grn", NULL), NULL); cut_assert_not_null(db); /* actual table */ t1 = grn_table_create(context, "t1", 2, cut_build_path(mrn_dir, "t1.grn", NULL), GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, 0); cut_assert_not_null(t1); /* lexicon table */ lc = grn_table_create(context, "lc", 2, cut_build_path(mrn_dir, "lc.grn", NULL), GRN_OBJ_TABLE_PAT_KEY|GRN_OBJ_PERSISTENT, grn_ctx_at(context, GRN_DB_SHORT_TEXT), 0); cut_assert_not_null(lc); grn_test_assert(grn_obj_set_info(context, lc, GRN_INFO_DEFAULT_TOKENIZER, grn_ctx_at(context, GRN_DB_BIGRAM))); /* actual column */ c1 = grn_column_create(context, t1, "c1", 2, cut_build_path(mrn_dir, "t1.c1.grn", NULL), GRN_OBJ_COLUMN_SCALAR|GRN_OBJ_PERSISTENT, grn_ctx_at(context, GRN_DB_TEXT)); cut_assert_not_null(c1); /* fulltext index */ ft = grn_column_create(context, lc, "ft", 2, cut_build_path(mrn_dir, "lc.ft.grn", NULL), GRN_OBJ_COLUMN_INDEX|GRN_OBJ_PERSISTENT, t1); cut_assert_not_null(ft); GRN_TEXT_INIT(&buff,0); /* link between actual column and fulltext index */ GRN_UINT32_SET(context, &buff, grn_obj_id(context, c1)); grn_obj_set_info(context, ft, GRN_INFO_SOURCE, &buff); /* need to use grn_id */ /* insert row */ r1 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(1,r1); GRN_TEXT_SETS(context, &buff, "abcde"); grn_test_assert(grn_obj_set_value(context, c1, r1, &buff, GRN_OBJ_SET)); r2 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(2,r2); GRN_TEXT_SETS(context, &buff, "fghij"); grn_test_assert(grn_obj_set_value(context, c1, r2, &buff, GRN_OBJ_SET)); r3 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(3,r3); GRN_TEXT_SETS(context, &buff, "11 22 33"); grn_test_assert(grn_obj_set_value(context, c1, r3, &buff, GRN_OBJ_SET)); r4 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(4,r4); GRN_TEXT_SETS(context, &buff, "44 22 55"); grn_test_assert(grn_obj_set_value(context, c1, r4, &buff, GRN_OBJ_SET)); /* confirm record are inserted in both column and index */ cut_assert_equal_int(4,grn_table_size(context,t1)); cut_assert_equal_int(23,grn_table_size(context,lc)); /* nlq search */ { grn_id id, docid; grn_obj *res; grn_table_cursor *tc; grn_obj score, *score_column; res = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, t1, 0); GRN_FLOAT_INIT(&score, 0); GRN_BULK_REWIND(&buff); GRN_TEXT_SETS(context, &buff, "hij"); grn_obj_search(context, ft, &buff, res, GRN_OP_OR, NULL); cut_assert_equal_int(1, grn_table_size(context, res)); score_column = grn_obj_column(context, res, "_score", 6); tc = grn_table_cursor_open(context, res, NULL, 0, NULL, 0, 0, -1, 0); while ((id = grn_table_cursor_next(context, tc))) { GRN_BULK_REWIND(&buff); grn_table_get_key(context, res, id, &docid, sizeof(grn_id)); cut_assert_equal_int(2, docid); cut_assert_not_null(grn_obj_get_value(context, c1, docid, &buff)); cut_assert_equal_int(5 ,GRN_TEXT_LEN(&buff)); cut_assert_equal_substring("fghij", (char*) GRN_BULK_HEAD(&buff),GRN_TEXT_LEN(&buff)); grn_obj_get_value(context, score_column, id, &score); cut_assert_equal_double(1.0, DBL_EPSILON, GRN_FLOAT_VALUE(&score)); } grn_table_cursor_close(context, tc); grn_obj_close(context, score_column); grn_obj_close(context, res); } /* boolean search */ { grn_id id, docid; grn_obj *res; grn_obj *match_columns, *match_columns_variable; grn_obj *expression, *expression_variable; grn_table_cursor *tc; grn_obj score, *score_column; const char *match_columns_expression = "c1 * 5"; const char *qstr = "+22 -55"; GRN_EXPR_CREATE_FOR_QUERY(context, t1, match_columns, match_columns_variable); grn_expr_parse(context, match_columns, match_columns_expression, strlen(match_columns_expression), NULL, GRN_OP_MATCH, GRN_OP_AND, GRN_EXPR_SYNTAX_SCRIPT); GRN_EXPR_CREATE_FOR_QUERY(context, t1, expression, expression_variable); res = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, t1, 0); grn_test_assert(grn_expr_parse(context, expression, qstr, strlen(qstr), match_columns, GRN_OP_MATCH, GRN_OP_OR, GRN_EXPR_SYNTAX_QUERY)); grn_table_select(context, t1, expression, res, GRN_OP_OR); cut_assert_equal_int(1, grn_table_size(context, res)); GRN_FLOAT_INIT(&score, 0); score_column = grn_obj_column(context, res, "_score", 6); tc = grn_table_cursor_open(context, res, NULL, 0, NULL, 0, 0, -1, 0); while ((id = grn_table_cursor_next(context, tc))) { GRN_BULK_REWIND(&buff); grn_table_get_key(context, res, id, &docid, sizeof(grn_id)); cut_assert_equal_int(3, docid); cut_assert_not_null(grn_obj_get_value(context, c1, docid, &buff)); cut_assert_equal_int(8, GRN_TEXT_LEN(&buff)); cut_assert_equal_substring("11 22 33", (char*) GRN_BULK_HEAD(&buff),GRN_TEXT_LEN(&buff)); grn_obj_get_value(context, score_column, id, &score); cut_assert_equal_double(5, DBL_EPSILON, GRN_FLOAT_VALUE(&score)); } grn_obj_close(context, expression); grn_obj_close(context, match_columns); grn_table_cursor_close(context ,tc); grn_obj_close(context, score_column); grn_obj_close(context, res); } grn_obj_close(context, &buff); grn_obj_close(context, ft); grn_obj_close(context, c1); grn_obj_close(context, lc); grn_obj_close(context, t1); }
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); }