Debian Codegraph @

lib/mrb/mrb_record.c

raw symbols SCIP
1 /*
2 Copyright(C) 2015 Brazil
3 Copyright(C) 2021-2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "../grn_ctx_impl.h"
21
22 #ifdef GRN_WITH_MRUBY
23 #include <mruby.h>
24 #include <mruby/class.h>
25 #include <mruby/variable.h>
26 #include <mruby/data.h>
27
28 #include "../grn_db.h"
29 #include "mrb_record.h"
30 #include "mrb_bulk.h"
31
32 typedef struct {
33 grn_obj *table;
34 grn_id id;
35 grn_obj key;
36 } grn_mrb_record;
37
38 static void
39 mrb_grn_record_free(mrb_state *mrb, void *data)
40 {
41 grn_ctx *ctx = (grn_ctx *)mrb->ud;
42 grn_mrb_record *record = data;
43
44 if (!record) {
45 return;
46 }
47
48 GRN_OBJ_FIN(ctx, &(record->key));
49 mrb_free(mrb, record);
50 }
51
52 static struct mrb_data_type mrb_grn_record_type = {
53 "Groonga::Record",
54 mrb_grn_record_free
55 };
56
57 static mrb_value
58 mrb_grn_record_initialize(mrb_state *mrb, mrb_value self)
59 {
60 grn_mrb_record *record;
61 mrb_value mrb_table;
62 mrb_value mrb_id;
63
64 mrb_get_args(mrb, "oo", &mrb_table, &mrb_id);
65
66 DATA_TYPE(self) = &mrb_grn_record_type;
67
68 record = mrb_malloc(mrb, sizeof(grn_mrb_record));
69 record->table = DATA_PTR(mrb_table);
70 if (mrb_nil_p(mrb_id)) {
71 record->id = GRN_ID_NIL;
72 } else {
73 record->id = (grn_id)mrb_integer(mrb_id);
74 }
75
76 switch (record->table->header.domain) {
77 case GRN_ID_NIL :
78 case GRN_DB_SHORT_TEXT :
79 GRN_SHORT_TEXT_INIT(&(record->key), 0);
80 break;
81 default :
82 GRN_VALUE_FIX_SIZE_INIT(&(record->key), 0, record->table->header.domain);
83 break;
84 }
85
86 DATA_PTR(self) = record;
87
88 mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@table"), mrb_table);
89 mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@id"), mrb_id);
90
91 return self;
92 }
93
94 static mrb_value
95 mrb_grn_record_set_id(mrb_state *mrb, mrb_value self)
96 {
97 grn_mrb_record *record;
98 mrb_value mrb_id;
99
100 record = DATA_PTR(self);
101 mrb_get_args(mrb, "o", &mrb_id);
102
103 if (mrb_nil_p(mrb_id)) {
104 record->id = GRN_ID_NIL;
105 } else {
106 record->id = (grn_id)mrb_integer(mrb_id);
107 }
108 mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@id"), mrb_id);
109
110 return mrb_id;
111 }
112
113 static mrb_value
114 mrb_grn_record_key(mrb_state *mrb, mrb_value self)
115 {
116 grn_ctx *ctx = (grn_ctx *)mrb->ud;
117 grn_mrb_record *record;
118 int key_size;
119
120 record = DATA_PTR(self);
121
122 if (record->id == GRN_ID_NIL) {
123 return mrb_nil_value();
124 }
125
126 if (record->table->header.type == GRN_TABLE_NO_KEY) {
127 return mrb_nil_value();
128 }
129
130 GRN_BULK_REWIND(&(record->key));
131 key_size = grn_table_get_key(ctx, record->table, record->id,
132 GRN_BULK_HEAD(&(record->key)),
133 GRN_BULK_VSIZE(&(record->key)));
134 if ((size_t)key_size > GRN_BULK_VSIZE(&(record->key))) {
135 grn_bulk_space(ctx, &(record->key), key_size);
136 key_size = grn_table_get_key(ctx, record->table, record->id,
137 GRN_BULK_HEAD(&(record->key)),
138 GRN_BULK_VSIZE(&(record->key)));
139 }
140
141 return grn_mrb_value_from_bulk(mrb, &(record->key));
142 }
143
144 void
145 grn_mrb_record_init(grn_ctx *ctx)
146 {
147 grn_mrb_data *data = &(ctx->impl->mrb);
148 mrb_state *mrb = data->state;
149 struct RClass *module = data->module;
150 struct RClass *klass;
151
152 klass = mrb_define_class_under(mrb, module, "Record", data->object_class);
153 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
154 mrb_define_method(mrb, klass, "initialize",
155 mrb_grn_record_initialize, MRB_ARGS_REQ(2));
156
157 mrb_define_method(mrb, klass, "id=",
158 mrb_grn_record_set_id, MRB_ARGS_REQ(1));
159
160 mrb_define_method(mrb, klass, "key",
161 mrb_grn_record_key, MRB_ARGS_NONE());
162 }
163 #endif