static mrb_value mrb_grn_index_cursor_count(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_id term_id; int n_records = 0; while (grn_index_cursor_next(ctx, DATA_PTR(self), &term_id)) { n_records++; } return mrb_fixnum_value(n_records); }
static VALUE rb_grn_index_cursor_each (VALUE self) { grn_obj *cursor; grn_ctx *context; RETURN_ENUMERATOR(self, 0, NULL); rb_grn_index_cursor_deconstruct(SELF(self), &cursor, &context, NULL, NULL, NULL, NULL); if (context && cursor) { grn_posting *posting; grn_id tid; while ((posting = grn_index_cursor_next(context, cursor, &tid))) { rb_yield(rb_grn_posting_new(posting, tid)); } } return Qnil; }
static VALUE rb_grn_index_cursor_next (VALUE self) { VALUE rb_posting = Qnil; grn_obj *cursor; grn_ctx *context; rb_grn_index_cursor_deconstruct(SELF(self), &cursor, &context, NULL, NULL, NULL, NULL); if (context && cursor) { grn_posting *posting; grn_id tid; posting = grn_index_cursor_next(context, cursor, &tid); if (posting) { rb_posting = rb_grn_posting_new(posting, tid); } } return rb_posting; }
static mrb_value mrb_grn_index_cursor_select(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; mrb_value mrb_result_set; mrb_value mrb_options; grn_obj *index_cursor; grn_obj *expr = NULL; grn_obj *expr_variable = NULL; int offset = 0; int limit = 10; int n_matched_records = 0; mrb_value mrb_index; grn_obj *index; grn_obj *lexicon; grn_obj *data_table; grn_hash *result_set; grn_posting *posting; grn_id term_id; grn_operator op = GRN_OP_OR; mrb_get_args(mrb, "o|H", &mrb_result_set, &mrb_options); index_cursor = DATA_PTR(self); result_set = DATA_PTR(mrb_result_set); if (!mrb_nil_p(mrb_options)) { mrb_value mrb_expr; mrb_value mrb_offset; mrb_value mrb_limit; mrb_expr = grn_mrb_options_get_lit(mrb, mrb_options, "expression"); if (!mrb_nil_p(mrb_expr)) { expr = DATA_PTR(mrb_expr); expr_variable = grn_expr_get_var_by_offset(ctx, expr, 0); } mrb_offset = grn_mrb_options_get_lit(mrb, mrb_options, "offset"); if (!mrb_nil_p(mrb_offset)) { offset = mrb_fixnum(mrb_offset); } mrb_limit = grn_mrb_options_get_lit(mrb, mrb_options, "limit"); if (!mrb_nil_p(mrb_limit)) { limit = mrb_fixnum(mrb_limit); } } if (limit <= 0) { return mrb_fixnum_value(n_matched_records); } mrb_index = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@index")); index = DATA_PTR(mrb_index); lexicon = ((grn_ii *)index)->lexicon; data_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, index)); while ((posting = grn_index_cursor_next(ctx, index_cursor, &term_id))) { if (expr) { grn_bool matched_raw; grn_obj *matched; GRN_RECORD_SET(ctx, expr_variable, posting->rid); matched = grn_expr_exec(ctx, expr, 0); if (!matched) { grn_mrb_ctx_check(mrb); continue; } GRN_TRUEP(ctx, matched, matched_raw); if (!matched_raw) { continue; } } n_matched_records++; if (offset > 0) { offset--; continue; } grn_ii_posting_add(ctx, (grn_ii_posting *)posting, result_set, op); limit--; if (limit == 0) { break; } } grn_ii_resolve_sel_and(ctx, result_set, op); return mrb_fixnum_value(n_matched_records); }