Debian Codegraph @

lib/msgpack.c

raw symbols SCIP
1 /*
2 Copyright(C) 2018 Brazil
3 Copyright(C) 2020-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.h"
21 #include "grn_db.h"
22 #include "grn_msgpack.h"
23
24 #ifdef GRN_WITH_MESSAGE_PACK
25
26 # include <groonga/msgpack.h>
27
28 const char *
29 grn_msgpack_object_type_to_string(msgpack_object_type type)
30 {
31 const char *string = "unknown";
32 switch (type) {
33 case MSGPACK_OBJECT_NIL:
34 string = "nil";
35 break;
36 case MSGPACK_OBJECT_BOOLEAN:
37 string = "boolean";
38 break;
39 case MSGPACK_OBJECT_POSITIVE_INTEGER:
40 string = "positive-integer";
41 break;
42 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
43 string = "negative-integer";
44 break;
45 case MSGPACK_OBJECT_FLOAT32:
46 string = "float32";
47 break;
48 case MSGPACK_OBJECT_FLOAT64:
49 string = "float64";
50 break;
51 case MSGPACK_OBJECT_STR:
52 string = "string";
53 break;
54 case MSGPACK_OBJECT_ARRAY:
55 string = "array";
56 break;
57 case MSGPACK_OBJECT_MAP:
58 string = "map";
59 break;
60 case MSGPACK_OBJECT_BIN:
61 string = "binary";
62 break;
63 case MSGPACK_OBJECT_EXT:
64 string = "extension";
65 break;
66 default:
67 break;
68 }
69 return string;
70 }
71
72 grn_rc
73 grn_msgpack_pack_raw_internal(grn_ctx *ctx,
74 msgpack_packer *packer,
75 const char *value,
76 size_t value_size,
77 grn_id value_domain)
78 {
79 /* If the length is 0 and the given value is a text, set to an empty string.
80 * Otherwise, set nil. */
81 if (value_size == 0 && !grn_type_id_is_text_family(ctx, value_domain)) {
82 msgpack_pack_nil(packer);
83 return ctx->rc;
84 }
85
86 switch (value_domain) {
87 case GRN_DB_VOID:
88 msgpack_pack_nil(packer);
89 break;
90 case GRN_DB_BOOL:
91 if (*((bool *)value)) {
92 msgpack_pack_true(packer);
93 } else {
94 msgpack_pack_false(packer);
95 }
96 break;
97 case GRN_DB_INT8:
98 msgpack_pack_int8(packer, *((int8_t *)(value)));
99 break;
100 case GRN_DB_UINT8:
101 msgpack_pack_uint8(packer, *((uint8_t *)(value)));
102 break;
103 case GRN_DB_INT16:
104 msgpack_pack_int16(packer, *((int16_t *)(value)));
105 break;
106 case GRN_DB_UINT16:
107 msgpack_pack_uint16(packer, *((uint16_t *)(value)));
108 break;
109 case GRN_DB_INT32:
110 msgpack_pack_int32(packer, *((int32_t *)(value)));
111 break;
112 case GRN_DB_UINT32:
113 msgpack_pack_uint32(packer, *((uint32_t *)(value)));
114 break;
115 case GRN_DB_INT64:
116 msgpack_pack_int64(packer, *((int64_t *)(value)));
117 break;
118 case GRN_DB_UINT64:
119 msgpack_pack_uint64(packer, *((uint64_t *)(value)));
120 break;
121 case GRN_DB_FLOAT32:
122 msgpack_pack_float(packer, *((float *)(value)));
123 break;
124 case GRN_DB_FLOAT:
125 msgpack_pack_double(packer, *((double *)(value)));
126 break;
127 case GRN_DB_TIME:
128 # if MSGPACK_VERSION_MAJOR < 1
129 {
130 double time_value = (*((int64_t *)value) / GRN_TIME_USEC_PER_SEC_F);
131 msgpack_pack_double(packer, time_value);
132 }
133 # else /* MSGPACK_VERSION_MAJOR < 1 */
134 /* TODO: Use timestamp time in spec. */
135 msgpack_pack_ext(packer, sizeof(int64_t), GRN_MSGPACK_OBJECT_EXT_TIME);
136 msgpack_pack_ext_body(packer, value, sizeof(int64_t));
137 # endif /* MSGPACK_VERSION_MAJOR < 1 */
138 break;
139 case GRN_DB_SHORT_TEXT:
140 case GRN_DB_TEXT:
141 case GRN_DB_LONG_TEXT:
142 msgpack_pack_str(packer, value_size);
143 msgpack_pack_str_body(packer, value, value_size);
144 break;
145 default:
146 {
147 char domain_name[GRN_TABLE_MAX_KEY_SIZE];
148 int domain_name_size;
149
150 domain_name_size = grn_table_get_key(ctx,
151 grn_ctx_db(ctx),
152 value_domain,
153 domain_name,
154 sizeof(domain_name));
155 ERR(GRN_FUNCTION_NOT_IMPLEMENTED,
156 "[msgpack][pack] unsupported type: <%.*s>",
157 domain_name_size,
158 domain_name);
159 }
160 break;
161 }
162
163 return ctx->rc;
164 }
165
166 grn_rc
167 grn_msgpack_pack_internal(grn_ctx *ctx, msgpack_packer *packer, grn_obj *value)
168 {
169 if (value) {
170 return grn_msgpack_pack_raw_internal(ctx,
171 packer,
172 GRN_BULK_HEAD(value),
173 GRN_BULK_VSIZE(value),
174 value->header.domain);
175 } else {
176 return grn_msgpack_pack_raw_internal(ctx, packer, NULL, 0, GRN_DB_VOID);
177 }
178 }
179
180 grn_rc
181 grn_msgpack_unpack_array_internal(grn_ctx *ctx,
182 msgpack_object_array *array,
183 grn_obj *vector)
184 {
185 uint32_t i;
186
187 grn_obj_ensure_vector(ctx, vector);
188
189 for (i = 0; i < array->size && ctx->rc == GRN_SUCCESS; i++) {
190 msgpack_object *element;
191
192 element = &(array->ptr[i]);
193 switch (element->type) {
194 case MSGPACK_OBJECT_BOOLEAN:
195 {
196 bool value = element->via.boolean;
197 grn_vector_add_element(ctx,
198 vector,
199 (const char *)&value,
200 sizeof(bool),
201 0,
202 GRN_DB_BOOL);
203 }
204 break;
205 case MSGPACK_OBJECT_POSITIVE_INTEGER:
206 grn_vector_add_element(ctx,
207 vector,
208 (const char *)&(element->via.i64),
209 sizeof(int64_t),
210 0,
211 GRN_DB_INT64);
212 break;
213 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
214 grn_vector_add_element(ctx,
215 vector,
216 (const char *)&(element->via.u64),
217 sizeof(uint64_t),
218 0,
219 GRN_DB_UINT64);
220 break;
221 # ifdef MSGPACK_HAVE_FLOAT32
222 case MSGPACK_OBJECT_FLOAT32:
223 {
224 float value = (float)MSGPACK_OBJECT_FLOAT32_VALUE(element);
225 grn_vector_add_element(ctx,
226 vector,
227 (const char *)&value,
228 sizeof(float),
229 0,
230 GRN_DB_FLOAT32);
231 }
232 break;
233 # endif
234 case MSGPACK_OBJECT_FLOAT64:
235 grn_vector_add_element(
236 ctx,
237 vector,
238 (const char *)&(MSGPACK_OBJECT_FLOAT64_VALUE(element)),
239 sizeof(double),
240 0,
241 GRN_DB_FLOAT);
242 break;
243 case MSGPACK_OBJECT_STR:
244 grn_vector_add_element(ctx,
245 vector,
246 MSGPACK_OBJECT_STR_PTR(element),
247 MSGPACK_OBJECT_STR_SIZE(element),
248 0,
249 GRN_DB_TEXT);
250 break;
251 # if MSGPACK_VERSION_MAJOR >= 1
252 case MSGPACK_OBJECT_EXT:
253 if (element->via.ext.type == GRN_MSGPACK_OBJECT_EXT_TIME) {
254 grn_vector_add_element(ctx,
255 vector,
256 element->via.ext.ptr,
257 sizeof(int64_t),
258 0,
259 GRN_DB_TIME);
260 } else {
261 ERR(GRN_INVALID_ARGUMENT,
262 "[msgpack] unknown extension type: <%u>",
263 element->via.ext.type);
264 }
265 break;
266 # endif /* MSGPACK_VERSION_MAJOR >= 1 */
267 default:
268 ERR(GRN_INVALID_ARGUMENT,
269 "[msgpack] unexpected element type: <%#x>",
270 element->type);
271 break;
272 }
273 }
274
275 return ctx->rc;
276 }
277
278 # if MSGPACK_VERSION_MAJOR >= 1
279 int64_t
280 grn_msgpack_unpack_ext_time_internal(grn_ctx *ctx, msgpack_object_ext *ext)
281 {
282 if (ext->type == GRN_MSGPACK_OBJECT_EXT_TIME) {
283 return *(int64_t *)(ext->ptr);
284 } else {
285 ERR(GRN_INVALID_ARGUMENT,
286 "[msgpack] time extension type must be <%u>: <%u>",
287 GRN_MSGPACK_OBJECT_EXT_TIME,
288 ext->type);
289 return 0;
290 }
291 }
292 # endif /* MSGPACK_VERSION_MAJOR >= 1 */
293
294 grn_rc
295 grn_msgpack_pack_raw(grn_ctx *ctx,
296 msgpack_packer *packer,
297 const char *value,
298 unsigned int value_size,
299 grn_id value_domain)
300 {
301 GRN_API_ENTER;
302 grn_msgpack_pack_raw_internal(ctx, packer, value, value_size, value_domain);
303 GRN_API_RETURN(ctx->rc);
304 }
305
306 grn_rc
307 grn_msgpack_pack(grn_ctx *ctx, msgpack_packer *packer, grn_obj *value)
308 {
309 GRN_API_ENTER;
310 grn_msgpack_pack_internal(ctx, packer, value);
311 GRN_API_RETURN(ctx->rc);
312 }
313
314 grn_rc
315 grn_msgpack_unpack_array(grn_ctx *ctx,
316 msgpack_object_array *array,
317 grn_obj *vector)
318 {
319 GRN_API_ENTER;
320 grn_msgpack_unpack_array_internal(ctx, array, vector);
321 GRN_API_RETURN(ctx->rc);
322 }
323
324 # if MSGPACK_VERSION_MAJOR >= 1
325 int64_t
326 grn_msgpack_unpack_ext_time(grn_ctx *ctx, msgpack_object_ext *ext)
327 {
328 int64_t time;
329 GRN_API_ENTER;
330 time = grn_msgpack_unpack_ext_time_internal(ctx, ext);
331 GRN_API_RETURN(time);
332 }
333 # endif /* MSGPACK_VERSION_MAJOR >= 1 */
334 #endif /* GRN_WITH_MESSAGE_PACK */