| 1 | /* |
| 2 | Copyright (C) 2017-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.h" |
| 21 | #include "grn_applier.h" |
| 22 | #include "grn_ctx.h" |
| 23 | #include "grn_ctx_impl.h" |
| 24 | #include "grn_dat.h" |
| 25 | #include "grn_hash.h" |
| 26 | #include "grn_pat.h" |
| 27 | #include "grn_expr_executor.h" |
| 28 | #include "grn_extractor.h" |
| 29 | |
| 30 | grn_rc |
| 31 | grn_table_apply_expr(grn_ctx *ctx, |
| 32 | grn_obj *table, |
| 33 | grn_obj *output_column, |
| 34 | grn_obj *expr) |
| 35 | { |
| 36 | GRN_API_ENTER; |
| 37 | |
| 38 | if (!grn_obj_is_data_column(ctx, output_column)) { |
| 39 | grn_obj inspected; |
| 40 | GRN_TEXT_INIT(&inspected, 0); |
| 41 | grn_inspect(ctx, &inspected, output_column); |
| 42 | ERR(GRN_INVALID_ARGUMENT, |
| 43 | "[table][apply-expr] output column isn't data column: %.*s", |
| 44 | (int)GRN_TEXT_LEN(&inspected), |
| 45 | GRN_TEXT_VALUE(&inspected)); |
| 46 | GRN_OBJ_FIN(ctx, &inspected); |
| 47 | GRN_API_RETURN(ctx->rc); |
| 48 | } |
| 49 | |
| 50 | if (!grn_obj_is_expr(ctx, expr)) { |
| 51 | grn_obj inspected; |
| 52 | GRN_TEXT_INIT(&inspected, 0); |
| 53 | grn_inspect(ctx, &inspected, expr); |
| 54 | ERR(GRN_INVALID_ARGUMENT, |
| 55 | "[table][apply-expr] expr is invalid: %.*s", |
| 56 | (int)GRN_TEXT_LEN(&inspected), |
| 57 | GRN_TEXT_VALUE(&inspected)); |
| 58 | GRN_OBJ_FIN(ctx, &inspected); |
| 59 | GRN_API_RETURN(ctx->rc); |
| 60 | } |
| 61 | |
| 62 | grn_applier_data data = {0}; |
| 63 | grn_applier_data_init(ctx, &data, table, output_column); |
| 64 | if (grn_applier_data_extract(ctx, &data, expr)) { |
| 65 | grn_rc rc = grn_applier_data_run(ctx, &data); |
| 66 | grn_applier_data_fin(ctx, &data); |
| 67 | GRN_API_RETURN(rc); |
| 68 | } else { |
| 69 | grn_applier_data_fin(ctx, &data); |
| 70 | |
| 71 | grn_expr_executor executor; |
| 72 | grn_expr_executor_init(ctx, &executor, expr); |
| 73 | if (ctx->rc != GRN_SUCCESS) { |
| 74 | GRN_API_RETURN(ctx->rc); |
| 75 | } |
| 76 | GRN_TABLE_EACH_BEGIN_FLAGS(ctx, table, cursor, id, GRN_CURSOR_BY_ID) |
| 77 | { |
| 78 | grn_obj *value; |
| 79 | value = grn_expr_executor_exec(ctx, &executor, id); |
| 80 | if (ctx->rc != GRN_SUCCESS) { |
| 81 | break; |
| 82 | } |
| 83 | if (value) { |
| 84 | grn_obj_set_value(ctx, output_column, id, value, GRN_OBJ_SET); |
| 85 | } |
| 86 | if (ctx->rc != GRN_SUCCESS) { |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | GRN_TABLE_EACH_END(ctx, cursor); |
| 91 | grn_expr_executor_fin(ctx, &executor); |
| 92 | |
| 93 | GRN_API_RETURN(ctx->rc); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | grn_id |
| 98 | grn_table_find_reference_object_raw(grn_ctx *ctx, grn_id table_id) |
| 99 | { |
| 100 | grn_id reference_object_id = GRN_ID_NIL; |
| 101 | |
| 102 | GRN_API_ENTER; |
| 103 | |
| 104 | GRN_DB_EACH_SPEC_BEGIN(ctx, cursor, id, spec) |
| 105 | { |
| 106 | if (id == table_id) { |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | switch (spec->header.type) { |
| 111 | case GRN_TABLE_HASH_KEY: |
| 112 | case GRN_TABLE_PAT_KEY: |
| 113 | case GRN_TABLE_DAT_KEY: |
| 114 | if (spec->header.domain == table_id) { |
| 115 | reference_object_id = id; |
| 116 | } |
| 117 | break; |
| 118 | case GRN_COLUMN_VAR_SIZE: |
| 119 | case GRN_COLUMN_FIX_SIZE: |
| 120 | if (spec->header.domain == table_id) { |
| 121 | break; |
| 122 | } |
| 123 | if (spec->range == table_id) { |
| 124 | reference_object_id = id; |
| 125 | } |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | if (reference_object_id != GRN_ID_NIL) { |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | GRN_DB_EACH_SPEC_END(ctx, cursor); |
| 136 | |
| 137 | GRN_API_RETURN(reference_object_id); |
| 138 | } |
| 139 | |
| 140 | grn_id |
| 141 | grn_table_find_reference_object(grn_ctx *ctx, grn_obj *table) |
| 142 | { |
| 143 | GRN_API_ENTER; |
| 144 | |
| 145 | if (!grn_obj_is_table(ctx, table)) { |
| 146 | GRN_API_RETURN(GRN_ID_NIL); |
| 147 | } |
| 148 | |
| 149 | grn_id table_id = DB_OBJ(table)->id; |
| 150 | grn_id reference_object_id = |
| 151 | grn_table_find_reference_object_raw(ctx, table_id); |
| 152 | |
| 153 | GRN_API_RETURN(reference_object_id); |
| 154 | } |
| 155 | |
| 156 | static void |
| 157 | grn_table_copy_array(grn_ctx *ctx, grn_obj *from, grn_obj *to) |
| 158 | { |
| 159 | grn_obj missing_ids; |
| 160 | GRN_RECORD_INIT(&missing_ids, GRN_OBJ_VECTOR, DB_OBJ(from)->id); |
| 161 | grn_id previous_from_id = GRN_ID_NIL; |
| 162 | GRN_TABLE_EACH_BEGIN(ctx, from, cursor, from_id) |
| 163 | { |
| 164 | for (; previous_from_id < from_id - 1; previous_from_id++) { |
| 165 | grn_id to_id = grn_table_add(ctx, to, NULL, 0, NULL); |
| 166 | if (to_id != previous_from_id + 1) { |
| 167 | GRN_DEFINE_NAME_CUSTOM(from, from_name); |
| 168 | GRN_DEFINE_NAME_CUSTOM(to, to_name); |
| 169 | ERR(GRN_INVALID_ARGUMENT, |
| 170 | "[table][copy] failed to assign ID for deleted record: " |
| 171 | "<%u> != <%u>: <%.*s> -> <%.*s>", |
| 172 | to_id, |
| 173 | previous_from_id + 1, |
| 174 | from_name_size, |
| 175 | from_name, |
| 176 | to_name_size, |
| 177 | to_name); |
| 178 | break; |
| 179 | } |
| 180 | GRN_RECORD_PUT(ctx, &missing_ids, to_id); |
| 181 | } |
| 182 | grn_id to_id = grn_table_add(ctx, to, NULL, 0, NULL); |
| 183 | if (to_id == GRN_ID_NIL) { |
| 184 | GRN_DEFINE_NAME_CUSTOM(from, from_name); |
| 185 | GRN_DEFINE_NAME_CUSTOM(to, to_name); |
| 186 | ERR(GRN_INVALID_ARGUMENT, |
| 187 | "[table][copy] failed to add a record for ID: <%u>: " |
| 188 | "<%.*s> -> <%.*s>", |
| 189 | from_id, |
| 190 | from_name_size, |
| 191 | from_name, |
| 192 | to_name_size, |
| 193 | to_name); |
| 194 | break; |
| 195 | } |
| 196 | previous_from_id = from_id; |
| 197 | } |
| 198 | GRN_TABLE_EACH_END(ctx, cursor); |
| 199 | |
| 200 | if (ctx->rc == GRN_SUCCESS) { |
| 201 | size_t n_missing_ids = GRN_RECORD_VECTOR_SIZE(&missing_ids); |
| 202 | size_t i; |
| 203 | for (i = 0; i < n_missing_ids; i++) { |
| 204 | grn_id missing_id = GRN_RECORD_VALUE_AT(&missing_ids, i); |
| 205 | grn_table_delete_by_id(ctx, to, missing_id); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | GRN_OBJ_FIN(ctx, &missing_ids); |
| 210 | } |
| 211 | |
| 212 | static void |
| 213 | grn_table_copy_same_key_type(grn_ctx *ctx, grn_obj *from, grn_obj *to) |
| 214 | { |
| 215 | GRN_TABLE_EACH_BEGIN_FLAGS(ctx, |
| 216 | from, |
| 217 | cursor, |
| 218 | from_id, |
| 219 | GRN_CURSOR_BY_KEY | GRN_CURSOR_ASCENDING) |
| 220 | { |
| 221 | void *key; |
| 222 | int key_size; |
| 223 | grn_id to_id; |
| 224 | |
| 225 | key_size = grn_table_cursor_get_key(ctx, cursor, &key); |
| 226 | to_id = grn_table_add(ctx, to, key, (unsigned int)key_size, NULL); |
| 227 | if (to_id == GRN_ID_NIL) { |
| 228 | char from_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 229 | int from_name_size; |
| 230 | char to_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 231 | int to_name_size; |
| 232 | grn_obj key_buffer; |
| 233 | grn_obj inspected_key; |
| 234 | |
| 235 | from_name_size = grn_obj_name(ctx, from, from_name, sizeof(from_name)); |
| 236 | to_name_size = grn_obj_name(ctx, to, to_name, sizeof(to_name)); |
| 237 | if (from->header.domain == GRN_DB_SHORT_TEXT) { |
| 238 | GRN_SHORT_TEXT_INIT(&key_buffer, 0); |
| 239 | } else { |
| 240 | GRN_VALUE_FIX_SIZE_INIT(&key_buffer, 0, from->header.domain); |
| 241 | } |
| 242 | grn_bulk_write(ctx, &key_buffer, key, (size_t)key_size); |
| 243 | GRN_TEXT_INIT(&inspected_key, 0); |
| 244 | grn_inspect(ctx, &inspected_key, &key_buffer); |
| 245 | ERR(GRN_INVALID_ARGUMENT, |
| 246 | "[table][copy] failed to copy key: <%.*s>: " |
| 247 | "<%.*s> -> <%.*s>", |
| 248 | (int)GRN_TEXT_LEN(&inspected_key), |
| 249 | GRN_TEXT_VALUE(&inspected_key), |
| 250 | from_name_size, |
| 251 | from_name, |
| 252 | to_name_size, |
| 253 | to_name); |
| 254 | GRN_OBJ_FIN(ctx, &inspected_key); |
| 255 | GRN_OBJ_FIN(ctx, &key_buffer); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | GRN_TABLE_EACH_END(ctx, cursor); |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | grn_table_copy_different(grn_ctx *ctx, grn_obj *from, grn_obj *to) |
| 264 | { |
| 265 | grn_obj from_key_buffer; |
| 266 | grn_obj to_key_buffer; |
| 267 | |
| 268 | if (from->header.domain == GRN_DB_SHORT_TEXT) { |
| 269 | GRN_SHORT_TEXT_INIT(&from_key_buffer, 0); |
| 270 | } else { |
| 271 | GRN_VALUE_FIX_SIZE_INIT(&from_key_buffer, 0, from->header.domain); |
| 272 | } |
| 273 | if (to->header.domain == GRN_DB_SHORT_TEXT) { |
| 274 | GRN_SHORT_TEXT_INIT(&to_key_buffer, 0); |
| 275 | } else { |
| 276 | GRN_VALUE_FIX_SIZE_INIT(&to_key_buffer, 0, to->header.domain); |
| 277 | } |
| 278 | |
| 279 | GRN_TABLE_EACH_BEGIN_FLAGS(ctx, |
| 280 | from, |
| 281 | cursor, |
| 282 | from_id, |
| 283 | GRN_CURSOR_BY_KEY | GRN_CURSOR_ASCENDING) |
| 284 | { |
| 285 | void *key; |
| 286 | int key_size; |
| 287 | grn_rc cast_rc; |
| 288 | grn_id to_id; |
| 289 | |
| 290 | GRN_BULK_REWIND(&from_key_buffer); |
| 291 | GRN_BULK_REWIND(&to_key_buffer); |
| 292 | |
| 293 | key_size = grn_table_cursor_get_key(ctx, cursor, &key); |
| 294 | grn_bulk_write(ctx, &from_key_buffer, key, (size_t)key_size); |
| 295 | cast_rc = grn_obj_cast(ctx, &from_key_buffer, &to_key_buffer, false); |
| 296 | if (cast_rc != GRN_SUCCESS) { |
| 297 | char from_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 298 | int from_name_size; |
| 299 | char to_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 300 | int to_name_size; |
| 301 | grn_obj *to_key_type; |
| 302 | grn_obj inspected_key; |
| 303 | grn_obj inspected_to_key_type; |
| 304 | |
| 305 | from_name_size = grn_obj_name(ctx, from, from_name, sizeof(from_name)); |
| 306 | to_name_size = grn_obj_name(ctx, to, to_name, sizeof(to_name)); |
| 307 | to_key_type = grn_ctx_at(ctx, to->header.domain); |
| 308 | GRN_TEXT_INIT(&inspected_key, 0); |
| 309 | GRN_TEXT_INIT(&inspected_to_key_type, 0); |
| 310 | grn_inspect(ctx, &inspected_key, &from_key_buffer); |
| 311 | grn_inspect(ctx, &inspected_to_key_type, to_key_type); |
| 312 | ERR(cast_rc, |
| 313 | "[table][copy] failed to cast key: <%.*s> -> %.*s: " |
| 314 | "<%.*s> -> <%.*s>", |
| 315 | (int)GRN_TEXT_LEN(&inspected_key), |
| 316 | GRN_TEXT_VALUE(&inspected_key), |
| 317 | (int)GRN_TEXT_LEN(&inspected_to_key_type), |
| 318 | GRN_TEXT_VALUE(&inspected_to_key_type), |
| 319 | from_name_size, |
| 320 | from_name, |
| 321 | to_name_size, |
| 322 | to_name); |
| 323 | GRN_OBJ_FIN(ctx, &inspected_key); |
| 324 | GRN_OBJ_FIN(ctx, &inspected_to_key_type); |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | to_id = grn_table_add(ctx, |
| 329 | to, |
| 330 | GRN_BULK_HEAD(&to_key_buffer), |
| 331 | (unsigned int)GRN_BULK_VSIZE(&to_key_buffer), |
| 332 | NULL); |
| 333 | if (to_id == GRN_ID_NIL) { |
| 334 | char from_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 335 | int from_name_size; |
| 336 | char to_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 337 | int to_name_size; |
| 338 | grn_obj inspected_from_key; |
| 339 | grn_obj inspected_to_key; |
| 340 | |
| 341 | from_name_size = grn_obj_name(ctx, from, from_name, sizeof(from_name)); |
| 342 | to_name_size = grn_obj_name(ctx, to, to_name, sizeof(to_name)); |
| 343 | GRN_TEXT_INIT(&inspected_from_key, 0); |
| 344 | GRN_TEXT_INIT(&inspected_to_key, 0); |
| 345 | grn_inspect(ctx, &inspected_from_key, &from_key_buffer); |
| 346 | grn_inspect(ctx, &inspected_to_key, &to_key_buffer); |
| 347 | ERR(GRN_INVALID_ARGUMENT, |
| 348 | "[table][copy] failed to copy key: <%.*s> -> <%.*s>: " |
| 349 | "<%.*s> -> <%.*s>", |
| 350 | (int)GRN_TEXT_LEN(&inspected_from_key), |
| 351 | GRN_TEXT_VALUE(&inspected_from_key), |
| 352 | (int)GRN_TEXT_LEN(&inspected_to_key), |
| 353 | GRN_TEXT_VALUE(&inspected_to_key), |
| 354 | from_name_size, |
| 355 | from_name, |
| 356 | to_name_size, |
| 357 | to_name); |
| 358 | GRN_OBJ_FIN(ctx, &inspected_from_key); |
| 359 | GRN_OBJ_FIN(ctx, &inspected_to_key); |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | GRN_TABLE_EACH_END(ctx, cursor); |
| 364 | GRN_OBJ_FIN(ctx, &from_key_buffer); |
| 365 | GRN_OBJ_FIN(ctx, &to_key_buffer); |
| 366 | } |
| 367 | |
| 368 | grn_rc |
| 369 | grn_table_copy(grn_ctx *ctx, grn_obj *from, grn_obj *to) |
| 370 | { |
| 371 | GRN_API_ENTER; |
| 372 | |
| 373 | if (from == to) { |
| 374 | char from_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 375 | int from_name_size; |
| 376 | |
| 377 | from_name_size = grn_obj_name(ctx, from, from_name, sizeof(from_name)); |
| 378 | ERR(GRN_OPERATION_NOT_SUPPORTED, |
| 379 | "[table][copy] from table and to table is the same: " |
| 380 | "<%.*s>", |
| 381 | from_name_size, |
| 382 | from_name); |
| 383 | goto exit; |
| 384 | } |
| 385 | |
| 386 | if ((from->header.type == GRN_TABLE_NO_KEY || |
| 387 | to->header.type == GRN_TABLE_NO_KEY) && |
| 388 | from->header.type != to->header.type) { |
| 389 | char from_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 390 | int from_name_size; |
| 391 | char to_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 392 | int to_name_size; |
| 393 | |
| 394 | from_name_size = grn_obj_name(ctx, from, from_name, sizeof(from_name)); |
| 395 | to_name_size = grn_obj_name(ctx, to, to_name, sizeof(to_name)); |
| 396 | ERR(GRN_OPERATION_NOT_SUPPORTED, |
| 397 | "[table][copy] copy from/to TABLE_NO_KEY isn't supported: " |
| 398 | "<%.*s> -> <%.*s>", |
| 399 | from_name_size, |
| 400 | from_name, |
| 401 | to_name_size, |
| 402 | to_name); |
| 403 | goto exit; |
| 404 | } |
| 405 | |
| 406 | if (from->header.type == GRN_TABLE_NO_KEY) { |
| 407 | grn_table_copy_array(ctx, from, to); |
| 408 | } else if (from->header.domain == to->header.domain) { |
| 409 | grn_table_copy_same_key_type(ctx, from, to); |
| 410 | } else { |
| 411 | grn_table_copy_different(ctx, from, to); |
| 412 | } |
| 413 | |
| 414 | exit: |
| 415 | GRN_API_RETURN(ctx->rc); |
| 416 | } |
| 417 | |
| 418 | double |
| 419 | grn_table_get_score(grn_ctx *ctx, grn_obj *table, grn_id id) |
| 420 | { |
| 421 | if (id == GRN_ID_NIL) { |
| 422 | return 0.0; |
| 423 | } |
| 424 | |
| 425 | uint32_t value_size; |
| 426 | grn_rset_recinfo *ri = |
| 427 | (grn_rset_recinfo *)grn_obj_get_value_(ctx, table, id, &value_size); |
| 428 | double score = ri->score; |
| 429 | if (!(table->header.domain & GRN_OBJ_TMP_OBJECT)) { |
| 430 | return score; |
| 431 | } |
| 432 | |
| 433 | grn_obj *parent = grn_ctx_at(ctx, table->header.domain); |
| 434 | grn_id parent_record_id = GRN_ID_NIL; |
| 435 | if (parent && (parent->header.flags & GRN_OBJ_WITH_SUBREC)) { |
| 436 | if (grn_table_get_key(ctx, |
| 437 | table, |
| 438 | id, |
| 439 | (void *)&parent_record_id, |
| 440 | sizeof(grn_id)) == 0) { |
| 441 | parent_record_id = GRN_ID_NIL; |
| 442 | } |
| 443 | } |
| 444 | if (parent_record_id == GRN_ID_NIL) { |
| 445 | return score; |
| 446 | } |
| 447 | |
| 448 | while (parent && (parent->header.flags & GRN_OBJ_WITH_SUBREC)) { |
| 449 | uint32_t parent_value_size; |
| 450 | grn_rset_recinfo *parent_ri = (grn_rset_recinfo *) |
| 451 | grn_obj_get_value_(ctx, parent, parent_record_id, &parent_value_size); |
| 452 | if (parent_value_size == 0) { |
| 453 | break; |
| 454 | } |
| 455 | score += parent_ri->score; |
| 456 | if (grn_table_get_key(ctx, |
| 457 | parent, |
| 458 | parent_record_id, |
| 459 | (void *)&parent_record_id, |
| 460 | sizeof(grn_id)) == 0) { |
| 461 | break; |
| 462 | } |
| 463 | if (parent_record_id == GRN_ID_NIL) { |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | grn_id next_parent_domain = parent->header.domain; |
| 468 | grn_obj_unref(ctx, parent); |
| 469 | parent = grn_ctx_at(ctx, next_parent_domain); |
| 470 | } |
| 471 | if (parent) { |
| 472 | grn_obj_unref(ctx, parent); |
| 473 | parent = NULL; |
| 474 | } |
| 475 | |
| 476 | return score; |
| 477 | } |
| 478 | |
| 479 | grn_rc |
| 480 | grn_table_get_duplicated_keys(grn_ctx *ctx, |
| 481 | grn_obj *table, |
| 482 | grn_obj **duplicated_keys) |
| 483 | { |
| 484 | GRN_API_ENTER; |
| 485 | |
| 486 | *duplicated_keys = NULL; |
| 487 | |
| 488 | const char *tag = "[table][get-duplicated-keys]"; |
| 489 | |
| 490 | if (!grn_obj_is_table_with_key(ctx, table)) { |
| 491 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 492 | int name_size; |
| 493 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 494 | ERR(GRN_INVALID_ARGUMENT, |
| 495 | "%s must be a table that has key: <%.*s>", |
| 496 | tag, |
| 497 | name_size, |
| 498 | name); |
| 499 | GRN_API_RETURN(ctx->rc); |
| 500 | } |
| 501 | |
| 502 | grn_obj *keys; |
| 503 | { |
| 504 | grn_obj *domain = grn_ctx_at(ctx, table->header.domain); |
| 505 | keys = grn_table_create(ctx, |
| 506 | NULL, |
| 507 | 0, |
| 508 | NULL, |
| 509 | GRN_TABLE_HASH_KEY | GRN_OBJ_KEY_LARGE, |
| 510 | domain, |
| 511 | NULL); |
| 512 | grn_obj_unref(ctx, domain); |
| 513 | } |
| 514 | if (!keys) { |
| 515 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 516 | int name_size; |
| 517 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 518 | char message[GRN_CTX_MSGSIZE]; |
| 519 | grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 520 | ERR(GRN_INVALID_ARGUMENT, |
| 521 | "%s failed to create output table: <%.*s>: %s", |
| 522 | tag, |
| 523 | name_size, |
| 524 | name, |
| 525 | message); |
| 526 | goto exit; |
| 527 | } |
| 528 | |
| 529 | const char *records_name = "records"; |
| 530 | grn_obj *records = grn_column_create(ctx, |
| 531 | keys, |
| 532 | records_name, |
| 533 | (unsigned int)strlen(records_name), |
| 534 | NULL, |
| 535 | GRN_OBJ_COLUMN_VECTOR, |
| 536 | table); |
| 537 | if (!records) { |
| 538 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 539 | int name_size; |
| 540 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 541 | char message[GRN_CTX_MSGSIZE]; |
| 542 | grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 543 | ERR(GRN_INVALID_ARGUMENT, |
| 544 | "%s failed to create output column: <%.*s>: %s", |
| 545 | tag, |
| 546 | name_size, |
| 547 | name, |
| 548 | message); |
| 549 | goto exit; |
| 550 | } |
| 551 | |
| 552 | grn_obj record_buffer; |
| 553 | GRN_RECORD_INIT(&record_buffer, 0, DB_OBJ(table)->id); |
| 554 | GRN_TABLE_EACH_BEGIN_FLAGS(ctx, |
| 555 | table, |
| 556 | cursor, |
| 557 | id, |
| 558 | GRN_CURSOR_ASCENDING | GRN_CURSOR_BY_ID) |
| 559 | { |
| 560 | void *key; |
| 561 | int key_size; |
| 562 | key_size = grn_table_cursor_get_key(ctx, cursor, &key); |
| 563 | grn_id keys_id = |
| 564 | grn_table_add(ctx, keys, key, (unsigned int)key_size, NULL); |
| 565 | if (keys_id == GRN_ID_NIL) { |
| 566 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 567 | int name_size; |
| 568 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 569 | char message[GRN_CTX_MSGSIZE]; |
| 570 | grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 571 | grn_obj key_buffer; |
| 572 | GRN_OBJ_INIT(&key_buffer, |
| 573 | GRN_BULK, |
| 574 | GRN_OBJ_DO_SHALLOW_COPY, |
| 575 | table->header.domain); |
| 576 | GRN_TEXT_SET(ctx, &key_buffer, key, key_size); |
| 577 | grn_obj inspected; |
| 578 | grn_inspect(ctx, &inspected, &key_buffer); |
| 579 | grn_rc rc = ctx->rc; |
| 580 | if (rc == GRN_SUCCESS) { |
| 581 | rc = GRN_INVALID_ARGUMENT; |
| 582 | } |
| 583 | ERR(rc, |
| 584 | "%s failed to add key: <%.*s>: %.*s: %s", |
| 585 | tag, |
| 586 | name_size, |
| 587 | name, |
| 588 | (int)GRN_TEXT_LEN(&key_buffer), |
| 589 | GRN_TEXT_VALUE(&key_buffer), |
| 590 | message); |
| 591 | GRN_OBJ_FIN(ctx, &inspected); |
| 592 | GRN_OBJ_FIN(ctx, &key_buffer); |
| 593 | break; |
| 594 | } |
| 595 | |
| 596 | GRN_RECORD_SET(ctx, &record_buffer, id); |
| 597 | grn_obj_set_value(ctx, records, keys_id, &record_buffer, GRN_OBJ_APPEND); |
| 598 | if (ctx->rc != GRN_SUCCESS) { |
| 599 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 600 | int name_size; |
| 601 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 602 | char message[GRN_CTX_MSGSIZE]; |
| 603 | grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 604 | ERR(ctx->rc, |
| 605 | "%s failed to add a record: <%.*s>: <%u>: <%u>: %s", |
| 606 | tag, |
| 607 | name_size, |
| 608 | name, |
| 609 | id, |
| 610 | keys_id, |
| 611 | message); |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | GRN_TABLE_EACH_END(ctx, cursor); |
| 616 | GRN_OBJ_FIN(ctx, &record_buffer); |
| 617 | if (ctx->rc != GRN_SUCCESS) { |
| 618 | goto exit; |
| 619 | } |
| 620 | |
| 621 | grn_obj records_buffer; |
| 622 | GRN_RECORD_INIT(&records_buffer, GRN_OBJ_VECTOR, DB_OBJ(table)->id); |
| 623 | GRN_TABLE_EACH_BEGIN(ctx, keys, cursor, id) |
| 624 | { |
| 625 | GRN_BULK_REWIND(&records_buffer); |
| 626 | grn_obj_get_value(ctx, records, id, &records_buffer); |
| 627 | if (ctx->rc != GRN_SUCCESS) { |
| 628 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 629 | int name_size; |
| 630 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 631 | char message[GRN_CTX_MSGSIZE]; |
| 632 | grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 633 | ERR(ctx->rc, |
| 634 | "%s failed to get records: <%.*s>: <%u>: %s", |
| 635 | tag, |
| 636 | name_size, |
| 637 | name, |
| 638 | id, |
| 639 | message); |
| 640 | break; |
| 641 | } |
| 642 | if (GRN_RECORD_VECTOR_SIZE(&records_buffer) == 1) { |
| 643 | grn_table_cursor_delete(ctx, cursor); |
| 644 | } |
| 645 | } |
| 646 | GRN_TABLE_EACH_END(ctx, cursor); |
| 647 | GRN_OBJ_FIN(ctx, &records_buffer); |
| 648 | if (ctx->rc != GRN_SUCCESS) { |
| 649 | goto exit; |
| 650 | } |
| 651 | |
| 652 | *duplicated_keys = keys; |
| 653 | keys = NULL; |
| 654 | |
| 655 | exit: |
| 656 | if (keys) { |
| 657 | grn_obj_close(ctx, keys); |
| 658 | } |
| 659 | GRN_API_RETURN(ctx->rc); |
| 660 | } |
| 661 | |
| 662 | bool |
| 663 | grn_table_have_duplicated_keys(grn_ctx *ctx, grn_obj *table) |
| 664 | { |
| 665 | GRN_API_ENTER; |
| 666 | |
| 667 | bool have_duplicated_keys = false; |
| 668 | |
| 669 | const char *tag = "[table][have-duplicated-keys]"; |
| 670 | |
| 671 | if (!grn_obj_is_table_with_key(ctx, table)) { |
| 672 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 673 | int name_size; |
| 674 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 675 | ERR(GRN_INVALID_ARGUMENT, |
| 676 | "%s must be a table that has key: <%.*s>", |
| 677 | tag, |
| 678 | name_size, |
| 679 | name); |
| 680 | GRN_API_RETURN(have_duplicated_keys); |
| 681 | } |
| 682 | |
| 683 | grn_obj *keys; |
| 684 | { |
| 685 | grn_obj *domain = grn_ctx_at(ctx, table->header.domain); |
| 686 | keys = grn_table_create(ctx, |
| 687 | NULL, |
| 688 | 0, |
| 689 | NULL, |
| 690 | GRN_TABLE_HASH_KEY | GRN_OBJ_KEY_LARGE, |
| 691 | domain, |
| 692 | NULL); |
| 693 | grn_obj_unref(ctx, domain); |
| 694 | } |
| 695 | if (!keys) { |
| 696 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 697 | int name_size; |
| 698 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 699 | char message[GRN_CTX_MSGSIZE]; |
| 700 | grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 701 | ERR(GRN_INVALID_ARGUMENT, |
| 702 | "%s failed to create internal table: <%.*s>: %s", |
| 703 | tag, |
| 704 | name_size, |
| 705 | name, |
| 706 | message); |
| 707 | goto exit; |
| 708 | } |
| 709 | |
| 710 | GRN_TABLE_EACH_BEGIN_FLAGS(ctx, |
| 711 | table, |
| 712 | cursor, |
| 713 | id, |
| 714 | GRN_CURSOR_ASCENDING | GRN_CURSOR_BY_ID) |
| 715 | { |
| 716 | void *key; |
| 717 | int key_size; |
| 718 | key_size = grn_table_cursor_get_key(ctx, cursor, &key); |
| 719 | int added = 0; |
| 720 | grn_id keys_id = |
| 721 | grn_table_add(ctx, keys, key, (unsigned int)key_size, &added); |
| 722 | if (keys_id == GRN_ID_NIL) { |
| 723 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 724 | int name_size; |
| 725 | name_size = grn_obj_name(ctx, table, name, GRN_TABLE_MAX_KEY_SIZE); |
| 726 | char message[GRN_CTX_MSGSIZE]; |
| 727 | grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 728 | grn_obj key_buffer; |
| 729 | GRN_OBJ_INIT(&key_buffer, |
| 730 | GRN_BULK, |
| 731 | GRN_OBJ_DO_SHALLOW_COPY, |
| 732 | table->header.domain); |
| 733 | GRN_TEXT_SET(ctx, &key_buffer, key, key_size); |
| 734 | grn_obj inspected; |
| 735 | grn_inspect(ctx, &inspected, &key_buffer); |
| 736 | grn_rc rc = ctx->rc; |
| 737 | if (rc == GRN_SUCCESS) { |
| 738 | rc = GRN_INVALID_ARGUMENT; |
| 739 | } |
| 740 | ERR(rc, |
| 741 | "%s failed to add key: <%.*s>: %.*s: %s", |
| 742 | tag, |
| 743 | name_size, |
| 744 | name, |
| 745 | (int)GRN_TEXT_LEN(&key_buffer), |
| 746 | GRN_TEXT_VALUE(&key_buffer), |
| 747 | message); |
| 748 | GRN_OBJ_FIN(ctx, &inspected); |
| 749 | GRN_OBJ_FIN(ctx, &key_buffer); |
| 750 | break; |
| 751 | } |
| 752 | if (!added) { |
| 753 | have_duplicated_keys = true; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | GRN_TABLE_EACH_END(ctx, cursor); |
| 758 | |
| 759 | exit: |
| 760 | if (keys) { |
| 761 | grn_obj_close(ctx, keys); |
| 762 | } |
| 763 | GRN_API_RETURN(have_duplicated_keys); |
| 764 | } |
| 765 | |
| 766 | grn_hash * |
| 767 | grn_table_all_columns(grn_ctx *ctx, grn_obj *table) |
| 768 | { |
| 769 | if (!GRN_OBJ_TABLEP(table)) { |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | grn_id id = DB_OBJ(table)->id; |
| 774 | if (id == GRN_ID_NIL) { |
| 775 | return NULL; |
| 776 | } |
| 777 | |
| 778 | CRITICAL_SECTION_ENTER(ctx->impl->columns_cache_lock); |
| 779 | void *value; |
| 780 | grn_hash *columns = NULL; |
| 781 | grn_id cache_id = |
| 782 | grn_hash_get(ctx, ctx->impl->columns_cache, &id, sizeof(grn_id), &value); |
| 783 | if (cache_id != GRN_ID_NIL) { |
| 784 | columns = *((grn_hash **)value); |
| 785 | } else { |
| 786 | int added = 0; |
| 787 | cache_id = grn_hash_add(ctx, |
| 788 | ctx->impl->columns_cache, |
| 789 | &id, |
| 790 | sizeof(grn_id), |
| 791 | &value, |
| 792 | &added); |
| 793 | if (cache_id != GRN_ID_NIL && added) { |
| 794 | columns = grn_hash_create(ctx, |
| 795 | NULL, |
| 796 | sizeof(grn_id), |
| 797 | 0, |
| 798 | GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); |
| 799 | grn_table_columns(ctx, table, "", 0, (grn_obj *)columns); |
| 800 | *((grn_hash **)value) = columns; |
| 801 | } |
| 802 | } |
| 803 | CRITICAL_SECTION_LEAVE(ctx->impl->columns_cache_lock); |
| 804 | |
| 805 | return columns; |
| 806 | } |
| 807 | |
| 808 | bool |
| 809 | grn_table_have_tokenizer(grn_ctx *ctx, grn_obj *table) |
| 810 | { |
| 811 | if (!GRN_OBJ_TABLEP(table)) { |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | grn_obj *tokenizer = NULL; |
| 816 | grn_table_get_info(ctx, table, NULL, NULL, &tokenizer, NULL, NULL); |
| 817 | return tokenizer != NULL; |
| 818 | } |
| 819 | |
| 820 | static grn_obj * |
| 821 | grn_table_get_extractors(grn_ctx *ctx, grn_obj *table) |
| 822 | { |
| 823 | if (!table) { |
| 824 | return NULL; |
| 825 | } |
| 826 | |
| 827 | switch (table->header.type) { |
| 828 | case GRN_TABLE_PAT_KEY: |
| 829 | return &(((grn_pat *)table)->extractors); |
| 830 | case GRN_TABLE_DAT_KEY: |
| 831 | return &(((grn_dat *)table)->extractors); |
| 832 | case GRN_TABLE_HASH_KEY: |
| 833 | return &(((grn_hash *)table)->extractors); |
| 834 | default: |
| 835 | return NULL; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | bool |
| 840 | grn_table_have_extractor(grn_ctx *ctx, grn_obj *table) |
| 841 | { |
| 842 | grn_obj *extractors = grn_table_get_extractors(ctx, table); |
| 843 | if (!extractors) { |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | return GRN_BULK_VSIZE(extractors) > 0; |
| 848 | } |
| 849 | |
| 850 | grn_obj * |
| 851 | grn_table_extract(grn_ctx *ctx, grn_obj *table, grn_obj *value) |
| 852 | { |
| 853 | GRN_API_ENTER; |
| 854 | |
| 855 | const char *tag = "[table][extract]"; |
| 856 | |
| 857 | grn_obj *extractors = grn_table_get_extractors(ctx, table); |
| 858 | if (!extractors) { |
| 859 | /* grn_array never have extractors but we accept it as a valid */ |
| 860 | /* table. */ |
| 861 | if (table && table->header.type == GRN_TABLE_NO_KEY) { |
| 862 | GRN_API_RETURN(value); |
| 863 | } else { |
| 864 | GRN_DEFINE_NAME(table); |
| 865 | ERR(GRN_INVALID_ARGUMENT, |
| 866 | "%s must be a table: <%.*s>", |
| 867 | tag, |
| 868 | name_size, |
| 869 | name); |
| 870 | GRN_API_RETURN(NULL); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | grn_extract_data data; |
| 875 | grn_extract_data_init(ctx, &data); |
| 876 | data.table = table; |
| 877 | data.value = value; |
| 878 | |
| 879 | grn_table_module *raw_extractors = |
| 880 | (grn_table_module *)GRN_BULK_HEAD(extractors); |
| 881 | size_t i; |
| 882 | size_t n = GRN_BULK_VSIZE(extractors) / sizeof(grn_table_module); |
| 883 | for (i = 0; i < n; i++) { |
| 884 | data.index = i; |
| 885 | |
| 886 | grn_obj *extractor = raw_extractors[i].proc; |
| 887 | grn_obj *extracted = grn_extractor_extract(ctx, extractor, &data); |
| 888 | if (ctx->rc != GRN_SUCCESS) { |
| 889 | data.value = NULL; |
| 890 | break; |
| 891 | } |
| 892 | if (extracted) { |
| 893 | if (data.value != value) { |
| 894 | grn_obj_close(ctx, data.value); |
| 895 | } |
| 896 | data.value = extracted; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | GRN_API_RETURN(data.value); |
| 901 | } |