Debian Codegraph @

lib/options.c

raw symbols SCIP
1 /*
2 Copyright (C) 2018 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_db.h"
21 #include "grn_msgpack.h"
22 #include "grn_options.h"
23 #include "grn_util.h"
24
25 #include <stdio.h>
26
27 #ifdef GRN_WITH_MESSAGE_PACK
28 # include <groonga/msgpack.h>
29 #endif /* GRN_WITH_MESSAGE_PACK */
30
31 struct _grn_options {
32 grn_ja *values;
33 };
34
35 static const char *GRN_OPTIONS_PATH_FORMAT = "%s.options";
36 static const unsigned int GRN_OPTIONS_MAX_VALUE_SIZE = 65536;
37
38 grn_options *
39 grn_options_create(grn_ctx *ctx,
40 const char *path,
41 const char *context_tag)
42 {
43 char *options_path;
44 char options_path_buffer[PATH_MAX];
45 grn_options *options;
46 uint32_t flags = 0;
47
48 if (path) {
49 grn_snprintf(options_path_buffer,
50 PATH_MAX,
51 PATH_MAX,
52 GRN_OPTIONS_PATH_FORMAT,
53 path);
54 options_path = options_path_buffer;
55 } else {
56 options_path = NULL;
57 }
58
59 options = GRN_CALLOC(sizeof(grn_options));
60 if (!options) {
61 ERR(GRN_NO_MEMORY_AVAILABLE,
62 "%s failed to allocate memory for options: <%s>",
63 context_tag,
64 options_path ? options_path : "(temporary)");
65 return NULL;
66 }
67
68 options->values = grn_ja_create(ctx,
69 options_path,
70 GRN_OPTIONS_MAX_VALUE_SIZE,
71 flags);
72 if (!options->values) {
73 GRN_FREE(options);
74 ERR(GRN_NO_MEMORY_AVAILABLE,
75 "%s failed to create data store for options: <%s>",
76 context_tag,
77 options_path ? options_path : "(temporary)");
78 return NULL;
79 }
80
81 return options;
82 }
83
84 grn_options *
85 grn_options_open(grn_ctx *ctx,
86 const char *path,
87 const char *context_tag)
88 {
89 char options_path[PATH_MAX];
90 grn_options *options;
91
92 grn_snprintf(options_path,
93 PATH_MAX,
94 PATH_MAX,
95 GRN_OPTIONS_PATH_FORMAT,
96 path);
97 if (!grn_path_exist(options_path)) {
98 return grn_options_create(ctx, path, context_tag);
99 }
100
101 options = GRN_CALLOC(sizeof(grn_options));
102 if (!options) {
103 ERR(GRN_NO_MEMORY_AVAILABLE,
104 "%s failed to allocate memory for options: <%s>",
105 context_tag,
106 options_path);
107 return NULL;
108 }
109
110 options->values = grn_ja_open(ctx, options_path);
111 if (!options->values) {
112 GRN_FREE(options);
113 ERR(GRN_NO_MEMORY_AVAILABLE,
114 "%s failed to open data store for options: <%s>",
115 context_tag,
116 options_path);
117 return NULL;
118 }
119
120 return options;
121 }
122
123 grn_rc
124 grn_options_close(grn_ctx *ctx, grn_options *options)
125 {
126 grn_rc rc;
127
128 if (!options) {
129 return GRN_SUCCESS;
130 }
131
132 rc = grn_ja_close(ctx, options->values);
133 GRN_FREE(options);
134
135 return rc;
136 }
137
138 grn_rc
139 grn_options_remove(grn_ctx *ctx, const char *path)
140 {
141 char options_path[PATH_MAX];
142
143 grn_snprintf(options_path,
144 PATH_MAX,
145 PATH_MAX,
146 GRN_OPTIONS_PATH_FORMAT,
147 path);
148 return grn_ja_remove(ctx, options_path);
149 }
150
151 bool
152 grn_options_is_locked(grn_ctx *ctx, grn_options *options)
153 {
154 return grn_obj_is_locked(ctx, (grn_obj *)(options->values));
155 }
156
157 grn_rc
158 grn_options_clear_lock(grn_ctx *ctx, grn_options *options)
159 {
160 return grn_obj_clear_lock(ctx, (grn_obj *)(options->values));
161 }
162
163 bool
164 grn_options_is_corrupt(grn_ctx *ctx, grn_options *options)
165 {
166 return grn_obj_is_corrupt(ctx, (grn_obj *)(options->values));
167 }
168
169 grn_rc
170 grn_options_flush(grn_ctx *ctx, grn_options *options)
171 {
172 return grn_obj_flush(ctx, (grn_obj *)(options->values));
173 }
174
175 #ifdef GRN_WITH_MESSAGE_PACK
176 typedef struct {
177 grn_ctx *ctx;
178 grn_obj *buffer;
179 } grn_options_msgpack_write_data;
180
181 static int
182 grn_options_msgpack_write(void *data, const char *buf, msgpack_size_t len)
183 {
184 grn_options_msgpack_write_data *write_data = data;
185 return grn_bulk_write(write_data->ctx,
186 write_data->buffer,
187 buf,
188 len);
189 }
190 #endif /* GRN_WITH_MESSAGE_PACK */
191
192 grn_rc
193 grn_options_set(grn_ctx *ctx,
194 grn_options *options,
195 grn_id id,
196 const char *name,
197 int name_length,
198 grn_obj *values)
199 {
200 #ifdef GRN_WITH_MESSAGE_PACK
201 msgpack_packer packer;
202 grn_options_msgpack_write_data data;
203 grn_obj buffer;
204 grn_io_win iw;
205 void *raw_value;
206 uint32_t length;
207 unsigned int i, n;
208
209 if (name_length < 0) {
210 name_length = (int)strlen(name);
211 }
212
213 GRN_TEXT_INIT(&buffer, 0);
214 data.ctx = ctx;
215 data.buffer = &buffer;
216
217 msgpack_packer_init(&packer, &data, grn_options_msgpack_write);
218
219 raw_value = grn_ja_ref(ctx, options->values, id, &iw, &length);
220 if (raw_value) {
221 msgpack_unpacker unpacker;
222 msgpack_unpacked unpacked;
223
224 msgpack_unpacker_init(&unpacker, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
225 msgpack_unpacked_init(&unpacked);
226
227 msgpack_unpacker_reserve_buffer(&unpacker, length);
228 grn_memcpy(msgpack_unpacker_buffer(&unpacker), raw_value, length);
229 msgpack_unpacker_buffer_consumed(&unpacker, length);
230 while (MSGPACK_UNPACKER_NEXT(&unpacker, &unpacked)) {
231 msgpack_object *object = &(unpacked.data);
232 if (object->type == MSGPACK_OBJECT_MAP) {
233 msgpack_object_map *map = &(object->via.map);
234 uint32_t i;
235 bool have_same_key = false;
236 uint32_t same_key_index = 0;
237
238 for (i = 0; i < map->size; i++) {
239 msgpack_object_kv *kv = &(map->ptr[i]);
240 msgpack_object *key = &(kv->key);
241 if (key->type == MSGPACK_OBJECT_STR &&
242 MSGPACK_OBJECT_STR_SIZE(key) == (size_t)name_length &&
243 memcmp(name, MSGPACK_OBJECT_STR_PTR(key), (size_t)name_length) == 0) {
244 have_same_key = true;
245 same_key_index = i;
246 break;
247 }
248 }
249 if (have_same_key) {
250 msgpack_pack_map(&packer, map->size);
251 } else {
252 msgpack_pack_map(&packer, map->size + 1);
253 }
254 for (i = 0; i < map->size; i++) {
255 msgpack_object_kv *kv = &(map->ptr[i]);
256 if (have_same_key && i == same_key_index) {
257 continue;
258 }
259 msgpack_pack_object(&packer, kv->key);
260 msgpack_pack_object(&packer, kv->val);
261 }
262 }
263 }
264
265 msgpack_unpacked_destroy(&unpacked);
266 msgpack_unpacker_destroy(&unpacker);
267
268 grn_ja_unref(ctx, &iw);
269 } else {
270 msgpack_pack_map(&packer, 1);
271 }
272
273 msgpack_pack_str(&packer, (size_t)name_length);
274 msgpack_pack_str_body(&packer, name, (size_t)name_length);
275
276 n = grn_vector_size(ctx, values);
277 msgpack_pack_array(&packer, n);
278 for (i = 0; i < n; i++) {
279 const char *value;
280 unsigned int value_size;
281 grn_id value_domain;
282
283 value_size = grn_vector_get_element(ctx,
284 values,
285 i,
286 &value,
287 NULL,
288 &value_domain);
289 grn_msgpack_pack_raw_internal(ctx, &packer, value, value_size, value_domain);
290 if (ctx->rc != GRN_SUCCESS) {
291 break;
292 }
293 }
294
295 if (ctx->rc == GRN_SUCCESS) {
296 grn_ja_put(ctx,
297 options->values,
298 id,
299 GRN_TEXT_VALUE(&buffer),
300 (uint32_t)GRN_TEXT_LEN(&buffer),
301 GRN_OBJ_SET,
302 NULL);
303 }
304 GRN_OBJ_FIN(ctx, &buffer);
305 #else /* GRN_WITH_MESSAGE_PACK */
306 ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "[options] MessagePack is required");
307 #endif /* GRN_WITH_MESSAGE_PACK */
308 return ctx->rc;
309 }
310
311 grn_option_revision
312 grn_options_get(grn_ctx *ctx,
313 grn_options *options,
314 grn_id id,
315 const char *name,
316 int name_length,
317 grn_option_revision revision,
318 grn_obj *value)
319 {
320 #ifdef GRN_WITH_MESSAGE_PACK
321 bool found = false;
322 grn_io_win iw;
323 void *raw_value;
324 uint32_t length;
325 msgpack_unpacker unpacker;
326 msgpack_unpacked unpacked;
327
328 raw_value = grn_ja_ref(ctx, options->values, id, &iw, &length);
329 if (!raw_value) {
330 return GRN_OPTION_REVISION_NONE;
331 }
332
333 if (raw_value == revision) {
334 grn_ja_unref(ctx, &iw);
335 return GRN_OPTION_REVISION_UNCHANGED;
336 }
337
338 if (name_length < 0) {
339 name_length = (int)strlen(name);
340 }
341
342 msgpack_unpacker_init(&unpacker, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
343 msgpack_unpacker_reserve_buffer(&unpacker, length);
344 msgpack_unpacked_init(&unpacked);
345 grn_memcpy(msgpack_unpacker_buffer(&unpacker),
346 iw.addr,
347 length);
348 msgpack_unpacker_buffer_consumed(&unpacker, length);
349 while (MSGPACK_UNPACKER_NEXT(&unpacker, &unpacked)) {
350 msgpack_object *object = &(unpacked.data);
351 if (object->type == MSGPACK_OBJECT_MAP) {
352 msgpack_object_map *map = &(object->via.map);
353 uint32_t i;
354 for (i = 0; i < map->size; i++) {
355 msgpack_object_kv *kv = &(map->ptr[i]);
356 if (kv->key.type == MSGPACK_OBJECT_STR &&
357 MSGPACK_OBJECT_STR_SIZE(&(kv->key)) == (size_t)name_length &&
358 memcmp(MSGPACK_OBJECT_STR_PTR(&(kv->key)), name,
359 (size_t)name_length) == 0) {
360 if (kv->val.type == MSGPACK_OBJECT_ARRAY) {
361 grn_msgpack_unpack_array_internal(ctx,
362 &(kv->val.via.array),
363 value);
364 found = true;
365 }
366 break;
367 }
368 }
369 }
370 }
371 msgpack_unpacked_destroy(&unpacked);
372 msgpack_unpacker_destroy(&unpacker);
373
374 grn_ja_unref(ctx, &iw);
375
376 return raw_value;
377 #else /* GRN_WITH_MESSAGE_PACK */
378 return GRN_OPTION_REVISION_NONE;
379 #endif /* GRN_WITH_MESSAGE_PACK */
380 }
381
382 grn_rc
383 grn_options_clear(grn_ctx *ctx,
384 grn_options *options,
385 grn_id id)
386 {
387 return grn_ja_put(ctx, options->values, id, NULL, 0, GRN_OBJ_SET, NULL);
388 }
389
390 grn_rc
391 grn_options_wal_recover(grn_ctx *ctx, grn_options *options)
392 {
393 return grn_ja_wal_recover(ctx, options->values);
394 }
395
396 grn_rc
397 grn_options_warm(grn_ctx *ctx, grn_options *options)
398 {
399 return grn_ja_warm(ctx, options->values);
400 }