Navigation Menu

Skip to content

Commit

Permalink
mrb: implement match with expr case in mruby
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jun 10, 2014
1 parent 1462fb0 commit e1c3c46
Show file tree
Hide file tree
Showing 9 changed files with 397 additions and 58 deletions.
4 changes: 4 additions & 0 deletions lib/ctx_impl_mrb.c
Expand Up @@ -20,6 +20,8 @@
#include "ctx_impl.h"

#include "mrb.h"
#include "mrb/mrb_id.h"
#include "mrb/mrb_operator.h"
#include "mrb/mrb_ctx.h"
#include "mrb/mrb_bulk.h"
#include "mrb/mrb_obj.h"
Expand All @@ -40,6 +42,8 @@ grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx)
mrb->ud = ctx;
ctx->impl->mrb.module = mrb_define_module(mrb, "Groonga");

grn_mrb_id_init(ctx);
grn_mrb_operator_init(ctx);
grn_mrb_ctx_init(ctx);
grn_mrb_bulk_init(ctx);
grn_mrb_obj_init(ctx);
Expand Down
9 changes: 5 additions & 4 deletions lib/mrb/mrb_bulk.c
Expand Up @@ -63,12 +63,13 @@ mrb_grn_bulk_get_value(mrb_state *mrb, mrb_value self)
switch (bulk->header.domain) {
case GRN_DB_UINT32 :
{
uint32_t value;
int64_t value;
value = GRN_UINT32_VALUE(bulk);
if (!FIXABLE(value)) {
mrb_raisef(mrb, E_ARGUMENT_ERROR,
"can't handle large number: <%u>: max: <%" PRIiMRB_INT ">",
value, PRIiMRB_INT);
mrb_raisef(mrb, E_RANGE_ERROR,
"can't handle large number: <%S>: max: <%S>",
mrb_fixnum_value(value), /* TODO: This will cause overflow */
mrb_fixnum_value(MRB_INT_MAX));
}
mrb_value_ = mrb_fixnum_value(value);
}
Expand Down
69 changes: 17 additions & 52 deletions lib/mrb/mrb_expr.c
Expand Up @@ -174,60 +174,14 @@ scan_info_build(grn_ctx *ctx, grn_obj *expr, int *n,
mrb_funcall(mrb, mrb_si, "end=", 1, mrb_fixnum_value(c - e->codes));
sis[i++] = si;
{
int sid, k;
grn_obj *index, *arg, **p = &arg;
int k;
grn_obj *arg, **p = &arg;
for (k = 0; (arg = grn_scan_info_get_arg(ctx, si, k)) ; k++) {
if ((*p)->header.type == GRN_EXPR) {
uint32_t j;
grn_expr_code *ec;
grn_expr *e = (grn_expr *)(*p);
for (j = e->codes_curr, ec = e->codes; j--; ec++) {
int32_t weight;
if (ec->value) {
switch (ec->value->header.type) {
case GRN_ACCESSOR :
{
mrb_value mrb_ec;

mrb_ec = mrb_grn_expr_code_new(mrb, ec);
mrb_funcall(mrb, mrb_si, "match_expr_resolve_index_accessor",
1, mrb_ec);
}
break;
case GRN_COLUMN_FIX_SIZE :
case GRN_COLUMN_VAR_SIZE :
{
mrb_value mrb_ec;

mrb_ec = mrb_grn_expr_code_new(mrb, ec);
mrb_funcall(mrb, mrb_si,
"match_expr_resolve_index_data_column",
1, mrb_ec);
}
break;
case GRN_COLUMN_INDEX :
sid = 0;
index = ec->value;
if (j > 2 &&
ec[1].value &&
ec[1].value->header.domain == GRN_DB_UINT32 &&
ec[2].op == GRN_OP_GET_MEMBER) {
sid = GRN_UINT32_VALUE(ec[1].value) + 1;
j -= 2;
ec += 2;
}
{
mrb_value mrb_ec = mrb_grn_expr_code_new(mrb, ec);
weight = mrb_fixnum(mrb_funcall(mrb, mrb_ec, "weight", 0));
}
mrb_funcall(mrb, mrb_si, "put_index", 3,
grn_mrb_value_from_grn_obj(mrb, index),
mrb_fixnum_value(sid),
mrb_fixnum_value(weight));
break;
}
}
}
mrb_value mrb_expr;
mrb_expr = grn_mrb_value_from_grn_obj(mrb, *p);
mrb_funcall(mrb, mrb_si, "match_resolve_index_expression",
1, mrb_expr);
} else if (GRN_DB_OBJP(*p)) {
mrb_value mrb_db_obj = grn_mrb_value_from_grn_obj(mrb, *p);
mrb_funcall(mrb, mrb_si, "match_resolve_index_db_obj", 1, mrb_db_obj);
Expand Down Expand Up @@ -518,6 +472,15 @@ mrb_grn_expr_code_get_value(mrb_state *mrb, mrb_value self)
return grn_mrb_value_from_grn_obj(mrb, expr_code->value);
}

static mrb_value
mrb_grn_expr_code_get_op(mrb_state *mrb, mrb_value self)
{
grn_expr_code *expr_code;

expr_code = DATA_PTR(self);
return mrb_fixnum_value(expr_code->op);
}

static mrb_value
mrb_grn_expression_initialize(mrb_state *mrb, mrb_value self)
{
Expand Down Expand Up @@ -587,6 +550,8 @@ grn_mrb_expr_init(grn_ctx *ctx)
mrb_grn_expr_code_get_weight, MRB_ARGS_NONE());
mrb_define_method(mrb, klass, "value",
mrb_grn_expr_code_get_value, MRB_ARGS_NONE());
mrb_define_method(mrb, klass, "op",
mrb_grn_expr_code_get_op, MRB_ARGS_NONE());

klass = mrb_define_class_under(mrb, module, "Expression", object_class);
MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
Expand Down
74 changes: 74 additions & 0 deletions lib/mrb/mrb_id.c
@@ -0,0 +1,74 @@
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2014 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../ctx_impl.h"

#ifdef GRN_WITH_MRUBY
#include <mruby.h>

#include "mrb_id.h"

void
grn_mrb_id_init(grn_ctx *ctx)
{
mrb_state *mrb = ctx->impl->mrb.state;
struct RClass *module = ctx->impl->mrb.module;
struct RClass *id_module;

id_module = mrb_define_module_under(mrb, module, "ID");

mrb_define_const(mrb, id_module, "VOID",
mrb_fixnum_value(GRN_DB_VOID));
mrb_define_const(mrb, id_module, "DB",
mrb_fixnum_value(GRN_DB_DB));
mrb_define_const(mrb, id_module, "OBJECT",
mrb_fixnum_value(GRN_DB_OBJECT));
mrb_define_const(mrb, id_module, "BOOL",
mrb_fixnum_value(GRN_DB_BOOL));
mrb_define_const(mrb, id_module, "INT8",
mrb_fixnum_value(GRN_DB_INT8));
mrb_define_const(mrb, id_module, "UINT8",
mrb_fixnum_value(GRN_DB_UINT8));
mrb_define_const(mrb, id_module, "INT16",
mrb_fixnum_value(GRN_DB_INT16));
mrb_define_const(mrb, id_module, "UINT16",
mrb_fixnum_value(GRN_DB_UINT16));
mrb_define_const(mrb, id_module, "INT32",
mrb_fixnum_value(GRN_DB_INT32));
mrb_define_const(mrb, id_module, "UINT32",
mrb_fixnum_value(GRN_DB_UINT32));
mrb_define_const(mrb, id_module, "INT64",
mrb_fixnum_value(GRN_DB_INT64));
mrb_define_const(mrb, id_module, "UINT64",
mrb_fixnum_value(GRN_DB_UINT64));
mrb_define_const(mrb, id_module, "FLOAT",
mrb_fixnum_value(GRN_DB_FLOAT));
mrb_define_const(mrb, id_module, "TIME",
mrb_fixnum_value(GRN_DB_TIME));
mrb_define_const(mrb, id_module, "SHORT_TEXT",
mrb_fixnum_value(GRN_DB_SHORT_TEXT));
mrb_define_const(mrb, id_module, "TEXT",
mrb_fixnum_value(GRN_DB_TEXT));
mrb_define_const(mrb, id_module, "LONG_TEXT",
mrb_fixnum_value(GRN_DB_LONG_TEXT));
mrb_define_const(mrb, id_module, "TOKYO_GEO_POINT",
mrb_fixnum_value(GRN_DB_TOKYO_GEO_POINT));
mrb_define_const(mrb, id_module, "WGS84_GEO_POINT",
mrb_fixnum_value(GRN_DB_WGS84_GEO_POINT));
}
#endif
34 changes: 34 additions & 0 deletions lib/mrb/mrb_id.h
@@ -0,0 +1,34 @@
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2014 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef GRN_MRB_ID_H
#define GRN_MRB_ID_H

#include "../ctx.h"

#ifdef __cplusplus
extern "C" {
#endif

void grn_mrb_id_init(grn_ctx *ctx);

#ifdef __cplusplus
}
#endif

#endif /* GRN_MRB_ID_H */

0 comments on commit e1c3c46

Please sign in to comment.