| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-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_ctx_impl.h" |
| 22 | #include "../grn_db.h" |
| 23 | #include "../grn_str.h" |
| 24 | |
| 25 | #include <groonga/plugin.h> |
| 26 | |
| 27 | static const size_t DUMP_FLUSH_THRESHOLD_SIZE = 256 * 1024; |
| 28 | |
| 29 | typedef struct { |
| 30 | bool is_referred; |
| 31 | bool have_data_column; |
| 32 | bool have_reference_column; |
| 33 | } grn_dumper_table_statistics; |
| 34 | |
| 35 | static void |
| 36 | grn_dumper_table_statistics_fin(grn_ctx *ctx, |
| 37 | grn_dumper_table_statistics *statistics) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | typedef struct { |
| 42 | grn_obj *output; |
| 43 | bool is_close_opened_object_mode; |
| 44 | bool have_reference_column; |
| 45 | bool have_index_column; |
| 46 | bool is_sort_hash_table; |
| 47 | bool is_dump_paths; |
| 48 | grn_obj column_name_buffer; |
| 49 | grn_hash *table_statistics; |
| 50 | } grn_dumper; |
| 51 | |
| 52 | static void |
| 53 | dumper_collect_statistics_table(grn_ctx *ctx, |
| 54 | grn_dumper *dumper, |
| 55 | grn_obj *table) |
| 56 | { |
| 57 | grn_dumper_table_statistics *statistics = NULL; |
| 58 | void *value; |
| 59 | if (grn_hash_add(ctx, |
| 60 | dumper->table_statistics, |
| 61 | &(DB_OBJ(table)->id), |
| 62 | sizeof(grn_id), |
| 63 | &value, |
| 64 | NULL) != GRN_ID_NIL) { |
| 65 | statistics = value; |
| 66 | } |
| 67 | |
| 68 | grn_hash *columns = grn_hash_create(ctx, |
| 69 | NULL, |
| 70 | sizeof(grn_id), |
| 71 | 0, |
| 72 | GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); |
| 73 | if (!columns) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | grn_table_columns(ctx, table, NULL, 0, (grn_obj *)columns); |
| 78 | GRN_HASH_EACH_BEGIN(ctx, columns, cursor, id) |
| 79 | { |
| 80 | void *key; |
| 81 | grn_id column_id; |
| 82 | grn_obj *column; |
| 83 | |
| 84 | grn_hash_cursor_get_key(ctx, cursor, &key); |
| 85 | column_id = *((grn_id *)key); |
| 86 | |
| 87 | if (dumper->is_close_opened_object_mode) { |
| 88 | grn_ctx_push_temporary_open_space(ctx); |
| 89 | } |
| 90 | |
| 91 | column = grn_ctx_at(ctx, column_id); |
| 92 | if (!column) { |
| 93 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 94 | goto next_loop; |
| 95 | } |
| 96 | |
| 97 | if (grn_obj_is_index_column(ctx, column)) { |
| 98 | dumper->have_index_column = true; |
| 99 | } else { |
| 100 | if (statistics) { |
| 101 | statistics->have_data_column = !grn_obj_is_token_column(ctx, column); |
| 102 | } |
| 103 | if (grn_obj_is_reference_column(ctx, column)) { |
| 104 | dumper->have_reference_column = true; |
| 105 | if (statistics) { |
| 106 | statistics->have_reference_column = true; |
| 107 | } |
| 108 | |
| 109 | void *value; |
| 110 | if (grn_hash_add(ctx, |
| 111 | dumper->table_statistics, |
| 112 | &(DB_OBJ(column)->range), |
| 113 | sizeof(grn_id), |
| 114 | &value, |
| 115 | NULL) != GRN_ID_NIL) { |
| 116 | grn_dumper_table_statistics *referred_statistics = value; |
| 117 | referred_statistics->is_referred = true; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | next_loop: |
| 123 | if (dumper->is_close_opened_object_mode) { |
| 124 | grn_ctx_pop_temporary_open_space(ctx); |
| 125 | } |
| 126 | } |
| 127 | GRN_HASH_EACH_END(ctx, cursor); |
| 128 | grn_hash_close(ctx, columns); |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | dumper_collect_statistics(grn_ctx *ctx, grn_dumper *dumper) |
| 133 | { |
| 134 | dumper->table_statistics = |
| 135 | grn_hash_create(ctx, |
| 136 | NULL, |
| 137 | sizeof(grn_id), |
| 138 | sizeof(grn_dumper_table_statistics), |
| 139 | GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); |
| 140 | |
| 141 | GRN_DB_EACH_BEGIN_BY_ID(ctx, cursor, id) |
| 142 | { |
| 143 | void *name; |
| 144 | int name_size; |
| 145 | grn_obj *object; |
| 146 | |
| 147 | if (grn_id_is_builtin(ctx, id)) { |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | name_size = grn_table_cursor_get_key(ctx, cursor, &name); |
| 152 | if (grn_obj_name_is_column(ctx, name, name_size)) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | if (dumper->is_close_opened_object_mode) { |
| 157 | grn_ctx_push_temporary_open_space(ctx); |
| 158 | } |
| 159 | |
| 160 | object = grn_ctx_at(ctx, id); |
| 161 | if (!object) { |
| 162 | /* XXX: this clause is executed when MeCab tokenizer is enabled in |
| 163 | database but the groonga isn't supported MeCab. |
| 164 | We should return error message about it and error exit status |
| 165 | but it's too difficult for this architecture. :< */ |
| 166 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 167 | goto next_loop; |
| 168 | } |
| 169 | |
| 170 | if (!grn_obj_is_table(ctx, object)) { |
| 171 | goto next_loop; |
| 172 | } |
| 173 | |
| 174 | dumper_collect_statistics_table(ctx, dumper, object); |
| 175 | |
| 176 | next_loop: |
| 177 | if (dumper->is_close_opened_object_mode) { |
| 178 | grn_ctx_pop_temporary_open_space(ctx); |
| 179 | } |
| 180 | } |
| 181 | GRN_DB_EACH_END(ctx, cursor); |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | dump_value_raw(grn_ctx *ctx, |
| 186 | grn_obj *output, |
| 187 | const char *value, |
| 188 | size_t value_len) |
| 189 | { |
| 190 | grn_obj escaped_value; |
| 191 | GRN_TEXT_INIT(&escaped_value, 0); |
| 192 | grn_text_esc(ctx, &escaped_value, value, value_len); |
| 193 | /* is no character escaped? */ |
| 194 | /* TODO false positive with spaces inside values */ |
| 195 | if (GRN_TEXT_LEN(&escaped_value) == (value_len + 2)) { |
| 196 | GRN_TEXT_PUT(ctx, output, value, value_len); |
| 197 | } else { |
| 198 | GRN_TEXT_PUT(ctx, |
| 199 | output, |
| 200 | GRN_TEXT_VALUE(&escaped_value), |
| 201 | GRN_TEXT_LEN(&escaped_value)); |
| 202 | } |
| 203 | grn_obj_close(ctx, &escaped_value); |
| 204 | } |
| 205 | |
| 206 | static void |
| 207 | dump_value(grn_ctx *ctx, |
| 208 | grn_dumper *dumper, |
| 209 | const char *value, |
| 210 | size_t value_len) |
| 211 | { |
| 212 | dump_value_raw(ctx, dumper->output, value, value_len); |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | dump_configs(grn_ctx *ctx, grn_dumper *dumper) |
| 217 | { |
| 218 | grn_obj *config_cursor; |
| 219 | |
| 220 | config_cursor = grn_config_cursor_open(ctx); |
| 221 | if (!config_cursor) return; |
| 222 | |
| 223 | while (grn_config_cursor_next(ctx, config_cursor)) { |
| 224 | const char *key; |
| 225 | uint32_t key_size; |
| 226 | const char *value; |
| 227 | uint32_t value_size; |
| 228 | |
| 229 | key_size = grn_config_cursor_get_key(ctx, config_cursor, &key); |
| 230 | value_size = grn_config_cursor_get_value(ctx, config_cursor, &value); |
| 231 | |
| 232 | GRN_TEXT_PUTS(ctx, dumper->output, "config_set "); |
| 233 | dump_value(ctx, dumper, key, key_size); |
| 234 | GRN_TEXT_PUTS(ctx, dumper->output, " "); |
| 235 | dump_value(ctx, dumper, value, value_size); |
| 236 | GRN_TEXT_PUTC(ctx, dumper->output, '\n'); |
| 237 | } |
| 238 | grn_obj_close(ctx, config_cursor); |
| 239 | } |
| 240 | |
| 241 | static void |
| 242 | dump_plugins(grn_ctx *ctx, grn_dumper *dumper) |
| 243 | { |
| 244 | grn_obj plugin_names; |
| 245 | unsigned int i, n; |
| 246 | |
| 247 | GRN_TEXT_INIT(&plugin_names, GRN_OBJ_VECTOR); |
| 248 | |
| 249 | grn_plugin_get_names(ctx, &plugin_names); |
| 250 | |
| 251 | n = grn_vector_size(ctx, &plugin_names); |
| 252 | if (n == 0) { |
| 253 | GRN_OBJ_FIN(ctx, &plugin_names); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (GRN_TEXT_LEN(dumper->output) > 0) { |
| 258 | GRN_TEXT_PUTC(ctx, dumper->output, '\n'); |
| 259 | grn_ctx_output_flush(ctx, 0); |
| 260 | } |
| 261 | for (i = 0; i < n; i++) { |
| 262 | const char *name; |
| 263 | unsigned int name_size; |
| 264 | |
| 265 | name_size = |
| 266 | grn_vector_get_element(ctx, &plugin_names, i, &name, NULL, NULL); |
| 267 | grn_text_printf(ctx, |
| 268 | dumper->output, |
| 269 | "plugin_register %.*s\n", |
| 270 | (int)name_size, |
| 271 | name); |
| 272 | } |
| 273 | |
| 274 | GRN_OBJ_FIN(ctx, &plugin_names); |
| 275 | } |
| 276 | |
| 277 | static void |
| 278 | dump_obj_name_raw(grn_ctx *ctx, grn_obj *output, grn_obj *obj) |
| 279 | { |
| 280 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 281 | int name_len; |
| 282 | name_len = grn_obj_name(ctx, obj, name, GRN_TABLE_MAX_KEY_SIZE); |
| 283 | dump_value_raw(ctx, output, name, (size_t)name_len); |
| 284 | } |
| 285 | |
| 286 | static void |
| 287 | dump_obj_name(grn_ctx *ctx, grn_dumper *dumper, grn_obj *obj) |
| 288 | { |
| 289 | dump_obj_name_raw(ctx, dumper->output, obj); |
| 290 | } |
| 291 | |
| 292 | static void |
| 293 | dump_column_name(grn_ctx *ctx, grn_dumper *dumper, grn_obj *column) |
| 294 | { |
| 295 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 296 | int name_len; |
| 297 | name_len = grn_column_name(ctx, column, name, GRN_TABLE_MAX_KEY_SIZE); |
| 298 | dump_value(ctx, dumper, name, (size_t)name_len); |
| 299 | } |
| 300 | |
| 301 | static void |
| 302 | dump_column_sources(grn_ctx *ctx, grn_dumper *dumper, grn_obj *column) |
| 303 | { |
| 304 | grn_obj sources; |
| 305 | grn_id *source_ids; |
| 306 | size_t i, n; |
| 307 | |
| 308 | GRN_OBJ_INIT(&sources, GRN_BULK, 0, GRN_ID_NIL); |
| 309 | grn_obj_get_info(ctx, column, GRN_INFO_SOURCE, &sources); |
| 310 | |
| 311 | n = GRN_BULK_VSIZE(&sources) / sizeof(grn_id); |
| 312 | source_ids = (grn_id *)GRN_BULK_HEAD(&sources); |
| 313 | if (n > 0) { |
| 314 | GRN_TEXT_PUTC(ctx, dumper->output, ' '); |
| 315 | } |
| 316 | for (i = 0; i < n; i++) { |
| 317 | grn_id source_id; |
| 318 | grn_obj *source; |
| 319 | |
| 320 | source_id = *source_ids; |
| 321 | source_ids++; |
| 322 | |
| 323 | if (dumper->is_close_opened_object_mode) { |
| 324 | grn_ctx_push_temporary_open_space(ctx); |
| 325 | } |
| 326 | |
| 327 | source = grn_ctx_at(ctx, source_id); |
| 328 | if (!source) { |
| 329 | goto next_loop; |
| 330 | } |
| 331 | |
| 332 | if (i) { |
| 333 | GRN_TEXT_PUTC(ctx, dumper->output, ','); |
| 334 | } |
| 335 | switch (source->header.type) { |
| 336 | case GRN_TABLE_PAT_KEY: |
| 337 | case GRN_TABLE_DAT_KEY: |
| 338 | case GRN_TABLE_HASH_KEY: |
| 339 | GRN_TEXT_PUT(ctx, |
| 340 | dumper->output, |
| 341 | GRN_COLUMN_NAME_KEY, |
| 342 | GRN_COLUMN_NAME_KEY_LEN); |
| 343 | break; |
| 344 | default: |
| 345 | dump_column_name(ctx, dumper, source); |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | next_loop: |
| 350 | if (dumper->is_close_opened_object_mode) { |
| 351 | grn_ctx_pop_temporary_open_space(ctx); |
| 352 | } |
| 353 | } |
| 354 | grn_obj_close(ctx, &sources); |
| 355 | } |
| 356 | |
| 357 | static void |
| 358 | dump_string(grn_ctx *ctx, grn_dumper *dumper, grn_obj *string) |
| 359 | { |
| 360 | const char *value = GRN_TEXT_VALUE(string); |
| 361 | size_t length = GRN_TEXT_LEN(string); |
| 362 | |
| 363 | if (grn_proc_text_include_special_character(ctx, value, length)) { |
| 364 | grn_text_otoj(ctx, dumper->output, string, NULL); |
| 365 | } else { |
| 366 | GRN_TEXT_PUT(ctx, dumper->output, value, length); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | static void |
| 371 | dump_column_generator(grn_ctx *ctx, grn_dumper *dumper, grn_obj *column) |
| 372 | { |
| 373 | grn_obj generator; |
| 374 | GRN_TEXT_INIT(&generator, 0); |
| 375 | grn_obj_get_info(ctx, column, GRN_INFO_GENERATOR, &generator); |
| 376 | if (GRN_TEXT_LEN(&generator) > 0) { |
| 377 | GRN_TEXT_PUTS(ctx, dumper->output, " --generator "); |
| 378 | dump_string(ctx, dumper, &generator); |
| 379 | } |
| 380 | GRN_OBJ_FIN(ctx, &generator); |
| 381 | } |
| 382 | |
| 383 | static void |
| 384 | dump_column(grn_ctx *ctx, grn_dumper *dumper, grn_obj *table, grn_obj *column) |
| 385 | { |
| 386 | grn_id type_id; |
| 387 | grn_obj *type; |
| 388 | grn_column_flags flags; |
| 389 | grn_column_flags default_flags = GRN_OBJ_PERSISTENT; |
| 390 | |
| 391 | type_id = grn_obj_get_range(ctx, column); |
| 392 | if (dumper->is_close_opened_object_mode) { |
| 393 | grn_ctx_push_temporary_open_space(ctx); |
| 394 | } |
| 395 | type = grn_ctx_at(ctx, type_id); |
| 396 | if (!type) { |
| 397 | /* ERR(GRN_RANGE_ERROR, "couldn't get column's type object"); */ |
| 398 | goto exit; |
| 399 | } |
| 400 | |
| 401 | GRN_TEXT_PUTS(ctx, dumper->output, "column_create "); |
| 402 | dump_obj_name(ctx, dumper, table); |
| 403 | GRN_TEXT_PUTC(ctx, dumper->output, ' '); |
| 404 | dump_column_name(ctx, dumper, column); |
| 405 | GRN_TEXT_PUTC(ctx, dumper->output, ' '); |
| 406 | if (type->header.type == GRN_TYPE) { |
| 407 | default_flags |= type->header.flags; |
| 408 | } |
| 409 | flags = grn_column_get_flags(ctx, column); |
| 410 | grn_dump_column_create_flags(ctx, flags & ~default_flags, dumper->output); |
| 411 | GRN_TEXT_PUTC(ctx, dumper->output, ' '); |
| 412 | dump_obj_name(ctx, dumper, type); |
| 413 | switch (column->header.type) { |
| 414 | case GRN_COLUMN_VAR_SIZE: |
| 415 | case GRN_COLUMN_INDEX: |
| 416 | dump_column_sources(ctx, dumper, column); |
| 417 | break; |
| 418 | } |
| 419 | switch (column->header.type) { |
| 420 | case GRN_COLUMN_VAR_SIZE: |
| 421 | dump_column_generator(ctx, dumper, column); |
| 422 | break; |
| 423 | } |
| 424 | if ((flags & GRN_OBJ_CUSTOM_NAME) && dumper->is_dump_paths) { |
| 425 | grn_obj sub_output; |
| 426 | GRN_TEXT_PUTS(ctx, dumper->output, " --path "); |
| 427 | GRN_TEXT_INIT(&sub_output, GRN_OBJ_DO_SHALLOW_COPY); |
| 428 | const char *path = grn_obj_path(ctx, column); |
| 429 | GRN_TEXT_SETS(ctx, &sub_output, path); |
| 430 | dump_string(ctx, dumper, &sub_output); |
| 431 | GRN_OBJ_FIN(ctx, &sub_output); |
| 432 | } |
| 433 | GRN_TEXT_PUTC(ctx, dumper->output, '\n'); |
| 434 | |
| 435 | exit: |
| 436 | if (dumper->is_close_opened_object_mode) { |
| 437 | grn_ctx_pop_temporary_open_space(ctx); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | static void |
| 442 | dump_columns(grn_ctx *ctx, |
| 443 | grn_dumper *dumper, |
| 444 | grn_obj *table, |
| 445 | bool dump_data_column, |
| 446 | bool dump_reference_column, |
| 447 | bool dump_index_column) |
| 448 | { |
| 449 | grn_hash *columns; |
| 450 | columns = grn_hash_create(ctx, |
| 451 | NULL, |
| 452 | sizeof(grn_id), |
| 453 | 0, |
| 454 | GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); |
| 455 | if (!columns) { |
| 456 | GRN_PLUGIN_ERROR(ctx, |
| 457 | GRN_NO_MEMORY_AVAILABLE, |
| 458 | "couldn't create a hash to hold columns"); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | if (grn_table_columns(ctx, table, NULL, 0, (grn_obj *)columns) >= 0) { |
| 463 | GRN_HASH_EACH_BEGIN(ctx, columns, cursor, id) |
| 464 | { |
| 465 | void *key; |
| 466 | grn_id column_id; |
| 467 | grn_obj *column; |
| 468 | |
| 469 | grn_hash_cursor_get_key(ctx, cursor, &key); |
| 470 | column_id = *((grn_id *)key); |
| 471 | |
| 472 | if (dumper->is_close_opened_object_mode) { |
| 473 | grn_ctx_push_temporary_open_space(ctx); |
| 474 | } |
| 475 | |
| 476 | column = grn_ctx_at(ctx, column_id); |
| 477 | if (!column) { |
| 478 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 479 | goto next_loop; |
| 480 | } |
| 481 | |
| 482 | if (grn_obj_is_index_column(ctx, column)) { |
| 483 | if (dump_index_column) { |
| 484 | dump_column(ctx, dumper, table, column); |
| 485 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 486 | } |
| 487 | } else if (grn_obj_is_reference_column(ctx, column)) { |
| 488 | if (dump_reference_column) { |
| 489 | dump_column(ctx, dumper, table, column); |
| 490 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 491 | } |
| 492 | } else { |
| 493 | if (dump_data_column) { |
| 494 | dump_column(ctx, dumper, table, column); |
| 495 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | next_loop: |
| 500 | if (dumper->is_close_opened_object_mode) { |
| 501 | grn_ctx_pop_temporary_open_space(ctx); |
| 502 | } |
| 503 | } |
| 504 | GRN_HASH_EACH_END(ctx, cursor); |
| 505 | } |
| 506 | grn_hash_close(ctx, columns); |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | dump_record_column_vector(grn_ctx *ctx, |
| 511 | grn_dumper *dumper, |
| 512 | grn_id id, |
| 513 | grn_obj *column, |
| 514 | grn_id range_id, |
| 515 | grn_obj *buf) |
| 516 | { |
| 517 | grn_obj *range; |
| 518 | grn_obj_format *format_argument = NULL; |
| 519 | grn_obj_format format; |
| 520 | |
| 521 | range = grn_ctx_at(ctx, range_id); |
| 522 | grn_column_flags column_flags = grn_column_get_flags(ctx, column); |
| 523 | if (column_flags & GRN_OBJ_WITH_WEIGHT) { |
| 524 | format.flags = GRN_OBJ_FORMAT_WITH_WEIGHT; |
| 525 | if (column_flags & (GRN_OBJ_WEIGHT_FLOAT32 | GRN_OBJ_WEIGHT_BFLOAT16)) { |
| 526 | format.flags |= GRN_OBJ_FORMAT_WEIGHT_FLOAT32; |
| 527 | } |
| 528 | format_argument = &format; |
| 529 | } |
| 530 | |
| 531 | if (grn_obj_is_table(ctx, range) || |
| 532 | (range->header.flags & GRN_OBJ_KEY_VAR_SIZE) == 0) { |
| 533 | GRN_OBJ_INIT(buf, GRN_UVECTOR, 0, range_id); |
| 534 | grn_obj_get_value(ctx, column, id, buf); |
| 535 | grn_text_otoj(ctx, dumper->output, buf, format_argument); |
| 536 | } else { |
| 537 | GRN_OBJ_INIT(buf, GRN_VECTOR, 0, range_id); |
| 538 | grn_obj_get_value(ctx, column, id, buf); |
| 539 | grn_text_otoj(ctx, dumper->output, buf, format_argument); |
| 540 | } |
| 541 | |
| 542 | grn_obj_unlink(ctx, range); |
| 543 | grn_obj_unlink(ctx, buf); |
| 544 | } |
| 545 | |
| 546 | static void |
| 547 | dump_record(grn_ctx *ctx, |
| 548 | grn_dumper *dumper, |
| 549 | grn_obj *table, |
| 550 | grn_id id, |
| 551 | grn_obj *columns, |
| 552 | size_t n_columns) |
| 553 | { |
| 554 | size_t i; |
| 555 | grn_obj buf; |
| 556 | grn_obj *column_name = &(dumper->column_name_buffer); |
| 557 | |
| 558 | GRN_TEXT_PUTC(ctx, dumper->output, '['); |
| 559 | for (i = 0; i < n_columns; i++) { |
| 560 | bool is_value_column; |
| 561 | grn_id range; |
| 562 | grn_obj *column; |
| 563 | column = GRN_PTR_VALUE_AT(columns, i); |
| 564 | /* TODO: use grn_obj_is_value_accessor() */ |
| 565 | GRN_BULK_REWIND(column_name); |
| 566 | grn_column_name_(ctx, column, column_name); |
| 567 | if (GRN_TEXT_LEN(column_name) == GRN_COLUMN_NAME_VALUE_LEN && |
| 568 | !memcmp(GRN_TEXT_VALUE(column_name), |
| 569 | GRN_COLUMN_NAME_VALUE, |
| 570 | GRN_COLUMN_NAME_VALUE_LEN)) { |
| 571 | is_value_column = true; |
| 572 | } else { |
| 573 | is_value_column = false; |
| 574 | } |
| 575 | range = grn_obj_get_range(ctx, column); |
| 576 | |
| 577 | if (i > 0) { |
| 578 | GRN_TEXT_PUTC(ctx, dumper->output, ','); |
| 579 | } |
| 580 | switch (column->header.type) { |
| 581 | case GRN_COLUMN_VAR_SIZE: |
| 582 | case GRN_COLUMN_FIX_SIZE: |
| 583 | switch (column->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) { |
| 584 | case GRN_OBJ_COLUMN_VECTOR: |
| 585 | dump_record_column_vector(ctx, dumper, id, column, range, &buf); |
| 586 | break; |
| 587 | case GRN_OBJ_COLUMN_SCALAR: |
| 588 | { |
| 589 | GRN_OBJ_INIT(&buf, GRN_BULK, 0, range); |
| 590 | grn_obj_get_value(ctx, column, id, &buf); |
| 591 | grn_text_otoj(ctx, dumper->output, &buf, NULL); |
| 592 | grn_obj_unlink(ctx, &buf); |
| 593 | } |
| 594 | break; |
| 595 | default: |
| 596 | GRN_PLUGIN_ERROR(ctx, |
| 597 | GRN_OPERATION_NOT_SUPPORTED, |
| 598 | "unsupported column type: %#x", |
| 599 | column->header.type); |
| 600 | break; |
| 601 | } |
| 602 | break; |
| 603 | case GRN_COLUMN_INDEX: |
| 604 | break; |
| 605 | case GRN_ACCESSOR: |
| 606 | { |
| 607 | GRN_OBJ_INIT(&buf, GRN_BULK, 0, range); |
| 608 | grn_obj_get_value(ctx, column, id, &buf); |
| 609 | /* XXX maybe, grn_obj_get_range() should not unconditionally return |
| 610 | GRN_DB_INT32 when column is GRN_ACCESSOR and |
| 611 | GRN_ACCESSOR_GET_VALUE */ |
| 612 | if (is_value_column) { |
| 613 | buf.header.domain = grn_obj_get_range(ctx, table); |
| 614 | } |
| 615 | grn_text_otoj(ctx, dumper->output, &buf, NULL); |
| 616 | grn_obj_unlink(ctx, &buf); |
| 617 | } |
| 618 | break; |
| 619 | default: |
| 620 | GRN_PLUGIN_ERROR(ctx, |
| 621 | GRN_OPERATION_NOT_SUPPORTED, |
| 622 | "unsupported header type %#x", |
| 623 | column->header.type); |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | GRN_TEXT_PUTC(ctx, dumper->output, ']'); |
| 628 | if (GRN_TEXT_LEN(dumper->output) >= DUMP_FLUSH_THRESHOLD_SIZE) { |
| 629 | grn_ctx_output_flush(ctx, 0); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | static void |
| 634 | dump_records(grn_ctx *ctx, |
| 635 | grn_dumper *dumper, |
| 636 | grn_obj *table, |
| 637 | bool dump_real_columns) |
| 638 | { |
| 639 | grn_table_cursor *cursor; |
| 640 | grn_obj columns; |
| 641 | bool have_auto_generated_value_column = false; |
| 642 | bool have_data_column = false; |
| 643 | |
| 644 | if (grn_table_size(ctx, table) == 0) { |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | if (dumper->is_close_opened_object_mode) { |
| 649 | grn_ctx_push_temporary_open_space(ctx); |
| 650 | } |
| 651 | |
| 652 | GRN_PTR_INIT(&columns, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 653 | |
| 654 | if (table->header.type == GRN_TABLE_NO_KEY) { |
| 655 | grn_obj *id_accessor; |
| 656 | id_accessor = |
| 657 | grn_obj_column(ctx, table, GRN_COLUMN_NAME_ID, GRN_COLUMN_NAME_ID_LEN); |
| 658 | GRN_PTR_PUT(ctx, &columns, id_accessor); |
| 659 | } else if (table->header.domain != GRN_ID_NIL) { |
| 660 | grn_obj *key_accessor; |
| 661 | key_accessor = |
| 662 | grn_obj_column(ctx, table, GRN_COLUMN_NAME_KEY, GRN_COLUMN_NAME_KEY_LEN); |
| 663 | GRN_PTR_PUT(ctx, &columns, key_accessor); |
| 664 | } |
| 665 | |
| 666 | if (grn_obj_get_range(ctx, table) != GRN_ID_NIL) { |
| 667 | grn_obj *value_accessor; |
| 668 | value_accessor = grn_obj_column(ctx, |
| 669 | table, |
| 670 | GRN_COLUMN_NAME_VALUE, |
| 671 | GRN_COLUMN_NAME_VALUE_LEN); |
| 672 | GRN_PTR_PUT(ctx, &columns, value_accessor); |
| 673 | } |
| 674 | |
| 675 | { |
| 676 | grn_hash *real_columns; |
| 677 | |
| 678 | real_columns = grn_hash_create(ctx, |
| 679 | NULL, |
| 680 | sizeof(grn_id), |
| 681 | 0, |
| 682 | GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); |
| 683 | grn_table_columns(ctx, table, NULL, 0, (grn_obj *)real_columns); |
| 684 | GRN_HASH_EACH_BEGIN(ctx, real_columns, cursor, id) |
| 685 | { |
| 686 | void *key; |
| 687 | grn_id column_id; |
| 688 | grn_obj *column; |
| 689 | |
| 690 | if (dumper->is_close_opened_object_mode) { |
| 691 | grn_ctx_push_temporary_open_space(ctx); |
| 692 | } |
| 693 | |
| 694 | grn_hash_cursor_get_key(ctx, cursor, &key); |
| 695 | column_id = *((grn_id *)key); |
| 696 | |
| 697 | column = grn_ctx_at(ctx, column_id); |
| 698 | if (column) { |
| 699 | if (grn_obj_is_index_column(ctx, column) || |
| 700 | grn_obj_is_token_column(ctx, column) || |
| 701 | grn_obj_is_generated_column(ctx, column)) { |
| 702 | have_auto_generated_value_column = true; |
| 703 | if (dumper->is_close_opened_object_mode) { |
| 704 | grn_ctx_pop_temporary_open_space(ctx); |
| 705 | } |
| 706 | } else { |
| 707 | have_data_column = true; |
| 708 | if (dump_real_columns) { |
| 709 | GRN_PTR_PUT(ctx, &columns, column); |
| 710 | } |
| 711 | if (dumper->is_close_opened_object_mode) { |
| 712 | grn_ctx_merge_temporary_open_space(ctx); |
| 713 | } |
| 714 | } |
| 715 | } else { |
| 716 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 717 | if (dumper->is_close_opened_object_mode) { |
| 718 | grn_ctx_pop_temporary_open_space(ctx); |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | GRN_HASH_EACH_END(ctx, cursor); |
| 723 | grn_hash_close(ctx, real_columns); |
| 724 | } |
| 725 | |
| 726 | size_t n_columns = GRN_BULK_VSIZE(&columns) / sizeof(grn_obj *); |
| 727 | |
| 728 | if (have_auto_generated_value_column && !have_data_column) { |
| 729 | goto exit; |
| 730 | } |
| 731 | |
| 732 | if (GRN_TEXT_LEN(dumper->output) > 0) { |
| 733 | GRN_TEXT_PUTC(ctx, dumper->output, '\n'); |
| 734 | } |
| 735 | |
| 736 | GRN_TEXT_PUTS(ctx, dumper->output, "load --table "); |
| 737 | dump_obj_name(ctx, dumper, table); |
| 738 | GRN_TEXT_PUTS(ctx, dumper->output, "\n[\n"); |
| 739 | |
| 740 | GRN_TEXT_PUTC(ctx, dumper->output, '['); |
| 741 | size_t i; |
| 742 | for (i = 0; i < n_columns; i++) { |
| 743 | grn_obj *column; |
| 744 | grn_obj *column_name = &(dumper->column_name_buffer); |
| 745 | |
| 746 | column = GRN_PTR_VALUE_AT(&columns, i); |
| 747 | if (i) { |
| 748 | GRN_TEXT_PUTC(ctx, dumper->output, ','); |
| 749 | } |
| 750 | GRN_BULK_REWIND(column_name); |
| 751 | grn_column_name_(ctx, column, column_name); |
| 752 | grn_text_otoj(ctx, dumper->output, column_name, NULL); |
| 753 | } |
| 754 | GRN_TEXT_PUTS(ctx, dumper->output, "],\n"); |
| 755 | |
| 756 | if (table->header.type == GRN_TABLE_HASH_KEY && dumper->is_sort_hash_table) { |
| 757 | grn_obj *sorted; |
| 758 | grn_table_sort_key sort_keys[1]; |
| 759 | int n_sort_keys = 1; |
| 760 | bool is_first_record = true; |
| 761 | |
| 762 | sort_keys[0].key = |
| 763 | grn_obj_column(ctx, table, GRN_COLUMN_NAME_KEY, GRN_COLUMN_NAME_KEY_LEN); |
| 764 | sort_keys[0].flags = GRN_TABLE_SORT_ASC; |
| 765 | sort_keys[0].offset = 0; |
| 766 | sorted = |
| 767 | grn_table_create(ctx, NULL, 0, NULL, GRN_TABLE_NO_KEY, NULL, table); |
| 768 | grn_table_sort(ctx, table, 0, -1, sorted, sort_keys, n_sort_keys); |
| 769 | cursor = grn_table_cursor_open(ctx, sorted, NULL, 0, NULL, 0, 0, -1, 0); |
| 770 | while (grn_table_cursor_next(ctx, cursor) != GRN_ID_NIL) { |
| 771 | void *value_raw; |
| 772 | grn_id id; |
| 773 | |
| 774 | grn_table_cursor_get_value(ctx, cursor, &value_raw); |
| 775 | id = *((grn_id *)value_raw); |
| 776 | |
| 777 | if (is_first_record) { |
| 778 | is_first_record = false; |
| 779 | } else { |
| 780 | GRN_TEXT_PUTS(ctx, dumper->output, ",\n"); |
| 781 | } |
| 782 | dump_record(ctx, dumper, table, id, &columns, n_columns); |
| 783 | } |
| 784 | grn_table_cursor_close(ctx, cursor); |
| 785 | GRN_TEXT_PUTS(ctx, dumper->output, "\n]\n"); |
| 786 | grn_obj_close(ctx, sorted); |
| 787 | grn_obj_unlink(ctx, sort_keys[0].key); |
| 788 | } else { |
| 789 | grn_obj delete_commands; |
| 790 | grn_id old_id = GRN_ID_NIL; |
| 791 | grn_id id; |
| 792 | |
| 793 | GRN_TEXT_INIT(&delete_commands, 0); |
| 794 | cursor = grn_table_cursor_open(ctx, |
| 795 | table, |
| 796 | NULL, |
| 797 | 0, |
| 798 | NULL, |
| 799 | 0, |
| 800 | 0, |
| 801 | -1, |
| 802 | GRN_CURSOR_BY_KEY); |
| 803 | while ((id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { |
| 804 | if (old_id != GRN_ID_NIL) { |
| 805 | GRN_TEXT_PUTS(ctx, dumper->output, ",\n"); |
| 806 | } |
| 807 | if (table->header.type == GRN_TABLE_NO_KEY && old_id + 1 < id) { |
| 808 | grn_id current_id; |
| 809 | for (current_id = old_id + 1; current_id < id; current_id++) { |
| 810 | GRN_TEXT_PUTS(ctx, dumper->output, "[],\n"); |
| 811 | GRN_TEXT_PUTS(ctx, &delete_commands, "delete --table "); |
| 812 | dump_obj_name_raw(ctx, &delete_commands, table); |
| 813 | GRN_TEXT_PUTS(ctx, &delete_commands, " --id "); |
| 814 | grn_text_lltoa(ctx, &delete_commands, current_id); |
| 815 | GRN_TEXT_PUTC(ctx, &delete_commands, '\n'); |
| 816 | } |
| 817 | } |
| 818 | dump_record(ctx, dumper, table, id, &columns, n_columns); |
| 819 | |
| 820 | old_id = id; |
| 821 | } |
| 822 | grn_table_cursor_close(ctx, cursor); |
| 823 | GRN_TEXT_PUTS(ctx, dumper->output, "\n]\n"); |
| 824 | GRN_TEXT_PUT(ctx, |
| 825 | dumper->output, |
| 826 | GRN_TEXT_VALUE(&delete_commands), |
| 827 | GRN_TEXT_LEN(&delete_commands)); |
| 828 | GRN_OBJ_FIN(ctx, &delete_commands); |
| 829 | } |
| 830 | exit: |
| 831 | for (i = 0; i < n_columns; i++) { |
| 832 | grn_obj *column; |
| 833 | |
| 834 | column = GRN_PTR_VALUE_AT(&columns, i); |
| 835 | if (column->header.type == GRN_ACCESSOR) { |
| 836 | grn_obj_close(ctx, column); |
| 837 | } |
| 838 | } |
| 839 | GRN_OBJ_FIN(ctx, &columns); |
| 840 | |
| 841 | if (dumper->is_close_opened_object_mode) { |
| 842 | grn_ctx_pop_temporary_open_space(ctx); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | static void |
| 847 | dump_table(grn_ctx *ctx, grn_dumper *dumper, grn_obj *table) |
| 848 | { |
| 849 | grn_obj *domain = NULL; |
| 850 | grn_id range_id; |
| 851 | grn_obj *range = NULL; |
| 852 | grn_table_flags flags; |
| 853 | grn_table_flags default_flags = GRN_OBJ_PERSISTENT; |
| 854 | grn_obj *default_tokenizer; |
| 855 | grn_obj *normalizer; |
| 856 | grn_obj *token_filters; |
| 857 | |
| 858 | switch (table->header.type) { |
| 859 | case GRN_TABLE_HASH_KEY: |
| 860 | case GRN_TABLE_PAT_KEY: |
| 861 | case GRN_TABLE_DAT_KEY: |
| 862 | domain = grn_ctx_at(ctx, table->header.domain); |
| 863 | break; |
| 864 | default: |
| 865 | break; |
| 866 | } |
| 867 | |
| 868 | if (GRN_TEXT_LEN(dumper->output) > 0) { |
| 869 | GRN_TEXT_PUTC(ctx, dumper->output, '\n'); |
| 870 | grn_ctx_output_flush(ctx, 0); |
| 871 | } |
| 872 | |
| 873 | grn_table_get_info(ctx, |
| 874 | table, |
| 875 | &flags, |
| 876 | NULL, |
| 877 | &default_tokenizer, |
| 878 | &normalizer, |
| 879 | &token_filters); |
| 880 | |
| 881 | GRN_TEXT_PUTS(ctx, dumper->output, "table_create "); |
| 882 | dump_obj_name(ctx, dumper, table); |
| 883 | GRN_TEXT_PUTC(ctx, dumper->output, ' '); |
| 884 | grn_dump_table_create_flags(ctx, flags & ~default_flags, dumper->output); |
| 885 | if (domain) { |
| 886 | GRN_TEXT_PUTC(ctx, dumper->output, ' '); |
| 887 | dump_obj_name(ctx, dumper, domain); |
| 888 | } |
| 889 | range_id = grn_obj_get_range(ctx, table); |
| 890 | if (range_id != GRN_ID_NIL) { |
| 891 | range = grn_ctx_at(ctx, range_id); |
| 892 | if (!range) { |
| 893 | // ERR(GRN_RANGE_ERROR, "couldn't get table's value_type object"); |
| 894 | return; |
| 895 | } |
| 896 | if (table->header.type != GRN_TABLE_NO_KEY) { |
| 897 | GRN_TEXT_PUTC(ctx, dumper->output, ' '); |
| 898 | } else { |
| 899 | GRN_TEXT_PUTS(ctx, dumper->output, " --value_type "); |
| 900 | } |
| 901 | dump_obj_name(ctx, dumper, range); |
| 902 | grn_obj_unlink(ctx, range); |
| 903 | } |
| 904 | if (default_tokenizer) { |
| 905 | grn_obj sub_output; |
| 906 | GRN_TEXT_PUTS(ctx, dumper->output, " --default_tokenizer "); |
| 907 | GRN_TEXT_INIT(&sub_output, 0); |
| 908 | grn_table_get_default_tokenizer_string(ctx, table, &sub_output); |
| 909 | dump_string(ctx, dumper, &sub_output); |
| 910 | GRN_OBJ_FIN(ctx, &sub_output); |
| 911 | } |
| 912 | if (normalizer) { |
| 913 | grn_obj normalizers; |
| 914 | GRN_PTR_INIT(&normalizers, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 915 | grn_obj_get_info(ctx, table, GRN_INFO_NORMALIZERS, &normalizers); |
| 916 | bool multiple_normalizers = (GRN_PTR_VECTOR_SIZE(&normalizers) > 1); |
| 917 | GRN_OBJ_FIN(ctx, &normalizers); |
| 918 | |
| 919 | grn_obj sub_output; |
| 920 | if (multiple_normalizers) { |
| 921 | GRN_TEXT_PUTS(ctx, dumper->output, " --normalizers "); |
| 922 | } else { |
| 923 | GRN_TEXT_PUTS(ctx, dumper->output, " --normalizer "); |
| 924 | } |
| 925 | GRN_TEXT_INIT(&sub_output, 0); |
| 926 | grn_table_get_normalizers_string(ctx, table, &sub_output); |
| 927 | dump_string(ctx, dumper, &sub_output); |
| 928 | GRN_OBJ_FIN(ctx, &sub_output); |
| 929 | } |
| 930 | if (token_filters && GRN_BULK_VSIZE(token_filters) > 0) { |
| 931 | grn_obj sub_output; |
| 932 | GRN_TEXT_PUTS(ctx, dumper->output, " --token_filters "); |
| 933 | GRN_TEXT_INIT(&sub_output, 0); |
| 934 | grn_table_get_token_filters_string(ctx, table, &sub_output); |
| 935 | dump_string(ctx, dumper, &sub_output); |
| 936 | GRN_OBJ_FIN(ctx, &sub_output); |
| 937 | } |
| 938 | if (domain) { |
| 939 | grn_obj sub_output; |
| 940 | GRN_TEXT_INIT(&sub_output, 0); |
| 941 | grn_table_get_extractors_string(ctx, table, &sub_output); |
| 942 | if (GRN_TEXT_LEN(&sub_output) > 0) { |
| 943 | GRN_TEXT_PUTS(ctx, dumper->output, " --extractors "); |
| 944 | dump_string(ctx, dumper, &sub_output); |
| 945 | } |
| 946 | GRN_OBJ_FIN(ctx, &sub_output); |
| 947 | } |
| 948 | if ((flags & GRN_OBJ_CUSTOM_NAME) && dumper->is_dump_paths) { |
| 949 | grn_obj sub_output; |
| 950 | GRN_TEXT_PUTS(ctx, dumper->output, " --path "); |
| 951 | GRN_TEXT_INIT(&sub_output, GRN_OBJ_DO_SHALLOW_COPY); |
| 952 | const char *path = grn_obj_path(ctx, table); |
| 953 | GRN_TEXT_SETS(ctx, &sub_output, path); |
| 954 | dump_string(ctx, dumper, &sub_output); |
| 955 | GRN_OBJ_FIN(ctx, &sub_output); |
| 956 | } |
| 957 | |
| 958 | GRN_TEXT_PUTC(ctx, dumper->output, '\n'); |
| 959 | |
| 960 | dump_columns(ctx, dumper, table, true, false, false); |
| 961 | } |
| 962 | |
| 963 | static void |
| 964 | dump_schema(grn_ctx *ctx, grn_dumper *dumper) |
| 965 | { |
| 966 | GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id) |
| 967 | { |
| 968 | void *name; |
| 969 | int name_size; |
| 970 | grn_obj *object; |
| 971 | |
| 972 | if (grn_id_is_builtin(ctx, id)) { |
| 973 | continue; |
| 974 | } |
| 975 | |
| 976 | name_size = grn_table_cursor_get_key(ctx, cursor, &name); |
| 977 | if (grn_obj_name_is_column(ctx, name, name_size)) { |
| 978 | continue; |
| 979 | } |
| 980 | |
| 981 | if (dumper->is_close_opened_object_mode) { |
| 982 | grn_ctx_push_temporary_open_space(ctx); |
| 983 | } |
| 984 | |
| 985 | if ((object = grn_ctx_at(ctx, id))) { |
| 986 | switch (object->header.type) { |
| 987 | case GRN_TABLE_HASH_KEY: |
| 988 | case GRN_TABLE_PAT_KEY: |
| 989 | case GRN_TABLE_DAT_KEY: |
| 990 | case GRN_TABLE_NO_KEY: |
| 991 | dump_table(ctx, dumper, object); |
| 992 | break; |
| 993 | default: |
| 994 | break; |
| 995 | } |
| 996 | } else { |
| 997 | /* XXX: this clause is executed when MeCab tokenizer is enabled in |
| 998 | database but the groonga isn't supported MeCab. |
| 999 | We should return error message about it and error exit status |
| 1000 | but it's too difficult for this architecture. :< */ |
| 1001 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 1002 | } |
| 1003 | |
| 1004 | if (dumper->is_close_opened_object_mode) { |
| 1005 | grn_ctx_pop_temporary_open_space(ctx); |
| 1006 | } |
| 1007 | } |
| 1008 | GRN_DB_EACH_END(ctx, cursor); |
| 1009 | |
| 1010 | if (!dumper->have_reference_column) { |
| 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | GRN_TEXT_PUTC(ctx, dumper->output, '\n'); |
| 1015 | grn_ctx_output_flush(ctx, 0); |
| 1016 | |
| 1017 | GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id) |
| 1018 | { |
| 1019 | void *name; |
| 1020 | int name_size; |
| 1021 | grn_obj *object; |
| 1022 | |
| 1023 | if (grn_id_is_builtin(ctx, id)) { |
| 1024 | continue; |
| 1025 | } |
| 1026 | |
| 1027 | name_size = grn_table_cursor_get_key(ctx, cursor, &name); |
| 1028 | if (grn_obj_name_is_column(ctx, name, name_size)) { |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
| 1032 | if (dumper->is_close_opened_object_mode) { |
| 1033 | grn_ctx_push_temporary_open_space(ctx); |
| 1034 | } |
| 1035 | |
| 1036 | if ((object = grn_ctx_at(ctx, id))) { |
| 1037 | switch (object->header.type) { |
| 1038 | case GRN_TABLE_HASH_KEY: |
| 1039 | case GRN_TABLE_PAT_KEY: |
| 1040 | case GRN_TABLE_DAT_KEY: |
| 1041 | case GRN_TABLE_NO_KEY: |
| 1042 | dump_columns(ctx, dumper, object, false, true, false); |
| 1043 | break; |
| 1044 | default: |
| 1045 | break; |
| 1046 | } |
| 1047 | } else { |
| 1048 | /* XXX: this clause is executed when MeCab tokenizer is enabled in |
| 1049 | database but the groonga isn't supported MeCab. |
| 1050 | We should return error message about it and error exit status |
| 1051 | but it's too difficult for this architecture. :< */ |
| 1052 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 1053 | } |
| 1054 | |
| 1055 | if (dumper->is_close_opened_object_mode) { |
| 1056 | grn_ctx_pop_temporary_open_space(ctx); |
| 1057 | } |
| 1058 | } |
| 1059 | GRN_DB_EACH_END(ctx, cursor); |
| 1060 | } |
| 1061 | |
| 1062 | static void |
| 1063 | dump_records_maybe_defer(grn_ctx *ctx, |
| 1064 | grn_dumper *dumper, |
| 1065 | grn_obj *table, |
| 1066 | grn_obj *deferred_table_ids) |
| 1067 | { |
| 1068 | const grn_dumper_table_statistics *statistics = NULL; |
| 1069 | void *value; |
| 1070 | if (grn_hash_get(ctx, |
| 1071 | dumper->table_statistics, |
| 1072 | &(DB_OBJ(table)->id), |
| 1073 | sizeof(grn_id), |
| 1074 | &value) != GRN_ID_NIL) { |
| 1075 | statistics = (const grn_dumper_table_statistics *)value; |
| 1076 | } |
| 1077 | if (statistics) { |
| 1078 | if (statistics->is_referred && grn_obj_is_table_with_key(ctx, table)) { |
| 1079 | if (statistics->have_reference_column) { |
| 1080 | dump_records(ctx, dumper, table, false); |
| 1081 | if (statistics->have_data_column) { |
| 1082 | GRN_UINT32_PUT(ctx, deferred_table_ids, DB_OBJ(table)->id); |
| 1083 | } |
| 1084 | } else { |
| 1085 | dump_records(ctx, dumper, table, true); |
| 1086 | } |
| 1087 | } else { |
| 1088 | if (statistics->have_reference_column) { |
| 1089 | GRN_UINT32_PUT(ctx, deferred_table_ids, DB_OBJ(table)->id); |
| 1090 | } else { |
| 1091 | dump_records(ctx, dumper, table, true); |
| 1092 | } |
| 1093 | } |
| 1094 | } else { |
| 1095 | GRN_UINT32_PUT(ctx, deferred_table_ids, DB_OBJ(table)->id); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | static void |
| 1100 | dump_records_deferred(grn_ctx *ctx, |
| 1101 | grn_dumper *dumper, |
| 1102 | grn_obj *deferred_table_ids) |
| 1103 | { |
| 1104 | size_t n_deferred_table_ids = GRN_UINT32_VECTOR_SIZE(deferred_table_ids); |
| 1105 | size_t i; |
| 1106 | for (i = 0; i < n_deferred_table_ids; i++) { |
| 1107 | grn_id table_id = GRN_UINT32_VALUE_AT(deferred_table_ids, i); |
| 1108 | grn_obj *table = grn_ctx_at(ctx, table_id); |
| 1109 | dump_records(ctx, dumper, table, true); |
| 1110 | grn_obj_unref(ctx, table); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | static void |
| 1115 | dump_selected_tables_records(grn_ctx *ctx, grn_dumper *dumper, grn_obj *tables) |
| 1116 | { |
| 1117 | grn_obj deferred_table_ids; |
| 1118 | GRN_UINT32_INIT(&deferred_table_ids, GRN_OBJ_VECTOR); |
| 1119 | |
| 1120 | const char *p = GRN_TEXT_VALUE(tables); |
| 1121 | const char *e = p + GRN_TEXT_LEN(tables); |
| 1122 | while (p < e) { |
| 1123 | int len; |
| 1124 | const char *token, *token_e; |
| 1125 | |
| 1126 | if ((len = grn_isspace(p, ctx->encoding))) { |
| 1127 | p += len; |
| 1128 | continue; |
| 1129 | } |
| 1130 | |
| 1131 | token = p; |
| 1132 | if (!(('a' <= *p && *p <= 'z') || ('A' <= *p && *p <= 'Z') || |
| 1133 | (*p == '_'))) { |
| 1134 | while (p < e && !grn_isspace(p, ctx->encoding)) { |
| 1135 | p++; |
| 1136 | } |
| 1137 | GRN_LOG(ctx, |
| 1138 | GRN_LOG_WARNING, |
| 1139 | "invalid table name is ignored: <%.*s>\n", |
| 1140 | (int)(p - token), |
| 1141 | token); |
| 1142 | continue; |
| 1143 | } |
| 1144 | while (p < e && (('a' <= *p && *p <= 'z') || ('A' <= *p && *p <= 'Z') || |
| 1145 | ('0' <= *p && *p <= '9') || (*p == '_'))) { |
| 1146 | p++; |
| 1147 | } |
| 1148 | token_e = p; |
| 1149 | while (p < e && (len = grn_isspace(p, ctx->encoding))) { |
| 1150 | p += len; |
| 1151 | continue; |
| 1152 | } |
| 1153 | if (p < e && *p == ',') { |
| 1154 | p++; |
| 1155 | } |
| 1156 | |
| 1157 | grn_obj *table = grn_ctx_get(ctx, token, (int)(token_e - token)); |
| 1158 | if (!table) { |
| 1159 | GRN_LOG(ctx, |
| 1160 | GRN_LOG_WARNING, |
| 1161 | "nonexistent table name is ignored: <%.*s>\n", |
| 1162 | (int)(token_e - token), |
| 1163 | token); |
| 1164 | continue; |
| 1165 | } |
| 1166 | |
| 1167 | if (grn_obj_is_table(ctx, table)) { |
| 1168 | dump_records_maybe_defer(ctx, dumper, table, &deferred_table_ids); |
| 1169 | } |
| 1170 | grn_obj_unref(ctx, table); |
| 1171 | } |
| 1172 | |
| 1173 | dump_records_deferred(ctx, dumper, &deferred_table_ids); |
| 1174 | GRN_OBJ_FIN(ctx, &deferred_table_ids); |
| 1175 | } |
| 1176 | |
| 1177 | static void |
| 1178 | dump_all_records(grn_ctx *ctx, grn_dumper *dumper) |
| 1179 | { |
| 1180 | grn_obj deferred_table_ids; |
| 1181 | GRN_UINT32_INIT(&deferred_table_ids, GRN_OBJ_VECTOR); |
| 1182 | |
| 1183 | GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id) |
| 1184 | { |
| 1185 | void *name; |
| 1186 | int name_size; |
| 1187 | |
| 1188 | if (grn_id_is_builtin(ctx, id)) { |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | name_size = grn_table_cursor_get_key(ctx, cursor, &name); |
| 1193 | if (grn_obj_name_is_column(ctx, name, name_size)) { |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
| 1197 | if (dumper->is_close_opened_object_mode) { |
| 1198 | grn_ctx_push_temporary_open_space(ctx); |
| 1199 | } |
| 1200 | |
| 1201 | grn_obj *table = grn_ctx_at(ctx, id); |
| 1202 | if (!table) { |
| 1203 | /* XXX: this clause is executed when MeCab tokenizer is enabled in |
| 1204 | database but the groonga isn't supported MeCab. |
| 1205 | We should return error message about it and error exit status |
| 1206 | but it's too difficult for this architecture. :< */ |
| 1207 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 1208 | goto next_loop; |
| 1209 | } |
| 1210 | |
| 1211 | if (grn_obj_is_table(ctx, table)) { |
| 1212 | dump_records_maybe_defer(ctx, dumper, table, &deferred_table_ids); |
| 1213 | } |
| 1214 | |
| 1215 | next_loop: |
| 1216 | if (dumper->is_close_opened_object_mode) { |
| 1217 | grn_ctx_pop_temporary_open_space(ctx); |
| 1218 | } |
| 1219 | } |
| 1220 | GRN_DB_EACH_END(ctx, cursor); |
| 1221 | |
| 1222 | dump_records_deferred(ctx, dumper, &deferred_table_ids); |
| 1223 | GRN_OBJ_FIN(ctx, &deferred_table_ids); |
| 1224 | } |
| 1225 | |
| 1226 | static void |
| 1227 | dump_indexes(grn_ctx *ctx, grn_dumper *dumper) |
| 1228 | { |
| 1229 | if (!dumper->have_index_column) { |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | if (GRN_TEXT_LEN(dumper->output) > 0) { |
| 1234 | GRN_TEXT_PUTC(ctx, dumper->output, '\n'); |
| 1235 | } |
| 1236 | |
| 1237 | GRN_DB_EACH_BEGIN_BY_KEY(ctx, cursor, id) |
| 1238 | { |
| 1239 | void *name; |
| 1240 | int name_size; |
| 1241 | grn_obj *object; |
| 1242 | |
| 1243 | if (grn_id_is_builtin(ctx, id)) { |
| 1244 | continue; |
| 1245 | } |
| 1246 | |
| 1247 | name_size = grn_table_cursor_get_key(ctx, cursor, &name); |
| 1248 | if (grn_obj_name_is_column(ctx, name, name_size)) { |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | if (dumper->is_close_opened_object_mode) { |
| 1253 | grn_ctx_push_temporary_open_space(ctx); |
| 1254 | } |
| 1255 | |
| 1256 | object = grn_ctx_at(ctx, id); |
| 1257 | if (!object) { |
| 1258 | /* XXX: this clause is executed when MeCab tokenizer is enabled in |
| 1259 | database but the groonga isn't supported MeCab. |
| 1260 | We should return error message about it and error exit status |
| 1261 | but it's too difficult for this architecture. :< */ |
| 1262 | GRN_PLUGIN_CLEAR_ERROR(ctx); |
| 1263 | goto next_loop; |
| 1264 | } |
| 1265 | |
| 1266 | if (grn_obj_is_table(ctx, object)) { |
| 1267 | dump_columns(ctx, dumper, object, false, false, true); |
| 1268 | } |
| 1269 | |
| 1270 | next_loop: |
| 1271 | if (dumper->is_close_opened_object_mode) { |
| 1272 | grn_ctx_pop_temporary_open_space(ctx); |
| 1273 | } |
| 1274 | } |
| 1275 | GRN_DB_EACH_END(ctx, cursor); |
| 1276 | } |
| 1277 | |
| 1278 | static grn_obj * |
| 1279 | command_dump(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 1280 | { |
| 1281 | grn_dumper dumper; |
| 1282 | grn_obj *tables; |
| 1283 | bool is_dump_plugins; |
| 1284 | bool is_dump_schema; |
| 1285 | bool is_dump_records; |
| 1286 | bool is_dump_indexes; |
| 1287 | bool is_dump_configs; |
| 1288 | |
| 1289 | dumper.output = ctx->impl->output.buf; |
| 1290 | if (grn_thread_get_limit() == 1) { |
| 1291 | dumper.is_close_opened_object_mode = true; |
| 1292 | } else { |
| 1293 | dumper.is_close_opened_object_mode = false; |
| 1294 | } |
| 1295 | dumper.have_reference_column = false; |
| 1296 | dumper.have_index_column = false; |
| 1297 | |
| 1298 | tables = grn_plugin_proc_get_var(ctx, user_data, "tables", -1); |
| 1299 | is_dump_plugins = |
| 1300 | grn_plugin_proc_get_var_bool(ctx, user_data, "dump_plugins", -1, true); |
| 1301 | is_dump_schema = |
| 1302 | grn_plugin_proc_get_var_bool(ctx, user_data, "dump_schema", -1, true); |
| 1303 | is_dump_records = |
| 1304 | grn_plugin_proc_get_var_bool(ctx, user_data, "dump_records", -1, true); |
| 1305 | is_dump_indexes = |
| 1306 | grn_plugin_proc_get_var_bool(ctx, user_data, "dump_indexes", -1, true); |
| 1307 | is_dump_configs = |
| 1308 | grn_plugin_proc_get_var_bool(ctx, user_data, "dump_configs", -1, true); |
| 1309 | dumper.is_sort_hash_table = |
| 1310 | grn_plugin_proc_get_var_bool(ctx, user_data, "sort_hash_table", -1, false); |
| 1311 | dumper.is_dump_paths = |
| 1312 | grn_plugin_proc_get_var_bool(ctx, user_data, "dump_paths", -1, true); |
| 1313 | GRN_TEXT_INIT(&(dumper.column_name_buffer), 0); |
| 1314 | |
| 1315 | grn_ctx_set_output_type(ctx, GRN_CONTENT_GROONGA_COMMAND_LIST); |
| 1316 | |
| 1317 | dumper_collect_statistics(ctx, &dumper); |
| 1318 | |
| 1319 | if (is_dump_configs) { |
| 1320 | dump_configs(ctx, &dumper); |
| 1321 | } |
| 1322 | if (is_dump_plugins) { |
| 1323 | dump_plugins(ctx, &dumper); |
| 1324 | } |
| 1325 | if (is_dump_schema) { |
| 1326 | dump_schema(ctx, &dumper); |
| 1327 | } |
| 1328 | if (is_dump_records) { |
| 1329 | /* To update index columns correctly, we first create the whole schema, then |
| 1330 | load non-derivative records, while skipping records of index columns. |
| 1331 | That way, Groonga will silently do the job of updating index columns for |
| 1332 | us. */ |
| 1333 | if (GRN_TEXT_LEN(tables) > 0) { |
| 1334 | dump_selected_tables_records(ctx, &dumper, tables); |
| 1335 | } else { |
| 1336 | dump_all_records(ctx, &dumper); |
| 1337 | } |
| 1338 | } |
| 1339 | if (is_dump_indexes) { |
| 1340 | dump_indexes(ctx, &dumper); |
| 1341 | } |
| 1342 | /* remove the last newline because another one will be added by the caller. |
| 1343 | maybe, the caller of proc functions currently doesn't consider the |
| 1344 | possibility of multiple-line output from proc functions. */ |
| 1345 | if (GRN_BULK_VSIZE(dumper.output) > 0) { |
| 1346 | grn_bulk_truncate(ctx, dumper.output, GRN_BULK_VSIZE(dumper.output) - 1); |
| 1347 | } |
| 1348 | |
| 1349 | GRN_OBJ_FIN(ctx, &(dumper.column_name_buffer)); |
| 1350 | GRN_HASH_EACH_BEGIN(ctx, dumper.table_statistics, cursor, id) |
| 1351 | { |
| 1352 | void *value; |
| 1353 | grn_hash_cursor_get_value(ctx, cursor, &value); |
| 1354 | grn_dumper_table_statistics_fin(ctx, value); |
| 1355 | } |
| 1356 | GRN_HASH_EACH_END(ctx, cursor); |
| 1357 | grn_hash_close(ctx, dumper.table_statistics); |
| 1358 | |
| 1359 | return NULL; |
| 1360 | } |
| 1361 | |
| 1362 | void |
| 1363 | grn_proc_init_dump(grn_ctx *ctx) |
| 1364 | { |
| 1365 | grn_expr_var vars[8]; |
| 1366 | |
| 1367 | grn_plugin_expr_var_init(ctx, &(vars[0]), "tables", -1); |
| 1368 | grn_plugin_expr_var_init(ctx, &(vars[1]), "dump_plugins", -1); |
| 1369 | grn_plugin_expr_var_init(ctx, &(vars[2]), "dump_schema", -1); |
| 1370 | grn_plugin_expr_var_init(ctx, &(vars[3]), "dump_records", -1); |
| 1371 | grn_plugin_expr_var_init(ctx, &(vars[4]), "dump_indexes", -1); |
| 1372 | grn_plugin_expr_var_init(ctx, &(vars[5]), "dump_configs", -1); |
| 1373 | grn_plugin_expr_var_init(ctx, &(vars[6]), "sort_hash_table", -1); |
| 1374 | grn_plugin_expr_var_init(ctx, &(vars[7]), "dump_paths", -1); |
| 1375 | grn_plugin_command_create(ctx, |
| 1376 | "dump", |
| 1377 | -1, |
| 1378 | command_dump, |
| 1379 | sizeof(vars) / sizeof(vars[0]), |
| 1380 | vars); |
| 1381 | } |