示例#1
0
VALUE
rb_grn_table_cursor_to_ruby_object (VALUE klass, grn_ctx *context,
				    grn_table_cursor *cursor,
				    rb_grn_boolean owner)
{
    return GRNOBJECT2RVAL(klass, context, cursor, owner);
}
示例#2
0
grn_id
rb_grn_id_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *table,
                            VALUE related_object)
{
    VALUE rb_id;

    if (NIL_P(object))
        return Qnil;

    if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnRecord))) {
        VALUE rb_table;
        rb_table = rb_funcall(object, rb_intern("table"), 0);
        if (table && RVAL2GRNOBJECT(rb_table, &context) != table) {
            VALUE rb_expected_table;

            rb_expected_table =
                GRNOBJECT2RVAL(Qnil, context, table, GRN_FALSE);
            rb_raise(rb_eGrnError,
                     "wrong table: expected <%s>: actual <%s>",
                     rb_grn_inspect(rb_expected_table),
                     rb_grn_inspect(rb_table));
        }
        rb_id = rb_funcall(object, rb_intern("id"), 0);
    } else {
        rb_id = object;
    }

    if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_id, rb_cInteger)))
        rb_raise(rb_eGrnError,
                 "should be unsigned integer or Groogna::Record: <%s>: <%s>",
                 rb_grn_inspect(object),
                 rb_grn_inspect(related_object));

    return NUM2UINT(rb_id);
}
示例#3
0
VALUE
rb_grn_index_cursor_to_ruby_object (grn_ctx *context,
				    grn_obj *cursor,
				    grn_bool owner)
{
    return GRNOBJECT2RVAL(rb_cGrnIndexCursor, context, cursor, owner);
}
示例#4
0
/*
 * データベース内のオブジェクトを順番にブロックに渡す。
 *
 * @example すべてのオブジェクトの名前を表示する:
 *   database.each do |object|
 *     p object.name
 *   end
 *
 * @example すべてのオブジェクトの名前をID順で表示する:
 *   database.each(:order_by => :id) do |object|
 *     p object.name
 *   end
 *
 * @example すべてのオブジェクトの名前をキー名の降順で表示する:
 *   database.each(:order_by => :key, :order => :desc) do |object|
 *     p object.name
 *   end
 *
 * @overload each(options=nil)
 *   @macro [new] database.each.options
 *     @param options [::Hash]
 *     @yield [object]
 *     @option options :order
 *       +:asc+ または +:ascending+ を指定すると昇順にレコードを取
 *       り出す。(デフォルト)
 *       +:desc+ または +:descending+ を指定すると降順にレコードを
 *       取り出す。
 *     @option options :order_by (:key)
 *       +:id+ を指定するとID順にレコードを取り出す。
 *       +:key+ 指定するとキー順にレコードを取り出す。(デフォル
 *       ト)
 *   @macro database.each.options
 *
 * @overload each(options=nil)
 *   @macro database.each.options
 *   @option options :ignore_missing_object (false)
 *     Specify +true+ to ignore missing object. Otherwise, an exception is
 *     raised for missing object.
 *
 *   @since 2.0.5
 */
static VALUE
rb_grn_database_each (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *database;
    grn_table_cursor *cursor;
    VALUE rb_cursor, rb_options, rb_order, rb_order_by;
    VALUE rb_ignore_missing_object;
    int flags = 0;
    grn_id id;
    VALUE exception;

    RETURN_ENUMERATOR(self, argc, argv);

    rb_grn_database_deconstruct(SELF(self), &database, &context,
                                NULL, NULL, NULL, NULL);

    rb_scan_args(argc, argv, "01", &rb_options);

    rb_grn_scan_options(rb_options,
                        "order", &rb_order,
                        "order_by", &rb_order_by,
                        "ignore_missing_object", &rb_ignore_missing_object,
                        NULL);

    flags |= rb_grn_table_cursor_order_to_flag(rb_order);
    flags |= rb_grn_table_cursor_order_by_to_flag(GRN_TABLE_PAT_KEY,
                                                  self,
                                                  rb_order_by);

    cursor = grn_table_cursor_open(context, database, NULL, 0, NULL, 0,
                                   0, -1,
                                   flags);
    rb_cursor = GRNTABLECURSOR2RVAL(Qnil, context, cursor);
    rb_iv_set(self, "cursor", rb_cursor);
    while ((id = grn_table_cursor_next(context, cursor)) != GRN_ID_NIL) {
        grn_obj *object;

        object = grn_ctx_at(context, id);
        if (!object && RTEST(rb_ignore_missing_object)) {
            context->rc = GRN_SUCCESS;
            continue;
        }

        exception = rb_grn_context_to_exception(context, self);
        if (!NIL_P(exception)) {
            rb_grn_object_close(rb_cursor);
            rb_iv_set(self, "cursor", Qnil);
            rb_exc_raise(exception);
        }

        if (object) {
            rb_yield(GRNOBJECT2RVAL(Qnil, context, object, GRN_FALSE));
        }
    }
    rb_grn_object_close(rb_cursor);
    rb_iv_set(self, "cursor", Qnil);

    return Qnil;
}
示例#5
0
/*
 * call-seq:
 *   patricia_trie.prefix_search(prefix) -> Groonga::Hash
 *
 * キーが_prefix_に前方一致するレコードのIDがキーに入っている
 * Groonga::Hashを返す。マッチするレコードがない場合は空の
 * Groonga::Hashが返る。
 *
 */
static VALUE
rb_grn_patricia_trie_prefix_search (VALUE self, VALUE rb_prefix)
{
    grn_ctx *context;
    grn_obj *table, *key, *domain, *result;
    grn_id domain_id;
    VALUE rb_result;

    rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
					 &key, &domain_id, &domain,
					 NULL, NULL, NULL,
					 NULL);

    result = grn_table_create(context, NULL, 0, NULL,
			      GRN_OBJ_TABLE_HASH_KEY,
			      table, 0);
    rb_grn_context_check(context, self);
    rb_result = GRNOBJECT2RVAL(Qnil, context, result, GRN_TRUE);

    GRN_BULK_REWIND(key);
    RVAL2GRNKEY(rb_prefix, context, key, domain_id, domain, self);
    grn_pat_prefix_search(context, (grn_pat *)table,
			  GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key),
			  (grn_hash *)result);
    rb_grn_context_check(context, self);

    return rb_result;
}
示例#6
0
/*
 * call-seq:
 *   column.sources -> Groonga::Columnの配列
 *
 * インデックス対象となっているカラムの配列を返す。
 */
static VALUE
rb_grn_index_column_get_sources (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *column;
    grn_obj sources;
    grn_id *source_ids;
    VALUE rb_sources;
    int i, n;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
				    NULL, NULL,
				    NULL, NULL, NULL, NULL,
				    NULL, NULL);

    GRN_OBJ_INIT(&sources, GRN_BULK, 0, GRN_ID_NIL);
    grn_obj_get_info(context, column, GRN_INFO_SOURCE, &sources);
    rb_grn_context_check(context, self);

    n = GRN_BULK_VSIZE(&sources) / sizeof(grn_id);
    source_ids = (grn_id *)GRN_BULK_HEAD(&sources);
    rb_sources = rb_ary_new2(n);
    for (i = 0; i < n; i++) {
	grn_obj *source;
	VALUE rb_source;

	source = grn_ctx_at(context, *source_ids);
	rb_source = GRNOBJECT2RVAL(Qnil, context, source, RB_GRN_FALSE);
	rb_ary_push(rb_sources, rb_source);
	source_ids++;
    }
    grn_obj_unlink(context, &sources);

    return rb_sources;
}
示例#7
0
/*
 * call-seq:
 *   Groonga::Array.create(options={})                -> Groonga::Array
 *   Groonga::Array.create(options={}) {|table| ... }
 *
 * キーのないテーブルを生成する。ブロックを指定すると、そのブ
 * ロックに生成したテーブルが渡され、ブロックを抜けると自動的
 * にテーブルが破棄される。
 *
 * @example
 *   #無名一時テーブルを生成する。
 *   Groonga::Array.create
 *
 *   #無名永続テーブルを生成する。
 *   Groonga::Array.create(:path => "/tmp/array.grn")
 *
 *   #名前付き永続テーブルを生成する。ただし、ファイル名は気にしない。
 *   Groonga::Array.create(:name => "Bookmarks",
 *                         :persistent => true)
 *
 *   #それぞれのレコードに512バイトの値を格納できる無名一時テーブルを生成する。
 *   Groonga::Array.create(:value => 512)
 *
 * @param [::Hash] options The name and value
 *   pairs. Omitted names are initialized as the default value.
 * @option options [Grrnga::Context] :context (Groonga::Context.default) The context
 *   テーブルが利用するGrrnga::Context
 * @option options :name The name
 *   テーブルの名前。名前をつけると、Groonga::Context#[]に名
 *   前を指定してテーブルを取得することができる。省略すると
 *   無名テーブルになり、テーブルIDでのみ取得できる。
 * @option options :path The path
 *   テーブルを保存するパス。パスを指定すると永続テーブルとな
 *   り、プロセス終了後もレコードは保持される。次回起動時に
 *   Groonga::Context#[]で保存されたレコードを利用することが
 *   できる。省略すると一時テーブルになり、プロセスが終了する
 *   とレコードは破棄される。
 * @option options :persistent The persistent
 *   +true+ を指定すると永続テーブルとなる。 +path+ を省略した
 *   場合は自動的にパスが付加される。 +:context+ で指定した
 *   Groonga::Contextに結びついているデータベースが一時デー
 *   タベースの場合は例外が発生する。
 * @option options :value_type (nil) The value_type
 *   値の型を指定する。省略すると値のための領域を確保しない。
 *   値を保存したい場合は必ず指定すること。
 *   参考: Groonga::Type.new
 * @option options [Groonga::Record#n_sub_records] :sub_records The sub_records
 *   +true+ を指定すると#groupでグループ化したときに、
 *   Groonga::Record#n_sub_recordsでグループに含まれるレコー
 *   ドの件数を取得できる。
 */
static VALUE
rb_grn_array_s_create (int argc, VALUE *argv, VALUE klass)
{
    grn_ctx *context = NULL;
    grn_obj *value_type = NULL, *table;
    const char *name = NULL, *path = NULL;
    unsigned name_size = 0;
    grn_obj_flags flags = GRN_OBJ_TABLE_NO_KEY;
    VALUE rb_table;
    VALUE options, rb_context, rb_name, rb_path, rb_persistent;
    VALUE rb_value_type, rb_sub_records;

    rb_scan_args(argc, argv, "01", &options);

    rb_grn_scan_options(options,
			"context", &rb_context,
			"name", &rb_name,
                        "path", &rb_path,
			"persistent", &rb_persistent,
			"value_type", &rb_value_type,
			"sub_records", &rb_sub_records,
			NULL);

    context = rb_grn_context_ensure(&rb_context);

    if (!NIL_P(rb_name)) {
        name = StringValuePtr(rb_name);
	name_size = RSTRING_LEN(rb_name);
	flags |= GRN_OBJ_PERSISTENT;
    }

    if (!NIL_P(rb_path)) {
        path = StringValueCStr(rb_path);
	flags |= GRN_OBJ_PERSISTENT;
    }

    if (RVAL2CBOOL(rb_persistent))
	flags |= GRN_OBJ_PERSISTENT;

    if (!NIL_P(rb_value_type))
	value_type = RVAL2GRNOBJECT(rb_value_type, &context);

    if (RVAL2CBOOL(rb_sub_records))
	flags |= GRN_OBJ_WITH_SUBREC;

    table = grn_table_create(context, name, name_size, path,
			     flags, NULL, value_type);
    if (!table)
	rb_grn_context_check(context, rb_ary_new4(argc, argv));
    rb_table = GRNOBJECT2RVAL(klass, context, table, GRN_TRUE);
    rb_grn_context_check(context, rb_table);
    rb_iv_set(rb_table, "@context", rb_context);

    if (rb_block_given_p())
        return rb_ensure(rb_yield, rb_table, rb_grn_object_close, rb_table);
    else
        return rb_table;
}
示例#8
0
/*
 * Document-method: search
 *
 * call-seq:
 *   column.search(query, options={}) -> Groonga::Hash
 *
 * _object_から_query_に対応するオブジェクトを検索し、見つかっ
 * たオブジェクトのIDがキーになっているGroonga::Hashを返す。
 *
 * 利用可能なオプションは以下の通り。
 *
 * [_:result_]
 *   結果を格納するGroonga::Hash。指定しない場合は新しく
 *   Groonga::Hashを生成し、それに結果を格納して返す。
 * [_:operator_]
 *   以下のどれかの値を指定する。+nil+, <tt>"or"</tt>, <tt>"||"</tt>,
 *   <tt>"and"</tt>, <tt>"+"</tt>, <tt>"&&"</tt>, <tt>"but"</tt>,
 *   <tt>"not"</tt>, <tt>"-"</tt>, <tt>"adjust"</tt>, <tt>">"</tt>。
 *   それぞれ以下のようになる。(FIXME: 「以下」)
 * [_:exact_]
 *   +true+を指定すると完全一致で検索する
 * [_:longest_common_prefix_]
 *   +true+を指定すると_query_と同じ接頭辞をもつエントリのう
 *   ち、もっとも長いエントリを検索する
 * [_:suffix_]
 *   +true+を指定すると_query_が後方一致するエントリを検索す
 *   る
 * [_:prefix_]
 *   +true+を指定すると_query_が前方一致するレコードを検索す
 *   る
 * [_:near_]
 *   +true+を指定すると_query_に指定した複数の語が近傍に含ま
 *   れるレコードを検索する
 * [...]
 *   ...
 */
static VALUE
rb_grn_index_column_search (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *column;
    grn_obj *range;
    grn_obj *query = NULL, *id_query = NULL, *string_query = NULL;
    grn_obj *result;
    grn_operator operator;
    grn_rc rc;
    VALUE rb_query, options, rb_result, rb_operator;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
				    NULL, NULL,
				    NULL, NULL, NULL, &range,
				    &id_query, &string_query);

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

    if (CBOOL2RVAL(rb_obj_is_kind_of(rb_query, rb_cGrnQuery))) {
	grn_query *_query;
	_query = RVAL2GRNQUERY(rb_query);
	query = (grn_obj *)_query;
    } else if (CBOOL2RVAL(rb_obj_is_kind_of(rb_query, rb_cInteger))) {
	grn_id id;
	id = NUM2UINT(rb_query);
	GRN_TEXT_SET(context, id_query, &id, sizeof(grn_id));
	query = id_query;
    } else {
	const char *_query;
	_query = StringValuePtr(rb_query);
	GRN_TEXT_SET(context, string_query, _query, RSTRING_LEN(rb_query));
	query = string_query;
    }

    rb_grn_scan_options(options,
			"result", &rb_result,
			"operator", &rb_operator,
			NULL);

    if (NIL_P(rb_result)) {
	result = grn_table_create(context, NULL, 0, NULL,
				  GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
				  range, 0);
	rb_grn_context_check(context, self);
	rb_result = GRNOBJECT2RVAL(Qnil, context, result, RB_GRN_TRUE);
    } else {
	result = RVAL2GRNOBJECT(rb_result, &context);
    }

    operator = RVAL2GRNOPERATOR(rb_operator);

    rc = grn_obj_search(context, column, query, result, operator, NULL);
    rb_grn_rc_check(rc, self);

    return rb_result;
}
示例#9
0
VALUE
rb_grn_value_to_ruby_object (grn_ctx *context,
			     grn_obj *value,
			     grn_obj *range,
			     VALUE related_object)
{
    if (!value)
	return Qnil;

    switch (value->header.type) {
      case GRN_VOID:
	return Qnil;
	break;
      case GRN_BULK:
	if (GRN_BULK_EMPTYP(value))
	    return Qnil;
	if (value->header.domain == GRN_ID_NIL && range)
	    value->header.domain = grn_obj_id(context, range);
	return GRNBULK2RVAL(context, value, range, related_object);
	break;
      case GRN_UVECTOR:
	{
	    VALUE rb_value, rb_range = Qnil;
	    grn_id *uvector, *uvector_end;

	    rb_value = rb_ary_new();
	    if (range)
		rb_range = GRNTABLE2RVAL(context, range, GRN_FALSE);
	    uvector = (grn_id *)GRN_BULK_HEAD(value);
	    uvector_end = (grn_id *)GRN_BULK_CURR(value);
	    for (; uvector < uvector_end; uvector++) {
		VALUE record = Qnil;
		if (*uvector != GRN_ID_NIL)
		    record = rb_grn_record_new(rb_range, *uvector, Qnil);
		rb_ary_push(rb_value, record);
	    }
	    return rb_value;
	}
	break;
      case GRN_VECTOR:
	return GRNVECTOR2RVAL(context, value);
	break;
      default:
	rb_raise(rb_eGrnError,
		 "unsupported value type: %s(%#x): %s",
		 rb_grn_inspect_type(value->header.type),
		 value->header.type,
		 rb_grn_inspect(related_object));
	break;
    }

    if (!range)
	return GRNOBJECT2RVAL(Qnil, context, value, GRN_FALSE);

    return Qnil;
}
示例#10
0
/*
 * Rewrites expression.
 *
 * @example
 *   expression.parse("age >= 10 AND age < 20",
 *                    :syntax => :script)
 *   expression.rewrite # => New rewritten expression.
 *                      #    It'll use between(age, 10, "include", 20, "exclude")
 *
 * @overload rewrite
 *   @return [Groonga::Expression, nil] new rewritten expression when
 *      the expression is rewritten, `nil` otherwise.
 *
 * @since 5.1.0
 */
static VALUE
rb_grn_expression_rewrite (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *expression;
    grn_obj *rewritten_expression;

    rb_grn_expression_deconstruct(SELF(self), &expression, &context,
                                  NULL, NULL,
                                  NULL, NULL, NULL);

    rewritten_expression = grn_expr_rewrite(context, expression);

    return GRNOBJECT2RVAL(Qnil, context, rewritten_expression, GRN_TRUE);
}
示例#11
0
/*
 * call-seq:
 *   column.table -> Groonga::Table
 *
 * カラムが所属するテーブルを返す。
 */
static VALUE
rb_grn_column_get_table (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *column;
    grn_obj *table;

    rb_grn_object_deconstruct((RbGrnObject *)(SELF(self)), &column, &context,
			      NULL, NULL,
			      NULL, NULL);
    table = grn_column_table(context, column);
    rb_grn_context_check(context, self);

    return GRNOBJECT2RVAL(Qnil, context, table, RB_GRN_FALSE);
}
/*
 * _key_ にマッチするレコードのIDがキーに入っている
 * {Groonga::Hash} を返す。マッチするレコードがない場合は空の
 * {fGroonga::Hash} が返る。
 * _options_ で +:result+ を指定することにより、そのテーブルにマッ
 * チしたレコードIDがキーのレコードを追加することができる。
 * +:result+ にテーブルを指定した場合は、そのテーブルが返る。
 *
 * @example 複数のキーで検索し、結果を1つのテーブルに集める。
 *   result = nil
 *   keys = ["morita", "gunyara-kun", "yu"]
 *   keys.each do |key|
 *     result = users.search(key, :result => result)
 *   end
 *   result.each do |record|
 *     user = record.key
 *     p user.key # -> "morita"または"gunyara-kun"または"yu"
 *   end
 *
 * @overload search(key, options=nil)
 *   @return [Groonga::Hash]
 *   @param key [String] レコードにマッチさせる値
 *   @param options [::Hash] The name and value
 *     pairs. Omitted names are initialized as the default value.
 *   @option options :result The result
 *     結果を格納するテーブル。
 *   @option options :operator (Groonga::Operator::OR)
 *     マッチしたレコードをどのように扱うか。指定可能な値は以
 *     下の通り。
 *
 *     - Groonga::Operator::OR :=
 *       マッチしたレコードを追加。すでにレコードが追加され
 *       ている場合は何もしない。 =:
 *     - Groonga::Operator::AND :=
 *       マッチしたレコードのスコアを増加。マッチしなかった
 *       レコードを削除。 =:
 *     - Groonga::Operator::AND_NOT :=
 *       マッチしたレコードを削除。 =:
 *     - Groonga::Operator::ADJUST :=
 *       マッチしたレコードのスコアを増加。 =:
 *     - +:type+ :=
 *       ????? =:
 *
 */
static VALUE
rb_grn_double_array_trie_search (int argc, VALUE *argv, VALUE self)
{
    grn_rc rc;
    grn_ctx *context;
    grn_obj *table;
    grn_id domain_id;
    grn_obj *key, *domain, *result;
    grn_operator operator;
    grn_search_optarg search_options;
    grn_bool search_options_is_set = GRN_FALSE;
    VALUE rb_key, options, rb_result, rb_operator, rb_type;

    rb_grn_double_array_trie_deconstruct(SELF(self), &table, &context,
                                         &key, NULL, &domain_id, &domain,
                                         NULL, NULL, NULL,
                                         NULL);

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

    RVAL2GRNKEY(rb_key, context, key, domain_id, domain, self);

    rb_grn_scan_options(options,
                        "result", &rb_result,
                        "operator", &rb_operator,
                        "type", &rb_type,
                        NULL);

    if (NIL_P(rb_result)) {
        result = grn_table_create(context, NULL, 0, NULL,
                                  GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
                                  table, 0);
        rb_grn_context_check(context, self);
        rb_result = GRNOBJECT2RVAL(Qnil, context, result, GRN_TRUE);
    } else {
        result = RVAL2GRNOBJECT(rb_result, &context);
    }

    operator = RVAL2GRNOPERATOR(rb_operator);

    rc = grn_obj_search(context, table, key,
                        result, operator,
                        search_options_is_set ? &search_options : NULL);
    rb_grn_rc_check(rc, self);

    return rb_result;
}
示例#13
0
/*
 * call-seq:
 *   Groonga::View.create(options={})                -> Groonga::View
 *   Groonga::View.create(options={}) {|table| ... }
 *
 * 複数のテーブルを1つのテーブルとして扱う仮想的なテーブル
 * (ビュー)を生成する。ブロックを指定すると、そのブロック
 * に生成したテーブルが渡され、ブロックを抜けると自動的にテー
 * ブルが破棄される。
 *
 * ビューにテーブルを追加するときはGroonga::View#add_tableを
 * 使う。
 *
 * _options_に指定可能な値は以下の通り。
 *
 * [+:context+]
 *   ビューが利用するGroonga::Context。省略すると
 *   Groonga::Context.defaultを用いる。
 *
 * [+:name+]
 *   ビューの名前。名前をつけると、Groonga::Context#[]に名
 *   前を指定してビューを取得することができる。省略すると
 *   無名ビューになり、ビューIDでのみ取得できる。
 *
 * [+:path+]
 *   ビューを保存するパス。パスを指定すると永続ビューとな
 *   り、プロセス終了後もレコードは保持される。次回起動時に
 *   Groonga::View.openで保存されたビューを利用することが
 *   できる。省略すると一時ビューになり、プロセスが終了する
 *   とビューは破棄される。
 *
 * [+:persistent+]
 *   +true+を指定すると永続ビューとなる。+path+を省略した
 *   場合は自動的にパスが付加される。+:context+で指定した
 *   Groonga::Contextに結びついているデータベースが一時デー
 *   タベースの場合は例外が発生する。
 *
 * 使用例:
 *
 * 無名一時ビューを生成する。
 *   Groonga::View.create
 *
 * 無名永続ブーを生成する。
 *   Groonga::View.create(:path => "/tmp/view.grn")
 *
 * 名前付き永続ビューを生成する。ただし、ファイル名は気に
 * しない。
 *   Groonga::View.create(:name => "Entries",
 *                        :persistent => true)
 *
 * +Users+テーブルと+Dogs+テーブルを横断検索するための
 * るビューを生成する。
 *   entries = Groonga::View.create(:name => "Entries")
 *   entries.add_table("Users")
 *   entries.add_table("Dogs")
 */
static VALUE
rb_grn_view_s_create (int argc, VALUE *argv, VALUE klass)
{
    grn_ctx *context;
    grn_obj *table;
    const char *name = NULL, *path = NULL;
    unsigned name_size = 0;
    grn_obj_flags flags = GRN_TABLE_VIEW;
    VALUE rb_table;
    VALUE options, rb_context, rb_name, rb_path, rb_persistent;

    rb_scan_args(argc, argv, "01", &options);

    rb_grn_scan_options(options,
                        "context", &rb_context,
                        "name", &rb_name,
                        "path", &rb_path,
                        "persistent", &rb_persistent,
                        NULL);

    context = rb_grn_context_ensure(&rb_context);

    if (!NIL_P(rb_name)) {
        name = StringValuePtr(rb_name);
        name_size = RSTRING_LEN(rb_name);
        flags |= GRN_OBJ_PERSISTENT;
    }

    if (!NIL_P(rb_path)) {
        path = StringValueCStr(rb_path);
        flags |= GRN_OBJ_PERSISTENT;
    }

    if (RVAL2CBOOL(rb_persistent))
        flags |= GRN_OBJ_PERSISTENT;

    table = grn_table_create(context, name, name_size, path, flags, NULL, NULL);
    if (!table)
        rb_grn_context_check(context, rb_ary_new4(argc, argv));
    rb_table = GRNOBJECT2RVAL(klass, context, table, RB_GRN_TRUE);

    if (rb_block_given_p())
        return rb_ensure(rb_yield, rb_table, rb_grn_object_close, rb_table);
    else
        return rb_table;
}
示例#14
0
/*
 * Returns the normalizer that is used by {Groonga::IndexColumn}.
 *
 * @overload normalizer
 *   @return [nil, Groonga::Procedure]
 */
static VALUE
rb_grn_table_key_support_get_normalizer (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_obj *normalizer = NULL;

    rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
                                         NULL, NULL, NULL,
                                         NULL, NULL, NULL,
                                         NULL);

    normalizer = grn_obj_get_info(context, table, GRN_INFO_NORMALIZER, NULL);
    rb_grn_context_check(context, self);

    return GRNOBJECT2RVAL(Qnil, context, normalizer, GRN_FALSE);
}
示例#15
0
/*
 * 新しくデータベースを作成する。
 * _options_ にはハッシュでオプションを指定する。
 *
 * @example
 *   # 一時データベースを作成:
 *   Groonga::Database.create
 *
 *   # 永続データベースを作成:
 *   Groonga::Database.create(:path => "/tmp/db.groonga")
 *
 * @overload create(options=nil)
 *   @return [Groonga::Database] 作成されたデータベースを返す。
 *   @param [::Hash] options The name and value
 *     pairs. Omitted names are initialized as the default value.
 *   @option options :path
 *     データベースを保存するパス。省略すると一時データベース
 *     となる。
 *   @option options :context (Groonga::Context.default)
 *     データベースを結びつけるコンテキスト。省略すると
 *     {Groonga::Context.default} を利用する。
 */
static VALUE
rb_grn_database_s_create (int argc, VALUE *argv, VALUE klass)
{
    grn_ctx *context;
    grn_obj *old_database, *database;
    grn_db_create_optarg create_args;
    const char *path = NULL;
    VALUE rb_database;
    VALUE rb_path, options, rb_context, builtin_type_names;
    grn_bool owner;

    rb_scan_args(argc, argv, "01", &options);

    rb_grn_scan_options(options,
                        "path", &rb_path,
                        "context", &rb_context,
                        "builtin_type_names", &builtin_type_names,
                        NULL);

    if (!NIL_P(rb_path))
        path = StringValuePtr(rb_path);
    context = rb_grn_context_ensure(&rb_context);

    create_args.builtin_type_names = NULL;
    create_args.n_builtin_type_names = 0;

    old_database = grn_ctx_db(context);
    if (old_database)
        grn_obj_unlink(context, old_database);
    reset_floating_objects(rb_context);
    database = grn_db_create(context, path, &create_args);
    rb_grn_context_check(context, rb_ary_new_from_values(argc, argv));
    owner = (context->flags & GRN_CTX_PER_DB) ? GRN_FALSE : GRN_TRUE;
    rb_database = GRNOBJECT2RVAL(klass, context, database, owner);
    rb_iv_set(rb_database, "@context", rb_context);
    if (!NIL_P(rb_context))
        rb_iv_set(rb_context, "database", rb_database);
    rb_grn_context_check(context, rb_ary_new_from_values(argc, argv));

    if (rb_block_given_p())
        return rb_ensure(rb_yield, rb_database,
                         rb_grn_database_close, rb_database);
    else
        return rb_database;
}
示例#16
0
/*
 * {Groonga::IndexColumn} で使用するトークナイザを返す。
 *
 * @overload default_tokenizer
 *   @return [nilまたはGroonga::Procedure]
 */
static VALUE
rb_grn_table_key_support_get_default_tokenizer (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_obj *tokenizer = NULL;

    rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
                                         NULL, NULL, NULL,
                                         NULL, NULL, NULL,
                                         NULL);

    tokenizer = grn_obj_get_info(context, table, GRN_INFO_DEFAULT_TOKENIZER,
                                 NULL);
    rb_grn_context_check(context, self);

    return GRNOBJECT2RVAL(Qnil, context, tokenizer, GRN_FALSE);
}
示例#17
0
/*
 * call-seq:
 *   database.each {|object| ...}
 *   database.each(options=nil) {|object| ...}
 *
 * データベース内のオブジェクトを順番にブロックに渡す。
 *
 * @example すべてのオブジェクトの名前を表示する:
 *   database.each do |object|
 *     p object.name
 *   end
 *
 * @example すべてのオブジェクトの名前をID順で表示する:
 *   database.each(:order_by => :id) do |object|
 *     p object.name
 *   end
 *
 * @example すべてのオブジェクトの名前をキー名の降順で表示する:
 *   database.each(:order_by => :key, :order => :desc) do |object|
 *     p object.name
 *   end
 *
 * @param options [::Hash]
 * @option options :order The order
 *   +:asc+ または +:ascending+ を指定すると昇順にレコードを取
 *   り出す。(デフォルト)
 *
 *   +:desc+ または +:descending+ を指定すると降順にレコードを
 *   取り出す。
 *
 * @option options :order_by (:key) The ordef by
 *   +:id+ を指定するとID順にレコードを取り出す。
 *
 *   +:key+ 指定するとキー順にレコードを取り出す。(デフォル
 *   ト)
 *
 */
static VALUE
rb_grn_database_each (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *database;
    grn_table_cursor *cursor;
    VALUE rb_cursor, rb_options, rb_order, rb_order_by;
    int flags = 0;
    grn_id id;

    rb_grn_database_deconstruct(SELF(self), &database, &context,
				NULL, NULL, NULL, NULL);

    rb_scan_args(argc, argv, "01", &rb_options);

    rb_grn_scan_options(rb_options,
			"order", &rb_order,
			"order_by", &rb_order_by,
			NULL);

    flags |= rb_grn_table_cursor_order_to_flag(rb_order);
    flags |= rb_grn_table_cursor_order_by_to_flag(GRN_TABLE_PAT_KEY,
						  self,
						  rb_order_by);

    cursor = grn_table_cursor_open(context, database, NULL, 0, NULL, 0,
				   0, -1,
				   flags);
    rb_cursor = GRNTABLECURSOR2RVAL(Qnil, context, cursor);
    rb_iv_set(self, "cursor", rb_cursor);
    while ((id = grn_table_cursor_next(context, cursor)) != GRN_ID_NIL) {
	grn_obj *object;

	object = grn_ctx_at(context, id);
	if (object)
	    rb_yield(GRNOBJECT2RVAL(Qnil, context, object, GRN_FALSE));
    }
    rb_grn_object_close(rb_cursor);
    rb_iv_set(self, "cursor", Qnil);

    return Qnil;
}
示例#18
0
VALUE
rb_grn_value_to_ruby_object (grn_ctx *context,
                             grn_obj *value,
                             grn_obj *range,
                             VALUE related_object)
{
    if (!value)
        return Qnil;

    switch (value->header.type) {
    case GRN_VOID:
        return Qnil;
        break;
    case GRN_BULK:
        if (GRN_BULK_EMPTYP(value))
            return Qnil;
        if (value->header.domain == GRN_ID_NIL && range)
            value->header.domain = grn_obj_id(context, range);
        return GRNBULK2RVAL(context, value, range, related_object);
        break;
    case GRN_UVECTOR:
        return GRNUVECTOR2RVAL(context, value, range, related_object);
        break;
    case GRN_VECTOR:
        return GRNVECTOR2RVAL(context, value);
        break;
    default:
        rb_raise(rb_eGrnError,
                 "unsupported value type: %s(%#x): %s",
                 rb_grn_inspect_type(value->header.type),
                 value->header.type,
                 rb_grn_inspect(related_object));
        break;
    }

    if (!range)
        return GRNOBJECT2RVAL(Qnil, context, value, GRN_FALSE);

    return Qnil;
}
示例#19
0
/*
 * _key_ にマッチするレコードのIDがキーに入っている
 * {Groonga::Hash} を返す。マッチするレコードがない場合は空の
 * {Groonga::Hash} が返る。
 *
 * _options_ で +:result+ を指定することにより、そのテーブルにマッ
 * チしたレコードIDがキーのレコードを追加することができる。
 * +:result+ にテーブルを指定した場合は、そのテーブルが返る。
 *
 * @example 複数のキーで検索し、結果を1つのテーブルに集める。
 *   result = nil
 *   keys = ["morita", "gunyara-kun", "yu"]
 *   keys.each do |key|
 *     result = users.search(key, :result => result)
 *   end
 *   result.each do |record|
 *     user = record.key
 *     p user.key # -> "morita"または"gunyara-kun"または"yu"
 *   end
 *
 * @overload search(key, options=nil)
 *   @param [::Hash] options The name and value
 *     pairs. Omitted names are initialized as the default value
 *   @option options :result
 *     結果を格納するテーブル。
 * @return [Groonga::Hash]
 */
static VALUE
rb_grn_hash_search (int argc, VALUE *argv, VALUE self)
{
    grn_rc rc;
    grn_ctx *context;
    grn_obj *table;
    grn_id domain_id;
    grn_obj *key, *domain, *result;
    VALUE rb_key, options, rb_result;

    rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
                                         &key, &domain_id, &domain,
                                         NULL, NULL, NULL,
                                         NULL);

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

    RVAL2GRNKEY(rb_key, context, key, domain_id, domain, self);

    rb_grn_scan_options(options,
                        "result", &rb_result,
                        NULL);

    if (NIL_P(rb_result)) {
        result = grn_table_create(context, NULL, 0, NULL,
                                  GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
                                  table, 0);
        rb_grn_context_check(context, self);
        rb_result = GRNOBJECT2RVAL(Qnil, context, result, GRN_TRUE);
    } else {
        result = RVAL2GRNOBJECT(rb_result, &context);
    }

    rc = grn_obj_search(context, table, key,
                        result, GRN_OP_OR, NULL);
    rb_grn_rc_check(rc, self);

    return rb_result;
}
示例#20
0
/*
 * Document-method: indexes
 *
 * call-seq:
 *   column.indexes(operator=Groonga::Operator::MATCH) -> [index_column, ...]
 *
 * _operation_ を実行できる _column_ のインデックスを返す。
 *
 * @since 1.0.9
 */
static VALUE
rb_grn_column_get_indexes (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *column;
    grn_obj **indexes = NULL;
    int i, n_indexes;
    grn_operator operator = GRN_OP_MATCH;
    VALUE rb_operator, rb_indexes;

    rb_scan_args(argc, argv, "01", &rb_operator);

    rb_grn_column_deconstruct(SELF(self), &column, &context,
			     NULL, NULL,
			     NULL, NULL, NULL);

    if (!NIL_P(rb_operator)) {
	operator = RVAL2GRNOPERATOR(rb_operator);
    }

    rb_indexes = rb_ary_new();
    n_indexes = grn_column_index(context, column, operator,
				 NULL, 0, NULL);
    if (n_indexes == 0)
	return rb_indexes;

    indexes = xmalloc(sizeof(grn_obj *) * n_indexes);
    n_indexes = grn_column_index(context, column, operator,
				 indexes, n_indexes, NULL);
    for (i = 0; i < n_indexes; i++) {
	VALUE rb_index;
	rb_index = GRNOBJECT2RVAL(Qnil, context, indexes[i], GRN_FALSE);
	rb_ary_push(rb_indexes, rb_index);
	grn_obj_unlink(context, indexes[i]);
    }
    xfree(indexes);
    return rb_indexes;
}
示例#21
0
/*
 * call-seq:
 *   context[name] -> Groonga::Object or nil
 *   context[id]   -> Groonga::Object or nil
 *
 * コンテキスト管理下にあるオブジェクトを返す。
 *
 * _name_として文字列を指定した場合はオブジェクト名でオブジェ
 * クトを検索する。
 *
 * _id_として数値を指定した場合はオブジェクトIDでオブジェク
 * トを検索する。
 */
static VALUE
rb_grn_context_array_reference (VALUE self, VALUE name_or_id)
{
    grn_ctx *context;
    grn_obj *object;
    const char *name;
    unsigned int name_size;
    grn_id id;

    context = SELF(self);
    switch (TYPE(name_or_id)) {
      case T_SYMBOL:
	name = rb_id2name(SYM2ID(name_or_id));
	name_size = strlen(name);
	object = rb_grn_context_get_backward_compatibility(context,
							   name, name_size);
	break;
      case T_STRING:
	name = StringValuePtr(name_or_id);
	name_size = RSTRING_LEN(name_or_id);
	object = rb_grn_context_get_backward_compatibility(context,
							   name, name_size);
	break;
      case T_FIXNUM:
	id = NUM2UINT(name_or_id);
	object = grn_ctx_at(context, id);
	break;
      default:
	rb_raise(rb_eArgError,
		 "should be String, Symbol or unsigned integer: %s",
		 rb_grn_inspect(name_or_id));
	break;
    }

    return GRNOBJECT2RVAL(Qnil, context, object, RB_GRN_FALSE);
}
/*
 * It creates a table that manages records by double array trie.
 * ブロックを指定すると、そのブロックに生成したテーブルが渡さ
 * れ、ブロックを抜けると自動的にテーブルが破棄される。
 *
 * @example
 *   #無名一時テーブルを生成する。
 *   Groonga::DoubleArrayTrie.create
 *
 *   #無名永続テーブルを生成する。
 *   Groonga::DoubleArrayTrie.create(:path => "/tmp/hash.grn")
 *
 *   #名前付き永続テーブルを生成する。ただし、ファイル名は気に
 *   #しない。
 *   Groonga::DoubleArrayTrie.create(:name => "Bookmarks",
 *                                   :persistent => true)
 *
 *   #それぞれのレコードに512バイトの値を格納できる無名一時テー
 *   #ブルを生成する。
 *   Groonga::DoubleArrayTrie.create(:value => 512)
 *
 *   #キーとして文字列を使用する無名一時テーブルを生成する。
 *   Groonga::DoubleArrayTrie.create(:key_type => Groonga::Type::SHORT_TEXT)
 *
 *   #キーとして文字列を使用する無名一時テーブルを生成する。
 *   #(キーの種類を表すオブジェクトは文字列で指定。)
 *   Groonga::DoubleArrayTrie.create(:key_type => "ShortText")
 *
 *   #キーとしてBookmarksテーブルのレコードを使用す
 *   #る無名一時テーブルを生成する。
 *   bookmarks = Groonga::DoubleArrayTrie.create(:name => "Bookmarks")
 *   Groonga::DoubleArrayTrie.create(:key_type => bookmarks)
 *
 *   #キーとしてBookmarksテーブルのレコードを使用す
 *   #る無名一時テーブルを生成する。
 *   #(テーブルは文字列で指定。)
 *   Groonga::DoubleArrayTrie.create(:name => "Bookmarks")
 *   Groonga::DoubleArrayTrie.create(:key_type => "Bookmarks")
 *
 *   #全文検索用のトークンをバイグラムで切り出す無名一時テーブ
 *   #ルを生成する。
 *   bookmarks = Groonga::DoubleArrayTrie.create(:name => "Bookmarks")
 *   bookmarks.define_column("comment", "Text")
 *   terms = Groonga::DoubleArrayTrie.create(:name => "Terms",
 *                                           :default_tokenizer => "TokenBigram")
 *   terms.define_index_column("content", bookmarks,
 *                             :source => "Bookmarks.comment")
 *
 * @overload create(options={})
 *   @return [Groonga::DoubleArrayTrie]
 *   @!macro [new] double-array-trie.create.options
 *     @param [::Hash] options The name and value
 *       pairs. Omitted names are initialized as the default value.
 *     @option options [Groonga::Context] :context (Groonga::Context.default)
 *       テーブルが利用する {Groonga::Context} 。
 *     @option options :name
 *       テーブルの名前。名前をつけると、 {Groonga::Context#[]} に名
 *       前を指定してテーブルを取得することができる。省略すると
 *       無名テーブルになり、テーブルIDでのみ取得できる。
 *     @option options :path
 *       テーブルを保存するパス。パスを指定すると永続テーブルとな
 *       り、プロセス終了後もレコードは保持される。次回起動時に
 *       {Groonga::Context#[]} で保存されたレコードを利用する
 *       ことができる。省略すると一時テーブルになり、プロセスが終
 *       了するとレコードは破棄される。
 *     @option options :persistent
 *       +true+ を指定すると永続テーブルとなる。 +path+ を省略した
 *       場合は自動的にパスが付加される。 +:context+ で指定した
 *       {Groonga::Context} に結びついているデータベースが一時デー
 *       タベースの場合は例外が発生する。
 *
 *     @option options :key_normalize (false) Keys are normalized
 *       if this value is @true@.
 *
 *       @deprecated Use @:normalizer => "NormalizerAuto"@ instead.
 *
 *     @option options :key_with_sis
 *       +true+ を指定するとキーの文字列の全suffixが自動的に登
 *       録される。
 *     @option options :key_type
 *       キーの種類を示すオブジェクトを指定する。キーの種類には型
 *       名("Int32"や"ShortText"など)または {Groonga::Type} または
 *       テーブル( {Groonga::Array} 、{Groonga::Hash} 、
 *       {Groonga::DoubleArrayTrie} のどれか)を指定する。
 *       {Groonga::Type} を指定した場合は、その型が示す範囲の値をキー
 *       として使用する。ただし、キーの最大サイズは4096バイトで
 *       あるため、 {Groonga::Type::TEXT} や {Groonga::Type::LONG_TEXT}
 *       は使用できない。
 *
 *       テーブルを指定した場合はレコードIDをキーとして使用する。
 *       指定したテーブルの {Groonga::Record} をキーとして使用するこ
 *       ともでき、その場合は自動的に {Groonga::Record} からレコード
 *       IDを取得する。
 *
 *       省略した場合はShortText型をキーとして使用する。この場合、
 *       4096バイトまで使用可能である。
 *     @option options :value_type
 *       値の型を指定する。省略すると値のための領域を確保しない。
 *       値を保存したい場合は必ず指定すること。
 *
 *       参考: {Groonga::Type.new}
 *
 *     @option options :default_tokenizer
 *       {Groonga::IndexColumn} で使用するトークナイザを指定する。
 *       デフォルトでは何も設定されていないので、テーブルに
 *       {Groonga::IndexColumn} を定義する場合は
 *       @"TokenBigram"@ などを指定する必要がある。
 *
 *     @option options [::Array<String, Groonga::Procedure>, nil]
 *       :token_filters (nil) The token filters to be used in the
 *       table.
 *
 *     @option options :sub_records
 *       +true+ を指定すると {#group} でグループ化したときに、
 *       {Groonga::Record#n_sub_records} でグループに含まれるレコー
 *       ドの件数を取得できる。
 *
 *     @option options [String, Groonga::Procedure, nil] :normalizer
 *       The normalizer that is used by {Groonga::IndexColumn}. You
 *       can specify this by normalizer name as String such as
 *       @"NormalizerAuto"@ or normalizer object.
 *
 *   @!macro double-array-trie.create.options
 * @overload create(options={})
 *   @yield [table]
 *   @!macro double-array-trie.create.options
 */
static VALUE
rb_grn_double_array_trie_s_create (int argc, VALUE *argv, VALUE klass)
{
    grn_ctx *context;
    grn_obj *key_type = NULL, *value_type = NULL, *table;
    const char *name = NULL, *path = NULL;
    unsigned name_size = 0;
    grn_obj_flags flags = GRN_OBJ_TABLE_DAT_KEY;
    VALUE rb_table;
    VALUE options, rb_context, rb_name, rb_path, rb_persistent;
    VALUE rb_key_normalize, rb_key_with_sis, rb_key_type;
    VALUE rb_value_type;
    VALUE rb_default_tokenizer;
    VALUE rb_token_filters;
    VALUE rb_sub_records;
    VALUE rb_normalizer;

    rb_scan_args(argc, argv, "01", &options);

    rb_grn_scan_options(options,
                        "context", &rb_context,
                        "name", &rb_name,
                        "path", &rb_path,
                        "persistent", &rb_persistent,
                        "key_normalize", &rb_key_normalize,
                        "key_with_sis", &rb_key_with_sis,
                        "key_type", &rb_key_type,
                        "value_type", &rb_value_type,
                        "default_tokenizer", &rb_default_tokenizer,
                        "token_filters", &rb_token_filters,
                        "sub_records", &rb_sub_records,
                        "normalizer", &rb_normalizer,
                        NULL);

    context = rb_grn_context_ensure(&rb_context);

    if (!NIL_P(rb_name)) {
        name = StringValuePtr(rb_name);
        name_size = RSTRING_LEN(rb_name);
        flags |= GRN_OBJ_PERSISTENT;
    }

    if (!NIL_P(rb_path)) {
        path = StringValueCStr(rb_path);
        flags |= GRN_OBJ_PERSISTENT;
    }

    if (RVAL2CBOOL(rb_persistent))
        flags |= GRN_OBJ_PERSISTENT;

    if (RVAL2CBOOL(rb_key_normalize))
        flags |= GRN_OBJ_KEY_NORMALIZE;

    if (RVAL2CBOOL(rb_key_with_sis))
        flags |= GRN_OBJ_KEY_WITH_SIS;

    if (NIL_P(rb_key_type)) {
        key_type = grn_ctx_at(context, GRN_DB_SHORT_TEXT);
    } else {
        key_type = RVAL2GRNOBJECT(rb_key_type, &context);
    }

    if (!NIL_P(rb_value_type))
        value_type = RVAL2GRNOBJECT(rb_value_type, &context);

    if (RVAL2CBOOL(rb_sub_records))
        flags |= GRN_OBJ_WITH_SUBREC;

    table = grn_table_create(context, name, name_size, path,
                             flags, key_type, value_type);
    if (!table)
        rb_grn_context_check(context, rb_ary_new4(argc, argv));
    rb_table = GRNOBJECT2RVAL(klass, context, table, GRN_TRUE);

    if (!NIL_P(rb_default_tokenizer))
        rb_funcall(rb_table, rb_intern("default_tokenizer="), 1,
                   rb_default_tokenizer);
    if (!NIL_P(rb_token_filters))
        rb_funcall(rb_table, rb_intern("token_filters="), 1,
                   rb_token_filters);
    if (!NIL_P(rb_normalizer))
        rb_funcall(rb_table, rb_intern("normalizer="), 1,
                   rb_normalizer);

    if (rb_block_given_p())
        return rb_ensure(rb_yield, rb_table, rb_grn_object_close, rb_table);
    else
        return rb_table;
}
示例#23
0
VALUE
rb_grn_database_to_ruby_object (grn_ctx *context, grn_obj *database,
                                grn_bool owner)
{
    return GRNOBJECT2RVAL(rb_cGrnDatabase, context, database, owner);
}
示例#24
0
/*
 * Opens cursor to iterate posting in the index column.
 *
 * @example
 *   # TODO
 *
 * @overload open_cursor(table_cursor, options={})
 *   @param [TableCursor] The table cursor for table of the index column.
 *   @param [::Hash] options
 *   @option options [Boolean] :with_section (nil)
 *      Includes section info the posting. It is enabled by default if
 *      the index column is created with @:with_section@ flag.
 *   @option options [Boolean] :with_weight (nil)
 *      Includes weight info the posting. It is enabled by default if
 *      the index column is created with @:with_weight@ flag.
 *   @option options [Boolean] :with_position (nil)
 *      Includes position info the posting. It is enabled by default if
 *      the index column is created with @:with_position@ flag.
 */
static VALUE
rb_grn_index_column_open_cursor (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *column;
    grn_obj *range_object;
    grn_table_cursor *table_cursor;
    grn_id rid_min = GRN_ID_NIL;
    grn_id rid_max = GRN_ID_MAX;
    int flags = 0;
    grn_obj *index_cursor;
    VALUE rb_table_cursor;
    VALUE options;
    VALUE rb_with_section, rb_with_weight, rb_with_position;
    VALUE rb_table;
    VALUE rb_lexicon;
    VALUE rb_cursor;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL,
                                    NULL, &range_object,
                                    NULL, NULL);

    rb_scan_args(argc, argv, "11", &rb_table_cursor, &options);
    rb_grn_scan_options(options,
                        "with_section", &rb_with_section,
                        "with_weight", &rb_with_weight,
                        "with_position", &rb_with_position,
                        NULL);

    table_cursor = RVAL2GRNTABLECURSOR(rb_table_cursor, NULL);
    rb_table = GRNOBJECT2RVAL(Qnil, context, range_object, GRN_FALSE);
    rb_lexicon = rb_iv_get(rb_table_cursor, "@table");

    if (NIL_P(rb_with_section)) {
        flags |= column->header.flags & GRN_OBJ_WITH_SECTION;
    } else if (RVAL2CBOOL(rb_with_section)) {
        flags |= GRN_OBJ_WITH_SECTION;
    }

    if (NIL_P(rb_with_weight)) {
        flags |= column->header.flags & GRN_OBJ_WITH_WEIGHT;
    } else if (RVAL2CBOOL(rb_with_weight)) {
        flags |= GRN_OBJ_WITH_WEIGHT;
    }

    if (NIL_P(rb_with_position)) {
        flags |= column->header.flags & GRN_OBJ_WITH_POSITION;
    } else if (RVAL2CBOOL(rb_with_position)) {
        flags |= GRN_OBJ_WITH_POSITION;
    }

    index_cursor = grn_index_cursor_open(context, table_cursor,
                                         column, rid_min, rid_max, flags);

    rb_cursor = GRNINDEXCURSOR2RVAL(context, index_cursor, rb_table, rb_lexicon);

    if (rb_block_given_p())
        return rb_ensure(rb_yield, rb_cursor, rb_grn_object_close, rb_cursor);
    else
        return rb_cursor;
}
示例#25
0
grn_obj *
rb_grn_bulk_from_ruby_object_with_type (VALUE object, grn_ctx *context,
					grn_obj *bulk,
					grn_id type_id, grn_obj *type)
{
    const char *string;
    unsigned int size;
    int32_t int32_value;
    uint32_t uint32_value;
    int64_t int64_value;
    uint64_t uint64_value;
    int64_t time_value;
    double double_value;
    grn_id record_id, range;
    VALUE rb_type_object;
    grn_obj_flags flags = 0;
    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_VIEW);
    if (string_p && !table_type_p) {
	return RVAL2GRNBULK(object, context, bulk);
    }

    switch (type_id) {
      case GRN_DB_INT32:
	int32_value = NUM2INT(object);
	string = (const char *)&int32_value;
	size = sizeof(int32_value);
	break;
      case GRN_DB_UINT32:
	uint32_value = NUM2UINT(object);
	string = (const char *)&uint32_value;
	size = sizeof(uint32_value);
	break;
      case GRN_DB_INT64:
	int64_value = NUM2LL(object);
	string = (const char *)&int64_value;
	size = sizeof(int64_value);
	break;
      case GRN_DB_UINT64:
	uint64_value = NUM2ULL(object);
	string = (const char *)&uint64_value;
	size = sizeof(uint64_value);
	break;
      case GRN_DB_FLOAT:
	double_value = NUM2DBL(object);
	string = (const char *)&double_value;
	size = sizeof(double_value);
	break;
      case GRN_DB_TIME:
	{
	    VALUE rb_sec, rb_usec;
            int64_t sec;
            int32_t usec;

            switch (TYPE(object)) {
            case T_FIXNUM:
            case T_BIGNUM:
                sec = NUM2LL(object);
                usec = 0;
                break;
            case T_FLOAT:
                rb_sec = rb_funcall(object, rb_intern("to_i"), 0);
                rb_usec = rb_funcall(object, rb_intern("remainder"), 1,
				     INT2NUM(1));

                sec = NUM2LL(rb_sec);
                usec = (int32_t)(NUM2DBL(rb_usec) * 1000000);
                break;
	    case T_NIL:
	        sec = 0;
	        usec = 0;
	        break;
            default:
                sec = NUM2LL(rb_funcall(object, rb_intern("to_i"), 0));
                usec = NUM2INT(rb_funcall(object, rb_intern("usec"), 0));
                break;
            }

	    time_value = GRN_TIME_PACK(sec, usec);
	}
	string = (const char *)&time_value;
	size = sizeof(time_value);
	break;
      case GRN_DB_SHORT_TEXT:
      case GRN_DB_TEXT:
      case GRN_DB_LONG_TEXT:
	string = StringValuePtr(object);
	size = RSTRING_LEN(object);
	range = grn_obj_get_range(context, type);
	if (size > range)
	    rb_raise(rb_eArgError,
		     "string is too large: expected: %u <= %u",
		     size, range);
	flags |= GRN_OBJ_DO_SHALLOW_COPY;
	break;
      case GRN_DB_VOID:
      case GRN_DB_DELIMIT:
      case GRN_DB_UNIGRAM:
      case GRN_DB_BIGRAM:
      case GRN_DB_TRIGRAM:
      case GRN_DB_MECAB:
	rb_type_object = GRNOBJECT2RVAL(Qnil, context, type, GRN_FALSE);
	rb_raise(rb_eArgError,
		 "unbulkable type: %s",
		 rb_grn_inspect(rb_type_object));
	break;
      default:
	if (table_type_p &&
	    (NIL_P(object) || (string_p && RSTRING_LEN(object) == 0))) {
	    record_id = GRN_ID_NIL;
	    string = (const char *)&record_id;
	    size = sizeof(record_id);
	    if (bulk && bulk->header.domain != type_id) {
		grn_obj_reinit(context, bulk, type_id, 0);
	    }
	} else {
	    return RVAL2GRNBULK(object, context, bulk);
	}
	break;
    }

    if (!bulk) {
	bulk = grn_obj_open(context, GRN_BULK, flags, GRN_ID_NIL);
	rb_grn_context_check(context, object);
    }
    GRN_TEXT_SET(context, bulk, string, size);

    return bulk;
}
示例#26
0
VALUE
rb_grn_column_to_ruby_object (VALUE klass, grn_ctx *context, grn_obj *column,
			      rb_grn_boolean owner)
{
    return GRNOBJECT2RVAL(klass, context, column, owner);
}
示例#27
0
VALUE
rb_grn_procedure_to_ruby_object (grn_ctx *context, grn_obj *procedure,
				 grn_bool owner)
{
    return GRNOBJECT2RVAL(rb_cGrnProcedure, context, procedure, owner);
}
示例#28
0
static VALUE
rb_grn_bulk_to_ruby_object_by_range_type (grn_ctx *context, grn_obj *bulk,
                                          grn_obj *range, grn_id range_id,
                                          VALUE related_object, VALUE *rb_value)
{
    grn_bool success = GRN_TRUE;

    if (!range && range_id != GRN_ID_NIL) {
        range = grn_ctx_at(context, range_id);
    }

    if (!range)
        return GRN_FALSE;

    switch (range->header.type) {
    case GRN_TABLE_HASH_KEY:
    case GRN_TABLE_PAT_KEY:
    case GRN_TABLE_DAT_KEY:
    case GRN_TABLE_NO_KEY: {
        grn_id id;

        id = *((grn_id *)GRN_BULK_HEAD(bulk));
        if (id == GRN_ID_NIL) {
            *rb_value = Qnil;
        } else {
            VALUE rb_range;

            rb_range = GRNOBJECT2RVAL(Qnil, context, range, GRN_FALSE);
            *rb_value = rb_grn_record_new(rb_range, id, Qnil);
        }
        break;
    }
    case GRN_TYPE:
        if (range->header.flags & GRN_OBJ_KEY_VAR_SIZE) {
            *rb_value = rb_grn_context_rb_string_new(context,
                                                     GRN_BULK_HEAD(bulk),
                                                     GRN_BULK_VSIZE(bulk));
        } else {
            switch (range->header.flags & GRN_OBJ_KEY_MASK) {
            case GRN_OBJ_KEY_UINT:
                *rb_value = INT2NUM(GRN_UINT32_VALUE(bulk));
                break;
            case GRN_OBJ_KEY_INT:
                *rb_value = INT2NUM(GRN_INT32_VALUE(bulk));
                break;
            case GRN_OBJ_KEY_FLOAT:
                *rb_value = rb_float_new(GRN_FLOAT_VALUE(bulk));
                break;
            default:
                success = GRN_FALSE;
            }
            break;
        }
        break;
    default:
        success = GRN_FALSE;
        break;
    }

    return success;
}
示例#29
0
/*
 * _expression_ から {Groonga::Snippet} を生成する。 _tags_ には
 * キーワードの前後に挿入するタグの配列を以下のような形式で指定
 * する。
 *
 * <pre>
 * !!!ruby
 *   [
 *    ["キーワード前に挿入する文字列1", "キーワード後に挿入する文字列1"],
 *    ["キーワード前に挿入する文字列2", "キーワード後に挿入する文字列2"],
 *    # ...,
 *   ]
 * </pre>
 *
 * もし、1つのスニペットの中に _tags_ で指定したタグより多くの
 * キーワードが含まれている場合は、以下のように、また、先頭
 * のタグから順番に使われる。
 *
 * <pre>
 * !!!ruby
 *   expression.parse("Ruby groonga 検索")
 *   tags = [["<tag1>", "</tag1>"], ["<tag2>", "</tag2>"]]
 *   snippet = expression.snippet(tags)
 *   p snippet.execute("Rubyでgroonga使って全文検索、高速検索。")
 *      # => ["<tag1>Ruby</tag1>で<tag2>groonga</tag2>"
 *      # =>  "使って全文<tag1>検索</tag1>、高速<tag2>検索</tag2>。"]
 * </pre>
 *
 * @overload snippet(tags, options)
 *   @param tags [Array<string>] キーワードの前後に挿入するタグの配列
 *     (詳細は上記を参照)
 *   @param options [::Hash] The name and value
 *     pairs. Omitted names are initialized as the default value.
 *   @option options :normalize (false)
 *     キーワード文字列・スニペット元の文字列を正規化するかど
 *     うか。省略した場合は +false+ で正規化しない。
 *   @option options :skip_leading_spaces (false)
 *     先頭の空白を無視するかどうか。省略した場合は +false+ で無
 *     視しない。
 *   @option options :width (100)
 *     スニペット文字列の長さ。省略した場合は100文字。
 *   @option options :max_results (3)
 *     生成するスニペットの最大数。省略した場合は3。
 *   @option options :html_escape (false)
 *     スニペット内の +<+, +>+, +&+, +"+ をHTMLエスケープするか
 *     どうか。省略した場合は +false+ で、HTMLエスケープしない。
 * @return [Groonga::Snippet]
 */
static VALUE
rb_grn_expression_snippet (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *expression;
    grn_obj *snippet;
    VALUE options;
    VALUE rb_normalize, rb_skip_leading_spaces;
    VALUE rb_width, rb_max_results, rb_tags;
    VALUE rb_html_escape;
    VALUE *rb_tag_values;
    VALUE related_object;
    unsigned int i;
    int flags = GRN_SNIP_COPY_TAG;
    unsigned int width = 100;
    unsigned int max_results = 3;
    unsigned int n_tags = 0;
    char **open_tags = NULL;
    unsigned int *open_tag_lengths = NULL;
    char **close_tags = NULL;
    unsigned int *close_tag_lengths = NULL;
    grn_snip_mapping *mapping = NULL;

    rb_grn_expression_deconstruct(SELF(self), &expression, &context,
                                  NULL, NULL,
                                  NULL, NULL, NULL);

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

    rb_grn_scan_options(options,
                        "normalize", &rb_normalize,
                        "skip_leading_spaces", &rb_skip_leading_spaces,
                        "width", &rb_width,
                        "max_results", &rb_max_results,
                        "html_escape", &rb_html_escape,
                        NULL);

    if (TYPE(rb_tags) != T_ARRAY) {
        rb_raise(rb_eArgError,
                 "tags should be "
                 "[\"open_tag\", \"close_tag\"] or "
                 "[[\"open_tag1\", \"close_tag1\"], ...]: %s",
                 rb_grn_inspect(rb_tags));
    }

    if (TYPE(RARRAY_PTR(rb_tags)[0]) == T_STRING) {
        rb_tags = rb_ary_new_from_args(1, rb_tags);
    }

    rb_tag_values = RARRAY_PTR(rb_tags);
    n_tags = RARRAY_LEN(rb_tags);
    open_tags = ALLOCA_N(char *, n_tags);
    open_tag_lengths = ALLOCA_N(unsigned int, n_tags);
    close_tags = ALLOCA_N(char *, n_tags);
    close_tag_lengths = ALLOCA_N(unsigned int, n_tags);
    for (i = 0; i < n_tags; i++) {
        VALUE *tag_pair;

        if (TYPE(rb_tag_values[i]) != T_ARRAY ||
            RARRAY_LEN(rb_tag_values[i]) != 2) {
            rb_raise(rb_eArgError,
                     "tags should be "
                     "[\"open_tag\", \"close_tag\"] or"
                     "[[\"open_tag1\", \"close_tag1\"], ...]: %s",
                     rb_grn_inspect(rb_tags));
        }
        tag_pair = RARRAY_PTR(rb_tag_values[i]);
        open_tags[i] = StringValuePtr(tag_pair[0]);
        open_tag_lengths[i] = RSTRING_LEN(tag_pair[0]);
        close_tags[i] = StringValuePtr(tag_pair[1]);
        close_tag_lengths[i] = RSTRING_LEN(tag_pair[1]);
    }

    if (RVAL2CBOOL(rb_normalize))
        flags |= GRN_SNIP_NORMALIZE;
    if (RVAL2CBOOL(rb_skip_leading_spaces))
        flags |= GRN_SNIP_SKIP_LEADING_SPACES;

    if (!NIL_P(rb_width))
        width = NUM2UINT(rb_width);

    if (!NIL_P(rb_max_results))
        max_results = NUM2UINT(rb_max_results);

    if (RVAL2CBOOL(rb_html_escape))
        mapping = (grn_snip_mapping *)-1;

    snippet = grn_expr_snip(context, expression, flags, width, max_results,
                            n_tags,
                            (const char **)open_tags, open_tag_lengths,
                            (const char **)close_tags, close_tag_lengths,
                            mapping);
    related_object =
        rb_ary_new_from_args(2, self, rb_ary_new_from_values(argc, argv));
    rb_grn_context_check(context, related_object);

    return GRNOBJECT2RVAL(Qnil, context, snippet, GRN_TRUE);
}
示例#30
0
grn_obj *
rb_grn_bulk_from_ruby_object_with_type (VALUE object, grn_ctx *context,
                                        grn_obj *bulk,
                                        grn_id type_id, grn_obj *type)
{
    const char *string;
    unsigned int size;
    union {
        int8_t int8_value;
        uint8_t uint8_value;
        int16_t int16_value;
        uint16_t uint16_value;
        int32_t int32_value;
        uint32_t uint32_value;
        int64_t int64_value;
        uint64_t uint64_value;
        int64_t time_value;
        double double_value;
        grn_geo_point geo_point_value;
        grn_id record_id;
    } value;
    grn_id range;
    VALUE rb_type_object;
    grn_obj_flags flags = 0;
    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);

    switch (type_id) {
    case GRN_DB_INT8:
        value.int8_value = NUM2SHORT(object);
        string = (const char *)&(value.int8_value);
        size = sizeof(value.int8_value);
        break;
    case GRN_DB_UINT8:
        value.uint8_value = NUM2USHORT(object);
        string = (const char *)&(value.uint8_value);
        size = sizeof(value.uint8_value);
        break;
    case GRN_DB_INT16:
        value.int16_value = NUM2SHORT(object);
        string = (const char *)&(value.int16_value);
        size = sizeof(value.int16_value);
        break;
    case GRN_DB_UINT16:
        value.uint16_value = NUM2USHORT(object);
        string = (const char *)&(value.uint16_value);
        size = sizeof(value.uint16_value);
        break;
    case GRN_DB_INT32:
        value.int32_value = NUM2INT(object);
        string = (const char *)&(value.int32_value);
        size = sizeof(value.int32_value);
        break;
    case GRN_DB_UINT32:
        value.uint32_value = NUM2UINT(object);
        string = (const char *)&(value.uint32_value);
        size = sizeof(value.uint32_value);
        break;
    case GRN_DB_INT64:
        value.int64_value = NUM2LL(object);
        string = (const char *)&(value.int64_value);
        size = sizeof(value.int64_value);
        break;
    case GRN_DB_UINT64:
        value.uint64_value = NUM2ULL(object);
        string = (const char *)&(value.uint64_value);
        size = sizeof(value.uint64_value);
        break;
    case GRN_DB_FLOAT:
        value.double_value = NUM2DBL(object);
        string = (const char *)&(value.double_value);
        size = sizeof(value.double_value);
        break;
    case GRN_DB_TIME: {
        VALUE rb_sec, rb_usec;
        int64_t sec;
        int32_t usec;

        if (string_p) {
            ID id_parse;
            CONST_ID(id_parse, "parse");
            object = rb_funcall(rb_cTime, id_parse, 1, object);
        }

        switch (TYPE(object)) {
        case T_FIXNUM:
        case T_BIGNUM:
            sec = NUM2LL(object);
            usec = 0;
            break;
        case T_FLOAT:
            rb_sec = rb_funcall(object, rb_intern("to_i"), 0);
            rb_usec = rb_funcall(object, rb_intern("remainder"), 1,
                                 INT2NUM(1));

            sec = NUM2LL(rb_sec);
            usec = (int32_t)(NUM2DBL(rb_usec) * 1000000);
            break;
        case T_NIL:
            sec = 0;
            usec = 0;
            break;
        default:
            sec = NUM2LL(rb_funcall(object, rb_intern("to_i"), 0));
            usec = NUM2INT(rb_funcall(object, rb_intern("usec"), 0));
            break;
        }

        value.time_value = GRN_TIME_PACK(sec, usec);
        string = (const char *)&(value.time_value);
        size = sizeof(value.time_value);
        break;
    }
    case GRN_DB_SHORT_TEXT:
    case GRN_DB_TEXT:
    case GRN_DB_LONG_TEXT:
        string = StringValuePtr(object);
        size = RSTRING_LEN(object);
        range = grn_obj_get_range(context, type);
        if (size > range)
            rb_raise(rb_eArgError,
                     "string is too large: expected: %u <= %u",
                     size, range);
        flags |= GRN_OBJ_DO_SHALLOW_COPY;
        break;
    case GRN_DB_TOKYO_GEO_POINT:
    case GRN_DB_WGS84_GEO_POINT: {
        VALUE rb_geo_point;
        VALUE rb_latitude, rb_longitude;
        if (type_id == GRN_DB_TOKYO_GEO_POINT) {
            rb_geo_point = rb_funcall(rb_cGrnTokyoGeoPoint,
                                      rb_intern("new"), 1, object);
        } else {
            rb_geo_point = rb_funcall(rb_cGrnWGS84GeoPoint,
                                      rb_intern("new"), 1, object);
        }
        rb_geo_point = rb_funcall(rb_geo_point, rb_intern("to_msec"), 0);
        rb_latitude  = rb_funcall(rb_geo_point, rb_intern("latitude"), 0);
        rb_longitude = rb_funcall(rb_geo_point, rb_intern("longitude"), 0);
        value.geo_point_value.latitude = NUM2INT(rb_latitude);
        value.geo_point_value.longitude = NUM2INT(rb_longitude);
        string = (const char *)&(value.geo_point_value);
        size = sizeof(value.geo_point_value);
        break;
    }
    case GRN_DB_VOID:
    case GRN_DB_DELIMIT:
    case GRN_DB_UNIGRAM:
    case GRN_DB_BIGRAM:
    case GRN_DB_TRIGRAM:
    case GRN_DB_MECAB:
        rb_type_object = GRNOBJECT2RVAL(Qnil, context, type, GRN_FALSE);
        rb_raise(rb_eArgError,
                 "unbulkable type: %s",
                 rb_grn_inspect(rb_type_object));
        break;
    default:
        if (table_type_p &&
            (NIL_P(object) || (string_p && RSTRING_LEN(object) == 0))) {
            value.record_id = GRN_ID_NIL;
            string = (const char *)&(value.record_id);
            size = sizeof(value.record_id);
            if (bulk && bulk->header.domain != type_id) {
                grn_obj_reinit(context, bulk, type_id, 0);
            }
        } else {
            return RVAL2GRNBULK(object, context, bulk);
        }
        break;
    }

    if (!bulk) {
        bulk = grn_obj_open(context, GRN_BULK, flags, GRN_ID_NIL);
        rb_grn_context_check(context, object);
    }
    GRN_TEXT_SET(context, bulk, string, size);

    return bulk;
}