| 1 | /* |
| 2 | Copyright (C) 2009-2016 Brazil |
| 3 | Copyright (C) 2018-2025 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 | |
| 22 | #include "../grn_ctx.h" |
| 23 | #include "../grn_db.h" |
| 24 | #include "../grn_progress_logger.h" |
| 25 | #include "../grn_str.h" |
| 26 | |
| 27 | #include <groonga/plugin.h> |
| 28 | |
| 29 | grn_column_flags |
| 30 | grn_proc_column_parse_flags(grn_ctx *ctx, |
| 31 | const char *error_message_tag, |
| 32 | const char *text, |
| 33 | const char *end) |
| 34 | { |
| 35 | grn_column_flags flags = 0; |
| 36 | while (text < end) { |
| 37 | size_t name_size; |
| 38 | |
| 39 | if (*text == '|' || *text == ' ') { |
| 40 | text += 1; |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | #define CHECK_FLAG(name) \ |
| 45 | name_size = strlen(#name); \ |
| 46 | if ((size_t)(end - text) >= name_size && \ |
| 47 | memcmp(text, #name, name_size) == 0) { \ |
| 48 | flags |= GRN_OBJ_##name; \ |
| 49 | text += name_size; \ |
| 50 | continue; \ |
| 51 | } |
| 52 | |
| 53 | CHECK_FLAG(COLUMN_SCALAR); |
| 54 | CHECK_FLAG(COLUMN_VECTOR); |
| 55 | CHECK_FLAG(COLUMN_INDEX); |
| 56 | CHECK_FLAG(COMPRESS_ZLIB); |
| 57 | CHECK_FLAG(COMPRESS_LZ4); |
| 58 | CHECK_FLAG(COMPRESS_ZSTD); |
| 59 | CHECK_FLAG(COMPRESS_OPENZL); |
| 60 | CHECK_FLAG(WITH_SECTION); |
| 61 | CHECK_FLAG(WITH_WEIGHT); |
| 62 | CHECK_FLAG(WITH_POSITION); |
| 63 | CHECK_FLAG(RING_BUFFER); |
| 64 | CHECK_FLAG(WEIGHT_BFLOAT16); |
| 65 | CHECK_FLAG(INDEX_SMALL); |
| 66 | CHECK_FLAG(INDEX_MEDIUM); |
| 67 | CHECK_FLAG(INDEX_LARGE); |
| 68 | CHECK_FLAG(WEIGHT_FLOAT32); |
| 69 | CHECK_FLAG(MISSING_ADD); |
| 70 | CHECK_FLAG(MISSING_IGNORE); |
| 71 | CHECK_FLAG(MISSING_NIL); |
| 72 | CHECK_FLAG(INVALID_ERROR); |
| 73 | CHECK_FLAG(INVALID_WARN); |
| 74 | CHECK_FLAG(INVALID_IGNORE); |
| 75 | CHECK_FLAG(COMPRESS_FILTER_SHUFFLE); |
| 76 | CHECK_FLAG(COMPRESS_FILTER_BYTE_DELTA); |
| 77 | CHECK_FLAG(COMPRESS_FILTER_TRUNCATE_PRECISION_1BYTE); |
| 78 | CHECK_FLAG(COMPRESS_FILTER_TRUNCATE_PRECISION_2BYTES); |
| 79 | |
| 80 | #undef CHECK_FLAG |
| 81 | |
| 82 | ERR(GRN_INVALID_ARGUMENT, |
| 83 | "%s unknown flag: <%.*s>", |
| 84 | error_message_tag, |
| 85 | (int)(end - text), |
| 86 | text); |
| 87 | return 0; |
| 88 | } |
| 89 | return flags; |
| 90 | } |
| 91 | |
| 92 | static grn_rc |
| 93 | command_column_create_resolve_source_name(grn_ctx *ctx, |
| 94 | grn_obj *table, |
| 95 | const char *source_name, |
| 96 | uint32_t source_name_length, |
| 97 | grn_obj *source_ids) |
| 98 | { |
| 99 | grn_obj *column; |
| 100 | |
| 101 | column = grn_obj_column(ctx, table, source_name, source_name_length); |
| 102 | if (!column) { |
| 103 | ERR(GRN_INVALID_ARGUMENT, |
| 104 | "[column][create] nonexistent source: <%.*s>", |
| 105 | source_name_length, |
| 106 | source_name); |
| 107 | return ctx->rc; |
| 108 | } |
| 109 | |
| 110 | if (column->header.type == GRN_ACCESSOR) { |
| 111 | if (strncmp(source_name, "_key", source_name_length) == 0) { |
| 112 | grn_id source_id = grn_obj_id(ctx, table); |
| 113 | GRN_UINT32_PUT(ctx, source_ids, source_id); |
| 114 | } else { |
| 115 | ERR(GRN_INVALID_ARGUMENT, |
| 116 | "[column][create] pseudo column except <_key> is invalid: <%.*s>", |
| 117 | source_name_length, |
| 118 | source_name); |
| 119 | } |
| 120 | } else { |
| 121 | grn_id source_id = grn_obj_id(ctx, column); |
| 122 | GRN_UINT32_PUT(ctx, source_ids, source_id); |
| 123 | } |
| 124 | grn_obj_unlink(ctx, column); |
| 125 | |
| 126 | return ctx->rc; |
| 127 | } |
| 128 | |
| 129 | static grn_rc |
| 130 | command_column_create_resolve_source_names(grn_ctx *ctx, |
| 131 | grn_obj *table, |
| 132 | grn_obj *source_names, |
| 133 | grn_obj *source_ids) |
| 134 | { |
| 135 | size_t i, names_length; |
| 136 | size_t start, source_name_length; |
| 137 | const char *names; |
| 138 | |
| 139 | names = GRN_TEXT_VALUE(source_names); |
| 140 | start = 0; |
| 141 | source_name_length = 0; |
| 142 | names_length = GRN_TEXT_LEN(source_names); |
| 143 | for (i = 0; i < names_length; i++) { |
| 144 | switch (names[i]) { |
| 145 | case ' ': |
| 146 | if (source_name_length == 0) { |
| 147 | start++; |
| 148 | } |
| 149 | break; |
| 150 | case ',': |
| 151 | { |
| 152 | grn_rc rc; |
| 153 | const char *source_name = names + start; |
| 154 | rc = command_column_create_resolve_source_name( |
| 155 | ctx, |
| 156 | table, |
| 157 | source_name, |
| 158 | (uint32_t)source_name_length, |
| 159 | source_ids); |
| 160 | if (rc) { |
| 161 | return rc; |
| 162 | } |
| 163 | start = i + 1; |
| 164 | source_name_length = 0; |
| 165 | } |
| 166 | break; |
| 167 | default: |
| 168 | source_name_length++; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (source_name_length > 0) { |
| 174 | grn_rc rc; |
| 175 | const char *source_name = names + start; |
| 176 | rc = command_column_create_resolve_source_name(ctx, |
| 177 | table, |
| 178 | source_name, |
| 179 | (uint32_t)source_name_length, |
| 180 | source_ids); |
| 181 | if (rc) { |
| 182 | return rc; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return GRN_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | typedef struct ProgressCallbackData { |
| 190 | grn_obj common_tag; |
| 191 | grn_obj vectorize_tag; |
| 192 | grn_obj cluster_tag; |
| 193 | grn_obj load_tag; |
| 194 | grn_obj commit_tag; |
| 195 | grn_log_level log_level; |
| 196 | grn_progress_logger logger; |
| 197 | grn_progress_index_phase current_phase; |
| 198 | } ProgressCallbackData; |
| 199 | |
| 200 | static void |
| 201 | progress_callback_data_init(grn_ctx *ctx, ProgressCallbackData *data) |
| 202 | { |
| 203 | GRN_VOID_INIT(&(data->common_tag)); |
| 204 | GRN_VOID_INIT(&(data->vectorize_tag)); |
| 205 | GRN_VOID_INIT(&(data->cluster_tag)); |
| 206 | GRN_VOID_INIT(&(data->load_tag)); |
| 207 | GRN_VOID_INIT(&(data->commit_tag)); |
| 208 | data->log_level = GRN_LOG_DEBUG; |
| 209 | memset(&(data->logger), 0, sizeof(grn_progress_logger)); |
| 210 | data->current_phase = GRN_PROGRESS_INDEX_INVALID; |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | progress_callback_data_fin(grn_ctx *ctx, ProgressCallbackData *data) |
| 215 | { |
| 216 | grn_progress_logger_fin(ctx, &(data->logger)); |
| 217 | GRN_OBJ_FIN(ctx, &(data->common_tag)); |
| 218 | GRN_OBJ_FIN(ctx, &(data->vectorize_tag)); |
| 219 | GRN_OBJ_FIN(ctx, &(data->cluster_tag)); |
| 220 | GRN_OBJ_FIN(ctx, &(data->load_tag)); |
| 221 | GRN_OBJ_FIN(ctx, &(data->commit_tag)); |
| 222 | } |
| 223 | |
| 224 | static void |
| 225 | progress_callback(grn_ctx *ctx, grn_progress *progress, void *user_data) |
| 226 | { |
| 227 | if (grn_progress_get_type(ctx, progress) != GRN_PROGRESS_INDEX) { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | ProgressCallbackData *data = user_data; |
| 232 | grn_progress_index_phase phase = grn_progress_index_get_phase(ctx, progress); |
| 233 | if (data->current_phase != phase) { |
| 234 | grn_progress_logger_fin(ctx, &(data->logger)); |
| 235 | data->current_phase = phase; |
| 236 | switch (phase) { |
| 237 | case GRN_PROGRESS_INDEX_INITIALIZE: |
| 238 | GRN_LOG(ctx, |
| 239 | data->log_level, |
| 240 | "%s[initialize]", |
| 241 | GRN_TEXT_VALUE(&(data->common_tag))); |
| 242 | break; |
| 243 | case GRN_PROGRESS_INDEX_VECTORIZE: |
| 244 | grn_progress_logger_init( |
| 245 | ctx, |
| 246 | &(data->logger), |
| 247 | GRN_TEXT_VALUE(&(data->vectorize_tag)), |
| 248 | "records", |
| 249 | grn_progress_index_get_n_target_records(ctx, progress)); |
| 250 | data->logger.log_level = data->log_level; |
| 251 | break; |
| 252 | case GRN_PROGRESS_INDEX_CLUSTER: |
| 253 | grn_progress_logger_init( |
| 254 | ctx, |
| 255 | &(data->logger), |
| 256 | GRN_TEXT_VALUE(&(data->cluster_tag)), |
| 257 | "records", |
| 258 | grn_progress_index_get_n_target_records(ctx, progress)); |
| 259 | data->logger.log_level = data->log_level; |
| 260 | break; |
| 261 | case GRN_PROGRESS_INDEX_LOAD: |
| 262 | grn_progress_logger_init( |
| 263 | ctx, |
| 264 | &(data->logger), |
| 265 | GRN_TEXT_VALUE(&(data->load_tag)), |
| 266 | "records", |
| 267 | grn_progress_index_get_n_target_records(ctx, progress)); |
| 268 | data->logger.log_level = data->log_level; |
| 269 | break; |
| 270 | case GRN_PROGRESS_INDEX_COMMIT: |
| 271 | grn_progress_logger_init( |
| 272 | ctx, |
| 273 | &(data->logger), |
| 274 | GRN_TEXT_VALUE(&(data->commit_tag)), |
| 275 | "terms", |
| 276 | grn_progress_index_get_n_target_terms(ctx, progress)); |
| 277 | data->logger.log_level = data->log_level; |
| 278 | break; |
| 279 | case GRN_PROGRESS_INDEX_FINALIZE: |
| 280 | GRN_LOG(ctx, |
| 281 | data->log_level, |
| 282 | "%s[finalize]", |
| 283 | GRN_TEXT_VALUE(&(data->common_tag))); |
| 284 | break; |
| 285 | case GRN_PROGRESS_INDEX_DONE: |
| 286 | GRN_LOG(ctx, |
| 287 | data->log_level, |
| 288 | "%s[done]", |
| 289 | GRN_TEXT_VALUE(&(data->common_tag))); |
| 290 | break; |
| 291 | default: |
| 292 | break; |
| 293 | } |
| 294 | } else { |
| 295 | switch (phase) { |
| 296 | case GRN_PROGRESS_INDEX_VECTORIZE: |
| 297 | case GRN_PROGRESS_INDEX_CLUSTER: |
| 298 | case GRN_PROGRESS_INDEX_LOAD: |
| 299 | data->logger.n_processed_targets = |
| 300 | grn_progress_index_get_n_processed_records(ctx, progress); |
| 301 | grn_progress_logger_log(ctx, &(data->logger)); |
| 302 | break; |
| 303 | case GRN_PROGRESS_INDEX_COMMIT: |
| 304 | data->logger.n_processed_targets = |
| 305 | grn_progress_index_get_n_processed_terms(ctx, progress); |
| 306 | grn_progress_logger_log(ctx, &(data->logger)); |
| 307 | break; |
| 308 | default: |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static grn_obj * |
| 315 | command_column_create(grn_ctx *ctx, |
| 316 | int nargs, |
| 317 | grn_obj **args, |
| 318 | grn_user_data *user_data) |
| 319 | { |
| 320 | bool succeeded = true; |
| 321 | bool use_progress_callback = (!grn_ctx_get_progress_callback(ctx)); |
| 322 | ProgressCallbackData progress_callback_data; |
| 323 | progress_callback_data_init(ctx, &progress_callback_data); |
| 324 | grn_obj *table; |
| 325 | grn_obj *column; |
| 326 | grn_obj *table_raw; |
| 327 | grn_obj *name; |
| 328 | grn_obj *flags_raw; |
| 329 | grn_obj *type_raw; |
| 330 | grn_obj *source_raw; |
| 331 | grn_obj *path_raw; |
| 332 | grn_obj *generator; |
| 333 | grn_column_flags flags; |
| 334 | grn_obj *type = NULL; |
| 335 | |
| 336 | table_raw = grn_plugin_proc_get_var(ctx, user_data, "table", -1); |
| 337 | name = grn_plugin_proc_get_var(ctx, user_data, "name", -1); |
| 338 | flags_raw = grn_plugin_proc_get_var(ctx, user_data, "flags", -1); |
| 339 | type_raw = grn_plugin_proc_get_var(ctx, user_data, "type", -1); |
| 340 | source_raw = grn_plugin_proc_get_var(ctx, user_data, "source", -1); |
| 341 | path_raw = grn_plugin_proc_get_var(ctx, user_data, "path", -1); |
| 342 | generator = grn_plugin_proc_get_var(ctx, user_data, "generator", -1); |
| 343 | progress_callback_data.log_level = |
| 344 | grn_plugin_proc_get_var_log_level(ctx, |
| 345 | user_data, |
| 346 | "progress_log_level", |
| 347 | -1, |
| 348 | progress_callback_data.log_level); |
| 349 | |
| 350 | table = |
| 351 | grn_ctx_get(ctx, GRN_TEXT_VALUE(table_raw), (int)GRN_TEXT_LEN(table_raw)); |
| 352 | if (!table) { |
| 353 | GRN_PLUGIN_ERROR(ctx, |
| 354 | GRN_INVALID_ARGUMENT, |
| 355 | "[column][create] table doesn't exist: <%.*s>", |
| 356 | (int)GRN_TEXT_LEN(table_raw), |
| 357 | GRN_TEXT_VALUE(table_raw)); |
| 358 | succeeded = false; |
| 359 | goto exit; |
| 360 | } |
| 361 | |
| 362 | { |
| 363 | const char *rest; |
| 364 | flags = (grn_column_flags)(grn_atoi(GRN_TEXT_VALUE(flags_raw), |
| 365 | GRN_BULK_CURR(flags_raw), |
| 366 | &rest)); |
| 367 | if (GRN_TEXT_VALUE(flags_raw) == rest) { |
| 368 | flags = grn_proc_column_parse_flags(ctx, |
| 369 | "[column][create][flags]", |
| 370 | GRN_TEXT_VALUE(flags_raw), |
| 371 | GRN_BULK_CURR(flags_raw)); |
| 372 | if (ctx->rc) { |
| 373 | succeeded = false; |
| 374 | goto exit; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | type = |
| 380 | grn_ctx_get(ctx, GRN_TEXT_VALUE(type_raw), (int)GRN_TEXT_LEN(type_raw)); |
| 381 | if (!type) { |
| 382 | GRN_PLUGIN_ERROR(ctx, |
| 383 | GRN_INVALID_ARGUMENT, |
| 384 | "[column][create] type doesn't exist: <%.*s>", |
| 385 | (int)GRN_TEXT_LEN(type_raw), |
| 386 | GRN_TEXT_VALUE(type_raw)); |
| 387 | succeeded = false; |
| 388 | goto exit; |
| 389 | } |
| 390 | |
| 391 | if (GRN_TEXT_LEN(name) == 0) { |
| 392 | GRN_PLUGIN_ERROR(ctx, |
| 393 | GRN_INVALID_ARGUMENT, |
| 394 | "[column][create] name is missing"); |
| 395 | succeeded = false; |
| 396 | goto exit; |
| 397 | } |
| 398 | |
| 399 | const char *path = NULL; |
| 400 | if (GRN_TEXT_LEN(path_raw) > 0) { |
| 401 | GRN_TEXT_PUTC(ctx, path_raw, '\0'); |
| 402 | path = GRN_TEXT_VALUE(path_raw); |
| 403 | } |
| 404 | flags |= GRN_OBJ_PERSISTENT; |
| 405 | column = grn_column_create(ctx, |
| 406 | table, |
| 407 | GRN_TEXT_VALUE(name), |
| 408 | (unsigned int)GRN_TEXT_LEN(name), |
| 409 | path, |
| 410 | flags, |
| 411 | type); |
| 412 | if (!column) { |
| 413 | succeeded = false; |
| 414 | goto exit; |
| 415 | } |
| 416 | |
| 417 | GRN_OBJ_FIN(ctx, &(progress_callback_data.common_tag)); |
| 418 | GRN_OBJ_FIN(ctx, &(progress_callback_data.vectorize_tag)); |
| 419 | GRN_OBJ_FIN(ctx, &(progress_callback_data.cluster_tag)); |
| 420 | GRN_OBJ_FIN(ctx, &(progress_callback_data.load_tag)); |
| 421 | GRN_OBJ_FIN(ctx, &(progress_callback_data.commit_tag)); |
| 422 | GRN_TEXT_INIT(&(progress_callback_data.common_tag), 0); |
| 423 | GRN_TEXT_INIT(&(progress_callback_data.vectorize_tag), 0); |
| 424 | GRN_TEXT_INIT(&(progress_callback_data.cluster_tag), 0); |
| 425 | GRN_TEXT_INIT(&(progress_callback_data.load_tag), 0); |
| 426 | GRN_TEXT_INIT(&(progress_callback_data.commit_tag), 0); |
| 427 | grn_text_printf(ctx, |
| 428 | &(progress_callback_data.common_tag), |
| 429 | "[column][create][progress][%.*s.%.*s]", |
| 430 | (int)GRN_TEXT_LEN(table_raw), |
| 431 | GRN_TEXT_VALUE(table_raw), |
| 432 | (int)(GRN_TEXT_LEN(name)), |
| 433 | GRN_TEXT_VALUE(name)); |
| 434 | GRN_TEXT_PUTC(ctx, &(progress_callback_data.common_tag), '\0'); |
| 435 | grn_text_printf(ctx, |
| 436 | &(progress_callback_data.vectorize_tag), |
| 437 | "%s[vectorize]", |
| 438 | GRN_TEXT_VALUE(&(progress_callback_data.common_tag))); |
| 439 | GRN_TEXT_PUTC(ctx, &(progress_callback_data.vectorize_tag), '\0'); |
| 440 | grn_text_printf(ctx, |
| 441 | &(progress_callback_data.cluster_tag), |
| 442 | "%s[cluster]", |
| 443 | GRN_TEXT_VALUE(&(progress_callback_data.common_tag))); |
| 444 | GRN_TEXT_PUTC(ctx, &(progress_callback_data.cluster_tag), '\0'); |
| 445 | grn_text_printf(ctx, |
| 446 | &(progress_callback_data.load_tag), |
| 447 | "%s[load]", |
| 448 | GRN_TEXT_VALUE(&(progress_callback_data.common_tag))); |
| 449 | GRN_TEXT_PUTC(ctx, &(progress_callback_data.load_tag), '\0'); |
| 450 | grn_text_printf(ctx, |
| 451 | &(progress_callback_data.commit_tag), |
| 452 | "%s[commit]", |
| 453 | GRN_TEXT_VALUE(&(progress_callback_data.common_tag))); |
| 454 | GRN_TEXT_PUTC(ctx, &(progress_callback_data.commit_tag), '\0'); |
| 455 | grn_ctx_set_progress_callback(ctx, |
| 456 | progress_callback, |
| 457 | &progress_callback_data); |
| 458 | if (GRN_TEXT_LEN(generator) > 0) { |
| 459 | grn_obj_set_info(ctx, column, GRN_INFO_GENERATOR, generator); |
| 460 | if (ctx->rc != GRN_SUCCESS) { |
| 461 | grn_rc original_rc = ctx->rc; |
| 462 | ctx->rc = GRN_SUCCESS; |
| 463 | grn_obj_remove(ctx, column); |
| 464 | ctx->rc = original_rc; |
| 465 | succeeded = false; |
| 466 | goto exit; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | if (GRN_TEXT_LEN(source_raw) > 0) { |
| 471 | grn_obj source_ids; |
| 472 | GRN_UINT32_INIT(&source_ids, GRN_OBJ_VECTOR); |
| 473 | grn_obj *source_table; |
| 474 | if (column->header.type == GRN_COLUMN_INDEX) { |
| 475 | source_table = type; |
| 476 | } else { |
| 477 | source_table = table; |
| 478 | } |
| 479 | grn_rc rc = command_column_create_resolve_source_names(ctx, |
| 480 | source_table, |
| 481 | source_raw, |
| 482 | &source_ids); |
| 483 | if (rc == GRN_SUCCESS && GRN_BULK_VSIZE(&source_ids) > 0) { |
| 484 | grn_obj_set_info(ctx, column, GRN_INFO_SOURCE, &source_ids); |
| 485 | rc = ctx->rc; |
| 486 | } |
| 487 | GRN_OBJ_FIN(ctx, &source_ids); |
| 488 | if (rc != GRN_SUCCESS) { |
| 489 | grn_rc original_rc = ctx->rc; |
| 490 | ctx->rc = GRN_SUCCESS; |
| 491 | grn_obj_remove(ctx, column); |
| 492 | ctx->rc = original_rc; |
| 493 | succeeded = false; |
| 494 | goto exit; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | grn_obj_unlink(ctx, column); |
| 499 | |
| 500 | exit: |
| 501 | grn_ctx_output_bool(ctx, succeeded); |
| 502 | if (table) { |
| 503 | grn_obj_unlink(ctx, table); |
| 504 | } |
| 505 | if (type) { |
| 506 | grn_obj_unlink(ctx, type); |
| 507 | } |
| 508 | if (use_progress_callback) { |
| 509 | grn_ctx_set_progress_callback(ctx, NULL, NULL); |
| 510 | } |
| 511 | progress_callback_data_fin(ctx, &progress_callback_data); |
| 512 | |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | void |
| 517 | grn_proc_init_column_create(grn_ctx *ctx) |
| 518 | { |
| 519 | grn_expr_var vars[8]; |
| 520 | unsigned int n_vars = 0; |
| 521 | |
| 522 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "table", -1); |
| 523 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "name", -1); |
| 524 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "flags", -1); |
| 525 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "type", -1); |
| 526 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "source", -1); |
| 527 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "path", -1); |
| 528 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "generator", -1); |
| 529 | grn_plugin_expr_var_init(ctx, &(vars[n_vars++]), "progress_log_level", -1); |
| 530 | grn_plugin_command_create(ctx, |
| 531 | "column_create", |
| 532 | -1, |
| 533 | command_column_create, |
| 534 | n_vars, |
| 535 | vars); |
| 536 | } |
| 537 | |
| 538 | static grn_obj * |
| 539 | command_column_remove(grn_ctx *ctx, |
| 540 | int nargs, |
| 541 | grn_obj **args, |
| 542 | grn_user_data *user_data) |
| 543 | { |
| 544 | grn_obj *table_raw; |
| 545 | grn_obj *name; |
| 546 | grn_obj *table; |
| 547 | grn_obj *column; |
| 548 | char fullname[GRN_TABLE_MAX_KEY_SIZE]; |
| 549 | int fullname_len; |
| 550 | |
| 551 | table_raw = grn_plugin_proc_get_var(ctx, user_data, "table", -1); |
| 552 | name = grn_plugin_proc_get_var(ctx, user_data, "name", -1); |
| 553 | |
| 554 | table = |
| 555 | grn_ctx_get(ctx, GRN_TEXT_VALUE(table_raw), (int)GRN_TEXT_LEN(table_raw)); |
| 556 | if (!table) { |
| 557 | GRN_PLUGIN_ERROR(ctx, |
| 558 | GRN_INVALID_ARGUMENT, |
| 559 | "[column][remove] table isn't found: <%.*s>", |
| 560 | (int)GRN_TEXT_LEN(table_raw), |
| 561 | GRN_TEXT_VALUE(table_raw)); |
| 562 | grn_ctx_output_bool(ctx, false); |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | fullname_len = grn_obj_name(ctx, table, fullname, GRN_TABLE_MAX_KEY_SIZE); |
| 567 | if (fullname_len == 0) { |
| 568 | GRN_PLUGIN_ERROR(ctx, |
| 569 | GRN_INVALID_ARGUMENT, |
| 570 | "[column][remove] table isn't found: <%.*s>", |
| 571 | (int)GRN_TEXT_LEN(table_raw), |
| 572 | GRN_TEXT_VALUE(table_raw)); |
| 573 | grn_ctx_output_bool(ctx, false); |
| 574 | grn_obj_unref(ctx, table); |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | fullname[fullname_len] = GRN_DB_DELIMITER; |
| 579 | fullname_len++; |
| 580 | if ((size_t)fullname_len + GRN_TEXT_LEN(name) > GRN_TABLE_MAX_KEY_SIZE) { |
| 581 | GRN_PLUGIN_ERROR(ctx, |
| 582 | GRN_INVALID_ARGUMENT, |
| 583 | "[column][remove] column name is too long: <%d> > <%u>: " |
| 584 | "<%.*s>", |
| 585 | (int)GRN_TEXT_LEN(name), |
| 586 | GRN_TABLE_MAX_KEY_SIZE - fullname_len, |
| 587 | (int)GRN_TEXT_LEN(name), |
| 588 | GRN_TEXT_VALUE(name)); |
| 589 | grn_ctx_output_bool(ctx, false); |
| 590 | grn_obj_unref(ctx, table); |
| 591 | return NULL; |
| 592 | } |
| 593 | grn_memcpy(fullname + fullname_len, GRN_TEXT_VALUE(name), GRN_TEXT_LEN(name)); |
| 594 | fullname_len += (int)GRN_TEXT_LEN(name); |
| 595 | column = grn_ctx_get(ctx, fullname, fullname_len); |
| 596 | if (!column) { |
| 597 | GRN_PLUGIN_ERROR(ctx, |
| 598 | GRN_INVALID_ARGUMENT, |
| 599 | "[column][remove] column isn't found: <%.*s%c%.*s>", |
| 600 | (int)GRN_TEXT_LEN(table_raw), |
| 601 | GRN_TEXT_VALUE(table_raw), |
| 602 | GRN_DB_DELIMITER, |
| 603 | (int)GRN_TEXT_LEN(name), |
| 604 | GRN_TEXT_VALUE(name)); |
| 605 | grn_ctx_output_bool(ctx, false); |
| 606 | grn_obj_unref(ctx, table); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | grn_obj_remove(ctx, column); |
| 611 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 612 | grn_obj_unref(ctx, table); |
| 613 | return NULL; |
| 614 | } |
| 615 | |
| 616 | void |
| 617 | grn_proc_init_column_remove(grn_ctx *ctx) |
| 618 | { |
| 619 | grn_expr_var vars[2]; |
| 620 | |
| 621 | grn_plugin_expr_var_init(ctx, &(vars[0]), "table", -1); |
| 622 | grn_plugin_expr_var_init(ctx, &(vars[1]), "name", -1); |
| 623 | grn_plugin_command_create(ctx, |
| 624 | "column_remove", |
| 625 | -1, |
| 626 | command_column_remove, |
| 627 | 2, |
| 628 | vars); |
| 629 | } |
| 630 | |
| 631 | static grn_obj * |
| 632 | command_column_rename(grn_ctx *ctx, |
| 633 | int nargs, |
| 634 | grn_obj **args, |
| 635 | grn_user_data *user_data) |
| 636 | { |
| 637 | grn_rc rc = GRN_SUCCESS; |
| 638 | grn_obj *table_raw; |
| 639 | grn_obj *name; |
| 640 | grn_obj *new_name; |
| 641 | grn_obj *table = NULL; |
| 642 | grn_obj *column = NULL; |
| 643 | |
| 644 | table_raw = grn_plugin_proc_get_var(ctx, user_data, "table", -1); |
| 645 | name = grn_plugin_proc_get_var(ctx, user_data, "name", -1); |
| 646 | new_name = grn_plugin_proc_get_var(ctx, user_data, "new_name", -1); |
| 647 | |
| 648 | if (GRN_TEXT_LEN(table_raw) == 0) { |
| 649 | rc = GRN_INVALID_ARGUMENT; |
| 650 | GRN_PLUGIN_ERROR(ctx, rc, "[column][rename] table name isn't specified"); |
| 651 | goto exit; |
| 652 | } |
| 653 | |
| 654 | table = |
| 655 | grn_ctx_get(ctx, GRN_TEXT_VALUE(table_raw), (int)GRN_TEXT_LEN(table_raw)); |
| 656 | if (!table) { |
| 657 | rc = GRN_INVALID_ARGUMENT; |
| 658 | GRN_PLUGIN_ERROR(ctx, |
| 659 | rc, |
| 660 | "[column][rename] table isn't found: <%.*s>", |
| 661 | (int)GRN_TEXT_LEN(table_raw), |
| 662 | GRN_TEXT_VALUE(table_raw)); |
| 663 | goto exit; |
| 664 | } |
| 665 | |
| 666 | if (GRN_TEXT_LEN(name) == 0) { |
| 667 | rc = GRN_INVALID_ARGUMENT; |
| 668 | GRN_PLUGIN_ERROR(ctx, |
| 669 | rc, |
| 670 | "[column][rename] column name isn't specified: <%.*s>", |
| 671 | (int)GRN_TEXT_LEN(table_raw), |
| 672 | GRN_TEXT_VALUE(table_raw)); |
| 673 | goto exit; |
| 674 | } |
| 675 | |
| 676 | column = grn_obj_column(ctx, |
| 677 | table, |
| 678 | GRN_TEXT_VALUE(name), |
| 679 | (uint32_t)GRN_TEXT_LEN(name)); |
| 680 | if (!column) { |
| 681 | rc = GRN_INVALID_ARGUMENT; |
| 682 | GRN_PLUGIN_ERROR(ctx, |
| 683 | rc, |
| 684 | "[column][rename] column isn't found: <%.*s%c%.*s>", |
| 685 | (int)GRN_TEXT_LEN(table_raw), |
| 686 | GRN_TEXT_VALUE(table_raw), |
| 687 | GRN_DB_DELIMITER, |
| 688 | (int)GRN_TEXT_LEN(name), |
| 689 | GRN_TEXT_VALUE(name)); |
| 690 | goto exit; |
| 691 | } |
| 692 | |
| 693 | if (GRN_TEXT_LEN(new_name) == 0) { |
| 694 | rc = GRN_INVALID_ARGUMENT; |
| 695 | GRN_PLUGIN_ERROR(ctx, |
| 696 | rc, |
| 697 | "[column][rename] new column name isn't specified: " |
| 698 | "<%.*s%c%.*s>", |
| 699 | (int)GRN_TEXT_LEN(table_raw), |
| 700 | GRN_TEXT_VALUE(table_raw), |
| 701 | GRN_DB_DELIMITER, |
| 702 | (int)GRN_TEXT_LEN(name), |
| 703 | GRN_TEXT_VALUE(name)); |
| 704 | goto exit; |
| 705 | } |
| 706 | |
| 707 | rc = grn_column_rename(ctx, |
| 708 | column, |
| 709 | GRN_TEXT_VALUE(new_name), |
| 710 | (unsigned int)GRN_TEXT_LEN(new_name)); |
| 711 | if (rc != GRN_SUCCESS && ctx->rc == GRN_SUCCESS) { |
| 712 | GRN_PLUGIN_ERROR(ctx, |
| 713 | rc, |
| 714 | "[column][rename] failed to rename: " |
| 715 | "<%.*s%c%.*s> -> <%.*s%c%.*s>", |
| 716 | (int)GRN_TEXT_LEN(table_raw), |
| 717 | GRN_TEXT_VALUE(table_raw), |
| 718 | GRN_DB_DELIMITER, |
| 719 | (int)GRN_TEXT_LEN(name), |
| 720 | GRN_TEXT_VALUE(name), |
| 721 | (int)GRN_TEXT_LEN(table_raw), |
| 722 | GRN_TEXT_VALUE(table_raw), |
| 723 | GRN_DB_DELIMITER, |
| 724 | (int)GRN_TEXT_LEN(new_name), |
| 725 | GRN_TEXT_VALUE(new_name)); |
| 726 | goto exit; |
| 727 | } |
| 728 | |
| 729 | exit: |
| 730 | grn_ctx_output_bool(ctx, rc == GRN_SUCCESS); |
| 731 | if (column) { |
| 732 | grn_obj_unlink(ctx, column); |
| 733 | } |
| 734 | if (table) { |
| 735 | grn_obj_unlink(ctx, table); |
| 736 | } |
| 737 | return NULL; |
| 738 | } |
| 739 | |
| 740 | void |
| 741 | grn_proc_init_column_rename(grn_ctx *ctx) |
| 742 | { |
| 743 | grn_expr_var vars[3]; |
| 744 | |
| 745 | grn_plugin_expr_var_init(ctx, &(vars[0]), "table", -1); |
| 746 | grn_plugin_expr_var_init(ctx, &(vars[1]), "name", -1); |
| 747 | grn_plugin_expr_var_init(ctx, &(vars[2]), "new_name", -1); |
| 748 | grn_plugin_command_create(ctx, |
| 749 | "column_rename", |
| 750 | -1, |
| 751 | command_column_rename, |
| 752 | 3, |
| 753 | vars); |
| 754 | } |
| 755 | |
| 756 | static void |
| 757 | output_column_name(grn_ctx *ctx, grn_obj *column) |
| 758 | { |
| 759 | grn_obj bulk; |
| 760 | int name_len; |
| 761 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 762 | |
| 763 | GRN_TEXT_INIT(&bulk, GRN_OBJ_DO_SHALLOW_COPY); |
| 764 | name_len = grn_column_name(ctx, column, name, GRN_TABLE_MAX_KEY_SIZE); |
| 765 | GRN_TEXT_SET(ctx, &bulk, name, name_len); |
| 766 | |
| 767 | grn_ctx_output_obj(ctx, &bulk, NULL); |
| 768 | GRN_OBJ_FIN(ctx, &bulk); |
| 769 | } |
| 770 | |
| 771 | static int |
| 772 | output_column_info(grn_ctx *ctx, grn_obj *column) |
| 773 | { |
| 774 | grn_obj o; |
| 775 | grn_id id; |
| 776 | const char *type; |
| 777 | const char *path; |
| 778 | grn_obj generator; |
| 779 | |
| 780 | GRN_TEXT_INIT(&generator, 0); |
| 781 | switch (column->header.type) { |
| 782 | case GRN_COLUMN_FIX_SIZE: |
| 783 | type = "fix"; |
| 784 | break; |
| 785 | case GRN_COLUMN_VAR_SIZE: |
| 786 | type = "var"; |
| 787 | grn_obj_get_info(ctx, column, GRN_INFO_GENERATOR, &generator); |
| 788 | break; |
| 789 | case GRN_COLUMN_INDEX: |
| 790 | type = "index"; |
| 791 | break; |
| 792 | default: |
| 793 | GRN_LOG(ctx, |
| 794 | GRN_LOG_NOTICE, |
| 795 | "invalid header type %d\n", |
| 796 | column->header.type); |
| 797 | return 0; |
| 798 | } |
| 799 | id = grn_obj_id(ctx, column); |
| 800 | path = grn_obj_path(ctx, column); |
| 801 | GRN_TEXT_INIT(&o, 0); |
| 802 | grn_ctx_output_array_open(ctx, "COLUMN", 8); |
| 803 | grn_ctx_output_int64(ctx, id); |
| 804 | output_column_name(ctx, column); |
| 805 | grn_ctx_output_cstr(ctx, path); |
| 806 | grn_ctx_output_cstr(ctx, type); |
| 807 | grn_dump_column_create_flags(ctx, grn_column_get_flags(ctx, column), &o); |
| 808 | grn_ctx_output_obj(ctx, &o, NULL); |
| 809 | grn_proc_output_object_id_name(ctx, column->header.domain); |
| 810 | grn_proc_output_object_id_name(ctx, grn_obj_get_range(ctx, column)); |
| 811 | { |
| 812 | grn_db_obj *obj = (grn_db_obj *)column; |
| 813 | grn_id *s = obj->source; |
| 814 | size_t i = 0, n = obj->source_size / sizeof(grn_id); |
| 815 | grn_ctx_output_array_open(ctx, "SOURCES", (int)n); |
| 816 | for (i = 0; i < n; i++, s++) { |
| 817 | grn_proc_output_object_id_name(ctx, *s); |
| 818 | } |
| 819 | grn_ctx_output_array_close(ctx); |
| 820 | } |
| 821 | /* output_obj_source(ctx, (grn_db_obj *)column); */ |
| 822 | grn_ctx_output_str(ctx, GRN_TEXT_VALUE(&generator), GRN_TEXT_LEN(&generator)); |
| 823 | grn_ctx_output_array_close(ctx); |
| 824 | GRN_OBJ_FIN(ctx, &generator); |
| 825 | GRN_OBJ_FIN(ctx, &o); |
| 826 | return 1; |
| 827 | } |
| 828 | |
| 829 | static grn_obj * |
| 830 | command_column_list(grn_ctx *ctx, |
| 831 | int nargs, |
| 832 | grn_obj **args, |
| 833 | grn_user_data *user_data) |
| 834 | { |
| 835 | grn_obj *table_raw; |
| 836 | grn_obj *table; |
| 837 | grn_hash *cols; |
| 838 | grn_obj *col; |
| 839 | int column_list_size = -1; |
| 840 | |
| 841 | table_raw = grn_plugin_proc_get_var(ctx, user_data, "table", -1); |
| 842 | |
| 843 | table = |
| 844 | grn_ctx_get(ctx, GRN_TEXT_VALUE(table_raw), (int)GRN_TEXT_LEN(table_raw)); |
| 845 | if (!table) { |
| 846 | GRN_PLUGIN_ERROR(ctx, |
| 847 | GRN_INVALID_ARGUMENT, |
| 848 | "[column][list] table doesn't exist: <%.*s>", |
| 849 | (int)GRN_TEXT_LEN(table_raw), |
| 850 | GRN_TEXT_VALUE(table_raw)); |
| 851 | return NULL; |
| 852 | } |
| 853 | |
| 854 | if (!grn_obj_is_table(ctx, table)) { |
| 855 | const char *type_name; |
| 856 | type_name = grn_obj_type_to_string(table->header.type); |
| 857 | grn_obj_unlink(ctx, table); |
| 858 | GRN_PLUGIN_ERROR(ctx, |
| 859 | GRN_INVALID_ARGUMENT, |
| 860 | "[column][list] not table: <%.*s>: <%s>", |
| 861 | (int)GRN_TEXT_LEN(table_raw), |
| 862 | GRN_TEXT_VALUE(table_raw), |
| 863 | type_name); |
| 864 | return NULL; |
| 865 | } |
| 866 | |
| 867 | column_list_size = 1; /* [header, (key), (COLUMNS)] */ |
| 868 | if (table->header.type != GRN_TABLE_NO_KEY) { |
| 869 | column_list_size++; |
| 870 | } |
| 871 | cols = grn_hash_create(ctx, |
| 872 | NULL, |
| 873 | sizeof(grn_id), |
| 874 | 0, |
| 875 | GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); |
| 876 | if (!cols) { |
| 877 | grn_obj_unlink(ctx, table); |
| 878 | GRN_PLUGIN_ERROR(ctx, |
| 879 | GRN_INVALID_ARGUMENT, |
| 880 | "[column][list] " |
| 881 | "failed to create temporary table to list columns: <%.*s>", |
| 882 | (int)GRN_TEXT_LEN(table_raw), |
| 883 | GRN_TEXT_VALUE(table_raw)); |
| 884 | return NULL; |
| 885 | } |
| 886 | |
| 887 | column_list_size += grn_table_columns(ctx, table, NULL, 0, (grn_obj *)cols); |
| 888 | |
| 889 | grn_ctx_output_array_open(ctx, "COLUMN_LIST", column_list_size); |
| 890 | grn_ctx_output_array_open(ctx, "HEADER", 9); |
| 891 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 892 | grn_ctx_output_cstr(ctx, "id"); |
| 893 | grn_ctx_output_cstr(ctx, "UInt32"); |
| 894 | grn_ctx_output_array_close(ctx); |
| 895 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 896 | grn_ctx_output_cstr(ctx, "name"); |
| 897 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 898 | grn_ctx_output_array_close(ctx); |
| 899 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 900 | grn_ctx_output_cstr(ctx, "path"); |
| 901 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 902 | grn_ctx_output_array_close(ctx); |
| 903 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 904 | grn_ctx_output_cstr(ctx, "type"); |
| 905 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 906 | grn_ctx_output_array_close(ctx); |
| 907 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 908 | grn_ctx_output_cstr(ctx, "flags"); |
| 909 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 910 | grn_ctx_output_array_close(ctx); |
| 911 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 912 | grn_ctx_output_cstr(ctx, "domain"); |
| 913 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 914 | grn_ctx_output_array_close(ctx); |
| 915 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 916 | grn_ctx_output_cstr(ctx, "range"); |
| 917 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 918 | grn_ctx_output_array_close(ctx); |
| 919 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 920 | grn_ctx_output_cstr(ctx, "source"); |
| 921 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 922 | grn_ctx_output_array_close(ctx); |
| 923 | grn_ctx_output_array_open(ctx, "PROPERTY", 2); |
| 924 | grn_ctx_output_cstr(ctx, "generator"); |
| 925 | grn_ctx_output_cstr(ctx, "ShortText"); |
| 926 | grn_ctx_output_array_close(ctx); |
| 927 | grn_ctx_output_array_close(ctx); |
| 928 | |
| 929 | if ((col = grn_obj_column(ctx, |
| 930 | table, |
| 931 | GRN_COLUMN_NAME_KEY, |
| 932 | GRN_COLUMN_NAME_KEY_LEN))) { |
| 933 | int name_len; |
| 934 | char name_buf[GRN_TABLE_MAX_KEY_SIZE]; |
| 935 | grn_id id; |
| 936 | grn_obj buf; |
| 937 | GRN_TEXT_INIT(&buf, 0); |
| 938 | grn_ctx_output_array_open(ctx, "COLUMN", 9); |
| 939 | id = grn_obj_id(ctx, table); |
| 940 | grn_ctx_output_int64(ctx, id); |
| 941 | grn_ctx_output_cstr(ctx, GRN_COLUMN_NAME_KEY); |
| 942 | grn_ctx_output_cstr(ctx, ""); |
| 943 | grn_ctx_output_cstr(ctx, ""); |
| 944 | grn_dump_column_create_flags(ctx, 0, &buf); |
| 945 | grn_ctx_output_obj(ctx, &buf, NULL); |
| 946 | name_len = grn_obj_name(ctx, table, name_buf, GRN_TABLE_MAX_KEY_SIZE); |
| 947 | grn_ctx_output_str(ctx, name_buf, (size_t)name_len); |
| 948 | grn_proc_output_object_id_name(ctx, table->header.domain); |
| 949 | grn_ctx_output_array_open(ctx, "SOURCES", 0); |
| 950 | grn_ctx_output_array_close(ctx); |
| 951 | grn_ctx_output_str(ctx, "", 0); |
| 952 | grn_ctx_output_array_close(ctx); |
| 953 | GRN_OBJ_FIN(ctx, &buf); |
| 954 | grn_obj_unlink(ctx, col); |
| 955 | } |
| 956 | { |
| 957 | grn_id *key; |
| 958 | GRN_HASH_EACH(ctx, cols, id, &key, NULL, NULL, { |
| 959 | if ((col = grn_ctx_at(ctx, *key))) { |
| 960 | output_column_info(ctx, col); |
| 961 | grn_obj_unlink(ctx, col); |
| 962 | } |
| 963 | }); |
| 964 | } |
| 965 | grn_ctx_output_array_close(ctx); |
| 966 | grn_hash_close(ctx, cols); |
| 967 | grn_obj_unlink(ctx, table); |
| 968 | |
| 969 | return NULL; |
| 970 | } |
| 971 | |
| 972 | void |
| 973 | grn_proc_init_column_list(grn_ctx *ctx) |
| 974 | { |
| 975 | grn_expr_var vars[1]; |
| 976 | |
| 977 | grn_plugin_expr_var_init(ctx, &(vars[0]), "table", -1); |
| 978 | grn_plugin_command_create(ctx, |
| 979 | "column_list", |
| 980 | -1, |
| 981 | command_column_list, |
| 982 | 1, |
| 983 | vars); |
| 984 | } |
| 985 | |
| 986 | static grn_obj * |
| 987 | command_column_copy(grn_ctx *ctx, |
| 988 | int nargs, |
| 989 | grn_obj **args, |
| 990 | grn_user_data *user_data) |
| 991 | { |
| 992 | grn_obj *from_table = NULL; |
| 993 | grn_obj *from_column = NULL; |
| 994 | grn_obj *to_table = NULL; |
| 995 | grn_obj *to_column = NULL; |
| 996 | grn_obj *from_table_name; |
| 997 | grn_obj *from_column_name; |
| 998 | grn_obj *to_table_name; |
| 999 | grn_obj *to_column_name; |
| 1000 | |
| 1001 | from_table_name = grn_plugin_proc_get_var(ctx, user_data, "from_table", -1); |
| 1002 | from_column_name = grn_plugin_proc_get_var(ctx, user_data, "from_name", -1); |
| 1003 | to_table_name = grn_plugin_proc_get_var(ctx, user_data, "to_table", -1); |
| 1004 | to_column_name = grn_plugin_proc_get_var(ctx, user_data, "to_name", -1); |
| 1005 | |
| 1006 | from_table = grn_proc_get_value_object(ctx, |
| 1007 | from_table_name, |
| 1008 | "[column][copy][from_table]"); |
| 1009 | if (!from_table) { |
| 1010 | goto exit; |
| 1011 | } |
| 1012 | from_column = grn_proc_get_value_column(ctx, |
| 1013 | from_column_name, |
| 1014 | from_table, |
| 1015 | "[column][copy][from_name]"); |
| 1016 | if (!from_column) { |
| 1017 | goto exit; |
| 1018 | } |
| 1019 | to_table = |
| 1020 | grn_proc_get_value_object(ctx, to_table_name, "[column][copy][to_table]"); |
| 1021 | if (!to_table) { |
| 1022 | goto exit; |
| 1023 | } |
| 1024 | to_column = grn_proc_get_value_column(ctx, |
| 1025 | to_column_name, |
| 1026 | to_table, |
| 1027 | "[column][copy][to_name]"); |
| 1028 | if (!to_column) { |
| 1029 | goto exit; |
| 1030 | } |
| 1031 | |
| 1032 | grn_column_copy(ctx, from_column, to_column); |
| 1033 | |
| 1034 | exit: |
| 1035 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 1036 | |
| 1037 | if (to_column) { |
| 1038 | grn_obj_unref(ctx, to_column); |
| 1039 | } |
| 1040 | if (to_table) { |
| 1041 | grn_obj_unref(ctx, to_table); |
| 1042 | } |
| 1043 | if (from_column) { |
| 1044 | grn_obj_unref(ctx, from_column); |
| 1045 | } |
| 1046 | if (from_table) { |
| 1047 | grn_obj_unref(ctx, from_table); |
| 1048 | } |
| 1049 | |
| 1050 | return NULL; |
| 1051 | } |
| 1052 | |
| 1053 | void |
| 1054 | grn_proc_init_column_copy(grn_ctx *ctx) |
| 1055 | { |
| 1056 | grn_expr_var vars[4]; |
| 1057 | |
| 1058 | grn_plugin_expr_var_init(ctx, &(vars[0]), "from_table", -1); |
| 1059 | grn_plugin_expr_var_init(ctx, &(vars[1]), "from_name", -1); |
| 1060 | grn_plugin_expr_var_init(ctx, &(vars[2]), "to_table", -1); |
| 1061 | grn_plugin_expr_var_init(ctx, &(vars[3]), "to_name", -1); |
| 1062 | grn_plugin_command_create(ctx, |
| 1063 | "column_copy", |
| 1064 | -1, |
| 1065 | command_column_copy, |
| 1066 | 4, |
| 1067 | vars); |
| 1068 | } |
| 1069 | |
| 1070 | static grn_obj * |
| 1071 | command_column_create_similar(grn_ctx *ctx, |
| 1072 | int nargs, |
| 1073 | grn_obj **args, |
| 1074 | grn_user_data *user_data) |
| 1075 | { |
| 1076 | grn_obj *table_name; |
| 1077 | grn_obj *table = NULL; |
| 1078 | grn_obj *name; |
| 1079 | grn_obj *base_column_name; |
| 1080 | grn_obj *base_column = NULL; |
| 1081 | grn_obj *column = NULL; |
| 1082 | |
| 1083 | table_name = grn_plugin_proc_get_var(ctx, user_data, "table", -1); |
| 1084 | name = grn_plugin_proc_get_var(ctx, user_data, "name", -1); |
| 1085 | base_column_name = grn_plugin_proc_get_var(ctx, user_data, "base_column", -1); |
| 1086 | |
| 1087 | table = grn_proc_get_value_object(ctx, |
| 1088 | table_name, |
| 1089 | "[column][create][similar][table]"); |
| 1090 | if (!table) { |
| 1091 | goto exit; |
| 1092 | } |
| 1093 | base_column = |
| 1094 | grn_proc_get_value_object(ctx, |
| 1095 | base_column_name, |
| 1096 | "[column][create][similar][base_column]"); |
| 1097 | if (!base_column) { |
| 1098 | goto exit; |
| 1099 | } |
| 1100 | |
| 1101 | column = grn_column_create_similar(ctx, |
| 1102 | table, |
| 1103 | GRN_TEXT_VALUE(name), |
| 1104 | (uint32_t)GRN_TEXT_LEN(name), |
| 1105 | NULL, |
| 1106 | base_column); |
| 1107 | |
| 1108 | exit: |
| 1109 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 1110 | |
| 1111 | if (table) { |
| 1112 | grn_obj_unref(ctx, table); |
| 1113 | } |
| 1114 | if (base_column) { |
| 1115 | grn_obj_unref(ctx, base_column); |
| 1116 | } |
| 1117 | if (column) { |
| 1118 | grn_obj_unref(ctx, column); |
| 1119 | } |
| 1120 | |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | |
| 1124 | void |
| 1125 | grn_proc_init_column_create_similar(grn_ctx *ctx) |
| 1126 | { |
| 1127 | grn_expr_var vars[3]; |
| 1128 | |
| 1129 | grn_plugin_expr_var_init(ctx, &(vars[0]), "table", -1); |
| 1130 | grn_plugin_expr_var_init(ctx, &(vars[1]), "name", -1); |
| 1131 | grn_plugin_expr_var_init(ctx, &(vars[2]), "base_column", -1); |
| 1132 | grn_plugin_command_create(ctx, |
| 1133 | "column_create_similar", |
| 1134 | -1, |
| 1135 | command_column_create_similar, |
| 1136 | 3, |
| 1137 | vars); |
| 1138 | } |