-
Notifications
You must be signed in to change notification settings - Fork 25
/
rb-grn-table.c
2558 lines (2279 loc) · 82.9 KB
/
rb-grn-table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
441
442
443
444
445
446
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
473
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
570
571
572
573
574
575
576
577
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
614
615
616
617
618
619
620
621
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2009-2013 Kouhei Sutou <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "rb-grn.h"
grn_obj *grn_table_open(grn_ctx *ctx,
const char *name, unsigned name_size, const char *path);
grn_obj *grn_column_open(grn_ctx *ctx, grn_obj *table,
const char *name, unsigned name_size,
const char *path, grn_obj *type);
#define SELF(object) ((RbGrnTable *)DATA_PTR(object))
VALUE rb_cGrnTable;
static ID id_array_reference;
static ID id_array_set;
/*
* Document-class: Groonga::Table < Groonga::Object
*
* rroongaが提供するテーブルのベースとなるクラス。このクラス
* から {Groonga::Array} , {Groonga::Hash} , {Groonga::PatriciaTrie}
* が継承されている。
*/
grn_obj *
rb_grn_table_from_ruby_object (VALUE object, grn_ctx **context)
{
if (!RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnTable))) {
rb_raise(rb_eTypeError,
"not a groonga table: <%s>",
rb_grn_inspect(object));
}
return RVAL2GRNOBJECT(object, context);
}
VALUE
rb_grn_table_to_ruby_object (grn_ctx *context, grn_obj *table,
grn_bool owner)
{
return GRNOBJECT2RVAL(Qnil, context, table, owner);
}
void
rb_grn_table_finalizer (grn_ctx *context, grn_obj *object,
RbGrnTable *rb_grn_table)
{
if (context && rb_grn_table->value)
grn_obj_unlink(context, rb_grn_table->value);
rb_grn_table->value = NULL;
rb_grn_table->columns = Qnil;
}
void
rb_grn_table_bind (RbGrnTable *rb_grn_table,
grn_ctx *context, grn_obj *table)
{
RbGrnObject *rb_grn_object;
rb_grn_object = RB_GRN_OBJECT(rb_grn_table);
rb_grn_table->value = grn_obj_open(context, GRN_BULK, 0,
rb_grn_object->range_id);
rb_grn_table->columns = Qnil;
rb_grn_table->columns = rb_ary_new();
}
void
rb_grn_table_deconstruct (RbGrnTable *rb_grn_table,
grn_obj **table,
grn_ctx **context,
grn_id *domain_id,
grn_obj **domain,
grn_obj **value,
grn_id *range_id,
grn_obj **range,
VALUE *columns)
{
RbGrnObject *rb_grn_object;
rb_grn_object = RB_GRN_OBJECT(rb_grn_table);
rb_grn_object_deconstruct(rb_grn_object, table, context,
domain_id, domain,
range_id, range);
if (value)
*value = rb_grn_table->value;
if (columns)
*columns = rb_grn_table->columns;
}
static void
rb_grn_table_mark (void *data)
{
RbGrnObject *rb_grn_object = data;
RbGrnTable *rb_grn_table = data;
grn_ctx *context;
grn_obj *table;
if (!rb_grn_object)
return;
rb_gc_mark(rb_grn_table->columns);
context = rb_grn_object->context;
table = rb_grn_object->object;
if (!context || !table)
return;
rb_grn_context_mark_grn_id(context, table->header.domain);
rb_grn_context_mark_grn_id(context, grn_obj_get_range(context, table));
if (!grn_obj_path(context, table))
return;
if (grn_obj_name(context, table, NULL, 0) == 0)
return;
}
static VALUE
rb_grn_table_alloc (VALUE klass)
{
return Data_Wrap_Struct(klass, rb_grn_table_mark, rb_grn_object_free, NULL);
}
static VALUE
rb_grn_table_initialize (VALUE self)
{
rb_raise(rb_eArgError, "Use Groonga::Context#[] for get existing table.");
return Qnil;
}
VALUE
rb_grn_table_inspect_content (VALUE self, VALUE inspected)
{
RbGrnTable *rb_grn_table;
grn_ctx *context = NULL;
grn_obj *table;
VALUE columns;
rb_grn_table = SELF(self);
if (!rb_grn_table)
return inspected;
rb_grn_table_deconstruct(rb_grn_table, &table, &context,
NULL, NULL,
NULL, NULL, NULL,
&columns);
if (!table)
return inspected;
if (!context)
return inspected;
rb_str_cat2(inspected, ", ");
rb_str_cat2(inspected, "size: <");
{
char buf[21]; /* ceil(log10(2 ** 64)) + 1('\0') == 21 */
snprintf(buf, sizeof(buf), "%u", grn_table_size(context, table));
rb_str_cat2(inspected, buf);
}
rb_str_cat2(inspected, ">");
/*
rb_str_cat2(inspected, ", ");
rb_str_cat2(inspected, "columns: <");
rb_str_concat(inspected, rb_inspect(columns));
rb_str_cat2(inspected, ">");
*/
return inspected;
}
/*
* テーブルの中身を人に見やすい文字列で返す。
*
* @overload inspect
* @return [String]
*/
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;
}
/*
* テーブルに名前が _name_ で型が _value_type_ のカラムを定義
* し、新しく定義されたカラムを返す。
*
* @overload define_column(name, value_type, options={})
* @param options [::Hash] The name and value
* pairs. Omitted names are initialized as the default value.
* @option options :path
* カラムを保存するパス。
* @option options :persistent (永続カラム)
* +true+ を指定すると永続カラムとなる。省略した場合は永
* 続カラムとなる。 +:path+ を省略した場合は自動的にパスが
* 付加される。
* @option options :type (:scalar)
* カラムの値の格納方法について指定する。省略した場合は、
* +:scalar+ になる。
*
* - +:scalar+ := スカラ値(単独の値)を格納する。
* - +:vector+ := 値の配列を格納する。
* @option options [Boolean] :with_weight (false)
* It specifies whether making the column weight vector column or not.
* Weight vector column can store weight for each element.
*
* You can't use this option for scalar column.
* @option options :compress
* 値の圧縮方法を指定する。省略した場合は、圧縮しない。
*
* - +:zlib+ := 値をzlib圧縮して格納する。
* - +:lzo+ := 値をlzo圧縮して格納する。
*
* @return [Groonga::FixSizeColumn or Groonga::VariableSizeColumn]
*/
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, rb_with_weight;
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,
"with_weight", &rb_with_weight,
"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 (RVAL2CBOOL(rb_with_weight)) {
if (flags & GRN_OBJ_COLUMN_VECTOR) {
flags |= GRN_OBJ_WITH_WEIGHT;
} else {
rb_raise(rb_eArgError,
"can't use weight for scalar column");
}
}
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);
if (context->rc) {
rb_grn_context_check(context,
rb_ary_new3(2, self, rb_ary_new4(argc, argv)));
}
rb_column = GRNCOLUMN2RVAL(Qnil, context, column, 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;
}
static grn_bool
n_gram_tokenizer_p(grn_ctx *context, grn_obj *tokenizer)
{
char tokenizer_name[GRN_TABLE_MAX_KEY_SIZE];
int name_size;
name_size = grn_obj_name(context, tokenizer,
tokenizer_name, sizeof(tokenizer_name) - 1);
if (name_size == 0)
return GRN_FALSE;
tokenizer_name[name_size] = '\0';
#define HAVE_PREFIX_P(prefix) \
(strncmp(tokenizer_name, prefix, strlen(prefix)) == 0)
return (HAVE_PREFIX_P("TokenUnigram") ||
HAVE_PREFIX_P("TokenBigram") ||
HAVE_PREFIX_P("TokenTrigram"));
#undef HAVE_PREFIX_P
}
/*
* テーブルに名前が _name_ で型が _value_type_ のインデックスカ
* ラムを定義し、新しく定義されたカラムを返す。
*
* @overload define_index_column(name, value_type, options={})
* @param options [::Hash] The name and value
* pairs. Omitted names are initialized as the default value.
* @option options :path
* カラムを保存するパス。
* @option options :persistent (永続カラム)
* +true+ を指定すると永続カラムとなる。省略した場合は永
* 続カラムとなる。 +:path+ を省略した場合は自動的にパスが
* 付加される。
* @option options :with_section
* 転置索引にsection(段落情報)を合わせて格納する。
* @option options :with_weight
* 転置索引にweight情報を合わせて格納する。
* @option options :with_position
* 転置索引に出現位置情報を合わせて格納する。
* @option options :source
* インデックス対象となるカラムを指定する。 +:sources+ との併用はできない。
* @option options :sources
* インデックス対象となる複数のカラムを指定する。 +:source+ との併用はできない。
*
* @return [Groonga::IndexColumn]
*/
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 (NIL_P(rb_with_section)) {
if (TYPE(rb_sources) == T_ARRAY && RARRAY_LEN(rb_sources) > 1) {
flags |= GRN_OBJ_WITH_SECTION;
}
} else 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_obj *tokenizer;
tokenizer = grn_obj_get_info(context, table,
GRN_INFO_DEFAULT_TOKENIZER,
NULL);
if (tokenizer && n_gram_tokenizer_p(context, tokenizer)) {
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);
if (context->rc) {
rb_grn_context_check(context,
rb_ary_new3(2, self, rb_ary_new4(argc, argv)));
}
rb_column = GRNCOLUMN2RVAL(Qnil, context, column, 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;
}
static void
ruby_object_to_column_name (VALUE rb_name,
const char **name, unsigned *name_size)
{
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;
}
}
/*
* テーブルの _name_ に対応するカラムを返す。カラムが存在しな
* い場合は +nil+ を返す。
*
* @overload column(name)
* @return [Groonga::Column or nil]
*/
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;
grn_bool 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);
ruby_object_to_column_name(rb_name, &name, &name_size);
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 (name_size == rb_grn_named_object->name_size &&
memcmp(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_grn_named_object_set_name(RB_GRN_NAMED_OBJECT(DATA_PTR(rb_column)),
name, name_size);
return rb_column;
}
VALUE
rb_grn_table_get_column_surely (VALUE self, VALUE rb_name)
{
VALUE rb_column;
rb_column = rb_grn_table_get_column(self, rb_name);
if (NIL_P(rb_column)) {
rb_raise(rb_eGrnNoSuchColumn,
"no such column: <%s>: <%s>",
rb_grn_inspect(rb_name), rb_grn_inspect(self));
}
return rb_column;
}
/*
* テーブルの全てのカラムを返す。 _name_ が指定された場合はカ
* ラム名の先頭が _name_ で始まるカラムを返す。
*
* @overload columns(name=nil)
* @return [Groonga::Columnの配列]
*/
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;
VALUE exception;
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) {
grn_obj_unlink(context, columns);
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);
exception = rb_grn_context_to_exception(context, self);
if (!NIL_P(exception)) {
grn_table_cursor_close(context, cursor);
grn_obj_unlink(context, columns);
rb_exc_raise(exception);
}
rb_column = GRNOBJECT2RVAL(Qnil, context, column, GRN_FALSE);
rb_ary_push(rb_columns, rb_column);
}
rc = grn_table_cursor_close(context, cursor);
grn_obj_unlink(context, columns);
if (rc != GRN_SUCCESS) {
rb_grn_context_check(context, self);
rb_grn_rc_check(rc, self);
}
return rb_columns;
}
/*
* テーブルが _name_ カラムを持っている場合は +true+ を返す。
*
* @overload have_column?(name)
*/
static VALUE
rb_grn_table_have_column (VALUE self, VALUE rb_name)
{
grn_ctx *context = NULL;
grn_obj *table;
grn_obj *column;
const char *name = NULL;
unsigned name_size = 0;
rb_grn_table_deconstruct(SELF(self), &table, &context,
NULL, NULL,
NULL, NULL, NULL,
NULL);
ruby_object_to_column_name(rb_name, &name, &name_size);
column = grn_obj_column(context, table, name, name_size);
if (column) {
grn_obj_unlink(context, column);
return Qtrue;
} else {
return Qfalse;
}
}
static grn_table_cursor *
rb_grn_table_open_grn_cursor (int argc, VALUE *argv, VALUE self,
grn_ctx **context)
{
grn_obj *table;
grn_table_cursor *cursor;
void *min_key = NULL, *max_key = NULL;
unsigned min_key_size = 0, max_key_size = 0;
int offset = 0, limit = -1;
int flags = 0;
VALUE options, rb_min, rb_max, rb_order, rb_order_by;
VALUE rb_greater_than, rb_less_than, rb_offset, rb_limit;
rb_grn_table_deconstruct(SELF(self), &table, context,
NULL, NULL,
NULL, NULL, NULL,
NULL);
rb_scan_args(argc, argv, "01", &options);
rb_grn_scan_options(options,
"min", &rb_min,
"max", &rb_max,
"offset", &rb_offset,
"limit", &rb_limit,
"order", &rb_order,
"order_by", &rb_order_by,
"greater_than", &rb_greater_than,
"less_than", &rb_less_than,
NULL);
if (!NIL_P(rb_min)) {
min_key = StringValuePtr(rb_min);
min_key_size = RSTRING_LEN(rb_min);
}
if (!NIL_P(rb_max)) {
max_key = StringValuePtr(rb_max);
max_key_size = RSTRING_LEN(rb_max);
}
if (!NIL_P(rb_offset))
offset = NUM2INT(rb_offset);
if (!NIL_P(rb_limit))
limit = NUM2INT(rb_limit);
flags |= rb_grn_table_cursor_order_to_flag(rb_order);
flags |= rb_grn_table_cursor_order_by_to_flag(table->header.type,
self,
rb_order_by);
if (RVAL2CBOOL(rb_greater_than))
flags |= GRN_CURSOR_GT;
if (RVAL2CBOOL(rb_less_than))
flags |= GRN_CURSOR_LT;
cursor = grn_table_cursor_open(*context, table,
min_key, min_key_size,
max_key, max_key_size,
offset, limit, flags);
rb_grn_context_check(*context, self);
return cursor;
}
/*
* カーソルを生成して返す。ブロックを指定すると、そのブロッ
* クに生成したカーソルが渡され、ブロックを抜けると自動的に
* カーソルが破棄される。
*
* @overload open_cursor(options={})
* @return [Groonga::TableCursor]
* @param options [::Hash] The name and value
* pairs. Omitted names are initialized as the default value.
* @option options :min
* キーの下限
* @option options :max
* キーの上限
* @option options :offset
* 該当する範囲のレコードのうち、(0ベースで) _:offset_ 番目
* からレコードを取り出す。
* @option options :limit
* 該当する範囲のレコードのうち、 _:limit_ 件のみを取り出す。
* 省略された場合または-1が指定された場合は、全件が指定され
* たものとみなす。
* @option options :order (asc)
* +:asc+ または +:ascending+ を指定すると昇順にレコードを取
* り出す。(デフォルト)
* +:desc+ または +:descending+ を指定すると降順にレコードを
* 取り出す。
* @option options :order_by
* +:id+ を指定するとID順にレコードを取り出す。(Arrayと
* Hashのデフォルト)
* +:key+ 指定するとキー順にレコードを取り出す。ただし、
* Groonga::PatriciaTrieにしか使えない。(PatriciaTrieのデ
* フォルト)
* @option options :greater_than
* +true+ を指定すると +:min+ で指定した値に一致した [ +key+ ] を
* 範囲に含まない。
* @option options :less_than
* +true+ を指定すると +:max+ で指定した値に一致した [ +key+ ] を
* 範囲に含まない。
* @overload open_cursor(options={})
* @yield [cursor]
* @param options [::Hash] The name and value
* pairs. Omitted names are initialized as the default value.
* @option options :min
* キーの下限
* @option options :max
* キーの上限
* @option options :offset
* 該当する範囲のレコードのうち、(0ベースで) _:offset_ 番目
* からレコードを取り出す。
* @option options :limit
* 該当する範囲のレコードのうち、 _:limit_ 件のみを取り出す。
* 省略された場合または-1が指定された場合は、全件が指定され
* たものとみなす。
* @option options :order (asc)
* +:asc+ または +:ascending+ を指定すると昇順にレコードを取
* り出す。(デフォルト)
* +:desc+ または +:descending+ を指定すると降順にレコードを
* 取り出す。
* @option options :order_by
* +:id+ を指定するとID順にレコードを取り出す。(Arrayと
* Hashのデフォルト)
* +:key+ 指定するとキー順にレコードを取り出す。ただし、
* Groonga::PatriciaTrieにしか使えない。(PatriciaTrieのデ
* フォルト)
* @option options :greater_than
* +true+ を指定すると +:min+ で指定した値に一致した [ +key+ ] を
* 範囲に含まない。
* @option options :less_than
* +true+ を指定すると +:max+ で指定した値に一致した [ +key+ ] を
* 範囲に含まない。
*/
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;
}
/*
* テーブルに登録されている全てのレコードが入っている配列を
* 返す。
*
* @overload records
* @return [Groonga::Recordの配列]
*/
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;
}
/*
* テーブルに登録されているレコード数を返す。
*
* @overload size
* @return [Integer]
*/
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);
}
/*
* テーブルにレコードが登録されていなければ +true+ を返す。
*
* @overload empty?
*/
static VALUE
rb_grn_table_empty_p (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 CBOOL2RVAL(size == 0);
}
/*
* テーブルの全レコードを一括して削除する。
*
* @overload truncate
*/
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;
}
/*
* テーブルに登録されているレコードを順番にブロックに渡す。
*
* _options_ is the same as {#open_cursor} 's one.
*
* @overload each
* @!macro [new] table.each.metadata
* @yield [record]
* @return [nil]
* @!macro table.each.metadata
* @overload each(options={})
* @!macro table.each.metadata
*/
static VALUE
rb_grn_table_each (int argc, VALUE *argv, VALUE self)
{
RbGrnTable *rb_table;
RbGrnObject *rb_grn_object;
grn_ctx *context = NULL;
grn_table_cursor *cursor;
VALUE rb_cursor;
grn_id id;
RETURN_ENUMERATOR(self, argc, argv);
cursor = rb_grn_table_open_grn_cursor(argc, argv, self, &context);
rb_cursor = GRNTABLECURSOR2RVAL(Qnil, context, cursor);
rb_table = SELF(self);
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;
}
VALUE
rb_grn_table_delete_by_id (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;
}
VALUE
rb_grn_table_delete_by_expression (VALUE self)
{
grn_ctx *context = NULL;
grn_obj *table;
VALUE rb_builder, rb_expression;
grn_obj *needless_records, *expression;
grn_operator operator = GRN_OP_OR;
grn_table_cursor *cursor;
rb_grn_table_deconstruct(SELF(self), &table, &context,