Kouhei Sutou
null+****@clear*****
Fri May 8 12:22:04 JST 2015
Kouhei Sutou 2015-05-08 12:22:04 +0900 (Fri, 08 May 2015) New Revision: 10bca58c1bad392694417b11408c1660a6f9cc39 https://github.com/groonga/groonga/commit/10bca58c1bad392694417b11408c1660a6f9cc39 Message: Split a long function Modified files: lib/db.c Modified: lib/db.c (+189 -130) =================================================================== --- lib/db.c 2015-05-08 11:50:13 +0900 (78e513f) +++ lib/db.c 2015-05-08 12:22:04 +0900 (fc0492a) @@ -2758,6 +2758,181 @@ grn_table_next(grn_ctx *ctx, grn_obj *table, grn_id id) GRN_API_RETURN(r); } +static grn_rc +grn_accessor_resolve_one_index_column(grn_ctx *ctx, grn_accessor *accessor, + grn_obj *current_res, grn_obj **next_res, + grn_search_optarg *optarg) +{ + grn_rc rc = GRN_SUCCESS; + grn_obj *column = NULL; + grn_id next_res_domain_id = GRN_ID_NIL; + + { + grn_obj *index; + grn_obj source_ids; + unsigned int i, n_ids; + + index = accessor->obj; + next_res_domain_id = index->header.domain; + + GRN_UINT32_INIT(&source_ids, GRN_OBJ_VECTOR); + grn_obj_get_info(ctx, index, GRN_INFO_SOURCE, &source_ids); + n_ids = GRN_BULK_VSIZE(&source_ids) / sizeof(grn_id); + for (i = 0; i < n_ids; i++) { + grn_id source_id; + grn_obj *source; + + source_id = GRN_UINT32_VALUE_AT(&source_ids, i); + source = grn_ctx_at(ctx, source_id); + if (DB_OBJ(source)->range == next_res_domain_id) { + column = source; + break; + } + grn_obj_unlink(ctx, source); + } + + if (!column) { + return GRN_INVALID_ARGUMENT; + } + } + + { + grn_rc rc; + grn_obj *next_res_domain = grn_ctx_at(ctx, next_res_domain_id); + *next_res = grn_table_create(ctx, NULL, 0, NULL, + GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, + next_res_domain, NULL); + rc = ctx->rc; + grn_obj_unlink(ctx, next_res_domain); + if (!*next_res) { + return rc; + } + } + + { + grn_obj_flags column_value_flags = 0; + grn_obj column_value; + grn_ii_posting add_posting; + grn_id *tid; + grn_rset_recinfo *recinfo; + + if (column->header.type == GRN_COLUMN_VAR_SIZE) { + column_value_flags |= GRN_OBJ_VECTOR; + } + GRN_VALUE_FIX_SIZE_INIT(&column_value, + column_value_flags, + next_res_domain_id); + + add_posting.sid = 0; + add_posting.pos = 0; + add_posting.weight = 0; + + GRN_HASH_EACH(ctx, (grn_hash *)current_res, id, &tid, NULL, &recinfo, { + int i; + int n_elements; + + add_posting.weight = recinfo->score - 1; + + GRN_BULK_REWIND(&column_value); + grn_obj_get_value(ctx, column, *tid, &column_value); + + n_elements = GRN_BULK_VSIZE(&column_value) / sizeof(grn_id); + for (i = 0; i < n_elements; i++) { + add_posting.rid = GRN_RECORD_VALUE_AT(&column_value, i); + rc = grn_ii_posting_add(ctx, + &add_posting, + (grn_hash *)*next_res, + GRN_OP_OR); + if (rc != GRN_SUCCESS) { + break; + } + } + if (rc != GRN_SUCCESS) { + break; + } + }); + + GRN_OBJ_FIN(ctx, &column_value); + } + + if (rc != GRN_SUCCESS) { + grn_obj_unlink(ctx, *next_res); + } + + return rc; +} + +static grn_rc +grn_accessor_resolve_one_data_column(grn_ctx *ctx, grn_accessor *accessor, + grn_obj *current_res, grn_obj **next_res, + grn_search_optarg *optarg) +{ + grn_rc rc = GRN_SUCCESS; + grn_obj *index = NULL; + grn_operator index_op = GRN_OP_MATCH; + grn_id next_res_domain_id = GRN_ID_NIL; + + if (grn_column_index(ctx, accessor->obj, index_op, &index, 1, NULL) == 0) { + return GRN_INVALID_ARGUMENT; + } + next_res_domain_id = DB_OBJ(index)->range; + + { + grn_rc rc; + grn_obj *next_res_domain = grn_ctx_at(ctx, next_res_domain_id); + *next_res = grn_table_create(ctx, NULL, 0, NULL, + GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, + next_res_domain, NULL); + rc = ctx->rc; + grn_obj_unlink(ctx, next_res_domain); + if (!*next_res) { + return rc; + } + } + + { + grn_id *tid; + grn_rset_recinfo *recinfo; + + GRN_HASH_EACH(ctx, (grn_hash *)current_res, id, &tid, NULL, &recinfo, { + grn_ii *ii = (grn_ii *)index; + grn_ii_cursor *ii_cursor; + grn_ii_posting *posting; + + ii_cursor = grn_ii_cursor_open(ctx, ii, *tid, + GRN_ID_NIL, GRN_ID_MAX, + ii->n_elements, + 0); + if (!ii_cursor) { + continue; + } + + while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) { + grn_ii_posting add_posting = *posting; + add_posting.weight += recinfo->score - 1; + rc = grn_ii_posting_add(ctx, + &add_posting, + (grn_hash *)*next_res, + GRN_OP_OR); + if (rc != GRN_SUCCESS) { + break; + } + } + grn_ii_cursor_close(ctx, ii_cursor); + + if (rc != GRN_SUCCESS) { + break; + } + }); + } + + if (rc != GRN_SUCCESS) { + grn_obj_unlink(ctx, *next_res); + } + + return rc; +} + grn_rc grn_accessor_resolve(grn_ctx *ctx, grn_obj *accessor, int deep, grn_obj *base_res, grn_obj **res, @@ -2780,144 +2955,28 @@ grn_accessor_resolve(grn_ctx *ctx, grn_obj *accessor, int deep, } for (i = n_accessors; i > 0; i--) { - grn_obj *index = NULL; - grn_obj *column = NULL; - grn_operator index_op = GRN_OP_MATCH; - grn_id next_res_domain_id = GRN_ID_NIL; + grn_obj *next_res = NULL; a = (grn_accessor *)GRN_PTR_VALUE_AT(&accessor_stack, i - 1); if (a->obj->header.type == GRN_COLUMN_INDEX) { - grn_obj source_ids; - unsigned int i, n_ids; - - next_res_domain_id = a->obj->header.domain; - - GRN_UINT32_INIT(&source_ids, GRN_OBJ_VECTOR); - grn_obj_get_info(ctx, a->obj, GRN_INFO_SOURCE, &source_ids); - n_ids = GRN_BULK_VSIZE(&source_ids) / sizeof(grn_id); - for (i = 0; i < n_ids; i++) { - grn_id source_id; - grn_obj *source; - - source_id = GRN_UINT32_VALUE_AT(&source_ids, i); - source = grn_ctx_at(ctx, source_id); - if (DB_OBJ(source)->range == next_res_domain_id) { - column = source; - break; - } - grn_obj_unlink(ctx, source); - } - - if (!column) { - rc = GRN_INVALID_ARGUMENT; - break; - } + rc = grn_accessor_resolve_one_index_column(ctx, a, + current_res, &next_res, + optarg); } else { - if (grn_column_index(ctx, a->obj, index_op, &index, 1, NULL) == 0) { - rc = GRN_INVALID_ARGUMENT; - break; - } - next_res_domain_id = DB_OBJ(index)->range; + rc = grn_accessor_resolve_one_data_column(ctx, a, + current_res, &next_res, + optarg); } - { - grn_id *tid; - grn_obj *next_res; - grn_rset_recinfo *recinfo; - { - grn_obj *next_res_domain = grn_ctx_at(ctx, next_res_domain_id); - next_res = grn_table_create(ctx, NULL, 0, NULL, - GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, - next_res_domain, NULL); - rc = ctx->rc; - grn_obj_unlink(ctx, next_res_domain); - if (!next_res) { - if (current_res != base_res) { - grn_obj_unlink(ctx, current_res); - } - break; - } - } - if (index) { - GRN_HASH_EACH(ctx, (grn_hash *)current_res, id, &tid, NULL, &recinfo, { - grn_ii *ii = (grn_ii *)index; - grn_ii_cursor *ii_cursor; - grn_ii_posting *posting; - - ii_cursor = grn_ii_cursor_open(ctx, ii, *tid, - GRN_ID_NIL, GRN_ID_MAX, - ii->n_elements, - 0); - if (!ii_cursor) { - continue; - } - - while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) { - grn_ii_posting add_posting = *posting; - add_posting.weight += recinfo->score - 1; - grn_ii_posting_add(ctx, - &add_posting, - (grn_hash *)next_res, - GRN_OP_OR); - } - grn_ii_cursor_close(ctx, ii_cursor); - - if (rc != GRN_SUCCESS) { - break; - } - }); - } else { - grn_obj_flags column_value_flags = 0; - grn_obj column_value; - grn_ii_posting add_posting; - - if (column->header.type == GRN_COLUMN_VAR_SIZE) { - column_value_flags |= GRN_OBJ_VECTOR; - } - GRN_VALUE_FIX_SIZE_INIT(&column_value, - column_value_flags, - next_res_domain_id); - - add_posting.sid = 0; - add_posting.pos = 0; - add_posting.weight = 0; - - GRN_HASH_EACH(ctx, (grn_hash *)current_res, id, &tid, NULL, &recinfo, { - int i; - int n_elements; - grn_rc rc = GRN_SUCCESS; - - add_posting.weight = recinfo->score - 1; - - GRN_BULK_REWIND(&column_value); - grn_obj_get_value(ctx, column, *tid, &column_value); + if (current_res != base_res) { + grn_obj_unlink(ctx, current_res); + } - n_elements = GRN_BULK_VSIZE(&column_value) / sizeof(grn_id); - for (i = 0; i < n_elements; i++) { - add_posting.rid = GRN_RECORD_VALUE_AT(&column_value, i); - rc = grn_ii_posting_add(ctx, - &add_posting, - (grn_hash *)next_res, - GRN_OP_OR); - if (rc != GRN_SUCCESS) { - break; - } - } - if (rc != GRN_SUCCESS) { - break; - } - }); - GRN_OBJ_FIN(ctx, &column_value); - } - if (current_res != base_res) { - grn_obj_unlink(ctx, current_res); - } - if (rc != GRN_SUCCESS) { - grn_obj_unlink(ctx, next_res); - break; - } - current_res = next_res; + if (rc != GRN_SUCCESS) { + break; } + + current_res = next_res; } if (rc == GRN_SUCCESS && current_res != base_res) { -------------- next part -------------- HTML����������������������������...Download