Module: Groonga::Table::KeySupport

Includes:
EncodingSupport
Defined in:
ext/rb-grn-table-key-support.c,
ext/rb-grn-table-key-support.c

Overview

主キーを持つテーブルであるGroonga::Hashと Groonga::PatriciaTrieに主キーの機能を提供するモジュール。

Instance Method Summary collapse

Methods included from EncodingSupport

#encoding

Instance Method Details

#[]Object

call-seq:

table[key] -> Groonga::Record

tablekeyに対応するGroonga::Recordを返す。

0.9.0から値ではなくGroonga::Recordを返すようになった。



315
316
317
318
319
320
321
322
323
324
325
326
# File 'ext/rb-grn-table-key-support.c', line 315

static VALUE
rb_grn_table_key_support_array_reference (VALUE self, VALUE rb_key)
{
    grn_id id;

    id = rb_grn_table_key_support_get(self, rb_key);
    if (id == GRN_ID_NIL) {
	return Qnil;
    } else {
	return rb_grn_record_new(self, id, Qnil);
    }
}

#[]=Object

call-seq:

table[key] = {:column_name => value, ...}

tablekeyに対応するカラムcolumn_nameの値を設定する。 keyに対応するレコードがない場合は新しく作成される。

0.9.0から値ではなくカラムの値を設定するようになった。



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'ext/rb-grn-table-key-support.c', line 368

static VALUE
rb_grn_table_key_support_array_set (VALUE self, VALUE rb_key, VALUE rb_values)
{
    grn_id id;
    SetValueData data;
    grn_ctx *context;
    grn_obj *table;

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

    id = rb_grn_table_key_support_add_raw(self, rb_key);

    if (id == GRN_ID_NIL) {
	rb_raise(rb_eGrnError,
		 "failed to add record: %s",
		 rb_grn_inspect(rb_ary_new3(3, self, rb_key, rb_values)));
    }

    data.self = self;
    data.id = id;
    data.table = table;
    data.rb_grn_object.context = context;
    rb_iterate(rb_each, rb_values, set_value, (VALUE)&data);

    return Qnil;
}

#add(key, values = nil) ⇒ Groonga::Recordまたはnil

主キーがkeyのレコード追加し、追加したレコードを返す。レ コードの追加に失敗した場合はnilを返す。

valuesにはレコードのカラムに設定する値を指定する。省略 した場合またはnilを指定した場合はカラムは設定しない。カ ラムの値は{:カラム名1 => 値1, :カラム名2 => 値2, ...}と指定する。

Returns:



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/rb-grn-table-key-support.c', line 138

static VALUE
rb_grn_table_key_support_add (int argc, VALUE *argv, VALUE self)
{
    grn_id id;
    VALUE key, values;

    rb_scan_args(argc, argv, "11", &key, &values);
    id = rb_grn_table_key_support_add_raw(self, key);
    if (GRN_ID_NIL == id)
	return Qnil;
    else
	return rb_grn_record_new(self, id, values);
}

#column_value(key, name) ⇒ Object #column_value(id, name, : id) ⇒ Object

tablekeyに対応するカラムnameの値を設定する。

TODO: keyに対応するレコードがない場合は例外?



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'ext/rb-grn-table-key-support.c', line 447

static VALUE
rb_grn_table_key_support_get_column_value (int argc, VALUE *argv, VALUE self)
{
    grn_id id;
    VALUE rb_key, rb_id_or_key, rb_name, rb_options;

    rb_scan_args(argc, argv, "21", &rb_id_or_key, &rb_name, &rb_options);
    if (!NIL_P(rb_options)) {
	VALUE rb_option_id;
	rb_grn_scan_options(rb_options,
			    "id", &rb_option_id,
			    NULL);
	if (RVAL2CBOOL(rb_option_id)) {
	    VALUE rb_id = rb_id_or_key;
	    return rb_grn_table_get_column_value(self, rb_id, rb_name);
	}
    }

    rb_key = rb_id_or_key;
    id = rb_grn_table_key_support_get(self, rb_key);
    if (id == GRN_ID_NIL) {
	return Qnil;
    }

    return rb_grn_table_get_column_value_raw(self, id, rb_name);
}

#default_tokenizernilまたはGroonga::Procedure

Groonga::IndexColumnで使用するトークナイザを返す。

Returns:



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'ext/rb-grn-table-key-support.c', line 626

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, RB_GRN_FALSE);
}

#default_tokenizer=Object

Groonga::IndexColumnで使用するトークナイザを設定する。

例:

# 2-gramを使用。
table.default_tokenizer = "TokenBigram"
# オブジェクトで指定
table.default_tokenizer = Groonga::Context.default["TokenBigram"]
# オブジェクトIDで指定
table.default_tokenizer = Groonga::Type::BIGRAM
# N-gram用のトークナイザを使うときはGroonga::IndexColumn
# には自動的に:with_section => trueが指定される。
index = table.define_index_column("blog_content", "Blogs",
                                  :source => "content")
p index # -> #<Groonga::IndexColumn ... flags: <WITH_POSITION|...>>

# MeCabを使用
table.default_tokenizer = "TokenMecab"


667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'ext/rb-grn-table-key-support.c', line 667

static VALUE
rb_grn_table_key_support_set_default_tokenizer (VALUE self, VALUE rb_tokenizer)
{
    grn_ctx *context;
    grn_obj *table;
    grn_obj *tokenizer;
    grn_rc rc;

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

    tokenizer = RVAL2GRNOBJECT(rb_tokenizer, &context);
    rc = grn_obj_set_info(context, table,
			  GRN_INFO_DEFAULT_TOKENIZER, tokenizer);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    return Qnil;
}

#delete(id) ⇒ Object #delete(key) ⇒ Object

テーブルのidまたはkeyに対応するレコードを削除する。



295
296
297
298
299
300
301
302
303
# File 'ext/rb-grn-table-key-support.c', line 295

static VALUE
rb_grn_table_key_support_delete (VALUE self, VALUE rb_id_or_key)
{
    if (FIXNUM_P(rb_id_or_key)) {
	return rb_grn_table_delete(self, rb_id_or_key);
    } else {
	return rb_grn_table_key_support_delete_by_key(self, rb_id_or_key);
    }
}

#find(key) ⇒ Groonga::Record

テーブルのkeyに対応するレコードを返す。

0.9.0から非推奨。代わりにtableを使うこと。

Returns:



482
483
484
485
486
487
488
489
# File 'ext/rb-grn-table-key-support.c', line 482

static VALUE
rb_grn_table_key_support_find (VALUE self, VALUE rb_key)
{
    rb_warn("#find is deprecated. Use #[] instead: %s",
	    rb_grn_inspect(self));

    return rb_grn_table_key_support_array_reference(self, rb_key);
}

#has_key?(key) ⇒ Boolean

テーブルに主キーがkeyのレコードがあるならtrueを返す。

Returns:

  • (Boolean)


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'ext/rb-grn-table-key-support.c', line 245

static VALUE
rb_grn_table_key_support_has_key (VALUE self, VALUE rb_key)
{
    grn_ctx *context;
    grn_obj *table, *key, *domain;
    grn_id id, domain_id;

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

    GRN_BULK_REWIND(key);
    RVAL2GRNKEY(rb_key, context, key, domain_id, domain, self);
    id = grn_table_get(context, table, GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key));

    return id == GRN_ID_NIL ? Qfalse : Qtrue;
}

#idObject #id(key) ⇒ Object

keyを指定しない場合はテーブルのIDを返す。

keyを指定した場合はテーブルのkeyに対応するレコードの IDを返す。



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/rb-grn-table-key-support.c', line 183

static VALUE
rb_grn_table_key_support_get_id (int argc, VALUE *argv, VALUE self)
{
    VALUE rb_key;

    rb_scan_args(argc, argv, "01", &rb_key);
    if (NIL_P(rb_key)) {
	return rb_grn_object_get_id(self);
    } else {
	grn_id id;

	id = rb_grn_table_key_support_get(self, rb_key);
	if (id == GRN_ID_NIL) {
	    return Qnil;
	} else {
	    return UINT2NUM(id);
	}
    }
}

#initializeObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/rb-grn-table-key-support.c', line 90

static VALUE
rb_grn_table_key_support_initialize (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    VALUE rb_context;

    table = rb_grn_table_open_raw(argc, argv, &context, &rb_context);
    rb_grn_context_check(context, self);
    rb_grn_object_assign(Qnil, self, rb_context, context, table);

    return Qnil;
}

#key(id) ⇒ Object

テーブルのidに対応する主キーを返す。



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'ext/rb-grn-table-key-support.c', line 209

static VALUE
rb_grn_table_key_support_get_key (VALUE self, VALUE rb_id)
{
    grn_ctx *context;
    grn_obj *table, *key;
    grn_id id;
    int key_size;
    VALUE rb_key;

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

    id = NUM2UINT(rb_id);
    GRN_BULK_REWIND(key);
    key_size = grn_table_get_key(context, table, id,
				 GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key));
    if (key_size == 0)
	return Qnil;

    if (GRN_BULK_VSIZE(key) < key_size) {
	grn_bulk_reserve(context, key, key_size);
	grn_table_get_key(context, table, id, GRN_BULK_HEAD(key), key_size);
    }

    rb_key = GRNKEY2RVAL(context, GRN_BULK_HEAD(key), key_size, table, self);
    return rb_key;
}

#set_column_value(key, name, value) ⇒ Object #set_column_value(id, name, value, : id) ⇒ Object

tablekeyに対応するカラムnameの値を設定する。 keyに対応するレコードがない場合は新しく作成される。



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'ext/rb-grn-table-key-support.c', line 406

static VALUE
rb_grn_table_key_support_set_column_value (int argc, VALUE *argv, VALUE self)
{
    grn_id id;
    VALUE rb_key, rb_id_or_key, rb_name, rb_value, rb_options;

    rb_scan_args(argc, argv, "31",
		 &rb_id_or_key, &rb_name, &rb_value, &rb_options);
    if (!NIL_P(rb_options)) {
	VALUE rb_option_id;
	rb_grn_scan_options(rb_options,
			    "id", &rb_option_id,
			    NULL);
	if (RVAL2CBOOL(rb_option_id)) {
	    VALUE rb_id = rb_id_or_key;
	    return rb_grn_table_set_column_value(self, rb_id, rb_name, rb_value);
	}
    }

    rb_key = rb_id_or_key;
    id = rb_grn_table_key_support_add_raw(self, rb_key);
    if (id == GRN_ID_NIL) {
	rb_raise(rb_eGrnError,
		 "failed to add record: %s",
		 rb_grn_inspect(rb_ary_new3(4,
					    self, rb_key,
					    rb_name, rb_value)));
    }

    return rb_grn_table_set_column_value_raw(self, id, rb_name, rb_value);
}

#set_value(id, value, : id) ⇒ Object #set_value(key, value) ⇒ Object

tableidまたはkeyに対応する値をvalueに設定する。 既存の値は上書きされる。



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'ext/rb-grn-table-key-support.c', line 592

static VALUE
rb_grn_table_key_support_set_value (int argc, VALUE *argv, VALUE self)
{
    VALUE rb_id_or_key, rb_value, rb_options;
    rb_grn_boolean use_key;

    rb_scan_args(argc, argv, "21", &rb_id_or_key, &rb_value, &rb_options);

    if (NIL_P(rb_options)) {
	use_key = RB_GRN_TRUE;
    } else {
	VALUE rb_option_id;

	rb_grn_scan_options(rb_options,
			    "id", &rb_option_id,
			    NULL);
	use_key = !RVAL2CBOOL(rb_option_id);
    }

    if (use_key) {
	return rb_grn_table_key_support_set_value_by_key(self,
							 rb_id_or_key,
							 rb_value);
    } else {
	return rb_grn_table_set_value(self, rb_id_or_key, rb_value);
    }
}

#value(id, : id) ⇒ Object #value(key) ⇒ Object

tableidまたはkeyに対応する値を返す。



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'ext/rb-grn-table-key-support.c', line 521

static VALUE
rb_grn_table_key_support_get_value (int argc, VALUE *argv, VALUE self)
{
    VALUE rb_id_or_key, rb_options;
    rb_grn_boolean use_key;

    rb_scan_args(argc, argv, "11", &rb_id_or_key, &rb_options);

    if (NIL_P(rb_options)) {
	use_key = RB_GRN_TRUE;
    } else {
	VALUE rb_option_id;

	rb_grn_scan_options(rb_options,
			    "id", &rb_option_id,
			    NULL);
	use_key = !RVAL2CBOOL(rb_option_id);
    }

    if (use_key) {
	return rb_grn_table_key_support_get_value_by_key(self, rb_id_or_key);
    } else {
	return rb_grn_table_get_value(self, rb_id_or_key);
    }
}