Debian Codegraph @

lib/proc/proc_object.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2016 Brazil
3 Copyright (C) 2019-2024 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_proc.h"
21 #include "../grn_io.h"
22
23 #include <groonga/plugin.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27
28 static grn_obj *
29 command_object_exist(grn_ctx *ctx,
30 int nargs,
31 grn_obj **args,
32 grn_user_data *user_data)
33 {
34 grn_obj *db;
35 grn_obj *name;
36 grn_id id;
37
38 db = grn_ctx_db(ctx);
39 name = grn_plugin_proc_get_var(ctx, user_data, "name", -1);
40 if (GRN_TEXT_LEN(name) == 0) {
41 GRN_PLUGIN_ERROR(ctx,
42 GRN_INVALID_ARGUMENT,
43 "[object][exist] name is missing");
44 grn_ctx_output_bool(ctx, false);
45 return NULL;
46 }
47
48 id = grn_table_get(ctx,
49 db,
50 GRN_TEXT_VALUE(name),
51 (unsigned int)GRN_TEXT_LEN(name));
52 grn_ctx_output_bool(ctx, id != GRN_ID_NIL);
53 return NULL;
54 }
55
56 void
57 grn_proc_init_object_exist(grn_ctx *ctx)
58 {
59 grn_expr_var vars[1];
60
61 grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1);
62 grn_plugin_command_create(ctx,
63 "object_exist",
64 -1,
65 command_object_exist,
66 1,
67 vars);
68 }
69
70 static grn_obj *
71 command_object_remove(grn_ctx *ctx,
72 int nargs,
73 grn_obj **args,
74 grn_user_data *user_data)
75 {
76 grn_obj *db;
77 grn_obj *name;
78 bool force;
79 grn_obj *target;
80 bool failed_to_open;
81
82 db = grn_ctx_db(ctx);
83 name = grn_plugin_proc_get_var(ctx, user_data, "name", -1);
84 force = grn_plugin_proc_get_var_bool(ctx, user_data, "force", -1, false);
85
86 if (GRN_TEXT_LEN(name) == 0) {
87 GRN_PLUGIN_ERROR(ctx,
88 GRN_INVALID_ARGUMENT,
89 "[object][remove] name is missing");
90 grn_ctx_output_bool(ctx, false);
91 return NULL;
92 }
93
94 target = grn_ctx_get(ctx, GRN_TEXT_VALUE(name), (int)GRN_TEXT_LEN(name));
95 if (target) {
96 grn_obj_remove(ctx, target);
97 if (!force || ctx->rc == GRN_SUCCESS) {
98 grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS);
99 return NULL;
100 }
101 grn_obj_close(ctx, target);
102 failed_to_open = true;
103 } else {
104 failed_to_open = (ctx->rc != GRN_SUCCESS);
105 }
106
107 if (force) {
108 grn_ctx_remove(ctx,
109 GRN_TEXT_VALUE(name),
110 (int)GRN_TEXT_LEN(name),
111 GRN_OBJ_REMOVE_DEPENDENT | GRN_OBJ_REMOVE_ENSURE);
112 grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS);
113 } else {
114 if (failed_to_open) {
115 GRN_PLUGIN_ERROR(ctx,
116 GRN_INVALID_ARGUMENT,
117 "[object][remove] "
118 "failed to open the target object: <%.*s>",
119 (int)GRN_TEXT_LEN(name),
120 GRN_TEXT_VALUE(name));
121 } else {
122 GRN_PLUGIN_ERROR(ctx,
123 GRN_INVALID_ARGUMENT,
124 "[object][remove] target object doesn't exist: <%.*s>",
125 (int)GRN_TEXT_LEN(name),
126 GRN_TEXT_VALUE(name));
127 }
128 grn_ctx_output_bool(ctx, false);
129 }
130
131 return NULL;
132 }
133
134 void
135 grn_proc_init_object_remove(grn_ctx *ctx)
136 {
137 grn_expr_var vars[2];
138
139 grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1);
140 grn_plugin_expr_var_init(ctx, &(vars[1]), "force", -1);
141 grn_plugin_command_create(ctx,
142 "object_remove",
143 -1,
144 command_object_remove,
145 2,
146 vars);
147 }
148
149 static grn_obj *
150 command_object_set_visibility(grn_ctx *ctx,
151 int n_args,
152 grn_obj **args,
153 grn_user_data *user_data)
154 {
155 grn_raw_string name;
156 grn_obj *object;
157
158 name.value =
159 grn_plugin_proc_get_var_string(ctx, user_data, "name", -1, &(name.length));
160 if (name.length == 0) {
161 GRN_PLUGIN_ERROR(ctx,
162 GRN_INVALID_ARGUMENT,
163 "[object][set-visibility] name is missing");
164 return NULL;
165 }
166
167 object = grn_ctx_get(ctx, name.value, (int)(name.length));
168 if (!object) {
169 GRN_PLUGIN_ERROR(ctx,
170 GRN_INVALID_ARGUMENT,
171 "[object][set-visibility] object doesn't exist: <%.*s>",
172 (int)(name.length),
173 name.value);
174 return NULL;
175 }
176
177 bool old_visibility = grn_obj_is_visible(ctx, object);
178 bool new_visibility =
179 grn_plugin_proc_get_var_bool(ctx, user_data, "visible", -1, old_visibility);
180 if (grn_obj_set_visibility(ctx, object, new_visibility) != GRN_SUCCESS) {
181 goto exit;
182 }
183
184 grn_ctx_output_map_open(ctx, "visibility", 2);
185 {
186 grn_ctx_output_cstr(ctx, "old");
187 grn_ctx_output_bool(ctx, old_visibility);
188 grn_ctx_output_cstr(ctx, "new");
189 grn_ctx_output_bool(ctx, grn_obj_is_visible(ctx, object));
190 }
191 grn_ctx_output_map_close(ctx);
192
193 exit:
194 if (grn_obj_is_accessor(ctx, object)) {
195 grn_obj_unlink(ctx, object);
196 }
197
198 return NULL;
199 }
200
201 void
202 grn_proc_init_object_set_visibility(grn_ctx *ctx)
203 {
204 grn_expr_var vars[2];
205 unsigned int n_vars = 0;
206
207 grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "name", -1);
208 grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "visible", -1);
209 grn_plugin_command_create(ctx,
210 "object_set_visibility",
211 -1,
212 command_object_set_visibility,
213 n_vars,
214 vars);
215 }
216
217 static grn_obj *
218 command_object_warm(grn_ctx *ctx,
219 int n_args,
220 grn_obj **args,
221 grn_user_data *user_data)
222 {
223 grn_obj *name = grn_plugin_proc_get_var(ctx, user_data, "name", -1);
224
225 grn_obj *target;
226 bool need_unref = false;
227 if (GRN_TEXT_LEN(name) == 0) {
228 target = grn_ctx_db(ctx);
229 } else {
230 target = grn_ctx_get(ctx, GRN_TEXT_VALUE(name), (int)GRN_TEXT_LEN(name));
231 if (!target) {
232 GRN_PLUGIN_ERROR(ctx,
233 GRN_INVALID_ARGUMENT,
234 "[object][warm] nonexistent target: <%.*s>",
235 (int)GRN_TEXT_LEN(name),
236 GRN_TEXT_VALUE(name));
237 grn_ctx_output_bool(ctx, false);
238 return NULL;
239 }
240 need_unref = true;
241 }
242
243 grn_rc rc = grn_obj_warm(ctx, target);
244 grn_ctx_output_bool(ctx, rc == GRN_SUCCESS);
245
246 if (need_unref) {
247 grn_obj_unref(ctx, target);
248 }
249
250 return NULL;
251 }
252
253 void
254 grn_proc_init_object_warm(grn_ctx *ctx)
255 {
256 grn_expr_var vars[1];
257
258 grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1);
259 grn_plugin_command_create(ctx,
260 "object_warm",
261 -1,
262 command_object_warm,
263 1,
264 vars);
265 }