| 1 | /* |
| 2 | Copyright (C) 2016-2017 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_pat.h" |
| 21 | #include "../grn_dat.h" |
| 22 | #include "../grn_ii.h" |
| 23 | |
| 24 | #include "../grn_proc.h" |
| 25 | |
| 26 | #include <groonga/plugin.h> |
| 27 | |
| 28 | static void |
| 29 | command_object_inspect_dispatch(grn_ctx *ctx, grn_obj *obj); |
| 30 | |
| 31 | static void |
| 32 | command_object_inspect_obj_name(grn_ctx *ctx, grn_obj *obj) |
| 33 | { |
| 34 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 35 | int name_size; |
| 36 | |
| 37 | name_size = grn_obj_name(ctx, obj, name, GRN_TABLE_MAX_KEY_SIZE); |
| 38 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 39 | } |
| 40 | |
| 41 | static void |
| 42 | command_object_inspect_obj_type(grn_ctx *ctx, uint8_t type) |
| 43 | { |
| 44 | grn_ctx_output_map_open(ctx, "type", 2); |
| 45 | { |
| 46 | grn_ctx_output_cstr(ctx, "id"); |
| 47 | grn_ctx_output_uint64(ctx, type); |
| 48 | grn_ctx_output_cstr(ctx, "name"); |
| 49 | grn_ctx_output_cstr(ctx, grn_obj_type_to_string(type)); |
| 50 | } |
| 51 | grn_ctx_output_map_close(ctx); |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | command_object_inspect_type(grn_ctx *ctx, grn_obj *type) |
| 56 | { |
| 57 | if (!type) { |
| 58 | grn_ctx_output_null(ctx); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | grn_ctx_output_map_open(ctx, "type", 4); |
| 63 | { |
| 64 | grn_ctx_output_cstr(ctx, "id"); |
| 65 | grn_ctx_output_uint64(ctx, grn_obj_id(ctx, type)); |
| 66 | grn_ctx_output_cstr(ctx, "name"); |
| 67 | command_object_inspect_obj_name(ctx, type); |
| 68 | grn_ctx_output_cstr(ctx, "type"); |
| 69 | command_object_inspect_obj_type(ctx, type->header.type); |
| 70 | grn_ctx_output_cstr(ctx, "size"); |
| 71 | if (type->header.type == GRN_TYPE) { |
| 72 | grn_ctx_output_uint64(ctx, grn_type_size(ctx, type)); |
| 73 | } else { |
| 74 | grn_ctx_output_uint64(ctx, sizeof(grn_id)); |
| 75 | } |
| 76 | } |
| 77 | grn_ctx_output_map_close(ctx); |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | command_object_inspect_disk_usage(grn_ctx *ctx, grn_obj *obj) |
| 82 | { |
| 83 | grn_ctx_output_uint64(ctx, grn_obj_get_disk_usage(ctx, obj)); |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | command_object_inspect_table_hash_key_key(grn_ctx *ctx, grn_hash *hash) |
| 88 | { |
| 89 | grn_ctx_output_map_open(ctx, "key", 3); |
| 90 | { |
| 91 | grn_ctx_output_cstr(ctx, "type"); |
| 92 | grn_obj *type = grn_ctx_at(ctx, hash->obj.header.domain); |
| 93 | command_object_inspect_type(ctx, type); |
| 94 | grn_obj_unref(ctx, type); |
| 95 | grn_ctx_output_cstr(ctx, "total_size"); |
| 96 | grn_ctx_output_uint64(ctx, grn_hash_total_key_size(ctx, hash)); |
| 97 | grn_ctx_output_cstr(ctx, "max_total_size"); |
| 98 | grn_ctx_output_uint64(ctx, grn_hash_max_total_key_size(ctx, hash)); |
| 99 | } |
| 100 | grn_ctx_output_map_close(ctx); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | command_object_inspect_table_pat_key_key(grn_ctx *ctx, grn_pat *pat) |
| 105 | { |
| 106 | grn_ctx_output_map_open(ctx, "key", 3); |
| 107 | { |
| 108 | grn_ctx_output_cstr(ctx, "type"); |
| 109 | grn_obj *type = grn_ctx_at(ctx, pat->obj.header.domain); |
| 110 | command_object_inspect_type(ctx, type); |
| 111 | grn_obj_unref(ctx, type); |
| 112 | grn_ctx_output_cstr(ctx, "total_size"); |
| 113 | grn_ctx_output_uint64(ctx, grn_pat_total_key_size(ctx, pat)); |
| 114 | grn_ctx_output_cstr(ctx, "max_total_size"); |
| 115 | grn_ctx_output_uint64(ctx, grn_pat_max_total_key_size(ctx, pat)); |
| 116 | } |
| 117 | grn_ctx_output_map_close(ctx); |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | command_object_inspect_table_dat_key_key(grn_ctx *ctx, grn_dat *dat) |
| 122 | { |
| 123 | grn_ctx_output_map_open(ctx, "key", 1); |
| 124 | { |
| 125 | grn_ctx_output_cstr(ctx, "type"); |
| 126 | grn_obj *type = grn_ctx_at(ctx, dat->obj.header.domain); |
| 127 | command_object_inspect_type(ctx, type); |
| 128 | grn_obj_unref(ctx, type); |
| 129 | } |
| 130 | grn_ctx_output_map_close(ctx); |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | command_object_inspect_table_key(grn_ctx *ctx, grn_obj *table) |
| 135 | { |
| 136 | switch (table->header.type) { |
| 137 | case GRN_TABLE_HASH_KEY: |
| 138 | command_object_inspect_table_hash_key_key(ctx, (grn_hash *)table); |
| 139 | break; |
| 140 | case GRN_TABLE_PAT_KEY: |
| 141 | command_object_inspect_table_pat_key_key(ctx, (grn_pat *)table); |
| 142 | break; |
| 143 | case GRN_TABLE_DAT_KEY: |
| 144 | command_object_inspect_table_dat_key_key(ctx, (grn_dat *)table); |
| 145 | break; |
| 146 | case GRN_TABLE_NO_KEY: |
| 147 | grn_ctx_output_null(ctx); |
| 148 | break; |
| 149 | default: |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | command_object_inspect_table_value(grn_ctx *ctx, grn_obj *table) |
| 156 | { |
| 157 | if (table->header.type == GRN_TABLE_DAT_KEY) { |
| 158 | grn_ctx_output_null(ctx); |
| 159 | } else { |
| 160 | grn_ctx_output_map_open(ctx, "value", 1); |
| 161 | { |
| 162 | grn_id range_id = grn_obj_get_range(ctx, table); |
| 163 | grn_ctx_output_cstr(ctx, "type"); |
| 164 | grn_obj *type = grn_ctx_at(ctx, range_id); |
| 165 | command_object_inspect_type(ctx, type); |
| 166 | grn_obj_unref(ctx, type); |
| 167 | } |
| 168 | grn_ctx_output_map_close(ctx); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | command_object_inspect_table(grn_ctx *ctx, grn_obj *obj) |
| 174 | { |
| 175 | grn_ctx_output_map_open(ctx, "table", 7); |
| 176 | { |
| 177 | grn_ctx_output_cstr(ctx, "id"); |
| 178 | grn_ctx_output_uint64(ctx, grn_obj_id(ctx, obj)); |
| 179 | grn_ctx_output_cstr(ctx, "name"); |
| 180 | command_object_inspect_obj_name(ctx, obj); |
| 181 | grn_ctx_output_cstr(ctx, "type"); |
| 182 | command_object_inspect_obj_type(ctx, obj->header.type); |
| 183 | grn_ctx_output_cstr(ctx, "key"); |
| 184 | command_object_inspect_table_key(ctx, obj); |
| 185 | grn_ctx_output_cstr(ctx, "value"); |
| 186 | command_object_inspect_table_value(ctx, obj); |
| 187 | grn_ctx_output_cstr(ctx, "n_records"); |
| 188 | grn_ctx_output_uint64(ctx, grn_table_size(ctx, obj)); |
| 189 | grn_ctx_output_cstr(ctx, "disk_usage"); |
| 190 | command_object_inspect_disk_usage(ctx, obj); |
| 191 | } |
| 192 | grn_ctx_output_map_close(ctx); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | command_object_inspect_column_name(grn_ctx *ctx, grn_obj *column) |
| 197 | { |
| 198 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 199 | int name_size; |
| 200 | |
| 201 | name_size = grn_column_name(ctx, column, name, GRN_TABLE_MAX_KEY_SIZE); |
| 202 | name[name_size] = '\0'; |
| 203 | grn_ctx_output_str(ctx, name, (size_t)name_size); |
| 204 | } |
| 205 | |
| 206 | static void |
| 207 | command_object_inspect_column_type_name(grn_ctx *ctx, grn_obj *column) |
| 208 | { |
| 209 | switch (column->header.type) { |
| 210 | case GRN_COLUMN_FIX_SIZE: |
| 211 | case GRN_COLUMN_VAR_SIZE: |
| 212 | switch (column->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) { |
| 213 | case GRN_OBJ_COLUMN_SCALAR: |
| 214 | grn_ctx_output_cstr(ctx, "scalar"); |
| 215 | break; |
| 216 | case GRN_OBJ_COLUMN_VECTOR: |
| 217 | grn_ctx_output_cstr(ctx, "vector"); |
| 218 | break; |
| 219 | } |
| 220 | break; |
| 221 | case GRN_COLUMN_INDEX: |
| 222 | grn_ctx_output_cstr(ctx, "index"); |
| 223 | break; |
| 224 | default: |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | command_object_inspect_column_type(grn_ctx *ctx, grn_obj *column) |
| 231 | { |
| 232 | grn_ctx_output_map_open(ctx, "type", 2); |
| 233 | { |
| 234 | grn_ctx_output_cstr(ctx, "name"); |
| 235 | command_object_inspect_column_type_name(ctx, column); |
| 236 | |
| 237 | grn_ctx_output_cstr(ctx, "raw"); |
| 238 | grn_ctx_output_map_open(ctx, "raw", 2); |
| 239 | { |
| 240 | grn_ctx_output_cstr(ctx, "id"); |
| 241 | grn_ctx_output_uint64(ctx, column->header.type); |
| 242 | grn_ctx_output_cstr(ctx, "name"); |
| 243 | grn_ctx_output_cstr(ctx, grn_obj_type_to_string(column->header.type)); |
| 244 | } |
| 245 | grn_ctx_output_map_close(ctx); |
| 246 | } |
| 247 | grn_ctx_output_map_close(ctx); |
| 248 | } |
| 249 | |
| 250 | static void |
| 251 | command_object_inspect_column_index_value_statistics(grn_ctx *ctx, grn_ii *ii) |
| 252 | { |
| 253 | grn_ctx_output_map_open(ctx, "statistics", 13); |
| 254 | { |
| 255 | grn_ii_header_common *h = ii->header.common; |
| 256 | |
| 257 | grn_ctx_output_cstr(ctx, "max_section_id"); |
| 258 | grn_ctx_output_uint64(ctx, grn_ii_max_section(ii)); |
| 259 | |
| 260 | { |
| 261 | uint32_t max_id = 0; |
| 262 | uint32_t n_garbage_segments = 0; |
| 263 | uint32_t n_array_segments = 0; |
| 264 | uint32_t n_buffer_segments = 0; |
| 265 | |
| 266 | grn_ctx_output_cstr(ctx, "n_garbage_segments"); |
| 267 | { |
| 268 | uint32_t i; |
| 269 | |
| 270 | for (i = h->bgqtail; i != h->bgqhead; |
| 271 | i = ((i + 1) & (GRN_II_BGQSIZE - 1))) { |
| 272 | uint32_t id = h->bgqbody[i]; |
| 273 | n_garbage_segments++; |
| 274 | if (id > max_id) { |
| 275 | max_id = id; |
| 276 | } |
| 277 | } |
| 278 | grn_ctx_output_uint64(ctx, n_garbage_segments); |
| 279 | } |
| 280 | |
| 281 | const uint32_t n_logical_segments = grn_ii_n_logical_segments(ii); |
| 282 | grn_ctx_output_cstr(ctx, "max_array_segment_id"); |
| 283 | grn_ctx_output_uint64(ctx, h->amax); |
| 284 | grn_ctx_output_cstr(ctx, "n_array_segments"); |
| 285 | { |
| 286 | for (uint32_t lseg = 0; lseg < n_logical_segments; lseg++) { |
| 287 | const uint32_t pseg = grn_ii_get_array_pseg(ii, lseg); |
| 288 | if (pseg != GRN_II_PSEG_NOT_ASSIGNED) { |
| 289 | if (pseg > max_id) { |
| 290 | max_id = pseg; |
| 291 | } |
| 292 | n_array_segments++; |
| 293 | } |
| 294 | } |
| 295 | grn_ctx_output_uint64(ctx, n_array_segments); |
| 296 | } |
| 297 | |
| 298 | grn_ctx_output_cstr(ctx, "max_buffer_segment_id"); |
| 299 | grn_ctx_output_uint64(ctx, h->bmax); |
| 300 | grn_ctx_output_cstr(ctx, "n_buffer_segments"); |
| 301 | { |
| 302 | for (uint32_t lseg = 0; lseg < n_logical_segments; lseg++) { |
| 303 | const uint32_t pseg = grn_ii_get_buffer_pseg(ii, lseg); |
| 304 | if (pseg != GRN_II_PSEG_NOT_ASSIGNED) { |
| 305 | if (pseg > max_id) { |
| 306 | max_id = pseg; |
| 307 | } |
| 308 | n_buffer_segments++; |
| 309 | } |
| 310 | } |
| 311 | grn_ctx_output_uint64(ctx, n_buffer_segments); |
| 312 | } |
| 313 | |
| 314 | grn_ctx_output_cstr(ctx, "max_in_use_physical_segment_id"); |
| 315 | grn_ctx_output_uint64(ctx, max_id); |
| 316 | |
| 317 | grn_ctx_output_cstr(ctx, "n_unmanaged_segments"); |
| 318 | grn_ctx_output_uint64(ctx, |
| 319 | h->pnext - n_array_segments - n_buffer_segments - |
| 320 | n_garbage_segments); |
| 321 | } |
| 322 | |
| 323 | { |
| 324 | grn_ctx_output_cstr(ctx, "total_chunk_size"); |
| 325 | grn_ctx_output_uint64(ctx, h->total_chunk_size); |
| 326 | grn_ctx_output_cstr(ctx, "max_in_use_chunk_id"); |
| 327 | { |
| 328 | uint32_t i; |
| 329 | uint32_t max_id; |
| 330 | |
| 331 | for (max_id = 0, i = 0; i < (GRN_II_MAX_CHUNK >> 3); i++) { |
| 332 | uint8_t sub_chunk_info = h->chunks[i]; |
| 333 | uint8_t bit; |
| 334 | |
| 335 | if (sub_chunk_info == 0) { |
| 336 | continue; |
| 337 | } |
| 338 | for (bit = 0; bit < 8; bit++) { |
| 339 | if (sub_chunk_info & (1 << bit)) { |
| 340 | max_id = (i << 3) + sub_chunk_info; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | grn_ctx_output_uint64(ctx, max_id); |
| 345 | } |
| 346 | grn_ctx_output_cstr(ctx, "n_garbage_chunks"); |
| 347 | grn_ctx_output_array_open(ctx, |
| 348 | "n_garbage_chunks", |
| 349 | GRN_II_N_CHUNK_VARIATION + 1); |
| 350 | { |
| 351 | uint32_t i; |
| 352 | for (i = 0; i <= GRN_II_N_CHUNK_VARIATION; i++) { |
| 353 | grn_ctx_output_uint64(ctx, h->ngarbages[i]); |
| 354 | } |
| 355 | } |
| 356 | grn_ctx_output_array_close(ctx); |
| 357 | } |
| 358 | |
| 359 | grn_ctx_output_cstr(ctx, "next_physical_segment_id"); |
| 360 | grn_ctx_output_uint64(ctx, h->pnext); |
| 361 | |
| 362 | grn_ctx_output_cstr(ctx, "max_n_physical_segments"); |
| 363 | grn_ctx_output_uint64(ctx, ii->seg->header->max_segment); |
| 364 | } |
| 365 | grn_ctx_output_map_close(ctx); |
| 366 | } |
| 367 | |
| 368 | static void |
| 369 | command_object_inspect_column_data_value_compress(grn_ctx *ctx, grn_obj *column) |
| 370 | { |
| 371 | const char *compress = NULL; |
| 372 | grn_column_flags column_flags; |
| 373 | |
| 374 | column_flags = grn_column_get_flags(ctx, column); |
| 375 | switch (column_flags & GRN_OBJ_COMPRESS_MASK) { |
| 376 | case GRN_OBJ_COMPRESS_ZLIB: |
| 377 | compress = "zlib"; |
| 378 | break; |
| 379 | case GRN_OBJ_COMPRESS_LZ4: |
| 380 | compress = "lz4"; |
| 381 | break; |
| 382 | case GRN_OBJ_COMPRESS_ZSTD: |
| 383 | compress = "zstd"; |
| 384 | break; |
| 385 | case GRN_OBJ_COMPRESS_OPENZL: |
| 386 | compress = "openzl"; |
| 387 | break; |
| 388 | default: |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | if (compress) { |
| 393 | grn_ctx_output_cstr(ctx, compress); |
| 394 | } else { |
| 395 | grn_ctx_output_null(ctx); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | command_object_inspect_column_data_value_compress_filters(grn_ctx *ctx, |
| 401 | grn_obj *column) |
| 402 | { |
| 403 | grn_column_flags column_flags = grn_column_get_flags(ctx, column); |
| 404 | uint32_t n_filters = 0; |
| 405 | if (column_flags & GRN_OBJ_COMPRESS_FILTER_SHUFFLE) { |
| 406 | n_filters++; |
| 407 | } |
| 408 | if (column_flags & GRN_OBJ_COMPRESS_FILTER_BYTE_DELTA) { |
| 409 | n_filters++; |
| 410 | } |
| 411 | grn_ctx_output_array_open(ctx, "compress_filters", n_filters); |
| 412 | if (column_flags & GRN_OBJ_COMPRESS_FILTER_SHUFFLE) { |
| 413 | grn_ctx_output_cstr(ctx, "shuffle"); |
| 414 | } |
| 415 | if (column_flags & GRN_OBJ_COMPRESS_FILTER_BYTE_DELTA) { |
| 416 | grn_ctx_output_cstr(ctx, "byte_delta"); |
| 417 | } |
| 418 | if (column_flags & GRN_OBJ_COMPRESS_FILTER_TRUNCATE_PRECISION_2BYTES) { |
| 419 | grn_ctx_output_cstr(ctx, "truncate_precision_2bytes"); |
| 420 | } else if (column_flags & GRN_OBJ_COMPRESS_FILTER_TRUNCATE_PRECISION_1BYTE) { |
| 421 | grn_ctx_output_cstr(ctx, "truncate_precision_1byte"); |
| 422 | } |
| 423 | grn_ctx_output_array_close(ctx); |
| 424 | } |
| 425 | |
| 426 | static void |
| 427 | command_object_inspect_column_value(grn_ctx *ctx, grn_obj *column) |
| 428 | { |
| 429 | int n_elements = 1; |
| 430 | grn_column_flags column_flags = grn_column_get_flags(ctx, column); |
| 431 | bool is_index = false; |
| 432 | bool is_vector = false; |
| 433 | switch (column_flags & GRN_OBJ_COLUMN_TYPE_MASK) { |
| 434 | case GRN_OBJ_COLUMN_VECTOR: |
| 435 | is_vector = true; |
| 436 | break; |
| 437 | case GRN_OBJ_COLUMN_INDEX: |
| 438 | is_index = true; |
| 439 | break; |
| 440 | default: |
| 441 | break; |
| 442 | } |
| 443 | bool is_generated_column = grn_obj_is_generated_column(ctx, column); |
| 444 | |
| 445 | if (is_index) { |
| 446 | n_elements += 5; |
| 447 | } else { |
| 448 | n_elements += 2; |
| 449 | if (is_vector) { |
| 450 | n_elements += 3; |
| 451 | } |
| 452 | if (is_generated_column) { |
| 453 | n_elements += 1; |
| 454 | } |
| 455 | } |
| 456 | grn_ctx_output_map_open(ctx, "value", n_elements); |
| 457 | { |
| 458 | grn_id range_id; |
| 459 | |
| 460 | range_id = grn_obj_get_range(ctx, column); |
| 461 | |
| 462 | grn_ctx_output_cstr(ctx, "type"); |
| 463 | grn_obj *range = grn_ctx_at(ctx, range_id); |
| 464 | command_object_inspect_type(ctx, range); |
| 465 | grn_obj_unref(ctx, range); |
| 466 | if (is_index) { |
| 467 | grn_ctx_output_cstr(ctx, "section"); |
| 468 | grn_ctx_output_bool(ctx, (column_flags & GRN_OBJ_WITH_SECTION) != 0); |
| 469 | grn_ctx_output_cstr(ctx, "weight"); |
| 470 | grn_ctx_output_bool(ctx, (column_flags & GRN_OBJ_WITH_WEIGHT) != 0); |
| 471 | grn_ctx_output_cstr(ctx, "position"); |
| 472 | grn_ctx_output_bool(ctx, (column_flags & GRN_OBJ_WITH_POSITION) != 0); |
| 473 | grn_ctx_output_cstr(ctx, "size"); |
| 474 | if ((column_flags & GRN_OBJ_INDEX_SMALL) != 0) { |
| 475 | grn_ctx_output_cstr(ctx, "small"); |
| 476 | } else if ((column_flags & GRN_OBJ_INDEX_MEDIUM) != 0) { |
| 477 | grn_ctx_output_cstr(ctx, "medium"); |
| 478 | } else if ((column_flags & GRN_OBJ_INDEX_LARGE) != 0) { |
| 479 | grn_ctx_output_cstr(ctx, "large"); |
| 480 | } else { |
| 481 | grn_ctx_output_cstr(ctx, "normal"); |
| 482 | } |
| 483 | grn_ctx_output_cstr(ctx, "statistics"); |
| 484 | command_object_inspect_column_index_value_statistics(ctx, |
| 485 | (grn_ii *)column); |
| 486 | } else { |
| 487 | grn_ctx_output_cstr(ctx, "compress"); |
| 488 | command_object_inspect_column_data_value_compress(ctx, column); |
| 489 | if (is_vector) { |
| 490 | grn_ctx_output_cstr(ctx, "weight"); |
| 491 | grn_ctx_output_bool(ctx, (column_flags & GRN_OBJ_WITH_WEIGHT) != 0); |
| 492 | grn_ctx_output_cstr(ctx, "weight_float32"); |
| 493 | grn_ctx_output_bool(ctx, (column_flags & GRN_OBJ_WEIGHT_FLOAT32) != 0); |
| 494 | grn_ctx_output_cstr(ctx, "weight_bfloat16"); |
| 495 | grn_ctx_output_bool(ctx, (column_flags & GRN_OBJ_WEIGHT_BFLOAT16) != 0); |
| 496 | } |
| 497 | grn_ctx_output_cstr(ctx, "compress_filters"); |
| 498 | command_object_inspect_column_data_value_compress_filters(ctx, column); |
| 499 | if (is_generated_column) { |
| 500 | grn_ctx_output_cstr(ctx, "generator"); |
| 501 | grn_obj generator; |
| 502 | GRN_TEXT_INIT(&generator, 0); |
| 503 | grn_obj_get_info(ctx, column, GRN_INFO_GENERATOR, &generator); |
| 504 | grn_ctx_output_str(ctx, |
| 505 | GRN_TEXT_VALUE(&generator), |
| 506 | GRN_TEXT_LEN(&generator)); |
| 507 | GRN_OBJ_FIN(ctx, &generator); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | grn_ctx_output_map_close(ctx); |
| 512 | } |
| 513 | |
| 514 | static void |
| 515 | command_object_inspect_column_sources(grn_ctx *ctx, grn_obj *column) |
| 516 | { |
| 517 | const bool is_generated_column = grn_obj_is_generated_column(ctx, column); |
| 518 | grn_obj *source_table; |
| 519 | if (is_generated_column) { |
| 520 | source_table = grn_ctx_at(ctx, column->header.domain); |
| 521 | } else { |
| 522 | source_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, column)); |
| 523 | } |
| 524 | |
| 525 | grn_obj source_ids; |
| 526 | GRN_RECORD_INIT(&source_ids, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 527 | grn_obj_get_info(ctx, column, GRN_INFO_SOURCE, &source_ids); |
| 528 | |
| 529 | int n_ids = (int)(GRN_BULK_VSIZE(&source_ids) / sizeof(grn_id)); |
| 530 | grn_ctx_output_array_open(ctx, "sources", n_ids); |
| 531 | int i; |
| 532 | for (i = 0; i < n_ids; i++) { |
| 533 | grn_id source_id; |
| 534 | grn_obj *source; |
| 535 | |
| 536 | source_id = GRN_RECORD_VALUE_AT(&source_ids, i); |
| 537 | source = grn_ctx_at(ctx, source_id); |
| 538 | |
| 539 | grn_ctx_output_map_open(ctx, "source", 4); |
| 540 | { |
| 541 | grn_ctx_output_cstr(ctx, "id"); |
| 542 | if (grn_obj_is_table(ctx, source)) { |
| 543 | grn_ctx_output_null(ctx); |
| 544 | } else { |
| 545 | grn_ctx_output_uint64(ctx, source_id); |
| 546 | } |
| 547 | |
| 548 | grn_ctx_output_cstr(ctx, "name"); |
| 549 | if (grn_obj_is_table(ctx, source)) { |
| 550 | grn_ctx_output_cstr(ctx, "_key"); |
| 551 | } else { |
| 552 | command_object_inspect_column_name(ctx, source); |
| 553 | } |
| 554 | |
| 555 | grn_ctx_output_cstr(ctx, "table"); |
| 556 | command_object_inspect_table(ctx, source_table); |
| 557 | |
| 558 | grn_ctx_output_cstr(ctx, "full_name"); |
| 559 | if (grn_obj_is_table(ctx, source)) { |
| 560 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 561 | int name_size; |
| 562 | name_size = grn_obj_name(ctx, source, name, GRN_TABLE_MAX_KEY_SIZE); |
| 563 | name[name_size] = '\0'; |
| 564 | grn_strcat(name, GRN_TABLE_MAX_KEY_SIZE, "._key"); |
| 565 | grn_ctx_output_cstr(ctx, name); |
| 566 | } else { |
| 567 | command_object_inspect_obj_name(ctx, source); |
| 568 | } |
| 569 | } |
| 570 | grn_ctx_output_map_close(ctx); |
| 571 | |
| 572 | grn_obj_unref(ctx, source); |
| 573 | } |
| 574 | grn_ctx_output_array_close(ctx); |
| 575 | |
| 576 | GRN_OBJ_FIN(ctx, &source_ids); |
| 577 | |
| 578 | grn_obj_unref(ctx, source_table); |
| 579 | } |
| 580 | |
| 581 | static void |
| 582 | command_object_inspect_column(grn_ctx *ctx, grn_obj *column) |
| 583 | { |
| 584 | int n_elements = 7; |
| 585 | bool have_sources = (grn_obj_is_index_column(ctx, column) || |
| 586 | grn_obj_is_token_column(ctx, column) || |
| 587 | grn_obj_is_generated_column(ctx, column)); |
| 588 | |
| 589 | if (have_sources) { |
| 590 | n_elements += 1; |
| 591 | } |
| 592 | grn_ctx_output_map_open(ctx, "column", n_elements); |
| 593 | { |
| 594 | grn_ctx_output_cstr(ctx, "id"); |
| 595 | grn_ctx_output_uint64(ctx, grn_obj_id(ctx, column)); |
| 596 | grn_ctx_output_cstr(ctx, "name"); |
| 597 | command_object_inspect_column_name(ctx, column); |
| 598 | grn_ctx_output_cstr(ctx, "table"); |
| 599 | grn_obj *table = grn_ctx_at(ctx, column->header.domain); |
| 600 | command_object_inspect_table(ctx, table); |
| 601 | grn_obj_unref(ctx, table); |
| 602 | grn_ctx_output_cstr(ctx, "full_name"); |
| 603 | command_object_inspect_obj_name(ctx, column); |
| 604 | grn_ctx_output_cstr(ctx, "type"); |
| 605 | command_object_inspect_column_type(ctx, column); |
| 606 | grn_ctx_output_cstr(ctx, "value"); |
| 607 | command_object_inspect_column_value(ctx, column); |
| 608 | if (have_sources) { |
| 609 | grn_ctx_output_cstr(ctx, "sources"); |
| 610 | command_object_inspect_column_sources(ctx, column); |
| 611 | } |
| 612 | grn_ctx_output_cstr(ctx, "disk_usage"); |
| 613 | command_object_inspect_disk_usage(ctx, column); |
| 614 | } |
| 615 | grn_ctx_output_map_close(ctx); |
| 616 | } |
| 617 | |
| 618 | static void |
| 619 | command_object_inspect_db(grn_ctx *ctx, grn_obj *obj) |
| 620 | { |
| 621 | grn_db *db = (grn_db *)obj; |
| 622 | |
| 623 | grn_ctx_output_map_open(ctx, "database", 3); |
| 624 | { |
| 625 | grn_ctx_output_cstr(ctx, "type"); |
| 626 | command_object_inspect_obj_type(ctx, obj->header.type); |
| 627 | grn_ctx_output_cstr(ctx, "name_table"); |
| 628 | command_object_inspect_dispatch(ctx, db->keys); |
| 629 | grn_ctx_output_cstr(ctx, "disk_usage"); |
| 630 | command_object_inspect_disk_usage(ctx, obj); |
| 631 | } |
| 632 | grn_ctx_output_map_close(ctx); |
| 633 | } |
| 634 | |
| 635 | static void |
| 636 | command_object_inspect_dispatch(grn_ctx *ctx, grn_obj *obj) |
| 637 | { |
| 638 | switch (obj->header.type) { |
| 639 | case GRN_TYPE: |
| 640 | command_object_inspect_type(ctx, obj); |
| 641 | break; |
| 642 | case GRN_TABLE_HASH_KEY: |
| 643 | case GRN_TABLE_PAT_KEY: |
| 644 | case GRN_TABLE_DAT_KEY: |
| 645 | case GRN_TABLE_NO_KEY: |
| 646 | command_object_inspect_table(ctx, obj); |
| 647 | break; |
| 648 | case GRN_COLUMN_FIX_SIZE: |
| 649 | case GRN_COLUMN_VAR_SIZE: |
| 650 | case GRN_COLUMN_INDEX: |
| 651 | command_object_inspect_column(ctx, obj); |
| 652 | break; |
| 653 | case GRN_DB: |
| 654 | command_object_inspect_db(ctx, obj); |
| 655 | break; |
| 656 | default: |
| 657 | { |
| 658 | GRN_PLUGIN_ERROR(ctx, |
| 659 | GRN_FUNCTION_NOT_IMPLEMENTED, |
| 660 | "[object][inspect] unsupported type: <%s>(%#x)", |
| 661 | grn_obj_type_to_string(obj->header.type), |
| 662 | obj->header.type); |
| 663 | grn_ctx_output_null(ctx); |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | static grn_obj * |
| 670 | command_object_inspect(grn_ctx *ctx, |
| 671 | int nargs, |
| 672 | grn_obj **args, |
| 673 | grn_user_data *user_data) |
| 674 | { |
| 675 | grn_obj *name; |
| 676 | grn_obj *target; |
| 677 | bool need_unref = false; |
| 678 | |
| 679 | name = grn_plugin_proc_get_var(ctx, user_data, "name", -1); |
| 680 | if (GRN_TEXT_LEN(name) == 0) { |
| 681 | target = grn_ctx_db(ctx); |
| 682 | } else { |
| 683 | target = grn_ctx_get(ctx, GRN_TEXT_VALUE(name), (int)GRN_TEXT_LEN(name)); |
| 684 | if (!target) { |
| 685 | GRN_PLUGIN_ERROR(ctx, |
| 686 | GRN_INVALID_ARGUMENT, |
| 687 | "[object][inspect] nonexistent target: <%.*s>", |
| 688 | (int)GRN_TEXT_LEN(name), |
| 689 | GRN_TEXT_VALUE(name)); |
| 690 | grn_ctx_output_null(ctx); |
| 691 | return NULL; |
| 692 | } |
| 693 | need_unref = true; |
| 694 | } |
| 695 | |
| 696 | command_object_inspect_dispatch(ctx, target); |
| 697 | if (need_unref) { |
| 698 | grn_obj_unref(ctx, target); |
| 699 | } |
| 700 | |
| 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | void |
| 705 | grn_proc_init_object_inspect(grn_ctx *ctx) |
| 706 | { |
| 707 | grn_expr_var vars[1]; |
| 708 | |
| 709 | grn_plugin_expr_var_init(ctx, &(vars[0]), "name", -1); |
| 710 | grn_plugin_command_create(ctx, |
| 711 | "object_inspect", |
| 712 | -1, |
| 713 | command_object_inspect, |
| 714 | 1, |
| 715 | vars); |
| 716 | } |