Debian Codegraph @

lib/mrb/mrb_database.c

raw symbols SCIP
1 /*
2 Copyright(C) 2014-2018 Brazil
3 Copyright(C) 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/data.h>
26
27 #include "mrb_ctx.h"
28 #include "mrb_database.h"
29 #include "mrb_converter.h"
30
31 #include "../grn_encoding.h"
32
33 static struct mrb_data_type mrb_grn_database_type = {
34 "Groonga::Database",
35 NULL
36 };
37
38 static mrb_value
39 mrb_grn_database_initialize(mrb_state *mrb, mrb_value self)
40 {
41 mrb_value mrb_database_ptr;
42
43 mrb_get_args(mrb, "o", &mrb_database_ptr);
44 DATA_TYPE(self) = &mrb_grn_database_type;
45 DATA_PTR(self) = mrb_cptr(mrb_database_ptr);
46 return self;
47 }
48
49 static mrb_value
50 mrb_grn_database_class_open(mrb_state *mrb, mrb_value klass)
51 {
52 grn_ctx *ctx = (grn_ctx *)mrb->ud;
53 grn_obj *database;
54 char *utf8_path;
55 const char *path;
56
57 mrb_get_args(mrb, "z", &utf8_path);
58
59 path = grn_encoding_convert_to_locale_from_utf8(ctx, utf8_path, -1, NULL);
60 database = grn_db_open(ctx, path);
61 grn_encoding_converted_free(ctx, path);
62 grn_mrb_ctx_check(mrb);
63
64 return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, database));
65 }
66
67 static mrb_value
68 mrb_grn_database_class_create(mrb_state *mrb, mrb_value klass)
69 {
70 grn_ctx *ctx = (grn_ctx *)mrb->ud;
71 grn_obj *database;
72 char *utf8_path;
73 const char *path;
74
75 mrb_get_args(mrb, "z", &utf8_path);
76
77 path = grn_encoding_convert_to_locale_from_utf8(ctx, utf8_path, -1, NULL);
78 database = grn_db_create(ctx, path, NULL);
79 grn_encoding_converted_free(ctx, path);
80 grn_mrb_ctx_check(mrb);
81
82 return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, database));
83 }
84
85 static mrb_value
86 mrb_grn_database_recover(mrb_state *mrb, mrb_value self)
87 {
88 grn_ctx *ctx = (grn_ctx *)mrb->ud;
89
90 grn_db_recover(ctx, DATA_PTR(self));
91 grn_mrb_ctx_check(mrb);
92
93 return mrb_nil_value();
94 }
95
96 static mrb_value
97 mrb_grn_database_is_locked(mrb_state *mrb, mrb_value self)
98 {
99 grn_ctx *ctx = (grn_ctx *)mrb->ud;
100 unsigned int is_locked;
101
102 is_locked = grn_obj_is_locked(ctx, DATA_PTR(self));
103 grn_mrb_ctx_check(mrb);
104
105 return mrb_bool_value(is_locked != 0);
106 }
107
108 static mrb_value
109 mrb_grn_database_clear_lock(mrb_state *mrb, mrb_value self)
110 {
111 grn_ctx *ctx = (grn_ctx *)mrb->ud;
112
113 grn_obj_clear_lock(ctx, DATA_PTR(self));
114 grn_mrb_ctx_check(mrb);
115
116 return mrb_nil_value();
117 }
118
119 static mrb_value
120 mrb_grn_database_get_last_modified(mrb_state *mrb, mrb_value self)
121 {
122 grn_ctx *ctx = (grn_ctx *)mrb->ud;
123 uint32_t last_modified;
124 struct RClass *time_class;
125
126 last_modified = grn_db_get_last_modified(ctx, DATA_PTR(self));
127
128 time_class = mrb_class_get(mrb, "Time");
129 return mrb_funcall(mrb,
130 mrb_obj_value(time_class),
131 "at",
132 1,
133 mrb_float_value(mrb, last_modified));
134 }
135
136 static mrb_value
137 mrb_grn_database_is_dirty(mrb_state *mrb, mrb_value self)
138 {
139 grn_ctx *ctx = (grn_ctx *)mrb->ud;
140 bool is_dirty = grn_db_is_dirty(ctx, DATA_PTR(self));
141
142 return mrb_bool_value(is_dirty);
143 }
144
145 static mrb_value
146 mrb_grn_database_array_reference(mrb_state *mrb, mrb_value self)
147 {
148 grn_ctx *ctx = (grn_ctx *)mrb->ud;
149 grn_obj *database;
150 mrb_value mrb_id_or_key;
151
152 mrb_get_args(mrb, "o", &mrb_id_or_key);
153
154 database = DATA_PTR(self);
155
156 if (mrb_integer_p(mrb_id_or_key)) {
157 char name[GRN_TABLE_MAX_KEY_SIZE];
158 int name_size;
159
160 name_size = grn_table_get_key(ctx,
161 grn_ctx_db(ctx),
162 (grn_id)mrb_integer(mrb_id_or_key),
163 name,
164 GRN_TABLE_MAX_KEY_SIZE);
165 if (name_size == 0) {
166 return mrb_nil_value();
167 } else {
168 return mrb_str_new(mrb, name, name_size);
169 }
170 } else {
171 grn_id name_domain_id = GRN_DB_SHORT_TEXT;
172 grn_id id;
173 grn_mrb_value_to_raw_data_buffer buffer;
174 void *name;
175 unsigned int name_size;
176
177 grn_mrb_value_to_raw_data_buffer_init(mrb, &buffer);
178 grn_mrb_value_to_raw_data(mrb, "name", mrb_id_or_key,
179 name_domain_id, &buffer,
180 &name, &name_size);
181 id = grn_table_get(ctx, database, name, name_size);
182 grn_mrb_value_to_raw_data_buffer_fin(mrb, &buffer);
183
184 if (id == GRN_ID_NIL) {
185 return mrb_nil_value();
186 } else {
187 return mrb_int_value(mrb, id);
188 }
189 }
190 }
191
192 void
193 grn_mrb_database_init(grn_ctx *ctx)
194 {
195 grn_mrb_data *data = &(ctx->impl->mrb);
196 mrb_state *mrb = data->state;
197 struct RClass *module = data->module;
198 struct RClass *object_class = data->object_class;
199 struct RClass *klass;
200
201 klass = mrb_define_class_under(mrb, module, "Database", object_class);
202 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
203
204 mrb_define_class_method(mrb, klass, "open",
205 mrb_grn_database_class_open,
206 MRB_ARGS_REQ(1));
207 mrb_define_class_method(mrb, klass, "create",
208 mrb_grn_database_class_create,
209 MRB_ARGS_REQ(1));
210
211 mrb_define_method(mrb, klass, "initialize",
212 mrb_grn_database_initialize, MRB_ARGS_REQ(1));
213 mrb_define_method(mrb, klass, "recover",
214 mrb_grn_database_recover, MRB_ARGS_NONE());
215 mrb_define_method(mrb, klass, "locked?",
216 mrb_grn_database_is_locked, MRB_ARGS_NONE());
217 mrb_define_method(mrb, klass, "clear_lock",
218 mrb_grn_database_clear_lock, MRB_ARGS_NONE());
219 mrb_define_method(mrb, klass, "last_modified",
220 mrb_grn_database_get_last_modified, MRB_ARGS_NONE());
221 mrb_define_method(mrb, klass, "dirty?",
222 mrb_grn_database_is_dirty, MRB_ARGS_NONE());
223 mrb_define_method(mrb, klass, "[]",
224 mrb_grn_database_array_reference, MRB_ARGS_REQ(1));
225 }
226 #endif