Class: Groonga::Table

Inherits:
GrnObject
  • Object
show all
Includes:
Enumerable
Defined in:
ext/rb-grn-table.c,
ext/rb-grn-table.c

Overview

Ruby/groongaが提供するテーブルのベースとなるクラス。このクラス からGroonga::Array, Groonga::Hash, Groonga::PatriciaTrie が継承されている。

Direct Known Subclasses

PatriciaTrie

Defined Under Namespace

Modules: KeySupport

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/rb-grn-table.c', line 161

static VALUE
rb_grn_table_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_object_assign(Qnil, self, rb_context, context, table);
    rb_grn_context_check(context, self);

    return Qnil;
}

Class Method Details

.Groonga::Table.open(options = {}) ⇒ Groonga::Table .Groonga::Table.open(options = ) { ... } ⇒ Object

既存のテーブルを開く。ブロックを指定すると、そのブロック に開かれたテーブルが渡され、ブロックを抜けると自動的にテ ーブルが破棄される。

optionsに指定可能な値は以下の通り。

:context

テーブルが利用するGroonga::Context。省略すると Groonga::Context.defaultを用いる。

:name

開こうとするテーブルの名前。

:path

開こうとするテーブルのパス。

Overloads:



196
197
198
199
200
201
202
203
204
205
206
207
208
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
238
239
# File 'ext/rb-grn-table.c', line 196

static VALUE
rb_grn_table_s_open (int argc, VALUE *argv, VALUE klass)
{
    grn_user_data *user_data;
    VALUE rb_table = Qnil;
    grn_obj *table;
    grn_ctx *context = NULL;
    VALUE rb_context;

    table = rb_grn_table_open_raw(argc, argv, &context, &rb_context);
    rb_grn_context_check(context, rb_ary_new4(argc, argv));

    if (!table)
	rb_raise(rb_eGrnError,
		 "unable to open table: %s: %s",
		 rb_grn_inspect(klass),
		 rb_grn_inspect(rb_ary_new4(argc, argv)));

    user_data = grn_obj_user_data(context, table);
    if (user_data && user_data->ptr) {
	rb_table = RB_GRN_OBJECT(user_data->ptr)->self;
    } else {
	if (klass == rb_cGrnTable) {
	    klass = GRNOBJECT2RCLASS(table);
	} else {
	    VALUE rb_class;

	    rb_class = GRNOBJECT2RCLASS(table);
	    if (rb_class != klass) {
		rb_raise(rb_eTypeError,
			 "unexpected existing table type: %s: expected %s",
			 rb_grn_inspect(rb_class),
			 rb_grn_inspect(klass));
	    }
	}

	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;
}

Instance Method Details

#[]Object

call-seq:

table[id] -> Groonga::Record

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

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



1268
1269
1270
1271
1272
# File 'ext/rb-grn-table.c', line 1268

VALUE
rb_grn_table_array_reference (VALUE self, VALUE rb_id)
{
    return rb_grn_record_new_raw(self, rb_id, Qnil);
}

#add_column(name, value_type, path) ⇒ Object

value_typeを値の型として、pathに保存されている永続的 なカラムを、テーブルのnameに対応するカラムとして開く。



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'ext/rb-grn-table.c', line 578

static VALUE
rb_grn_table_add_column (VALUE self, VALUE rb_name, VALUE rb_value_type,
			 VALUE rb_path)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_obj *value_type, *column;
    char *name = NULL, *path = NULL;
    unsigned name_size = 0;
    VALUE rb_column;
    VALUE columns;

    rb_grn_table_deconstruct(SELF(self), &table, &context,
			     NULL, NULL,
			     NULL, NULL, NULL,
			     &columns);

    name = StringValuePtr(rb_name);
    name_size = RSTRING_LEN(rb_name);

    value_type = RVAL2GRNOBJECT(rb_value_type, &context);

    path = StringValueCStr(rb_path);

    column = grn_column_open(context, table, name, name_size,
			     path, value_type);
    rb_grn_context_check(context, self);

    rb_column = GRNCOLUMN2RVAL(Qnil, context, column, RB_GRN_TRUE);
    rb_iv_set(rb_column, "table", self);
    rb_ary_push(columns, rb_column);
    rb_grn_named_object_set_name(RB_GRN_NAMED_OBJECT(DATA_PTR(rb_column)),
				 name, name_size);

    return rb_column;
}

#clear_lockObject

call-seq:

table.clear_lock(options={})

tableのロックを強制的に解除する。

利用可能なオプションは以下の通り。

:id

:idで指定したレコードのロックを強制的に解除する。 (注: groonga側が未実装のため、現在は無視される。実装さ れるのではないかと思っているが、実装されないかもしれな い。)



1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
# File 'ext/rb-grn-table.c', line 1629

static VALUE
rb_grn_table_clear_lock (int argc, VALUE *argv, VALUE self)
{
    grn_id id = GRN_ID_NIL;
    grn_ctx *context;
    grn_obj *table;
    VALUE options, rb_id;

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

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

    rb_grn_scan_options(options,
			"id", &rb_id,
			NULL);

    if (!NIL_P(rb_id))
	id = NUM2UINT(rb_id);

    grn_obj_clear_lock(context, table);

    return Qnil;
}

#column(name) ⇒ Groonga::Column?

テーブルのnameに対応するカラムを返す。カラムが存在しな い場合はnilを返す。

Returns:



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'ext/rb-grn-table.c', line 622

VALUE
rb_grn_table_get_column (VALUE self, VALUE rb_name)
{
    grn_user_data *user_data;
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_obj *column;
    const char *name = NULL;
    unsigned name_size = 0;
    rb_grn_boolean owner;
    VALUE rb_column;
    VALUE columns;
    VALUE *raw_columns;
    long i, n;

    rb_grn_table_deconstruct(SELF(self), &table, &context,
			     NULL, NULL,
			     NULL, NULL, NULL,
			     &columns);

    switch (TYPE(rb_name)) {
      case T_SYMBOL:
	name = rb_id2name(SYM2ID(rb_name));
	name_size = strlen(name);
	break;
      case T_STRING:
	name = StringValuePtr(rb_name);
	name_size = RSTRING_LEN(rb_name);
	break;
      default:
	rb_raise(rb_eArgError,
		 "column name should be String or Symbol: %s",
		 rb_grn_inspect(rb_name));
	break;
    }

    raw_columns = RARRAY_PTR(columns);
    n = RARRAY_LEN(columns);
    for (i = 0; i < n; i++) {
	VALUE rb_column = raw_columns[i];
	RbGrnNamedObject *rb_grn_named_object;

	rb_grn_named_object = RB_GRN_NAMED_OBJECT(DATA_PTR(rb_column));
	if (rb_grn_named_object->name_size > 0 &&
	    strncmp(name, rb_grn_named_object->name, name_size) == 0) {
	    return rb_column;
	}
    }

    column = grn_obj_column(context, table, name, name_size);
    rb_grn_context_check(context, self);
    if (!column)
	return Qnil;

    user_data = grn_obj_user_data(context, column);
    if (user_data) {
	RbGrnObject *rb_grn_object;
	rb_grn_object = user_data->ptr;
	if (rb_grn_object) {
	    rb_ary_push(columns, rb_grn_object->self);
	    return rb_grn_object->self;
	}
    }

    owner = column->header.type == GRN_ACCESSOR;
    rb_column = GRNCOLUMN2RVAL(Qnil, context, column, owner);
    if (owner) {
	rb_iv_set(rb_column, "table", self);
    }
    rb_ary_push(columns, rb_column);

    return rb_column;
}

#column_valueObject

call-seq:

table.column_value(id, name) -> 値
table.column_value(id, name, :id => true) -> 値

tableidに対応するカラムnameの値を返す。

:id => trueが指定できるのは利便性のため。 Groonga::ArrayでもGroonga::HashやGroonga::PatriciaTrieと 同じ引数で動くようになる。



1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
# File 'ext/rb-grn-table.c', line 1422

static VALUE
rb_grn_table_get_column_value_convenience (int argc, VALUE *argv, VALUE self)
{
    VALUE rb_id, rb_name, rb_options;

    rb_scan_args(argc, argv, "21", &rb_id, &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 (!(NIL_P(rb_option_id) || RVAL2CBOOL(rb_option_id))) {
	    rb_raise(rb_eArgError, ":id options must be true or nil: %s: %s",
		     rb_grn_inspect(rb_option_id),
		     rb_grn_inspect(rb_ary_new3(2,
						self,
						rb_ary_new4(argc, argv))));
	}
    }

    return rb_grn_table_get_column_value(self, rb_id, rb_name);
}

#columns(name = nil) ⇒ Groonga::Columnの配列

テーブルの全てのカラムを返す。nameが指定された場合はカ ラム名の先頭がnameで始まるカラムを返す。

Returns:



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'ext/rb-grn-table.c', line 717

static VALUE
rb_grn_table_get_columns (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_obj *columns;
    grn_rc rc;
    int n;
    grn_table_cursor *cursor;
    VALUE rb_name, rb_columns;
    char *name = NULL;
    unsigned name_size = 0;

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

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

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

    columns = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_HASH_KEY,
			       NULL, 0);
    n = grn_table_columns(context, table, name, name_size, columns);
    rb_grn_context_check(context, self);

    rb_columns = rb_ary_new2(n);
    if (n == 0)
	return rb_columns;

    cursor = grn_table_cursor_open(context, columns, NULL, 0, NULL, 0,
				   0, -1, GRN_CURSOR_ASCENDING);
    rb_grn_context_check(context, self);
    while (grn_table_cursor_next(context, cursor) != GRN_ID_NIL) {
	void *key;
	grn_id *column_id;
	grn_obj *column;
	VALUE rb_column;

	grn_table_cursor_get_key(context, cursor, &key);
	column_id = key;
	column = grn_ctx_at(context, *column_id);
	rb_column = GRNOBJECT2RVAL(Qnil, context, column, RB_GRN_FALSE);
	rb_ary_push(rb_columns, rb_column);
    }
    rc = grn_table_cursor_close(context, cursor);
    if (rc != GRN_SUCCESS) {
	rb_grn_context_check(context, self);
	rb_grn_rc_check(rc, self);
    }

    return rb_columns;
}

#define_column(name, value_type, options = {}) ⇒ Object #Groonga::FixSizeColumnObject

テーブルに名前がnameで型がvalue_typeのカラムを定義 し、新しく定義されたカラムを返す。

optionsに指定可能な値は以下の通り。

:path

カラムを保存するパス。

:persistent

trueを指定すると永続カラムとなる。省略した場合は永 続カラムとなる。:pathを省略した場合は自動的にパスが 付加される。

:type

カラムの値の格納方法について指定する。省略した場合は、 :scalarになる。

:scalar

スカラ値(単独の値)を格納する。

:vector

値の配列を格納する。

:compress

値の圧縮方法を指定する。省略した場合は、圧縮しない。

:zlib

値をzlib圧縮して格納する。

:lzo

値をlzo圧縮して格納する。



358
359
360
361
362
363
364
365
366
367
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
397
398
399
400
401
402
403
404
405
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
437
438
439
440
# File 'ext/rb-grn-table.c', line 358

static VALUE
rb_grn_table_define_column (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_obj *value_type, *column;
    char *name = NULL, *path = NULL;
    unsigned name_size = 0;
    grn_obj_flags flags = 0;
    VALUE rb_name, rb_value_type;
    VALUE options, rb_path, rb_persistent, rb_compress, rb_type;
    VALUE columns;
    VALUE rb_column;

    rb_grn_table_deconstruct(SELF(self), &table, &context,
			     NULL, NULL,
			     NULL, NULL, NULL,
			     &columns);

    rb_scan_args(argc, argv, "21", &rb_name, &rb_value_type, &options);

    name = StringValuePtr(rb_name);
    name_size = RSTRING_LEN(rb_name);

    rb_grn_scan_options(options,
			"path", &rb_path,
			"persistent", &rb_persistent,
			"type", &rb_type,
			"compress", &rb_compress,
			NULL);

    value_type = RVAL2GRNOBJECT(rb_value_type, &context);

    if ((NIL_P(rb_persistent) && grn_obj_path(context, table)) ||
	RVAL2CBOOL(rb_persistent)) {
	flags |= GRN_OBJ_PERSISTENT;
    }

    if (!NIL_P(rb_path)) {
	path = StringValueCStr(rb_path);
	if ((flags & GRN_OBJ_PERSISTENT) != GRN_OBJ_PERSISTENT) {
	    rb_raise(rb_eArgError,
		     "should not pass :path if :persistent is false: <%s>",
		     path);
	}
	flags |= GRN_OBJ_PERSISTENT;
    }

    if (NIL_P(rb_type) ||
	(rb_grn_equal_option(rb_type, "scalar"))) {
	flags |= GRN_OBJ_COLUMN_SCALAR;
    } else if (rb_grn_equal_option(rb_type, "vector")) {
	flags |= GRN_OBJ_COLUMN_VECTOR;
    } else {
	rb_raise(rb_eArgError,
		 "invalid column type: %s: "
		 "available types: [:scalar, :vector, nil]",
		 rb_grn_inspect(rb_type));
    }

    if (NIL_P(rb_compress)) {
    } else if (rb_grn_equal_option(rb_compress, "zlib")) {
	flags |= GRN_OBJ_COMPRESS_ZLIB;
    } else if (rb_grn_equal_option(rb_compress, "lzo")) {
	flags |= GRN_OBJ_COMPRESS_LZO;
    } else {
	rb_raise(rb_eArgError,
		 "invalid compress type: %s: "
		 "available types: [:zlib, :lzo, nil]",
		 rb_grn_inspect(rb_compress));
    }

    column = grn_column_create(context, table, name, name_size,
			       path, flags, value_type);
    rb_grn_context_check(context, self);

    rb_column = GRNCOLUMN2RVAL(Qnil, context, column, RB_GRN_TRUE);
    rb_ary_push(columns, rb_column);
    rb_grn_named_object_set_name(RB_GRN_NAMED_OBJECT(DATA_PTR(rb_column)),
				 name, name_size);

    return rb_column;
}

#define_index_column(name, value_type, options = {}) ⇒ Groonga::IndexColumn

テーブルに名前がnameで型がvalue_typeのインデックスカ ラムを定義し、新しく定義されたカラムを返す。

optionsに指定可能な値は以下の通り。

:path

カラムを保存するパス。

:persistent

trueを指定すると永続カラムとなる。省略した場合は永 続カラムとなる。:pathを省略した場合は自動的にパスが 付加される。

:with_section

転置索引にsection(段落情報)を合わせて格納する。

:with_weight

転置索引にweight情報を合わせて格納する。

:with_position

転置索引に出現位置情報を合わせて格納する。

:source

インデックス対象となるカラムを指定する。:sourcesとの併用はできない。

:sources

インデックス対象となる複数のカラムを指定する。:sourceとの併用はできない。



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'ext/rb-grn-table.c', line 474

static VALUE
rb_grn_table_define_index_column (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_obj *value_type, *column;
    char *name = NULL, *path = NULL;
    unsigned name_size = 0;
    grn_obj_flags flags = GRN_OBJ_COLUMN_INDEX;
    VALUE rb_name, rb_value_type;
    VALUE options, rb_path, rb_persistent;
    VALUE rb_with_section, rb_with_weight, rb_with_position;
    VALUE rb_column, rb_source, rb_sources;
    VALUE columns;

    rb_grn_table_deconstruct(SELF(self), &table, &context,
			     NULL, NULL,
			     NULL, NULL, NULL,
			     &columns);

    rb_scan_args(argc, argv, "21", &rb_name, &rb_value_type, &options);

    name = StringValuePtr(rb_name);
    name_size = RSTRING_LEN(rb_name);

    rb_grn_scan_options(options,
			"path", &rb_path,
			"persistent", &rb_persistent,
			"with_section", &rb_with_section,
			"with_weight", &rb_with_weight,
			"with_position", &rb_with_position,
			"source", &rb_source,
			"sources", &rb_sources,
			NULL);

    value_type = RVAL2GRNOBJECT(rb_value_type, &context);

    if ((NIL_P(rb_persistent) && grn_obj_path(context, table)) ||
	RVAL2CBOOL(rb_persistent)) {
	flags |= GRN_OBJ_PERSISTENT;
    }

    if (!NIL_P(rb_path)) {
	path = StringValueCStr(rb_path);
	if ((flags & GRN_OBJ_PERSISTENT) != GRN_OBJ_PERSISTENT) {
	    rb_raise(rb_eArgError,
		     "should not pass :path if :persistent is false: <%s>",
		     path);
	}
	flags |= GRN_OBJ_PERSISTENT;
    }

    if (RVAL2CBOOL(rb_with_section))
	flags |= GRN_OBJ_WITH_SECTION;

    if (RVAL2CBOOL(rb_with_weight))
	flags |= GRN_OBJ_WITH_WEIGHT;

    if (NIL_P(rb_with_position) &&
	(table->header.type == GRN_TABLE_HASH_KEY ||
	 table->header.type == GRN_TABLE_PAT_KEY)) {
	grn_id tokenizer_id;
	grn_obj *tokenizer;

	tokenizer = grn_obj_get_info(context, table,
				     GRN_INFO_DEFAULT_TOKENIZER,
				     NULL);
	tokenizer_id = grn_obj_id(context, tokenizer);
	if ((tokenizer_id == GRN_DB_UNIGRAM) ||
	    (tokenizer_id == GRN_DB_BIGRAM) ||
	    (tokenizer_id == GRN_DB_TRIGRAM)) {
	    rb_with_position = Qtrue;
	}
    }
    if (RVAL2CBOOL(rb_with_position))
	flags |= GRN_OBJ_WITH_POSITION;

    if (!NIL_P(rb_source) && !NIL_P(rb_sources))
	rb_raise(rb_eArgError, "should not pass both of :source and :sources.");

    column = grn_column_create(context, table, name, name_size,
			       path, flags, value_type);
    rb_grn_context_check(context, self);

    rb_column = GRNCOLUMN2RVAL(Qnil, context, column, RB_GRN_TRUE);
    if (!NIL_P(rb_source))
	rb_funcall(rb_column, rb_intern("source="), 1, rb_source);
    if (!NIL_P(rb_sources))
	rb_funcall(rb_column, rb_intern("sources="), 1, rb_sources);

    rb_ary_push(columns, rb_column);
    rb_grn_named_object_set_name(RB_GRN_NAMED_OBJECT(DATA_PTR(rb_column)),
				 name, name_size);

    return rb_column;
}

#delete(id) ⇒ Object

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



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'ext/rb-grn-table.c', line 1012

VALUE
rb_grn_table_delete (VALUE self, VALUE rb_id)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_id id;
    grn_rc rc;

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

    id = NUM2UINT(rb_id);
    rc = grn_table_delete_by_id(context, table, id);
    rb_grn_rc_check(rc, self);

    return Qnil;
}

#difference!(other) ⇒ Groonga::Table

キーを比較し、otherにも登録されているレコードをtable から削除する。

Returns:



1945
1946
1947
1948
1949
# File 'ext/rb-grn-table.c', line 1945

static VALUE
rb_grn_table_difference_bang (VALUE self, VALUE rb_other)
{
    return rb_grn_table_set_operation_bang(self, rb_other, GRN_OP_BUT);
}

#each {|record| ... } ⇒ Object

テーブルに登録されているレコードを順番にブロックに渡す。

Yields:

  • (record)


977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
# File 'ext/rb-grn-table.c', line 977

static VALUE
rb_grn_table_each (VALUE self)
{
    RbGrnTable *rb_table;
    RbGrnObject *rb_grn_object;
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_table_cursor *cursor;
    VALUE rb_cursor;
    grn_id id;

    rb_table = SELF(self);
    rb_grn_table_deconstruct(rb_table, &table, &context,
			     NULL, NULL,
			     NULL, NULL, NULL,
			     NULL);
    cursor = grn_table_cursor_open(context, table, NULL, 0, NULL, 0,
				   0, -1, GRN_CURSOR_ASCENDING);
    rb_cursor = GRNTABLECURSOR2RVAL(Qnil, context, cursor);
    rb_grn_object = RB_GRN_OBJECT(rb_table);
    while (rb_grn_object->object &&
	   (id = grn_table_cursor_next(context, cursor)) != GRN_ID_NIL) {
	rb_yield(rb_grn_record_new(self, id, Qnil));
    }
    rb_grn_object_close(rb_cursor);

    return Qnil;
}

#group(column, options = {}) ⇒ Groonga::Hash #group(column1, column2, ..., options = {}) ⇒ Array

tableのレコードをcolumn1, column2, で指定したカ ラムの値でグループ化する。カラムはカラム名(文字列)でも 指定可能。

このAPIは将来変更されます。

Overloads:



1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
# File 'ext/rb-grn-table.c', line 1178

static VALUE
rb_grn_table_group (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_table_sort_key *keys;
    grn_table_group_result *results;
    int i, n_keys, n_results;
    grn_rc rc;
    VALUE rb_keys;
    VALUE *rb_sort_keys;
    VALUE rb_results;

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

    rb_scan_args(argc, argv, "00*", &rb_keys);

    n_keys = RARRAY_LEN(rb_keys);
    rb_sort_keys = RARRAY_PTR(rb_keys);
    if (n_keys == 1 && TYPE(rb_sort_keys[0]) == T_ARRAY) {
	n_keys = RARRAY_LEN(rb_sort_keys[0]);
	rb_sort_keys = RARRAY_PTR(rb_sort_keys[0]);
    }
    keys = ALLOCA_N(grn_table_sort_key, n_keys);
    for (i = 0; i < n_keys; i++) {
	VALUE rb_sort_options, rb_key;

	if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_keys[i], rb_cHash))) {
	    rb_sort_options = rb_sort_keys[i];
	} else {
	    rb_sort_options = rb_hash_new();
	    rb_hash_aset(rb_sort_options,
			 RB_GRN_INTERN("key"),
			 rb_sort_keys[i]);
	}
	rb_grn_scan_options(rb_sort_options,
			    "key", &rb_key,
			    NULL);
	if (RVAL2CBOOL(rb_obj_is_kind_of(rb_key, rb_cString)))
	    rb_key = rb_grn_table_get_column(self, rb_key);
	keys[i].key = RVAL2GRNOBJECT(rb_key, &context);
	keys[i].flags = 0;
    }

    n_results = n_keys;
    results = ALLOCA_N(grn_table_group_result, n_results);
    rb_results = rb_ary_new();
    for (i = 0; i < n_results; i++) {
	grn_obj *result;
	grn_id range_id;
	VALUE rb_result;

	range_id = grn_obj_get_range(context, keys[i].key);
	result = grn_table_create(context, NULL, 0, NULL,
				  GRN_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
				  grn_ctx_at(context, range_id), 0);
	results[i].table = result;
	results[i].key_begin = 0;
	results[i].key_end = 0;
	results[i].limit = 0;
	results[i].flags = 0;
	results[i].op = GRN_OP_OR;

	rb_result = GRNOBJECT2RVAL(Qnil, context, result, RB_GRN_TRUE);
	rb_ary_push(rb_results, rb_result);
    }

    rc = grn_table_group(context, table, keys, n_keys, results, n_results);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    if (n_results == 1)
	return rb_ary_pop(rb_results);
    else
	return rb_results;
}

#inspectString

テーブルの中身を人に見やすい文字列で返す。

Returns:

  • (String)


307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'ext/rb-grn-table.c', line 307

static VALUE
rb_grn_table_inspect (VALUE self)
{
    VALUE inspected;

    inspected = rb_str_new2("");
    rb_grn_object_inspect_header(self, inspected);
    rb_grn_object_inspect_content(self, inspected);
    rb_grn_table_inspect_content(self, inspected);
    rb_grn_object_inspect_footer(self, inspected);

    return inspected;
}

#intersection!(other) ⇒ Groonga::Table

キーを比較し、otherには登録されていないレコードを tableから削除する。

Returns:



1931
1932
1933
1934
1935
# File 'ext/rb-grn-table.c', line 1931

static VALUE
rb_grn_table_intersection_bang (VALUE self, VALUE rb_other)
{
    return rb_grn_table_set_operation_bang(self, rb_other, GRN_OP_AND);
}

#lockObject

call-seq:

table.lock(options={})
table.lock(options={}) {...}

tableをロックする。ロックに失敗した場合は Groonga::ResourceDeadlockAvoided例外が発生する。

ブロックを指定した場合はブロックを抜けたときにunlockする。

利用可能なオプションは以下の通り。

:timeout

ロックを獲得できなかった場合は:timeout秒間ロックの獲 得を試みる。:timeout秒以内にロックを獲得できなかった 場合は例外が発生する。

:id

:idで指定したレコードをロックする。(注: groonga側が 未実装のため、現在は無視される)



1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
# File 'ext/rb-grn-table.c', line 1574

static VALUE
rb_grn_table_lock (int argc, VALUE *argv, VALUE self)
{
    grn_id id = GRN_ID_NIL;
    grn_ctx *context;
    grn_obj *table;
    int timeout = 0;
    grn_rc rc;
    VALUE options, rb_timeout, rb_id;

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

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

    rb_grn_scan_options(options,
			"timeout", &rb_timeout,
			"id", &rb_id,
			NULL);

    if (!NIL_P(rb_timeout))
	timeout = NUM2UINT(rb_timeout);

    if (!NIL_P(rb_id))
	id = NUM2UINT(rb_id);

    rc = grn_obj_lock(context, table, id, timeout);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    if (rb_block_given_p()) {
	return rb_ensure(rb_yield, Qnil, rb_grn_table_unlock_ensure, self);
    } else {
	return Qnil;
    }
}

#locked?Boolean

call-seq:

table.locked?(options={})

tableがロックされていればtrueを返す。

利用可能なオプションは以下の通り。

:id

:idで指定したレコードがロックされていればtrueを返す。 (注: groonga側が未実装のため、現在は無視される。実装さ れるのではないかと思っているが、実装されないかもしれな い。)

Returns:

  • (Boolean)


1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
# File 'ext/rb-grn-table.c', line 1672

static VALUE
rb_grn_table_is_locked (int argc, VALUE *argv, VALUE self)
{
    grn_id id = GRN_ID_NIL;
    grn_ctx *context;
    grn_obj *table;
    VALUE options, rb_id;

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

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

    rb_grn_scan_options(options,
			"id", &rb_id,
			NULL);

    if (!NIL_P(rb_id))
	id = NUM2UINT(rb_id);

    return CBOOL2RVAL(grn_obj_is_locked(context, table));
}

#merge!(other) ⇒ Groonga::Table

キーを比較し、otherにも登録されているtableのレコード のスコアをotherのスコアと同値にする。

Returns:



1959
1960
1961
1962
1963
# File 'ext/rb-grn-table.c', line 1959

static VALUE
rb_grn_table_merge_bang (VALUE self, VALUE rb_other)
{
    return rb_grn_table_set_operation_bang(self, rb_other, GRN_OP_ADJUST);
}

#open_cursor(options = {}) ⇒ Groonga::TableCursor #open_cursor(options = ) { ... } ⇒ Object

カーソルを生成して返す。ブロックを指定すると、そのブロッ クに生成したカーソルが渡され、ブロックを抜けると自動的に カーソルが破棄される。

optionsに指定可能な値は以下の通り。

:min

キーの下限

:max

キーの上限

:offset

該当する範囲のレコードのうち、(0ベースで):offset番目 からレコードを取り出す。

:limit

該当する範囲のレコードのうち、:limit件のみを取り出す。 省略された場合または-1が指定された場合は、全件が指定され たものとみなす。

:order

:ascまたは:ascendingを指定すると昇順にレコードを取 り出す。 :descまたは:descendingを指定すると降順にレコードを 取り出す。

:greater_than

trueを指定すると:minで指定した値に一致した[key]を 範囲に含まない。

:less_than

trueを指定すると:maxで指定した値に一致した[key]を 範囲に含まない。

Overloads:



886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
# File 'ext/rb-grn-table.c', line 886

static VALUE
rb_grn_table_open_cursor (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_table_cursor *cursor;
    VALUE rb_cursor;

    cursor = rb_grn_table_open_grn_cursor(argc, argv, self, &context);
    rb_cursor = GRNTABLECURSOR2RVAL(Qnil, context, cursor);
    rb_iv_set(rb_cursor, "@table", self); /* FIXME: cursor should mark table */
    if (rb_block_given_p())
	return rb_ensure(rb_yield, rb_cursor, rb_grn_object_close, rb_cursor);
    else
	return rb_cursor;
}

#recordsGroonga::Recordの配列

テーブルに登録されている全てのレコードが入っている配列を 返す。

Returns:



909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
# File 'ext/rb-grn-table.c', line 909

static VALUE
rb_grn_table_get_records (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_table_cursor *cursor;
    grn_id record_id;
    VALUE records;

    cursor = rb_grn_table_open_grn_cursor(argc, argv, self, &context);
    records = rb_ary_new();
    while ((record_id = grn_table_cursor_next(context, cursor))) {
	rb_ary_push(records, rb_grn_record_new(self, record_id, Qnil));
    }
    grn_table_cursor_close(context, cursor);

    return records;
}

#select(options) {|record| ... } ⇒ Groonga::Hash #select(query, options) ⇒ Groonga::Hash #select(expression, options) ⇒ Groonga::Hash

tableからブロックまたは文字列で指定した条件にマッチする レコードを返す。返されたテーブルにはexpressionという特 異メソッドがあり、指定した条件を表している Groonga::Expressionを取得できる。 Groonga::Expression#snippetを使うことにより、指定した条件 用のスニペットを簡単に生成できる。

results = table.select do |record|
  record["description"] =~ "groonga"
end
snippet = results.expression.snippet([["<em>", "</em>"]])
results.each do |record|
  puts "#{record['name']}の説明文の中で「groonga」が含まれる部分"
  snippet.execute(record["description"].each do |snippet|
    puts "---"
    puts "#{snippet}..."
    puts "---"
  end
end

出力例

Ruby/groongaの説明文の中で「groonga」が含まれる部分
---
Ruby/<em>groonga</em>は<em>groonga</em>のいわゆるDB-APIの層の...
---

queryには「:[演算子]」という書式で条件を 指定する。演算子は以下の通り。

なし
カラム値

[値]

!
カラム値

!= [値]

<
カラム値

< [値]

>
カラム値

> [値]

<=
カラム値

<= [値]

>=
カラム値

>= [値]

@

[カラム値]がを含んでいるかどうか

例:

"name:daijiro" # "name"カラムの値が"daijiro"のレコードにマッチ
"description:@groonga" # "description"カラムが
                       # "groonga"を含んでいるレコードにマッチ

expressionには既に作成済みのGroonga::Expressionを渡す

ブロックで条件を指定する場合は Groonga::RecordExpressionBuilderを参照。

optionsに指定可能な値は以下の通り。

:operator

マッチしたレコードをどのように扱うか。指定可能な値は以 下の通り。省略した場合はGroonga::Operation::OR。

Groonga::Operation::OR

マッチしたレコードを追加。すでにレコードが追加され ている場合は何もしない。

Groonga::Operation::AND

マッチしたレコードのスコアを増加。マッチしなかった レコードを削除。

Groonga::Operation::BUT

マッチしたレコードを削除。

Groonga::Operation::ADJUST

マッチしたレコードのスコアを増加。

:result

検索結果を格納するテーブル。マッチしたレコードが追加さ れていく。省略した場合は新しくテーブルを作成して返す。

:name

条件の名前。省略した場合は名前を付けない。

:syntax

queryの構文。省略した場合は:query

参考: Groonga::Expression#parse.

:allow_pragma

query構文時にプラグマを利用するかどうか。省略した場合は 利用する。

参考: Groonga::Expression#parse.

:allow_column

query構文時にカラム指定を利用するかどうか。省略した場合 は利用する。

参考: Groonga::Expression#parse.

:allow_update

script構文時に更新操作を利用するかどうか。省略した場合 は利用する。

参考: Groonga::Expression#parse.

Overloads:



1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
# File 'ext/rb-grn-table.c', line 1804

static VALUE
rb_grn_table_select (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *table, *result, *expression;
    grn_operator operator = GRN_OP_OR;
    VALUE rb_query = Qnil, condition_or_options, options;
    VALUE rb_name, rb_operator, rb_result, rb_syntax;
    VALUE rb_allow_pragma, rb_allow_column, rb_allow_update;
    VALUE rb_expression = Qnil, builder;

    rb_scan_args(argc, argv, "02", &condition_or_options, &options);

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

    if (RVAL2CBOOL(rb_obj_is_kind_of(condition_or_options, rb_cString))) {
	rb_query = condition_or_options;
    } else if (RVAL2CBOOL(rb_obj_is_kind_of(condition_or_options,
					    rb_cGrnExpression))) {
	rb_expression = condition_or_options;
    } else {
	if (!NIL_P(options))
	    rb_raise(rb_eArgError,
		     "should be [query_string, option_hash], "
		     "[expression, opion_hash] "
		     "or [option_hash]: %s",
		     rb_grn_inspect(rb_ary_new4(argc, argv)));
	options = condition_or_options;
    }

    rb_grn_scan_options(options,
			"operator", &rb_operator,
			"result", &rb_result,
			"name", &rb_name,
			"syntax", &rb_syntax,
			"allow_pragma", &rb_allow_pragma,
			"allow_column", &rb_allow_column,
			"allow_update", &rb_allow_update,
			NULL);

    if (!NIL_P(rb_operator))
	operator = NUM2INT(rb_operator);

    if (NIL_P(rb_result)) {
	result = grn_table_create(context, NULL, 0, NULL,
				  GRN_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
				  table,
				  0);
	rb_result = GRNTABLE2RVAL(context, result, RB_GRN_TRUE);
    } else {
	result = RVAL2GRNTABLE(rb_result, &context);
    }

    if (NIL_P(rb_expression)) {
      builder = rb_grn_record_expression_builder_new(self, rb_name);
      rb_funcall(builder, rb_intern("query="), 1, rb_query);
      rb_funcall(builder, rb_intern("syntax="), 1, rb_syntax);
      rb_funcall(builder, rb_intern("allow_pragma="), 1, rb_allow_pragma);
      rb_funcall(builder, rb_intern("allow_column="), 1, rb_allow_column);
      rb_funcall(builder, rb_intern("allow_update="), 1, rb_allow_update);
      rb_expression = rb_grn_record_expression_builder_build(builder);
    }
    rb_grn_object_deconstruct(RB_GRN_OBJECT(DATA_PTR(rb_expression)),
                              &expression, NULL,
			      NULL, NULL, NULL, NULL);

    grn_table_select(context, table, expression, result, operator);
    rb_grn_context_check(context, self);

    rb_attr(rb_singleton_class(rb_result),
	    rb_intern("expression"),
	    RB_GRN_TRUE, RB_GRN_FALSE, RB_GRN_FALSE);
    rb_iv_set(rb_result, "@expression", rb_expression);

    return rb_result;
}

#set_column_valueObject

call-seq:

table.set_column_value(id, name, value)
table.set_column_value(id, name, value, :id => true)

tableidに対応するカラムnameの値としてvalue設定す る。既存の値は上書きされる。

:id => trueが指定できるのは利便性のため。 Groonga::ArrayでもGroonga::HashやGroonga::PatriciaTrieと 同じ引数で動くようになる。



1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
# File 'ext/rb-grn-table.c', line 1479

static VALUE
rb_grn_table_set_column_value_convenience (int argc, VALUE *argv, VALUE self)
{
    VALUE rb_id, rb_name, rb_value, rb_options;

    rb_scan_args(argc, argv, "31", &rb_id, &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 (!(NIL_P(rb_option_id) || RVAL2CBOOL(rb_option_id))) {
	    rb_raise(rb_eArgError, ":id options must be true or nil: %s: %s",
		     rb_grn_inspect(rb_option_id),
		     rb_grn_inspect(rb_ary_new3(2,
						self,
						rb_ary_new4(argc, argv))));
	}
    }

    return rb_grn_table_set_column_value(self, rb_id, rb_name, rb_value);
}

#set_valueObject

call-seq:

table.set_value(id, value)
table.set_value(id, value, :id => true)

tableidに対応する値としてvalue設定する。既存の値は 上書きされる。

:id => trueが指定できるのは利便性のため。 Groonga::ArrayでもGroonga::HashやGroonga::PatriciaTrieと 同じ引数で動くようになる。



1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
# File 'ext/rb-grn-table.c', line 1370

static VALUE
rb_grn_table_set_value_convenience (int argc, VALUE *argv, VALUE self)
{
    VALUE rb_id, rb_value, rb_options;

    rb_scan_args(argc, argv, "21", &rb_id, &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 (!(NIL_P(rb_option_id) || RVAL2CBOOL(rb_option_id))) {
	    rb_raise(rb_eArgError, ":id options must be true or nil: %s: %s",
		     rb_grn_inspect(rb_option_id),
		     rb_grn_inspect(rb_ary_new3(2,
						self, rb_ary_new4(argc, argv))));
	}
    }

    return rb_grn_table_set_value(self, rb_id, rb_value);
}

#sizeObject

テーブルに登録されているレコード数を返す。



933
934
935
936
937
938
939
940
941
942
943
944
945
946
# File 'ext/rb-grn-table.c', line 933

static VALUE
rb_grn_table_get_size (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    unsigned int size;

    rb_grn_table_deconstruct(SELF(self), &table, &context,
			     NULL, NULL,
			     NULL, NULL, NULL,
			     NULL);
    size = grn_table_size(context, table);
    return UINT2NUM(size);
}

#sort(keys, options = {}) ⇒ Groonga::Recordの配列

テーブルに登録されているレコードをkeysで指定されたルー ルに従ってソートしたレコードの配列を返す。

[
 {:key => "カラム名", :order => :asc, :ascending,
                                :desc, :descendingのいずれか},
 {:key => "カラム名", :order => :asc, :ascending,
                                :desc, :descendingのいずれか},
 ...,
]

optionsに指定可能な値は以下の通り。

:offset

ソートされたレコードのうち、(0ベースで):offset番目 からレコードを取り出す。

:limit

ソートされたレコードのうち、:limit件のみを取り出す。 省略された場合または-1が指定された場合は、全件が指定され たものとみなす。

Returns:



1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
# File 'ext/rb-grn-table.c', line 1058

static VALUE
rb_grn_table_sort (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_obj *result;
    grn_table_sort_key *keys;
    int i, n_keys;
    int n_records, offset = 0, limit = -1;
    VALUE rb_keys, options;
    VALUE rb_offset, rb_limit;
    VALUE *rb_sort_keys;
    grn_table_cursor *cursor;
    VALUE rb_result;
    VALUE exception;

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

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

    if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_keys, rb_cArray)))
	rb_raise(rb_eArgError, "keys should be an array of key: <%s>",
		 rb_grn_inspect(rb_keys));

    n_keys = RARRAY_LEN(rb_keys);
    rb_sort_keys = RARRAY_PTR(rb_keys);
    keys = ALLOCA_N(grn_table_sort_key, n_keys);
    for (i = 0; i < n_keys; i++) {
	VALUE rb_sort_options, rb_key, rb_order;

	if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_keys[i], rb_cHash))) {
	    rb_sort_options = rb_sort_keys[i];
	} else if (RVAL2CBOOL(rb_obj_is_kind_of(rb_sort_keys[i], rb_cArray))) {
	    rb_sort_options = rb_hash_new();
	    rb_hash_aset(rb_sort_options,
			 RB_GRN_INTERN("key"),
			 rb_ary_entry(rb_sort_keys[i], 0));
	    rb_hash_aset(rb_sort_options,
			 RB_GRN_INTERN("order"),
			 rb_ary_entry(rb_sort_keys[i], 1));
	} else {
	    rb_sort_options = rb_hash_new();
	    rb_hash_aset(rb_sort_options,
			 RB_GRN_INTERN("key"),
			 rb_sort_keys[i]);
	}
	rb_grn_scan_options(rb_sort_options,
			    "key", &rb_key,
			    "order", &rb_order,
			    NULL);
	if (RVAL2CBOOL(rb_obj_is_kind_of(rb_key, rb_cString)))
	    rb_key = rb_grn_table_get_column(self, rb_key);
	keys[i].key = RVAL2GRNOBJECT(rb_key, &context);
	if (NIL_P(rb_order)) {
	    keys[i].flags = 0;
	} else if (rb_grn_equal_option(rb_order, "desc") ||
		   rb_grn_equal_option(rb_order, "descending")) {
	    keys[i].flags = GRN_TABLE_SORT_DESC;
	} else if (rb_grn_equal_option(rb_order, "asc") ||
		   rb_grn_equal_option(rb_order, "ascending")) {
	    keys[i].flags = GRN_TABLE_SORT_ASC;
	} else {
	    rb_raise(rb_eArgError,
		     "order should be one of "
		     "[nil, :desc, :descending, :asc, :ascending]: %s",
		     rb_grn_inspect(rb_order));
	}
    }

    rb_grn_scan_options(options,
			"offset", &rb_offset,
			"limit", &rb_limit,
			NULL);

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

    result = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_NO_KEY,
			      NULL, table);
    n_records = grn_table_sort(context, table, offset, limit,
			       result, keys, n_keys);
    exception = rb_grn_context_to_exception(context, self);
    if (!NIL_P(exception)) {
        grn_obj_close(context, result);
        rb_exc_raise(exception);
    }

    rb_result = rb_ary_new();
    cursor = grn_table_cursor_open(context, result, NULL, 0, NULL, 0,
				   0, -1, GRN_CURSOR_ASCENDING);
    while (grn_table_cursor_next(context, cursor) != GRN_ID_NIL) {
	void *value;
	grn_id *id;

	grn_table_cursor_get_value(context, cursor, &value);
	id = value;
	rb_ary_push(rb_result, rb_grn_record_new(self, *id, Qnil));
    }
    grn_table_cursor_close(context, cursor);
    grn_obj_close(context, result);

    return rb_result;
}

#truncateObject

テーブルの全レコードを一括して削除する。



954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'ext/rb-grn-table.c', line 954

static VALUE
rb_grn_table_truncate (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *table;
    grn_rc rc;

    rb_grn_table_deconstruct(SELF(self), &table, &context,
			     NULL, NULL,
			     NULL, NULL, NULL,
			     NULL);
    rc = grn_table_truncate(context, table);
    rb_grn_rc_check(rc, self);

    return Qnil;
}

#union!(other) ⇒ Groonga::Table

キーを比較し、tableには登録されていないotherのレコー ドをtableに作成する。

Returns:



1916
1917
1918
1919
1920
# File 'ext/rb-grn-table.c', line 1916

static VALUE
rb_grn_table_union_bang (VALUE self, VALUE rb_other)
{
    return rb_grn_table_set_operation_bang(self, rb_other, GRN_OP_OR);
}

#unlockObject

call-seq:

table.unlock(options={})

tableのロックを解除する。

利用可能なオプションは以下の通り。

:id

:idで指定したレコードのロックを解除する。(注: groonga側が未実装のため、現在は無視される)



1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
# File 'ext/rb-grn-table.c', line 1516

static VALUE
rb_grn_table_unlock (int argc, VALUE *argv, VALUE self)
{
    grn_id id = GRN_ID_NIL;
    grn_ctx *context;
    grn_obj *table;
    grn_rc rc;
    VALUE options, rb_id;

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

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

    rb_grn_scan_options(options,
			"id", &rb_id,
			NULL);

    if (!NIL_P(rb_id))
	id = NUM2UINT(rb_id);

    rc = grn_obj_unlock(context, table, id);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    return Qnil;
}

#valueObject

call-seq:

table.value(id) -> 値
table.value(id, :id => true) -> 値

tableidに対応する値を返す。

:id => trueが指定できるのは利便性のため。 Groonga::ArrayでもGroonga::HashやGroonga::PatriciaTrieと 同じ引数で動くようになる。



1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
# File 'ext/rb-grn-table.c', line 1309

static VALUE
rb_grn_table_get_value_convenience (int argc, VALUE *argv, VALUE self)
{
    VALUE rb_id, rb_options;

    rb_scan_args(argc, argv, "11", &rb_id, &rb_options);
    if (!NIL_P(rb_options)) {
	VALUE rb_option_id;
	rb_grn_scan_options(rb_options,
			    "id", &rb_option_id,
			    NULL);
	if (!(NIL_P(rb_option_id) || RVAL2CBOOL(rb_option_id))) {
	    rb_raise(rb_eArgError, ":id options must be true or nil: %s: %s",
		     rb_grn_inspect(rb_option_id),
		     rb_grn_inspect(rb_ary_new3(2,
						self, rb_ary_new4(argc, argv))));
	}
    }

    return rb_grn_table_get_value(self, rb_id);
}