| 1 | /* |
| 2 | Copyright (C) 2016 Brazil |
| 3 | Copyright (C) 2020-2026 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_db.h" |
| 22 | #include "../grn_vector.h" |
| 23 | |
| 24 | #include <groonga/plugin.h> |
| 25 | |
| 26 | static void |
| 27 | command_object_list_dump_flags(grn_ctx *ctx, grn_obj_spec *spec) |
| 28 | { |
| 29 | grn_obj flags; |
| 30 | |
| 31 | GRN_TEXT_INIT(&flags, 0); |
| 32 | |
| 33 | switch (spec->header.type) { |
| 34 | case GRN_TABLE_HASH_KEY: |
| 35 | case GRN_TABLE_PAT_KEY: |
| 36 | case GRN_TABLE_DAT_KEY: |
| 37 | case GRN_TABLE_NO_KEY: |
| 38 | grn_dump_table_create_flags(ctx, spec->header.flags, &flags); |
| 39 | break; |
| 40 | case GRN_COLUMN_VAR_SIZE: |
| 41 | case GRN_COLUMN_FIX_SIZE: |
| 42 | case GRN_COLUMN_INDEX: |
| 43 | grn_dump_column_create_flags(ctx, spec->header.flags, &flags); |
| 44 | break; |
| 45 | case GRN_TYPE: |
| 46 | if (spec->header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 47 | GRN_TEXT_PUTS(ctx, &flags, "KEY_VAR_SIZE"); |
| 48 | } else { |
| 49 | switch (spec->header.flags & GRN_OBJ_KEY_MASK) { |
| 50 | case GRN_OBJ_KEY_UINT: |
| 51 | GRN_TEXT_PUTS(ctx, &flags, "KEY_UINT"); |
| 52 | break; |
| 53 | case GRN_OBJ_KEY_INT: |
| 54 | GRN_TEXT_PUTS(ctx, &flags, "KEY_INT"); |
| 55 | break; |
| 56 | case GRN_OBJ_KEY_FLOAT: |
| 57 | GRN_TEXT_PUTS(ctx, &flags, "KEY_FLOAT"); |
| 58 | break; |
| 59 | case GRN_OBJ_KEY_GEO_POINT: |
| 60 | GRN_TEXT_PUTS(ctx, &flags, "KEY_GEO_POINT"); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | break; |
| 65 | } |
| 66 | if (spec->header.flags & GRN_OBJ_CUSTOM_NAME) { |
| 67 | if (GRN_TEXT_LEN(&flags) > 0) { |
| 68 | GRN_TEXT_PUTS(ctx, &flags, "|"); |
| 69 | } |
| 70 | GRN_TEXT_PUTS(ctx, &flags, "CUSTOM_NAME"); |
| 71 | } |
| 72 | |
| 73 | grn_ctx_output_str(ctx, GRN_TEXT_VALUE(&flags), GRN_TEXT_LEN(&flags)); |
| 74 | |
| 75 | GRN_OBJ_FIN(ctx, &flags); |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | command_object_list_output_table_modules(grn_ctx *ctx, |
| 80 | grn_obj *spec_vector, |
| 81 | unsigned int spec_index, |
| 82 | const char *singular, |
| 83 | const char *plural) |
| 84 | { |
| 85 | const grn_id *ids; |
| 86 | int n_ids; |
| 87 | |
| 88 | unsigned int n_elements = grn_vector_size(ctx, spec_vector); |
| 89 | if (n_elements > spec_index) { |
| 90 | uint32_t element_size; |
| 91 | |
| 92 | element_size = grn_vector_get_element(ctx, |
| 93 | spec_vector, |
| 94 | spec_index, |
| 95 | (const char **)&ids, |
| 96 | NULL, |
| 97 | NULL); |
| 98 | n_ids = (int)(element_size / sizeof(grn_id)); |
| 99 | } else { |
| 100 | ids = NULL; |
| 101 | n_ids = 0; |
| 102 | } |
| 103 | |
| 104 | grn_ctx_output_cstr(ctx, plural); |
| 105 | grn_ctx_output_array_open(ctx, plural, n_ids); |
| 106 | grn_obj *db = grn_ctx_db(ctx); |
| 107 | for (int i = 0; i < n_ids; i++) { |
| 108 | grn_id id = ids[i]; |
| 109 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 110 | int name_size; |
| 111 | name_size = grn_table_get_key(ctx, db, id, name, GRN_TABLE_MAX_KEY_SIZE); |
| 112 | |
| 113 | grn_ctx_output_map_open(ctx, singular, 2); |
| 114 | { |
| 115 | grn_ctx_output_cstr(ctx, "id"); |
| 116 | grn_ctx_output_uint64(ctx, id); |
| 117 | |
| 118 | grn_ctx_output_cstr(ctx, "name"); |
| 119 | if (name_size == 0) { |
| 120 | grn_ctx_output_null(ctx); |
| 121 | } else { |
| 122 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 123 | } |
| 124 | } |
| 125 | grn_ctx_output_map_close(ctx); |
| 126 | } |
| 127 | grn_ctx_output_array_close(ctx); |
| 128 | } |
| 129 | |
| 130 | static grn_obj * |
| 131 | command_object_list(grn_ctx *ctx, |
| 132 | int nargs, |
| 133 | grn_obj **args, |
| 134 | grn_user_data *user_data) |
| 135 | { |
| 136 | grn_db *db; |
| 137 | int n_objects = 0; |
| 138 | grn_obj vector; |
| 139 | |
| 140 | db = (grn_db *)grn_ctx_db(ctx); |
| 141 | if (!db->specs) { |
| 142 | grn_ctx_output_map_open(ctx, "objects", n_objects); |
| 143 | grn_ctx_output_map_close(ctx); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | GRN_TABLE_EACH_BEGIN_FLAGS(ctx, |
| 148 | (grn_obj *)db, |
| 149 | cursor, |
| 150 | id, |
| 151 | GRN_CURSOR_BY_ID | GRN_CURSOR_ASCENDING) |
| 152 | { |
| 153 | grn_io_win jw; |
| 154 | uint32_t value_len; |
| 155 | char *value; |
| 156 | |
| 157 | value = grn_ja_ref(ctx, db->specs, id, &jw, &value_len); |
| 158 | if (value) { |
| 159 | n_objects++; |
| 160 | grn_ja_unref(ctx, &jw); |
| 161 | } |
| 162 | } |
| 163 | GRN_TABLE_EACH_END(ctx, cursor); |
| 164 | |
| 165 | GRN_OBJ_INIT(&vector, GRN_VECTOR, 0, GRN_DB_TEXT); |
| 166 | |
| 167 | grn_ctx_output_map_open(ctx, "objects", n_objects); |
| 168 | GRN_TABLE_EACH_BEGIN_FLAGS(ctx, |
| 169 | (grn_obj *)db, |
| 170 | cursor, |
| 171 | id, |
| 172 | GRN_CURSOR_BY_ID | GRN_CURSOR_ASCENDING) |
| 173 | { |
| 174 | void *name; |
| 175 | int name_size; |
| 176 | grn_io_win jw; |
| 177 | uint32_t value_len; |
| 178 | char *value; |
| 179 | unsigned int n_elements; |
| 180 | |
| 181 | value = grn_ja_ref(ctx, db->specs, id, &jw, &value_len); |
| 182 | if (!value) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | name_size = grn_table_cursor_get_key(ctx, cursor, &name); |
| 187 | |
| 188 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 189 | |
| 190 | GRN_BULK_REWIND(&vector); |
| 191 | grn_rc rc = grn_vector_unpack(ctx, &vector, value, value_len, 0, NULL); |
| 192 | if (rc != GRN_SUCCESS) { |
| 193 | grn_ctx_output_map_open(ctx, "object", 4); |
| 194 | { |
| 195 | grn_ctx_output_cstr(ctx, "id"); |
| 196 | grn_ctx_output_int64(ctx, id); |
| 197 | grn_ctx_output_cstr(ctx, "name"); |
| 198 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 199 | grn_ctx_output_cstr(ctx, "opened"); |
| 200 | grn_ctx_output_bool(ctx, grn_ctx_is_opened(ctx, id)); |
| 201 | grn_ctx_output_cstr(ctx, "value_size"); |
| 202 | grn_ctx_output_uint64(ctx, value_len); |
| 203 | } |
| 204 | grn_ctx_output_map_close(ctx); |
| 205 | goto next; |
| 206 | } |
| 207 | |
| 208 | n_elements = grn_vector_size(ctx, &vector); |
| 209 | |
| 210 | { |
| 211 | uint32_t element_size; |
| 212 | grn_obj_spec *spec; |
| 213 | int n_properties = 8; |
| 214 | bool need_sources = false; |
| 215 | bool need_generator = false; |
| 216 | bool need_token_filters = false; |
| 217 | bool need_normalizers = false; |
| 218 | bool need_extractors = false; |
| 219 | |
| 220 | element_size = grn_vector_get_element(ctx, |
| 221 | &vector, |
| 222 | GRN_SERIALIZED_SPEC_INDEX_SPEC, |
| 223 | (const char **)&spec, |
| 224 | NULL, |
| 225 | NULL); |
| 226 | if (element_size == 0) { |
| 227 | grn_ctx_output_map_open(ctx, "object", 4); |
| 228 | { |
| 229 | grn_ctx_output_cstr(ctx, "id"); |
| 230 | grn_ctx_output_int64(ctx, id); |
| 231 | grn_ctx_output_cstr(ctx, "name"); |
| 232 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 233 | grn_ctx_output_cstr(ctx, "opened"); |
| 234 | grn_ctx_output_bool(ctx, grn_ctx_is_opened(ctx, id)); |
| 235 | grn_ctx_output_cstr(ctx, "n_elements"); |
| 236 | grn_ctx_output_uint64(ctx, n_elements); |
| 237 | } |
| 238 | grn_ctx_output_map_close(ctx); |
| 239 | goto next; |
| 240 | } |
| 241 | |
| 242 | switch (spec->header.type) { |
| 243 | case GRN_COLUMN_FIX_SIZE: |
| 244 | case GRN_COLUMN_VAR_SIZE: |
| 245 | need_sources = true; |
| 246 | n_properties++; |
| 247 | need_generator = true; |
| 248 | n_properties++; |
| 249 | break; |
| 250 | case GRN_COLUMN_INDEX: |
| 251 | need_sources = true; |
| 252 | n_properties++; |
| 253 | break; |
| 254 | case GRN_TABLE_PAT_KEY: |
| 255 | case GRN_TABLE_DAT_KEY: |
| 256 | case GRN_TABLE_HASH_KEY: |
| 257 | need_token_filters = true; |
| 258 | n_properties++; |
| 259 | need_normalizers = true; |
| 260 | n_properties++; |
| 261 | need_extractors = true; |
| 262 | n_properties++; |
| 263 | break; |
| 264 | case GRN_TABLE_NO_KEY: |
| 265 | break; |
| 266 | } |
| 267 | grn_ctx_output_map_open(ctx, "object", n_properties); |
| 268 | { |
| 269 | grn_ctx_output_cstr(ctx, "id"); |
| 270 | grn_ctx_output_uint64(ctx, id); |
| 271 | |
| 272 | grn_ctx_output_cstr(ctx, "name"); |
| 273 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 274 | |
| 275 | grn_ctx_output_cstr(ctx, "opened"); |
| 276 | grn_ctx_output_bool(ctx, grn_ctx_is_opened(ctx, id)); |
| 277 | |
| 278 | grn_ctx_output_cstr(ctx, "n_elements"); |
| 279 | grn_ctx_output_uint64(ctx, n_elements); |
| 280 | |
| 281 | grn_ctx_output_cstr(ctx, "type"); |
| 282 | grn_ctx_output_map_open(ctx, "type", 2); |
| 283 | { |
| 284 | grn_ctx_output_cstr(ctx, "id"); |
| 285 | grn_ctx_output_uint64(ctx, spec->header.type); |
| 286 | grn_ctx_output_cstr(ctx, "name"); |
| 287 | grn_ctx_output_cstr(ctx, grn_obj_type_to_string(spec->header.type)); |
| 288 | } |
| 289 | grn_ctx_output_map_close(ctx); |
| 290 | |
| 291 | grn_ctx_output_cstr(ctx, "flags"); |
| 292 | grn_ctx_output_map_open(ctx, "flags", 2); |
| 293 | { |
| 294 | grn_ctx_output_cstr(ctx, "value"); |
| 295 | grn_ctx_output_uint64(ctx, spec->header.flags); |
| 296 | grn_ctx_output_cstr(ctx, "names"); |
| 297 | command_object_list_dump_flags(ctx, spec); |
| 298 | } |
| 299 | grn_ctx_output_map_close(ctx); |
| 300 | |
| 301 | grn_ctx_output_cstr(ctx, "path"); |
| 302 | char path[PATH_MAX]; |
| 303 | grn_obj_spec_get_path(ctx, spec, id, path, db, &vector); |
| 304 | size_t path_size = strlen(path); |
| 305 | if (path_size == 0) { |
| 306 | grn_ctx_output_null(ctx); |
| 307 | } else { |
| 308 | grn_ctx_output_str(ctx, path, path_size); |
| 309 | } |
| 310 | |
| 311 | switch (spec->header.type) { |
| 312 | case GRN_TYPE: |
| 313 | grn_ctx_output_cstr(ctx, "size"); |
| 314 | grn_ctx_output_uint64(ctx, spec->range); |
| 315 | break; |
| 316 | case GRN_PROC: |
| 317 | grn_ctx_output_cstr(ctx, "plugin_id"); |
| 318 | grn_ctx_output_uint64(ctx, spec->range); |
| 319 | break; |
| 320 | default: |
| 321 | grn_ctx_output_cstr(ctx, "range"); |
| 322 | grn_ctx_output_map_open(ctx, "range", 2); |
| 323 | { |
| 324 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 325 | int name_size; |
| 326 | |
| 327 | name_size = grn_table_get_key(ctx, |
| 328 | (grn_obj *)db, |
| 329 | spec->range, |
| 330 | name, |
| 331 | GRN_TABLE_MAX_KEY_SIZE); |
| 332 | |
| 333 | grn_ctx_output_cstr(ctx, "id"); |
| 334 | grn_ctx_output_uint64(ctx, spec->range); |
| 335 | |
| 336 | grn_ctx_output_cstr(ctx, "name"); |
| 337 | if (name_size == 0) { |
| 338 | grn_ctx_output_null(ctx); |
| 339 | } else { |
| 340 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 341 | } |
| 342 | } |
| 343 | grn_ctx_output_map_close(ctx); |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | if (need_sources) { |
| 348 | const grn_id *source_ids; |
| 349 | int n_source_ids; |
| 350 | int i; |
| 351 | |
| 352 | if (n_elements > GRN_SERIALIZED_SPEC_INDEX_SOURCE) { |
| 353 | uint32_t element_size; |
| 354 | |
| 355 | element_size = |
| 356 | grn_vector_get_element(ctx, |
| 357 | &vector, |
| 358 | GRN_SERIALIZED_SPEC_INDEX_SOURCE, |
| 359 | (const char **)&source_ids, |
| 360 | NULL, |
| 361 | NULL); |
| 362 | n_source_ids = (int)(element_size / sizeof(grn_id)); |
| 363 | } else { |
| 364 | source_ids = NULL; |
| 365 | n_source_ids = 0; |
| 366 | } |
| 367 | |
| 368 | grn_ctx_output_cstr(ctx, "sources"); |
| 369 | grn_ctx_output_array_open(ctx, "sources", n_source_ids); |
| 370 | for (i = 0; i < n_source_ids; i++) { |
| 371 | grn_id source_id; |
| 372 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 373 | int name_size; |
| 374 | |
| 375 | source_id = source_ids[i]; |
| 376 | name_size = grn_table_get_key(ctx, |
| 377 | (grn_obj *)db, |
| 378 | source_id, |
| 379 | name, |
| 380 | GRN_TABLE_MAX_KEY_SIZE); |
| 381 | |
| 382 | grn_ctx_output_map_open(ctx, "source", 2); |
| 383 | { |
| 384 | grn_ctx_output_cstr(ctx, "id"); |
| 385 | grn_ctx_output_uint64(ctx, source_id); |
| 386 | |
| 387 | grn_ctx_output_cstr(ctx, "name"); |
| 388 | if (name_size == 0) { |
| 389 | grn_ctx_output_null(ctx); |
| 390 | } else { |
| 391 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 392 | } |
| 393 | } |
| 394 | grn_ctx_output_map_close(ctx); |
| 395 | } |
| 396 | grn_ctx_output_array_close(ctx); |
| 397 | } |
| 398 | |
| 399 | if (need_generator) { |
| 400 | grn_raw_string generator; |
| 401 | GRN_RAW_STRING_INIT(generator); |
| 402 | if (n_elements > GRN_SERIALIZED_SPEC_INDEX_JA_GENERATOR) { |
| 403 | generator.length = |
| 404 | grn_vector_get_element(ctx, |
| 405 | &vector, |
| 406 | GRN_SERIALIZED_SPEC_INDEX_JA_GENERATOR, |
| 407 | &(generator.value), |
| 408 | NULL, |
| 409 | NULL); |
| 410 | } |
| 411 | |
| 412 | grn_ctx_output_cstr(ctx, "generator"); |
| 413 | grn_ctx_output_str(ctx, generator.value, generator.length); |
| 414 | } |
| 415 | |
| 416 | if (need_token_filters) { |
| 417 | command_object_list_output_table_modules( |
| 418 | ctx, |
| 419 | &vector, |
| 420 | GRN_SERIALIZED_SPEC_INDEX_TOKEN_FILTERS, |
| 421 | "token_filter", |
| 422 | "token_filters"); |
| 423 | } |
| 424 | |
| 425 | if (need_normalizers) { |
| 426 | command_object_list_output_table_modules( |
| 427 | ctx, |
| 428 | &vector, |
| 429 | GRN_SERIALIZED_SPEC_INDEX_NORMALIZERS, |
| 430 | "normalizer", |
| 431 | "normalizers"); |
| 432 | } |
| 433 | |
| 434 | if (need_extractors) { |
| 435 | command_object_list_output_table_modules( |
| 436 | ctx, |
| 437 | &vector, |
| 438 | GRN_SERIALIZED_SPEC_INDEX_EXTRACTORS, |
| 439 | "extractor", |
| 440 | "extractors"); |
| 441 | } |
| 442 | } |
| 443 | grn_ctx_output_map_close(ctx); |
| 444 | } |
| 445 | |
| 446 | next: |
| 447 | grn_ja_unref(ctx, &jw); |
| 448 | } |
| 449 | GRN_TABLE_EACH_END(ctx, cursor); |
| 450 | grn_ctx_output_map_close(ctx); |
| 451 | |
| 452 | GRN_OBJ_FIN(ctx, &vector); |
| 453 | |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | void |
| 458 | grn_proc_init_object_list(grn_ctx *ctx) |
| 459 | { |
| 460 | grn_plugin_command_create(ctx, |
| 461 | "object_list", |
| 462 | -1, |
| 463 | command_object_list, |
| 464 | 0, |
| 465 | NULL); |
| 466 | } |