Kouhei Sutou
null+****@clear*****
Sat Feb 6 19:41:09 JST 2016
Kouhei Sutou 2016-02-06 19:41:09 +0900 (Sat, 06 Feb 2016) New Revision: 83a8d13b557575fc02546de9e7d2ac7a4a843c95 https://github.com/groonga/groonga/commit/83a8d13b557575fc02546de9e7d2ac7a4a843c95 Message: Extract GRN_OP_EQUAL code for index search as a function Modified files: lib/expr.c Modified: lib/expr.c (+121 -108) =================================================================== --- lib/expr.c 2016-02-06 17:34:49 +0900 (47c6305) +++ lib/expr.c 2016-02-06 19:41:09 +0900 (be49894) @@ -5445,6 +5445,126 @@ grn_table_select_index_report(grn_ctx *ctx, const char *tag, grn_obj *index) } static inline grn_bool +grn_table_select_index_equal(grn_ctx *ctx, + grn_obj *table, + grn_obj *index, + scan_info *si, + grn_obj *res) +{ + grn_bool processed = GRN_FALSE; + + if (GRN_BULK_VSIZE(si->query) == 0) { + /* We can't use index for empty value. */ + return GRN_FALSE; + } + + if (si->flags & SCAN_ACCESSOR) { + if (index->header.type == GRN_ACCESSOR && !((grn_accessor *)index)->next) { + grn_obj dest; + grn_accessor *a = (grn_accessor *)index; + grn_posting posting; + posting.sid = 1; + posting.pos = 0; + posting.weight = 0; + switch (a->action) { + case GRN_ACCESSOR_GET_ID : + grn_table_select_index_report(ctx, "[equal][accessor][id]", table); + GRN_UINT32_INIT(&dest, 0); + if (!grn_obj_cast(ctx, si->query, &dest, GRN_FALSE)) { + posting.rid = GRN_UINT32_VALUE(&dest); + if (posting.rid) { + if (posting.rid == grn_table_at(ctx, table, posting.rid)) { + grn_ii_posting_add(ctx, &posting, (grn_hash *)res, + si->logical_op); + } + } + processed = GRN_TRUE; + } + grn_ii_resolve_sel_and(ctx, (grn_hash *)res, si->logical_op); + GRN_OBJ_FIN(ctx, &dest); + break; + case GRN_ACCESSOR_GET_KEY : + grn_table_select_index_report(ctx, "[equal][accessor][key]", table); + GRN_OBJ_INIT(&dest, GRN_BULK, 0, table->header.domain); + if (!grn_obj_cast(ctx, si->query, &dest, GRN_FALSE)) { + if ((posting.rid = grn_table_get(ctx, table, + GRN_BULK_HEAD(&dest), + GRN_BULK_VSIZE(&dest)))) { + grn_ii_posting_add(ctx, &posting, (grn_hash *)res, + si->logical_op); + } + processed = GRN_TRUE; + } + grn_ii_resolve_sel_and(ctx, (grn_hash *)res, si->logical_op); + GRN_OBJ_FIN(ctx, &dest); + break; + } + } + } else { + grn_obj *domain = grn_ctx_at(ctx, index->header.domain); + if (domain) { + grn_id tid; + if (GRN_OBJ_GET_DOMAIN(si->query) == DB_OBJ(domain)->id) { + tid = GRN_RECORD_VALUE(si->query); + } else { + tid = grn_table_get(ctx, domain, + GRN_BULK_HEAD(si->query), + GRN_BULK_VSIZE(si->query)); + } + if (tid == GRN_ID_NIL) { + processed = GRN_TRUE; + } else { + uint32_t sid; + int32_t weight; + grn_ii *ii = (grn_ii *)index; + grn_ii_cursor *ii_cursor; + + grn_table_select_index_report(ctx, "[equal]", index); + + sid = GRN_UINT32_VALUE_AT(&(si->wv), 0); + weight = GRN_INT32_VALUE_AT(&(si->wv), 1); + ii_cursor = grn_ii_cursor_open(ctx, ii, tid, + GRN_ID_NIL, GRN_ID_MAX, + ii->n_elements, 0); + if (ii_cursor) { + grn_posting *posting; + while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) { + grn_posting new_posting; + + if (!(sid == 0 || posting->sid == sid)) { + continue; + } + + if (si->position.specified) { + while ((posting = grn_ii_cursor_next_pos(ctx, ii_cursor))) { + if (posting->pos == si->position.start) { + break; + } + } + if (!posting) { + continue; + } + } + + new_posting = *posting; + new_posting.weight *= weight; + grn_ii_posting_add(ctx, &new_posting, (grn_hash *)res, + si->logical_op); + } + grn_ii_cursor_close(ctx, ii_cursor); + processed = GRN_TRUE; + } + } + } + if (processed) { + grn_ii_resolve_sel_and(ctx, (grn_hash *)res, si->logical_op); + } + } + + return processed; +} + +static inline grn_bool grn_table_select_index_range_column(grn_ctx *ctx, grn_obj *table, grn_obj *index, scan_info *si, grn_operator logical_op, @@ -5734,114 +5854,7 @@ grn_table_select_index(grn_ctx *ctx, grn_obj *table, scan_info *si, grn_obj *index = GRN_PTR_VALUE(&si->index); switch (si->op) { case GRN_OP_EQUAL : - if (GRN_BULK_VSIZE(si->query) == 0) { - /* We can't use index for empty value. */ - return GRN_FALSE; - } - if (si->flags & SCAN_ACCESSOR) { - if (index->header.type == GRN_ACCESSOR && - !((grn_accessor *)index)->next) { - grn_obj dest; - grn_accessor *a = (grn_accessor *)index; - grn_posting posting; - posting.sid = 1; - posting.pos = 0; - posting.weight = 0; - switch (a->action) { - case GRN_ACCESSOR_GET_ID : - grn_table_select_index_report(ctx, "[equal][accessor][id]", table); - GRN_UINT32_INIT(&dest, 0); - if (!grn_obj_cast(ctx, si->query, &dest, GRN_FALSE)) { - posting.rid = GRN_UINT32_VALUE(&dest); - if (posting.rid) { - if (posting.rid == grn_table_at(ctx, table, posting.rid)) { - grn_ii_posting_add(ctx, &posting, (grn_hash *)res, - si->logical_op); - } - } - processed = GRN_TRUE; - } - grn_ii_resolve_sel_and(ctx, (grn_hash *)res, si->logical_op); - GRN_OBJ_FIN(ctx, &dest); - break; - case GRN_ACCESSOR_GET_KEY : - grn_table_select_index_report(ctx, "[equal][accessor][key]", table); - GRN_OBJ_INIT(&dest, GRN_BULK, 0, table->header.domain); - if (!grn_obj_cast(ctx, si->query, &dest, GRN_FALSE)) { - if ((posting.rid = grn_table_get(ctx, table, - GRN_BULK_HEAD(&dest), - GRN_BULK_VSIZE(&dest)))) { - grn_ii_posting_add(ctx, &posting, (grn_hash *)res, - si->logical_op); - } - processed = GRN_TRUE; - } - grn_ii_resolve_sel_and(ctx, (grn_hash *)res, si->logical_op); - GRN_OBJ_FIN(ctx, &dest); - break; - } - } - } else { - grn_obj *domain = grn_ctx_at(ctx, index->header.domain); - if (domain) { - grn_id tid; - if (GRN_OBJ_GET_DOMAIN(si->query) == DB_OBJ(domain)->id) { - tid = GRN_RECORD_VALUE(si->query); - } else { - tid = grn_table_get(ctx, domain, - GRN_BULK_HEAD(si->query), - GRN_BULK_VSIZE(si->query)); - } - if (tid == GRN_ID_NIL) { - processed = GRN_TRUE; - } else { - uint32_t sid; - int32_t weight; - grn_ii *ii = (grn_ii *)index; - grn_ii_cursor *ii_cursor; - - grn_table_select_index_report(ctx, "[equal]", index); - - /* TODO: extract this routine as a function. */ - sid = GRN_UINT32_VALUE_AT(&(si->wv), 0); - weight = GRN_INT32_VALUE_AT(&(si->wv), 1); - ii_cursor = grn_ii_cursor_open(ctx, ii, tid, - GRN_ID_NIL, GRN_ID_MAX, - ii->n_elements, 0); - if (ii_cursor) { - grn_posting *posting; - while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) { - grn_posting new_posting; - - if (!(sid == 0 || posting->sid == sid)) { - continue; - } - - if (si->position.specified) { - while ((posting = grn_ii_cursor_next_pos(ctx, ii_cursor))) { - if (posting->pos == si->position.start) { - break; - } - } - if (!posting) { - continue; - } - } - - new_posting = *posting; - new_posting.weight *= weight; - grn_ii_posting_add(ctx, &new_posting, (grn_hash *)res, - si->logical_op); - } - grn_ii_cursor_close(ctx, ii_cursor); - processed = GRN_TRUE; - } - } - } - if (processed) { - grn_ii_resolve_sel_and(ctx, (grn_hash *)res, si->logical_op); - } - } + processed = grn_table_select_index_equal(ctx, table, index, si, res); break; case GRN_OP_SUFFIX : { -------------- next part -------------- HTML����������������������������... Télécharger