| 1 | /* |
| 2 | Copyright(C) 2009-2017 Brazil |
| 3 | Copyright(C) 2018-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn.h" |
| 21 | #include "grn_column.h" |
| 22 | #include "grn_db.h" |
| 23 | #include "grn_ii.h" |
| 24 | #include "grn_util.h" |
| 25 | |
| 26 | grn_column_flags |
| 27 | grn_column_get_flags(grn_ctx *ctx, grn_obj *column) |
| 28 | { |
| 29 | grn_column_flags flags = 0; |
| 30 | |
| 31 | GRN_API_ENTER; |
| 32 | |
| 33 | if (!column) { |
| 34 | GRN_API_RETURN(0); |
| 35 | } |
| 36 | |
| 37 | switch (column->header.type) { |
| 38 | case GRN_COLUMN_FIX_SIZE : |
| 39 | flags = grn_ra_get_flags(ctx, (grn_ra *)column); |
| 40 | break; |
| 41 | case GRN_COLUMN_VAR_SIZE : |
| 42 | flags = grn_ja_get_flags(ctx, (grn_ja *)column); |
| 43 | break; |
| 44 | case GRN_COLUMN_INDEX : |
| 45 | flags = grn_ii_get_flags(ctx, (grn_ii *)column); |
| 46 | break; |
| 47 | default : |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | GRN_API_RETURN(flags); |
| 52 | } |
| 53 | |
| 54 | grn_column_flags |
| 55 | grn_column_get_missing_mode(grn_ctx *ctx, grn_obj *column) |
| 56 | { |
| 57 | GRN_API_ENTER; |
| 58 | grn_column_flags flags = |
| 59 | grn_column_get_flags(ctx, column) & GRN_OBJ_MISSING_MASK; |
| 60 | GRN_API_RETURN(flags); |
| 61 | } |
| 62 | |
| 63 | grn_column_flags |
| 64 | grn_column_get_invalid_mode(grn_ctx *ctx, grn_obj *column) |
| 65 | { |
| 66 | GRN_API_ENTER; |
| 67 | grn_column_flags flags = |
| 68 | grn_column_get_flags(ctx, column) & GRN_OBJ_INVALID_MASK; |
| 69 | GRN_API_RETURN(flags); |
| 70 | } |
| 71 | |
| 72 | grn_column_cache * |
| 73 | grn_column_cache_open(grn_ctx *ctx, grn_obj *column) |
| 74 | { |
| 75 | grn_column_cache *cache; |
| 76 | |
| 77 | GRN_API_ENTER; |
| 78 | |
| 79 | if (!column) { |
| 80 | GRN_API_RETURN(NULL); |
| 81 | } |
| 82 | |
| 83 | grn_ra *ra = NULL; |
| 84 | grn_obj *accessor = NULL; |
| 85 | switch (column->header.type) { |
| 86 | case GRN_ACCESSOR : |
| 87 | { |
| 88 | grn_accessor *a; |
| 89 | for (a = (grn_accessor *)column; a->next; a = a->next) { |
| 90 | switch (a->action) { |
| 91 | case GRN_ACCESSOR_GET_KEY : |
| 92 | case GRN_ACCESSOR_GET_VALUE : |
| 93 | break; |
| 94 | case GRN_ACCESSOR_GET_COLUMN_VALUE : |
| 95 | if (a->next) { |
| 96 | GRN_API_RETURN(NULL); |
| 97 | } |
| 98 | break; |
| 99 | default : |
| 100 | GRN_API_RETURN(NULL); |
| 101 | } |
| 102 | } |
| 103 | grn_accessor *last_accessor = a; |
| 104 | if (last_accessor->action != GRN_ACCESSOR_GET_COLUMN_VALUE) { |
| 105 | GRN_API_RETURN(NULL); |
| 106 | } |
| 107 | if (last_accessor->obj->header.type != GRN_COLUMN_FIX_SIZE) { |
| 108 | GRN_API_RETURN(NULL); |
| 109 | } |
| 110 | for (a = (grn_accessor *)column; a->next; a = a->next) { |
| 111 | switch (a->action) { |
| 112 | case GRN_ACCESSOR_GET_KEY : |
| 113 | { |
| 114 | grn_obj *domain = grn_ctx_at(ctx, a->obj->header.domain); |
| 115 | bool is_table = grn_obj_is_table(ctx, domain); |
| 116 | grn_obj_unref(ctx, domain); |
| 117 | if (!is_table) { |
| 118 | GRN_API_RETURN(NULL); |
| 119 | } |
| 120 | } |
| 121 | break; |
| 122 | case GRN_ACCESSOR_GET_VALUE : |
| 123 | { |
| 124 | grn_obj *range = grn_ctx_at(ctx, DB_OBJ(a->obj)->range); |
| 125 | bool is_table = grn_obj_is_table(ctx, range); |
| 126 | grn_obj_unref(ctx, range); |
| 127 | if (!is_table) { |
| 128 | GRN_API_RETURN(NULL); |
| 129 | } |
| 130 | } |
| 131 | break; |
| 132 | default : |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | ra = (grn_ra *)(last_accessor->obj); |
| 137 | accessor = column; |
| 138 | } |
| 139 | break; |
| 140 | case GRN_COLUMN_FIX_SIZE : |
| 141 | ra = (grn_ra *)column; |
| 142 | break; |
| 143 | default : |
| 144 | GRN_API_RETURN(NULL); |
| 145 | } |
| 146 | |
| 147 | cache = GRN_CALLOC(sizeof(grn_column_cache)); |
| 148 | if (!cache) { |
| 149 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 150 | "[column-cache][open] failed to allocate memory"); |
| 151 | GRN_API_RETURN(NULL); |
| 152 | } |
| 153 | |
| 154 | cache->ra = ra; |
| 155 | cache->accessor = accessor; |
| 156 | GRN_RA_CACHE_INIT(column, &(cache->ra_cache)); |
| 157 | |
| 158 | GRN_API_RETURN(cache); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | grn_column_cache_close(grn_ctx *ctx, grn_column_cache *cache) |
| 163 | { |
| 164 | GRN_API_ENTER; |
| 165 | |
| 166 | if (!cache) { |
| 167 | GRN_API_RETURN(); |
| 168 | } |
| 169 | |
| 170 | GRN_RA_CACHE_FIN(ctx, cache->ra, &(cache->ra_cache)); |
| 171 | GRN_FREE(cache); |
| 172 | |
| 173 | GRN_API_RETURN(); |
| 174 | } |
| 175 | |
| 176 | void * |
| 177 | grn_column_cache_ref(grn_ctx *ctx, |
| 178 | grn_column_cache *cache, |
| 179 | grn_id id, |
| 180 | size_t *value_size) |
| 181 | { |
| 182 | void *value; |
| 183 | |
| 184 | GRN_API_ENTER; |
| 185 | |
| 186 | if (!cache) { |
| 187 | *value_size = 0; |
| 188 | GRN_API_RETURN(NULL); |
| 189 | } |
| 190 | |
| 191 | if (cache->accessor) { |
| 192 | id = grn_accessor_resolve_id(ctx, cache->accessor, id); |
| 193 | if (id == GRN_ID_NIL) { |
| 194 | *value_size = 0; |
| 195 | GRN_API_RETURN(NULL); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | value = grn_ra_ref_cache(ctx, cache->ra, id, &(cache->ra_cache)); |
| 200 | if (value_size) { |
| 201 | if (value) { |
| 202 | *value_size = cache->ra->header->element_size; |
| 203 | } else { |
| 204 | *value_size = 0; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | GRN_API_RETURN(value); |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | grn_column_copy_same_table_report_invalid_reference_value(grn_ctx *ctx, |
| 213 | grn_obj *table, |
| 214 | grn_obj *from_column, |
| 215 | grn_id id, |
| 216 | grn_id value) |
| 217 | { |
| 218 | GRN_DEFINE_NAME(from_column); |
| 219 | char key[GRN_TABLE_MAX_KEY_SIZE]; |
| 220 | int key_size = 0; |
| 221 | if (grn_obj_is_table_with_key(ctx, table)) { |
| 222 | key_size = grn_table_get_key(ctx, table, id, key, sizeof(key)); |
| 223 | } |
| 224 | grn_obj inspected_key; |
| 225 | GRN_TEXT_INIT(&inspected_key, 0); |
| 226 | grn_inspect_key(ctx, &inspected_key, table, key, (uint32_t)key_size); |
| 227 | GRN_LOG(ctx, |
| 228 | GRN_LOG_WARNING, |
| 229 | "[column][copy][%.*s] " |
| 230 | "ignore invalid reference value: " |
| 231 | "value:%u, " |
| 232 | "id:%u, " |
| 233 | "key:%.*s", |
| 234 | name_size, name, |
| 235 | value, |
| 236 | id, |
| 237 | (int)GRN_TEXT_LEN(&inspected_key), |
| 238 | GRN_TEXT_VALUE(&inspected_key)); |
| 239 | GRN_OBJ_FIN(ctx, &inspected_key); |
| 240 | } |
| 241 | |
| 242 | static void |
| 243 | grn_column_copy_same_table(grn_ctx *ctx, |
| 244 | grn_obj *table, |
| 245 | grn_obj *from_column, |
| 246 | grn_obj *to_column) |
| 247 | { |
| 248 | grn_id from_range_id = GRN_ID_NIL; |
| 249 | grn_obj_flags from_range_flags = 0; |
| 250 | grn_id to_range_id = GRN_ID_NIL; |
| 251 | grn_obj_flags to_range_flags = 0; |
| 252 | bool need_validate = false; |
| 253 | if (grn_obj_is_reference_column(ctx, from_column) && |
| 254 | grn_obj_is_reference_column(ctx, to_column)) { |
| 255 | grn_obj_get_range_info(ctx, from_column, &from_range_id, &from_range_flags); |
| 256 | grn_obj_get_range_info(ctx, to_column, &to_range_id, &to_range_flags); |
| 257 | need_validate = (from_range_id == to_range_id && |
| 258 | from_range_flags == to_range_flags); |
| 259 | } |
| 260 | |
| 261 | grn_obj value; |
| 262 | GRN_VOID_INIT(&value); |
| 263 | if (need_validate) { |
| 264 | grn_obj validated_value; |
| 265 | GRN_RECORD_INIT(&validated_value, to_range_flags, to_range_id); |
| 266 | if (to_range_flags & GRN_OBJ_WITH_WEIGHT) { |
| 267 | validated_value.header.flags |= GRN_OBJ_WITH_WEIGHT; |
| 268 | } |
| 269 | bool is_uvector = grn_obj_is_uvector(ctx, &validated_value); |
| 270 | grn_obj *range = grn_ctx_at(ctx, to_range_id); |
| 271 | GRN_TABLE_EACH_BEGIN(ctx, table, cursor, id) { |
| 272 | GRN_BULK_REWIND(&value); |
| 273 | grn_obj_get_value(ctx, from_column, id, &value); |
| 274 | GRN_BULK_REWIND(&validated_value); |
| 275 | if (is_uvector) { |
| 276 | uint32_t n_elements = grn_uvector_size(ctx, &value); |
| 277 | uint32_t i; |
| 278 | for (i = 0; i < n_elements; i++) { |
| 279 | float weight; |
| 280 | grn_id record_id = grn_uvector_get_element_record(ctx, |
| 281 | &value, |
| 282 | i, |
| 283 | &weight); |
| 284 | if (record_id == GRN_ID_NIL || |
| 285 | record_id >= GRN_ID_MAX || |
| 286 | !grn_table_at(ctx, range, record_id)) { |
| 287 | grn_column_copy_same_table_report_invalid_reference_value( |
| 288 | ctx, |
| 289 | table, |
| 290 | from_column, |
| 291 | id, |
| 292 | record_id); |
| 293 | continue; |
| 294 | } |
| 295 | grn_uvector_add_element_record(ctx, |
| 296 | &validated_value, |
| 297 | record_id, |
| 298 | weight); |
| 299 | } |
| 300 | } else { |
| 301 | if (GRN_BULK_VSIZE(&value) > 0) { |
| 302 | grn_id record_id = GRN_RECORD_VALUE(&value); |
| 303 | if (record_id == GRN_ID_NIL || |
| 304 | record_id >= GRN_ID_MAX || |
| 305 | !grn_table_at(ctx, range, record_id)) { |
| 306 | grn_column_copy_same_table_report_invalid_reference_value( |
| 307 | ctx, |
| 308 | table, |
| 309 | from_column, |
| 310 | id, |
| 311 | record_id); |
| 312 | } else { |
| 313 | GRN_RECORD_SET(ctx, &validated_value, record_id); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | grn_obj_set_value(ctx, to_column, id, &validated_value, GRN_OBJ_SET); |
| 318 | } GRN_TABLE_EACH_END(ctx, cursor); |
| 319 | grn_obj_unref(ctx, range); |
| 320 | GRN_OBJ_FIN(ctx, &validated_value); |
| 321 | } else { |
| 322 | GRN_TABLE_EACH_BEGIN(ctx, table, cursor, id) { |
| 323 | GRN_BULK_REWIND(&value); |
| 324 | grn_obj_get_value(ctx, from_column, id, &value); |
| 325 | grn_obj_set_value(ctx, to_column, id, &value, GRN_OBJ_SET); |
| 326 | } GRN_TABLE_EACH_END(ctx, cursor); |
| 327 | } |
| 328 | GRN_OBJ_FIN(ctx, &value); |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | grn_column_copy_array(grn_ctx *ctx, |
| 333 | grn_obj *from_table, |
| 334 | grn_obj *from_column, |
| 335 | grn_obj *to_table, |
| 336 | grn_obj *to_column) |
| 337 | { |
| 338 | grn_obj value; |
| 339 | GRN_VOID_INIT(&value); |
| 340 | GRN_TABLE_EACH_BEGIN(ctx, from_table, cursor, from_id) { |
| 341 | grn_id to_id = grn_table_at(ctx, to_table, from_id); |
| 342 | if (to_id != from_id) { |
| 343 | GRN_DEFINE_NAME_CUSTOM(from_table, from_name); |
| 344 | GRN_DEFINE_NAME_CUSTOM(to_table, to_name); |
| 345 | ERR(GRN_INVALID_ARGUMENT, |
| 346 | "[column][copy] target ID doesn't exist: <%u>: " |
| 347 | "<%.*s> -> <%.*s>", |
| 348 | from_id, |
| 349 | from_name_size, from_name, |
| 350 | to_name_size, to_name); |
| 351 | break; |
| 352 | } |
| 353 | GRN_BULK_REWIND(&value); |
| 354 | grn_obj_get_value(ctx, from_column, from_id, &value); |
| 355 | grn_obj_set_value(ctx, to_column, to_id, &value, GRN_OBJ_SET); |
| 356 | } GRN_TABLE_EACH_END(ctx, cursor); |
| 357 | GRN_OBJ_FIN(ctx, &value); |
| 358 | } |
| 359 | |
| 360 | static void |
| 361 | grn_column_copy_same_key_type(grn_ctx *ctx, |
| 362 | grn_obj *from_table, |
| 363 | grn_obj *from_column, |
| 364 | grn_obj *to_table, |
| 365 | grn_obj *to_column) |
| 366 | { |
| 367 | grn_table_cursor *cursor; |
| 368 | grn_id from_id; |
| 369 | grn_obj value; |
| 370 | |
| 371 | cursor = grn_table_cursor_open(ctx, from_table, |
| 372 | NULL, 0, |
| 373 | NULL, 0, |
| 374 | 0, -1, 0); |
| 375 | if (!cursor) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | GRN_VOID_INIT(&value); |
| 380 | while ((from_id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { |
| 381 | void *key; |
| 382 | int key_size; |
| 383 | grn_id to_id; |
| 384 | |
| 385 | key_size = grn_table_cursor_get_key(ctx, cursor, &key); |
| 386 | to_id = grn_table_add(ctx, to_table, key, (uint32_t)key_size, NULL); |
| 387 | if (to_id == GRN_ID_NIL) { |
| 388 | char from_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 389 | int from_name_size; |
| 390 | char to_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 391 | int to_name_size; |
| 392 | grn_obj key_buffer; |
| 393 | grn_obj inspected_key; |
| 394 | |
| 395 | from_name_size = grn_obj_name(ctx, |
| 396 | from_column, |
| 397 | from_name, |
| 398 | sizeof(from_name)); |
| 399 | to_name_size = grn_obj_name(ctx, |
| 400 | to_column, |
| 401 | to_name, |
| 402 | sizeof(to_name)); |
| 403 | if (from_table->header.domain == GRN_DB_SHORT_TEXT) { |
| 404 | GRN_SHORT_TEXT_INIT(&key_buffer, 0); |
| 405 | } else { |
| 406 | GRN_VALUE_FIX_SIZE_INIT(&key_buffer, 0, from_table->header.domain); |
| 407 | } |
| 408 | grn_bulk_write(ctx, &key_buffer, key, (size_t)key_size); |
| 409 | grn_inspect(ctx, &inspected_key, &key_buffer); |
| 410 | ERR(GRN_INVALID_ARGUMENT, |
| 411 | "[column][copy] failed to copy key: <%.*s>: " |
| 412 | "<%.*s> -> <%.*s>", |
| 413 | (int)GRN_TEXT_LEN(&inspected_key), |
| 414 | GRN_TEXT_VALUE(&inspected_key), |
| 415 | from_name_size, from_name, |
| 416 | to_name_size, to_name); |
| 417 | GRN_OBJ_FIN(ctx, &inspected_key); |
| 418 | GRN_OBJ_FIN(ctx, &key_buffer); |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | GRN_BULK_REWIND(&value); |
| 423 | grn_obj_get_value(ctx, from_column, from_id, &value); |
| 424 | grn_obj_set_value(ctx, to_column, to_id, &value, GRN_OBJ_SET); |
| 425 | } |
| 426 | GRN_OBJ_FIN(ctx, &value); |
| 427 | grn_table_cursor_close(ctx, cursor); |
| 428 | } |
| 429 | |
| 430 | static void |
| 431 | grn_column_copy_different(grn_ctx *ctx, |
| 432 | grn_obj *from_table, |
| 433 | grn_obj *from_column, |
| 434 | grn_obj *to_table, |
| 435 | grn_obj *to_column) |
| 436 | { |
| 437 | grn_table_cursor *cursor; |
| 438 | grn_id from_id; |
| 439 | grn_obj from_key_buffer; |
| 440 | grn_obj to_key_buffer; |
| 441 | grn_obj value; |
| 442 | |
| 443 | cursor = grn_table_cursor_open(ctx, from_table, |
| 444 | NULL, 0, |
| 445 | NULL, 0, |
| 446 | 0, -1, 0); |
| 447 | if (!cursor) { |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | if (from_table->header.domain == GRN_DB_SHORT_TEXT) { |
| 452 | GRN_SHORT_TEXT_INIT(&from_key_buffer, 0); |
| 453 | } else { |
| 454 | GRN_VALUE_FIX_SIZE_INIT(&from_key_buffer, 0, from_table->header.domain); |
| 455 | } |
| 456 | if (to_table->header.domain == GRN_DB_SHORT_TEXT) { |
| 457 | GRN_SHORT_TEXT_INIT(&to_key_buffer, 0); |
| 458 | } else { |
| 459 | GRN_VALUE_FIX_SIZE_INIT(&to_key_buffer, 0, to_table->header.domain); |
| 460 | } |
| 461 | GRN_VOID_INIT(&value); |
| 462 | while ((from_id = grn_table_cursor_next(ctx, cursor)) != GRN_ID_NIL) { |
| 463 | void *key; |
| 464 | int key_size; |
| 465 | grn_rc cast_rc; |
| 466 | grn_id to_id; |
| 467 | |
| 468 | GRN_BULK_REWIND(&from_key_buffer); |
| 469 | GRN_BULK_REWIND(&to_key_buffer); |
| 470 | |
| 471 | key_size = grn_table_cursor_get_key(ctx, cursor, &key); |
| 472 | grn_bulk_write(ctx, &from_key_buffer, key, (size_t)key_size); |
| 473 | cast_rc = grn_obj_cast(ctx, &from_key_buffer, &to_key_buffer, false); |
| 474 | if (cast_rc != GRN_SUCCESS) { |
| 475 | char from_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 476 | int from_name_size; |
| 477 | char to_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 478 | int to_name_size; |
| 479 | grn_obj *to_key_type; |
| 480 | grn_obj inspected_key; |
| 481 | grn_obj inspected_to_key_type; |
| 482 | |
| 483 | from_name_size = grn_obj_name(ctx, |
| 484 | from_column, |
| 485 | from_name, |
| 486 | sizeof(from_name)); |
| 487 | to_name_size = grn_obj_name(ctx, |
| 488 | to_column, |
| 489 | to_name, |
| 490 | sizeof(to_name)); |
| 491 | to_key_type = grn_ctx_at(ctx, to_table->header.domain); |
| 492 | GRN_TEXT_INIT(&inspected_key, 0); |
| 493 | GRN_TEXT_INIT(&inspected_to_key_type, 0); |
| 494 | grn_inspect(ctx, &inspected_key, &from_key_buffer); |
| 495 | grn_inspect(ctx, &inspected_to_key_type, to_key_type); |
| 496 | ERR(cast_rc, |
| 497 | "[column][copy] failed to cast key: <%.*s> -> %.*s: " |
| 498 | "<%.*s> -> <%.*s>", |
| 499 | (int)GRN_TEXT_LEN(&inspected_key), |
| 500 | GRN_TEXT_VALUE(&inspected_key), |
| 501 | (int)GRN_TEXT_LEN(&inspected_to_key_type), |
| 502 | GRN_TEXT_VALUE(&inspected_to_key_type), |
| 503 | from_name_size, from_name, |
| 504 | to_name_size, to_name); |
| 505 | GRN_OBJ_FIN(ctx, &inspected_key); |
| 506 | GRN_OBJ_FIN(ctx, &inspected_to_key_type); |
| 507 | break; |
| 508 | } |
| 509 | to_id = grn_table_add(ctx, to_table, |
| 510 | GRN_BULK_HEAD(&to_key_buffer), |
| 511 | (unsigned int)GRN_BULK_VSIZE(&to_key_buffer), |
| 512 | NULL); |
| 513 | if (to_id == GRN_ID_NIL) { |
| 514 | continue; |
| 515 | } |
| 516 | |
| 517 | GRN_BULK_REWIND(&value); |
| 518 | grn_obj_get_value(ctx, from_column, from_id, &value); |
| 519 | grn_obj_set_value(ctx, to_column, to_id, &value, GRN_OBJ_SET); |
| 520 | } |
| 521 | GRN_OBJ_FIN(ctx, &from_key_buffer); |
| 522 | GRN_OBJ_FIN(ctx, &to_key_buffer); |
| 523 | GRN_OBJ_FIN(ctx, &value); |
| 524 | |
| 525 | grn_table_cursor_close(ctx, cursor); |
| 526 | } |
| 527 | |
| 528 | grn_rc |
| 529 | grn_column_copy(grn_ctx *ctx, grn_obj *from, grn_obj *to) |
| 530 | { |
| 531 | grn_obj *from_table; |
| 532 | grn_obj *to_table; |
| 533 | |
| 534 | GRN_API_ENTER; |
| 535 | |
| 536 | from_table = grn_ctx_at(ctx, from->header.domain); |
| 537 | to_table = grn_ctx_at(ctx, to->header.domain); |
| 538 | if ((from_table->header.type == GRN_TABLE_NO_KEY || |
| 539 | to_table->header.type == GRN_TABLE_NO_KEY) && |
| 540 | (from_table->header.type != to_table->header.type)) { |
| 541 | char from_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 542 | int from_name_size; |
| 543 | char to_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 544 | int to_name_size; |
| 545 | |
| 546 | from_name_size = grn_obj_name(ctx, from, from_name, sizeof(from_name)); |
| 547 | to_name_size = grn_obj_name(ctx, to, to_name, sizeof(to_name)); |
| 548 | ERR(GRN_OPERATION_NOT_SUPPORTED, |
| 549 | "[column][copy] copy from/to TABLE_NO_KEY isn't supported: " |
| 550 | "<%.*s> -> <%.*s>", |
| 551 | from_name_size, from_name, |
| 552 | to_name_size, to_name); |
| 553 | goto exit; |
| 554 | } |
| 555 | |
| 556 | if (from_table == to_table) { |
| 557 | grn_column_copy_same_table(ctx, from_table, from, to); |
| 558 | } else if (from_table->header.type == GRN_TABLE_NO_KEY) { |
| 559 | grn_column_copy_array(ctx, |
| 560 | from_table, from, |
| 561 | to_table, to); |
| 562 | } else if (from_table->header.domain == to_table->header.domain) { |
| 563 | grn_column_copy_same_key_type(ctx, |
| 564 | from_table, from, |
| 565 | to_table, to); |
| 566 | } else { |
| 567 | grn_column_copy_different(ctx, |
| 568 | from_table, from, |
| 569 | to_table, to); |
| 570 | } |
| 571 | |
| 572 | exit : |
| 573 | GRN_API_RETURN(ctx->rc); |
| 574 | } |
| 575 | |
| 576 | grn_obj * |
| 577 | grn_column_cast_value(grn_ctx *ctx, |
| 578 | grn_obj *column, |
| 579 | grn_obj *value, |
| 580 | grn_obj *buffer, |
| 581 | int set_flags) |
| 582 | { |
| 583 | GRN_API_ENTER; |
| 584 | |
| 585 | const char *tag = "[column][cast-value]"; |
| 586 | |
| 587 | if (!column) { |
| 588 | ERR(GRN_INVALID_ARGUMENT, "%s column must not be NULL", tag); |
| 589 | GRN_API_RETURN(NULL); |
| 590 | } |
| 591 | |
| 592 | grn_obj *casted_value = NULL; |
| 593 | switch (column->header.type) { |
| 594 | case GRN_COLUMN_FIX_SIZE : |
| 595 | casted_value = grn_ra_cast_value(ctx, |
| 596 | (grn_ra *)column, |
| 597 | value, |
| 598 | buffer, |
| 599 | set_flags); |
| 600 | break; |
| 601 | case GRN_COLUMN_VAR_SIZE : |
| 602 | casted_value = grn_ja_cast_value(ctx, |
| 603 | (grn_ja *)column, |
| 604 | value, |
| 605 | buffer, |
| 606 | set_flags); |
| 607 | break; |
| 608 | default : |
| 609 | { |
| 610 | grn_obj inspected; |
| 611 | GRN_TEXT_INIT(&inspected, 0); |
| 612 | grn_inspect(ctx, &inspected, column); |
| 613 | ERR(GRN_INVALID_ARGUMENT, |
| 614 | "%s must be data column: %.*s", |
| 615 | tag, |
| 616 | (int)GRN_TEXT_LEN(&inspected), |
| 617 | GRN_TEXT_VALUE(&inspected)); |
| 618 | GRN_OBJ_FIN(ctx, &inspected); |
| 619 | } |
| 620 | break; |
| 621 | } |
| 622 | GRN_API_RETURN(casted_value); |
| 623 | } |