Example #1
0
grn_obj *
rb_grn_key_from_ruby_object (VALUE rb_key, grn_ctx *context,
                             grn_obj *key, grn_id domain_id, grn_obj *domain,
                             VALUE related_object)
{
    grn_id id;

    if (!domain)
        return RVAL2GRNBULK(rb_key, context, key);

    switch (domain->header.type) {
    case GRN_TYPE:
        return RVAL2GRNBULK_WITH_TYPE(rb_key, context, key, domain_id, domain);
        break;
    case GRN_TABLE_HASH_KEY:
    case GRN_TABLE_PAT_KEY:
    case GRN_TABLE_DAT_KEY:
    case GRN_TABLE_NO_KEY:
        id = RVAL2GRNID(rb_key, context, domain, related_object);
        break;
    default:
        if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_key, rb_cInteger)))
            rb_raise(rb_eGrnError,
                     "should be unsigned integer: <%s>: <%s>",
                     rb_grn_inspect(rb_key),
                     rb_grn_inspect(related_object));

        id = NUM2UINT(rb_key);
        break;
    }

    GRN_TEXT_SET(context, key, &id, sizeof(id));
    return key;
}
Example #2
0
grn_obj *
rb_grn_value_from_ruby_object (VALUE object, grn_ctx *context,
                               grn_obj *value, grn_id type_id, grn_obj *type)
{
    grn_bool string_p, table_type_p;

    string_p = rb_type(object) == T_STRING;
    table_type_p = (GRN_TABLE_HASH_KEY <= type->header.type &&
                    type->header.type <= GRN_TABLE_NO_KEY);
    if (!string_p) {
        return RVAL2GRNBULK_WITH_TYPE(object, context, value, type_id, type);
    }

    if (table_type_p && RSTRING_LEN(object) == 0) {
        if (value) {
            if (value->header.domain != type_id) {
                grn_obj_reinit(context, value, type_id, 0);
            }
        } else {
            value = grn_obj_open(context, GRN_BULK, 0, type_id);
            rb_grn_context_check(context, object);
        }
        GRN_RECORD_SET(context, value, GRN_ID_NIL);
        return value;
    }

    return RVAL2GRNBULK(object, context, value);
}
static grn_table_cursor *
rb_grn_patricia_trie_open_grn_near_cursor (int argc, VALUE *argv, VALUE self,
					     grn_ctx **context, int flags)
{
    grn_obj *table;
    grn_obj *key_p = NULL, casted_key;
    grn_table_cursor *cursor;
    unsigned min_size = 0;
    int offset = 0, limit = -1;
    VALUE options, rb_key, rb_min_size;
    VALUE rb_greater_than, rb_less_than, rb_offset, rb_limit;

    flags |= GRN_CURSOR_PREFIX;

    rb_grn_table_deconstruct((RbGrnTable *)SELF(self), &table, context,
			     NULL, NULL,
			     NULL, NULL, NULL,
			     NULL);

    rb_scan_args(argc, argv, "11", &rb_key, &options);

    rb_grn_scan_options(options,
			"size", &rb_min_size,
                        "offset", &rb_offset,
                        "limit", &rb_limit,
			"greater_than", &rb_greater_than,
			"less_than", &rb_less_than,
			NULL);

    key_p = RVAL2GRNBULK_WITH_TYPE(rb_key, *context, key_p,
				   table->header.domain, grn_ctx_at(*context, table->header.domain));
    GRN_OBJ_INIT(&casted_key, GRN_BULK, 0, table->header.domain);
    if (key_p->header.domain != table->header.domain) {
	grn_obj_cast(*context, key_p, &casted_key, 0);
	key_p = &casted_key;
    }

    if (!NIL_P(rb_min_size))
	min_size = NUM2UINT(rb_min_size);
    if (!NIL_P(rb_offset))
	offset = NUM2INT(rb_offset);
    if (!NIL_P(rb_limit))
	limit = NUM2INT(rb_limit);

    if (RVAL2CBOOL(rb_greater_than))
	flags |= GRN_CURSOR_GT;
    if (RVAL2CBOOL(rb_less_than))
	flags |= GRN_CURSOR_LT;

    cursor = grn_table_cursor_open(*context, table,
				   NULL, min_size,
				   GRN_BULK_HEAD(key_p), GRN_BULK_VSIZE(key_p),
				   offset, limit, flags);
    GRN_OBJ_FIN(*context, &casted_key);
    rb_grn_context_check(*context, self);

    return cursor;
}
Example #4
0
grn_obj *
rb_grn_bulk_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *bulk)
{
    if (bulk && bulk->header.domain == GRN_DB_TIME)
        return RVAL2GRNBULK_WITH_TYPE(object, context, bulk,
                                      bulk->header.domain,
                                      grn_ctx_at(context, bulk->header.domain));

    if (!bulk) {
        bulk = grn_obj_open(context, GRN_BULK, 0, GRN_ID_NIL);
        rb_grn_context_check(context, object);
    }

    switch (TYPE(object)) {
    case T_NIL:
        grn_obj_reinit(context, bulk, GRN_DB_VOID, 0);
        break;
    case T_SYMBOL:
        object = rb_funcall(object, rb_intern("to_s"), 0);
    case T_STRING:
        grn_obj_reinit(context, bulk, GRN_DB_TEXT, 0);
        rb_grn_context_text_set(context, bulk, object);
        break;
    case T_FIXNUM:
    case T_BIGNUM: {
        int64_t int64_value;
        int64_value = NUM2LL(object);
        if (int64_value <= INT32_MAX) {
            grn_obj_reinit(context, bulk, GRN_DB_INT32, 0);
            GRN_INT32_SET(context, bulk, int64_value);
        } else {
            grn_obj_reinit(context, bulk, GRN_DB_INT64, 0);
            GRN_INT64_SET(context, bulk, int64_value);
        }
        break;
    }
    case T_FLOAT:
        grn_obj_reinit(context, bulk, GRN_DB_FLOAT, 0);
        GRN_FLOAT_SET(context, bulk, NUM2DBL(object));
        break;
    case T_TRUE:
        grn_obj_reinit(context, bulk, GRN_DB_BOOL, 0);
        GRN_BOOL_SET(context, bulk, GRN_TRUE);
        break;
    case T_FALSE:
        grn_obj_reinit(context, bulk, GRN_DB_BOOL, 0);
        GRN_BOOL_SET(context, bulk, GRN_FALSE);
        break;
    default:
        if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cTime))) {
            VALUE sec, usec;
            int64_t time_value;

            sec = rb_funcall(object, rb_intern("to_i"), 0);
            usec = rb_funcall(object, rb_intern("usec"), 0);
            time_value = GRN_TIME_PACK(NUM2LL(sec), NUM2LL(usec));
            grn_obj_reinit(context, bulk, GRN_DB_TIME, 0);
            GRN_TIME_SET(context, bulk, time_value);
        } else if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnObject))) {
            grn_obj *grn_object;
            grn_id id_value;

            grn_object = RVAL2GRNOBJECT(object, &context);
            grn_obj_reinit(context, bulk, grn_object->header.domain, 0);
            id_value = grn_obj_id(context, grn_object);
            GRN_RECORD_SET(context, bulk, id_value);
        } else if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnRecord))) {
            grn_obj *table;
            grn_id id_value;

            table = RVAL2GRNOBJECT(rb_funcall(object, rb_intern("table"), 0),
                                   &context);
            id_value = NUM2UINT(rb_funcall(object, rb_intern("id"), 0));
            grn_obj_reinit(context, bulk, grn_obj_id(context, table), 0);
            GRN_RECORD_SET(context, bulk, id_value);
        } else {
            rb_raise(rb_eTypeError,
                     "bulked object should be one of "
                     "[nil, true, false, String, Symbol, Integer, Float, Time, "
                     "Groonga::Object, Groonga::Record]: %s",
                     rb_grn_inspect(object));
        }
        break;
    }

    return bulk;
}