| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2026 Sutou Kouhei <[email protected]> |
| 4 | Copyright (C) 2025 Horimoto Yasuhiro <[email protected]> |
| 5 | |
| 6 | This library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | This library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with this library; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include "grn.h" |
| 22 | #include "grn_ctx_impl.h" |
| 23 | #include "grn_pat.h" |
| 24 | #include "grn_obj.h" |
| 25 | #include "grn_output.h" |
| 26 | #include "grn_util.h" |
| 27 | #include "grn_normalizer.h" |
| 28 | #include "grn_wal.h" |
| 29 | #include "grn_portability.h" |
| 30 | |
| 31 | #include <limits.h> |
| 32 | #include <math.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #define GRN_PAT_DELETED (GRN_ID_MAX + 1) |
| 36 | |
| 37 | #define GRN_PAT_SEGMENT_SIZE 0x400000 |
| 38 | #define W_OF_KEY_IN_A_SEGMENT 22 |
| 39 | #define W_OF_PAT_IN_A_SEGMENT 18 |
| 40 | #define W_OF_SIS_IN_A_SEGMENT 19 |
| 41 | #define KEY_MASK_IN_A_SEGMENT 0x3fffff |
| 42 | #define PAT_MASK_IN_A_SEGMENT 0x3ffff |
| 43 | #define SIS_MASK_IN_A_SEGMENT 0x7ffff |
| 44 | #define SEG_NOT_ASSIGNED 0xffff |
| 45 | #define GRN_PAT_MAX_SEGMENT 0x1000 |
| 46 | /* If we use GRN_PAT_MAX_N_SEGMENTS, max total key size is 4GiB: |
| 47 | GRN_PAT_SEGMENT_SIZE * 0x400 = 4GiB */ |
| 48 | #define GRN_PAT_MAX_N_SEGMENTS 0x400 |
| 49 | /* If we use GRN_PAT_MAX_N_SEGMENTS_LARGE, max total key size is 1TiB: |
| 50 | GRN_PAT_SEGMENT_SIZE * 0x40000 = 1TiB */ |
| 51 | #define GRN_PAT_MAX_N_SEGMENTS_LARGE 0x40000 |
| 52 | #define GRN_PAT_MAX_TOTAL_KEY_SIZE (UINT32_MAX - 1) |
| 53 | #define GRN_PAT_MAX_TOTAL_KEY_SIZE_LARGE \ |
| 54 | ((uint64_t)GRN_PAT_SEGMENT_SIZE * (uint64_t)GRN_PAT_MAX_N_SEGMENTS_LARGE - \ |
| 55 | 1) // 1TiB - 1 (-1 is initial curr_key value) |
| 56 | #define GRN_PAT_MDELINFOS (GRN_PAT_NDELINFOS - 1) |
| 57 | |
| 58 | #define GRN_PAT_BIN_KEY 0x70000 |
| 59 | |
| 60 | #define GRN_PAT_SEGMENT_SIZE_IN_BIT 22 /* log2(GRN_PAT_SEGMENT_SIZE) == 22 */ |
| 61 | |
| 62 | typedef enum { |
| 63 | DIRECTION_LEFT = 0, |
| 64 | DIRECTION_RIGHT = 1, |
| 65 | } pat_node_direction; |
| 66 | |
| 67 | typedef struct { |
| 68 | /* |
| 69 | lr[0]: the left node. |
| 70 | lr[1]: the right node. |
| 71 | |
| 72 | The left node has 0 at the nth bit at the nth byte. |
| 73 | The right node has 1 at the nth bit at the nth byte. |
| 74 | 'check' value indicate 'at the nth bit at the nth byte'. |
| 75 | |
| 76 | The both available nodes has larger check value rather |
| 77 | than the current node. |
| 78 | |
| 79 | The first node (PAT_AT(pat, GRN_ID_NIL, node)) has only |
| 80 | the right node and the node is the start point. |
| 81 | */ |
| 82 | grn_id lr[2]; |
| 83 | /* |
| 84 | pat_node_is_key_immediate(pat, node) == false: key bytes offset in memory |
| 85 | map. |
| 86 | pat_node_is_key_immediate(pat, node) == true: the key bytes. |
| 87 | */ |
| 88 | uint32_t key; |
| 89 | /* |
| 90 | nth byte: 12, nth bit: 3, terminated: 1 |
| 91 | |
| 92 | nth byte is different in key bytes: (check >> 4): max == 4095 |
| 93 | the left most byte is the 0th byte and the right most byte is the 11th byte. |
| 94 | |
| 95 | nth bit is different in nth byte: ((check >> 1) & 0b111) |
| 96 | the left most bit is the 0th bit and the right most bit is the 7th bit. |
| 97 | |
| 98 | terminated: (check & 0b1) |
| 99 | terminated == 1: key is terminated. |
| 100 | */ |
| 101 | uint16_t check; |
| 102 | /* length: 13, immediate: 1, deleting: 1 */ |
| 103 | uint16_t bits; |
| 104 | } pat_node; |
| 105 | |
| 106 | /* |
| 107 | This struct is almost the same as `pat_node`. |
| 108 | We only use this struct when we specify `KEY_LARGE` |
| 109 | This struct is different from pat_node only in that the size of key. |
| 110 | The size of pat_node.key is uint32_t. |
| 111 | The size of pat_node_large.key is uint64_t. |
| 112 | */ |
| 113 | typedef struct { |
| 114 | grn_id lr[2]; |
| 115 | uint64_t key; |
| 116 | uint16_t check; |
| 117 | uint16_t bits; |
| 118 | } pat_node_large; |
| 119 | |
| 120 | typedef union { |
| 121 | pat_node node; |
| 122 | pat_node_large node_large; |
| 123 | } pat_node_common; |
| 124 | |
| 125 | #define PAT_DELETING (1 << 1) |
| 126 | #define PAT_IMMEDIATE (1 << 2) |
| 127 | |
| 128 | static inline bool |
| 129 | pat_is_key_large(grn_pat *pat) |
| 130 | { |
| 131 | return (pat->header->flags & GRN_OBJ_KEY_LARGE) == GRN_OBJ_KEY_LARGE; |
| 132 | } |
| 133 | |
| 134 | static inline bool |
| 135 | pat_key_is_embeddable(grn_pat *pat, uint32_t key_size) |
| 136 | { |
| 137 | if (pat_is_key_large(pat)) { |
| 138 | return key_size <= sizeof(uint64_t); |
| 139 | } else { |
| 140 | return key_size <= sizeof(uint32_t); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static inline uint32_t |
| 145 | pat_key_storage_size(grn_pat *pat, uint32_t key_size) |
| 146 | { |
| 147 | return pat_key_is_embeddable(pat, key_size) ? 0 : key_size; |
| 148 | } |
| 149 | |
| 150 | static inline grn_id |
| 151 | pat_node_get_child(grn_pat *pat, |
| 152 | pat_node_common *node, |
| 153 | pat_node_direction direction) |
| 154 | { |
| 155 | if (pat_is_key_large(pat)) { |
| 156 | return node->node_large.lr[direction]; |
| 157 | } else { |
| 158 | return node->node.lr[direction]; |
| 159 | } |
| 160 | } |
| 161 | static inline grn_id |
| 162 | pat_node_get_right(grn_pat *pat, pat_node_common *node) |
| 163 | { |
| 164 | return pat_node_get_child(pat, node, DIRECTION_RIGHT); |
| 165 | } |
| 166 | static inline grn_id |
| 167 | pat_node_get_left(grn_pat *pat, pat_node_common *node) |
| 168 | { |
| 169 | return pat_node_get_child(pat, node, DIRECTION_LEFT); |
| 170 | } |
| 171 | |
| 172 | static inline grn_id * |
| 173 | pat_node_get_child_address(grn_pat *pat, |
| 174 | pat_node_common *node, |
| 175 | pat_node_direction direction) |
| 176 | { |
| 177 | if (pat_is_key_large(pat)) { |
| 178 | return &(node->node_large.lr[direction]); |
| 179 | } else { |
| 180 | return &(node->node.lr[direction]); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static inline grn_id * |
| 185 | pat_node_get_right_address(grn_pat *pat, pat_node_common *node) |
| 186 | { |
| 187 | return pat_node_get_child_address(pat, node, DIRECTION_RIGHT); |
| 188 | } |
| 189 | |
| 190 | static inline grn_id * |
| 191 | pat_node_get_left_address(grn_pat *pat, pat_node_common *node) |
| 192 | { |
| 193 | return pat_node_get_child_address(pat, node, DIRECTION_LEFT); |
| 194 | } |
| 195 | |
| 196 | static inline void |
| 197 | pat_node_set_child(grn_pat *pat, |
| 198 | pat_node_common *node, |
| 199 | pat_node_direction direction, |
| 200 | grn_id id) |
| 201 | { |
| 202 | if (pat_is_key_large(pat)) { |
| 203 | node->node_large.lr[direction] = id; |
| 204 | } else { |
| 205 | node->node.lr[direction] = id; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | static inline void |
| 210 | pat_node_set_right(grn_pat *pat, pat_node_common *node, grn_id id) |
| 211 | { |
| 212 | pat_node_set_child(pat, node, DIRECTION_RIGHT, id); |
| 213 | } |
| 214 | |
| 215 | static inline void |
| 216 | pat_node_set_left(grn_pat *pat, pat_node_common *node, grn_id id) |
| 217 | { |
| 218 | pat_node_set_child(pat, node, DIRECTION_LEFT, id); |
| 219 | } |
| 220 | |
| 221 | static inline bool |
| 222 | pat_node_is_deleting(grn_pat *pat, pat_node_common *node) |
| 223 | { |
| 224 | if (pat_is_key_large(pat)) { |
| 225 | return node->node_large.bits & PAT_DELETING; |
| 226 | } else { |
| 227 | return node->node.bits & PAT_DELETING; |
| 228 | } |
| 229 | } |
| 230 | static inline bool |
| 231 | pat_node_is_key_immediate(grn_pat *pat, pat_node_common *node) |
| 232 | { |
| 233 | if (pat_is_key_large(pat)) { |
| 234 | return node->node_large.bits & PAT_IMMEDIATE; |
| 235 | } else { |
| 236 | return node->node.bits & PAT_IMMEDIATE; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static inline uint32_t |
| 241 | pat_node_get_key_length(grn_pat *pat, pat_node_common *node) |
| 242 | { |
| 243 | uint16_t bits; |
| 244 | if (pat_is_key_large(pat)) { |
| 245 | bits = node->node_large.bits; |
| 246 | } else { |
| 247 | bits = node->node.bits; |
| 248 | } |
| 249 | return (uint32_t)((bits >> 3) + 1); |
| 250 | } |
| 251 | static inline uint16_t |
| 252 | pat_node_get_check(grn_pat *pat, pat_node_common *node) |
| 253 | { |
| 254 | if (pat_is_key_large(pat)) { |
| 255 | return node->node_large.check; |
| 256 | } else { |
| 257 | return node->node.check; |
| 258 | } |
| 259 | } |
| 260 | static inline void |
| 261 | pat_node_set_deleting_on(grn_pat *pat, pat_node_common *node) |
| 262 | { |
| 263 | if (pat_is_key_large(pat)) { |
| 264 | node->node_large.bits |= PAT_DELETING; |
| 265 | } else { |
| 266 | node->node.bits |= PAT_DELETING; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static inline void |
| 271 | pat_node_set_key_immediate_on(grn_pat *pat, pat_node_common *node) |
| 272 | { |
| 273 | if (pat_is_key_large(pat)) { |
| 274 | node->node_large.bits |= PAT_IMMEDIATE; |
| 275 | } else { |
| 276 | node->node.bits |= PAT_IMMEDIATE; |
| 277 | } |
| 278 | } |
| 279 | static inline void |
| 280 | pat_node_set_deleting_off(grn_pat *pat, pat_node_common *node) |
| 281 | { |
| 282 | if (pat_is_key_large(pat)) { |
| 283 | node->node_large.bits &= ~PAT_DELETING; |
| 284 | } else { |
| 285 | node->node.bits &= ~PAT_DELETING; |
| 286 | } |
| 287 | } |
| 288 | static inline void |
| 289 | pat_node_set_key_immediate_off(grn_pat *pat, pat_node_common *node) |
| 290 | { |
| 291 | if (pat_is_key_large(pat)) { |
| 292 | node->node_large.bits &= ~PAT_IMMEDIATE; |
| 293 | } else { |
| 294 | node->node.bits &= ~PAT_IMMEDIATE; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static inline void |
| 299 | pat_node_set_key_length(grn_pat *pat, |
| 300 | pat_node_common *node, |
| 301 | uint32_t key_length) |
| 302 | { |
| 303 | if (pat_is_key_large(pat)) { |
| 304 | node->node_large.bits = |
| 305 | (node->node_large.bits & ((1 << 3) - 1)) | ((key_length - 1) << 3); |
| 306 | } else { |
| 307 | node->node.bits = |
| 308 | (node->node.bits & ((1 << 3) - 1)) | ((key_length - 1) << 3); |
| 309 | } |
| 310 | } |
| 311 | static inline void |
| 312 | pat_node_set_check(grn_pat *pat, pat_node_common *node, uint16_t check) |
| 313 | { |
| 314 | if (pat_is_key_large(pat)) { |
| 315 | node->node_large.check = check; |
| 316 | } else { |
| 317 | node->node.check = check; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | #define PAT_CHECK_BYTE_DIFFERENCES_SHIFT 4 |
| 322 | #define PAT_CHECK_BIT_DIFFERENCES_SHIFT 1 |
| 323 | #define PAT_CHECK_PACK(byte_differences, bit_differences, terminated) \ |
| 324 | (((byte_differences) << PAT_CHECK_BYTE_DIFFERENCES_SHIFT) + \ |
| 325 | ((bit_differences) << PAT_CHECK_BIT_DIFFERENCES_SHIFT) + \ |
| 326 | ((terminated) ? 1 : 0)) |
| 327 | #define PAT_CHECK_BYTE_DIFFERENCES(check) \ |
| 328 | ((check) >> PAT_CHECK_BYTE_DIFFERENCES_SHIFT) |
| 329 | #define PAT_CHECK_BIT_DIFFERENCES(check) \ |
| 330 | (((check) >> PAT_CHECK_BIT_DIFFERENCES_SHIFT) & 0b111) |
| 331 | #define PAT_CHECK_IS_TERMINATED(check) ((check) & 1) |
| 332 | #define PAT_CHECK_ADD_BIT_DIFFERENCES(check, n) \ |
| 333 | ((check) + ((n) << PAT_CHECK_BIT_DIFFERENCES_SHIFT)) |
| 334 | |
| 335 | typedef struct { |
| 336 | grn_id children; |
| 337 | grn_id sibling; |
| 338 | } sis_node; |
| 339 | |
| 340 | enum { |
| 341 | SEGMENT_KEY = 0, |
| 342 | SEGMENT_PAT = 1, |
| 343 | SEGMENT_SIS = 2 |
| 344 | }; |
| 345 | |
| 346 | void |
| 347 | grn_p_pat_node(grn_ctx *ctx, grn_pat *pat, pat_node_common *node); |
| 348 | |
| 349 | /* WAL operation */ |
| 350 | |
| 351 | typedef struct { |
| 352 | grn_pat *pat; |
| 353 | uint64_t wal_id; |
| 354 | const char *tag; |
| 355 | grn_wal_event event; |
| 356 | grn_id record_id; |
| 357 | pat_node_direction record_direction; |
| 358 | const uint8_t *key; |
| 359 | uint32_t key_size; |
| 360 | uint64_t key_offset; |
| 361 | uint32_t shared_key_offset; |
| 362 | bool is_shared; |
| 363 | uint16_t check; |
| 364 | grn_id parent_record_id; |
| 365 | pat_node_direction parent_record_direction; |
| 366 | uint16_t parent_check; |
| 367 | grn_id grandparent_record_id; |
| 368 | grn_id otherside_record_id; |
| 369 | uint16_t otherside_check; |
| 370 | grn_id left_record_id; |
| 371 | grn_id right_record_id; |
| 372 | grn_id next_garbage_record_id; |
| 373 | uint32_t n_garbages; |
| 374 | uint32_t n_entries; |
| 375 | uint32_t delete_info_index; |
| 376 | uint32_t delete_info_phase1_index; |
| 377 | uint32_t delete_info_phase2_index; |
| 378 | uint32_t delete_info_phase3_index; |
| 379 | grn_id previous_record_id; |
| 380 | } grn_pat_wal_add_entry_data; |
| 381 | |
| 382 | typedef struct { |
| 383 | bool record_id; |
| 384 | bool record_direction; |
| 385 | bool key; |
| 386 | bool key_size; |
| 387 | bool key_offset; |
| 388 | bool shared_key_offset; |
| 389 | bool is_shared; |
| 390 | bool check; |
| 391 | bool parent_record_id; |
| 392 | bool parent_record_direction; |
| 393 | bool parent_check; |
| 394 | bool grandparent_record_id; |
| 395 | bool otherside_record_id; |
| 396 | bool otherside_check; |
| 397 | bool left_record_id; |
| 398 | bool right_record_id; |
| 399 | bool next_garbage_record_id; |
| 400 | bool n_garbages; |
| 401 | bool n_entries; |
| 402 | bool delete_info_index; |
| 403 | bool delete_info_phase1_index; |
| 404 | bool delete_info_phase2_index; |
| 405 | bool delete_info_phase3_index; |
| 406 | bool previous_record_id; |
| 407 | } grn_pat_wal_add_entry_used; |
| 408 | |
| 409 | static grn_rc |
| 410 | grn_pat_wal_add_entry_add_entry(grn_ctx *ctx, |
| 411 | grn_pat_wal_add_entry_data *data, |
| 412 | grn_pat_wal_add_entry_used *used) |
| 413 | { |
| 414 | used->record_id = true; |
| 415 | used->record_direction = true; |
| 416 | used->key = true; |
| 417 | used->key_size = true; |
| 418 | used->key_offset = true; |
| 419 | used->check = true; |
| 420 | used->parent_record_id = true; |
| 421 | used->n_entries = true; |
| 422 | used->previous_record_id = true; |
| 423 | const void *key = data->key; |
| 424 | size_t key_size = data->key_size; |
| 425 | uint32_t record_direction = data->record_direction; |
| 426 | uint32_t check = data->check; |
| 427 | return grn_wal_add_entry(ctx, |
| 428 | (grn_obj *)(data->pat), |
| 429 | false, |
| 430 | &(data->wal_id), |
| 431 | data->tag, |
| 432 | |
| 433 | GRN_WAL_KEY_EVENT, |
| 434 | GRN_WAL_VALUE_EVENT, |
| 435 | data->event, |
| 436 | |
| 437 | GRN_WAL_KEY_RECORD_ID, |
| 438 | GRN_WAL_VALUE_RECORD_ID, |
| 439 | data->record_id, |
| 440 | |
| 441 | GRN_WAL_KEY_RECORD_DIRECTION, |
| 442 | GRN_WAL_VALUE_UINT32, |
| 443 | record_direction, |
| 444 | |
| 445 | GRN_WAL_KEY_KEY, |
| 446 | GRN_WAL_VALUE_BINARY, |
| 447 | key, |
| 448 | key_size, |
| 449 | |
| 450 | GRN_WAL_KEY_KEY_OFFSET, |
| 451 | GRN_WAL_VALUE_UINT64, |
| 452 | data->key_offset, |
| 453 | |
| 454 | GRN_WAL_KEY_CHECK, |
| 455 | GRN_WAL_VALUE_UINT32, |
| 456 | check, |
| 457 | |
| 458 | GRN_WAL_KEY_PARENT_RECORD_ID, |
| 459 | GRN_WAL_VALUE_RECORD_ID, |
| 460 | data->parent_record_id, |
| 461 | |
| 462 | GRN_WAL_KEY_N_ENTRIES, |
| 463 | GRN_WAL_VALUE_UINT32, |
| 464 | data->n_entries, |
| 465 | |
| 466 | GRN_WAL_KEY_PREVIOUS_RECORD_ID, |
| 467 | GRN_WAL_VALUE_RECORD_ID, |
| 468 | data->previous_record_id, |
| 469 | |
| 470 | GRN_WAL_KEY_END); |
| 471 | } |
| 472 | |
| 473 | static grn_rc |
| 474 | grn_pat_wal_add_entry_add_shared_entry(grn_ctx *ctx, |
| 475 | grn_pat_wal_add_entry_data *data, |
| 476 | grn_pat_wal_add_entry_used *used) |
| 477 | { |
| 478 | used->record_id = true; |
| 479 | used->record_direction = true; |
| 480 | used->key = true; |
| 481 | used->key_size = true; |
| 482 | used->shared_key_offset = true; |
| 483 | used->check = true; |
| 484 | used->parent_record_id = true; |
| 485 | used->n_entries = true; |
| 486 | used->previous_record_id = true; |
| 487 | const void *key = data->key; |
| 488 | size_t key_size = data->key_size; |
| 489 | uint32_t check = data->check; |
| 490 | uint32_t record_direction = data->record_direction; |
| 491 | return grn_wal_add_entry(ctx, |
| 492 | (grn_obj *)(data->pat), |
| 493 | false, |
| 494 | &(data->wal_id), |
| 495 | data->tag, |
| 496 | |
| 497 | GRN_WAL_KEY_EVENT, |
| 498 | GRN_WAL_VALUE_EVENT, |
| 499 | data->event, |
| 500 | |
| 501 | GRN_WAL_KEY_RECORD_ID, |
| 502 | GRN_WAL_VALUE_RECORD_ID, |
| 503 | data->record_id, |
| 504 | |
| 505 | GRN_WAL_KEY_RECORD_DIRECTION, |
| 506 | GRN_WAL_VALUE_UINT32, |
| 507 | record_direction, |
| 508 | |
| 509 | GRN_WAL_KEY_KEY, |
| 510 | GRN_WAL_VALUE_BINARY, |
| 511 | key, |
| 512 | key_size, |
| 513 | |
| 514 | GRN_WAL_KEY_SHARED_KEY_OFFSET, |
| 515 | GRN_WAL_VALUE_UINT32, |
| 516 | data->shared_key_offset, |
| 517 | |
| 518 | GRN_WAL_KEY_CHECK, |
| 519 | GRN_WAL_VALUE_UINT32, |
| 520 | check, |
| 521 | |
| 522 | GRN_WAL_KEY_PARENT_RECORD_ID, |
| 523 | GRN_WAL_VALUE_RECORD_ID, |
| 524 | data->parent_record_id, |
| 525 | |
| 526 | GRN_WAL_KEY_N_ENTRIES, |
| 527 | GRN_WAL_VALUE_UINT32, |
| 528 | data->n_entries, |
| 529 | |
| 530 | GRN_WAL_KEY_PREVIOUS_RECORD_ID, |
| 531 | GRN_WAL_VALUE_RECORD_ID, |
| 532 | data->previous_record_id, |
| 533 | |
| 534 | GRN_WAL_KEY_END); |
| 535 | } |
| 536 | |
| 537 | static grn_rc |
| 538 | grn_pat_wal_add_entry_reuse_entry(grn_ctx *ctx, |
| 539 | grn_pat_wal_add_entry_data *data, |
| 540 | grn_pat_wal_add_entry_used *used) |
| 541 | { |
| 542 | used->record_id = true; |
| 543 | used->record_direction = true; |
| 544 | used->key = true; |
| 545 | used->key_size = true; |
| 546 | used->check = true; |
| 547 | used->parent_record_id = true; |
| 548 | used->next_garbage_record_id = true; |
| 549 | used->n_garbages = true; |
| 550 | used->n_entries = true; |
| 551 | used->previous_record_id = true; |
| 552 | const void *key = data->key; |
| 553 | size_t key_size = data->key_size; |
| 554 | uint32_t record_direction = data->record_direction; |
| 555 | uint32_t check = data->check; |
| 556 | return grn_wal_add_entry(ctx, |
| 557 | (grn_obj *)(data->pat), |
| 558 | false, |
| 559 | &(data->wal_id), |
| 560 | data->tag, |
| 561 | |
| 562 | GRN_WAL_KEY_EVENT, |
| 563 | GRN_WAL_VALUE_EVENT, |
| 564 | data->event, |
| 565 | |
| 566 | GRN_WAL_KEY_RECORD_ID, |
| 567 | GRN_WAL_VALUE_RECORD_ID, |
| 568 | data->record_id, |
| 569 | |
| 570 | GRN_WAL_KEY_RECORD_DIRECTION, |
| 571 | GRN_WAL_VALUE_UINT32, |
| 572 | record_direction, |
| 573 | |
| 574 | GRN_WAL_KEY_KEY, |
| 575 | GRN_WAL_VALUE_BINARY, |
| 576 | key, |
| 577 | key_size, |
| 578 | |
| 579 | GRN_WAL_KEY_CHECK, |
| 580 | GRN_WAL_VALUE_UINT32, |
| 581 | check, |
| 582 | |
| 583 | GRN_WAL_KEY_NEXT_GARBAGE_RECORD_ID, |
| 584 | GRN_WAL_VALUE_RECORD_ID, |
| 585 | data->next_garbage_record_id, |
| 586 | |
| 587 | GRN_WAL_KEY_PARENT_RECORD_ID, |
| 588 | GRN_WAL_VALUE_RECORD_ID, |
| 589 | data->parent_record_id, |
| 590 | |
| 591 | GRN_WAL_KEY_N_GARBAGES, |
| 592 | GRN_WAL_VALUE_UINT32, |
| 593 | data->n_garbages, |
| 594 | |
| 595 | GRN_WAL_KEY_N_ENTRIES, |
| 596 | GRN_WAL_VALUE_UINT32, |
| 597 | data->n_entries, |
| 598 | |
| 599 | GRN_WAL_KEY_PREVIOUS_RECORD_ID, |
| 600 | GRN_WAL_VALUE_RECORD_ID, |
| 601 | data->previous_record_id, |
| 602 | |
| 603 | GRN_WAL_KEY_END); |
| 604 | } |
| 605 | |
| 606 | static grn_rc |
| 607 | grn_pat_wal_add_entry_reuse_shared_entry(grn_ctx *ctx, |
| 608 | grn_pat_wal_add_entry_data *data, |
| 609 | grn_pat_wal_add_entry_used *used) |
| 610 | { |
| 611 | used->record_id = true; |
| 612 | used->record_direction = true; |
| 613 | used->key = true; |
| 614 | used->key_size = true; |
| 615 | used->shared_key_offset = true; |
| 616 | used->check = true; |
| 617 | used->parent_record_id = true; |
| 618 | used->next_garbage_record_id = true; |
| 619 | used->n_garbages = true; |
| 620 | used->n_entries = true; |
| 621 | used->previous_record_id = true; |
| 622 | const void *key = data->key; |
| 623 | size_t key_size = data->key_size; |
| 624 | uint32_t check = data->check; |
| 625 | uint32_t record_direction = data->record_direction; |
| 626 | return grn_wal_add_entry(ctx, |
| 627 | (grn_obj *)(data->pat), |
| 628 | false, |
| 629 | &(data->wal_id), |
| 630 | data->tag, |
| 631 | |
| 632 | GRN_WAL_KEY_EVENT, |
| 633 | GRN_WAL_VALUE_EVENT, |
| 634 | data->event, |
| 635 | |
| 636 | GRN_WAL_KEY_RECORD_ID, |
| 637 | GRN_WAL_VALUE_RECORD_ID, |
| 638 | data->record_id, |
| 639 | |
| 640 | GRN_WAL_KEY_RECORD_DIRECTION, |
| 641 | GRN_WAL_VALUE_UINT32, |
| 642 | record_direction, |
| 643 | |
| 644 | GRN_WAL_KEY_KEY, |
| 645 | GRN_WAL_VALUE_BINARY, |
| 646 | key, |
| 647 | key_size, |
| 648 | |
| 649 | GRN_WAL_KEY_SHARED_KEY_OFFSET, |
| 650 | GRN_WAL_VALUE_UINT32, |
| 651 | data->shared_key_offset, |
| 652 | |
| 653 | GRN_WAL_KEY_CHECK, |
| 654 | GRN_WAL_VALUE_UINT32, |
| 655 | check, |
| 656 | |
| 657 | GRN_WAL_KEY_PARENT_RECORD_ID, |
| 658 | GRN_WAL_VALUE_RECORD_ID, |
| 659 | data->parent_record_id, |
| 660 | |
| 661 | GRN_WAL_KEY_NEXT_GARBAGE_RECORD_ID, |
| 662 | GRN_WAL_VALUE_RECORD_ID, |
| 663 | data->next_garbage_record_id, |
| 664 | |
| 665 | GRN_WAL_KEY_N_GARBAGES, |
| 666 | GRN_WAL_VALUE_UINT32, |
| 667 | data->n_garbages, |
| 668 | |
| 669 | GRN_WAL_KEY_N_ENTRIES, |
| 670 | GRN_WAL_VALUE_UINT32, |
| 671 | data->n_entries, |
| 672 | |
| 673 | GRN_WAL_KEY_PREVIOUS_RECORD_ID, |
| 674 | GRN_WAL_VALUE_RECORD_ID, |
| 675 | data->previous_record_id, |
| 676 | |
| 677 | GRN_WAL_KEY_END); |
| 678 | } |
| 679 | |
| 680 | static grn_rc |
| 681 | grn_pat_wal_add_entry_delete_info_phase1(grn_ctx *ctx, |
| 682 | grn_pat_wal_add_entry_data *data, |
| 683 | grn_pat_wal_add_entry_used *used) |
| 684 | { |
| 685 | used->is_shared = true; |
| 686 | used->delete_info_phase1_index = true; |
| 687 | int is_shared = data->is_shared; |
| 688 | return grn_wal_add_entry(ctx, |
| 689 | (grn_obj *)(data->pat), |
| 690 | false, |
| 691 | &(data->wal_id), |
| 692 | data->tag, |
| 693 | |
| 694 | GRN_WAL_KEY_EVENT, |
| 695 | GRN_WAL_VALUE_EVENT, |
| 696 | data->event, |
| 697 | |
| 698 | GRN_WAL_KEY_IS_SHARED, |
| 699 | GRN_WAL_VALUE_BOOLEAN, |
| 700 | is_shared, |
| 701 | |
| 702 | GRN_WAL_KEY_DELETE_INFO_PHASE1_INDEX, |
| 703 | GRN_WAL_VALUE_UINT32, |
| 704 | data->delete_info_phase1_index, |
| 705 | |
| 706 | GRN_WAL_KEY_END); |
| 707 | } |
| 708 | |
| 709 | static grn_rc |
| 710 | grn_pat_wal_add_entry_delete_info_phase2(grn_ctx *ctx, |
| 711 | grn_pat_wal_add_entry_data *data, |
| 712 | grn_pat_wal_add_entry_used *used) |
| 713 | { |
| 714 | used->record_id = true; |
| 715 | used->key_size = true; |
| 716 | used->check = true; |
| 717 | used->parent_record_id = true; |
| 718 | used->left_record_id = true; |
| 719 | used->right_record_id = true; |
| 720 | used->delete_info_phase2_index = true; |
| 721 | uint32_t check = data->check; |
| 722 | return grn_wal_add_entry(ctx, |
| 723 | (grn_obj *)(data->pat), |
| 724 | false, |
| 725 | &(data->wal_id), |
| 726 | data->tag, |
| 727 | |
| 728 | GRN_WAL_KEY_EVENT, |
| 729 | GRN_WAL_VALUE_EVENT, |
| 730 | data->event, |
| 731 | |
| 732 | GRN_WAL_KEY_RECORD_ID, |
| 733 | GRN_WAL_VALUE_RECORD_ID, |
| 734 | data->record_id, |
| 735 | |
| 736 | GRN_WAL_KEY_KEY_SIZE, |
| 737 | GRN_WAL_VALUE_UINT32, |
| 738 | data->key_size, |
| 739 | |
| 740 | GRN_WAL_KEY_CHECK, |
| 741 | GRN_WAL_VALUE_UINT32, |
| 742 | check, |
| 743 | |
| 744 | GRN_WAL_KEY_PARENT_RECORD_ID, |
| 745 | GRN_WAL_VALUE_RECORD_ID, |
| 746 | data->parent_record_id, |
| 747 | |
| 748 | GRN_WAL_KEY_LEFT_RECORD_ID, |
| 749 | GRN_WAL_VALUE_RECORD_ID, |
| 750 | data->left_record_id, |
| 751 | |
| 752 | GRN_WAL_KEY_RIGHT_RECORD_ID, |
| 753 | GRN_WAL_VALUE_RECORD_ID, |
| 754 | data->right_record_id, |
| 755 | |
| 756 | GRN_WAL_KEY_DELETE_INFO_PHASE2_INDEX, |
| 757 | GRN_WAL_VALUE_UINT32, |
| 758 | data->delete_info_phase2_index, |
| 759 | |
| 760 | GRN_WAL_KEY_END); |
| 761 | } |
| 762 | |
| 763 | static grn_rc |
| 764 | grn_pat_wal_add_entry_delete_info_phase3(grn_ctx *ctx, |
| 765 | grn_pat_wal_add_entry_data *data, |
| 766 | grn_pat_wal_add_entry_used *used) |
| 767 | { |
| 768 | used->record_id = true; |
| 769 | used->is_shared = true; |
| 770 | used->check = true; |
| 771 | used->next_garbage_record_id = true; |
| 772 | used->delete_info_phase3_index = true; |
| 773 | int is_shared = data->is_shared; |
| 774 | return grn_wal_add_entry(ctx, |
| 775 | (grn_obj *)(data->pat), |
| 776 | false, |
| 777 | &(data->wal_id), |
| 778 | data->tag, |
| 779 | |
| 780 | GRN_WAL_KEY_EVENT, |
| 781 | GRN_WAL_VALUE_EVENT, |
| 782 | data->event, |
| 783 | |
| 784 | GRN_WAL_KEY_RECORD_ID, |
| 785 | GRN_WAL_VALUE_RECORD_ID, |
| 786 | data->record_id, |
| 787 | |
| 788 | GRN_WAL_KEY_IS_SHARED, |
| 789 | GRN_WAL_VALUE_BOOLEAN, |
| 790 | is_shared, |
| 791 | |
| 792 | GRN_WAL_KEY_NEXT_GARBAGE_RECORD_ID, |
| 793 | GRN_WAL_VALUE_RECORD_ID, |
| 794 | data->next_garbage_record_id, |
| 795 | |
| 796 | GRN_WAL_KEY_DELETE_INFO_PHASE3_INDEX, |
| 797 | GRN_WAL_VALUE_UINT32, |
| 798 | data->delete_info_phase3_index, |
| 799 | |
| 800 | GRN_WAL_KEY_END); |
| 801 | } |
| 802 | |
| 803 | static grn_rc |
| 804 | grn_pat_wal_add_entry_delete_entry(grn_ctx *ctx, |
| 805 | grn_pat_wal_add_entry_data *data, |
| 806 | grn_pat_wal_add_entry_used *used) |
| 807 | { |
| 808 | used->record_id = true; |
| 809 | used->record_direction = true; |
| 810 | used->check = true; |
| 811 | used->parent_record_id = true; |
| 812 | used->parent_record_direction = true; |
| 813 | used->parent_check = true; |
| 814 | used->grandparent_record_id = true; |
| 815 | used->otherside_record_id = true; |
| 816 | used->otherside_check = true; |
| 817 | used->left_record_id = true; |
| 818 | used->right_record_id = true; |
| 819 | used->n_garbages = true; |
| 820 | used->n_entries = true; |
| 821 | used->delete_info_index = true; |
| 822 | used->delete_info_phase1_index = true; |
| 823 | used->delete_info_phase2_index = true; |
| 824 | uint32_t check = data->check; |
| 825 | uint32_t parent_check = data->parent_check; |
| 826 | uint32_t otherside_check = data->otherside_check; |
| 827 | return grn_wal_add_entry(ctx, |
| 828 | (grn_obj *)(data->pat), |
| 829 | false, |
| 830 | &(data->wal_id), |
| 831 | data->tag, |
| 832 | |
| 833 | GRN_WAL_KEY_EVENT, |
| 834 | GRN_WAL_VALUE_EVENT, |
| 835 | data->event, |
| 836 | |
| 837 | GRN_WAL_KEY_RECORD_ID, |
| 838 | GRN_WAL_VALUE_RECORD_ID, |
| 839 | data->record_id, |
| 840 | |
| 841 | GRN_WAL_KEY_RECORD_DIRECTION, |
| 842 | GRN_WAL_VALUE_UINT32, |
| 843 | data->record_direction, |
| 844 | |
| 845 | GRN_WAL_KEY_CHECK, |
| 846 | GRN_WAL_VALUE_UINT32, |
| 847 | check, |
| 848 | |
| 849 | GRN_WAL_KEY_PARENT_RECORD_ID, |
| 850 | GRN_WAL_VALUE_RECORD_ID, |
| 851 | data->parent_record_id, |
| 852 | |
| 853 | GRN_WAL_KEY_PARENT_CHECK, |
| 854 | GRN_WAL_VALUE_UINT32, |
| 855 | parent_check, |
| 856 | |
| 857 | GRN_WAL_KEY_PARENT_RECORD_DIRECTION, |
| 858 | GRN_WAL_VALUE_UINT32, |
| 859 | data->parent_record_direction, |
| 860 | |
| 861 | GRN_WAL_KEY_GRANDPARENT_RECORD_ID, |
| 862 | GRN_WAL_VALUE_RECORD_ID, |
| 863 | data->grandparent_record_id, |
| 864 | |
| 865 | GRN_WAL_KEY_OTHERSIDE_RECORD_ID, |
| 866 | GRN_WAL_VALUE_RECORD_ID, |
| 867 | data->otherside_record_id, |
| 868 | |
| 869 | GRN_WAL_KEY_OTHERSIDE_CHECK, |
| 870 | GRN_WAL_VALUE_UINT32, |
| 871 | otherside_check, |
| 872 | |
| 873 | GRN_WAL_KEY_LEFT_RECORD_ID, |
| 874 | GRN_WAL_VALUE_RECORD_ID, |
| 875 | data->left_record_id, |
| 876 | |
| 877 | GRN_WAL_KEY_RIGHT_RECORD_ID, |
| 878 | GRN_WAL_VALUE_RECORD_ID, |
| 879 | data->right_record_id, |
| 880 | |
| 881 | GRN_WAL_KEY_N_GARBAGES, |
| 882 | GRN_WAL_VALUE_UINT32, |
| 883 | data->n_garbages, |
| 884 | |
| 885 | GRN_WAL_KEY_N_ENTRIES, |
| 886 | GRN_WAL_VALUE_UINT32, |
| 887 | data->n_entries, |
| 888 | |
| 889 | GRN_WAL_KEY_DELETE_INFO_INDEX, |
| 890 | GRN_WAL_VALUE_UINT32, |
| 891 | data->delete_info_index, |
| 892 | |
| 893 | GRN_WAL_KEY_DELETE_INFO_PHASE1_INDEX, |
| 894 | GRN_WAL_VALUE_UINT32, |
| 895 | data->delete_info_phase1_index, |
| 896 | |
| 897 | GRN_WAL_KEY_DELETE_INFO_PHASE2_INDEX, |
| 898 | GRN_WAL_VALUE_UINT32, |
| 899 | data->delete_info_phase2_index, |
| 900 | |
| 901 | GRN_WAL_KEY_END); |
| 902 | } |
| 903 | |
| 904 | static grn_rc |
| 905 | grn_pat_wal_add_entry_defrag_key(grn_ctx *ctx, |
| 906 | grn_pat_wal_add_entry_data *data, |
| 907 | grn_pat_wal_add_entry_used *used) |
| 908 | { |
| 909 | used->record_id = true; |
| 910 | used->key = true; |
| 911 | used->key_size = true; |
| 912 | used->key_offset = true; |
| 913 | const void *key = data->key; |
| 914 | size_t key_size = data->key_size; |
| 915 | return grn_wal_add_entry(ctx, |
| 916 | (grn_obj *)(data->pat), |
| 917 | false, |
| 918 | &(data->wal_id), |
| 919 | data->tag, |
| 920 | |
| 921 | GRN_WAL_KEY_EVENT, |
| 922 | GRN_WAL_VALUE_EVENT, |
| 923 | data->event, |
| 924 | |
| 925 | GRN_WAL_KEY_RECORD_ID, |
| 926 | GRN_WAL_VALUE_RECORD_ID, |
| 927 | data->record_id, |
| 928 | |
| 929 | GRN_WAL_KEY_KEY, |
| 930 | GRN_WAL_VALUE_BINARY, |
| 931 | key, |
| 932 | key_size, |
| 933 | |
| 934 | GRN_WAL_KEY_KEY_OFFSET, |
| 935 | GRN_WAL_VALUE_UINT64, |
| 936 | data->key_offset, |
| 937 | |
| 938 | GRN_WAL_KEY_END); |
| 939 | } |
| 940 | |
| 941 | static grn_rc |
| 942 | grn_pat_wal_add_entry_defrag_current_key(grn_ctx *ctx, |
| 943 | grn_pat_wal_add_entry_data *data, |
| 944 | grn_pat_wal_add_entry_used *used) |
| 945 | { |
| 946 | // `record_id` is the max grn_id alive at the time of defragmentation. |
| 947 | used->record_id = true; |
| 948 | used->key_offset = true; |
| 949 | return grn_wal_add_entry(ctx, |
| 950 | (grn_obj *)(data->pat), |
| 951 | false, |
| 952 | &(data->wal_id), |
| 953 | data->tag, |
| 954 | |
| 955 | GRN_WAL_KEY_EVENT, |
| 956 | GRN_WAL_VALUE_EVENT, |
| 957 | data->event, |
| 958 | |
| 959 | GRN_WAL_KEY_RECORD_ID, |
| 960 | GRN_WAL_VALUE_RECORD_ID, |
| 961 | data->record_id, |
| 962 | |
| 963 | GRN_WAL_KEY_KEY_OFFSET, |
| 964 | GRN_WAL_VALUE_UINT64, |
| 965 | data->key_offset, |
| 966 | |
| 967 | GRN_WAL_KEY_END); |
| 968 | } |
| 969 | |
| 970 | static void |
| 971 | grn_pat_wal_add_entry_format_details(grn_ctx *ctx, |
| 972 | grn_pat_wal_add_entry_data *data, |
| 973 | grn_pat_wal_add_entry_used *used, |
| 974 | grn_obj *details) |
| 975 | { |
| 976 | grn_text_printf(ctx, |
| 977 | details, |
| 978 | "event:%u<%s> ", |
| 979 | data->event, |
| 980 | grn_wal_event_to_string(data->event)); |
| 981 | if (used->record_id) { |
| 982 | grn_text_printf(ctx, details, "record-id:%u ", data->record_id); |
| 983 | } |
| 984 | if (used->record_direction) { |
| 985 | grn_text_printf(ctx, |
| 986 | details, |
| 987 | "record-direction:%s ", |
| 988 | data->record_direction == DIRECTION_LEFT ? "left" |
| 989 | : "right"); |
| 990 | } |
| 991 | if (used->key_size) { |
| 992 | grn_text_printf(ctx, details, "key-size:%u ", data->key_size); |
| 993 | } |
| 994 | if (used->key_offset) { |
| 995 | grn_text_printf(ctx, |
| 996 | details, |
| 997 | "key-offset:%" GRN_FMT_INT64U " ", |
| 998 | data->key_offset); |
| 999 | } |
| 1000 | if (used->shared_key_offset) { |
| 1001 | grn_text_printf(ctx, |
| 1002 | details, |
| 1003 | "shared-key-offset:%u ", |
| 1004 | data->shared_key_offset); |
| 1005 | } |
| 1006 | if (used->is_shared) { |
| 1007 | grn_text_printf(ctx, |
| 1008 | details, |
| 1009 | "is-shared:%s ", |
| 1010 | data->is_shared ? "true" : "false"); |
| 1011 | } |
| 1012 | if (used->check) { |
| 1013 | uint16_t check = data->check; |
| 1014 | grn_text_printf(ctx, |
| 1015 | details, |
| 1016 | "check:<%u|%u|%s>(%u) ", |
| 1017 | PAT_CHECK_BYTE_DIFFERENCES(check), |
| 1018 | PAT_CHECK_BIT_DIFFERENCES(check), |
| 1019 | PAT_CHECK_IS_TERMINATED(check) ? "true" : "false", |
| 1020 | check); |
| 1021 | } |
| 1022 | if (used->parent_record_id) { |
| 1023 | grn_text_printf(ctx, |
| 1024 | details, |
| 1025 | "parent-record-id:%u ", |
| 1026 | data->parent_record_id); |
| 1027 | } |
| 1028 | if (used->parent_record_direction) { |
| 1029 | grn_text_printf(ctx, |
| 1030 | details, |
| 1031 | "parent-record-direction:%s ", |
| 1032 | data->parent_record_direction == DIRECTION_LEFT ? "left" |
| 1033 | : "right"); |
| 1034 | } |
| 1035 | if (used->grandparent_record_id) { |
| 1036 | grn_text_printf(ctx, |
| 1037 | details, |
| 1038 | "grandparent-record-id:%u ", |
| 1039 | data->grandparent_record_id); |
| 1040 | } |
| 1041 | if (used->parent_check) { |
| 1042 | uint16_t check = data->parent_check; |
| 1043 | grn_text_printf(ctx, |
| 1044 | details, |
| 1045 | "parent-check:<%u|%u|%s>(%u) ", |
| 1046 | PAT_CHECK_BYTE_DIFFERENCES(check), |
| 1047 | PAT_CHECK_BIT_DIFFERENCES(check), |
| 1048 | PAT_CHECK_IS_TERMINATED(check) ? "true" : "false", |
| 1049 | check); |
| 1050 | } |
| 1051 | if (used->otherside_record_id) { |
| 1052 | grn_text_printf(ctx, |
| 1053 | details, |
| 1054 | "otherside-record-id:%u ", |
| 1055 | data->otherside_record_id); |
| 1056 | } |
| 1057 | if (used->otherside_check) { |
| 1058 | uint16_t check = data->otherside_check; |
| 1059 | grn_text_printf(ctx, |
| 1060 | details, |
| 1061 | "otherside-check:<%u|%u|%s>(%u) ", |
| 1062 | PAT_CHECK_BYTE_DIFFERENCES(check), |
| 1063 | PAT_CHECK_BIT_DIFFERENCES(check), |
| 1064 | PAT_CHECK_IS_TERMINATED(check) ? "true" : "false", |
| 1065 | check); |
| 1066 | } |
| 1067 | if (used->left_record_id) { |
| 1068 | grn_text_printf(ctx, details, "left-record-id:%u ", data->left_record_id); |
| 1069 | } |
| 1070 | if (used->right_record_id) { |
| 1071 | grn_text_printf(ctx, details, "right-record-id:%u ", data->right_record_id); |
| 1072 | } |
| 1073 | if (used->next_garbage_record_id) { |
| 1074 | grn_text_printf(ctx, |
| 1075 | details, |
| 1076 | "next-garbage-record-id:%u ", |
| 1077 | data->next_garbage_record_id); |
| 1078 | } |
| 1079 | if (used->n_garbages) { |
| 1080 | grn_text_printf(ctx, details, "n-garbages:%u ", data->n_garbages); |
| 1081 | } |
| 1082 | if (used->n_entries) { |
| 1083 | grn_text_printf(ctx, details, "n-entries:%u ", data->n_entries); |
| 1084 | } |
| 1085 | if (used->delete_info_index) { |
| 1086 | grn_text_printf(ctx, |
| 1087 | details, |
| 1088 | "delete-info-index:%u ", |
| 1089 | data->delete_info_index); |
| 1090 | } |
| 1091 | if (used->delete_info_phase1_index) { |
| 1092 | grn_text_printf(ctx, |
| 1093 | details, |
| 1094 | "delete-info-phase1-index:%u ", |
| 1095 | data->delete_info_phase1_index); |
| 1096 | } |
| 1097 | if (used->delete_info_phase2_index) { |
| 1098 | grn_text_printf(ctx, |
| 1099 | details, |
| 1100 | "delete-info-phase2-index:%u ", |
| 1101 | data->delete_info_phase2_index); |
| 1102 | } |
| 1103 | if (used->delete_info_phase3_index) { |
| 1104 | grn_text_printf(ctx, |
| 1105 | details, |
| 1106 | "delete-info-phase3-index:%u ", |
| 1107 | data->delete_info_phase3_index); |
| 1108 | } |
| 1109 | if (used->previous_record_id) { |
| 1110 | grn_text_printf(ctx, |
| 1111 | details, |
| 1112 | "previous-record-id:%u ", |
| 1113 | data->previous_record_id); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | static inline grn_rc |
| 1118 | grn_pat_wal_add_entry(grn_ctx *ctx, grn_pat_wal_add_entry_data *data) |
| 1119 | { |
| 1120 | if (GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_NONE) { |
| 1121 | return GRN_SUCCESS; |
| 1122 | } |
| 1123 | |
| 1124 | if (data->pat->io->path[0] == '\0') { |
| 1125 | return GRN_SUCCESS; |
| 1126 | } |
| 1127 | |
| 1128 | grn_rc rc = GRN_SUCCESS; |
| 1129 | const char *usage = ""; |
| 1130 | grn_pat_wal_add_entry_used used = {0}; |
| 1131 | |
| 1132 | switch (data->event) { |
| 1133 | case GRN_WAL_EVENT_ADD_ENTRY: |
| 1134 | usage = "adding entry"; |
| 1135 | rc = grn_pat_wal_add_entry_add_entry(ctx, data, &used); |
| 1136 | break; |
| 1137 | case GRN_WAL_EVENT_ADD_SHARED_ENTRY: |
| 1138 | usage = "adding shared entry"; |
| 1139 | rc = grn_pat_wal_add_entry_add_shared_entry(ctx, data, &used); |
| 1140 | break; |
| 1141 | case GRN_WAL_EVENT_REUSE_ENTRY: |
| 1142 | usage = "reusing entry"; |
| 1143 | rc = grn_pat_wal_add_entry_reuse_entry(ctx, data, &used); |
| 1144 | break; |
| 1145 | case GRN_WAL_EVENT_REUSE_SHARED_ENTRY: |
| 1146 | usage = "reusing shared entry"; |
| 1147 | rc = grn_pat_wal_add_entry_reuse_shared_entry(ctx, data, &used); |
| 1148 | break; |
| 1149 | case GRN_WAL_EVENT_DELETE_INFO_PHASE1: |
| 1150 | usage = "deleting info phase1"; |
| 1151 | rc = grn_pat_wal_add_entry_delete_info_phase1(ctx, data, &used); |
| 1152 | break; |
| 1153 | case GRN_WAL_EVENT_DELETE_INFO_PHASE2: |
| 1154 | usage = "deleting info phase2"; |
| 1155 | rc = grn_pat_wal_add_entry_delete_info_phase2(ctx, data, &used); |
| 1156 | break; |
| 1157 | case GRN_WAL_EVENT_DELETE_INFO_PHASE3: |
| 1158 | usage = "deleting info phase3"; |
| 1159 | rc = grn_pat_wal_add_entry_delete_info_phase3(ctx, data, &used); |
| 1160 | break; |
| 1161 | case GRN_WAL_EVENT_DELETE_ENTRY: |
| 1162 | usage = "deleting entry"; |
| 1163 | rc = grn_pat_wal_add_entry_delete_entry(ctx, data, &used); |
| 1164 | break; |
| 1165 | case GRN_WAL_EVENT_DEFRAG_KEY: |
| 1166 | usage = "defrag key"; |
| 1167 | rc = grn_pat_wal_add_entry_defrag_key(ctx, data, &used); |
| 1168 | break; |
| 1169 | case GRN_WAL_EVENT_DEFRAG_CURRENT_KEY: |
| 1170 | usage = "defrag current key"; |
| 1171 | rc = grn_pat_wal_add_entry_defrag_current_key(ctx, data, &used); |
| 1172 | break; |
| 1173 | default: |
| 1174 | usage = "not implemented event"; |
| 1175 | rc = GRN_FUNCTION_NOT_IMPLEMENTED; |
| 1176 | break; |
| 1177 | } |
| 1178 | |
| 1179 | if (rc != GRN_SUCCESS) { |
| 1180 | grn_obj details; |
| 1181 | GRN_TEXT_INIT(&details, 0); |
| 1182 | grn_pat_wal_add_entry_format_details(ctx, data, &used, &details); |
| 1183 | grn_obj_set_error(ctx, |
| 1184 | (grn_obj *)(data->pat), |
| 1185 | rc, |
| 1186 | data->record_id, |
| 1187 | data->tag, |
| 1188 | "failed to add WAL entry for %s: %.*s", |
| 1189 | usage, |
| 1190 | (int)GRN_TEXT_LEN(&details), |
| 1191 | GRN_TEXT_VALUE(&details)); |
| 1192 | GRN_OBJ_FIN(ctx, &details); |
| 1193 | } |
| 1194 | |
| 1195 | return rc; |
| 1196 | } |
| 1197 | |
| 1198 | static inline void |
| 1199 | grn_pat_wal_add_entry_data_set_record_direction( |
| 1200 | grn_ctx *ctx, |
| 1201 | grn_pat *pat, |
| 1202 | grn_pat_wal_add_entry_data *data, |
| 1203 | grn_id parent_record_id, |
| 1204 | pat_node_common *parent_node, |
| 1205 | grn_id *id_location) |
| 1206 | { |
| 1207 | data->grandparent_record_id = data->parent_record_id; |
| 1208 | data->parent_record_direction = data->record_direction; |
| 1209 | data->parent_record_id = parent_record_id; |
| 1210 | if (id_location == pat_node_get_left_address(pat, parent_node)) { |
| 1211 | data->record_direction = DIRECTION_LEFT; |
| 1212 | } else { |
| 1213 | data->record_direction = DIRECTION_RIGHT; |
| 1214 | } |
| 1215 | data->previous_record_id = *id_location; |
| 1216 | } |
| 1217 | |
| 1218 | /* bit operation */ |
| 1219 | |
| 1220 | #define nth_bit(key, check) \ |
| 1221 | ((((key)[PAT_CHECK_BYTE_DIFFERENCES((check))]) >> \ |
| 1222 | (0b111 - PAT_CHECK_BIT_DIFFERENCES((check)))) & \ |
| 1223 | 1) |
| 1224 | |
| 1225 | /* segment operation */ |
| 1226 | |
| 1227 | /* patricia array operation */ |
| 1228 | |
| 1229 | #define PAT_AT(pat, id, n) \ |
| 1230 | do { \ |
| 1231 | int flags = 0; \ |
| 1232 | n = grn_io_array_at(ctx, pat->io, SEGMENT_PAT, id, &flags); \ |
| 1233 | } while (0) |
| 1234 | |
| 1235 | static inline pat_node_common * |
| 1236 | pat_node_get(grn_ctx *ctx, grn_pat *pat, grn_id id) |
| 1237 | { |
| 1238 | if (id > GRN_ID_MAX) { |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | int flags = GRN_TABLE_ADD; |
| 1242 | return grn_io_array_at(ctx, pat->io, SEGMENT_PAT, id, &flags); |
| 1243 | } |
| 1244 | |
| 1245 | /* sis operation */ |
| 1246 | |
| 1247 | static inline sis_node * |
| 1248 | sis_at(grn_ctx *ctx, grn_pat *pat, grn_id id) |
| 1249 | { |
| 1250 | int flags = 0; |
| 1251 | if (id > GRN_ID_MAX) { |
| 1252 | return NULL; |
| 1253 | } |
| 1254 | return grn_io_array_at(ctx, pat->io, SEGMENT_SIS, id, &flags); |
| 1255 | } |
| 1256 | |
| 1257 | static inline sis_node * |
| 1258 | sis_get(grn_ctx *ctx, grn_pat *pat, grn_id id) |
| 1259 | { |
| 1260 | int flags = GRN_TABLE_ADD; |
| 1261 | if (id > GRN_ID_MAX) { |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | return grn_io_array_at(ctx, pat->io, SEGMENT_SIS, id, &flags); |
| 1265 | } |
| 1266 | |
| 1267 | #define MAX_LEVEL 16 |
| 1268 | |
| 1269 | static void |
| 1270 | sis_collect(grn_ctx *ctx, grn_pat *pat, grn_hash *h, grn_id id, uint32_t level) |
| 1271 | { |
| 1272 | uint32_t *offset; |
| 1273 | sis_node *sl = sis_at(ctx, pat, id); |
| 1274 | if (sl) { |
| 1275 | grn_id sid = sl->children; |
| 1276 | while (sid && sid != id) { |
| 1277 | if (grn_hash_add(ctx, h, &sid, sizeof(grn_id), (void **)&offset, NULL)) { |
| 1278 | *offset = level; |
| 1279 | if (level < MAX_LEVEL) { |
| 1280 | sis_collect(ctx, pat, h, sid, level + 1); |
| 1281 | } |
| 1282 | if (!(sl = sis_at(ctx, pat, sid))) { |
| 1283 | break; |
| 1284 | } |
| 1285 | sid = sl->sibling; |
| 1286 | } else { |
| 1287 | /* todo : must be handled */ |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | /* key operation */ |
| 1294 | |
| 1295 | #define KEY_AT(pat, pos, ptr, addp) \ |
| 1296 | do { \ |
| 1297 | int flags = addp; \ |
| 1298 | ptr = grn_io_array_at(ctx, pat->io, SEGMENT_KEY, pos, &flags); \ |
| 1299 | } while (0) |
| 1300 | |
| 1301 | static inline void |
| 1302 | pat_update_total_key_size(grn_ctx *ctx, |
| 1303 | grn_pat *pat, |
| 1304 | uint64_t new_total_key_size) |
| 1305 | { |
| 1306 | if (pat_is_key_large(pat)) { |
| 1307 | pat->header->curr_key_large = new_total_key_size; |
| 1308 | } else { |
| 1309 | pat->header->curr_key = new_total_key_size; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | static inline uint64_t |
| 1314 | key_put(grn_ctx *ctx, grn_pat *pat, const uint8_t *key, uint32_t len) |
| 1315 | { |
| 1316 | // if (len >= GRN_PAT_SEGMENT_SIZE) { return 0; /* error */ } |
| 1317 | uint64_t res = grn_pat_total_key_size(ctx, pat); |
| 1318 | if (res < grn_pat_max_total_key_size(ctx, pat) && |
| 1319 | len > grn_pat_max_total_key_size(ctx, pat) - res) { |
| 1320 | GRN_DEFINE_NAME(pat); |
| 1321 | ERR(GRN_NOT_ENOUGH_SPACE, |
| 1322 | "[pat][key][put] total key size is over: <%.*s>: " |
| 1323 | "max=%" GRN_FMT_INT64U ": current=%" GRN_FMT_INT64U ": new key size=%u", |
| 1324 | name_size, |
| 1325 | name, |
| 1326 | grn_pat_max_total_key_size(ctx, pat), |
| 1327 | res, |
| 1328 | len); |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* Check if the key to be added can fit in the current segment. */ |
| 1333 | uint64_t end_segment = (res + len - 1) >> W_OF_KEY_IN_A_SEGMENT; |
| 1334 | if (res >> W_OF_KEY_IN_A_SEGMENT != end_segment) { |
| 1335 | /* If it does not fit, update `curr_key`(`res`) to add it to the next |
| 1336 | * segment. */ |
| 1337 | res = end_segment << W_OF_KEY_IN_A_SEGMENT; |
| 1338 | pat_update_total_key_size(ctx, pat, res); |
| 1339 | } |
| 1340 | { |
| 1341 | uint8_t *dest; |
| 1342 | KEY_AT(pat, res, dest, GRN_TABLE_ADD); |
| 1343 | if (!dest) { |
| 1344 | GRN_DEFINE_NAME(pat); |
| 1345 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 1346 | "[pat][key][put] failed to allocate memory for new key: <%.*s>: " |
| 1347 | "new offset:%" GRN_FMT_INT64U " key size:%u", |
| 1348 | name_size, |
| 1349 | name, |
| 1350 | res, |
| 1351 | len); |
| 1352 | return 0; |
| 1353 | } |
| 1354 | grn_memcpy(dest, key, len); |
| 1355 | } |
| 1356 | pat_update_total_key_size(ctx, pat, grn_pat_total_key_size(ctx, pat) + len); |
| 1357 | return res; |
| 1358 | } |
| 1359 | |
| 1360 | static inline void |
| 1361 | pat_node_set_key_position(grn_pat *pat, |
| 1362 | pat_node_common *node, |
| 1363 | uint64_t position) |
| 1364 | { |
| 1365 | if (pat_is_key_large(pat)) { |
| 1366 | node->node_large.key = position; |
| 1367 | } else { |
| 1368 | node->node.key = position; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * We must not call this function when pat_node_is_key_immediate() is true. |
| 1374 | * If pat_node_is_key_immediate() is true, this function does not return the |
| 1375 | * key offset but the value of the key directly. |
| 1376 | */ |
| 1377 | static inline uint64_t |
| 1378 | pat_node_get_key_offset(grn_pat *pat, pat_node_common *node) |
| 1379 | { |
| 1380 | if (pat_is_key_large(pat)) { |
| 1381 | return node->node_large.key; |
| 1382 | } else { |
| 1383 | return node->node.key; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | static inline void |
| 1388 | pat_node_set_key_offset(grn_pat *pat, |
| 1389 | pat_node_common *node, |
| 1390 | uint64_t key_offset) |
| 1391 | { |
| 1392 | if (pat_is_key_large(pat)) { |
| 1393 | node->node_large.key = key_offset; |
| 1394 | } else { |
| 1395 | node->node.key = (uint32_t)key_offset; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | static inline uint64_t * |
| 1400 | pat_node_get_key_address(grn_pat *pat, pat_node_common *node) |
| 1401 | { |
| 1402 | if (pat_is_key_large(pat)) { |
| 1403 | return &(node->node_large.key); |
| 1404 | } else { |
| 1405 | return (uint64_t *)&(node->node.key); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | static inline uint8_t * |
| 1410 | pat_node_get_key(grn_ctx *ctx, grn_pat *pat, pat_node_common *node) |
| 1411 | { |
| 1412 | if (pat_node_is_key_immediate(pat, node)) { |
| 1413 | if (pat_is_key_large(pat)) { |
| 1414 | return (uint8_t *)&(node->node_large.key); |
| 1415 | } else { |
| 1416 | return (uint8_t *)&(node->node.key); |
| 1417 | } |
| 1418 | } else { |
| 1419 | uint8_t *res; |
| 1420 | if (pat_is_key_large(pat)) { |
| 1421 | KEY_AT(pat, node->node_large.key, res, 0); |
| 1422 | } else { |
| 1423 | KEY_AT(pat, node->node.key, res, 0); |
| 1424 | } |
| 1425 | return res; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | static inline grn_rc |
| 1430 | pat_node_set_key(grn_ctx *ctx, |
| 1431 | grn_pat *pat, |
| 1432 | pat_node_common *node, |
| 1433 | const uint8_t *key, |
| 1434 | uint32_t len) |
| 1435 | { |
| 1436 | if (!key || !len) { |
| 1437 | return GRN_INVALID_ARGUMENT; |
| 1438 | } |
| 1439 | pat_node_set_key_length(pat, node, len); |
| 1440 | if (pat_key_is_embeddable(pat, len)) { |
| 1441 | pat_node_set_key_immediate_on(pat, node); |
| 1442 | grn_memcpy(pat_node_get_key_address(pat, node), key, len); |
| 1443 | return GRN_SUCCESS; |
| 1444 | } else { |
| 1445 | pat_node_set_key_immediate_off(pat, node); |
| 1446 | pat_node_set_key_offset(pat, node, key_put(ctx, pat, key, len)); |
| 1447 | return ctx->rc; |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | static inline void |
| 1452 | pat_node_set_shared_key(grn_ctx *ctx, |
| 1453 | grn_pat *pat, |
| 1454 | pat_node_common *node, |
| 1455 | uint32_t key_size, |
| 1456 | uint32_t shared_key_offset) |
| 1457 | { |
| 1458 | pat_node_set_key_immediate_off(pat, node); |
| 1459 | pat_node_set_key_length(pat, node, key_size); |
| 1460 | pat_node_set_key_offset(pat, node, shared_key_offset); |
| 1461 | } |
| 1462 | |
| 1463 | /* delinfo operation */ |
| 1464 | |
| 1465 | enum { |
| 1466 | /* The delinfo is currently not used. */ |
| 1467 | DL_EMPTY = 0, |
| 1468 | /* |
| 1469 | * stat->d refers to a deleting node (in a tree). |
| 1470 | * The deletion requires an additional operation. |
| 1471 | */ |
| 1472 | DL_PHASE1, |
| 1473 | /* |
| 1474 | * stat->d refers to a deleted node (not in a tree). |
| 1475 | * The node is pending for safety. |
| 1476 | */ |
| 1477 | DL_PHASE2 |
| 1478 | }; |
| 1479 | |
| 1480 | static inline grn_id * |
| 1481 | grn_pat_next_location(grn_ctx *ctx, |
| 1482 | grn_pat *pat, |
| 1483 | pat_node_common *node, |
| 1484 | const uint8_t *key, |
| 1485 | uint16_t check, |
| 1486 | int32_t check_max) |
| 1487 | { |
| 1488 | if (PAT_CHECK_IS_TERMINATED(check)) { |
| 1489 | /* check + 1: delete terminated flag and increment bit differences */ |
| 1490 | if (check + 1 < check_max) { |
| 1491 | return pat_node_get_right_address(pat, node); |
| 1492 | } else { |
| 1493 | return pat_node_get_left_address(pat, node); |
| 1494 | } |
| 1495 | } else { |
| 1496 | return pat_node_get_child_address(pat, node, nth_bit(key, check)); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | static inline grn_pat_delinfo * |
| 1501 | delinfo_search(grn_pat *pat, grn_id id, uint32_t *index) |
| 1502 | { |
| 1503 | uint32_t i; |
| 1504 | grn_pat_delinfo *di; |
| 1505 | for (i = (pat->header->curr_del2) & GRN_PAT_MDELINFOS; |
| 1506 | i != pat->header->curr_del; |
| 1507 | i = (i + 1) & GRN_PAT_MDELINFOS) { |
| 1508 | di = &pat->header->delinfos[i]; |
| 1509 | if (di->stat != DL_PHASE1) { |
| 1510 | continue; |
| 1511 | } |
| 1512 | if (di->ld == id || di->d == id) { |
| 1513 | if (index) { |
| 1514 | *index = i; |
| 1515 | } |
| 1516 | return di; |
| 1517 | } |
| 1518 | } |
| 1519 | return NULL; |
| 1520 | } |
| 1521 | |
| 1522 | static inline uint32_t |
| 1523 | delinfo_turn_1_pre(grn_ctx *ctx, grn_pat *pat, grn_pat_delinfo **di) |
| 1524 | { |
| 1525 | *di = &pat->header->delinfos[pat->header->curr_del]; |
| 1526 | return (pat->header->curr_del + 1) & GRN_PAT_MDELINFOS; |
| 1527 | } |
| 1528 | |
| 1529 | static inline void |
| 1530 | delinfo_turn_1_post(grn_ctx *ctx, grn_pat *pat, uint32_t next_delete_info_index) |
| 1531 | { |
| 1532 | pat->header->curr_del = next_delete_info_index; |
| 1533 | } |
| 1534 | |
| 1535 | static inline grn_rc |
| 1536 | delinfo_turn_2_internal(grn_ctx *ctx, |
| 1537 | grn_pat *pat, |
| 1538 | grn_pat_delinfo *di, |
| 1539 | pat_node_common *ln, |
| 1540 | pat_node_common *dn) |
| 1541 | { |
| 1542 | grn_id d = di->d; |
| 1543 | grn_id *p = NULL; |
| 1544 | pat_node_set_deleting_off(pat, ln); |
| 1545 | pat_node_set_deleting_off(pat, dn); |
| 1546 | { |
| 1547 | grn_id *p0; |
| 1548 | pat_node_common *rn; |
| 1549 | int32_t c0 = -1, c; |
| 1550 | int32_t check_max = |
| 1551 | PAT_CHECK_PACK(pat_node_get_key_length(pat, dn), 0, false); |
| 1552 | const uint8_t *key = pat_node_get_key(ctx, pat, dn); |
| 1553 | if (!key) { |
| 1554 | return GRN_INVALID_ARGUMENT; |
| 1555 | } |
| 1556 | PAT_AT(pat, 0, rn); |
| 1557 | p0 = pat_node_get_right_address(pat, rn); |
| 1558 | for (;;) { |
| 1559 | grn_id r = *p0; |
| 1560 | if (!r) { |
| 1561 | break; |
| 1562 | } |
| 1563 | if (r == d) { |
| 1564 | p = p0; |
| 1565 | break; |
| 1566 | } |
| 1567 | PAT_AT(pat, r, rn); |
| 1568 | if (!rn) { |
| 1569 | return GRN_FILE_CORRUPT; |
| 1570 | } |
| 1571 | c = pat_node_get_check(pat, rn); |
| 1572 | if (c <= c0 || check_max <= c) { |
| 1573 | break; |
| 1574 | } |
| 1575 | p0 = grn_pat_next_location(ctx, pat, rn, key, c, check_max); |
| 1576 | c0 = c; |
| 1577 | } |
| 1578 | } |
| 1579 | if (p) { |
| 1580 | pat_node_set_check(pat, ln, pat_node_get_check(pat, dn)); |
| 1581 | pat_node_set_right(pat, ln, pat_node_get_right(pat, dn)); |
| 1582 | pat_node_set_left(pat, ln, pat_node_get_left(pat, dn)); |
| 1583 | *p = di->ld; |
| 1584 | } else { |
| 1585 | /* debug */ |
| 1586 | uint32_t j; |
| 1587 | grn_id dd; |
| 1588 | grn_pat_delinfo *ddi; |
| 1589 | GRN_LOG(ctx, GRN_LOG_DEBUG, "failed to find d=%d", d); |
| 1590 | for (j = (pat->header->curr_del2 + 1) & GRN_PAT_MDELINFOS; |
| 1591 | j != pat->header->curr_del; |
| 1592 | j = (j + 1) & GRN_PAT_MDELINFOS) { |
| 1593 | ddi = &pat->header->delinfos[j]; |
| 1594 | if (ddi->stat != DL_PHASE1) { |
| 1595 | continue; |
| 1596 | } |
| 1597 | PAT_AT(pat, ddi->ld, ln); |
| 1598 | if (!ln) { |
| 1599 | continue; |
| 1600 | } |
| 1601 | if (!(dd = ddi->d)) { |
| 1602 | continue; |
| 1603 | } |
| 1604 | if (d == ddi->ld) { |
| 1605 | GRN_LOG(ctx, GRN_LOG_DEBUG, "found!!!, d(%d) become ld of (%d)", d, dd); |
| 1606 | } |
| 1607 | } |
| 1608 | /* debug */ |
| 1609 | } |
| 1610 | di->stat = DL_PHASE2; |
| 1611 | di->d = d; |
| 1612 | // grn_log("delinfo_turn_2< di->d=%d di->ld=%d", di->d, di->ld); |
| 1613 | return GRN_SUCCESS; |
| 1614 | } |
| 1615 | |
| 1616 | static inline grn_rc |
| 1617 | delinfo_turn_2(grn_ctx *ctx, |
| 1618 | grn_pat *pat, |
| 1619 | grn_pat_wal_add_entry_data *base_wal_data, |
| 1620 | uint32_t di_index) |
| 1621 | { |
| 1622 | grn_pat_delinfo *di = &pat->header->delinfos[di_index]; |
| 1623 | pat_node_common *ln, *dn; |
| 1624 | // grn_log("delinfo_turn_2> di->d=%d di->ld=%d stat=%d", di->d, di->ld, |
| 1625 | // di->stat); |
| 1626 | if (di->stat != DL_PHASE1) { |
| 1627 | return GRN_SUCCESS; |
| 1628 | } |
| 1629 | PAT_AT(pat, di->ld, ln); |
| 1630 | if (!ln) { |
| 1631 | return GRN_INVALID_ARGUMENT; |
| 1632 | } |
| 1633 | grn_id d = di->d; |
| 1634 | if (!d) { |
| 1635 | return GRN_INVALID_ARGUMENT; |
| 1636 | } |
| 1637 | PAT_AT(pat, d, dn); |
| 1638 | if (!dn) { |
| 1639 | return GRN_INVALID_ARGUMENT; |
| 1640 | } |
| 1641 | grn_pat_wal_add_entry_data wal_data = *base_wal_data; |
| 1642 | wal_data.event = GRN_WAL_EVENT_DELETE_INFO_PHASE2; |
| 1643 | wal_data.record_id = d; |
| 1644 | wal_data.key_size = pat_node_get_key_length(pat, dn); |
| 1645 | wal_data.check = pat_node_get_check(pat, dn); |
| 1646 | wal_data.parent_record_id = di->ld; |
| 1647 | wal_data.left_record_id = pat_node_get_left(pat, dn); |
| 1648 | wal_data.right_record_id = pat_node_get_right(pat, dn); |
| 1649 | wal_data.delete_info_phase2_index = di_index; |
| 1650 | if (grn_pat_wal_add_entry(ctx, &wal_data) != GRN_SUCCESS) { |
| 1651 | return ctx->rc; |
| 1652 | } |
| 1653 | grn_rc rc = delinfo_turn_2_internal(ctx, pat, di, ln, dn); |
| 1654 | if (rc == GRN_SUCCESS) { |
| 1655 | pat->header->wal_id = wal_data.wal_id; |
| 1656 | } |
| 1657 | return rc; |
| 1658 | } |
| 1659 | |
| 1660 | static inline void |
| 1661 | delinfo_turn_2_post(grn_ctx *ctx, grn_pat *pat) |
| 1662 | { |
| 1663 | pat->header->curr_del2 = (pat->header->curr_del2 + 1) & GRN_PAT_MDELINFOS; |
| 1664 | } |
| 1665 | |
| 1666 | static inline uint32_t |
| 1667 | delinfo_compute_storage_size(grn_ctx *ctx, |
| 1668 | grn_pat *pat, |
| 1669 | grn_pat_delinfo *di, |
| 1670 | pat_node_common *dn) |
| 1671 | { |
| 1672 | if (di->shared) { |
| 1673 | return 0; |
| 1674 | } else { |
| 1675 | if (pat_node_is_key_immediate(pat, dn)) { |
| 1676 | return 0; |
| 1677 | } else { |
| 1678 | return pat_node_get_key_length(pat, dn); |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | static inline grn_rc |
| 1684 | delinfo_turn_3_internal(grn_ctx *ctx, |
| 1685 | grn_pat *pat, |
| 1686 | grn_pat_delinfo *di, |
| 1687 | pat_node_common *dn) |
| 1688 | { |
| 1689 | if (di->shared) { |
| 1690 | pat_node_set_key_immediate_on(pat, dn); |
| 1691 | } |
| 1692 | di->stat = DL_EMPTY; |
| 1693 | // dn->lr[1] = GRN_PAT_DELETED; |
| 1694 | uint32_t size = delinfo_compute_storage_size(ctx, pat, di, dn); |
| 1695 | pat_node_set_left(pat, dn, pat->header->garbages[size]); |
| 1696 | pat->header->garbages[size] = di->d; |
| 1697 | return GRN_SUCCESS; |
| 1698 | } |
| 1699 | |
| 1700 | static inline grn_rc |
| 1701 | delinfo_turn_3(grn_ctx *ctx, |
| 1702 | grn_pat *pat, |
| 1703 | grn_pat_wal_add_entry_data *base_wal_data, |
| 1704 | uint32_t di_index) |
| 1705 | { |
| 1706 | grn_pat_delinfo *di = &pat->header->delinfos[di_index]; |
| 1707 | pat_node_common *dn; |
| 1708 | if (di->stat != DL_PHASE2) { |
| 1709 | return GRN_SUCCESS; |
| 1710 | } |
| 1711 | PAT_AT(pat, di->d, dn); |
| 1712 | if (!dn) { |
| 1713 | return GRN_INVALID_ARGUMENT; |
| 1714 | } |
| 1715 | uint32_t size = delinfo_compute_storage_size(ctx, pat, di, dn); |
| 1716 | grn_pat_wal_add_entry_data wal_data = *base_wal_data; |
| 1717 | wal_data.event = GRN_WAL_EVENT_DELETE_INFO_PHASE3; |
| 1718 | wal_data.record_id = di->d; |
| 1719 | wal_data.is_shared = di->shared; |
| 1720 | wal_data.next_garbage_record_id = pat->header->garbages[size]; |
| 1721 | wal_data.delete_info_phase3_index = di_index; |
| 1722 | if (grn_pat_wal_add_entry(ctx, &wal_data) != GRN_SUCCESS) { |
| 1723 | return ctx->rc; |
| 1724 | } |
| 1725 | grn_rc rc = delinfo_turn_3_internal(ctx, pat, di, dn); |
| 1726 | if (rc == GRN_SUCCESS) { |
| 1727 | pat->header->wal_id = wal_data.wal_id; |
| 1728 | } |
| 1729 | return rc; |
| 1730 | } |
| 1731 | |
| 1732 | static inline void |
| 1733 | delinfo_turn_3_post(grn_ctx *ctx, grn_pat *pat) |
| 1734 | { |
| 1735 | pat->header->curr_del3 = (pat->header->curr_del3 + 1) & GRN_PAT_MDELINFOS; |
| 1736 | } |
| 1737 | |
| 1738 | static inline grn_pat_delinfo * |
| 1739 | delinfo_new(grn_ctx *ctx, |
| 1740 | grn_pat *pat, |
| 1741 | grn_pat_wal_add_entry_data *base_wal_data) |
| 1742 | { |
| 1743 | base_wal_data->delete_info_index = pat->header->curr_del; |
| 1744 | grn_pat_wal_add_entry_data wal_data = *base_wal_data; |
| 1745 | wal_data.event = GRN_WAL_EVENT_DELETE_INFO_PHASE1; |
| 1746 | wal_data.delete_info_phase1_index = pat->header->curr_del; |
| 1747 | if (grn_pat_wal_add_entry(ctx, &wal_data) != GRN_SUCCESS) { |
| 1748 | return NULL; |
| 1749 | } |
| 1750 | grn_pat_delinfo *di; |
| 1751 | uint32_t next_delete_info_index = delinfo_turn_1_pre(ctx, pat, &di); |
| 1752 | di->shared = wal_data.is_shared; |
| 1753 | int gap = |
| 1754 | ((next_delete_info_index + GRN_PAT_NDELINFOS - pat->header->curr_del2) & |
| 1755 | GRN_PAT_MDELINFOS) - |
| 1756 | (GRN_PAT_NDELINFOS / 2); |
| 1757 | while (gap-- > 0) { |
| 1758 | if (delinfo_turn_2(ctx, pat, base_wal_data, pat->header->curr_del2)) { |
| 1759 | GRN_LOG(ctx, |
| 1760 | GRN_LOG_CRIT, |
| 1761 | "d2 failed: %d", |
| 1762 | pat->header->delinfos[pat->header->curr_del2].ld); |
| 1763 | } |
| 1764 | delinfo_turn_2_post(ctx, pat); |
| 1765 | } |
| 1766 | if (next_delete_info_index == pat->header->curr_del3) { |
| 1767 | if (delinfo_turn_3(ctx, pat, base_wal_data, pat->header->curr_del3)) { |
| 1768 | GRN_LOG(ctx, |
| 1769 | GRN_LOG_CRIT, |
| 1770 | "d3 failed: %d", |
| 1771 | pat->header->delinfos[pat->header->curr_del3].ld); |
| 1772 | } |
| 1773 | delinfo_turn_3_post(ctx, pat); |
| 1774 | } |
| 1775 | delinfo_turn_1_post(ctx, pat, next_delete_info_index); |
| 1776 | /* We can't do this because this overwrites WAL ID for phase2 and phase3. */ |
| 1777 | /* pat->header->wal_id = wal_data.wal_id; */ |
| 1778 | return di; |
| 1779 | } |
| 1780 | |
| 1781 | /* pat operation */ |
| 1782 | |
| 1783 | static inline uint32_t |
| 1784 | compute_needed_bits(uint32_t size_in_byte) |
| 1785 | { |
| 1786 | uint32_t needed_bits; |
| 1787 | for (needed_bits = 0; (size_t)(1 << needed_bits) < size_in_byte; |
| 1788 | needed_bits++) { |
| 1789 | /* nop */ |
| 1790 | } |
| 1791 | return needed_bits; |
| 1792 | } |
| 1793 | |
| 1794 | static inline grn_pat * |
| 1795 | _grn_pat_create(grn_ctx *ctx, |
| 1796 | grn_pat *pat, |
| 1797 | const char *path, |
| 1798 | uint32_t key_size, |
| 1799 | uint32_t value_size, |
| 1800 | uint32_t flags) |
| 1801 | { |
| 1802 | grn_io *io; |
| 1803 | pat_node_common *node0; |
| 1804 | struct grn_pat_header *header; |
| 1805 | grn_encoding encoding = ctx->encoding; |
| 1806 | { |
| 1807 | grn_io_array_spec array_spec[3]; |
| 1808 | uint32_t pat_entry_size, needed_bits_of_pat_entry; |
| 1809 | array_spec[SEGMENT_KEY].w_of_element = 0; |
| 1810 | if (flags & GRN_OBJ_KEY_LARGE) { |
| 1811 | array_spec[SEGMENT_KEY].max_n_segments = GRN_PAT_MAX_N_SEGMENTS_LARGE; |
| 1812 | pat_entry_size = sizeof(pat_node_large); |
| 1813 | } else { |
| 1814 | array_spec[SEGMENT_KEY].max_n_segments = GRN_PAT_MAX_N_SEGMENTS; |
| 1815 | pat_entry_size = sizeof(pat_node); |
| 1816 | } |
| 1817 | needed_bits_of_pat_entry = compute_needed_bits(pat_entry_size); |
| 1818 | array_spec[SEGMENT_PAT].w_of_element = needed_bits_of_pat_entry; |
| 1819 | array_spec[SEGMENT_PAT].max_n_segments = |
| 1820 | 1 << (GRN_ID_MAX_IN_BIT - |
| 1821 | (GRN_PAT_SEGMENT_SIZE_IN_BIT - needed_bits_of_pat_entry)); |
| 1822 | uint32_t sis_entry_size; |
| 1823 | if (flags & GRN_OBJ_KEY_WITH_SIS) { |
| 1824 | sis_entry_size = sizeof(sis_node) + value_size; |
| 1825 | } else { |
| 1826 | sis_entry_size = value_size; |
| 1827 | } |
| 1828 | uint32_t needed_bits_of_sis_entry = compute_needed_bits(sis_entry_size); |
| 1829 | array_spec[SEGMENT_SIS].w_of_element = needed_bits_of_sis_entry; |
| 1830 | array_spec[SEGMENT_SIS].max_n_segments = |
| 1831 | 1 << (GRN_ID_MAX_IN_BIT - |
| 1832 | (GRN_PAT_SEGMENT_SIZE_IN_BIT - needed_bits_of_sis_entry)); |
| 1833 | io = grn_io_create_with_array(ctx, |
| 1834 | path, |
| 1835 | sizeof(struct grn_pat_header), |
| 1836 | GRN_PAT_SEGMENT_SIZE, |
| 1837 | GRN_IO_AUTO, |
| 1838 | 3, |
| 1839 | array_spec); |
| 1840 | } |
| 1841 | if (!io) { |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | if (encoding == GRN_ENC_DEFAULT) { |
| 1845 | encoding = grn_gctx.encoding; |
| 1846 | } |
| 1847 | header = grn_io_header(io); |
| 1848 | grn_io_set_type(io, GRN_TABLE_PAT_KEY); |
| 1849 | header->flags = flags; |
| 1850 | header->encoding = encoding; |
| 1851 | header->key_size = key_size; |
| 1852 | header->value_size = value_size; |
| 1853 | header->n_entries = 0; |
| 1854 | header->curr_rec = 0; |
| 1855 | header->curr_key = 0; |
| 1856 | header->curr_key_large = 0; |
| 1857 | header->curr_del = 0; |
| 1858 | header->curr_del2 = 0; |
| 1859 | header->curr_del3 = 0; |
| 1860 | header->n_garbages = 0; |
| 1861 | header->tokenizer = GRN_ID_NIL; |
| 1862 | grn_table_modules_init(ctx, &(pat->normalizers)); |
| 1863 | if (header->flags & GRN_OBJ_KEY_NORMALIZE) { |
| 1864 | header->flags &= ~GRN_OBJ_KEY_NORMALIZE; |
| 1865 | header->normalizer = GRN_ID_NIL; |
| 1866 | grn_obj *normalizer = grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1); |
| 1867 | grn_table_modules_add(ctx, &(pat->normalizers), normalizer); |
| 1868 | } else { |
| 1869 | header->normalizer = GRN_ID_NIL; |
| 1870 | } |
| 1871 | header->truncated = false; |
| 1872 | grn_table_modules_init(ctx, &(pat->token_filters)); |
| 1873 | GRN_PTR_INIT(&(pat->token_filter_procs), GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 1874 | grn_table_modules_init(ctx, &(pat->extractors)); |
| 1875 | pat->io = io; |
| 1876 | pat->header = header; |
| 1877 | pat->key_size = key_size; |
| 1878 | pat->value_size = value_size; |
| 1879 | grn_table_module_init(ctx, &(pat->tokenizer), GRN_ID_NIL); |
| 1880 | pat->encoding = encoding; |
| 1881 | pat->obj.header.flags = header->flags; |
| 1882 | if (!(node0 = pat_node_get(ctx, pat, 0))) { |
| 1883 | grn_io_close(ctx, io); |
| 1884 | return NULL; |
| 1885 | } |
| 1886 | pat_node_set_right(pat, node0, GRN_ID_NIL); |
| 1887 | pat_node_set_left(pat, node0, GRN_ID_NIL); |
| 1888 | pat_node_set_key_offset(pat, node0, 0); |
| 1889 | return pat; |
| 1890 | } |
| 1891 | |
| 1892 | grn_pat * |
| 1893 | grn_pat_create(grn_ctx *ctx, |
| 1894 | const char *path, |
| 1895 | uint32_t key_size, |
| 1896 | uint32_t value_size, |
| 1897 | uint32_t flags) |
| 1898 | { |
| 1899 | grn_pat *pat; |
| 1900 | if (!(pat = GRN_CALLOC(sizeof(grn_pat)))) { |
| 1901 | return NULL; |
| 1902 | } |
| 1903 | GRN_DB_OBJ_SET_TYPE(pat, GRN_TABLE_PAT_KEY); |
| 1904 | if (!_grn_pat_create(ctx, pat, path, key_size, value_size, flags)) { |
| 1905 | GRN_FREE(pat); |
| 1906 | return NULL; |
| 1907 | } |
| 1908 | pat->cache = NULL; |
| 1909 | pat->cache_size = 0; |
| 1910 | pat->is_dirty = false; |
| 1911 | CRITICAL_SECTION_INIT(pat->lock); |
| 1912 | return pat; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | grn_pat_cache_enable() and grn_pat_cache_disable() are not thread-safe. |
| 1917 | So far, they can be used only from single threaded programs. |
| 1918 | */ |
| 1919 | |
| 1920 | grn_rc |
| 1921 | grn_pat_cache_enable(grn_ctx *ctx, grn_pat *pat, uint32_t cache_size) |
| 1922 | { |
| 1923 | grn_id *cache; |
| 1924 | |
| 1925 | if (cache_size & (cache_size - 1)) { |
| 1926 | ERR(GRN_INVALID_ARGUMENT, |
| 1927 | "[pat][cache][enable] size must be a power of two: %u", |
| 1928 | cache_size); |
| 1929 | return ctx->rc; |
| 1930 | } |
| 1931 | if (cache_size <= pat->cache_size) { |
| 1932 | return GRN_SUCCESS; |
| 1933 | } |
| 1934 | if (!(cache = GRN_CALLOC(cache_size * sizeof(grn_id)))) { |
| 1935 | return ctx->rc; |
| 1936 | } |
| 1937 | if (pat->cache) { |
| 1938 | GRN_FREE(pat->cache); |
| 1939 | } |
| 1940 | pat->cache = cache; |
| 1941 | pat->cache_size = cache_size; |
| 1942 | |
| 1943 | return GRN_SUCCESS; |
| 1944 | } |
| 1945 | |
| 1946 | void |
| 1947 | grn_pat_cache_disable(grn_ctx *ctx, grn_pat *pat) |
| 1948 | { |
| 1949 | if (pat->cache) { |
| 1950 | GRN_FREE(pat->cache); |
| 1951 | pat->cache_size = 0; |
| 1952 | pat->cache = NULL; |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | grn_pat * |
| 1957 | grn_pat_open(grn_ctx *ctx, const char *path) |
| 1958 | { |
| 1959 | grn_io *io; |
| 1960 | grn_pat *pat; |
| 1961 | pat_node_common *node0; |
| 1962 | struct grn_pat_header *header; |
| 1963 | uint32_t io_type; |
| 1964 | io = grn_io_open(ctx, path, GRN_IO_AUTO); |
| 1965 | if (!io) { |
| 1966 | grn_rc rc = ctx->rc; |
| 1967 | if (rc == GRN_SUCCESS) { |
| 1968 | rc = GRN_UNKNOWN_ERROR; |
| 1969 | } |
| 1970 | ERR(rc, |
| 1971 | "[pat][open] failed to open grn_io: <%s>", |
| 1972 | path ? path : "(temporary)"); |
| 1973 | return NULL; |
| 1974 | } |
| 1975 | header = grn_io_header(io); |
| 1976 | io_type = grn_io_get_type(io); |
| 1977 | if (io_type != GRN_TABLE_PAT_KEY) { |
| 1978 | ERR(GRN_INVALID_FORMAT, |
| 1979 | "[pat] file type must be %#04x: <%#04x>: <%s>", |
| 1980 | GRN_TABLE_PAT_KEY, |
| 1981 | io_type, |
| 1982 | path ? path : "(temporary)"); |
| 1983 | grn_io_close(ctx, io); |
| 1984 | return NULL; |
| 1985 | } |
| 1986 | if (!(pat = GRN_CALLOC(sizeof(grn_pat)))) { |
| 1987 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 1988 | "[pat][open] failed to allocate memory: <%s>", |
| 1989 | path ? path : "(temporary)"); |
| 1990 | grn_io_close(ctx, io); |
| 1991 | return NULL; |
| 1992 | } |
| 1993 | GRN_DB_OBJ_SET_TYPE(pat, GRN_TABLE_PAT_KEY); |
| 1994 | pat->io = io; |
| 1995 | pat->header = header; |
| 1996 | pat->key_size = header->key_size; |
| 1997 | pat->value_size = header->value_size; |
| 1998 | pat->encoding = header->encoding; |
| 1999 | grn_table_module_init(ctx, &(pat->tokenizer), header->tokenizer); |
| 2000 | grn_table_modules_init(ctx, &(pat->normalizers)); |
| 2001 | if (header->flags & GRN_OBJ_KEY_NORMALIZE) { |
| 2002 | header->flags &= ~GRN_OBJ_KEY_NORMALIZE; |
| 2003 | header->normalizer = GRN_ID_NIL; |
| 2004 | grn_obj *normalizer = grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1); |
| 2005 | grn_table_modules_add(ctx, &(pat->normalizers), normalizer); |
| 2006 | } else if (header->normalizer != GRN_ID_NIL) { |
| 2007 | grn_obj *normalizer = grn_ctx_at(ctx, header->normalizer); |
| 2008 | grn_table_modules_add(ctx, &(pat->normalizers), normalizer); |
| 2009 | } |
| 2010 | grn_table_modules_init(ctx, &(pat->token_filters)); |
| 2011 | GRN_PTR_INIT(&(pat->token_filter_procs), GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 2012 | grn_table_modules_init(ctx, &(pat->extractors)); |
| 2013 | pat->obj.header.flags = header->flags; |
| 2014 | PAT_AT(pat, 0, node0); |
| 2015 | if (!node0) { |
| 2016 | ERR(GRN_INVALID_FORMAT, |
| 2017 | "[pat][open] failed to get the root node: <%s>", |
| 2018 | path ? path : "(temporary)"); |
| 2019 | grn_io_close(ctx, io); |
| 2020 | GRN_FREE(pat); |
| 2021 | return NULL; |
| 2022 | } |
| 2023 | pat->cache = NULL; |
| 2024 | pat->cache_size = 0; |
| 2025 | pat->is_dirty = false; |
| 2026 | CRITICAL_SECTION_INIT(pat->lock); |
| 2027 | return pat; |
| 2028 | } |
| 2029 | |
| 2030 | /* |
| 2031 | * grn_pat_error_if_truncated() logs an error and returns its error code if |
| 2032 | * a pat is truncated by another process. |
| 2033 | * Otherwise, this function returns GRN_SUCCESS. |
| 2034 | * Note that `ctx` and `pat` must be valid. |
| 2035 | * |
| 2036 | * FIXME: A pat should be reopened if possible. |
| 2037 | */ |
| 2038 | static grn_rc |
| 2039 | grn_pat_error_if_truncated(grn_ctx *ctx, grn_pat *pat) |
| 2040 | { |
| 2041 | if (pat->header->truncated) { |
| 2042 | ERR(GRN_FILE_CORRUPT, |
| 2043 | "pat is truncated, please unmap or reopen the database"); |
| 2044 | return GRN_FILE_CORRUPT; |
| 2045 | } |
| 2046 | return GRN_SUCCESS; |
| 2047 | } |
| 2048 | |
| 2049 | static void |
| 2050 | grn_pat_close_token_filters(grn_ctx *ctx, grn_pat *pat) |
| 2051 | { |
| 2052 | grn_table_modules_fin(ctx, &(pat->token_filters)); |
| 2053 | GRN_OBJ_FIN(ctx, &(pat->token_filter_procs)); |
| 2054 | } |
| 2055 | |
| 2056 | grn_rc |
| 2057 | grn_pat_close(grn_ctx *ctx, grn_pat *pat) |
| 2058 | { |
| 2059 | grn_rc rc; |
| 2060 | |
| 2061 | CRITICAL_SECTION_FIN(pat->lock); |
| 2062 | |
| 2063 | if (pat->is_dirty) { |
| 2064 | uint32_t n_dirty_opens; |
| 2065 | GRN_ATOMIC_ADD_EX(&(pat->header->n_dirty_opens), -1, n_dirty_opens); |
| 2066 | } |
| 2067 | |
| 2068 | if (pat->io->path[0] != '\0' && |
| 2069 | GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_PRIMARY) { |
| 2070 | grn_obj_flush(ctx, (grn_obj *)pat); |
| 2071 | } |
| 2072 | rc = grn_io_close(ctx, pat->io); |
| 2073 | if (rc != GRN_SUCCESS) { |
| 2074 | ERR(rc, "[pat][close] failed to close IO"); |
| 2075 | } |
| 2076 | grn_table_module_fin(ctx, &(pat->tokenizer)); |
| 2077 | grn_table_modules_fin(ctx, &(pat->normalizers)); |
| 2078 | grn_pat_close_token_filters(ctx, pat); |
| 2079 | grn_table_modules_fin(ctx, &(pat->extractors)); |
| 2080 | if (pat->cache) { |
| 2081 | grn_pat_cache_disable(ctx, pat); |
| 2082 | } |
| 2083 | GRN_FREE(pat); |
| 2084 | |
| 2085 | return rc; |
| 2086 | } |
| 2087 | |
| 2088 | grn_rc |
| 2089 | grn_pat_remove(grn_ctx *ctx, const char *path) |
| 2090 | { |
| 2091 | if (!path) { |
| 2092 | ERR(GRN_INVALID_ARGUMENT, "path is null"); |
| 2093 | return GRN_INVALID_ARGUMENT; |
| 2094 | } |
| 2095 | grn_rc wal_rc = grn_wal_remove(ctx, path, "[pat]"); |
| 2096 | grn_rc io_rc = grn_io_remove(ctx, path); |
| 2097 | grn_rc rc = wal_rc; |
| 2098 | if (rc == GRN_SUCCESS) { |
| 2099 | rc = io_rc; |
| 2100 | } |
| 2101 | return rc; |
| 2102 | } |
| 2103 | |
| 2104 | grn_rc |
| 2105 | grn_pat_truncate(grn_ctx *ctx, grn_pat *pat) |
| 2106 | { |
| 2107 | grn_rc rc; |
| 2108 | const char *io_path; |
| 2109 | char *path; |
| 2110 | uint32_t key_size, value_size, flags; |
| 2111 | |
| 2112 | rc = grn_pat_error_if_truncated(ctx, pat); |
| 2113 | if (rc != GRN_SUCCESS) { |
| 2114 | return rc; |
| 2115 | } |
| 2116 | if ((io_path = grn_io_path(pat->io)) && *io_path != '\0') { |
| 2117 | if (!(path = GRN_STRDUP(io_path))) { |
| 2118 | ERR(GRN_NO_MEMORY_AVAILABLE, "cannot duplicate path: <%s>", io_path); |
| 2119 | return GRN_NO_MEMORY_AVAILABLE; |
| 2120 | } |
| 2121 | } else { |
| 2122 | path = NULL; |
| 2123 | } |
| 2124 | key_size = pat->key_size; |
| 2125 | value_size = pat->value_size; |
| 2126 | flags = pat->header->flags; |
| 2127 | if (path) { |
| 2128 | pat->header->truncated = true; |
| 2129 | } |
| 2130 | if ((rc = grn_io_close(ctx, pat->io))) { |
| 2131 | goto exit; |
| 2132 | } |
| 2133 | grn_table_module_fin(ctx, &(pat->tokenizer)); |
| 2134 | grn_table_modules_fin(ctx, &(pat->normalizers)); |
| 2135 | grn_pat_close_token_filters(ctx, pat); |
| 2136 | grn_table_modules_fin(ctx, &(pat->extractors)); |
| 2137 | pat->io = NULL; |
| 2138 | if (path && (rc = grn_pat_remove(ctx, path))) { |
| 2139 | goto exit; |
| 2140 | } |
| 2141 | if (!_grn_pat_create(ctx, pat, path, key_size, value_size, flags)) { |
| 2142 | rc = GRN_UNKNOWN_ERROR; |
| 2143 | } |
| 2144 | if (pat->cache && pat->cache_size) { |
| 2145 | memset(pat->cache, 0, pat->cache_size * sizeof(grn_id)); |
| 2146 | } |
| 2147 | exit: |
| 2148 | if (path) { |
| 2149 | GRN_FREE(path); |
| 2150 | } |
| 2151 | return rc; |
| 2152 | } |
| 2153 | |
| 2154 | static uint32_t |
| 2155 | grn_pat_cache_compute_id(grn_ctx *ctx, |
| 2156 | grn_pat *pat, |
| 2157 | const uint8_t *key, |
| 2158 | uint32_t size) |
| 2159 | { |
| 2160 | const uint8_t *p = key; |
| 2161 | uint32_t length = size; |
| 2162 | uint32_t cache_id = 0; |
| 2163 | for (cache_id = 0; length--; p++) { |
| 2164 | cache_id = (cache_id * 37) + *p; |
| 2165 | } |
| 2166 | cache_id &= (pat->cache_size - 1); |
| 2167 | return cache_id; |
| 2168 | } |
| 2169 | |
| 2170 | typedef struct { |
| 2171 | grn_pat_wal_add_entry_data wal_data; |
| 2172 | bool added; |
| 2173 | uint32_t shared_key_offset; |
| 2174 | uint32_t cache_id; |
| 2175 | grn_id found_id; |
| 2176 | grn_id *last_id_location; |
| 2177 | int32_t check_max; |
| 2178 | } grn_pat_add_data; |
| 2179 | |
| 2180 | static inline bool |
| 2181 | grn_pat_add_internal_find(grn_ctx *ctx, grn_pat_add_data *data) |
| 2182 | { |
| 2183 | grn_pat *pat = data->wal_data.pat; |
| 2184 | const uint8_t *key = data->wal_data.key; |
| 2185 | uint32_t key_size = data->wal_data.key_size; |
| 2186 | int32_t check_max = data->check_max; |
| 2187 | |
| 2188 | grn_id id = GRN_ID_NIL; |
| 2189 | pat_node_common *node; |
| 2190 | PAT_AT(pat, id, node); |
| 2191 | grn_id *id_location_previous = NULL; |
| 2192 | grn_id *id_location = pat_node_get_right_address(pat, node); |
| 2193 | data->wal_data.record_direction = DIRECTION_RIGHT; |
| 2194 | grn_pat_wal_add_entry_data_set_record_direction(ctx, |
| 2195 | pat, |
| 2196 | &(data->wal_data), |
| 2197 | id, |
| 2198 | node, |
| 2199 | id_location); |
| 2200 | if (*id_location == GRN_ID_NIL) { |
| 2201 | data->last_id_location = id_location; |
| 2202 | data->wal_data.check = PAT_CHECK_PACK(key_size - 1, 7, false); |
| 2203 | return true; |
| 2204 | } |
| 2205 | |
| 2206 | const uint8_t *found_key = NULL; |
| 2207 | uint32_t found_key_size = 0; |
| 2208 | grn_id id_previous = GRN_ID_NIL; |
| 2209 | int check_node = -1; |
| 2210 | int check_node_previous = -1; |
| 2211 | for (;;) { |
| 2212 | id_previous = id; |
| 2213 | id = *id_location; |
| 2214 | if (id == GRN_ID_NIL) { |
| 2215 | found_key = pat_node_get_key(ctx, pat, node); |
| 2216 | if (!found_key) { |
| 2217 | grn_obj_set_error(ctx, |
| 2218 | (grn_obj *)pat, |
| 2219 | GRN_INVALID_ARGUMENT, |
| 2220 | id_previous, |
| 2221 | data->wal_data.tag, |
| 2222 | "failed to get key from node"); |
| 2223 | return false; |
| 2224 | } |
| 2225 | found_key_size = pat_node_get_key_length(pat, node); |
| 2226 | break; |
| 2227 | } |
| 2228 | PAT_AT(pat, id, node); |
| 2229 | if (!node) { |
| 2230 | grn_obj_set_error(ctx, |
| 2231 | (grn_obj *)pat, |
| 2232 | GRN_INVALID_ARGUMENT, |
| 2233 | id, |
| 2234 | data->wal_data.tag, |
| 2235 | "failed to get node"); |
| 2236 | return false; |
| 2237 | } |
| 2238 | uint16_t check_node_current = pat_node_get_check(pat, node); |
| 2239 | if (check_node < check_node_current && check_node_current < check_max) { |
| 2240 | check_node_previous = check_node; |
| 2241 | check_node = check_node_current; |
| 2242 | id_location_previous = id_location; |
| 2243 | id_location = |
| 2244 | grn_pat_next_location(ctx, pat, node, key, check_node, check_max); |
| 2245 | grn_pat_wal_add_entry_data_set_record_direction(ctx, |
| 2246 | pat, |
| 2247 | &(data->wal_data), |
| 2248 | id, |
| 2249 | node, |
| 2250 | id_location); |
| 2251 | } else { |
| 2252 | found_key = pat_node_get_key(ctx, pat, node); |
| 2253 | if (!found_key) { |
| 2254 | grn_obj_set_error(ctx, |
| 2255 | (grn_obj *)pat, |
| 2256 | GRN_INVALID_ARGUMENT, |
| 2257 | id, |
| 2258 | data->wal_data.tag, |
| 2259 | "failed to get key from node: " |
| 2260 | "check_node:%d " |
| 2261 | "node->check:%u " |
| 2262 | "check_max:%d", |
| 2263 | check_node, |
| 2264 | check_node_current, |
| 2265 | check_max); |
| 2266 | return false; |
| 2267 | } |
| 2268 | found_key_size = pat_node_get_key_length(pat, node); |
| 2269 | if (key_size == found_key_size && memcmp(found_key, key, key_size) == 0) { |
| 2270 | if (pat->cache) { |
| 2271 | pat->cache[data->cache_id] = id; |
| 2272 | } |
| 2273 | data->found_id = id; |
| 2274 | return true; |
| 2275 | } |
| 2276 | break; |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | int32_t check; |
| 2281 | { |
| 2282 | uint32_t min = key_size > found_key_size ? found_key_size : key_size; |
| 2283 | uint16_t byte_differences = 0; |
| 2284 | const uint8_t *k1 = key; |
| 2285 | const uint8_t *k2 = found_key; |
| 2286 | for (; min > 0 && *k1 == *k2; byte_differences++, k1++, k2++, min--) { |
| 2287 | } |
| 2288 | if (min == 0) { |
| 2289 | check = PAT_CHECK_PACK(byte_differences - 1, 7, true); |
| 2290 | } else { |
| 2291 | uint8_t bit_differences = 0; |
| 2292 | int xor = *k1 ^ *k2; |
| 2293 | int mask = 0x80; |
| 2294 | for (; !(xor&mask); mask >>= 1, bit_differences++) { |
| 2295 | } |
| 2296 | check = PAT_CHECK_PACK(byte_differences, bit_differences, false); |
| 2297 | } |
| 2298 | } |
| 2299 | if (check == check_node && *id_location == GRN_ID_NIL) { |
| 2300 | if (check < (check_max - (1 << PAT_CHECK_BIT_DIFFERENCES_SHIFT))) { |
| 2301 | check = PAT_CHECK_ADD_BIT_DIFFERENCES(check, 1); |
| 2302 | } |
| 2303 | } else { |
| 2304 | if (check < check_node) { |
| 2305 | if (check > check_node_previous) { |
| 2306 | data->wal_data.record_direction = |
| 2307 | data->wal_data.parent_record_direction; |
| 2308 | data->wal_data.parent_record_id = data->wal_data.grandparent_record_id; |
| 2309 | id = id_previous; |
| 2310 | data->wal_data.previous_record_id = id_previous; |
| 2311 | id_location = id_location_previous; |
| 2312 | } else { |
| 2313 | id = GRN_ID_NIL; |
| 2314 | PAT_AT(pat, id, node); |
| 2315 | id_location = pat_node_get_right_address(pat, node); |
| 2316 | data->wal_data.parent_record_direction = DIRECTION_RIGHT; |
| 2317 | grn_pat_wal_add_entry_data_set_record_direction(ctx, |
| 2318 | pat, |
| 2319 | &(data->wal_data), |
| 2320 | id, |
| 2321 | node, |
| 2322 | id_location); |
| 2323 | while ((id = *id_location) != GRN_ID_NIL) { |
| 2324 | PAT_AT(pat, id, node); |
| 2325 | if (!node) { |
| 2326 | grn_obj_set_error(ctx, |
| 2327 | (grn_obj *)pat, |
| 2328 | GRN_INVALID_ARGUMENT, |
| 2329 | id, |
| 2330 | data->wal_data.tag, |
| 2331 | "failed to get node to detect position"); |
| 2332 | return false; |
| 2333 | } |
| 2334 | check_node = pat_node_get_check(pat, node); |
| 2335 | if (check < check_node) { |
| 2336 | break; |
| 2337 | } |
| 2338 | id_location = |
| 2339 | grn_pat_next_location(ctx, pat, node, key, check_node, check_max); |
| 2340 | grn_pat_wal_add_entry_data_set_record_direction(ctx, |
| 2341 | pat, |
| 2342 | &(data->wal_data), |
| 2343 | id, |
| 2344 | node, |
| 2345 | id_location); |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | if (check >= check_max) { |
| 2351 | grn_obj_set_error(ctx, |
| 2352 | (grn_obj *)pat, |
| 2353 | GRN_INVALID_ARGUMENT, |
| 2354 | id, |
| 2355 | data->wal_data.tag, |
| 2356 | "BUG: computed check is too large: " |
| 2357 | "check:%u " |
| 2358 | "check_max:%d " |
| 2359 | "key_size:%u", |
| 2360 | check, |
| 2361 | check_max, |
| 2362 | key_size); |
| 2363 | return false; |
| 2364 | } |
| 2365 | data->last_id_location = id_location; |
| 2366 | data->wal_data.check = check; |
| 2367 | return true; |
| 2368 | } |
| 2369 | |
| 2370 | static inline void |
| 2371 | grn_pat_enable_node(grn_ctx *ctx, |
| 2372 | grn_pat *pat, |
| 2373 | pat_node_common *node, |
| 2374 | grn_id id, |
| 2375 | const uint8_t *key, |
| 2376 | uint16_t check, |
| 2377 | int32_t check_max, |
| 2378 | grn_id *id_location) |
| 2379 | { |
| 2380 | pat_node_set_check(pat, node, check); |
| 2381 | pat_node_set_deleting_off(pat, node); |
| 2382 | if (PAT_CHECK_IS_TERMINATED(check) |
| 2383 | ? |
| 2384 | /* check + 1: |
| 2385 | * delete terminated flag and increment bit differences */ |
| 2386 | (check + 1 < check_max) |
| 2387 | : nth_bit(key, check)) { |
| 2388 | pat_node_set_right(pat, node, id); |
| 2389 | pat_node_set_left(pat, node, *id_location); |
| 2390 | } else { |
| 2391 | pat_node_set_right(pat, node, *id_location); |
| 2392 | pat_node_set_left(pat, node, id); |
| 2393 | } |
| 2394 | *id_location = id; |
| 2395 | } |
| 2396 | |
| 2397 | static inline void |
| 2398 | grn_pat_reuse_shared_node(grn_ctx *ctx, |
| 2399 | grn_pat *pat, |
| 2400 | pat_node_common *node, |
| 2401 | grn_id id, |
| 2402 | const uint8_t *key, |
| 2403 | uint32_t key_size, |
| 2404 | uint32_t shared_key_offset, |
| 2405 | uint16_t check, |
| 2406 | int32_t check_max, |
| 2407 | grn_id *id_location) |
| 2408 | { |
| 2409 | pat->header->garbages[0] = pat_node_get_left(pat, node); |
| 2410 | pat->header->n_garbages--; |
| 2411 | pat->header->n_entries++; |
| 2412 | pat_node_set_shared_key(ctx, pat, node, key_size, shared_key_offset); |
| 2413 | grn_pat_enable_node(ctx, pat, node, id, key, check, check_max, id_location); |
| 2414 | } |
| 2415 | |
| 2416 | static inline void |
| 2417 | grn_pat_add_shared_node(grn_ctx *ctx, |
| 2418 | grn_pat *pat, |
| 2419 | pat_node_common *node, |
| 2420 | grn_id id, |
| 2421 | const uint8_t *key, |
| 2422 | uint32_t key_size, |
| 2423 | uint32_t shared_key_offset, |
| 2424 | uint16_t check, |
| 2425 | int32_t check_max, |
| 2426 | grn_id *id_location) |
| 2427 | { |
| 2428 | pat->header->curr_rec = id; |
| 2429 | pat->header->n_entries++; |
| 2430 | pat_node_set_shared_key(ctx, pat, node, key_size, shared_key_offset); |
| 2431 | grn_pat_enable_node(ctx, pat, node, id, key, check, check_max, id_location); |
| 2432 | } |
| 2433 | |
| 2434 | static inline grn_rc |
| 2435 | grn_pat_reuse_node(grn_ctx *ctx, |
| 2436 | grn_pat *pat, |
| 2437 | pat_node_common *node, |
| 2438 | grn_id id, |
| 2439 | const uint8_t *key, |
| 2440 | uint32_t key_size, |
| 2441 | uint16_t check, |
| 2442 | int32_t check_max, |
| 2443 | grn_id *id_location, |
| 2444 | const char *tag) |
| 2445 | { |
| 2446 | uint8_t *key_buffer = pat_node_get_key(ctx, pat, node); |
| 2447 | if (!key_buffer) { |
| 2448 | grn_obj_set_error(ctx, |
| 2449 | (grn_obj *)pat, |
| 2450 | GRN_FILE_CORRUPT, |
| 2451 | id, |
| 2452 | tag, |
| 2453 | "failed to get key from node: " |
| 2454 | "size:%u", |
| 2455 | key_size); |
| 2456 | return ctx->rc; |
| 2457 | } |
| 2458 | uint32_t key_storage_size = pat_key_storage_size(pat, key_size); |
| 2459 | pat->header->garbages[key_storage_size] = pat_node_get_left(pat, node); |
| 2460 | pat_node_set_key_length(pat, node, key_size); |
| 2461 | grn_memcpy(key_buffer, key, key_size); |
| 2462 | pat->header->n_garbages--; |
| 2463 | pat->header->n_entries++; |
| 2464 | grn_pat_enable_node(ctx, pat, node, id, key, check, check_max, id_location); |
| 2465 | return GRN_SUCCESS; |
| 2466 | } |
| 2467 | |
| 2468 | static inline grn_rc |
| 2469 | grn_pat_add_node(grn_ctx *ctx, |
| 2470 | grn_pat *pat, |
| 2471 | pat_node_common *node, |
| 2472 | grn_id id, |
| 2473 | const uint8_t *key, |
| 2474 | uint32_t key_size, |
| 2475 | uint16_t check, |
| 2476 | int32_t check_max, |
| 2477 | grn_id *id_location, |
| 2478 | const char *tag) |
| 2479 | { |
| 2480 | grn_rc rc = pat_node_set_key(ctx, pat, node, key, key_size); |
| 2481 | if (rc != GRN_SUCCESS) { |
| 2482 | grn_obj inspected_key; |
| 2483 | GRN_TEXT_INIT(&inspected_key, 0); |
| 2484 | grn_inspect_key(ctx, &inspected_key, (grn_obj *)pat, key, key_size); |
| 2485 | grn_obj_set_error(ctx, |
| 2486 | (grn_obj *)pat, |
| 2487 | rc, |
| 2488 | id, |
| 2489 | tag, |
| 2490 | "failed to set key: %.*s", |
| 2491 | (int)GRN_TEXT_LEN(&inspected_key), |
| 2492 | GRN_TEXT_VALUE(&inspected_key)); |
| 2493 | GRN_OBJ_FIN(ctx, &inspected_key); |
| 2494 | return ctx->rc; |
| 2495 | } |
| 2496 | pat->header->curr_rec = id; |
| 2497 | pat->header->n_entries++; |
| 2498 | grn_pat_enable_node(ctx, pat, node, id, key, check, check_max, id_location); |
| 2499 | return GRN_SUCCESS; |
| 2500 | } |
| 2501 | |
| 2502 | static inline grn_id |
| 2503 | grn_pat_add_internal(grn_ctx *ctx, grn_pat_add_data *data) |
| 2504 | { |
| 2505 | grn_pat *pat = data->wal_data.pat; |
| 2506 | const uint8_t *key = data->wal_data.key; |
| 2507 | uint32_t key_size = data->wal_data.key_size; |
| 2508 | |
| 2509 | data->added = false; |
| 2510 | data->cache_id = 0; |
| 2511 | if (pat->cache) { |
| 2512 | data->cache_id = grn_pat_cache_compute_id(ctx, pat, key, key_size); |
| 2513 | if (pat->cache[data->cache_id] != GRN_ID_NIL) { |
| 2514 | pat_node_common *node; |
| 2515 | PAT_AT(pat, pat->cache[data->cache_id], node); |
| 2516 | if (node) { |
| 2517 | const uint8_t *k = pat_node_get_key(ctx, pat, node); |
| 2518 | if (k && key_size == pat_node_get_key_length(pat, node) && |
| 2519 | memcmp(k, key, key_size) == 0) { |
| 2520 | return pat->cache[data->cache_id]; |
| 2521 | } |
| 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | data->check_max = PAT_CHECK_PACK(key_size, 0, false); |
| 2527 | data->found_id = GRN_ID_NIL; |
| 2528 | if (!grn_pat_add_internal_find(ctx, data)) { |
| 2529 | return GRN_ID_NIL; |
| 2530 | } |
| 2531 | if (data->found_id != GRN_ID_NIL) { |
| 2532 | return data->found_id; |
| 2533 | } |
| 2534 | |
| 2535 | data->wal_data.key_offset = grn_pat_total_key_size(ctx, pat); |
| 2536 | pat_node_common *node = NULL; |
| 2537 | { |
| 2538 | uint32_t key_storage_size = pat_key_storage_size(pat, key_size); |
| 2539 | if (data->shared_key_offset > 0 && key_storage_size > 0) { |
| 2540 | data->wal_data.shared_key_offset = data->shared_key_offset; |
| 2541 | if (pat->header->garbages[0] != GRN_ID_NIL) { |
| 2542 | data->wal_data.event = GRN_WAL_EVENT_REUSE_SHARED_ENTRY; |
| 2543 | data->wal_data.record_id = pat->header->garbages[0]; |
| 2544 | PAT_AT(pat, data->wal_data.record_id, node); |
| 2545 | if (!node) { |
| 2546 | grn_obj_set_error(ctx, |
| 2547 | (grn_obj *)pat, |
| 2548 | GRN_INVALID_ARGUMENT, |
| 2549 | data->wal_data.record_id, |
| 2550 | data->wal_data.tag, |
| 2551 | "failed to get node from garbage: " |
| 2552 | "shared-key-offset:%u", |
| 2553 | data->shared_key_offset); |
| 2554 | return GRN_ID_NIL; |
| 2555 | } |
| 2556 | data->wal_data.next_garbage_record_id = pat_node_get_left(pat, node); |
| 2557 | data->wal_data.n_garbages = pat->header->n_garbages; |
| 2558 | data->wal_data.n_entries = pat->header->n_entries; |
| 2559 | if (grn_pat_wal_add_entry(ctx, &(data->wal_data)) != GRN_SUCCESS) { |
| 2560 | return GRN_ID_NIL; |
| 2561 | } |
| 2562 | grn_pat_reuse_shared_node(ctx, |
| 2563 | pat, |
| 2564 | node, |
| 2565 | data->wal_data.record_id, |
| 2566 | data->wal_data.key, |
| 2567 | data->wal_data.key_size, |
| 2568 | data->wal_data.shared_key_offset, |
| 2569 | data->wal_data.check, |
| 2570 | data->check_max, |
| 2571 | data->last_id_location); |
| 2572 | pat->header->wal_id = data->wal_data.wal_id; |
| 2573 | } else { |
| 2574 | data->wal_data.event = GRN_WAL_EVENT_ADD_SHARED_ENTRY; |
| 2575 | data->wal_data.record_id = pat->header->curr_rec + 1; |
| 2576 | data->wal_data.n_entries = pat->header->n_entries; |
| 2577 | if (grn_pat_wal_add_entry(ctx, &(data->wal_data)) != GRN_SUCCESS) { |
| 2578 | return GRN_ID_NIL; |
| 2579 | } |
| 2580 | node = pat_node_get(ctx, pat, data->wal_data.record_id); |
| 2581 | if (!node) { |
| 2582 | grn_obj_set_error(ctx, |
| 2583 | (grn_obj *)pat, |
| 2584 | GRN_INVALID_ARGUMENT, |
| 2585 | data->wal_data.record_id, |
| 2586 | data->wal_data.tag, |
| 2587 | "failed to get node: " |
| 2588 | "shared-key-offset:%u", |
| 2589 | data->shared_key_offset); |
| 2590 | return GRN_ID_NIL; |
| 2591 | } |
| 2592 | grn_pat_add_shared_node(ctx, |
| 2593 | pat, |
| 2594 | node, |
| 2595 | data->wal_data.record_id, |
| 2596 | data->wal_data.key, |
| 2597 | data->wal_data.key_size, |
| 2598 | data->wal_data.shared_key_offset, |
| 2599 | data->wal_data.check, |
| 2600 | data->check_max, |
| 2601 | data->last_id_location); |
| 2602 | pat->header->wal_id = data->wal_data.wal_id; |
| 2603 | } |
| 2604 | } else { |
| 2605 | if (pat->header->garbages[key_storage_size] != GRN_ID_NIL) { |
| 2606 | data->wal_data.event = GRN_WAL_EVENT_REUSE_ENTRY; |
| 2607 | data->wal_data.record_id = pat->header->garbages[key_storage_size]; |
| 2608 | data->wal_data.n_garbages = pat->header->n_garbages; |
| 2609 | data->wal_data.n_entries = pat->header->n_entries; |
| 2610 | PAT_AT(pat, data->wal_data.record_id, node); |
| 2611 | if (!node) { |
| 2612 | grn_obj_set_error(ctx, |
| 2613 | (grn_obj *)pat, |
| 2614 | GRN_INVALID_ARGUMENT, |
| 2615 | data->wal_data.record_id, |
| 2616 | data->wal_data.tag, |
| 2617 | "failed to get node from garbage"); |
| 2618 | return GRN_ID_NIL; |
| 2619 | } |
| 2620 | data->wal_data.next_garbage_record_id = pat_node_get_left(pat, node); |
| 2621 | if (grn_pat_wal_add_entry(ctx, &(data->wal_data)) != GRN_SUCCESS) { |
| 2622 | return GRN_ID_NIL; |
| 2623 | } |
| 2624 | if (grn_pat_reuse_node(ctx, |
| 2625 | pat, |
| 2626 | node, |
| 2627 | data->wal_data.record_id, |
| 2628 | data->wal_data.key, |
| 2629 | data->wal_data.key_size, |
| 2630 | data->wal_data.check, |
| 2631 | data->check_max, |
| 2632 | data->last_id_location, |
| 2633 | data->wal_data.tag) != GRN_SUCCESS) { |
| 2634 | return GRN_ID_NIL; |
| 2635 | } |
| 2636 | pat->header->wal_id = data->wal_data.wal_id; |
| 2637 | } else { |
| 2638 | data->wal_data.event = GRN_WAL_EVENT_ADD_ENTRY; |
| 2639 | data->wal_data.record_id = pat->header->curr_rec + 1; |
| 2640 | data->wal_data.n_entries = pat->header->n_entries; |
| 2641 | if (grn_pat_wal_add_entry(ctx, &(data->wal_data)) != GRN_SUCCESS) { |
| 2642 | return GRN_ID_NIL; |
| 2643 | } |
| 2644 | node = pat_node_get(ctx, pat, data->wal_data.record_id); |
| 2645 | if (!node) { |
| 2646 | grn_obj_set_error(ctx, |
| 2647 | (grn_obj *)pat, |
| 2648 | GRN_INVALID_ARGUMENT, |
| 2649 | data->wal_data.record_id, |
| 2650 | data->wal_data.tag, |
| 2651 | "failed to get node"); |
| 2652 | return GRN_ID_NIL; |
| 2653 | } |
| 2654 | if (grn_pat_add_node(ctx, |
| 2655 | pat, |
| 2656 | node, |
| 2657 | data->wal_data.record_id, |
| 2658 | data->wal_data.key, |
| 2659 | data->wal_data.key_size, |
| 2660 | data->wal_data.check, |
| 2661 | data->check_max, |
| 2662 | data->last_id_location, |
| 2663 | data->wal_data.tag) != GRN_SUCCESS) { |
| 2664 | return GRN_ID_NIL; |
| 2665 | } |
| 2666 | pat->header->wal_id = data->wal_data.wal_id; |
| 2667 | } |
| 2668 | data->shared_key_offset = pat_node_get_key_offset(pat, node); |
| 2669 | } |
| 2670 | } |
| 2671 | data->added = true; |
| 2672 | if (pat->cache) { |
| 2673 | pat->cache[data->cache_id] = data->wal_data.record_id; |
| 2674 | } |
| 2675 | return data->wal_data.record_id; |
| 2676 | } |
| 2677 | |
| 2678 | static inline bool |
| 2679 | chop(grn_ctx *ctx, |
| 2680 | grn_pat *pat, |
| 2681 | const char **key, |
| 2682 | const char *end, |
| 2683 | uint32_t *offset) |
| 2684 | { |
| 2685 | size_t len = grn_charlen(ctx, *key, end); |
| 2686 | if (len > 0) { |
| 2687 | *offset += len; |
| 2688 | *key += len; |
| 2689 | return (end - *key) > 0; |
| 2690 | } else { |
| 2691 | return false; |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | #define MAX_FIXED_KEY_SIZE (sizeof(int64_t)) |
| 2696 | |
| 2697 | #define KEY_NEEDS_CONVERT(pat, size) \ |
| 2698 | (!((pat)->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) && \ |
| 2699 | (size_t)(size) <= MAX_FIXED_KEY_SIZE) |
| 2700 | |
| 2701 | #define KEY_ENC(pat, keybuf, key, size) \ |
| 2702 | do { \ |
| 2703 | switch ((pat)->obj.header.flags & GRN_OBJ_KEY_MASK) { \ |
| 2704 | case GRN_OBJ_KEY_UINT: \ |
| 2705 | if (((pat)->obj.header.domain != GRN_DB_TOKYO_GEO_POINT) && \ |
| 2706 | ((pat)->obj.header.domain != GRN_DB_WGS84_GEO_POINT)) { \ |
| 2707 | grn_hton((keybuf), (key), (size)); \ |
| 2708 | break; \ |
| 2709 | } \ |
| 2710 | case GRN_OBJ_KEY_GEO_POINT: \ |
| 2711 | grn_gton((keybuf), (key), (size)); \ |
| 2712 | break; \ |
| 2713 | case GRN_OBJ_KEY_INT: \ |
| 2714 | grn_hton((keybuf), (key), (size)); \ |
| 2715 | *((uint8_t *)(keybuf)) ^= 0x80; \ |
| 2716 | break; \ |
| 2717 | case GRN_OBJ_KEY_FLOAT: \ |
| 2718 | if ((size) == sizeof(int64_t)) { \ |
| 2719 | int64_t v = *(int64_t *)(key); \ |
| 2720 | v ^= ((v >> 63) | (1ULL << 63)); \ |
| 2721 | grn_hton((keybuf), &v, (size)); \ |
| 2722 | } else if ((size) == sizeof(int32_t)) { \ |
| 2723 | int32_t v = *(int32_t *)(key); \ |
| 2724 | v ^= ((v >> 31) | (1ULL << 31)); \ |
| 2725 | grn_hton((keybuf), &v, (size)); \ |
| 2726 | } \ |
| 2727 | break; \ |
| 2728 | } \ |
| 2729 | } while (0) |
| 2730 | |
| 2731 | #define KEY_DEC(pat, keybuf, key, size) \ |
| 2732 | do { \ |
| 2733 | switch ((pat)->obj.header.flags & GRN_OBJ_KEY_MASK) { \ |
| 2734 | case GRN_OBJ_KEY_UINT: \ |
| 2735 | if (((pat)->obj.header.domain != GRN_DB_TOKYO_GEO_POINT) && \ |
| 2736 | ((pat)->obj.header.domain != GRN_DB_WGS84_GEO_POINT)) { \ |
| 2737 | grn_ntoh((keybuf), (key), (size)); \ |
| 2738 | break; \ |
| 2739 | } \ |
| 2740 | case GRN_OBJ_KEY_GEO_POINT: \ |
| 2741 | grn_ntog((keybuf), (key), (size)); \ |
| 2742 | break; \ |
| 2743 | case GRN_OBJ_KEY_INT: \ |
| 2744 | grn_ntohi((keybuf), (key), (size)); \ |
| 2745 | break; \ |
| 2746 | case GRN_OBJ_KEY_FLOAT: \ |
| 2747 | if ((size) == sizeof(int64_t)) { \ |
| 2748 | int64_t v; \ |
| 2749 | grn_hton(&v, (key), (size)); \ |
| 2750 | *((int64_t *)(keybuf)) = \ |
| 2751 | v ^ ((((int64_t)(v ^ (1ULL << 63))) >> 63) | (1ULL << 63)); \ |
| 2752 | } else if ((size) == sizeof(int32_t)) { \ |
| 2753 | int32_t v; \ |
| 2754 | grn_hton(&v, (key), (size)); \ |
| 2755 | *((int32_t *)(keybuf)) = \ |
| 2756 | v ^ ((((int32_t)(v ^ (1ULL << 31))) >> 31) | (1ULL << 31)); \ |
| 2757 | } \ |
| 2758 | break; \ |
| 2759 | } \ |
| 2760 | } while (0) |
| 2761 | |
| 2762 | #define KEY_ENCODE(pat, keybuf, key, size) \ |
| 2763 | do { \ |
| 2764 | if (KEY_NEEDS_CONVERT(pat, size)) { \ |
| 2765 | KEY_ENC((pat), (keybuf), (key), (size)); \ |
| 2766 | (key) = (keybuf); \ |
| 2767 | } \ |
| 2768 | } while (0) |
| 2769 | |
| 2770 | grn_id |
| 2771 | grn_pat_add(grn_ctx *ctx, |
| 2772 | grn_pat *pat, |
| 2773 | const void *key, |
| 2774 | uint32_t key_size, |
| 2775 | void **value, |
| 2776 | int *added) |
| 2777 | { |
| 2778 | grn_pat_add_data data = {0}; |
| 2779 | data.wal_data.pat = pat; |
| 2780 | data.wal_data.tag = "[pat][add]"; |
| 2781 | data.wal_data.key = key; |
| 2782 | data.wal_data.key_size = key_size; |
| 2783 | data.added = false; |
| 2784 | data.shared_key_offset = 0; |
| 2785 | grn_id r0; |
| 2786 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 2787 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 2788 | return GRN_ID_NIL; |
| 2789 | } |
| 2790 | if (!key) { |
| 2791 | grn_obj_set_error(ctx, |
| 2792 | (grn_obj *)pat, |
| 2793 | GRN_INVALID_ARGUMENT, |
| 2794 | GRN_ID_NIL, |
| 2795 | data.wal_data.tag, |
| 2796 | "key must not NULL"); |
| 2797 | return GRN_ID_NIL; |
| 2798 | } |
| 2799 | if (!key_size) { |
| 2800 | grn_obj_set_error(ctx, |
| 2801 | (grn_obj *)pat, |
| 2802 | GRN_INVALID_ARGUMENT, |
| 2803 | GRN_ID_NIL, |
| 2804 | data.wal_data.tag, |
| 2805 | "key size must not zero"); |
| 2806 | return GRN_ID_NIL; |
| 2807 | } |
| 2808 | if (key_size > GRN_TABLE_MAX_KEY_SIZE) { |
| 2809 | grn_obj_set_error(ctx, |
| 2810 | (grn_obj *)pat, |
| 2811 | GRN_INVALID_ARGUMENT, |
| 2812 | GRN_ID_NIL, |
| 2813 | data.wal_data.tag, |
| 2814 | "too long key: <%u>", |
| 2815 | key_size); |
| 2816 | return GRN_ID_NIL; |
| 2817 | } |
| 2818 | KEY_ENCODE(pat, keybuf, data.wal_data.key, data.wal_data.key_size); |
| 2819 | r0 = grn_pat_add_internal(ctx, &data); |
| 2820 | if (r0 == GRN_ID_NIL) { |
| 2821 | grn_obj_set_error(ctx, |
| 2822 | (grn_obj *)pat, |
| 2823 | GRN_INVALID_ARGUMENT, |
| 2824 | GRN_ID_NIL, |
| 2825 | data.wal_data.tag, |
| 2826 | "failed to add a key"); |
| 2827 | return GRN_ID_NIL; |
| 2828 | } |
| 2829 | if (added) { |
| 2830 | *added = data.added; |
| 2831 | } |
| 2832 | if (r0 && (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) && |
| 2833 | (*(data.wal_data.key) & 0x80)) { // todo: refine!! |
| 2834 | sis_node *sl, *sr; |
| 2835 | grn_id l = r0, r; |
| 2836 | if (data.added && (sl = sis_get(ctx, pat, l))) { |
| 2837 | const char *sis = data.wal_data.key; |
| 2838 | const char *end = sis + data.wal_data.key_size; |
| 2839 | sl->children = l; |
| 2840 | sl->sibling = 0; |
| 2841 | while (chop(ctx, pat, &sis, end, &(data.shared_key_offset))) { |
| 2842 | if (!(*sis & 0x80)) { |
| 2843 | break; |
| 2844 | } |
| 2845 | data.wal_data.key = sis; |
| 2846 | data.wal_data.key_size = end - sis; |
| 2847 | if (!(r = grn_pat_add_internal(ctx, &data))) { |
| 2848 | break; |
| 2849 | } |
| 2850 | if (!(sr = sis_get(ctx, pat, r))) { |
| 2851 | break; |
| 2852 | } |
| 2853 | if (data.added) { |
| 2854 | sl->sibling = r; |
| 2855 | sr->children = l; |
| 2856 | sr->sibling = 0; |
| 2857 | } else { |
| 2858 | sl->sibling = sr->children; |
| 2859 | sr->children = l; |
| 2860 | break; |
| 2861 | } |
| 2862 | l = r; |
| 2863 | sl = sr; |
| 2864 | } |
| 2865 | } |
| 2866 | } |
| 2867 | if (r0 && value) { |
| 2868 | byte *v = (byte *)sis_get(ctx, pat, r0); |
| 2869 | if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { |
| 2870 | *value = v + sizeof(sis_node); |
| 2871 | } else { |
| 2872 | *value = v; |
| 2873 | } |
| 2874 | } |
| 2875 | return r0; |
| 2876 | } |
| 2877 | |
| 2878 | static inline grn_id |
| 2879 | _grn_pat_get( |
| 2880 | grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size, void **value) |
| 2881 | { |
| 2882 | grn_id r; |
| 2883 | pat_node_common *rn; |
| 2884 | int32_t c0 = -1, c; |
| 2885 | int32_t len = key_size * 16; |
| 2886 | PAT_AT(pat, 0, rn); |
| 2887 | for (r = pat_node_get_right(pat, rn); r;) { |
| 2888 | PAT_AT(pat, r, rn); |
| 2889 | if (!rn) { |
| 2890 | break; /* corrupt? */ |
| 2891 | } |
| 2892 | c = pat_node_get_check(pat, rn); |
| 2893 | if (len <= c) { |
| 2894 | break; |
| 2895 | } |
| 2896 | if (c <= c0) { |
| 2897 | const uint8_t *k = pat_node_get_key(ctx, pat, rn); |
| 2898 | if (k && key_size == pat_node_get_key_length(pat, rn) && |
| 2899 | !memcmp(k, key, key_size)) { |
| 2900 | if (value) { |
| 2901 | byte *v = (byte *)sis_get(ctx, pat, r); |
| 2902 | if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { |
| 2903 | *value = v + sizeof(sis_node); |
| 2904 | } else { |
| 2905 | *value = v; |
| 2906 | } |
| 2907 | } |
| 2908 | return r; |
| 2909 | } |
| 2910 | break; |
| 2911 | } |
| 2912 | r = *grn_pat_next_location(ctx, pat, rn, key, c, len); |
| 2913 | c0 = c; |
| 2914 | } |
| 2915 | return GRN_ID_NIL; |
| 2916 | } |
| 2917 | |
| 2918 | grn_id |
| 2919 | grn_pat_get( |
| 2920 | grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size, void **value) |
| 2921 | { |
| 2922 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 2923 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 2924 | return GRN_ID_NIL; |
| 2925 | } |
| 2926 | KEY_ENCODE(pat, keybuf, key, key_size); |
| 2927 | return _grn_pat_get(ctx, pat, key, key_size, value); |
| 2928 | } |
| 2929 | |
| 2930 | grn_id |
| 2931 | grn_pat_nextid(grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size) |
| 2932 | { |
| 2933 | grn_id r = GRN_ID_NIL; |
| 2934 | if (pat && key) { |
| 2935 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 2936 | return GRN_ID_NIL; |
| 2937 | } |
| 2938 | uint32_t key_storage_size = pat_key_storage_size(pat, key_size); |
| 2939 | if (!(r = pat->header->garbages[key_storage_size])) { |
| 2940 | r = pat->header->curr_rec + 1; |
| 2941 | } |
| 2942 | } |
| 2943 | return r; |
| 2944 | } |
| 2945 | |
| 2946 | static void |
| 2947 | get_tc(grn_ctx *ctx, grn_pat *pat, grn_hash *h, pat_node_common *rn) |
| 2948 | { |
| 2949 | grn_id id; |
| 2950 | pat_node_common *node; |
| 2951 | id = pat_node_get_right(pat, rn); |
| 2952 | if (id) { |
| 2953 | PAT_AT(pat, id, node); |
| 2954 | if (node) { |
| 2955 | if (pat_node_get_check(pat, node) > pat_node_get_check(pat, rn)) { |
| 2956 | get_tc(ctx, pat, h, node); |
| 2957 | } else { |
| 2958 | grn_hash_add(ctx, h, &id, sizeof(grn_id), NULL, NULL); |
| 2959 | } |
| 2960 | } |
| 2961 | } |
| 2962 | id = pat_node_get_left(pat, rn); |
| 2963 | if (id) { |
| 2964 | PAT_AT(pat, id, node); |
| 2965 | if (node) { |
| 2966 | if (pat_node_get_check(pat, node) > pat_node_get_check(pat, rn)) { |
| 2967 | get_tc(ctx, pat, h, node); |
| 2968 | } else { |
| 2969 | grn_hash_add(ctx, h, &id, sizeof(grn_id), NULL, NULL); |
| 2970 | } |
| 2971 | } |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | grn_rc |
| 2976 | grn_pat_prefix_search( |
| 2977 | grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size, grn_hash *h) |
| 2978 | { |
| 2979 | int32_t c0 = -1, c; |
| 2980 | const uint8_t *k; |
| 2981 | int32_t len = key_size * 16; |
| 2982 | grn_id r; |
| 2983 | pat_node_common *rn; |
| 2984 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 2985 | grn_rc rc = grn_pat_error_if_truncated(ctx, pat); |
| 2986 | if (rc != GRN_SUCCESS) { |
| 2987 | return rc; |
| 2988 | } |
| 2989 | if (key_size == 0) { |
| 2990 | return GRN_END_OF_DATA; |
| 2991 | } |
| 2992 | KEY_ENCODE(pat, keybuf, key, key_size); |
| 2993 | PAT_AT(pat, 0, rn); |
| 2994 | r = pat_node_get_right(pat, rn); |
| 2995 | while (r) { |
| 2996 | PAT_AT(pat, r, rn); |
| 2997 | if (!rn) { |
| 2998 | return GRN_FILE_CORRUPT; |
| 2999 | } |
| 3000 | c = pat_node_get_check(pat, rn); |
| 3001 | if (c0 < c && c < len - 1) { |
| 3002 | r = *grn_pat_next_location(ctx, pat, rn, key, c, len); |
| 3003 | c0 = c; |
| 3004 | continue; |
| 3005 | } |
| 3006 | if (!(k = pat_node_get_key(ctx, pat, rn))) { |
| 3007 | break; |
| 3008 | } |
| 3009 | if (pat_node_get_key_length(pat, rn) < key_size) { |
| 3010 | break; |
| 3011 | } |
| 3012 | if (!memcmp(k, key, key_size)) { |
| 3013 | if (c >= len - 1) { |
| 3014 | get_tc(ctx, pat, h, rn); |
| 3015 | } else { |
| 3016 | grn_hash_add(ctx, h, &r, sizeof(grn_id), NULL, NULL); |
| 3017 | } |
| 3018 | return GRN_SUCCESS; |
| 3019 | } |
| 3020 | break; |
| 3021 | } |
| 3022 | return GRN_END_OF_DATA; |
| 3023 | } |
| 3024 | |
| 3025 | grn_hash * |
| 3026 | grn_pat_prefix_search2(grn_ctx *ctx, |
| 3027 | grn_pat *pat, |
| 3028 | const void *key, |
| 3029 | uint32_t key_size) |
| 3030 | { |
| 3031 | grn_hash *h; |
| 3032 | if (!pat || !key) { |
| 3033 | return NULL; |
| 3034 | } |
| 3035 | if ((h = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, 0))) { |
| 3036 | if (grn_pat_prefix_search(ctx, pat, key, key_size, h)) { |
| 3037 | grn_hash_close(ctx, h); |
| 3038 | h = NULL; |
| 3039 | } |
| 3040 | } |
| 3041 | return h; |
| 3042 | } |
| 3043 | |
| 3044 | grn_rc |
| 3045 | grn_pat_suffix_search( |
| 3046 | grn_ctx *ctx, grn_pat *pat, const void *key, uint32_t key_size, grn_hash *h) |
| 3047 | { |
| 3048 | grn_id r; |
| 3049 | if ((r = grn_pat_get(ctx, pat, key, key_size, NULL))) { |
| 3050 | uint32_t *offset; |
| 3051 | if (grn_hash_add(ctx, h, &r, sizeof(grn_id), (void **)&offset, NULL)) { |
| 3052 | *offset = 0; |
| 3053 | if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { |
| 3054 | sis_collect(ctx, pat, h, r, 1); |
| 3055 | } |
| 3056 | return GRN_SUCCESS; |
| 3057 | } |
| 3058 | } |
| 3059 | return GRN_END_OF_DATA; |
| 3060 | } |
| 3061 | |
| 3062 | grn_hash * |
| 3063 | grn_pat_suffix_search2(grn_ctx *ctx, |
| 3064 | grn_pat *pat, |
| 3065 | const void *key, |
| 3066 | uint32_t key_size) |
| 3067 | { |
| 3068 | grn_hash *h; |
| 3069 | if (!pat || !key) { |
| 3070 | return NULL; |
| 3071 | } |
| 3072 | if ((h = grn_hash_create(ctx, NULL, sizeof(grn_id), sizeof(uint32_t), 0))) { |
| 3073 | if (grn_pat_suffix_search(ctx, pat, key, key_size, h)) { |
| 3074 | grn_hash_close(ctx, h); |
| 3075 | h = NULL; |
| 3076 | } |
| 3077 | } |
| 3078 | return h; |
| 3079 | } |
| 3080 | |
| 3081 | grn_id |
| 3082 | grn_pat_lcp_search(grn_ctx *ctx, |
| 3083 | grn_pat *pat, |
| 3084 | const void *key, |
| 3085 | uint32_t key_size) |
| 3086 | { |
| 3087 | pat_node_common *rn; |
| 3088 | grn_id r, r2 = GRN_ID_NIL; |
| 3089 | int32_t len = PAT_CHECK_PACK(key_size, 0, false); |
| 3090 | int32_t c0 = -1, c; |
| 3091 | if (!pat || !key) { |
| 3092 | return GRN_ID_NIL; |
| 3093 | } |
| 3094 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 3095 | return GRN_ID_NIL; |
| 3096 | } |
| 3097 | if (!(pat->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE)) { |
| 3098 | return GRN_ID_NIL; |
| 3099 | } |
| 3100 | PAT_AT(pat, 0, rn); |
| 3101 | for (r = pat_node_get_right(pat, rn); r;) { |
| 3102 | PAT_AT(pat, r, rn); |
| 3103 | if (!rn) { |
| 3104 | break; /* corrupt? */ |
| 3105 | } |
| 3106 | c = pat_node_get_check(pat, rn); |
| 3107 | if (c <= c0) { |
| 3108 | uint32_t rn_key_length = pat_node_get_key_length(pat, rn); |
| 3109 | if (rn_key_length <= key_size) { |
| 3110 | uint8_t *p = pat_node_get_key(ctx, pat, rn); |
| 3111 | if (!p) { |
| 3112 | break; |
| 3113 | } |
| 3114 | if (!memcmp(p, key, rn_key_length)) { |
| 3115 | return r; |
| 3116 | } |
| 3117 | } |
| 3118 | break; |
| 3119 | } |
| 3120 | if (len <= c) { |
| 3121 | break; |
| 3122 | } |
| 3123 | if (PAT_CHECK_IS_TERMINATED(c)) { |
| 3124 | uint8_t *p; |
| 3125 | pat_node_common *rn0; |
| 3126 | grn_id r0 = pat_node_get_left(pat, rn); |
| 3127 | PAT_AT(pat, r0, rn0); |
| 3128 | if (!rn0) { |
| 3129 | break; /* corrupt? */ |
| 3130 | } |
| 3131 | p = pat_node_get_key(ctx, pat, rn0); |
| 3132 | if (!p) { |
| 3133 | break; |
| 3134 | } |
| 3135 | uint32_t rn0_key_length = pat_node_get_key_length(pat, rn0); |
| 3136 | if (rn0_key_length <= key_size && !memcmp(p, key, rn0_key_length)) { |
| 3137 | r2 = r0; |
| 3138 | } |
| 3139 | } |
| 3140 | r = *grn_pat_next_location(ctx, pat, rn, key, c, len); |
| 3141 | c0 = c; |
| 3142 | } |
| 3143 | return r2; |
| 3144 | } |
| 3145 | |
| 3146 | typedef struct { |
| 3147 | grn_id id; |
| 3148 | uint16_t distance; |
| 3149 | } fuzzy_heap_node; |
| 3150 | |
| 3151 | typedef struct { |
| 3152 | uint32_t n_entries; |
| 3153 | uint32_t limit; |
| 3154 | fuzzy_heap_node *nodes; |
| 3155 | } fuzzy_heap; |
| 3156 | |
| 3157 | static inline fuzzy_heap * |
| 3158 | fuzzy_heap_open(grn_ctx *ctx, uint32_t max) |
| 3159 | { |
| 3160 | fuzzy_heap *h = GRN_CALLOC(sizeof(fuzzy_heap)); |
| 3161 | if (!h) { |
| 3162 | return NULL; |
| 3163 | } |
| 3164 | h->nodes = GRN_MALLOC(sizeof(fuzzy_heap_node) * max); |
| 3165 | if (!h->nodes) { |
| 3166 | GRN_FREE(h); |
| 3167 | return NULL; |
| 3168 | } |
| 3169 | h->n_entries = 0; |
| 3170 | h->limit = max; |
| 3171 | return h; |
| 3172 | } |
| 3173 | |
| 3174 | static inline bool |
| 3175 | fuzzy_heap_push(grn_ctx *ctx, fuzzy_heap *h, grn_id id, uint16_t distance) |
| 3176 | { |
| 3177 | int n, n2; |
| 3178 | fuzzy_heap_node node = {id, distance}; |
| 3179 | fuzzy_heap_node node2; |
| 3180 | if (h->n_entries >= h->limit) { |
| 3181 | uint32_t max = h->limit * 2; |
| 3182 | fuzzy_heap_node *nodes = GRN_REALLOC(h->nodes, sizeof(fuzzy_heap) * max); |
| 3183 | if (!h) { |
| 3184 | return false; |
| 3185 | } |
| 3186 | h->limit = max; |
| 3187 | h->nodes = nodes; |
| 3188 | } |
| 3189 | h->nodes[h->n_entries] = node; |
| 3190 | n = h->n_entries++; |
| 3191 | while (n) { |
| 3192 | n2 = (n - 1) >> 1; |
| 3193 | if (h->nodes[n2].distance <= h->nodes[n].distance) { |
| 3194 | break; |
| 3195 | } |
| 3196 | node2 = h->nodes[n]; |
| 3197 | h->nodes[n] = h->nodes[n2]; |
| 3198 | h->nodes[n2] = node2; |
| 3199 | n = n2; |
| 3200 | } |
| 3201 | return true; |
| 3202 | } |
| 3203 | |
| 3204 | static inline void |
| 3205 | fuzzy_heap_close(grn_ctx *ctx, fuzzy_heap *h) |
| 3206 | { |
| 3207 | GRN_FREE(h->nodes); |
| 3208 | GRN_FREE(h); |
| 3209 | } |
| 3210 | |
| 3211 | typedef struct { |
| 3212 | grn_pat *pat; |
| 3213 | const char *key; |
| 3214 | uint32_t key_size; |
| 3215 | uint16_t *dists; |
| 3216 | uint32_t x_length; |
| 3217 | uint32_t flags; |
| 3218 | uint32_t max_distance; |
| 3219 | uint32_t max_expansions; |
| 3220 | uint32_t prefix_match_size; |
| 3221 | fuzzy_heap *heap; |
| 3222 | struct { |
| 3223 | const char *key; |
| 3224 | uint32_t key_length; |
| 3225 | bool can_transition; |
| 3226 | } last_node; |
| 3227 | } fuzzy_search_data; |
| 3228 | |
| 3229 | static grn_id |
| 3230 | grn_pat_fuzzy_search_find_prefixed_start_node_id(grn_ctx *ctx, |
| 3231 | fuzzy_search_data *data) |
| 3232 | { |
| 3233 | int32_t c0 = -1, c; |
| 3234 | const uint8_t *k; |
| 3235 | int32_t len = data->prefix_match_size * 16; |
| 3236 | grn_id r; |
| 3237 | pat_node_common *node; |
| 3238 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 3239 | |
| 3240 | KEY_ENCODE(data->pat, keybuf, data->key, data->prefix_match_size); |
| 3241 | PAT_AT(data->pat, GRN_ID_NIL, node); |
| 3242 | r = pat_node_get_right(data->pat, node); |
| 3243 | while (r) { |
| 3244 | PAT_AT(data->pat, r, node); |
| 3245 | if (!node) { |
| 3246 | return GRN_ID_NIL; |
| 3247 | } |
| 3248 | c = pat_node_get_check(data->pat, node); |
| 3249 | if (c0 < c && c < len - 1) { |
| 3250 | r = *grn_pat_next_location(ctx, data->pat, node, data->key, c, len); |
| 3251 | c0 = c; |
| 3252 | continue; |
| 3253 | } |
| 3254 | if (!(k = pat_node_get_key(ctx, data->pat, node))) { |
| 3255 | break; |
| 3256 | } |
| 3257 | if (pat_node_get_key_length(data->pat, node) < data->prefix_match_size) { |
| 3258 | break; |
| 3259 | } |
| 3260 | if (memcmp(k, data->key, data->prefix_match_size) == 0) { |
| 3261 | return r; |
| 3262 | } |
| 3263 | break; |
| 3264 | } |
| 3265 | return GRN_ID_NIL; |
| 3266 | } |
| 3267 | |
| 3268 | #define DIST(data, ox, oy) \ |
| 3269 | ((data)->dists[(((data)->x_length + 1) * (oy)) + (ox)]) |
| 3270 | |
| 3271 | static inline uint16_t |
| 3272 | grn_pat_fuzzy_search_calc_edit_distance(grn_ctx *ctx, |
| 3273 | fuzzy_search_data *data, |
| 3274 | const char *sy, |
| 3275 | const char *ey, |
| 3276 | uint32_t offset) |
| 3277 | { |
| 3278 | const char *sx = data->key; |
| 3279 | const char *ex = sx + data->key_size; |
| 3280 | uint32_t cx, cy, x, y; |
| 3281 | const char *px, *py; |
| 3282 | const char *px_previous; |
| 3283 | const char *py_previous; |
| 3284 | |
| 3285 | /* |
| 3286 | * Continue from grn_pat_fuzzy_search(). |
| 3287 | * |
| 3288 | * Skip already calculated rows. |
| 3289 | * |
| 3290 | * If the previous y is "data" and the new y is "day" and offset is |
| 3291 | * 2, we can reuse the current "da" result. |
| 3292 | * |
| 3293 | * Current dists: |
| 3294 | * | | | d | a | t | e | |
| 3295 | * | | 0 | 1 | 2 | 3 | 4 | |
| 3296 | * | d | 1 | 0 | 1 | 2 | 3 | |
| 3297 | * | a | 2 | 1 | 0 | 1 | 2 | |
| 3298 | * | t | 3 | 2 | 1 | 0 | 1 | |
| 3299 | * | a | 4 | 3 | 2 | 1 | 1 | |
| 3300 | * | ? | 5 | ? | ? | ? | ? | |
| 3301 | * | ? | 6 | ? | ? | ? | ? | |
| 3302 | * | ? | 7 | ? | ? | ? | ? | |
| 3303 | * |
| 3304 | * Reused dists: |
| 3305 | * | | | d | a | t | e | |
| 3306 | * | | 0 | 1 | 2 | 3 | 4 | |
| 3307 | * | d | 1 | 0 | 1 | 2 | 3 | |
| 3308 | * | a | 2 | 1 | 0 | 1 | 2 | |
| 3309 | * | y | 3 | ? | ? | ? | ? | |
| 3310 | * | ? | 4 | ? | ? | ? | ? | |
| 3311 | * | ? | 5 | ? | ? | ? | ? | |
| 3312 | * | ? | 6 | ? | ? | ? | ? | |
| 3313 | * | ? | 7 | ? | ? | ? | ? | |
| 3314 | */ |
| 3315 | for (py_previous = NULL, py = sy, y = 1; |
| 3316 | py < ey && (cy = grn_charlen(ctx, py, ey)); |
| 3317 | py_previous = py, py += cy, y++) { |
| 3318 | if ((uint32_t)(py - sy) >= offset) { |
| 3319 | break; |
| 3320 | } |
| 3321 | } |
| 3322 | /* |
| 3323 | * Continue from grn_pat_fuzzy_search(). |
| 3324 | * |
| 3325 | * y: data |
| 3326 | * |
| 3327 | * 1: d |
| 3328 | * | | | d | a | t | e | |
| 3329 | * | | 0 | 1 | 2 | 3 | 4 | |
| 3330 | * | d | 1 | 0 | 1 | 2 | 3 | |
| 3331 | * | ? | 2 | ? | ? | ? | ? | |
| 3332 | * | ? | 3 | ? | ? | ? | ? | |
| 3333 | * | ? | 4 | ? | ? | ? | ? | |
| 3334 | * | ? | 5 | ? | ? | ? | ? | |
| 3335 | * | ? | 6 | ? | ? | ? | ? | |
| 3336 | * | ? | 7 | ? | ? | ? | ? | |
| 3337 | * |
| 3338 | * 2: a |
| 3339 | * | | | d | a | t | e | |
| 3340 | * | | 0 | 1 | 2 | 3 | 4 | |
| 3341 | * | d | 1 | 0 | 1 | 2 | 3 | |
| 3342 | * | a | 2 | 1 | 0 | 1 | 3 | |
| 3343 | * | t | 3 | ? | ? | ? | ? | |
| 3344 | * | a | 4 | ? | ? | ? | ? | |
| 3345 | * | ? | 5 | ? | ? | ? | ? | |
| 3346 | * | ? | 6 | ? | ? | ? | ? | |
| 3347 | * | ? | 7 | ? | ? | ? | ? | |
| 3348 | * |
| 3349 | * 3: t |
| 3350 | * | | | d | a | t | e | |
| 3351 | * | | 0 | 1 | 2 | 3 | 4 | |
| 3352 | * | d | 1 | 0 | 1 | 2 | 3 | |
| 3353 | * | a | 2 | 1 | 0 | 1 | 3 | |
| 3354 | * | t | 3 | 2 | 1 | 0 | 1 | |
| 3355 | * | a | 4 | ? | ? | ? | ? | |
| 3356 | * | ? | 5 | ? | ? | ? | ? | |
| 3357 | * | ? | 6 | ? | ? | ? | ? | |
| 3358 | * | ? | 7 | ? | ? | ? | ? | |
| 3359 | * |
| 3360 | * 4: a |
| 3361 | * | | | d | a | t | e | |
| 3362 | * | | 0 | 1 | 2 | 3 | 4 | |
| 3363 | * | d | 1 | 0 | 1 | 2 | 3 | |
| 3364 | * | a | 2 | 1 | 0 | 1 | 2 | |
| 3365 | * | t | 3 | 2 | 1 | 0 | 1 | |
| 3366 | * | a | 4 | 3 | 2 | 1 | 1 | |
| 3367 | * | ? | 5 | ? | ? | ? | ? | |
| 3368 | * | ? | 6 | ? | ? | ? | ? | |
| 3369 | * | ? | 7 | ? | ? | ? | ? | |
| 3370 | */ |
| 3371 | for (; py < ey && (cy = grn_charlen(ctx, py, ey)); |
| 3372 | py_previous = py, py += cy, y++) { |
| 3373 | /* children nodes will be no longer smaller than max distance |
| 3374 | * with only insertion costs. |
| 3375 | * This is end of row on allocated memory. */ |
| 3376 | if (y > data->x_length + data->max_distance) { |
| 3377 | data->last_node.can_transition = false; |
| 3378 | return data->max_distance + 1; |
| 3379 | } |
| 3380 | |
| 3381 | for (px_previous = NULL, px = sx, x = 1; |
| 3382 | px < ex && (cx = grn_charlen(ctx, px, ex)); |
| 3383 | px_previous = px, px += cx, x++) { |
| 3384 | if (cx == cy && !memcmp(px, py, cx)) { |
| 3385 | /* Use the upper left cell value as-is when the target |
| 3386 | * characters equal. */ |
| 3387 | DIST(data, x, y) = DIST(data, x - 1, y - 1); |
| 3388 | } else { |
| 3389 | uint32_t a, b, c; |
| 3390 | a = DIST(data, x - 1, y) + 1; |
| 3391 | b = DIST(data, x, y - 1) + 1; |
| 3392 | c = DIST(data, x - 1, y - 1) + 1; |
| 3393 | /* Use the minimum value in a, b and c. */ |
| 3394 | DIST(data, x, y) = ((a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c)); |
| 3395 | if (data->flags & GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITION && |
| 3396 | /* have the previous x character */ |
| 3397 | x > 1 && |
| 3398 | /* have the previous y character */ |
| 3399 | y > 1 && |
| 3400 | /* the current x character and the previous y character have the |
| 3401 | same length */ |
| 3402 | cx == (py - py_previous) && |
| 3403 | /* the current x character and the previous y character are same. */ |
| 3404 | memcmp(px, py_previous, cx) == 0 && |
| 3405 | /* the previous x character and the current y character have the |
| 3406 | same length */ |
| 3407 | (px - px_previous) == cy && |
| 3408 | /* the previous x character and the current y character are same. */ |
| 3409 | memcmp(px_previous, py, cy) == 0) { |
| 3410 | /* Use 1 distance for transposition. */ |
| 3411 | uint32_t t = DIST(data, x - 2, y - 2) + 1; |
| 3412 | if (t < (DIST(data, x, y))) { |
| 3413 | DIST(data, x, y) = t; |
| 3414 | } |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | } |
| 3419 | if (data->x_length > 0) { |
| 3420 | /* If there is no cell which is smaller than equal to max distance on end of |
| 3421 | * row, children nodes will be no longer smaller than max distance */ |
| 3422 | data->last_node.can_transition = false; |
| 3423 | for (x = 1; x <= data->x_length; x++) { |
| 3424 | if (DIST(data, x, y - 1) <= data->max_distance) { |
| 3425 | data->last_node.can_transition = true; |
| 3426 | break; |
| 3427 | } |
| 3428 | } |
| 3429 | } |
| 3430 | /* The bottom right cell is the distance of this input. */ |
| 3431 | return DIST(data, data->x_length, y - 1); |
| 3432 | } |
| 3433 | |
| 3434 | /* id must not be GRN_ID_NIL. Caller must check it. */ |
| 3435 | static inline void |
| 3436 | grn_pat_fuzzy_search_recursive(grn_ctx *ctx, |
| 3437 | fuzzy_search_data *data, |
| 3438 | grn_id id, |
| 3439 | int last_check) |
| 3440 | { |
| 3441 | pat_node_common *node = NULL; |
| 3442 | int check; |
| 3443 | const char *k; |
| 3444 | |
| 3445 | PAT_AT(data->pat, id, node); |
| 3446 | if (!node) { |
| 3447 | return; |
| 3448 | } |
| 3449 | check = pat_node_get_check(data->pat, node); |
| 3450 | uint32_t len = pat_node_get_key_length(data->pat, node); |
| 3451 | k = pat_node_get_key(ctx, data->pat, node); |
| 3452 | |
| 3453 | /* There are sub nodes. */ |
| 3454 | if (check > last_check) { |
| 3455 | if (len >= data->last_node.key_length && |
| 3456 | !memcmp(k, data->last_node.key, data->last_node.key_length)) { |
| 3457 | if (!data->last_node.can_transition) { |
| 3458 | return; |
| 3459 | } |
| 3460 | } |
| 3461 | grn_id left_record_id = pat_node_get_left(data->pat, node); |
| 3462 | grn_id right_record_id = pat_node_get_right(data->pat, node); |
| 3463 | if (left_record_id != GRN_ID_NIL) { |
| 3464 | grn_pat_fuzzy_search_recursive(ctx, data, left_record_id, check); |
| 3465 | } |
| 3466 | if (right_record_id != GRN_ID_NIL) { |
| 3467 | grn_pat_fuzzy_search_recursive(ctx, data, right_record_id, check); |
| 3468 | } |
| 3469 | } else { |
| 3470 | if (data->prefix_match_size > 0) { |
| 3471 | if (len < data->prefix_match_size) { |
| 3472 | return; |
| 3473 | } |
| 3474 | if (memcmp(k, data->key, data->prefix_match_size) != 0) { |
| 3475 | return; |
| 3476 | } |
| 3477 | } |
| 3478 | /* This is an optimization to avoid re-calculated dists. See also |
| 3479 | * a comment in grn_pat_fuzzy_search_calc_edit_distance(). |
| 3480 | * |
| 3481 | * Set already calculated common prefix length. */ |
| 3482 | uint32_t offset = 0; |
| 3483 | if (len >= data->last_node.key_length && |
| 3484 | memcmp(k, data->last_node.key, data->last_node.key_length) == 0) { |
| 3485 | if (!data->last_node.can_transition) { |
| 3486 | return; |
| 3487 | } |
| 3488 | offset = data->last_node.key_length; |
| 3489 | } else { |
| 3490 | if (!data->last_node.can_transition) { |
| 3491 | data->last_node.can_transition = true; |
| 3492 | } |
| 3493 | if (data->last_node.key_length > 0) { |
| 3494 | const char *kp = k; |
| 3495 | const char *ke = k + len; |
| 3496 | const char *p = data->last_node.key; |
| 3497 | const char *e = data->last_node.key + data->last_node.key_length; |
| 3498 | int lp; |
| 3499 | for (; p < e && kp < ke && (lp = grn_charlen(ctx, p, e)); |
| 3500 | p += lp, kp += lp) { |
| 3501 | if (p + lp <= e && kp + lp <= ke && memcmp(p, kp, lp) != 0) { |
| 3502 | break; |
| 3503 | } |
| 3504 | } |
| 3505 | offset = kp - k; |
| 3506 | } |
| 3507 | } |
| 3508 | if (len > offset) { |
| 3509 | uint16_t distance; |
| 3510 | distance = |
| 3511 | grn_pat_fuzzy_search_calc_edit_distance(ctx, data, k, k + len, offset); |
| 3512 | if (distance <= data->max_distance) { |
| 3513 | fuzzy_heap_push(ctx, data->heap, id, distance); |
| 3514 | } |
| 3515 | } |
| 3516 | data->last_node.key = k; |
| 3517 | data->last_node.key_length = len; |
| 3518 | } |
| 3519 | return; |
| 3520 | } |
| 3521 | |
| 3522 | #define HEAP_SIZE 256 |
| 3523 | |
| 3524 | grn_rc |
| 3525 | grn_pat_fuzzy_search(grn_ctx *ctx, |
| 3526 | grn_pat *pat, |
| 3527 | const void *key, |
| 3528 | uint32_t key_size, |
| 3529 | grn_fuzzy_search_optarg *args, |
| 3530 | grn_hash *h) |
| 3531 | { |
| 3532 | pat_node_common *node; |
| 3533 | grn_id id; |
| 3534 | fuzzy_search_data data; |
| 3535 | uint32_t len, x, y, i; |
| 3536 | const char *s = key; |
| 3537 | const char *e = (const char *)key + key_size; |
| 3538 | grn_rc rc = grn_pat_error_if_truncated(ctx, pat); |
| 3539 | if (rc != GRN_SUCCESS) { |
| 3540 | return rc; |
| 3541 | } |
| 3542 | data.pat = pat; |
| 3543 | data.key = key; |
| 3544 | data.key_size = key_size; |
| 3545 | if (args) { |
| 3546 | data.flags = args->flags; |
| 3547 | if (data.flags & GRN_TABLE_FUZZY_SEARCH_USE_MAX_DISTANCE_RATIO) { |
| 3548 | size_t key_length = grn_str_len(ctx, |
| 3549 | data.key, |
| 3550 | data.key + data.key_size, |
| 3551 | ctx->encoding, |
| 3552 | NULL); |
| 3553 | data.max_distance = floorf(key_length * args->max_distance_ratio); |
| 3554 | } else { |
| 3555 | data.max_distance = args->max_distance; |
| 3556 | } |
| 3557 | data.max_expansions = args->max_expansion; |
| 3558 | if (data.flags & GRN_TABLE_FUZZY_SEARCH_USE_PREFIX_LENGTH) { |
| 3559 | const char *key_current = key; |
| 3560 | const char *key_end = key_current + key_size; |
| 3561 | uint32_t i; |
| 3562 | data.prefix_match_size = 0; |
| 3563 | for (i = 0; i < args->prefix_length; i++) { |
| 3564 | if (key_current >= key_end) { |
| 3565 | break; |
| 3566 | } |
| 3567 | int length = grn_charlen(ctx, key_current, key_end); |
| 3568 | if (length == 0) { |
| 3569 | break; |
| 3570 | } |
| 3571 | data.prefix_match_size += length; |
| 3572 | key_current += length; |
| 3573 | } |
| 3574 | } else { |
| 3575 | data.prefix_match_size = args->prefix_match_size; |
| 3576 | } |
| 3577 | } else { |
| 3578 | data.flags = 0; |
| 3579 | data.max_distance = 1; |
| 3580 | data.max_expansions = 0; |
| 3581 | data.prefix_match_size = 0; |
| 3582 | } |
| 3583 | if (data.max_distance == 0) { |
| 3584 | return GRN_END_OF_DATA; |
| 3585 | } |
| 3586 | if (data.key_size > GRN_TABLE_MAX_KEY_SIZE || |
| 3587 | data.max_distance > GRN_TABLE_MAX_KEY_SIZE || |
| 3588 | data.prefix_match_size > data.key_size) { |
| 3589 | return GRN_INVALID_ARGUMENT; |
| 3590 | } |
| 3591 | |
| 3592 | if (data.prefix_match_size > 0) { |
| 3593 | id = grn_pat_fuzzy_search_find_prefixed_start_node_id(ctx, &data); |
| 3594 | } else { |
| 3595 | PAT_AT(pat, GRN_ID_NIL, node); |
| 3596 | if (!node) { |
| 3597 | return ctx->rc; |
| 3598 | } |
| 3599 | id = pat_node_get_right(pat, node); |
| 3600 | } |
| 3601 | if (id == GRN_ID_NIL) { |
| 3602 | return GRN_END_OF_DATA; |
| 3603 | } |
| 3604 | |
| 3605 | data.heap = fuzzy_heap_open(ctx, HEAP_SIZE); |
| 3606 | if (!data.heap) { |
| 3607 | return GRN_NO_MEMORY_AVAILABLE; |
| 3608 | } |
| 3609 | |
| 3610 | for (data.x_length = 0; s < e && (len = grn_charlen(ctx, s, e)); s += len) { |
| 3611 | data.x_length++; |
| 3612 | } |
| 3613 | data.dists = |
| 3614 | GRN_MALLOC((data.x_length + 1) * (data.x_length + data.max_distance + 1) * |
| 3615 | sizeof(uint16_t)); |
| 3616 | if (!data.dists) { |
| 3617 | fuzzy_heap_close(ctx, data.heap); |
| 3618 | return GRN_NO_MEMORY_AVAILABLE; |
| 3619 | } |
| 3620 | |
| 3621 | /* |
| 3622 | * x: each character of the given key |
| 3623 | * x_length: the number of x in character |
| 3624 | * y: each character of a key in this pat |
| 3625 | * |
| 3626 | * Example: |
| 3627 | * |
| 3628 | * x: date |
| 3629 | * max_distance: 2 |
| 3630 | * |
| 3631 | * Initial: |
| 3632 | * | | | d | a | t | e | |
| 3633 | * | | 0 | 1 | 2 | 3 | 4 | |
| 3634 | * | ? | 1 | ? | ? | ? | ? | |
| 3635 | * | ? | 2 | ? | ? | ? | ? | |
| 3636 | * | ? | 3 | ? | ? | ? | ? | |
| 3637 | * | ? | 4 | ? | ? | ? | ? | |
| 3638 | * | ? | 5 | ? | ? | ? | ? | |
| 3639 | * | ? | 6 | ? | ? | ? | ? | |
| 3640 | * | ? | 7 | ? | ? | ? | ? | |
| 3641 | */ |
| 3642 | for (x = 0; x <= data.x_length; x++) { |
| 3643 | DIST(&data, x, 0) = x; |
| 3644 | } |
| 3645 | for (y = 0; y <= data.x_length + data.max_distance; y++) { |
| 3646 | DIST(&data, 0, y) = y; |
| 3647 | } |
| 3648 | |
| 3649 | data.last_node.key = NULL; |
| 3650 | data.last_node.key_length = 0; |
| 3651 | data.last_node.can_transition = true; |
| 3652 | /* |
| 3653 | * Fill dists and find matched IDs recursively. See |
| 3654 | * grn_pat_fuzzy_search_calc_edit_distance() how to fill dists. |
| 3655 | */ |
| 3656 | grn_pat_fuzzy_search_recursive(ctx, &data, id, -1); |
| 3657 | GRN_FREE(data.dists); |
| 3658 | uint32_t n_results = 0; |
| 3659 | for (i = 0; i < data.heap->n_entries; i++) { |
| 3660 | if (data.max_expansions > 0 && i >= data.max_expansions) { |
| 3661 | break; |
| 3662 | } |
| 3663 | if (DB_OBJ(h)->header.flags & GRN_OBJ_WITH_SUBREC) { |
| 3664 | grn_rset_recinfo *ri; |
| 3665 | if (grn_hash_add(ctx, |
| 3666 | h, |
| 3667 | &(data.heap->nodes[i].id), |
| 3668 | sizeof(grn_id), |
| 3669 | (void **)&ri, |
| 3670 | NULL)) { |
| 3671 | ri->score = data.max_distance - data.heap->nodes[i].distance + 1; |
| 3672 | } |
| 3673 | } else { |
| 3674 | grn_hash_add(ctx, |
| 3675 | h, |
| 3676 | &(data.heap->nodes[i].id), |
| 3677 | sizeof(grn_id), |
| 3678 | NULL, |
| 3679 | NULL); |
| 3680 | } |
| 3681 | n_results++; |
| 3682 | } |
| 3683 | fuzzy_heap_close(ctx, data.heap); |
| 3684 | if (n_results > 0) { |
| 3685 | return GRN_SUCCESS; |
| 3686 | } else { |
| 3687 | return GRN_END_OF_DATA; |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | typedef struct { |
| 3692 | grn_pat_delinfo *di; |
| 3693 | grn_id id; |
| 3694 | uint16_t check; |
| 3695 | uint16_t check0; |
| 3696 | pat_node_common *node; |
| 3697 | pat_node_common *node_previous; |
| 3698 | pat_node_common *node_otherside; |
| 3699 | grn_id otherside; |
| 3700 | grn_id *proot; |
| 3701 | grn_id *p; |
| 3702 | grn_id *p0; |
| 3703 | uint32_t n_garbages; |
| 3704 | uint32_t n_entries; |
| 3705 | } grn_pat_del_data; |
| 3706 | |
| 3707 | static inline grn_rc |
| 3708 | grn_pat_del_internal(grn_ctx *ctx, grn_pat *pat, grn_pat_del_data *data) |
| 3709 | { |
| 3710 | grn_pat_delinfo *di = data->di; |
| 3711 | grn_id id = data->id; |
| 3712 | uint16_t check = data->check; |
| 3713 | uint16_t check0 = data->check0; |
| 3714 | pat_node_common *node = data->node; |
| 3715 | pat_node_common *node_previous = data->node_previous; |
| 3716 | pat_node_common *node_otherside = data->node_otherside; |
| 3717 | grn_id otherside = data->otherside; |
| 3718 | grn_id *proot = data->proot; |
| 3719 | grn_id *p = data->p; |
| 3720 | grn_id *p0 = data->p0; |
| 3721 | if (node == node_previous) { |
| 3722 | /* The last transition (p) is a self-loop. */ |
| 3723 | di->stat = DL_PHASE2; |
| 3724 | di->d = id; |
| 3725 | if (otherside) { |
| 3726 | uint16_t node_otherside_check = pat_node_get_check(pat, node_otherside); |
| 3727 | if (check0 < node_otherside_check && node_otherside_check <= check) { |
| 3728 | /* To keep node_otherside as an output node, its check is set to |
| 3729 | * zero. */ |
| 3730 | if (!delinfo_search(pat, otherside, NULL)) { |
| 3731 | GRN_LOG(ctx, GRN_LOG_DEBUG, "no delinfo found %d", otherside); |
| 3732 | } |
| 3733 | pat_node_set_check(pat, node_otherside, 0); |
| 3734 | } |
| 3735 | if (proot == p0 && !pat_node_get_check(pat, node_otherside)) { |
| 3736 | /* |
| 3737 | * Update node_otherside->lr because the first node, |
| 3738 | * node_otherside becomes the new first |
| 3739 | * node, is not an output node even if its check is zero. |
| 3740 | */ |
| 3741 | const uint8_t *k = pat_node_get_key(ctx, pat, node_otherside); |
| 3742 | int direction = k ? (*k >> 7) : 1; |
| 3743 | pat_node_set_child(pat, node_otherside, direction, otherside); |
| 3744 | pat_node_set_child(pat, node_otherside, !direction, GRN_ID_NIL); |
| 3745 | } |
| 3746 | } |
| 3747 | *p0 = otherside; |
| 3748 | } else if ((pat_node_get_left(pat, node) == GRN_ID_NIL && |
| 3749 | pat_node_get_right(pat, node) == id) || |
| 3750 | (pat_node_get_right(pat, node) == GRN_ID_NIL && |
| 3751 | pat_node_get_left(pat, node) == id)) { |
| 3752 | /* The output node has only a disabled self-loop. */ |
| 3753 | di->stat = DL_PHASE2; |
| 3754 | di->d = id; |
| 3755 | *p = GRN_ID_NIL; |
| 3756 | } else { |
| 3757 | /* The last transition (p) is not a self-loop. */ |
| 3758 | grn_pat_delinfo *ldi = NULL, *ddi = NULL; |
| 3759 | if (pat_node_is_deleting(pat, node)) { |
| 3760 | ldi = delinfo_search(pat, id, NULL); |
| 3761 | } |
| 3762 | if (pat_node_is_deleting(pat, node_previous)) { |
| 3763 | ddi = delinfo_search(pat, *p0, NULL); |
| 3764 | } |
| 3765 | if (ldi) { |
| 3766 | pat_node_set_deleting_off(pat, node); |
| 3767 | di->stat = DL_PHASE2; |
| 3768 | if (ddi) { |
| 3769 | pat_node_set_deleting_off(pat, node_previous); |
| 3770 | ddi->stat = DL_PHASE2; |
| 3771 | if (ddi == ldi) { |
| 3772 | if (id != ddi->ld) { |
| 3773 | GRN_LOG(ctx, GRN_LOG_ERROR, "id(%u) != ddi->ld(%u)", id, ddi->ld); |
| 3774 | } |
| 3775 | di->d = id; |
| 3776 | } else { |
| 3777 | ldi->ld = ddi->ld; |
| 3778 | di->d = id; |
| 3779 | } |
| 3780 | } else { |
| 3781 | pat_node_set_deleting_on(pat, node_previous); |
| 3782 | ldi->ld = *p0; |
| 3783 | di->d = id; |
| 3784 | } |
| 3785 | } else { |
| 3786 | pat_node_set_deleting_on(pat, node); |
| 3787 | if (ddi) { |
| 3788 | if (ddi->d != *p0) { |
| 3789 | GRN_LOG(ctx, GRN_LOG_ERROR, "ddi->d(%d) != *p0(%d)", ddi->d, *p0); |
| 3790 | } |
| 3791 | pat_node_set_deleting_off(pat, node_previous); |
| 3792 | ddi->stat = DL_PHASE2; |
| 3793 | di->stat = DL_PHASE1; |
| 3794 | di->ld = ddi->ld; |
| 3795 | di->d = id; |
| 3796 | } else { |
| 3797 | pat_node_set_deleting_on(pat, node_previous); |
| 3798 | di->stat = DL_PHASE1; |
| 3799 | di->ld = *p0; |
| 3800 | di->d = id; |
| 3801 | } |
| 3802 | } |
| 3803 | if (*p0 == otherside) { |
| 3804 | /* The previous node (*p0) has a self-loop (node_previous == |
| 3805 | * node_otherside). */ |
| 3806 | pat_node_set_check(pat, node_otherside, 0); |
| 3807 | if (proot == p0) { |
| 3808 | /* |
| 3809 | * Update node_otherside->lr because the first node, |
| 3810 | * node_otherside becomes the new first |
| 3811 | * node, is not an output node even if its check is zero. |
| 3812 | */ |
| 3813 | const uint8_t *k = pat_node_get_key(ctx, pat, node_otherside); |
| 3814 | int direction = k ? (*k >> 7) : 1; |
| 3815 | pat_node_set_child(pat, node_otherside, direction, otherside); |
| 3816 | pat_node_set_child(pat, node_otherside, !direction, GRN_ID_NIL); |
| 3817 | } |
| 3818 | } else { |
| 3819 | if (otherside) { |
| 3820 | uint16_t node_otherside_check = pat_node_get_check(pat, node_otherside); |
| 3821 | if (check0 < node_otherside_check && node_otherside_check <= check) { |
| 3822 | /* To keep node_otherside as an output node, its check is set to |
| 3823 | * zero. */ |
| 3824 | if (!delinfo_search(pat, otherside, NULL)) { |
| 3825 | GRN_LOG(ctx, GRN_LOG_ERROR, "no delinfo found %d", otherside); |
| 3826 | } |
| 3827 | pat_node_set_check(pat, node_otherside, 0); |
| 3828 | } |
| 3829 | if (proot == p0 && !pat_node_get_check(pat, node_otherside)) { |
| 3830 | /* |
| 3831 | * Update node_otherside->lr because the first node, |
| 3832 | * node_otherside becomes the new first |
| 3833 | * node, is not an output node even if its check is zero. |
| 3834 | */ |
| 3835 | const uint8_t *k = pat_node_get_key(ctx, pat, node_otherside); |
| 3836 | int direction = k ? (*k >> 7) : 1; |
| 3837 | pat_node_set_child(pat, node_otherside, direction, otherside); |
| 3838 | pat_node_set_child(pat, node_otherside, !direction, GRN_ID_NIL); |
| 3839 | } |
| 3840 | } |
| 3841 | *p0 = otherside; |
| 3842 | } |
| 3843 | } |
| 3844 | pat->header->n_garbages = data->n_garbages; |
| 3845 | pat->header->n_entries = data->n_entries; |
| 3846 | return GRN_SUCCESS; |
| 3847 | } |
| 3848 | |
| 3849 | static inline grn_rc |
| 3850 | _grn_pat_del(grn_ctx *ctx, |
| 3851 | grn_pat *pat, |
| 3852 | const char *key, |
| 3853 | uint32_t key_size, |
| 3854 | int shared, |
| 3855 | grn_table_delete_optarg *optarg) |
| 3856 | { |
| 3857 | pat_node_common *node, *node_previous = NULL, *node_otherside = NULL; |
| 3858 | int32_t node_check = -1, node_previous_check = -1, check; |
| 3859 | int32_t check_max = PAT_CHECK_PACK(key_size, 0, false); |
| 3860 | grn_id otherside, *proot, *p, *p0 = NULL; |
| 3861 | |
| 3862 | grn_pat_wal_add_entry_data wal_data = {0}; |
| 3863 | wal_data.pat = pat; |
| 3864 | wal_data.tag = "[pat][delete]"; |
| 3865 | wal_data.key = key; |
| 3866 | wal_data.key_size = key_size; |
| 3867 | wal_data.is_shared = shared; |
| 3868 | |
| 3869 | /* delinfo_new() must be called before searching for node. */ |
| 3870 | grn_pat_delinfo *di = delinfo_new(ctx, pat, &wal_data); |
| 3871 | if (!di) { |
| 3872 | return ctx->rc; |
| 3873 | } |
| 3874 | |
| 3875 | /* |
| 3876 | * Search a patricia tree for a given key. |
| 3877 | * If the key exists, get its output node. |
| 3878 | * |
| 3879 | * node, node_previous: the output node and its previous node. |
| 3880 | * node_otherside: the other side of node (the other destination |
| 3881 | * of node_previous). |
| 3882 | * node_check, node_previous_check: checks of node and its previous node. |
| 3883 | * p, p0: pointers to transitions (IDs) that refer to node and node_previous. |
| 3884 | * id, id0: ID of p and p0. |
| 3885 | */ |
| 3886 | grn_id id = GRN_ID_NIL; |
| 3887 | grn_id id0 = GRN_ID_NIL; |
| 3888 | PAT_AT(pat, id, node); |
| 3889 | proot = p = pat_node_get_right_address(pat, node); |
| 3890 | wal_data.record_direction = DIRECTION_RIGHT; |
| 3891 | grn_pat_wal_add_entry_data_set_record_direction(ctx, |
| 3892 | pat, |
| 3893 | &wal_data, |
| 3894 | id, |
| 3895 | node, |
| 3896 | p); |
| 3897 | for (;;) { |
| 3898 | grn_id next_id = *p; |
| 3899 | if (next_id == GRN_ID_NIL) { |
| 3900 | return GRN_INVALID_ARGUMENT; |
| 3901 | } |
| 3902 | id0 = id; |
| 3903 | id = next_id; |
| 3904 | PAT_AT(pat, id, node); |
| 3905 | if (!node) { |
| 3906 | return GRN_FILE_CORRUPT; |
| 3907 | } |
| 3908 | check = pat_node_get_check(pat, node); |
| 3909 | if (check_max <= check) { |
| 3910 | return GRN_INVALID_ARGUMENT; |
| 3911 | } |
| 3912 | if (node_check >= check) { |
| 3913 | /* Output node found. */ |
| 3914 | const uint8_t *k = pat_node_get_key(ctx, pat, node); |
| 3915 | if (!k) { |
| 3916 | return GRN_INVALID_ARGUMENT; |
| 3917 | } |
| 3918 | if (key_size != pat_node_get_key_length(pat, node) || |
| 3919 | memcmp(k, key, key_size)) { |
| 3920 | return GRN_INVALID_ARGUMENT; |
| 3921 | } |
| 3922 | /* Given key found. */ |
| 3923 | break; |
| 3924 | } |
| 3925 | node_previous_check = node_check; |
| 3926 | p0 = p; |
| 3927 | node_check = check; |
| 3928 | p = grn_pat_next_location(ctx, pat, node, key, node_check, check_max); |
| 3929 | grn_pat_wal_add_entry_data_set_record_direction(ctx, |
| 3930 | pat, |
| 3931 | &wal_data, |
| 3932 | id, |
| 3933 | node, |
| 3934 | p); |
| 3935 | node_previous = node; |
| 3936 | } |
| 3937 | if (optarg && optarg->func && |
| 3938 | !optarg->func(ctx, (grn_obj *)pat, id, optarg->func_arg)) { |
| 3939 | return GRN_SUCCESS; |
| 3940 | } |
| 3941 | grn_id node_previous_right = pat_node_get_right(pat, node_previous); |
| 3942 | grn_id node_previous_left = pat_node_get_left(pat, node_previous); |
| 3943 | if (node_previous_left == node_previous_right) { |
| 3944 | GRN_LOG(ctx, |
| 3945 | GRN_LOG_DEBUG, |
| 3946 | "*p0 (%d), node_previous->lr[0] == node_previous->lr[1] (%d)", |
| 3947 | *p0, |
| 3948 | node_previous_left); |
| 3949 | return GRN_FILE_CORRUPT; |
| 3950 | } |
| 3951 | otherside = |
| 3952 | (node_previous_right == id) ? node_previous_left : node_previous_right; |
| 3953 | if (otherside) { |
| 3954 | PAT_AT(pat, otherside, node_otherside); |
| 3955 | if (!node_otherside) { |
| 3956 | return GRN_FILE_CORRUPT; |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | if (pat->cache) { |
| 3961 | uint32_t cache_id; |
| 3962 | cache_id = grn_pat_cache_compute_id(ctx, pat, key, key_size); |
| 3963 | if (pat->cache[cache_id]) { |
| 3964 | pat->cache[cache_id] = GRN_ID_NIL; |
| 3965 | } |
| 3966 | } |
| 3967 | |
| 3968 | wal_data.event = GRN_WAL_EVENT_DELETE_ENTRY; |
| 3969 | wal_data.record_id = id; |
| 3970 | wal_data.check = node_check; |
| 3971 | wal_data.parent_check = node_previous_check; |
| 3972 | wal_data.otherside_record_id = otherside; |
| 3973 | if (node_otherside) { |
| 3974 | wal_data.otherside_check = pat_node_get_check(pat, node_otherside); |
| 3975 | } |
| 3976 | wal_data.left_record_id = pat_node_get_left(pat, node); |
| 3977 | wal_data.right_record_id = pat_node_get_right(pat, node); |
| 3978 | wal_data.n_garbages = pat->header->n_garbages + 1; |
| 3979 | wal_data.n_entries = pat->header->n_entries - 1; |
| 3980 | wal_data.delete_info_phase1_index = pat->header->curr_del; |
| 3981 | wal_data.delete_info_phase2_index = pat->header->curr_del2; |
| 3982 | if (grn_pat_wal_add_entry(ctx, &wal_data) != GRN_SUCCESS) { |
| 3983 | return ctx->rc; |
| 3984 | } |
| 3985 | |
| 3986 | grn_pat_del_data data = {0}; |
| 3987 | data.di = di; |
| 3988 | data.id = id; |
| 3989 | data.check = node_check; |
| 3990 | data.check0 = node_previous_check; |
| 3991 | data.node = node; |
| 3992 | data.node_previous = node_previous; |
| 3993 | data.node_otherside = node_otherside; |
| 3994 | data.otherside = otherside; |
| 3995 | data.proot = proot; |
| 3996 | data.p = p; |
| 3997 | data.p0 = p0; |
| 3998 | data.n_garbages = wal_data.n_garbages; |
| 3999 | data.n_entries = wal_data.n_entries; |
| 4000 | grn_rc rc = grn_pat_del_internal(ctx, pat, &data); |
| 4001 | if (rc == GRN_SUCCESS) { |
| 4002 | pat->header->wal_id = wal_data.wal_id; |
| 4003 | } |
| 4004 | return rc; |
| 4005 | } |
| 4006 | |
| 4007 | static grn_rc |
| 4008 | _grn_pat_delete(grn_ctx *ctx, |
| 4009 | grn_pat *pat, |
| 4010 | const void *key, |
| 4011 | uint32_t key_size, |
| 4012 | grn_table_delete_optarg *optarg) |
| 4013 | { |
| 4014 | if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { |
| 4015 | grn_id id = grn_pat_get(ctx, pat, key, key_size, NULL); |
| 4016 | if (id && grn_pat_delete_with_sis(ctx, pat, id, optarg)) { |
| 4017 | return GRN_SUCCESS; |
| 4018 | } |
| 4019 | return GRN_INVALID_ARGUMENT; |
| 4020 | } |
| 4021 | return _grn_pat_del(ctx, pat, key, key_size, 0, optarg); |
| 4022 | } |
| 4023 | |
| 4024 | grn_rc |
| 4025 | grn_pat_delete(grn_ctx *ctx, |
| 4026 | grn_pat *pat, |
| 4027 | const void *key, |
| 4028 | uint32_t key_size, |
| 4029 | grn_table_delete_optarg *optarg) |
| 4030 | { |
| 4031 | grn_rc rc; |
| 4032 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 4033 | if (!pat || !key || !key_size) { |
| 4034 | return GRN_INVALID_ARGUMENT; |
| 4035 | } |
| 4036 | rc = grn_pat_error_if_truncated(ctx, pat); |
| 4037 | if (rc != GRN_SUCCESS) { |
| 4038 | return rc; |
| 4039 | } |
| 4040 | KEY_ENCODE(pat, keybuf, key, key_size); |
| 4041 | return _grn_pat_delete(ctx, pat, key, key_size, optarg); |
| 4042 | } |
| 4043 | |
| 4044 | uint32_t |
| 4045 | grn_pat_size(grn_ctx *ctx, grn_pat *pat) |
| 4046 | { |
| 4047 | if (!pat) { |
| 4048 | ERR(GRN_INVALID_ARGUMENT, "[pat][size] pat is null"); |
| 4049 | return 0; |
| 4050 | } |
| 4051 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4052 | return 0; |
| 4053 | } |
| 4054 | return pat->header->n_entries; |
| 4055 | } |
| 4056 | |
| 4057 | const char * |
| 4058 | _grn_pat_key(grn_ctx *ctx, grn_pat *pat, grn_id id, uint32_t *key_size) |
| 4059 | { |
| 4060 | pat_node_common *node; |
| 4061 | uint8_t *key; |
| 4062 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4063 | *key_size = 0; |
| 4064 | return NULL; |
| 4065 | } |
| 4066 | if (id == GRN_ID_NIL) { |
| 4067 | *key_size = 0; |
| 4068 | return NULL; |
| 4069 | } |
| 4070 | PAT_AT(pat, id, node); |
| 4071 | if (!node) { |
| 4072 | *key_size = 0; |
| 4073 | return NULL; |
| 4074 | } |
| 4075 | key = pat_node_get_key(ctx, pat, node); |
| 4076 | if (key) { |
| 4077 | *key_size = pat_node_get_key_length(pat, node); |
| 4078 | } else { |
| 4079 | *key_size = 0; |
| 4080 | } |
| 4081 | return (const char *)key; |
| 4082 | } |
| 4083 | |
| 4084 | grn_rc |
| 4085 | grn_pat_delete_by_id(grn_ctx *ctx, |
| 4086 | grn_pat *pat, |
| 4087 | grn_id id, |
| 4088 | grn_table_delete_optarg *optarg) |
| 4089 | { |
| 4090 | grn_rc rc; |
| 4091 | if (!pat || !id) { |
| 4092 | return GRN_INVALID_ARGUMENT; |
| 4093 | } |
| 4094 | rc = grn_pat_error_if_truncated(ctx, pat); |
| 4095 | if (rc != GRN_SUCCESS) { |
| 4096 | return rc; |
| 4097 | } |
| 4098 | { |
| 4099 | uint32_t key_size; |
| 4100 | const char *key = _grn_pat_key(ctx, pat, id, &key_size); |
| 4101 | return _grn_pat_delete(ctx, pat, key, key_size, optarg); |
| 4102 | } |
| 4103 | } |
| 4104 | |
| 4105 | int |
| 4106 | grn_pat_get_key( |
| 4107 | grn_ctx *ctx, grn_pat *pat, grn_id id, void *keybuf, int bufsize) |
| 4108 | { |
| 4109 | int len; |
| 4110 | uint8_t *key; |
| 4111 | pat_node_common *node; |
| 4112 | if (!pat) { |
| 4113 | return 0; |
| 4114 | } |
| 4115 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4116 | return 0; |
| 4117 | } |
| 4118 | if (!id) { |
| 4119 | return 0; |
| 4120 | } |
| 4121 | PAT_AT(pat, id, node); |
| 4122 | if (!node) { |
| 4123 | return 0; |
| 4124 | } |
| 4125 | if (!(key = pat_node_get_key(ctx, pat, node))) { |
| 4126 | return 0; |
| 4127 | } |
| 4128 | len = pat_node_get_key_length(pat, node); |
| 4129 | if (keybuf && bufsize >= len) { |
| 4130 | if (KEY_NEEDS_CONVERT(pat, len)) { |
| 4131 | KEY_DEC(pat, keybuf, key, len); |
| 4132 | } else { |
| 4133 | grn_memcpy(keybuf, key, len); |
| 4134 | } |
| 4135 | } |
| 4136 | return len; |
| 4137 | } |
| 4138 | |
| 4139 | int |
| 4140 | grn_pat_get_key2(grn_ctx *ctx, grn_pat *pat, grn_id id, grn_obj *bulk) |
| 4141 | { |
| 4142 | uint32_t len; |
| 4143 | uint8_t *key; |
| 4144 | pat_node_common *node; |
| 4145 | if (!pat) { |
| 4146 | return GRN_INVALID_ARGUMENT; |
| 4147 | } |
| 4148 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4149 | return 0; |
| 4150 | } |
| 4151 | if (!id) { |
| 4152 | return 0; |
| 4153 | } |
| 4154 | PAT_AT(pat, id, node); |
| 4155 | if (!node) { |
| 4156 | return 0; |
| 4157 | } |
| 4158 | if (!(key = pat_node_get_key(ctx, pat, node))) { |
| 4159 | return 0; |
| 4160 | } |
| 4161 | len = pat_node_get_key_length(pat, node); |
| 4162 | if (KEY_NEEDS_CONVERT(pat, len)) { |
| 4163 | if (bulk->header.impl_flags & GRN_OBJ_REFER) { |
| 4164 | GRN_TEXT_INIT(bulk, 0); |
| 4165 | } |
| 4166 | if (!grn_bulk_reserve(ctx, bulk, len)) { |
| 4167 | char *curr = GRN_BULK_CURR(bulk); |
| 4168 | KEY_DEC(pat, curr, key, len); |
| 4169 | GRN_BULK_INCR_LEN(bulk, len); |
| 4170 | } |
| 4171 | } else { |
| 4172 | if (bulk->header.impl_flags & GRN_OBJ_REFER) { |
| 4173 | bulk->u.b.head = (char *)key; |
| 4174 | bulk->u.b.curr = (char *)key + len; |
| 4175 | } else { |
| 4176 | grn_bulk_write(ctx, bulk, (char *)key, len); |
| 4177 | } |
| 4178 | } |
| 4179 | return len; |
| 4180 | } |
| 4181 | |
| 4182 | int |
| 4183 | grn_pat_get_value(grn_ctx *ctx, grn_pat *pat, grn_id id, void *valuebuf) |
| 4184 | { |
| 4185 | int value_size; |
| 4186 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4187 | return 0; |
| 4188 | } |
| 4189 | value_size = (int)pat->value_size; |
| 4190 | if (value_size) { |
| 4191 | byte *v = (byte *)sis_at(ctx, pat, id); |
| 4192 | if (v) { |
| 4193 | if (valuebuf) { |
| 4194 | if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { |
| 4195 | grn_memcpy(valuebuf, v + sizeof(sis_node), value_size); |
| 4196 | } else { |
| 4197 | grn_memcpy(valuebuf, v, value_size); |
| 4198 | } |
| 4199 | } |
| 4200 | return value_size; |
| 4201 | } |
| 4202 | } |
| 4203 | return 0; |
| 4204 | } |
| 4205 | |
| 4206 | const char * |
| 4207 | grn_pat_get_value_(grn_ctx *ctx, grn_pat *pat, grn_id id, uint32_t *size) |
| 4208 | { |
| 4209 | const char *value = NULL; |
| 4210 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4211 | return NULL; |
| 4212 | } |
| 4213 | if ((*size = pat->value_size)) { |
| 4214 | if ((value = (const char *)sis_at(ctx, pat, id)) && |
| 4215 | (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS)) { |
| 4216 | value += sizeof(sis_node); |
| 4217 | } |
| 4218 | } |
| 4219 | return value; |
| 4220 | } |
| 4221 | |
| 4222 | grn_rc |
| 4223 | grn_pat_set_value( |
| 4224 | grn_ctx *ctx, grn_pat *pat, grn_id id, const void *value, int flags) |
| 4225 | { |
| 4226 | grn_rc rc = grn_pat_error_if_truncated(ctx, pat); |
| 4227 | if (rc != GRN_SUCCESS) { |
| 4228 | return rc; |
| 4229 | } |
| 4230 | if (value) { |
| 4231 | uint32_t value_size = pat->value_size; |
| 4232 | if (value_size) { |
| 4233 | byte *v = (byte *)sis_get(ctx, pat, id); |
| 4234 | if (v) { |
| 4235 | if (pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { |
| 4236 | v += sizeof(sis_node); |
| 4237 | } |
| 4238 | switch ((flags & GRN_OBJ_SET_MASK)) { |
| 4239 | case GRN_OBJ_SET: |
| 4240 | grn_memcpy(v, value, value_size); |
| 4241 | return GRN_SUCCESS; |
| 4242 | case GRN_OBJ_INCR: |
| 4243 | switch (value_size) { |
| 4244 | case sizeof(int32_t): |
| 4245 | *((int32_t *)v) += *((int32_t *)value); |
| 4246 | return GRN_SUCCESS; |
| 4247 | case sizeof(int64_t): |
| 4248 | *((int64_t *)v) += *((int64_t *)value); |
| 4249 | return GRN_SUCCESS; |
| 4250 | default: |
| 4251 | return GRN_INVALID_ARGUMENT; |
| 4252 | } |
| 4253 | break; |
| 4254 | case GRN_OBJ_DECR: |
| 4255 | switch (value_size) { |
| 4256 | case sizeof(int32_t): |
| 4257 | *((int32_t *)v) -= *((int32_t *)value); |
| 4258 | return GRN_SUCCESS; |
| 4259 | case sizeof(int64_t): |
| 4260 | *((int64_t *)v) -= *((int64_t *)value); |
| 4261 | return GRN_SUCCESS; |
| 4262 | default: |
| 4263 | return GRN_INVALID_ARGUMENT; |
| 4264 | } |
| 4265 | break; |
| 4266 | default: |
| 4267 | // todo : support other types. |
| 4268 | return GRN_INVALID_ARGUMENT; |
| 4269 | } |
| 4270 | } else { |
| 4271 | return GRN_NO_MEMORY_AVAILABLE; |
| 4272 | } |
| 4273 | } |
| 4274 | } |
| 4275 | return GRN_INVALID_ARGUMENT; |
| 4276 | } |
| 4277 | |
| 4278 | grn_rc |
| 4279 | grn_pat_info(grn_ctx *ctx, |
| 4280 | grn_pat *pat, |
| 4281 | int *key_size, |
| 4282 | unsigned int *flags, |
| 4283 | grn_encoding *encoding, |
| 4284 | unsigned int *n_entries, |
| 4285 | unsigned int *file_size) |
| 4286 | { |
| 4287 | grn_rc rc; |
| 4288 | ERRCLR(NULL); |
| 4289 | if (!pat) { |
| 4290 | return GRN_INVALID_ARGUMENT; |
| 4291 | } |
| 4292 | rc = grn_pat_error_if_truncated(ctx, pat); |
| 4293 | if (rc != GRN_SUCCESS) { |
| 4294 | return rc; |
| 4295 | } |
| 4296 | if (key_size) { |
| 4297 | *key_size = pat->key_size; |
| 4298 | } |
| 4299 | if (flags) { |
| 4300 | *flags = pat->obj.header.flags; |
| 4301 | } |
| 4302 | if (encoding) { |
| 4303 | *encoding = pat->encoding; |
| 4304 | } |
| 4305 | if (n_entries) { |
| 4306 | *n_entries = pat->header->n_entries; |
| 4307 | } |
| 4308 | if (file_size) { |
| 4309 | uint64_t tmp = 0; |
| 4310 | if ((rc = grn_io_size(ctx, pat->io, &tmp))) { |
| 4311 | return rc; |
| 4312 | } |
| 4313 | *file_size = (unsigned int)tmp; /* FIXME: inappropriate cast */ |
| 4314 | } |
| 4315 | return GRN_SUCCESS; |
| 4316 | } |
| 4317 | |
| 4318 | int |
| 4319 | grn_pat_delete_with_sis(grn_ctx *ctx, |
| 4320 | grn_pat *pat, |
| 4321 | grn_id id, |
| 4322 | grn_table_delete_optarg *optarg) |
| 4323 | { |
| 4324 | int level = 0, shared; |
| 4325 | const char *key = NULL, *_key; |
| 4326 | sis_node *sp, *ss = NULL, *si; |
| 4327 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4328 | return 0; |
| 4329 | } |
| 4330 | si = sis_at(ctx, pat, id); |
| 4331 | while (id) { |
| 4332 | pat_node_common *rn; |
| 4333 | uint32_t key_size; |
| 4334 | if ((si && si->children && si->children != id) || |
| 4335 | (optarg && optarg->func && |
| 4336 | !optarg->func(ctx, (grn_obj *)pat, id, optarg->func_arg))) { |
| 4337 | break; |
| 4338 | } |
| 4339 | PAT_AT(pat, id, rn); |
| 4340 | if (!(_key = (char *)pat_node_get_key(ctx, pat, rn))) { |
| 4341 | return 0; |
| 4342 | } |
| 4343 | if (_key == key) { |
| 4344 | shared = 1; |
| 4345 | } else { |
| 4346 | key = _key; |
| 4347 | shared = 0; |
| 4348 | } |
| 4349 | key_size = pat_node_get_key_length(pat, rn); |
| 4350 | if (key && key_size) { |
| 4351 | _grn_pat_del(ctx, pat, key, key_size, shared, NULL); |
| 4352 | } |
| 4353 | if (si) { |
| 4354 | grn_id *p, sid; |
| 4355 | uint32_t offset = 0; |
| 4356 | if ((*key & 0x80) && chop(ctx, pat, &key, key + key_size, &offset)) { |
| 4357 | if ((sid = grn_pat_get(ctx, pat, key, key_size - offset, NULL)) && |
| 4358 | (ss = sis_at(ctx, pat, sid))) { |
| 4359 | for (p = &ss->children; *p && *p != sid; p = &sp->sibling) { |
| 4360 | if (*p == id) { |
| 4361 | *p = si->sibling; |
| 4362 | break; |
| 4363 | } |
| 4364 | if (!(sp = sis_at(ctx, pat, *p))) { |
| 4365 | break; |
| 4366 | } |
| 4367 | } |
| 4368 | } |
| 4369 | } else { |
| 4370 | sid = GRN_ID_NIL; |
| 4371 | } |
| 4372 | si->sibling = 0; |
| 4373 | si->children = 0; |
| 4374 | id = sid; |
| 4375 | si = ss; |
| 4376 | } else { |
| 4377 | id = GRN_ID_NIL; |
| 4378 | } |
| 4379 | level++; |
| 4380 | } |
| 4381 | if (level) { |
| 4382 | uint32_t shared_key_offset = 0; |
| 4383 | while (id && key) { |
| 4384 | uint32_t key_size; |
| 4385 | if (_grn_pat_key(ctx, pat, id, &key_size) != key) { |
| 4386 | break; |
| 4387 | } |
| 4388 | { |
| 4389 | pat_node_common *rn; |
| 4390 | PAT_AT(pat, id, rn); |
| 4391 | if (!rn) { |
| 4392 | break; |
| 4393 | } |
| 4394 | if (shared_key_offset > 0) { |
| 4395 | pat_node_set_key_offset(pat, rn, shared_key_offset); |
| 4396 | } else { |
| 4397 | pat_node_set_key(ctx, pat, rn, (uint8_t *)key, key_size); |
| 4398 | shared_key_offset = pat_node_get_key_offset(pat, rn); |
| 4399 | } |
| 4400 | } |
| 4401 | { |
| 4402 | const char *end = key + key_size; |
| 4403 | if (!((*key & 0x80) && chop(ctx, pat, &key, end, &shared_key_offset))) { |
| 4404 | break; |
| 4405 | } |
| 4406 | id = grn_pat_get(ctx, pat, key, end - key, NULL); |
| 4407 | } |
| 4408 | } |
| 4409 | } |
| 4410 | return level; |
| 4411 | } |
| 4412 | |
| 4413 | grn_id |
| 4414 | grn_pat_next(grn_ctx *ctx, grn_pat *pat, grn_id id) |
| 4415 | { |
| 4416 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4417 | return GRN_ID_NIL; |
| 4418 | } |
| 4419 | while (++id <= pat->header->curr_rec) { |
| 4420 | uint32_t key_size; |
| 4421 | const char *key = _grn_pat_key(ctx, pat, id, &key_size); |
| 4422 | if (id == grn_pat_get(ctx, pat, key, key_size, NULL)) { |
| 4423 | return id; |
| 4424 | } |
| 4425 | } |
| 4426 | return GRN_ID_NIL; |
| 4427 | } |
| 4428 | |
| 4429 | grn_id |
| 4430 | grn_pat_at(grn_ctx *ctx, grn_pat *pat, grn_id id) |
| 4431 | { |
| 4432 | uint32_t key_size; |
| 4433 | const char *key = _grn_pat_key(ctx, pat, id, &key_size); |
| 4434 | if (key && (id == _grn_pat_get(ctx, pat, key, key_size, NULL))) { |
| 4435 | return id; |
| 4436 | } |
| 4437 | return GRN_ID_NIL; |
| 4438 | } |
| 4439 | |
| 4440 | grn_id |
| 4441 | grn_pat_curr_id(grn_ctx *ctx, grn_pat *pat) |
| 4442 | { |
| 4443 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4444 | return GRN_ID_NIL; |
| 4445 | } |
| 4446 | return pat->header->curr_rec; |
| 4447 | } |
| 4448 | |
| 4449 | int |
| 4450 | grn_pat_scan(grn_ctx *ctx, |
| 4451 | grn_pat *pat, |
| 4452 | const char *str, |
| 4453 | unsigned int str_len, |
| 4454 | grn_pat_scan_hit *sh, |
| 4455 | unsigned int sh_size, |
| 4456 | const char **rest) |
| 4457 | { |
| 4458 | int n = 0; |
| 4459 | grn_id tid; |
| 4460 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 4461 | return 0; |
| 4462 | } |
| 4463 | if (GRN_BULK_VSIZE(&(pat->normalizers)) > 0) { |
| 4464 | int flags = |
| 4465 | GRN_STRING_REMOVE_BLANK | GRN_STRING_WITH_TYPES | GRN_STRING_WITH_CHECKS; |
| 4466 | grn_obj *nstr = grn_string_open(ctx, str, str_len, (grn_obj *)pat, flags); |
| 4467 | if (nstr) { |
| 4468 | const short *cp = grn_string_get_checks(ctx, nstr); |
| 4469 | const unsigned char *tp = grn_string_get_types(ctx, nstr); |
| 4470 | unsigned int offset = 0, offset0 = 0; |
| 4471 | unsigned int normalized_length_in_bytes; |
| 4472 | const char *sp, *se; |
| 4473 | grn_string_get_normalized(ctx, |
| 4474 | nstr, |
| 4475 | &sp, |
| 4476 | &normalized_length_in_bytes, |
| 4477 | NULL); |
| 4478 | se = sp + normalized_length_in_bytes; |
| 4479 | while ((unsigned int)n < sh_size) { |
| 4480 | if ((tid = grn_pat_lcp_search(ctx, pat, sp, se - sp))) { |
| 4481 | const char *key; |
| 4482 | uint32_t len; |
| 4483 | int first_key_char_len; |
| 4484 | key = _grn_pat_key(ctx, pat, tid, &len); |
| 4485 | sh[n].id = tid; |
| 4486 | sh[n].offset = (*cp > 0) ? offset : offset0; |
| 4487 | first_key_char_len = grn_charlen(ctx, key, key + len); |
| 4488 | if (sh[n].offset > 0 && GRN_CHAR_IS_BLANK(tp[-1]) && |
| 4489 | ((first_key_char_len == 1 && key[0] != ' ') || |
| 4490 | first_key_char_len > 1)) { |
| 4491 | /* Remove leading spaces. */ |
| 4492 | const char *original_str = str + sh[n].offset; |
| 4493 | while (true) { |
| 4494 | int char_len = grn_charlen(ctx, original_str, str + str_len); |
| 4495 | if (char_len == 0) { |
| 4496 | break; |
| 4497 | } |
| 4498 | if (!grn_isspace(original_str, ctx->encoding)) { |
| 4499 | break; |
| 4500 | } |
| 4501 | original_str += char_len; |
| 4502 | sh[n].offset += char_len; |
| 4503 | } |
| 4504 | } |
| 4505 | { |
| 4506 | bool blank_in_alnum = false; |
| 4507 | const unsigned char *start_tp = tp; |
| 4508 | const unsigned char *blank_in_alnum_check_tp; |
| 4509 | while (len--) { |
| 4510 | if (*cp > 0) { |
| 4511 | offset0 = offset; |
| 4512 | offset += *cp; |
| 4513 | tp++; |
| 4514 | } |
| 4515 | sp++; |
| 4516 | cp++; |
| 4517 | } |
| 4518 | sh[n].length = offset - sh[n].offset; |
| 4519 | for (blank_in_alnum_check_tp = start_tp + 1; |
| 4520 | blank_in_alnum_check_tp < tp; |
| 4521 | blank_in_alnum_check_tp++) { |
| 4522 | #define GRN_CHAR_IS_ALNUM(char_type) \ |
| 4523 | (GRN_CHAR_TYPE(char_type) == GRN_CHAR_ALPHA || \ |
| 4524 | GRN_CHAR_TYPE(char_type) == GRN_CHAR_DIGIT) |
| 4525 | if (GRN_CHAR_IS_BLANK(blank_in_alnum_check_tp[-1]) && |
| 4526 | GRN_CHAR_IS_ALNUM(blank_in_alnum_check_tp[-1]) && |
| 4527 | GRN_CHAR_IS_ALNUM(blank_in_alnum_check_tp[0])) { |
| 4528 | blank_in_alnum = true; |
| 4529 | break; |
| 4530 | } |
| 4531 | #undef GRN_CHAR_IS_ALNUM |
| 4532 | } |
| 4533 | if (!blank_in_alnum) { |
| 4534 | n++; |
| 4535 | } |
| 4536 | } |
| 4537 | } else { |
| 4538 | if (*cp > 0) { |
| 4539 | offset0 = offset; |
| 4540 | offset += *cp; |
| 4541 | tp++; |
| 4542 | } |
| 4543 | do { |
| 4544 | sp++; |
| 4545 | cp++; |
| 4546 | } while (sp < se && !*cp); |
| 4547 | } |
| 4548 | if (se <= sp) { |
| 4549 | offset = str_len; |
| 4550 | break; |
| 4551 | } |
| 4552 | } |
| 4553 | if (rest) { |
| 4554 | grn_string_get_original(ctx, nstr, rest, NULL); |
| 4555 | *rest += offset; |
| 4556 | } |
| 4557 | grn_obj_close(ctx, nstr); |
| 4558 | } else { |
| 4559 | n = -1; |
| 4560 | if (rest) { |
| 4561 | *rest = str; |
| 4562 | } |
| 4563 | } |
| 4564 | } else { |
| 4565 | uint32_t len; |
| 4566 | const char *sp, *se = str + str_len; |
| 4567 | for (sp = str; sp < se && (unsigned int)n < sh_size; sp += len) { |
| 4568 | if ((tid = grn_pat_lcp_search(ctx, pat, sp, se - sp))) { |
| 4569 | _grn_pat_key(ctx, pat, tid, &len); |
| 4570 | sh[n].id = tid; |
| 4571 | sh[n].offset = sp - str; |
| 4572 | sh[n].length = len; |
| 4573 | n++; |
| 4574 | } else { |
| 4575 | len = grn_charlen(ctx, sp, se); |
| 4576 | } |
| 4577 | if (!len) { |
| 4578 | break; |
| 4579 | } |
| 4580 | } |
| 4581 | if (rest) { |
| 4582 | *rest = sp; |
| 4583 | } |
| 4584 | } |
| 4585 | return n; |
| 4586 | } |
| 4587 | |
| 4588 | #define INITIAL_SIZE 512 |
| 4589 | |
| 4590 | static inline void |
| 4591 | push(grn_pat_cursor *c, grn_id id, uint16_t check) |
| 4592 | { |
| 4593 | grn_ctx *ctx = c->ctx; |
| 4594 | grn_pat_cursor_entry *se; |
| 4595 | if (c->size <= c->sp) { |
| 4596 | if (c->ss) { |
| 4597 | uint32_t size = c->size * 4; |
| 4598 | grn_pat_cursor_entry *ss = |
| 4599 | GRN_REALLOC(c->ss, sizeof(grn_pat_cursor_entry) * size); |
| 4600 | GRN_LOG(ctx, |
| 4601 | GRN_LOG_DEBUG, |
| 4602 | "[pat][cursor][push][realloc] " |
| 4603 | "%p: %p -> %p: <%" GRN_FMT_INT32U ">", |
| 4604 | c, |
| 4605 | c->ss, |
| 4606 | ss, |
| 4607 | size); |
| 4608 | if (!ss) { |
| 4609 | return; /* give up */ |
| 4610 | } |
| 4611 | c->ss = ss; |
| 4612 | c->size = size; |
| 4613 | } else { |
| 4614 | uint32_t size = INITIAL_SIZE; |
| 4615 | if (!(c->ss = GRN_MALLOC(sizeof(grn_pat_cursor_entry) * size))) { |
| 4616 | return; /* give up */ |
| 4617 | } |
| 4618 | c->size = size; |
| 4619 | } |
| 4620 | } |
| 4621 | se = &c->ss[c->sp++]; |
| 4622 | se->id = id; |
| 4623 | se->check = check; |
| 4624 | } |
| 4625 | |
| 4626 | static inline grn_pat_cursor_entry * |
| 4627 | pop(grn_pat_cursor *c) |
| 4628 | { |
| 4629 | return c->sp ? &c->ss[--c->sp] : NULL; |
| 4630 | } |
| 4631 | |
| 4632 | static inline void |
| 4633 | grn_pat_cursor_push(grn_ctx *ctx, |
| 4634 | grn_pat_cursor *cursor, |
| 4635 | int order, |
| 4636 | grn_pat *pat, |
| 4637 | pat_node_common *node) |
| 4638 | { |
| 4639 | uint16_t check = pat_node_get_check(pat, node); |
| 4640 | if (order & GRN_CURSOR_DESCENDING) { |
| 4641 | push(cursor, pat_node_get_left(pat, node), check); |
| 4642 | push(cursor, pat_node_get_right(pat, node), check); |
| 4643 | } else { |
| 4644 | push(cursor, pat_node_get_right(pat, node), check); |
| 4645 | push(cursor, pat_node_get_left(pat, node), check); |
| 4646 | } |
| 4647 | } |
| 4648 | |
| 4649 | static grn_id |
| 4650 | grn_pat_cursor_next_by_id(grn_ctx *ctx, grn_pat_cursor *c) |
| 4651 | { |
| 4652 | grn_pat *pat = c->pat; |
| 4653 | int dir = (c->obj.header.flags & GRN_CURSOR_DESCENDING) ? -1 : 1; |
| 4654 | while (c->curr_rec != c->tail) { |
| 4655 | c->curr_rec += dir; |
| 4656 | if (pat->header->n_garbages) { |
| 4657 | uint32_t key_size; |
| 4658 | const void *key = _grn_pat_key(ctx, pat, c->curr_rec, &key_size); |
| 4659 | if (_grn_pat_get(ctx, pat, key, key_size, NULL) != c->curr_rec) { |
| 4660 | continue; |
| 4661 | } |
| 4662 | } |
| 4663 | c->rest--; |
| 4664 | return c->curr_rec; |
| 4665 | } |
| 4666 | return GRN_ID_NIL; |
| 4667 | } |
| 4668 | |
| 4669 | grn_id |
| 4670 | grn_pat_cursor_next(grn_ctx *ctx, grn_pat_cursor *c) |
| 4671 | { |
| 4672 | pat_node_common *node; |
| 4673 | grn_pat_cursor_entry *se; |
| 4674 | if (!c->rest) { |
| 4675 | return GRN_ID_NIL; |
| 4676 | } |
| 4677 | if ((c->obj.header.flags & GRN_CURSOR_BY_ID)) { |
| 4678 | return grn_pat_cursor_next_by_id(ctx, c); |
| 4679 | } |
| 4680 | while ((se = pop(c))) { |
| 4681 | grn_id id = se->id; |
| 4682 | int check = se->check, ch; |
| 4683 | while (id) { |
| 4684 | PAT_AT(c->pat, id, node); |
| 4685 | if (!node) { |
| 4686 | break; |
| 4687 | } |
| 4688 | ch = pat_node_get_check(c->pat, node); |
| 4689 | if (ch > check) { |
| 4690 | if (c->obj.header.flags & GRN_CURSOR_DESCENDING) { |
| 4691 | push(c, pat_node_get_left(c->pat, node), ch); |
| 4692 | id = pat_node_get_right(c->pat, node); |
| 4693 | } else { |
| 4694 | push(c, pat_node_get_right(c->pat, node), ch); |
| 4695 | id = pat_node_get_left(c->pat, node); |
| 4696 | } |
| 4697 | check = ch; |
| 4698 | continue; |
| 4699 | } else { |
| 4700 | if (id == c->tail) { |
| 4701 | c->sp = 0; |
| 4702 | } else { |
| 4703 | if (!c->curr_rec && c->tail) { |
| 4704 | uint32_t lmin, lmax; |
| 4705 | pat_node_common *nmin, *nmax; |
| 4706 | const uint8_t *kmin, *kmax; |
| 4707 | if (c->obj.header.flags & GRN_CURSOR_DESCENDING) { |
| 4708 | PAT_AT(c->pat, c->tail, nmin); |
| 4709 | PAT_AT(c->pat, id, nmax); |
| 4710 | } else { |
| 4711 | PAT_AT(c->pat, id, nmin); |
| 4712 | PAT_AT(c->pat, c->tail, nmax); |
| 4713 | } |
| 4714 | lmin = pat_node_get_key_length(c->pat, nmin); |
| 4715 | lmax = pat_node_get_key_length(c->pat, nmax); |
| 4716 | kmin = pat_node_get_key(ctx, c->pat, nmin); |
| 4717 | kmax = pat_node_get_key(ctx, c->pat, nmax); |
| 4718 | if ((lmin < lmax) ? (memcmp(kmin, kmax, lmin) > 0) |
| 4719 | : (memcmp(kmin, kmax, lmax) >= 0)) { |
| 4720 | c->sp = 0; |
| 4721 | break; |
| 4722 | } |
| 4723 | } |
| 4724 | } |
| 4725 | c->curr_rec = id; |
| 4726 | c->rest--; |
| 4727 | return id; |
| 4728 | } |
| 4729 | } |
| 4730 | } |
| 4731 | return GRN_ID_NIL; |
| 4732 | } |
| 4733 | |
| 4734 | void |
| 4735 | grn_pat_cursor_close(grn_ctx *ctx, grn_pat_cursor *c) |
| 4736 | { |
| 4737 | GRN_ASSERT(c->ctx == ctx); |
| 4738 | if (c->ss) { |
| 4739 | GRN_FREE(c->ss); |
| 4740 | } |
| 4741 | GRN_FREE(c); |
| 4742 | } |
| 4743 | |
| 4744 | static inline int |
| 4745 | bitcmp(const void *s1, const void *s2, int offset, int length) |
| 4746 | { |
| 4747 | int r, rest = length + (offset & 7) - 8, bl = offset >> 3, |
| 4748 | mask = 0xff >> (offset & 7); |
| 4749 | unsigned char *a = (unsigned char *)s1 + bl, *b = (unsigned char *)s2 + bl; |
| 4750 | if (rest <= 0) { |
| 4751 | mask &= 0xff << -rest; |
| 4752 | return (*a & mask) - (*b & mask); |
| 4753 | } |
| 4754 | if ((r = (*a & mask) - (*b & mask))) { |
| 4755 | return r; |
| 4756 | } |
| 4757 | a++; |
| 4758 | b++; |
| 4759 | if ((bl = rest >> 3)) { |
| 4760 | if ((r = memcmp(a, b, bl))) { |
| 4761 | return r; |
| 4762 | } |
| 4763 | a += bl; |
| 4764 | b += bl; |
| 4765 | } |
| 4766 | mask = 0xff << (8 - (rest & 7)); |
| 4767 | return (*a & mask) - (*b & mask); |
| 4768 | } |
| 4769 | |
| 4770 | static inline grn_rc |
| 4771 | set_cursor_prefix(grn_ctx *ctx, |
| 4772 | grn_pat *pat, |
| 4773 | grn_pat_cursor *c, |
| 4774 | const void *key, |
| 4775 | uint32_t key_size, |
| 4776 | int flags) |
| 4777 | { |
| 4778 | int32_t c0 = -1, ch; |
| 4779 | const uint8_t *k; |
| 4780 | int32_t len; |
| 4781 | uint32_t byte_len; |
| 4782 | grn_id id; |
| 4783 | pat_node_common *node; |
| 4784 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 4785 | if (flags & GRN_CURSOR_SIZE_BY_BIT) { |
| 4786 | len = key_size * 2; |
| 4787 | byte_len = key_size >> 3; |
| 4788 | } else { |
| 4789 | len = key_size * 16; |
| 4790 | byte_len = key_size; |
| 4791 | } |
| 4792 | KEY_ENCODE(pat, keybuf, key, byte_len); |
| 4793 | PAT_AT(pat, 0, node); |
| 4794 | id = pat_node_get_right(pat, node); |
| 4795 | while (id) { |
| 4796 | PAT_AT(pat, id, node); |
| 4797 | if (!node) { |
| 4798 | return GRN_FILE_CORRUPT; |
| 4799 | } |
| 4800 | ch = pat_node_get_check(pat, node); |
| 4801 | if (c0 < ch && ch < len - 1) { |
| 4802 | id = *grn_pat_next_location(ctx, pat, node, key, ch, len); |
| 4803 | c0 = ch; |
| 4804 | continue; |
| 4805 | } |
| 4806 | if (!(k = pat_node_get_key(ctx, pat, node))) { |
| 4807 | break; |
| 4808 | } |
| 4809 | if (pat_node_get_key_length(pat, node) < byte_len) { |
| 4810 | break; |
| 4811 | } |
| 4812 | if ((flags & GRN_CURSOR_SIZE_BY_BIT) ? !bitcmp(k, key, 0, key_size) |
| 4813 | : !memcmp(k, key, key_size)) { |
| 4814 | if (c0 < ch) { |
| 4815 | if (flags & GRN_CURSOR_DESCENDING) { |
| 4816 | if ((ch > len - 1) || !(flags & GRN_CURSOR_GT)) { |
| 4817 | push(c, pat_node_get_left(pat, node), ch); |
| 4818 | } |
| 4819 | push(c, pat_node_get_right(pat, node), ch); |
| 4820 | } else { |
| 4821 | push(c, pat_node_get_right(pat, node), ch); |
| 4822 | if ((ch > len - 1) || !(flags & GRN_CURSOR_GT)) { |
| 4823 | push(c, pat_node_get_left(pat, node), ch); |
| 4824 | } |
| 4825 | } |
| 4826 | } else { |
| 4827 | int32_t check_max = |
| 4828 | PAT_CHECK_PACK(pat_node_get_key_length(pat, node), 0, false); |
| 4829 | if (check_max > len || !(flags & GRN_CURSOR_GT)) { |
| 4830 | push(c, id, ch); |
| 4831 | } |
| 4832 | } |
| 4833 | } |
| 4834 | break; |
| 4835 | } |
| 4836 | return GRN_SUCCESS; |
| 4837 | } |
| 4838 | |
| 4839 | static inline grn_rc |
| 4840 | set_cursor_near(grn_ctx *ctx, |
| 4841 | grn_pat *pat, |
| 4842 | grn_pat_cursor *c, |
| 4843 | uint32_t min_size, |
| 4844 | const void *key, |
| 4845 | int flags) |
| 4846 | { |
| 4847 | grn_id id; |
| 4848 | pat_node_common *node; |
| 4849 | const uint8_t *k; |
| 4850 | int32_t r, check = -1, ch; |
| 4851 | int32_t min = min_size * 16; |
| 4852 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 4853 | KEY_ENCODE(pat, keybuf, key, pat->key_size); |
| 4854 | PAT_AT(pat, 0, node); |
| 4855 | for (id = pat_node_get_right(pat, node); id;) { |
| 4856 | PAT_AT(pat, id, node); |
| 4857 | if (!node) { |
| 4858 | return GRN_FILE_CORRUPT; |
| 4859 | } |
| 4860 | ch = pat_node_get_check(pat, node); |
| 4861 | if (ch <= check) { |
| 4862 | if (check >= min) { |
| 4863 | push(c, id, check); |
| 4864 | } |
| 4865 | break; |
| 4866 | } |
| 4867 | if ((check += 2) < ch) { |
| 4868 | if (!(k = pat_node_get_key(ctx, pat, node))) { |
| 4869 | return GRN_FILE_CORRUPT; |
| 4870 | } |
| 4871 | if ((r = bitcmp(key, k, check >> 1, (ch - check) >> 1))) { |
| 4872 | if (ch >= min) { |
| 4873 | push(c, pat_node_get_right(pat, node), ch); |
| 4874 | push(c, pat_node_get_left(pat, node), ch); |
| 4875 | } |
| 4876 | break; |
| 4877 | } |
| 4878 | } |
| 4879 | check = ch; |
| 4880 | if (nth_bit((uint8_t *)key, check)) { |
| 4881 | if (check >= min) { |
| 4882 | push(c, pat_node_get_left(pat, node), check); |
| 4883 | } |
| 4884 | id = pat_node_get_right(pat, node); |
| 4885 | } else { |
| 4886 | if (check >= min) { |
| 4887 | push(c, pat_node_get_right(pat, node), check); |
| 4888 | } |
| 4889 | id = pat_node_get_left(pat, node); |
| 4890 | } |
| 4891 | } |
| 4892 | return GRN_SUCCESS; |
| 4893 | } |
| 4894 | |
| 4895 | static inline grn_rc |
| 4896 | set_cursor_common_prefix(grn_ctx *ctx, |
| 4897 | grn_pat *pat, |
| 4898 | grn_pat_cursor *c, |
| 4899 | uint32_t min_size, |
| 4900 | const void *key, |
| 4901 | uint32_t key_size, |
| 4902 | int flags) |
| 4903 | { |
| 4904 | grn_id id; |
| 4905 | pat_node_common *node; |
| 4906 | const uint8_t *k; |
| 4907 | int32_t check = -1, ch; |
| 4908 | int32_t len = key_size * 16; |
| 4909 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 4910 | KEY_ENCODE(pat, keybuf, key, key_size); |
| 4911 | PAT_AT(pat, 0, node); |
| 4912 | for (id = pat_node_get_right(pat, node); id;) { |
| 4913 | PAT_AT(pat, id, node); |
| 4914 | if (!node) { |
| 4915 | return GRN_FILE_CORRUPT; |
| 4916 | } |
| 4917 | ch = pat_node_get_check(pat, node); |
| 4918 | if (ch <= check) { |
| 4919 | if (!(k = pat_node_get_key(ctx, pat, node))) { |
| 4920 | return GRN_FILE_CORRUPT; |
| 4921 | } |
| 4922 | { |
| 4923 | uint32_t l = pat_node_get_key_length(pat, node); |
| 4924 | if (min_size <= l && l <= key_size) { |
| 4925 | if (!memcmp(key, k, l)) { |
| 4926 | push(c, id, check); |
| 4927 | } |
| 4928 | } |
| 4929 | } |
| 4930 | break; |
| 4931 | } |
| 4932 | check = ch; |
| 4933 | if (len <= check) { |
| 4934 | break; |
| 4935 | } |
| 4936 | if (PAT_CHECK_IS_TERMINATED(check)) { |
| 4937 | grn_id id0 = pat_node_get_left(pat, node); |
| 4938 | pat_node_common *node0; |
| 4939 | PAT_AT(pat, id0, node0); |
| 4940 | if (!node0) { |
| 4941 | return GRN_FILE_CORRUPT; |
| 4942 | } |
| 4943 | if (!(k = pat_node_get_key(ctx, pat, node0))) { |
| 4944 | return GRN_FILE_CORRUPT; |
| 4945 | } |
| 4946 | { |
| 4947 | uint32_t l = pat_node_get_key_length(pat, node0); |
| 4948 | if (memcmp(key, k, l)) { |
| 4949 | break; |
| 4950 | } |
| 4951 | if (min_size <= l) { |
| 4952 | push(c, id0, check); |
| 4953 | } |
| 4954 | } |
| 4955 | id = pat_node_get_right(pat, node); |
| 4956 | } else { |
| 4957 | id = pat_node_get_child(pat, node, nth_bit((uint8_t *)key, check)); |
| 4958 | } |
| 4959 | } |
| 4960 | return GRN_SUCCESS; |
| 4961 | } |
| 4962 | |
| 4963 | static inline grn_rc |
| 4964 | set_cursor_ascend(grn_ctx *ctx, |
| 4965 | grn_pat *pat, |
| 4966 | grn_pat_cursor *c, |
| 4967 | const void *key, |
| 4968 | uint32_t key_size, |
| 4969 | int flags) |
| 4970 | { |
| 4971 | grn_id id; |
| 4972 | pat_node_common *node; |
| 4973 | const uint8_t *k; |
| 4974 | int32_t r, check = -1, ch, c2; |
| 4975 | int32_t len = key_size * 16; |
| 4976 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 4977 | KEY_ENCODE(pat, keybuf, key, key_size); |
| 4978 | PAT_AT(pat, 0, node); |
| 4979 | for (id = pat_node_get_right(pat, node); id;) { |
| 4980 | PAT_AT(pat, id, node); |
| 4981 | if (!node) { |
| 4982 | return GRN_FILE_CORRUPT; |
| 4983 | } |
| 4984 | ch = pat_node_get_check(pat, node); |
| 4985 | if (ch <= check) { |
| 4986 | if (!(k = pat_node_get_key(ctx, pat, node))) { |
| 4987 | return GRN_FILE_CORRUPT; |
| 4988 | } |
| 4989 | { |
| 4990 | uint32_t l = pat_node_get_key_length(pat, node); |
| 4991 | if (l == key_size) { |
| 4992 | if (flags & GRN_CURSOR_GT) { |
| 4993 | if (memcmp(key, k, l) < 0) { |
| 4994 | push(c, id, check); |
| 4995 | } |
| 4996 | } else { |
| 4997 | if (memcmp(key, k, l) <= 0) { |
| 4998 | push(c, id, check); |
| 4999 | } |
| 5000 | } |
| 5001 | } else if (l < key_size) { |
| 5002 | if (memcmp(key, k, l) < 0) { |
| 5003 | push(c, id, check); |
| 5004 | } |
| 5005 | } else { |
| 5006 | if (memcmp(key, k, key_size) <= 0) { |
| 5007 | push(c, id, check); |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | break; |
| 5012 | } |
| 5013 | c2 = len < ch ? len : ch; |
| 5014 | if ((check += 2) < c2) { |
| 5015 | if (!(k = pat_node_get_key(ctx, pat, node))) { |
| 5016 | return GRN_FILE_CORRUPT; |
| 5017 | } |
| 5018 | if ((r = bitcmp(key, k, check >> 1, ((c2 + 1) >> 1) - (check >> 1)))) { |
| 5019 | if (r < 0) { |
| 5020 | push(c, pat_node_get_right(pat, node), ch); |
| 5021 | push(c, pat_node_get_left(pat, node), ch); |
| 5022 | } |
| 5023 | break; |
| 5024 | } |
| 5025 | } |
| 5026 | check = ch; |
| 5027 | if (len <= check) { |
| 5028 | push(c, pat_node_get_right(pat, node), ch); |
| 5029 | push(c, pat_node_get_left(pat, node), ch); |
| 5030 | break; |
| 5031 | } |
| 5032 | if (PAT_CHECK_IS_TERMINATED(check)) { |
| 5033 | if (check + 1 < len) { |
| 5034 | id = pat_node_get_right(pat, node); |
| 5035 | } else { |
| 5036 | push(c, pat_node_get_right(pat, node), check); |
| 5037 | id = pat_node_get_left(pat, node); |
| 5038 | } |
| 5039 | } else { |
| 5040 | if (nth_bit((uint8_t *)key, check)) { |
| 5041 | id = pat_node_get_right(pat, node); |
| 5042 | } else { |
| 5043 | push(c, pat_node_get_right(pat, node), check); |
| 5044 | id = pat_node_get_left(pat, node); |
| 5045 | } |
| 5046 | } |
| 5047 | } |
| 5048 | return GRN_SUCCESS; |
| 5049 | } |
| 5050 | |
| 5051 | static inline grn_rc |
| 5052 | set_cursor_descend(grn_ctx *ctx, |
| 5053 | grn_pat *pat, |
| 5054 | grn_pat_cursor *c, |
| 5055 | const void *key, |
| 5056 | uint32_t key_size, |
| 5057 | int flags) |
| 5058 | { |
| 5059 | grn_id id; |
| 5060 | pat_node_common *node; |
| 5061 | const uint8_t *k; |
| 5062 | int32_t r, check = -1, ch, c2; |
| 5063 | int32_t len = key_size * 16; |
| 5064 | uint8_t keybuf[MAX_FIXED_KEY_SIZE]; |
| 5065 | KEY_ENCODE(pat, keybuf, key, key_size); |
| 5066 | PAT_AT(pat, 0, node); |
| 5067 | for (id = pat_node_get_right(pat, node); id;) { |
| 5068 | PAT_AT(pat, id, node); |
| 5069 | if (!node) { |
| 5070 | return GRN_FILE_CORRUPT; |
| 5071 | } |
| 5072 | ch = pat_node_get_check(pat, node); |
| 5073 | if (ch <= check) { |
| 5074 | if (!(k = pat_node_get_key(ctx, pat, node))) { |
| 5075 | return GRN_FILE_CORRUPT; |
| 5076 | } |
| 5077 | { |
| 5078 | uint32_t l = pat_node_get_key_length(pat, node); |
| 5079 | if (l <= key_size) { |
| 5080 | if ((flags & GRN_CURSOR_LT) && l == key_size) { |
| 5081 | if (memcmp(key, k, l) > 0) { |
| 5082 | push(c, id, check); |
| 5083 | } |
| 5084 | } else { |
| 5085 | if (memcmp(key, k, l) >= 0) { |
| 5086 | push(c, id, check); |
| 5087 | } |
| 5088 | } |
| 5089 | } else { |
| 5090 | if (memcmp(key, k, key_size) > 0) { |
| 5091 | push(c, id, check); |
| 5092 | } |
| 5093 | } |
| 5094 | } |
| 5095 | break; |
| 5096 | } |
| 5097 | c2 = len < ch ? len : ch; |
| 5098 | if ((check += 2) < c2) { |
| 5099 | if (!(k = pat_node_get_key(ctx, pat, node))) { |
| 5100 | return GRN_FILE_CORRUPT; |
| 5101 | } |
| 5102 | if ((r = bitcmp(key, k, check >> 1, ((c2 + 1) >> 1) - (check >> 1)))) { |
| 5103 | if (r >= 0) { |
| 5104 | push(c, pat_node_get_left(pat, node), ch); |
| 5105 | push(c, pat_node_get_right(pat, node), ch); |
| 5106 | } |
| 5107 | break; |
| 5108 | } |
| 5109 | } |
| 5110 | check = ch; |
| 5111 | if (len <= check) { |
| 5112 | break; |
| 5113 | } |
| 5114 | if (PAT_CHECK_IS_TERMINATED(check)) { |
| 5115 | if (check + 1 < len) { |
| 5116 | push(c, pat_node_get_left(pat, node), check); |
| 5117 | id = pat_node_get_right(pat, node); |
| 5118 | } else { |
| 5119 | id = pat_node_get_left(pat, node); |
| 5120 | } |
| 5121 | } else { |
| 5122 | if (nth_bit((uint8_t *)key, check)) { |
| 5123 | push(c, pat_node_get_left(pat, node), check); |
| 5124 | id = pat_node_get_right(pat, node); |
| 5125 | } else { |
| 5126 | id = pat_node_get_left(pat, node); |
| 5127 | } |
| 5128 | } |
| 5129 | } |
| 5130 | return GRN_SUCCESS; |
| 5131 | } |
| 5132 | |
| 5133 | static grn_pat_cursor * |
| 5134 | grn_pat_cursor_open_by_id(grn_ctx *ctx, |
| 5135 | grn_pat *pat, |
| 5136 | const void *min, |
| 5137 | uint32_t min_size, |
| 5138 | const void *max, |
| 5139 | uint32_t max_size, |
| 5140 | int offset, |
| 5141 | int limit, |
| 5142 | int flags) |
| 5143 | { |
| 5144 | int dir; |
| 5145 | grn_pat_cursor *c; |
| 5146 | if (!pat || !ctx) { |
| 5147 | return NULL; |
| 5148 | } |
| 5149 | if (!(c = GRN_CALLOC(sizeof(grn_pat_cursor)))) { |
| 5150 | return NULL; |
| 5151 | } |
| 5152 | GRN_DB_OBJ_SET_TYPE(c, GRN_CURSOR_TABLE_PAT_KEY); |
| 5153 | c->pat = pat; |
| 5154 | c->ctx = ctx; |
| 5155 | c->obj.header.flags = flags; |
| 5156 | c->obj.header.domain = GRN_ID_NIL; |
| 5157 | c->size = 0; |
| 5158 | c->sp = 0; |
| 5159 | c->ss = NULL; |
| 5160 | c->tail = 0; |
| 5161 | if (flags & GRN_CURSOR_DESCENDING) { |
| 5162 | dir = -1; |
| 5163 | if (max) { |
| 5164 | if (!(c->curr_rec = grn_pat_get(ctx, pat, max, max_size, NULL))) { |
| 5165 | c->tail = GRN_ID_NIL; |
| 5166 | goto exit; |
| 5167 | } |
| 5168 | if (!(flags & GRN_CURSOR_LT)) { |
| 5169 | c->curr_rec++; |
| 5170 | } |
| 5171 | } else { |
| 5172 | c->curr_rec = pat->header->curr_rec + 1; |
| 5173 | } |
| 5174 | if (min) { |
| 5175 | if (!(c->tail = grn_pat_get(ctx, pat, min, min_size, NULL))) { |
| 5176 | c->curr_rec = GRN_ID_NIL; |
| 5177 | goto exit; |
| 5178 | } |
| 5179 | if ((flags & GRN_CURSOR_GT)) { |
| 5180 | c->tail++; |
| 5181 | } |
| 5182 | } else { |
| 5183 | c->tail = GRN_ID_NIL + 1; |
| 5184 | } |
| 5185 | if (c->curr_rec < c->tail) { |
| 5186 | c->tail = c->curr_rec; |
| 5187 | } |
| 5188 | } else { |
| 5189 | dir = 1; |
| 5190 | if (min) { |
| 5191 | if (!(c->curr_rec = grn_pat_get(ctx, pat, min, min_size, NULL))) { |
| 5192 | c->tail = GRN_ID_NIL; |
| 5193 | goto exit; |
| 5194 | } |
| 5195 | if (!(flags & GRN_CURSOR_GT)) { |
| 5196 | c->curr_rec--; |
| 5197 | } |
| 5198 | } else { |
| 5199 | c->curr_rec = GRN_ID_NIL; |
| 5200 | } |
| 5201 | if (max) { |
| 5202 | if (!(c->tail = grn_pat_get(ctx, pat, max, max_size, NULL))) { |
| 5203 | c->curr_rec = GRN_ID_NIL; |
| 5204 | goto exit; |
| 5205 | } |
| 5206 | if ((flags & GRN_CURSOR_LT)) { |
| 5207 | c->tail--; |
| 5208 | } |
| 5209 | } else { |
| 5210 | c->tail = pat->header->curr_rec; |
| 5211 | } |
| 5212 | if (c->tail < c->curr_rec) { |
| 5213 | c->tail = c->curr_rec; |
| 5214 | } |
| 5215 | } |
| 5216 | if (pat->header->n_garbages) { |
| 5217 | while (offset && c->curr_rec != c->tail) { |
| 5218 | uint32_t key_size; |
| 5219 | const void *key; |
| 5220 | c->curr_rec += dir; |
| 5221 | key = _grn_pat_key(ctx, pat, c->curr_rec, &key_size); |
| 5222 | if (_grn_pat_get(ctx, pat, key, key_size, NULL) == c->curr_rec) { |
| 5223 | offset--; |
| 5224 | } |
| 5225 | } |
| 5226 | } else { |
| 5227 | if ((int)(dir * (c->tail - c->curr_rec)) < offset) { |
| 5228 | c->curr_rec = c->tail; |
| 5229 | } else { |
| 5230 | c->curr_rec += dir * offset; |
| 5231 | } |
| 5232 | } |
| 5233 | c->rest = (limit < 0) ? GRN_ID_MAX : limit; |
| 5234 | exit: |
| 5235 | return c; |
| 5236 | } |
| 5237 | |
| 5238 | static grn_rc |
| 5239 | set_cursor_rk(grn_ctx *ctx, |
| 5240 | grn_pat *pat, |
| 5241 | grn_pat_cursor *c, |
| 5242 | const void *key, |
| 5243 | uint32_t key_size, |
| 5244 | int flags); |
| 5245 | |
| 5246 | grn_pat_cursor * |
| 5247 | grn_pat_cursor_open(grn_ctx *ctx, |
| 5248 | grn_pat *pat, |
| 5249 | const void *min, |
| 5250 | uint32_t min_size, |
| 5251 | const void *max, |
| 5252 | uint32_t max_size, |
| 5253 | int offset, |
| 5254 | int limit, |
| 5255 | int flags) |
| 5256 | { |
| 5257 | grn_id id; |
| 5258 | pat_node_common *node; |
| 5259 | grn_pat_cursor *c; |
| 5260 | if (!pat || !ctx) { |
| 5261 | return NULL; |
| 5262 | } |
| 5263 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 5264 | return NULL; |
| 5265 | } |
| 5266 | if ((flags & GRN_CURSOR_BY_ID)) { |
| 5267 | return grn_pat_cursor_open_by_id(ctx, |
| 5268 | pat, |
| 5269 | min, |
| 5270 | min_size, |
| 5271 | max, |
| 5272 | max_size, |
| 5273 | offset, |
| 5274 | limit, |
| 5275 | flags); |
| 5276 | } |
| 5277 | if (!(c = GRN_CALLOC(sizeof(grn_pat_cursor)))) { |
| 5278 | return NULL; |
| 5279 | } |
| 5280 | GRN_DB_OBJ_SET_TYPE(c, GRN_CURSOR_TABLE_PAT_KEY); |
| 5281 | c->pat = pat; |
| 5282 | c->ctx = ctx; |
| 5283 | c->size = 0; |
| 5284 | c->sp = 0; |
| 5285 | c->ss = NULL; |
| 5286 | c->tail = 0; |
| 5287 | c->rest = GRN_ID_MAX; |
| 5288 | c->curr_rec = GRN_ID_NIL; |
| 5289 | c->obj.header.domain = GRN_ID_NIL; |
| 5290 | if (flags & GRN_CURSOR_PREFIX) { |
| 5291 | if (max && max_size) { |
| 5292 | if ((pat->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE)) { |
| 5293 | set_cursor_common_prefix(ctx, pat, c, min_size, max, max_size, flags); |
| 5294 | } else { |
| 5295 | set_cursor_near(ctx, pat, c, min_size, max, flags); |
| 5296 | } |
| 5297 | goto exit; |
| 5298 | } else { |
| 5299 | if (min && min_size) { |
| 5300 | if (flags & GRN_CURSOR_RK) { |
| 5301 | set_cursor_rk(ctx, pat, c, min, min_size, flags); |
| 5302 | } else { |
| 5303 | set_cursor_prefix(ctx, pat, c, min, min_size, flags); |
| 5304 | } |
| 5305 | goto exit; |
| 5306 | } |
| 5307 | } |
| 5308 | } |
| 5309 | if (flags & GRN_CURSOR_DESCENDING) { |
| 5310 | if (min && min_size) { |
| 5311 | set_cursor_ascend(ctx, pat, c, min, min_size, flags); |
| 5312 | c->obj.header.flags = GRN_CURSOR_ASCENDING; |
| 5313 | c->tail = grn_pat_cursor_next(ctx, c); |
| 5314 | c->sp = 0; |
| 5315 | if (!c->tail) { |
| 5316 | goto exit; |
| 5317 | } |
| 5318 | } |
| 5319 | if (max && max_size) { |
| 5320 | set_cursor_descend(ctx, pat, c, max, max_size, flags); |
| 5321 | } else { |
| 5322 | PAT_AT(pat, 0, node); |
| 5323 | if (!node) { |
| 5324 | grn_pat_cursor_close(ctx, c); |
| 5325 | return NULL; |
| 5326 | } |
| 5327 | if ((id = pat_node_get_right(pat, node))) { |
| 5328 | PAT_AT(pat, id, node); |
| 5329 | if (node) { |
| 5330 | grn_pat_cursor_push(ctx, c, GRN_CURSOR_DESCENDING, pat, node); |
| 5331 | } |
| 5332 | } |
| 5333 | } |
| 5334 | } else { |
| 5335 | if (max && max_size) { |
| 5336 | set_cursor_descend(ctx, pat, c, max, max_size, flags); |
| 5337 | c->obj.header.flags = GRN_CURSOR_DESCENDING; |
| 5338 | c->tail = grn_pat_cursor_next(ctx, c); |
| 5339 | c->sp = 0; |
| 5340 | if (!c->tail) { |
| 5341 | goto exit; |
| 5342 | } |
| 5343 | } |
| 5344 | if (min && min_size) { |
| 5345 | set_cursor_ascend(ctx, pat, c, min, min_size, flags); |
| 5346 | } else { |
| 5347 | PAT_AT(pat, 0, node); |
| 5348 | if (!node) { |
| 5349 | grn_pat_cursor_close(ctx, c); |
| 5350 | return NULL; |
| 5351 | } |
| 5352 | if ((id = pat_node_get_right(pat, node))) { |
| 5353 | PAT_AT(pat, id, node); |
| 5354 | if (node) { |
| 5355 | grn_pat_cursor_push(ctx, c, GRN_CURSOR_ASCENDING, pat, node); |
| 5356 | } |
| 5357 | } |
| 5358 | } |
| 5359 | } |
| 5360 | exit: |
| 5361 | c->obj.header.flags = flags; |
| 5362 | c->curr_rec = GRN_ID_NIL; |
| 5363 | while (offset--) { |
| 5364 | grn_pat_cursor_next(ctx, c); |
| 5365 | } |
| 5366 | c->rest = (limit < 0) ? GRN_ID_MAX : limit; |
| 5367 | return c; |
| 5368 | } |
| 5369 | |
| 5370 | int |
| 5371 | grn_pat_cursor_get_key(grn_ctx *ctx, grn_pat_cursor *c, void **key) |
| 5372 | { |
| 5373 | *key = c->curr_key; |
| 5374 | return grn_pat_get_key(ctx, |
| 5375 | c->pat, |
| 5376 | c->curr_rec, |
| 5377 | *key, |
| 5378 | GRN_TABLE_MAX_KEY_SIZE); |
| 5379 | } |
| 5380 | |
| 5381 | int |
| 5382 | grn_pat_cursor_get_value(grn_ctx *ctx, grn_pat_cursor *c, void **value) |
| 5383 | { |
| 5384 | int value_size = (int)c->pat->value_size; |
| 5385 | if (value_size) { |
| 5386 | byte *v = (byte *)sis_at(ctx, c->pat, c->curr_rec); |
| 5387 | if (v) { |
| 5388 | if (c->pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { |
| 5389 | *value = v + sizeof(sis_node); |
| 5390 | } else { |
| 5391 | *value = v; |
| 5392 | } |
| 5393 | } else { |
| 5394 | *value = NULL; |
| 5395 | } |
| 5396 | } |
| 5397 | return value_size; |
| 5398 | } |
| 5399 | |
| 5400 | int |
| 5401 | grn_pat_cursor_get_key_value( |
| 5402 | grn_ctx *ctx, grn_pat_cursor *c, void **key, uint32_t *key_size, void **value) |
| 5403 | { |
| 5404 | int value_size = (int)c->pat->value_size; |
| 5405 | if (key_size) { |
| 5406 | *key_size = (uint32_t)grn_pat_get_key(ctx, |
| 5407 | c->pat, |
| 5408 | c->curr_rec, |
| 5409 | c->curr_key, |
| 5410 | GRN_TABLE_MAX_KEY_SIZE); |
| 5411 | if (key) { |
| 5412 | *key = c->curr_key; |
| 5413 | } |
| 5414 | } |
| 5415 | if (value && value_size) { |
| 5416 | byte *v = (byte *)sis_at(ctx, c->pat, c->curr_rec); |
| 5417 | if (v) { |
| 5418 | if (c->pat->obj.header.flags & GRN_OBJ_KEY_WITH_SIS) { |
| 5419 | *value = v + sizeof(sis_node); |
| 5420 | } else { |
| 5421 | *value = v; |
| 5422 | } |
| 5423 | } else { |
| 5424 | *value = NULL; |
| 5425 | } |
| 5426 | } |
| 5427 | return value_size; |
| 5428 | } |
| 5429 | |
| 5430 | grn_rc |
| 5431 | grn_pat_cursor_set_value(grn_ctx *ctx, |
| 5432 | grn_pat_cursor *c, |
| 5433 | const void *value, |
| 5434 | int flags) |
| 5435 | { |
| 5436 | return grn_pat_set_value(ctx, c->pat, c->curr_rec, value, flags); |
| 5437 | } |
| 5438 | |
| 5439 | grn_rc |
| 5440 | grn_pat_cursor_delete(grn_ctx *ctx, |
| 5441 | grn_pat_cursor *c, |
| 5442 | grn_table_delete_optarg *optarg) |
| 5443 | { |
| 5444 | return grn_pat_delete_by_id(ctx, c->pat, c->curr_rec, optarg); |
| 5445 | } |
| 5446 | |
| 5447 | void |
| 5448 | grn_pat_check(grn_ctx *ctx, grn_pat *pat) |
| 5449 | { |
| 5450 | char buf[8]; |
| 5451 | struct grn_pat_header *h = pat->header; |
| 5452 | if (grn_pat_error_if_truncated(ctx, pat) != GRN_SUCCESS) { |
| 5453 | return; |
| 5454 | } |
| 5455 | GRN_OUTPUT_ARRAY_OPEN("RESULT", 1); |
| 5456 | GRN_OUTPUT_MAP_OPEN("SUMMARY", 23); |
| 5457 | GRN_OUTPUT_CSTR("flags"); |
| 5458 | grn_itoh(h->flags, buf, 8); |
| 5459 | GRN_OUTPUT_STR(buf, 8); |
| 5460 | GRN_OUTPUT_CSTR("key size"); |
| 5461 | GRN_OUTPUT_INT64(h->key_size); |
| 5462 | GRN_OUTPUT_CSTR("value_size"); |
| 5463 | GRN_OUTPUT_INT64(h->value_size); |
| 5464 | GRN_OUTPUT_CSTR("tokenizer"); |
| 5465 | GRN_OUTPUT_INT64(h->tokenizer); |
| 5466 | GRN_OUTPUT_CSTR("normalizer"); |
| 5467 | GRN_OUTPUT_INT64(h->normalizer); |
| 5468 | GRN_OUTPUT_CSTR("n_entries"); |
| 5469 | GRN_OUTPUT_INT64(h->n_entries); |
| 5470 | GRN_OUTPUT_CSTR("curr_rec"); |
| 5471 | GRN_OUTPUT_INT64(h->curr_rec); |
| 5472 | GRN_OUTPUT_CSTR("curr_key"); |
| 5473 | if (pat_is_key_large(pat)) { |
| 5474 | GRN_OUTPUT_UINT64(h->curr_key_large); |
| 5475 | } else { |
| 5476 | GRN_OUTPUT_INT64(h->curr_key); |
| 5477 | } |
| 5478 | GRN_OUTPUT_CSTR("curr_del"); |
| 5479 | GRN_OUTPUT_INT64(h->curr_del); |
| 5480 | GRN_OUTPUT_CSTR("curr_del2"); |
| 5481 | GRN_OUTPUT_INT64(h->curr_del2); |
| 5482 | GRN_OUTPUT_CSTR("curr_del3"); |
| 5483 | GRN_OUTPUT_INT64(h->curr_del3); |
| 5484 | GRN_OUTPUT_CSTR("n_garbages"); |
| 5485 | GRN_OUTPUT_INT64(h->n_garbages); |
| 5486 | GRN_OUTPUT_MAP_CLOSE(); |
| 5487 | GRN_OUTPUT_ARRAY_CLOSE(); |
| 5488 | } |
| 5489 | |
| 5490 | /* utilities */ |
| 5491 | void |
| 5492 | grn_p_pat_node(grn_ctx *ctx, grn_pat *pat, pat_node_common *node) |
| 5493 | { |
| 5494 | uint8_t *key = NULL; |
| 5495 | |
| 5496 | if (!node) { |
| 5497 | printf("#<pat_node:(null)>\n"); |
| 5498 | return; |
| 5499 | } |
| 5500 | |
| 5501 | if (pat_node_is_key_immediate(pat, node)) { |
| 5502 | key = pat_node_get_key(ctx, pat, node); |
| 5503 | } else { |
| 5504 | KEY_AT(pat, pat_node_get_key_offset(pat, node), key, 0); |
| 5505 | } |
| 5506 | |
| 5507 | uint16_t check = pat_node_get_check(pat, node); |
| 5508 | printf("#<pat_node:%p " |
| 5509 | "left:%u " |
| 5510 | "right:%u " |
| 5511 | "deleting:%s " |
| 5512 | "immediate:%s " |
| 5513 | "length:%u " |
| 5514 | "nth-byte:%u " |
| 5515 | "nth-bit:%u " |
| 5516 | "terminated:%s " |
| 5517 | "key:<%.*s>" |
| 5518 | ">\n", |
| 5519 | node, |
| 5520 | pat_node_get_left(pat, node), |
| 5521 | pat_node_get_right(pat, node), |
| 5522 | pat_node_is_deleting(pat, node) ? "true" : "false", |
| 5523 | pat_node_is_key_immediate(pat, node) ? "true" : "false", |
| 5524 | pat_node_get_key_length(pat, node), |
| 5525 | PAT_CHECK_BYTE_DIFFERENCES(check), |
| 5526 | PAT_CHECK_BIT_DIFFERENCES(check), |
| 5527 | PAT_CHECK_IS_TERMINATED(check) ? "true" : "false", |
| 5528 | pat_node_get_key_length(pat, node), |
| 5529 | (char *)key); |
| 5530 | } |
| 5531 | |
| 5532 | static void |
| 5533 | grn_pat_inspect_check(grn_ctx *ctx, grn_obj *buf, int check) |
| 5534 | { |
| 5535 | GRN_TEXT_PUTS(ctx, buf, "{"); |
| 5536 | grn_text_lltoa(ctx, buf, PAT_CHECK_BYTE_DIFFERENCES(check)); |
| 5537 | GRN_TEXT_PUTS(ctx, buf, ","); |
| 5538 | grn_text_lltoa(ctx, buf, PAT_CHECK_BIT_DIFFERENCES(check)); |
| 5539 | GRN_TEXT_PUTS(ctx, buf, ","); |
| 5540 | if (PAT_CHECK_IS_TERMINATED(check)) { |
| 5541 | GRN_TEXT_PUTS(ctx, buf, "true"); |
| 5542 | } else { |
| 5543 | GRN_TEXT_PUTS(ctx, buf, "false"); |
| 5544 | } |
| 5545 | GRN_TEXT_PUTS(ctx, buf, "}"); |
| 5546 | } |
| 5547 | |
| 5548 | static void |
| 5549 | grn_pat_inspect_node(grn_ctx *ctx, |
| 5550 | grn_pat *pat, |
| 5551 | grn_id id, |
| 5552 | int check, |
| 5553 | grn_obj *key_buf, |
| 5554 | int indent, |
| 5555 | const char *prefix, |
| 5556 | grn_obj *buf) |
| 5557 | { |
| 5558 | pat_node_common *node = NULL; |
| 5559 | int i, c; |
| 5560 | |
| 5561 | PAT_AT(pat, id, node); |
| 5562 | c = pat_node_get_check(pat, node); |
| 5563 | |
| 5564 | for (i = 0; i < indent; i++) { |
| 5565 | GRN_TEXT_PUTC(ctx, buf, ' '); |
| 5566 | } |
| 5567 | GRN_TEXT_PUTS(ctx, buf, prefix); |
| 5568 | grn_text_lltoa(ctx, buf, id); |
| 5569 | grn_pat_inspect_check(ctx, buf, c); |
| 5570 | |
| 5571 | if (c > check) { |
| 5572 | GRN_TEXT_PUTS(ctx, buf, "\n"); |
| 5573 | grn_pat_inspect_node(ctx, |
| 5574 | pat, |
| 5575 | pat_node_get_left(pat, node), |
| 5576 | c, |
| 5577 | key_buf, |
| 5578 | indent + 2, |
| 5579 | "L:", |
| 5580 | buf); |
| 5581 | GRN_TEXT_PUTS(ctx, buf, "\n"); |
| 5582 | grn_pat_inspect_node(ctx, |
| 5583 | pat, |
| 5584 | pat_node_get_right(pat, node), |
| 5585 | c, |
| 5586 | key_buf, |
| 5587 | indent + 2, |
| 5588 | "R:", |
| 5589 | buf); |
| 5590 | } else if (id != GRN_ID_NIL) { |
| 5591 | int key_size; |
| 5592 | uint8_t *key; |
| 5593 | |
| 5594 | key_size = pat_node_get_key_length(pat, node); |
| 5595 | GRN_BULK_REWIND(key_buf); |
| 5596 | grn_bulk_space(ctx, key_buf, key_size); |
| 5597 | grn_pat_get_key(ctx, pat, id, GRN_BULK_HEAD(key_buf), key_size); |
| 5598 | GRN_TEXT_PUTS(ctx, buf, "("); |
| 5599 | grn_inspect(ctx, buf, key_buf); |
| 5600 | GRN_TEXT_PUTS(ctx, buf, ")"); |
| 5601 | |
| 5602 | GRN_TEXT_PUTS(ctx, buf, "["); |
| 5603 | key = pat_node_get_key(ctx, pat, node); |
| 5604 | for (i = 0; i < key_size; i++) { |
| 5605 | int j; |
| 5606 | uint8_t byte = key[i]; |
| 5607 | if (i != 0) { |
| 5608 | GRN_TEXT_PUTS(ctx, buf, " "); |
| 5609 | } |
| 5610 | for (j = 0; j < 8; j++) { |
| 5611 | grn_text_lltoa(ctx, buf, (byte >> (7 - j)) & 1); |
| 5612 | } |
| 5613 | } |
| 5614 | GRN_TEXT_PUTS(ctx, buf, "]"); |
| 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | void |
| 5619 | grn_pat_inspect_nodes(grn_ctx *ctx, grn_pat *pat, grn_obj *buf) |
| 5620 | { |
| 5621 | pat_node_common *node; |
| 5622 | grn_obj key_buf; |
| 5623 | |
| 5624 | GRN_TEXT_PUTS(ctx, buf, "{"); |
| 5625 | PAT_AT(pat, GRN_ID_NIL, node); |
| 5626 | if (node) { |
| 5627 | grn_id right_node_id = pat_node_get_right(pat, node); |
| 5628 | if (right_node_id != GRN_ID_NIL) { |
| 5629 | GRN_TEXT_PUTS(ctx, buf, "\n"); |
| 5630 | GRN_OBJ_INIT(&key_buf, GRN_BULK, 0, pat->obj.header.domain); |
| 5631 | grn_pat_inspect_node(ctx, pat, right_node_id, -1, &key_buf, 0, "", buf); |
| 5632 | GRN_OBJ_FIN(ctx, &key_buf); |
| 5633 | GRN_TEXT_PUTS(ctx, buf, "\n"); |
| 5634 | } |
| 5635 | } |
| 5636 | GRN_TEXT_PUTS(ctx, buf, "}"); |
| 5637 | } |
| 5638 | |
| 5639 | static void |
| 5640 | grn_pat_cursor_inspect_entries(grn_ctx *ctx, grn_pat_cursor *c, grn_obj *buf) |
| 5641 | { |
| 5642 | uint32_t i; |
| 5643 | GRN_TEXT_PUTS(ctx, buf, "["); |
| 5644 | for (i = 0; i < c->sp; i++) { |
| 5645 | grn_pat_cursor_entry *e = c->ss + i; |
| 5646 | if (i != 0) { |
| 5647 | GRN_TEXT_PUTS(ctx, buf, ", "); |
| 5648 | } |
| 5649 | GRN_TEXT_PUTS(ctx, buf, "["); |
| 5650 | grn_text_lltoa(ctx, buf, e->id); |
| 5651 | GRN_TEXT_PUTS(ctx, buf, ","); |
| 5652 | grn_pat_inspect_check(ctx, buf, e->check); |
| 5653 | GRN_TEXT_PUTS(ctx, buf, "]"); |
| 5654 | } |
| 5655 | GRN_TEXT_PUTS(ctx, buf, "]"); |
| 5656 | } |
| 5657 | |
| 5658 | void |
| 5659 | grn_pat_cursor_inspect(grn_ctx *ctx, grn_pat_cursor *c, grn_obj *buf) |
| 5660 | { |
| 5661 | GRN_TEXT_PUTS(ctx, buf, "#<cursor:pat:"); |
| 5662 | grn_inspect_name(ctx, buf, (grn_obj *)(c->pat)); |
| 5663 | |
| 5664 | GRN_TEXT_PUTS(ctx, buf, " "); |
| 5665 | GRN_TEXT_PUTS(ctx, buf, "current:"); |
| 5666 | grn_text_lltoa(ctx, buf, c->curr_rec); |
| 5667 | |
| 5668 | GRN_TEXT_PUTS(ctx, buf, " "); |
| 5669 | GRN_TEXT_PUTS(ctx, buf, "tail:"); |
| 5670 | grn_text_lltoa(ctx, buf, c->tail); |
| 5671 | |
| 5672 | GRN_TEXT_PUTS(ctx, buf, " "); |
| 5673 | GRN_TEXT_PUTS(ctx, buf, "flags:"); |
| 5674 | if (c->obj.header.flags & GRN_CURSOR_PREFIX) { |
| 5675 | GRN_TEXT_PUTS(ctx, buf, "prefix"); |
| 5676 | } else { |
| 5677 | if (c->obj.header.flags & GRN_CURSOR_DESCENDING) { |
| 5678 | GRN_TEXT_PUTS(ctx, buf, "descending"); |
| 5679 | } else { |
| 5680 | GRN_TEXT_PUTS(ctx, buf, "ascending"); |
| 5681 | } |
| 5682 | GRN_TEXT_PUTS(ctx, buf, "|"); |
| 5683 | if (c->obj.header.flags & GRN_CURSOR_GT) { |
| 5684 | GRN_TEXT_PUTS(ctx, buf, "greater-than"); |
| 5685 | } else { |
| 5686 | GRN_TEXT_PUTS(ctx, buf, "greater"); |
| 5687 | } |
| 5688 | GRN_TEXT_PUTS(ctx, buf, "|"); |
| 5689 | if (c->obj.header.flags & GRN_CURSOR_LT) { |
| 5690 | GRN_TEXT_PUTS(ctx, buf, "less-than"); |
| 5691 | } else { |
| 5692 | GRN_TEXT_PUTS(ctx, buf, "less"); |
| 5693 | } |
| 5694 | if (c->obj.header.flags & GRN_CURSOR_BY_ID) { |
| 5695 | GRN_TEXT_PUTS(ctx, buf, "|by-id"); |
| 5696 | } |
| 5697 | if (c->obj.header.flags & GRN_CURSOR_BY_KEY) { |
| 5698 | GRN_TEXT_PUTS(ctx, buf, "|by-key"); |
| 5699 | } |
| 5700 | } |
| 5701 | |
| 5702 | GRN_TEXT_PUTS(ctx, buf, " "); |
| 5703 | GRN_TEXT_PUTS(ctx, buf, "rest:"); |
| 5704 | grn_text_lltoa(ctx, buf, c->rest); |
| 5705 | |
| 5706 | GRN_TEXT_PUTS(ctx, buf, " "); |
| 5707 | GRN_TEXT_PUTS(ctx, buf, "entries:"); |
| 5708 | grn_pat_cursor_inspect_entries(ctx, c, buf); |
| 5709 | |
| 5710 | GRN_TEXT_PUTS(ctx, buf, ">"); |
| 5711 | } |
| 5712 | |
| 5713 | typedef struct { |
| 5714 | uint8_t code; |
| 5715 | uint8_t next; |
| 5716 | uint8_t emit; |
| 5717 | uint8_t attr; |
| 5718 | } rk_tree_node; |
| 5719 | |
| 5720 | static uint16_t rk_str_idx[] = { |
| 5721 | 0x0003, 0x0006, 0x0009, 0x000c, 0x0012, 0x0015, 0x0018, 0x001e, 0x0024, |
| 5722 | 0x002a, 0x0030, 0x0036, 0x003c, 0x0042, 0x0048, 0x004e, 0x0054, 0x005a, |
| 5723 | 0x0060, 0x0066, 0x006c, 0x0072, 0x0078, 0x007e, 0x0084, 0x008a, 0x0090, |
| 5724 | 0x0096, 0x009c, 0x00a2, 0x00a8, 0x00ae, 0x00b4, 0x00ba, 0x00c0, 0x00c3, |
| 5725 | 0x00c6, 0x00c9, 0x00cc, 0x00cf, 0x00d2, 0x00d5, 0x00db, 0x00e1, 0x00e7, |
| 5726 | 0x00ea, 0x00f0, 0x00f6, 0x00fc, 0x00ff, 0x0105, 0x0108, 0x010e, 0x0111, |
| 5727 | 0x0114, 0x0117, 0x011a, 0x011d, 0x0120, 0x0123, 0x0129, 0x012f, 0x0135, |
| 5728 | 0x013b, 0x013e, 0x0144, 0x014a, 0x0150, 0x0156, 0x0159, 0x015c, 0x015f, |
| 5729 | 0x0162, 0x0165, 0x0168, 0x016b, 0x016e, 0x0171, 0x0177, 0x017d, 0x0183, |
| 5730 | 0x0189, 0x018c, 0x0192, 0x0198, 0x019e, 0x01a1, 0x01a4, 0x01aa, 0x01b0, |
| 5731 | 0x01b6, 0x01bc, 0x01bf, 0x01c2, 0x01c8, 0x01ce, 0x01d1, 0x01d7, 0x01dd, |
| 5732 | 0x01e0, 0x01e6, 0x01e9, 0x01ef, 0x01f2, 0x01f5, 0x01fb, 0x0201, 0x0207, |
| 5733 | 0x020d, 0x0213, 0x0216, 0x0219, 0x021c, 0x021f, 0x0222, 0x0225, 0x0228, |
| 5734 | 0x022e, 0x0234, 0x023a, 0x023d, 0x0243, 0x0249, 0x024f, 0x0252, 0x0258, |
| 5735 | 0x025e, 0x0264, 0x0267, 0x026d, 0x0273, 0x0279, 0x027f, 0x0285, 0x0288, |
| 5736 | 0x028b, 0x028e, 0x0291, 0x0294, 0x0297, 0x029a, 0x029d, 0x02a0, 0x02a3, |
| 5737 | 0x02a9, 0x02af, 0x02b5, 0x02b8, 0x02bb, 0x02be, 0x02c1, 0x02c4, 0x02c7, |
| 5738 | 0x02ca, 0x02cd, 0x02d0, 0x02d3, 0x02d6, 0x02dc, 0x02e2, 0x02e8, 0x02eb, |
| 5739 | 0x02ee, 0x02f1, 0x02f4, 0x02f7, 0x02fa, 0x02fd, 0x0300, 0x0303, 0x0309, |
| 5740 | 0x030c, 0x0312, 0x0318, 0x031e, 0x0324, 0x0327, 0x032a, 0x032d}; |
| 5741 | static char rk_str[] = { |
| 5742 | 0xe3, 0x82, 0xa1, 0xe3, 0x82, 0xa2, 0xe3, 0x82, 0xa3, 0xe3, 0x82, 0xa4, 0xe3, |
| 5743 | 0x82, 0xa4, 0xe3, 0x82, 0xa7, 0xe3, 0x82, 0xa5, 0xe3, 0x82, 0xa6, 0xe3, 0x82, |
| 5744 | 0xa6, 0xe3, 0x82, 0xa2, 0xe3, 0x82, 0xa6, 0xe3, 0x82, 0xa3, 0xe3, 0x82, 0xa6, |
| 5745 | 0xe3, 0x82, 0xa4, 0xe3, 0x82, 0xa6, 0xe3, 0x82, 0xa6, 0xe3, 0x82, 0xa6, 0xe3, |
| 5746 | 0x82, 0xa7, 0xe3, 0x82, 0xa6, 0xe3, 0x82, 0xa8, 0xe3, 0x82, 0xa6, 0xe3, 0x82, |
| 5747 | 0xaa, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa0, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa1, |
| 5748 | 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa2, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa3, 0xe3, |
| 5749 | 0x82, 0xa6, 0xe3, 0x83, 0xa4, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa5, 0xe3, 0x82, |
| 5750 | 0xa6, 0xe3, 0x83, 0xa6, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xa6, |
| 5751 | 0xe3, 0x83, 0xa8, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xa9, 0xe3, 0x82, 0xa6, 0xe3, |
| 5752 | 0x83, 0xaa, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xab, 0xe3, 0x82, 0xa6, 0xe3, 0x83, |
| 5753 | 0xac, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xad, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xae, |
| 5754 | 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xaf, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xb0, 0xe3, |
| 5755 | 0x82, 0xa6, 0xe3, 0x83, 0xb1, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xb2, 0xe3, 0x82, |
| 5756 | 0xa6, 0xe3, 0x83, 0xb3, 0xe3, 0x82, 0xa6, 0xe3, 0x83, 0xbc, 0xe3, 0x82, 0xa7, |
| 5757 | 0xe3, 0x82, 0xa8, 0xe3, 0x82, 0xa9, 0xe3, 0x82, 0xaa, 0xe3, 0x82, 0xab, 0xe3, |
| 5758 | 0x82, 0xac, 0xe3, 0x82, 0xad, 0xe3, 0x82, 0xad, 0xe3, 0x83, 0xa3, 0xe3, 0x82, |
| 5759 | 0xad, 0xe3, 0x83, 0xa5, 0xe3, 0x82, 0xad, 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xae, |
| 5760 | 0xe3, 0x82, 0xae, 0xe3, 0x83, 0xa3, 0xe3, 0x82, 0xae, 0xe3, 0x83, 0xa5, 0xe3, |
| 5761 | 0x82, 0xae, 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xaf, 0xe3, 0x82, 0xaf, 0xe3, 0x82, |
| 5762 | 0xa1, 0xe3, 0x82, 0xb0, 0xe3, 0x82, 0xb0, 0xe3, 0x82, 0xa1, 0xe3, 0x82, 0xb1, |
| 5763 | 0xe3, 0x82, 0xb2, 0xe3, 0x82, 0xb3, 0xe3, 0x82, 0xb4, 0xe3, 0x82, 0xb5, 0xe3, |
| 5764 | 0x82, 0xb6, 0xe3, 0x82, 0xb7, 0xe3, 0x82, 0xb7, 0xe3, 0x82, 0xa7, 0xe3, 0x82, |
| 5765 | 0xb7, 0xe3, 0x83, 0xa3, 0xe3, 0x82, 0xb7, 0xe3, 0x83, 0xa5, 0xe3, 0x82, 0xb7, |
| 5766 | 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xb8, 0xe3, 0x82, 0xb8, 0xe3, 0x82, 0xa7, 0xe3, |
| 5767 | 0x82, 0xb8, 0xe3, 0x83, 0xa3, 0xe3, 0x82, 0xb8, 0xe3, 0x83, 0xa5, 0xe3, 0x82, |
| 5768 | 0xb8, 0xe3, 0x83, 0xa7, 0xe3, 0x82, 0xb9, 0xe3, 0x82, 0xba, 0xe3, 0x82, 0xbb, |
| 5769 | 0xe3, 0x82, 0xbc, 0xe3, 0x82, 0xbd, 0xe3, 0x82, 0xbe, 0xe3, 0x82, 0xbf, 0xe3, |
| 5770 | 0x83, 0x80, 0xe3, 0x83, 0x81, 0xe3, 0x83, 0x81, 0xe3, 0x82, 0xa7, 0xe3, 0x83, |
| 5771 | 0x81, 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0x81, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x81, |
| 5772 | 0xe3, 0x83, 0xa7, 0xe3, 0x83, 0x82, 0xe3, 0x83, 0x82, 0xe3, 0x83, 0xa3, 0xe3, |
| 5773 | 0x83, 0x82, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x82, 0xe3, 0x83, 0xa7, 0xe3, 0x83, |
| 5774 | 0x83, 0xe3, 0x83, 0x84, 0xe3, 0x83, 0x84, 0xe3, 0x82, 0xa1, 0xe3, 0x83, 0x84, |
| 5775 | 0xe3, 0x82, 0xa3, 0xe3, 0x83, 0x84, 0xe3, 0x82, 0xa7, 0xe3, 0x83, 0x84, 0xe3, |
| 5776 | 0x82, 0xa9, 0xe3, 0x83, 0x85, 0xe3, 0x83, 0x86, 0xe3, 0x83, 0x86, 0xe3, 0x82, |
| 5777 | 0xa3, 0xe3, 0x83, 0x86, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x87, 0xe3, 0x83, 0x87, |
| 5778 | 0xe3, 0x82, 0xa3, 0xe3, 0x83, 0x87, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x88, 0xe3, |
| 5779 | 0x83, 0x88, 0xe3, 0x82, 0xa5, 0xe3, 0x83, 0x89, 0xe3, 0x83, 0x89, 0xe3, 0x82, |
| 5780 | 0xa5, 0xe3, 0x83, 0x8a, 0xe3, 0x83, 0x8b, 0xe3, 0x83, 0x8b, 0xe3, 0x82, 0xa3, |
| 5781 | 0xe3, 0x83, 0x8b, 0xe3, 0x82, 0xa7, 0xe3, 0x83, 0x8b, 0xe3, 0x83, 0xa3, 0xe3, |
| 5782 | 0x83, 0x8b, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x8b, 0xe3, 0x83, 0xa7, 0xe3, 0x83, |
| 5783 | 0x8c, 0xe3, 0x83, 0x8d, 0xe3, 0x83, 0x8e, 0xe3, 0x83, 0x8f, 0xe3, 0x83, 0x90, |
| 5784 | 0xe3, 0x83, 0x91, 0xe3, 0x83, 0x92, 0xe3, 0x83, 0x92, 0xe3, 0x83, 0xa3, 0xe3, |
| 5785 | 0x83, 0x92, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x92, 0xe3, 0x83, 0xa7, 0xe3, 0x83, |
| 5786 | 0x93, 0xe3, 0x83, 0x93, 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0x93, 0xe3, 0x83, 0xa5, |
| 5787 | 0xe3, 0x83, 0x93, 0xe3, 0x83, 0xa7, 0xe3, 0x83, 0x94, 0xe3, 0x83, 0x94, 0xe3, |
| 5788 | 0x83, 0xa3, 0xe3, 0x83, 0x94, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x94, 0xe3, 0x83, |
| 5789 | 0xa7, 0xe3, 0x83, 0x95, 0xe3, 0x83, 0x95, 0xe3, 0x82, 0xa1, 0xe3, 0x83, 0x95, |
| 5790 | 0xe3, 0x82, 0xa3, 0xe3, 0x83, 0x95, 0xe3, 0x82, 0xa7, 0xe3, 0x83, 0x95, 0xe3, |
| 5791 | 0x82, 0xa9, 0xe3, 0x83, 0x95, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0x96, 0xe3, 0x83, |
| 5792 | 0x97, 0xe3, 0x83, 0x98, 0xe3, 0x83, 0x99, 0xe3, 0x83, 0x9a, 0xe3, 0x83, 0x9b, |
| 5793 | 0xe3, 0x83, 0x9c, 0xe3, 0x83, 0x9d, 0xe3, 0x83, 0x9e, 0xe3, 0x83, 0x9f, 0xe3, |
| 5794 | 0x83, 0x9f, 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0x9f, 0xe3, 0x83, 0xa5, 0xe3, 0x83, |
| 5795 | 0x9f, 0xe3, 0x83, 0xa7, 0xe3, 0x83, 0xa0, 0xe3, 0x83, 0xa1, 0xe3, 0x83, 0xa2, |
| 5796 | 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0xa4, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0xa6, 0xe3, |
| 5797 | 0x83, 0xa7, 0xe3, 0x83, 0xa8, 0xe3, 0x83, 0xa9, 0xe3, 0x83, 0xaa, 0xe3, 0x83, |
| 5798 | 0xaa, 0xe3, 0x83, 0xa3, 0xe3, 0x83, 0xaa, 0xe3, 0x83, 0xa5, 0xe3, 0x83, 0xaa, |
| 5799 | 0xe3, 0x83, 0xa7, 0xe3, 0x83, 0xab, 0xe3, 0x83, 0xac, 0xe3, 0x83, 0xad, 0xe3, |
| 5800 | 0x83, 0xae, 0xe3, 0x83, 0xaf, 0xe3, 0x83, 0xb0, 0xe3, 0x83, 0xb1, 0xe3, 0x83, |
| 5801 | 0xb2, 0xe3, 0x83, 0xb3, 0xe3, 0x83, 0xb3, 0xe3, 0x83, 0xbc, 0xe3, 0x83, 0xb4, |
| 5802 | 0xe3, 0x83, 0xb4, 0xe3, 0x82, 0xa1, 0xe3, 0x83, 0xb4, 0xe3, 0x82, 0xa3, 0xe3, |
| 5803 | 0x83, 0xb4, 0xe3, 0x82, 0xa7, 0xe3, 0x83, 0xb4, 0xe3, 0x82, 0xa9, 0xe3, 0x83, |
| 5804 | 0xb5, 0xe3, 0x83, 0xb6, 0xe3, 0x83, 0xbc}; |
| 5805 | static uint16_t rk_tree_idx[] = { |
| 5806 | 0x001b, 0x0022, 0x0025, 0x0028, 0x002d, 0x0030, 0x0039, 0x003b, 0x003c, |
| 5807 | 0x003f, 0x0046, 0x0047, 0x004f, 0x0050, 0x0053, 0x005a, 0x005d, 0x0064, |
| 5808 | 0x0067, 0x006f, 0x0070, 0x0073, 0x007d, 0x007f, 0x0081, 0x0082, 0x0083, |
| 5809 | 0x0088, 0x008f, 0x0092, 0x00af, 0x00b5, 0x00bc, 0x00bf, 0x00c6, 0x00c9, |
| 5810 | 0x00d1, 0x00d6, 0x00da, 0x00e4, 0x00e6, 0x00eb, 0x00ec, 0x00f0, 0x00f6, |
| 5811 | 0x00fc, 0x00fe, 0x0108, 0x010a, 0x010c, 0x010d, 0x010e, 0x0113, 0x0118, |
| 5812 | 0x011f, 0x0123, 0x0125, 0x0164, 0x0180, 0x0183, 0x0199, 0x01ad}; |
| 5813 | static rk_tree_node rk_tree[] = { |
| 5814 | {0x2d, 0x00, 0xb2, 0x01}, {0x61, 0x00, 0x01, 0x01}, {0x62, 0x01, 0xff, 0x01}, |
| 5815 | {0x63, 0x03, 0xff, 0x01}, {0x64, 0x06, 0xff, 0x01}, {0x65, 0x00, 0x24, 0x01}, |
| 5816 | {0x66, 0x0a, 0xff, 0x01}, {0x67, 0x0c, 0xff, 0x01}, {0x68, 0x0f, 0xff, 0x01}, |
| 5817 | {0x69, 0x00, 0x03, 0x01}, {0x6a, 0x11, 0xff, 0x01}, {0x6b, 0x13, 0xff, 0x01}, |
| 5818 | {0x6c, 0x16, 0xff, 0x01}, {0x6d, 0x1c, 0xff, 0x01}, {0x6e, 0x1e, 0xff, 0x01}, |
| 5819 | {0x6f, 0x00, 0x26, 0x01}, {0x70, 0x20, 0xff, 0x01}, {0x72, 0x22, 0xff, 0x01}, |
| 5820 | {0x73, 0x24, 0xff, 0x01}, {0x74, 0x27, 0xff, 0x01}, {0x75, 0x00, 0x06, 0x01}, |
| 5821 | {0x76, 0x2c, 0xff, 0x01}, {0x77, 0x2d, 0xff, 0x01}, {0x78, 0x2f, 0xff, 0x01}, |
| 5822 | {0x79, 0x35, 0xff, 0x01}, {0x7a, 0x36, 0xff, 0x01}, {0xe3, 0x38, 0xff, 0x01}, |
| 5823 | {0x61, 0x00, 0x72, 0x01}, {0x62, 0x01, 0x56, 0x01}, {0x65, 0x00, 0x89, 0x01}, |
| 5824 | {0x69, 0x00, 0x78, 0x01}, {0x6f, 0x00, 0x8c, 0x01}, {0x75, 0x00, 0x86, 0x01}, |
| 5825 | {0x79, 0x02, 0xff, 0x00}, {0x61, 0x00, 0x79, 0x01}, {0x6f, 0x00, 0x7b, 0x01}, |
| 5826 | {0x75, 0x00, 0x7a, 0x01}, {0x63, 0x03, 0x56, 0x01}, {0x68, 0x04, 0xff, 0x01}, |
| 5827 | {0x79, 0x05, 0xff, 0x01}, {0x61, 0x00, 0x4f, 0x00}, {0x65, 0x00, 0x4e, 0x00}, |
| 5828 | {0x69, 0x00, 0x4d, 0x01}, {0x6f, 0x00, 0x51, 0x00}, {0x75, 0x00, 0x50, 0x00}, |
| 5829 | {0x61, 0x00, 0x4f, 0x01}, {0x6f, 0x00, 0x51, 0x01}, {0x75, 0x00, 0x50, 0x01}, |
| 5830 | {0x61, 0x00, 0x4c, 0x01}, {0x64, 0x06, 0x56, 0x01}, {0x65, 0x00, 0x60, 0x01}, |
| 5831 | {0x68, 0x07, 0xff, 0x00}, {0x69, 0x00, 0x61, 0x00}, {0x6f, 0x00, 0x65, 0x01}, |
| 5832 | {0x75, 0x00, 0x5c, 0x01}, {0x77, 0x08, 0xff, 0x00}, {0x79, 0x09, 0xff, 0x01}, |
| 5833 | {0x69, 0x00, 0x61, 0x01}, {0x75, 0x00, 0x62, 0x01}, {0x75, 0x00, 0x66, 0x01}, |
| 5834 | {0x61, 0x00, 0x53, 0x01}, {0x6f, 0x00, 0x55, 0x01}, {0x75, 0x00, 0x54, 0x01}, |
| 5835 | {0x61, 0x00, 0x81, 0x00}, {0x65, 0x00, 0x83, 0x00}, {0x66, 0x0a, 0x56, 0x01}, |
| 5836 | {0x69, 0x00, 0x82, 0x00}, {0x6f, 0x00, 0x84, 0x00}, {0x75, 0x00, 0x80, 0x01}, |
| 5837 | {0x79, 0x0b, 0xff, 0x00}, {0x75, 0x00, 0x85, 0x01}, {0x61, 0x00, 0x28, 0x01}, |
| 5838 | {0x65, 0x00, 0x36, 0x01}, {0x67, 0x0c, 0x56, 0x01}, {0x69, 0x00, 0x2d, 0x01}, |
| 5839 | {0x6f, 0x00, 0x38, 0x01}, {0x75, 0x00, 0x33, 0x01}, {0x77, 0x0d, 0xff, 0x00}, |
| 5840 | {0x79, 0x0e, 0xff, 0x00}, {0x61, 0x00, 0x34, 0x01}, {0x61, 0x00, 0x2e, 0x01}, |
| 5841 | {0x6f, 0x00, 0x30, 0x01}, {0x75, 0x00, 0x2f, 0x01}, {0x61, 0x00, 0x71, 0x01}, |
| 5842 | {0x65, 0x00, 0x88, 0x01}, {0x68, 0x0f, 0x56, 0x01}, {0x69, 0x00, 0x74, 0x01}, |
| 5843 | {0x6f, 0x00, 0x8b, 0x01}, {0x75, 0x00, 0x80, 0x01}, {0x79, 0x10, 0xff, 0x00}, |
| 5844 | {0x61, 0x00, 0x75, 0x01}, {0x6f, 0x00, 0x77, 0x01}, {0x75, 0x00, 0x76, 0x01}, |
| 5845 | {0x61, 0x00, 0x42, 0x00}, {0x65, 0x00, 0x41, 0x00}, {0x69, 0x00, 0x40, 0x01}, |
| 5846 | {0x6a, 0x11, 0x56, 0x01}, {0x6f, 0x00, 0x44, 0x00}, {0x75, 0x00, 0x43, 0x00}, |
| 5847 | {0x79, 0x12, 0xff, 0x00}, {0x61, 0x00, 0x42, 0x01}, {0x6f, 0x00, 0x44, 0x01}, |
| 5848 | {0x75, 0x00, 0x43, 0x01}, {0x61, 0x00, 0x27, 0x01}, {0x65, 0x00, 0x35, 0x01}, |
| 5849 | {0x69, 0x00, 0x29, 0x01}, {0x6b, 0x13, 0x56, 0x01}, {0x6f, 0x00, 0x37, 0x01}, |
| 5850 | {0x75, 0x00, 0x31, 0x01}, {0x77, 0x14, 0xff, 0x00}, {0x79, 0x15, 0xff, 0x00}, |
| 5851 | {0x61, 0x00, 0x32, 0x01}, {0x61, 0x00, 0x2a, 0x01}, {0x6f, 0x00, 0x2c, 0x01}, |
| 5852 | {0x75, 0x00, 0x2b, 0x01}, {0x61, 0x00, 0x00, 0x01}, {0x65, 0x00, 0x23, 0x01}, |
| 5853 | {0x69, 0x00, 0x02, 0x01}, {0x6b, 0x17, 0xff, 0x01}, {0x6c, 0x16, 0x56, 0x01}, |
| 5854 | {0x6f, 0x00, 0x25, 0x01}, {0x74, 0x18, 0xff, 0x01}, {0x75, 0x00, 0x05, 0x01}, |
| 5855 | {0x77, 0x1a, 0xff, 0x01}, {0x79, 0x1b, 0xff, 0x01}, {0x61, 0x00, 0xb0, 0x01}, |
| 5856 | {0x65, 0x00, 0xb1, 0x01}, {0x73, 0x19, 0xff, 0x00}, {0x75, 0x00, 0x56, 0x01}, |
| 5857 | {0x75, 0x00, 0x56, 0x01}, {0x61, 0x00, 0xa4, 0x01}, {0x61, 0x00, 0x96, 0x01}, |
| 5858 | {0x65, 0x00, 0x23, 0x01}, {0x69, 0x00, 0x02, 0x01}, {0x6f, 0x00, 0x9a, 0x01}, |
| 5859 | {0x75, 0x00, 0x98, 0x01}, {0x61, 0x00, 0x8e, 0x01}, {0x65, 0x00, 0x94, 0x01}, |
| 5860 | {0x69, 0x00, 0x8f, 0x01}, {0x6d, 0x1c, 0x56, 0x01}, {0x6f, 0x00, 0x95, 0x01}, |
| 5861 | {0x75, 0x00, 0x93, 0x01}, {0x79, 0x1d, 0xff, 0x00}, {0x61, 0x00, 0x90, 0x01}, |
| 5862 | {0x6f, 0x00, 0x92, 0x01}, {0x75, 0x00, 0x91, 0x01}, {0x00, 0x00, 0xa9, 0x01}, |
| 5863 | {0x27, 0x00, 0xa9, 0x00}, {0x2d, 0x00, 0xaa, 0x00}, {0x61, 0x00, 0x67, 0x01}, |
| 5864 | {0x62, 0x01, 0xa9, 0x00}, {0x63, 0x03, 0xa9, 0x00}, {0x64, 0x06, 0xa9, 0x00}, |
| 5865 | {0x65, 0x00, 0x6f, 0x01}, {0x66, 0x0a, 0xa9, 0x00}, {0x67, 0x0c, 0xa9, 0x00}, |
| 5866 | {0x68, 0x0f, 0xa9, 0x00}, {0x69, 0x00, 0x68, 0x01}, {0x6a, 0x11, 0xa9, 0x00}, |
| 5867 | {0x6b, 0x13, 0xa9, 0x00}, {0x6c, 0x16, 0xa9, 0x00}, {0x6d, 0x1c, 0xa9, 0x00}, |
| 5868 | {0x6e, 0x00, 0xa9, 0x00}, {0x6f, 0x00, 0x70, 0x01}, {0x70, 0x20, 0xa9, 0x00}, |
| 5869 | {0x72, 0x22, 0xa9, 0x00}, {0x73, 0x24, 0xa9, 0x00}, {0x74, 0x27, 0xa9, 0x00}, |
| 5870 | {0x75, 0x00, 0x6e, 0x01}, {0x76, 0x2c, 0xa9, 0x00}, {0x77, 0x2d, 0xa9, 0x00}, |
| 5871 | {0x78, 0x2f, 0xa9, 0x00}, {0x79, 0x1f, 0xff, 0x00}, {0x7a, 0x36, 0xa9, 0x00}, |
| 5872 | {0xe3, 0x38, 0xa9, 0x00}, {0x00, 0x00, 0xa9, 0x01}, {0x61, 0x00, 0x6b, 0x01}, |
| 5873 | {0x65, 0x00, 0x6a, 0x01}, {0x69, 0x00, 0x69, 0x01}, {0x6f, 0x00, 0x6d, 0x01}, |
| 5874 | {0x75, 0x00, 0x6c, 0x01}, {0x61, 0x00, 0x73, 0x01}, {0x65, 0x00, 0x8a, 0x01}, |
| 5875 | {0x69, 0x00, 0x7c, 0x01}, {0x6f, 0x00, 0x8d, 0x01}, {0x70, 0x20, 0x56, 0x01}, |
| 5876 | {0x75, 0x00, 0x87, 0x01}, {0x79, 0x21, 0xff, 0x00}, {0x61, 0x00, 0x7d, 0x01}, |
| 5877 | {0x6f, 0x00, 0x7f, 0x01}, {0x75, 0x00, 0x7e, 0x01}, {0x61, 0x00, 0x9c, 0x01}, |
| 5878 | {0x65, 0x00, 0xa2, 0x01}, {0x69, 0x00, 0x9d, 0x01}, {0x6f, 0x00, 0xa3, 0x01}, |
| 5879 | {0x72, 0x22, 0x56, 0x01}, {0x75, 0x00, 0xa1, 0x01}, {0x79, 0x23, 0xff, 0x00}, |
| 5880 | {0x61, 0x00, 0x9e, 0x01}, {0x6f, 0x00, 0xa0, 0x01}, {0x75, 0x00, 0x9f, 0x01}, |
| 5881 | {0x61, 0x00, 0x39, 0x01}, {0x65, 0x00, 0x47, 0x01}, {0x68, 0x25, 0xff, 0x00}, |
| 5882 | {0x69, 0x00, 0x3b, 0x01}, {0x6f, 0x00, 0x49, 0x01}, {0x73, 0x24, 0x56, 0x01}, |
| 5883 | {0x75, 0x00, 0x45, 0x01}, {0x79, 0x26, 0xff, 0x00}, {0x61, 0x00, 0x3d, 0x00}, |
| 5884 | {0x65, 0x00, 0x3c, 0x00}, {0x69, 0x00, 0x3b, 0x01}, {0x6f, 0x00, 0x3f, 0x00}, |
| 5885 | {0x75, 0x00, 0x3e, 0x00}, {0x61, 0x00, 0x3d, 0x01}, {0x65, 0x00, 0x3c, 0x01}, |
| 5886 | {0x6f, 0x00, 0x3f, 0x01}, {0x75, 0x00, 0x3e, 0x01}, {0x61, 0x00, 0x4b, 0x01}, |
| 5887 | {0x65, 0x00, 0x5d, 0x01}, {0x68, 0x28, 0xff, 0x00}, {0x69, 0x00, 0x4d, 0x01}, |
| 5888 | {0x6f, 0x00, 0x63, 0x01}, {0x73, 0x29, 0xff, 0x00}, {0x74, 0x27, 0x56, 0x01}, |
| 5889 | {0x75, 0x00, 0x57, 0x01}, {0x77, 0x2a, 0xff, 0x00}, {0x79, 0x2b, 0xff, 0x00}, |
| 5890 | {0x69, 0x00, 0x5e, 0x01}, {0x75, 0x00, 0x5f, 0x01}, {0x61, 0x00, 0x58, 0x00}, |
| 5891 | {0x65, 0x00, 0x5a, 0x00}, {0x69, 0x00, 0x59, 0x00}, {0x6f, 0x00, 0x5b, 0x00}, |
| 5892 | {0x75, 0x00, 0x57, 0x01}, {0x75, 0x00, 0x64, 0x01}, {0x61, 0x00, 0x4f, 0x01}, |
| 5893 | {0x65, 0x00, 0x4e, 0x01}, {0x6f, 0x00, 0x51, 0x01}, {0x75, 0x00, 0x50, 0x01}, |
| 5894 | {0x61, 0x00, 0xac, 0x00}, {0x65, 0x00, 0xae, 0x00}, {0x69, 0x00, 0xad, 0x00}, |
| 5895 | {0x6f, 0x00, 0xaf, 0x00}, {0x75, 0x00, 0xab, 0x01}, {0x76, 0x2c, 0x56, 0x01}, |
| 5896 | {0x61, 0x00, 0xa5, 0x01}, {0x65, 0x00, 0x0b, 0x01}, {0x69, 0x00, 0x08, 0x01}, |
| 5897 | {0x6f, 0x00, 0xa8, 0x01}, {0x77, 0x2d, 0x56, 0x01}, {0x79, 0x2e, 0xff, 0x01}, |
| 5898 | {0x65, 0x00, 0xa7, 0x01}, {0x69, 0x00, 0xa6, 0x01}, {0x61, 0x00, 0x00, 0x01}, |
| 5899 | {0x65, 0x00, 0x23, 0x01}, {0x69, 0x00, 0x02, 0x01}, {0x6b, 0x30, 0xff, 0x01}, |
| 5900 | {0x6f, 0x00, 0x25, 0x01}, {0x74, 0x31, 0xff, 0x01}, {0x75, 0x00, 0x05, 0x01}, |
| 5901 | {0x77, 0x33, 0xff, 0x01}, {0x78, 0x2f, 0x56, 0x01}, {0x79, 0x34, 0xff, 0x01}, |
| 5902 | {0x61, 0x00, 0xb0, 0x01}, {0x65, 0x00, 0xb1, 0x01}, {0x73, 0x32, 0xff, 0x00}, |
| 5903 | {0x75, 0x00, 0x56, 0x01}, {0x75, 0x00, 0x56, 0x01}, {0x61, 0x00, 0xa4, 0x01}, |
| 5904 | {0x61, 0x00, 0x96, 0x01}, {0x65, 0x00, 0x23, 0x01}, {0x69, 0x00, 0x02, 0x01}, |
| 5905 | {0x6f, 0x00, 0x9a, 0x01}, {0x75, 0x00, 0x98, 0x01}, {0x61, 0x00, 0x97, 0x01}, |
| 5906 | {0x65, 0x00, 0x04, 0x01}, {0x6f, 0x00, 0x9b, 0x01}, {0x75, 0x00, 0x99, 0x01}, |
| 5907 | {0x79, 0x35, 0x56, 0x01}, {0x61, 0x00, 0x3a, 0x01}, {0x65, 0x00, 0x48, 0x01}, |
| 5908 | {0x69, 0x00, 0x40, 0x01}, {0x6f, 0x00, 0x4a, 0x01}, {0x75, 0x00, 0x46, 0x01}, |
| 5909 | {0x79, 0x37, 0xff, 0x00}, {0x7a, 0x36, 0x56, 0x01}, {0x61, 0x00, 0x42, 0x01}, |
| 5910 | {0x65, 0x00, 0x41, 0x01}, {0x6f, 0x00, 0x44, 0x01}, {0x75, 0x00, 0x43, 0x01}, |
| 5911 | {0x81, 0x39, 0xff, 0x01}, {0x82, 0x3d, 0xff, 0x01}, {0x81, 0x00, 0x00, 0x01}, |
| 5912 | {0x82, 0x00, 0x01, 0x01}, {0x83, 0x00, 0x02, 0x01}, {0x84, 0x00, 0x03, 0x01}, |
| 5913 | {0x85, 0x00, 0x05, 0x01}, {0x86, 0x3a, 0xff, 0x01}, {0x87, 0x00, 0x23, 0x01}, |
| 5914 | {0x88, 0x00, 0x24, 0x01}, {0x89, 0x00, 0x25, 0x01}, {0x8a, 0x00, 0x26, 0x01}, |
| 5915 | {0x8b, 0x00, 0x27, 0x01}, {0x8c, 0x00, 0x28, 0x01}, {0x8d, 0x00, 0x29, 0x01}, |
| 5916 | {0x8e, 0x00, 0x2d, 0x01}, {0x8f, 0x00, 0x31, 0x01}, {0x90, 0x00, 0x33, 0x01}, |
| 5917 | {0x91, 0x00, 0x35, 0x01}, {0x92, 0x00, 0x36, 0x01}, {0x93, 0x00, 0x37, 0x01}, |
| 5918 | {0x94, 0x00, 0x38, 0x01}, {0x95, 0x00, 0x39, 0x01}, {0x96, 0x00, 0x3a, 0x01}, |
| 5919 | {0x97, 0x00, 0x3b, 0x01}, {0x98, 0x00, 0x40, 0x01}, {0x99, 0x00, 0x45, 0x01}, |
| 5920 | {0x9a, 0x00, 0x46, 0x01}, {0x9b, 0x00, 0x47, 0x01}, {0x9c, 0x00, 0x48, 0x01}, |
| 5921 | {0x9d, 0x00, 0x49, 0x01}, {0x9e, 0x00, 0x4a, 0x01}, {0x9f, 0x00, 0x4b, 0x01}, |
| 5922 | {0xa0, 0x00, 0x4c, 0x01}, {0xa1, 0x00, 0x4d, 0x01}, {0xa2, 0x00, 0x52, 0x01}, |
| 5923 | {0xa3, 0x00, 0x56, 0x01}, {0xa4, 0x00, 0x57, 0x01}, {0xa5, 0x00, 0x5c, 0x01}, |
| 5924 | {0xa6, 0x00, 0x5d, 0x01}, {0xa7, 0x00, 0x60, 0x01}, {0xa8, 0x00, 0x63, 0x01}, |
| 5925 | {0xa9, 0x00, 0x65, 0x01}, {0xaa, 0x00, 0x67, 0x01}, {0xab, 0x00, 0x68, 0x01}, |
| 5926 | {0xac, 0x00, 0x6e, 0x01}, {0xad, 0x00, 0x6f, 0x01}, {0xae, 0x00, 0x70, 0x01}, |
| 5927 | {0xaf, 0x00, 0x71, 0x01}, {0xb0, 0x00, 0x72, 0x01}, {0xb1, 0x00, 0x73, 0x01}, |
| 5928 | {0xb2, 0x00, 0x74, 0x01}, {0xb3, 0x00, 0x78, 0x01}, {0xb4, 0x00, 0x7c, 0x01}, |
| 5929 | {0xb5, 0x00, 0x80, 0x01}, {0xb6, 0x00, 0x86, 0x01}, {0xb7, 0x00, 0x87, 0x01}, |
| 5930 | {0xb8, 0x00, 0x88, 0x01}, {0xb9, 0x00, 0x89, 0x01}, {0xba, 0x00, 0x8a, 0x01}, |
| 5931 | {0xbb, 0x00, 0x8b, 0x01}, {0xbc, 0x00, 0x8c, 0x01}, {0xbd, 0x00, 0x8d, 0x01}, |
| 5932 | {0xbe, 0x00, 0x8e, 0x01}, {0xbf, 0x00, 0x8f, 0x01}, {0x00, 0x00, 0x06, 0x00}, |
| 5933 | {0x2d, 0x00, 0x22, 0x00}, {0x61, 0x00, 0x07, 0x00}, {0x62, 0x01, 0x06, 0x00}, |
| 5934 | {0x63, 0x03, 0x06, 0x00}, {0x64, 0x06, 0x06, 0x00}, {0x65, 0x00, 0x0c, 0x00}, |
| 5935 | {0x66, 0x0a, 0x06, 0x00}, {0x67, 0x0c, 0x06, 0x00}, {0x68, 0x0f, 0x06, 0x00}, |
| 5936 | {0x69, 0x00, 0x09, 0x00}, {0x6a, 0x11, 0x06, 0x00}, {0x6b, 0x13, 0x06, 0x00}, |
| 5937 | {0x6c, 0x16, 0x06, 0x00}, {0x6d, 0x1c, 0x06, 0x00}, {0x6e, 0x1e, 0x06, 0x00}, |
| 5938 | {0x6f, 0x00, 0x0d, 0x00}, {0x70, 0x20, 0x06, 0x00}, {0x72, 0x22, 0x06, 0x00}, |
| 5939 | {0x73, 0x24, 0x06, 0x00}, {0x74, 0x27, 0x06, 0x00}, {0x75, 0x00, 0x0a, 0x00}, |
| 5940 | {0x76, 0x2c, 0x06, 0x00}, {0x77, 0x2d, 0x06, 0x00}, {0x78, 0x2f, 0x06, 0x00}, |
| 5941 | {0x79, 0x35, 0x06, 0x00}, {0x7a, 0x36, 0x06, 0x00}, {0xe3, 0x3b, 0xff, 0x01}, |
| 5942 | {0x00, 0x00, 0x06, 0x00}, {0x81, 0x39, 0x06, 0x00}, {0x82, 0x3c, 0xff, 0x01}, |
| 5943 | {0x00, 0x00, 0x06, 0x01}, {0x80, 0x00, 0x0e, 0x00}, {0x81, 0x00, 0x0f, 0x00}, |
| 5944 | {0x82, 0x00, 0x10, 0x00}, {0x83, 0x00, 0x11, 0x00}, {0x84, 0x00, 0x12, 0x00}, |
| 5945 | {0x85, 0x00, 0x13, 0x00}, {0x86, 0x00, 0x14, 0x00}, {0x87, 0x00, 0x15, 0x00}, |
| 5946 | {0x88, 0x00, 0x16, 0x00}, {0x89, 0x00, 0x17, 0x00}, {0x8a, 0x00, 0x18, 0x00}, |
| 5947 | {0x8b, 0x00, 0x19, 0x00}, {0x8c, 0x00, 0x1a, 0x00}, {0x8d, 0x00, 0x1b, 0x00}, |
| 5948 | {0x8e, 0x00, 0x1c, 0x00}, {0x8f, 0x00, 0x1d, 0x00}, {0x90, 0x00, 0x1e, 0x00}, |
| 5949 | {0x91, 0x00, 0x1f, 0x00}, {0x92, 0x00, 0x20, 0x00}, {0x93, 0x00, 0x21, 0x00}, |
| 5950 | {0x9b, 0x00, 0xab, 0x01}, {0x80, 0x00, 0x93, 0x01}, {0x81, 0x00, 0x94, 0x01}, |
| 5951 | {0x82, 0x00, 0x95, 0x01}, {0x83, 0x00, 0x96, 0x01}, {0x84, 0x00, 0x97, 0x01}, |
| 5952 | {0x85, 0x00, 0x98, 0x01}, {0x86, 0x00, 0x99, 0x01}, {0x87, 0x00, 0x9a, 0x01}, |
| 5953 | {0x88, 0x00, 0x9b, 0x01}, {0x89, 0x00, 0x9c, 0x01}, {0x8a, 0x00, 0x9d, 0x01}, |
| 5954 | {0x8b, 0x00, 0xa1, 0x01}, {0x8c, 0x00, 0xa2, 0x01}, {0x8d, 0x00, 0xa3, 0x01}, |
| 5955 | {0x8e, 0x00, 0xa4, 0x01}, {0x8f, 0x00, 0xa5, 0x01}, {0x90, 0x00, 0xa6, 0x01}, |
| 5956 | {0x91, 0x00, 0xa7, 0x01}, {0x92, 0x00, 0xa8, 0x01}, {0x93, 0x00, 0xa9, 0x01}}; |
| 5957 | |
| 5958 | static rk_tree_node * |
| 5959 | rk_lookup(uint8_t state, uint8_t code) |
| 5960 | { |
| 5961 | if (state < sizeof(rk_tree_idx) / sizeof(uint16_t)) { |
| 5962 | uint16_t ns = state ? rk_tree_idx[state - 1] : 0; |
| 5963 | uint16_t ne = rk_tree_idx[state]; |
| 5964 | while (ns < ne) { |
| 5965 | uint16_t m = (ns + ne) >> 1; |
| 5966 | rk_tree_node *rn = &rk_tree[m]; |
| 5967 | if (rn->code == code) { |
| 5968 | return rn; |
| 5969 | } |
| 5970 | if (rn->code < code) { |
| 5971 | ns = m + 1; |
| 5972 | } else { |
| 5973 | ne = m; |
| 5974 | } |
| 5975 | } |
| 5976 | } |
| 5977 | return NULL; |
| 5978 | } |
| 5979 | |
| 5980 | static uint32_t |
| 5981 | rk_emit(rk_tree_node *rn, char **str) |
| 5982 | { |
| 5983 | if (rn && rn->emit != 0xff) { |
| 5984 | uint16_t pos = rn->emit ? rk_str_idx[rn->emit - 1] : 0; |
| 5985 | *str = &rk_str[pos]; |
| 5986 | return (uint32_t)(rk_str_idx[rn->emit] - pos); |
| 5987 | } else { |
| 5988 | *str = NULL; |
| 5989 | return 0; |
| 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | #define RK_OUTPUT(e, l) \ |
| 5994 | do { \ |
| 5995 | if (oc < oe) { \ |
| 5996 | uint32_t l_ = (oc + (l) < oe) ? (l) : (oe - oc); \ |
| 5997 | grn_memcpy(oc, (e), l_); \ |
| 5998 | oc += l_; \ |
| 5999 | ic_ = ic; \ |
| 6000 | } \ |
| 6001 | } while (0) |
| 6002 | |
| 6003 | static uint32_t |
| 6004 | rk_conv(const char *str, |
| 6005 | uint32_t str_len, |
| 6006 | uint8_t *buf, |
| 6007 | uint32_t buf_size, |
| 6008 | uint8_t *statep) |
| 6009 | { |
| 6010 | uint32_t l; |
| 6011 | uint8_t state = 0; |
| 6012 | rk_tree_node *rn; |
| 6013 | char *e; |
| 6014 | uint8_t *oc = buf, *oe = oc + buf_size; |
| 6015 | const uint8_t *ic = (uint8_t *)str, *ic_ = ic, *ie = ic + str_len; |
| 6016 | while (ic < ie) { |
| 6017 | if ((rn = rk_lookup(state, *ic))) { |
| 6018 | ic++; |
| 6019 | if ((l = rk_emit(rn, &e))) { |
| 6020 | RK_OUTPUT(e, l); |
| 6021 | } |
| 6022 | state = rn->next; |
| 6023 | } else { |
| 6024 | if (!state) { |
| 6025 | ic++; |
| 6026 | } |
| 6027 | if (ic_ < ic) { |
| 6028 | RK_OUTPUT(ic_, ic - ic_); |
| 6029 | } |
| 6030 | state = 0; |
| 6031 | } |
| 6032 | } |
| 6033 | #ifdef FLUSH_UNRESOLVED_INPUT |
| 6034 | if ((rn = rk_lookup(state, 0))) { |
| 6035 | if ((l = rk_emit(rn, &e))) { |
| 6036 | RK_OUTPUT(e, l); |
| 6037 | } |
| 6038 | state = rn->next; |
| 6039 | } else { |
| 6040 | if (ic_ < ic) { |
| 6041 | RK_OUTPUT(ic_, ic - ic_); |
| 6042 | } |
| 6043 | } |
| 6044 | #endif /* FLUSH_UNRESOLVED_INPUT */ |
| 6045 | *statep = state; |
| 6046 | return oc - buf; |
| 6047 | } |
| 6048 | |
| 6049 | static grn_id |
| 6050 | sub_search(grn_ctx *ctx, |
| 6051 | grn_pat *pat, |
| 6052 | grn_id id, |
| 6053 | int32_t *c0, |
| 6054 | uint8_t *key, |
| 6055 | uint32_t key_len) |
| 6056 | { |
| 6057 | pat_node_common *pn; |
| 6058 | int32_t len = key_len * 16; |
| 6059 | if (!key_len) { |
| 6060 | return id; |
| 6061 | } |
| 6062 | PAT_AT(pat, id, pn); |
| 6063 | while (pn) { |
| 6064 | int32_t ch; |
| 6065 | ch = pat_node_get_check(pat, pn); |
| 6066 | if (*c0 < ch && ch < len - 1) { |
| 6067 | if (ch & 1) { |
| 6068 | id = (ch + 1 < len) ? pat_node_get_right(pat, pn) |
| 6069 | : pat_node_get_left(pat, pn); |
| 6070 | } else { |
| 6071 | id = pat_node_get_child(pat, pn, nth_bit(key, ch)); |
| 6072 | } |
| 6073 | *c0 = ch; |
| 6074 | PAT_AT(pat, id, pn); |
| 6075 | } else { |
| 6076 | const uint8_t *k = pat_node_get_key(ctx, pat, pn); |
| 6077 | return (k && key_len <= pat_node_get_key_length(pat, pn) && |
| 6078 | !memcmp(k, key, key_len)) |
| 6079 | ? id |
| 6080 | : GRN_ID_NIL; |
| 6081 | } |
| 6082 | } |
| 6083 | return GRN_ID_NIL; |
| 6084 | } |
| 6085 | |
| 6086 | static void |
| 6087 | search_push(grn_ctx *ctx, |
| 6088 | grn_pat *pat, |
| 6089 | grn_pat_cursor *c, |
| 6090 | uint8_t *key, |
| 6091 | uint32_t key_len, |
| 6092 | uint8_t state, |
| 6093 | grn_id id, |
| 6094 | int c0, |
| 6095 | int flags) |
| 6096 | { |
| 6097 | if (state) { |
| 6098 | int step; |
| 6099 | uint16_t ns, ne; |
| 6100 | if (flags & GRN_CURSOR_DESCENDING) { |
| 6101 | ns = rk_tree_idx[state - 1]; |
| 6102 | ne = rk_tree_idx[state]; |
| 6103 | step = 1; |
| 6104 | } else { |
| 6105 | ns = rk_tree_idx[state] - 1; |
| 6106 | ne = rk_tree_idx[state - 1] - 1; |
| 6107 | step = -1; |
| 6108 | } |
| 6109 | for (; ns != ne; ns += step) { |
| 6110 | rk_tree_node *rn = &rk_tree[ns]; |
| 6111 | if (rn->attr) { |
| 6112 | char *e; |
| 6113 | uint32_t l = rk_emit(rn, &e); |
| 6114 | if (l) { |
| 6115 | if (l + key_len <= GRN_TABLE_MAX_KEY_SIZE) { |
| 6116 | int32_t ch = c0; |
| 6117 | grn_id i; |
| 6118 | grn_memcpy(key + key_len, e, l); |
| 6119 | if ((i = sub_search(ctx, pat, id, &ch, key, key_len + l))) { |
| 6120 | search_push(ctx, |
| 6121 | pat, |
| 6122 | c, |
| 6123 | key, |
| 6124 | key_len + l, |
| 6125 | rn->next, |
| 6126 | i, |
| 6127 | ch, |
| 6128 | flags); |
| 6129 | } |
| 6130 | } |
| 6131 | } else { |
| 6132 | search_push(ctx, pat, c, key, key_len, rn->next, id, c0, flags); |
| 6133 | } |
| 6134 | } |
| 6135 | } |
| 6136 | } else { |
| 6137 | pat_node_common *node; |
| 6138 | PAT_AT(pat, id, node); |
| 6139 | if (node) { |
| 6140 | int32_t check = pat_node_get_check(pat, node); |
| 6141 | int32_t check_max = PAT_CHECK_PACK(key_len, 0, false); |
| 6142 | if (c0 < check) { |
| 6143 | if (flags & GRN_CURSOR_DESCENDING) { |
| 6144 | if ((check > check_max - 1) || !(flags & GRN_CURSOR_GT)) { |
| 6145 | push(c, pat_node_get_left(pat, node), check); |
| 6146 | } |
| 6147 | push(c, pat_node_get_right(pat, node), check); |
| 6148 | } else { |
| 6149 | push(c, pat_node_get_right(pat, node), check); |
| 6150 | if ((check > check_max - 1) || !(flags & GRN_CURSOR_GT)) { |
| 6151 | push(c, pat_node_get_left(pat, node), check); |
| 6152 | } |
| 6153 | } |
| 6154 | } else { |
| 6155 | if (PAT_CHECK_PACK(pat_node_get_key_length(pat, node), 0, false) > |
| 6156 | (uint32_t)check_max || |
| 6157 | !(flags & GRN_CURSOR_GT)) { |
| 6158 | push(c, id, check); |
| 6159 | } |
| 6160 | } |
| 6161 | } |
| 6162 | } |
| 6163 | } |
| 6164 | |
| 6165 | static grn_rc |
| 6166 | set_cursor_rk(grn_ctx *ctx, |
| 6167 | grn_pat *pat, |
| 6168 | grn_pat_cursor *c, |
| 6169 | const void *key, |
| 6170 | uint32_t key_len, |
| 6171 | int flags) |
| 6172 | { |
| 6173 | grn_id id; |
| 6174 | uint8_t state; |
| 6175 | pat_node_common *node; |
| 6176 | int c0 = -1; |
| 6177 | uint32_t byte_len; |
| 6178 | uint8_t keybuf[GRN_TABLE_MAX_KEY_SIZE]; |
| 6179 | if (flags & GRN_CURSOR_SIZE_BY_BIT) { |
| 6180 | return GRN_OPERATION_NOT_SUPPORTED; |
| 6181 | } |
| 6182 | byte_len = rk_conv(key, key_len, keybuf, GRN_TABLE_MAX_KEY_SIZE, &state); |
| 6183 | PAT_AT(pat, 0, node); |
| 6184 | if (node) { |
| 6185 | id = pat_node_get_right(pat, node); |
| 6186 | if ((id = sub_search(ctx, pat, id, &c0, keybuf, byte_len))) { |
| 6187 | search_push(ctx, pat, c, keybuf, byte_len, state, id, c0, flags); |
| 6188 | } |
| 6189 | } |
| 6190 | return ctx->rc; |
| 6191 | } |
| 6192 | |
| 6193 | uint64_t |
| 6194 | grn_pat_total_key_size(grn_ctx *ctx, grn_pat *pat) |
| 6195 | { |
| 6196 | if (pat_is_key_large(pat)) { |
| 6197 | return pat->header->curr_key_large; |
| 6198 | } else { |
| 6199 | return pat->header->curr_key; |
| 6200 | } |
| 6201 | } |
| 6202 | |
| 6203 | uint64_t |
| 6204 | grn_pat_max_total_key_size(grn_ctx *ctx, grn_pat *pat) |
| 6205 | { |
| 6206 | if (pat_is_key_large(pat)) { |
| 6207 | return GRN_PAT_MAX_TOTAL_KEY_SIZE_LARGE; |
| 6208 | } else { |
| 6209 | return GRN_PAT_MAX_TOTAL_KEY_SIZE; |
| 6210 | } |
| 6211 | } |
| 6212 | |
| 6213 | bool |
| 6214 | grn_pat_is_key_encoded(grn_ctx *ctx, grn_pat *pat) |
| 6215 | { |
| 6216 | grn_obj *domain; |
| 6217 | uint32_t key_size; |
| 6218 | |
| 6219 | domain = grn_ctx_at(ctx, pat->obj.header.domain); |
| 6220 | if (grn_obj_is_type(ctx, domain)) { |
| 6221 | key_size = grn_type_size(ctx, domain); |
| 6222 | } else { |
| 6223 | key_size = sizeof(grn_id); |
| 6224 | } |
| 6225 | |
| 6226 | return KEY_NEEDS_CONVERT(pat, key_size); |
| 6227 | } |
| 6228 | |
| 6229 | grn_rc |
| 6230 | grn_pat_dirty(grn_ctx *ctx, grn_pat *pat) |
| 6231 | { |
| 6232 | grn_rc rc = GRN_SUCCESS; |
| 6233 | |
| 6234 | CRITICAL_SECTION_ENTER(pat->lock); |
| 6235 | if (!pat->is_dirty) { |
| 6236 | uint32_t n_dirty_opens; |
| 6237 | pat->is_dirty = true; |
| 6238 | GRN_ATOMIC_ADD_EX(&(pat->header->n_dirty_opens), 1, n_dirty_opens); |
| 6239 | rc = grn_io_flush(ctx, pat->io); |
| 6240 | } |
| 6241 | CRITICAL_SECTION_LEAVE(pat->lock); |
| 6242 | |
| 6243 | return rc; |
| 6244 | } |
| 6245 | |
| 6246 | bool |
| 6247 | grn_pat_is_dirty(grn_ctx *ctx, grn_pat *pat) |
| 6248 | { |
| 6249 | return pat->header->n_dirty_opens > 0; |
| 6250 | } |
| 6251 | |
| 6252 | grn_rc |
| 6253 | grn_pat_clean(grn_ctx *ctx, grn_pat *pat) |
| 6254 | { |
| 6255 | grn_rc rc = GRN_SUCCESS; |
| 6256 | |
| 6257 | CRITICAL_SECTION_ENTER(pat->lock); |
| 6258 | if (pat->is_dirty) { |
| 6259 | uint32_t n_dirty_opens; |
| 6260 | pat->is_dirty = false; |
| 6261 | GRN_ATOMIC_ADD_EX(&(pat->header->n_dirty_opens), -1, n_dirty_opens); |
| 6262 | rc = grn_io_flush(ctx, pat->io); |
| 6263 | } |
| 6264 | CRITICAL_SECTION_LEAVE(pat->lock); |
| 6265 | |
| 6266 | return rc; |
| 6267 | } |
| 6268 | |
| 6269 | grn_rc |
| 6270 | grn_pat_clear_dirty(grn_ctx *ctx, grn_pat *pat) |
| 6271 | { |
| 6272 | grn_rc rc = GRN_SUCCESS; |
| 6273 | |
| 6274 | CRITICAL_SECTION_ENTER(pat->lock); |
| 6275 | pat->is_dirty = false; |
| 6276 | pat->header->n_dirty_opens = 0; |
| 6277 | rc = grn_io_flush(ctx, pat->io); |
| 6278 | CRITICAL_SECTION_LEAVE(pat->lock); |
| 6279 | |
| 6280 | return rc; |
| 6281 | } |
| 6282 | |
| 6283 | typedef struct { |
| 6284 | grn_ctx *ctx; |
| 6285 | grn_pat *pat; |
| 6286 | } pat_node_compare_by_key_data; |
| 6287 | |
| 6288 | int |
| 6289 | grn_pat_node_compare_by_key(const grn_id id1, const grn_id id2, void *arg) |
| 6290 | { |
| 6291 | grn_ctx *ctx = ((pat_node_compare_by_key_data *)arg)->ctx; |
| 6292 | grn_pat *pat = ((pat_node_compare_by_key_data *)arg)->pat; |
| 6293 | |
| 6294 | pat_node_common *node1 = pat_node_get(ctx, pat, id1); |
| 6295 | pat_node_common *node2 = pat_node_get(ctx, pat, id2); |
| 6296 | uint64_t node1_key_offset = pat_node_get_key_offset(pat, node1); |
| 6297 | uint64_t node2_key_offset = pat_node_get_key_offset(pat, node2); |
| 6298 | |
| 6299 | if (node1_key_offset > node2_key_offset) { |
| 6300 | return 1; |
| 6301 | } |
| 6302 | if (node1_key_offset < node2_key_offset) { |
| 6303 | return -1; |
| 6304 | } |
| 6305 | return 0; |
| 6306 | } |
| 6307 | |
| 6308 | static inline void |
| 6309 | pat_key_move(grn_ctx *ctx, |
| 6310 | grn_pat *pat, |
| 6311 | pat_node_common *node, |
| 6312 | uint64_t new_position, |
| 6313 | uint8_t *key, |
| 6314 | uint32_t key_size) |
| 6315 | { |
| 6316 | uint8_t *new_key_address; |
| 6317 | KEY_AT(pat, new_position, new_key_address, 0); |
| 6318 | grn_memmove(new_key_address, key, key_size); |
| 6319 | pat_node_set_key_position(pat, node, new_position); |
| 6320 | } |
| 6321 | |
| 6322 | typedef void (*pat_key_defrag_each_callback)( |
| 6323 | grn_ctx *ctx, pat_node_common *node, grn_pat_wal_add_entry_data *wal_data); |
| 6324 | |
| 6325 | static inline void |
| 6326 | pat_key_defrag_callback(grn_ctx *ctx, |
| 6327 | pat_node_common *node, |
| 6328 | grn_pat_wal_add_entry_data *wal_data) |
| 6329 | { |
| 6330 | pat_key_move(ctx, |
| 6331 | wal_data->pat, |
| 6332 | node, |
| 6333 | wal_data->key_offset, |
| 6334 | (uint8_t *)(wal_data->key), |
| 6335 | wal_data->key_size); |
| 6336 | } |
| 6337 | |
| 6338 | static inline void |
| 6339 | pat_key_defrag_wal_add_entry_callback(grn_ctx *ctx, |
| 6340 | pat_node_common *node, |
| 6341 | grn_pat_wal_add_entry_data *wal_data) |
| 6342 | { |
| 6343 | grn_pat_wal_add_entry(ctx, wal_data); |
| 6344 | } |
| 6345 | |
| 6346 | static inline uint64_t |
| 6347 | pat_key_defrag_each(grn_ctx *ctx, |
| 6348 | grn_pat *pat, |
| 6349 | grn_id *target_ids, |
| 6350 | size_t n_targets, |
| 6351 | pat_key_defrag_each_callback callback) |
| 6352 | { |
| 6353 | uint64_t new_curr_key = 0; |
| 6354 | for (size_t i = 0; i < n_targets; i++) { |
| 6355 | grn_id record_id = target_ids[i]; |
| 6356 | pat_node_common *node = pat_node_get(ctx, pat, record_id); |
| 6357 | |
| 6358 | /* Check if the key to be added can fit in the current segment. */ |
| 6359 | uint32_t key_length = pat_node_get_key_length(pat, node); |
| 6360 | uint64_t new_end_segment = |
| 6361 | (new_curr_key + key_length - 1) >> W_OF_KEY_IN_A_SEGMENT; |
| 6362 | if (new_curr_key >> W_OF_KEY_IN_A_SEGMENT != new_end_segment) { |
| 6363 | /* If it does not fit, update `new_curr_key` to add it to the next |
| 6364 | * segment. */ |
| 6365 | new_curr_key = new_end_segment << W_OF_KEY_IN_A_SEGMENT; |
| 6366 | } |
| 6367 | |
| 6368 | /* If the offset is the same, do not copy because the same key is already |
| 6369 | * there. */ |
| 6370 | uint64_t node_key_offset = pat_node_get_key_offset(pat, node); |
| 6371 | if (node_key_offset != new_curr_key) { |
| 6372 | uint8_t *key_address; |
| 6373 | KEY_AT(pat, node_key_offset, key_address, 0); |
| 6374 | |
| 6375 | grn_pat_wal_add_entry_data wal_data = {0}; |
| 6376 | wal_data.event = GRN_WAL_EVENT_DEFRAG_KEY; |
| 6377 | wal_data.pat = pat; |
| 6378 | wal_data.record_id = record_id; |
| 6379 | wal_data.key = key_address; |
| 6380 | wal_data.key_size = key_length; |
| 6381 | wal_data.key_offset = new_curr_key; |
| 6382 | wal_data.tag = "[pat][defrag][key]"; |
| 6383 | |
| 6384 | callback(ctx, node, &wal_data); |
| 6385 | } |
| 6386 | new_curr_key += key_length; |
| 6387 | } |
| 6388 | return new_curr_key; |
| 6389 | } |
| 6390 | |
| 6391 | static inline grn_rc |
| 6392 | delinfos_turn_2_force(grn_ctx *ctx, grn_pat *pat) |
| 6393 | { |
| 6394 | for (uint32_t di_index = 0; di_index < GRN_PAT_NDELINFOS; di_index++) { |
| 6395 | grn_pat_delinfo *delete_info = &(pat->header->delinfos[di_index]); |
| 6396 | if (delete_info->stat != DL_PHASE1) { |
| 6397 | continue; |
| 6398 | } |
| 6399 | |
| 6400 | grn_pat_wal_add_entry_data wal_data = {0}; |
| 6401 | wal_data.tag = "[pat][defrag][delinfos][phase2]"; |
| 6402 | wal_data.pat = pat; |
| 6403 | wal_data.delete_info_index = di_index; |
| 6404 | grn_rc rc = delinfo_turn_2(ctx, pat, &wal_data, di_index); |
| 6405 | if (rc != GRN_SUCCESS) { |
| 6406 | ERR(rc, "%s: failed %d", wal_data.tag, delete_info->ld); |
| 6407 | return rc; |
| 6408 | } |
| 6409 | delinfo_turn_2_post(ctx, pat); |
| 6410 | } |
| 6411 | return GRN_SUCCESS; |
| 6412 | } |
| 6413 | |
| 6414 | /* Clear to avoid reuse of unexpected nodes. */ |
| 6415 | static inline void |
| 6416 | grn_pat_defrag_clear_delinfos(grn_ctx *ctx, grn_pat *pat, grn_id active_max_id) |
| 6417 | { |
| 6418 | memset(pat->header->garbages, 0, sizeof(pat->header->garbages)); |
| 6419 | memset(pat->header->delinfos, 0, sizeof(pat->header->delinfos)); |
| 6420 | pat->header->curr_rec = active_max_id; |
| 6421 | pat->header->curr_del = 0; |
| 6422 | pat->header->curr_del2 = 0; |
| 6423 | pat->header->curr_del3 = 0; |
| 6424 | pat->header->n_garbages = 0; |
| 6425 | } |
| 6426 | |
| 6427 | /* See test/command/suite/defrag/pat/README.md when you change this. |
| 6428 | * You must update tests for this too. |
| 6429 | * If you're using a grn_pat in multi-thread/multi-process environment, you must |
| 6430 | * use grn_obj_defrag() instead of using this function directly. */ |
| 6431 | uint64_t |
| 6432 | grn_pat_defrag(grn_ctx *ctx, grn_pat *pat) |
| 6433 | { |
| 6434 | grn_pat_wal_add_entry_data wal_data = {0}; |
| 6435 | wal_data.event = GRN_WAL_EVENT_DEFRAG_CURRENT_KEY; |
| 6436 | wal_data.pat = pat; |
| 6437 | wal_data.tag = "[pat][defrag][current-key]"; |
| 6438 | |
| 6439 | uint64_t reduced_bytes = 0; |
| 6440 | /* We will clear grn_pat_header::delinfos and grn_pat_header::garbages after |
| 6441 | * defragmentation. Execute delinfo_turn_2() on the data remaining in |
| 6442 | * grn_pat_header::delinfos before clearing. |
| 6443 | * |
| 6444 | * Since grn_pat_header::delinfos and grn_pat_header::garbages are cleared |
| 6445 | * after defragmentation, exercise of delinfo_turn_3() is not needed. */ |
| 6446 | if (delinfos_turn_2_force(ctx, pat) != GRN_SUCCESS) { |
| 6447 | return reduced_bytes; |
| 6448 | } |
| 6449 | |
| 6450 | uint32_t n_records = grn_pat_size(ctx, pat); |
| 6451 | if (n_records == 0) { |
| 6452 | uint64_t new_curr_key = 0; |
| 6453 | grn_id active_max_id = 0; |
| 6454 | wal_data.key_offset = new_curr_key; |
| 6455 | wal_data.record_id = active_max_id; |
| 6456 | grn_pat_wal_add_entry(ctx, &wal_data); |
| 6457 | |
| 6458 | reduced_bytes = grn_pat_total_key_size(ctx, pat); |
| 6459 | pat_update_total_key_size(ctx, pat, new_curr_key); |
| 6460 | grn_pat_defrag_clear_delinfos(ctx, pat, active_max_id); |
| 6461 | return reduced_bytes; |
| 6462 | } |
| 6463 | |
| 6464 | /* First, get the number of targets. */ |
| 6465 | size_t n_targets = 0; |
| 6466 | grn_id active_max_id = GRN_ID_NIL; |
| 6467 | GRN_PAT_EACH_BEGIN(ctx, pat, cursor, id) |
| 6468 | { |
| 6469 | pat_node_common *node = pat_node_get(ctx, pat, id); |
| 6470 | if (active_max_id < id) { |
| 6471 | active_max_id = id; |
| 6472 | } |
| 6473 | if (pat_node_is_key_immediate(pat, node)) { |
| 6474 | continue; |
| 6475 | } |
| 6476 | n_targets++; |
| 6477 | } |
| 6478 | GRN_PAT_EACH_END(ctx, cursor); |
| 6479 | |
| 6480 | /* Allocate only the necessary areas. */ |
| 6481 | grn_id *target_ids = GRN_MALLOC(sizeof(grn_id) * n_targets); |
| 6482 | n_targets = 0; |
| 6483 | GRN_PAT_EACH_BEGIN(ctx, pat, cursor, id) |
| 6484 | { |
| 6485 | pat_node_common *node = pat_node_get(ctx, pat, id); |
| 6486 | if (pat_node_is_key_immediate(pat, node)) { |
| 6487 | continue; |
| 6488 | } |
| 6489 | target_ids[n_targets++] = id; |
| 6490 | } |
| 6491 | GRN_PAT_EACH_END(ctx, cursor); |
| 6492 | |
| 6493 | /* Defragment from the head of the key segment. */ |
| 6494 | pat_node_compare_by_key_data data = {ctx, pat}; |
| 6495 | grn_qsort_r_grn_id(target_ids, n_targets, grn_pat_node_compare_by_key, &data); |
| 6496 | |
| 6497 | uint64_t new_curr_key = |
| 6498 | pat_key_defrag_each(ctx, |
| 6499 | pat, |
| 6500 | target_ids, |
| 6501 | n_targets, |
| 6502 | pat_key_defrag_wal_add_entry_callback); |
| 6503 | wal_data.record_id = active_max_id; |
| 6504 | wal_data.key_offset = new_curr_key; |
| 6505 | grn_pat_wal_add_entry(ctx, &wal_data); |
| 6506 | |
| 6507 | pat_key_defrag_each(ctx, pat, target_ids, n_targets, pat_key_defrag_callback); |
| 6508 | reduced_bytes = grn_pat_total_key_size(ctx, pat) - new_curr_key; |
| 6509 | pat_update_total_key_size(ctx, pat, new_curr_key); |
| 6510 | grn_pat_defrag_clear_delinfos(ctx, pat, active_max_id); |
| 6511 | |
| 6512 | GRN_FREE(target_ids); |
| 6513 | return reduced_bytes; |
| 6514 | } |
| 6515 | |
| 6516 | static void |
| 6517 | grn_pat_wal_recover_add_entry(grn_ctx *ctx, |
| 6518 | grn_pat *pat, |
| 6519 | grn_wal_reader_entry *entry, |
| 6520 | const char *tag, |
| 6521 | const char *wal_error_tag) |
| 6522 | { |
| 6523 | pat_update_total_key_size(ctx, pat, entry->key_offset); |
| 6524 | pat->header->n_entries = entry->n_entries; |
| 6525 | pat_node_common *node = pat_node_get(ctx, pat, entry->record_id); |
| 6526 | if (!node) { |
| 6527 | grn_wal_set_recover_error(ctx, |
| 6528 | GRN_NO_MEMORY_AVAILABLE, |
| 6529 | (grn_obj *)pat, |
| 6530 | entry, |
| 6531 | wal_error_tag, |
| 6532 | "failed to refer node"); |
| 6533 | return; |
| 6534 | } |
| 6535 | pat_node_common *parent_node = |
| 6536 | pat_node_get(ctx, pat, entry->parent_record_id); |
| 6537 | if (!parent_node) { |
| 6538 | grn_wal_set_recover_error(ctx, |
| 6539 | GRN_NO_MEMORY_AVAILABLE, |
| 6540 | (grn_obj *)pat, |
| 6541 | entry, |
| 6542 | wal_error_tag, |
| 6543 | "failed to refer parent node"); |
| 6544 | return; |
| 6545 | } |
| 6546 | if (ctx->rc != GRN_SUCCESS) { |
| 6547 | return; |
| 6548 | } |
| 6549 | grn_id *id_location = |
| 6550 | pat_node_get_child_address(pat, parent_node, entry->record_direction); |
| 6551 | *id_location = entry->previous_record_id; |
| 6552 | int32_t check_max = PAT_CHECK_PACK(entry->key.content.binary.size, 0, false); |
| 6553 | grn_pat_add_node(ctx, |
| 6554 | pat, |
| 6555 | node, |
| 6556 | entry->record_id, |
| 6557 | entry->key.content.binary.data, |
| 6558 | entry->key.content.binary.size, |
| 6559 | entry->check, |
| 6560 | check_max, |
| 6561 | id_location, |
| 6562 | tag); |
| 6563 | } |
| 6564 | |
| 6565 | static void |
| 6566 | grn_pat_wal_recover_add_shared_entry(grn_ctx *ctx, |
| 6567 | grn_pat *pat, |
| 6568 | grn_wal_reader_entry *entry, |
| 6569 | const char *wal_error_tag) |
| 6570 | { |
| 6571 | pat->header->n_entries = entry->n_entries; |
| 6572 | pat_node_common *node = pat_node_get(ctx, pat, entry->record_id); |
| 6573 | if (!node) { |
| 6574 | grn_wal_set_recover_error(ctx, |
| 6575 | GRN_NO_MEMORY_AVAILABLE, |
| 6576 | (grn_obj *)pat, |
| 6577 | entry, |
| 6578 | wal_error_tag, |
| 6579 | "failed to refer node"); |
| 6580 | return; |
| 6581 | } |
| 6582 | pat_node_common *parent_node = |
| 6583 | pat_node_get(ctx, pat, entry->parent_record_id); |
| 6584 | if (!parent_node) { |
| 6585 | grn_wal_set_recover_error(ctx, |
| 6586 | GRN_NO_MEMORY_AVAILABLE, |
| 6587 | (grn_obj *)pat, |
| 6588 | entry, |
| 6589 | wal_error_tag, |
| 6590 | "failed to refer parent node"); |
| 6591 | return; |
| 6592 | } |
| 6593 | grn_id *id_location = |
| 6594 | pat_node_get_child_address(pat, parent_node, entry->record_direction); |
| 6595 | *id_location = entry->previous_record_id; |
| 6596 | int32_t check_max = PAT_CHECK_PACK(entry->key.content.binary.size, 0, false); |
| 6597 | grn_pat_add_shared_node(ctx, |
| 6598 | pat, |
| 6599 | node, |
| 6600 | entry->record_id, |
| 6601 | entry->key.content.binary.data, |
| 6602 | entry->key.content.binary.size, |
| 6603 | (uint32_t)(entry->shared_key_offset), |
| 6604 | entry->check, |
| 6605 | check_max, |
| 6606 | id_location); |
| 6607 | } |
| 6608 | |
| 6609 | static void |
| 6610 | grn_pat_wal_recover_reuse_entry(grn_ctx *ctx, |
| 6611 | grn_pat *pat, |
| 6612 | grn_wal_reader_entry *entry, |
| 6613 | const char *tag, |
| 6614 | const char *wal_error_tag) |
| 6615 | { |
| 6616 | pat->header->n_garbages = entry->n_garbages; |
| 6617 | pat->header->n_entries = entry->n_entries; |
| 6618 | pat_node_common *node; |
| 6619 | PAT_AT(pat, entry->record_id, node); |
| 6620 | if (!node) { |
| 6621 | grn_wal_set_recover_error(ctx, |
| 6622 | GRN_NO_MEMORY_AVAILABLE, |
| 6623 | (grn_obj *)pat, |
| 6624 | entry, |
| 6625 | wal_error_tag, |
| 6626 | "failed to refer node"); |
| 6627 | return; |
| 6628 | } |
| 6629 | pat_node_common *parent_node; |
| 6630 | PAT_AT(pat, entry->parent_record_id, parent_node); |
| 6631 | if (!parent_node) { |
| 6632 | grn_wal_set_recover_error(ctx, |
| 6633 | GRN_NO_MEMORY_AVAILABLE, |
| 6634 | (grn_obj *)pat, |
| 6635 | entry, |
| 6636 | wal_error_tag, |
| 6637 | "failed to refer parent node"); |
| 6638 | return; |
| 6639 | } |
| 6640 | grn_id *id_location = |
| 6641 | pat_node_get_child_address(pat, parent_node, entry->record_direction); |
| 6642 | *id_location = entry->previous_record_id; |
| 6643 | int32_t check_max = PAT_CHECK_PACK(entry->key.content.binary.size, 0, false); |
| 6644 | grn_pat_reuse_node(ctx, |
| 6645 | pat, |
| 6646 | node, |
| 6647 | entry->record_id, |
| 6648 | entry->key.content.binary.data, |
| 6649 | entry->key.content.binary.size, |
| 6650 | entry->check, |
| 6651 | check_max, |
| 6652 | id_location, |
| 6653 | tag); |
| 6654 | } |
| 6655 | |
| 6656 | static void |
| 6657 | grn_pat_wal_recover_reuse_shared_entry(grn_ctx *ctx, |
| 6658 | grn_pat *pat, |
| 6659 | grn_wal_reader_entry *entry, |
| 6660 | const char *wal_error_tag) |
| 6661 | { |
| 6662 | pat_node_common *node = pat_node_get(ctx, pat, entry->record_id); |
| 6663 | if (!node) { |
| 6664 | grn_wal_set_recover_error(ctx, |
| 6665 | GRN_NO_MEMORY_AVAILABLE, |
| 6666 | (grn_obj *)pat, |
| 6667 | entry, |
| 6668 | wal_error_tag, |
| 6669 | "failed to refer node"); |
| 6670 | return; |
| 6671 | } |
| 6672 | pat_node_set_left(pat, node, entry->next_garbage_record_id); |
| 6673 | pat_node_common *parent_node = |
| 6674 | pat_node_get(ctx, pat, entry->parent_record_id); |
| 6675 | if (!parent_node) { |
| 6676 | grn_wal_set_recover_error(ctx, |
| 6677 | GRN_NO_MEMORY_AVAILABLE, |
| 6678 | (grn_obj *)pat, |
| 6679 | entry, |
| 6680 | wal_error_tag, |
| 6681 | "failed to refer parent node"); |
| 6682 | return; |
| 6683 | } |
| 6684 | grn_id *id_location = |
| 6685 | pat_node_get_child_address(pat, parent_node, entry->record_direction); |
| 6686 | *id_location = entry->previous_record_id; |
| 6687 | int32_t check_max = PAT_CHECK_PACK(entry->key.content.binary.size, 0, false); |
| 6688 | pat->header->n_garbages = entry->n_garbages; |
| 6689 | pat->header->n_entries = entry->n_entries; |
| 6690 | grn_pat_reuse_shared_node(ctx, |
| 6691 | pat, |
| 6692 | node, |
| 6693 | entry->record_id, |
| 6694 | entry->key.content.binary.data, |
| 6695 | entry->key.content.binary.size, |
| 6696 | (uint32_t)(entry->shared_key_offset), |
| 6697 | entry->check, |
| 6698 | check_max, |
| 6699 | id_location); |
| 6700 | } |
| 6701 | |
| 6702 | static void |
| 6703 | grn_pat_wal_recover_delete_info_phase1(grn_ctx *ctx, |
| 6704 | grn_pat *pat, |
| 6705 | grn_wal_reader_entry *entry, |
| 6706 | const char *wal_error_tag) |
| 6707 | { |
| 6708 | pat->header->curr_del = entry->delete_info_phase1_index; |
| 6709 | grn_pat_delinfo *di; |
| 6710 | uint32_t next_delete_info_index = delinfo_turn_1_pre(ctx, pat, &di); |
| 6711 | di->shared = entry->is_shared; |
| 6712 | delinfo_turn_1_post(ctx, pat, next_delete_info_index); |
| 6713 | } |
| 6714 | |
| 6715 | static void |
| 6716 | grn_pat_wal_recover_delete_info_phase2(grn_ctx *ctx, |
| 6717 | grn_pat *pat, |
| 6718 | grn_wal_reader_entry *entry, |
| 6719 | const char *wal_error_tag) |
| 6720 | { |
| 6721 | pat->header->curr_del2 = entry->delete_info_phase2_index; |
| 6722 | grn_pat_delinfo *di = &(pat->header->delinfos[pat->header->curr_del2]); |
| 6723 | di->stat = DL_PHASE1; |
| 6724 | di->d = entry->record_id; |
| 6725 | pat_node_common *node; |
| 6726 | PAT_AT(pat, di->d, node); |
| 6727 | if (!node) { |
| 6728 | grn_wal_set_recover_error(ctx, |
| 6729 | GRN_NO_MEMORY_AVAILABLE, |
| 6730 | (grn_obj *)pat, |
| 6731 | entry, |
| 6732 | wal_error_tag, |
| 6733 | "failed to refer node"); |
| 6734 | return; |
| 6735 | } |
| 6736 | di->ld = entry->parent_record_id; |
| 6737 | pat_node_common *parent_node; |
| 6738 | PAT_AT(pat, di->ld, parent_node); |
| 6739 | if (!parent_node) { |
| 6740 | grn_wal_set_recover_error(ctx, |
| 6741 | GRN_NO_MEMORY_AVAILABLE, |
| 6742 | (grn_obj *)pat, |
| 6743 | entry, |
| 6744 | wal_error_tag, |
| 6745 | "failed to refer parent node"); |
| 6746 | return; |
| 6747 | } |
| 6748 | pat_node_set_key_length(pat, node, (uint16_t)(entry->key.content.uint64)); |
| 6749 | pat_node_set_check(pat, node, entry->check); |
| 6750 | pat_node_set_left(pat, node, entry->left_record_id); |
| 6751 | pat_node_set_right(pat, node, entry->right_record_id); |
| 6752 | delinfo_turn_2_internal(ctx, pat, di, parent_node, node); |
| 6753 | delinfo_turn_2_post(ctx, pat); |
| 6754 | } |
| 6755 | |
| 6756 | static void |
| 6757 | grn_pat_wal_recover_delete_info_phase3(grn_ctx *ctx, |
| 6758 | grn_pat *pat, |
| 6759 | grn_wal_reader_entry *entry, |
| 6760 | const char *wal_error_tag) |
| 6761 | { |
| 6762 | pat->header->curr_del3 = entry->delete_info_phase3_index; |
| 6763 | grn_pat_delinfo *di = &(pat->header->delinfos[pat->header->curr_del3]); |
| 6764 | di->stat = DL_PHASE2; |
| 6765 | di->d = entry->record_id; |
| 6766 | pat_node_common *node; |
| 6767 | PAT_AT(pat, di->d, node); |
| 6768 | if (!node) { |
| 6769 | grn_wal_set_recover_error(ctx, |
| 6770 | GRN_NO_MEMORY_AVAILABLE, |
| 6771 | (grn_obj *)pat, |
| 6772 | entry, |
| 6773 | wal_error_tag, |
| 6774 | "failed to refer node"); |
| 6775 | return; |
| 6776 | } |
| 6777 | di->shared = entry->is_shared; |
| 6778 | uint32_t size = delinfo_compute_storage_size(ctx, pat, di, node); |
| 6779 | pat->header->garbages[size] = entry->next_garbage_record_id; |
| 6780 | delinfo_turn_3_internal(ctx, pat, di, node); |
| 6781 | delinfo_turn_3_post(ctx, pat); |
| 6782 | } |
| 6783 | |
| 6784 | static void |
| 6785 | grn_pat_wal_recover_delete_entry(grn_ctx *ctx, |
| 6786 | grn_pat *pat, |
| 6787 | grn_wal_reader_entry *entry, |
| 6788 | const char *wal_error_tag) |
| 6789 | { |
| 6790 | pat->header->curr_del = entry->delete_info_phase1_index; |
| 6791 | pat->header->curr_del2 = entry->delete_info_phase2_index; |
| 6792 | grn_pat_del_data data = {0}; |
| 6793 | data.di = &(pat->header->delinfos[entry->delete_info_index]); |
| 6794 | data.id = entry->record_id; |
| 6795 | data.check = entry->check; |
| 6796 | data.check0 = entry->parent_check; |
| 6797 | PAT_AT(pat, data.id, data.node); |
| 6798 | if (!data.node) { |
| 6799 | grn_wal_set_recover_error(ctx, |
| 6800 | GRN_NO_MEMORY_AVAILABLE, |
| 6801 | (grn_obj *)pat, |
| 6802 | entry, |
| 6803 | wal_error_tag, |
| 6804 | "failed to refer node"); |
| 6805 | return; |
| 6806 | } |
| 6807 | PAT_AT(pat, entry->parent_record_id, data.node_previous); |
| 6808 | if (!data.node_previous) { |
| 6809 | grn_wal_set_recover_error(ctx, |
| 6810 | GRN_NO_MEMORY_AVAILABLE, |
| 6811 | (grn_obj *)pat, |
| 6812 | entry, |
| 6813 | wal_error_tag, |
| 6814 | "failed to refer parent node"); |
| 6815 | return; |
| 6816 | } |
| 6817 | pat_node_common *grandparent_node; |
| 6818 | PAT_AT(pat, entry->grandparent_record_id, grandparent_node); |
| 6819 | if (!grandparent_node) { |
| 6820 | grn_wal_set_recover_error(ctx, |
| 6821 | GRN_NO_MEMORY_AVAILABLE, |
| 6822 | (grn_obj *)pat, |
| 6823 | entry, |
| 6824 | wal_error_tag, |
| 6825 | "failed to refer grandparent node"); |
| 6826 | return; |
| 6827 | } |
| 6828 | data.otherside = entry->otherside_record_id; |
| 6829 | if (data.otherside != GRN_ID_NIL) { |
| 6830 | PAT_AT(pat, data.otherside, data.node_otherside); |
| 6831 | if (!data.node_otherside) { |
| 6832 | grn_wal_set_recover_error(ctx, |
| 6833 | GRN_NO_MEMORY_AVAILABLE, |
| 6834 | (grn_obj *)pat, |
| 6835 | entry, |
| 6836 | wal_error_tag, |
| 6837 | "failed to refer otherside node"); |
| 6838 | return; |
| 6839 | } |
| 6840 | } |
| 6841 | pat_node_common *root_node; |
| 6842 | PAT_AT(pat, GRN_ID_NIL, root_node); |
| 6843 | if (!root_node) { |
| 6844 | grn_wal_set_recover_error(ctx, |
| 6845 | GRN_NO_MEMORY_AVAILABLE, |
| 6846 | (grn_obj *)pat, |
| 6847 | entry, |
| 6848 | wal_error_tag, |
| 6849 | "failed to refer root node"); |
| 6850 | return; |
| 6851 | } |
| 6852 | data.proot = pat_node_get_right_address(pat, root_node); |
| 6853 | data.p = pat_node_get_child_address(pat, |
| 6854 | data.node_previous, |
| 6855 | entry->record_direction); |
| 6856 | data.p0 = pat_node_get_child_address(pat, |
| 6857 | grandparent_node, |
| 6858 | entry->parent_record_direction); |
| 6859 | data.n_garbages = entry->n_garbages; |
| 6860 | data.n_entries = entry->n_entries; |
| 6861 | grn_pat_del_internal(ctx, pat, &data); |
| 6862 | } |
| 6863 | |
| 6864 | static void |
| 6865 | grn_pat_wal_recover_defrag_key(grn_ctx *ctx, |
| 6866 | grn_pat *pat, |
| 6867 | grn_wal_reader_entry *entry, |
| 6868 | const char *wal_error_tag) |
| 6869 | { |
| 6870 | pat_node_common *node = pat_node_get(ctx, pat, entry->record_id); |
| 6871 | if (!node) { |
| 6872 | grn_rc rc = ctx->rc; |
| 6873 | if (rc == GRN_SUCCESS) { |
| 6874 | rc = GRN_UNKNOWN_ERROR; |
| 6875 | } |
| 6876 | grn_wal_set_recover_error(ctx, |
| 6877 | rc, |
| 6878 | (grn_obj *)pat, |
| 6879 | entry, |
| 6880 | wal_error_tag, |
| 6881 | "failed to refer node"); |
| 6882 | return; |
| 6883 | } |
| 6884 | pat_key_move(ctx, |
| 6885 | pat, |
| 6886 | node, |
| 6887 | entry->key_offset, |
| 6888 | (uint8_t *)(entry->key.content.binary.data), |
| 6889 | entry->key.content.binary.size); |
| 6890 | } |
| 6891 | |
| 6892 | static void |
| 6893 | grn_pat_wal_recover_defrag_current_key(grn_ctx *ctx, |
| 6894 | grn_pat *pat, |
| 6895 | grn_wal_reader_entry *entry, |
| 6896 | uint64_t defrag_key_start_id, |
| 6897 | const char *tag, |
| 6898 | const char *wal_error_tag) |
| 6899 | { |
| 6900 | grn_wal_reader *reader = grn_wal_reader_open(ctx, (grn_obj *)pat, tag); |
| 6901 | if (ctx->rc != GRN_SUCCESS) { |
| 6902 | return; |
| 6903 | } |
| 6904 | if (!reader) { |
| 6905 | grn_wal_set_recover_error(ctx, |
| 6906 | GRN_UNKNOWN_ERROR, |
| 6907 | (grn_obj *)pat, |
| 6908 | entry, |
| 6909 | wal_error_tag, |
| 6910 | "failed to open WAL reader"); |
| 6911 | return; |
| 6912 | } |
| 6913 | while (true) { |
| 6914 | grn_wal_reader_entry defrag_key_entry = {0}; |
| 6915 | grn_rc rc = grn_wal_reader_read_entry(ctx, reader, &defrag_key_entry); |
| 6916 | if (rc != GRN_SUCCESS) { |
| 6917 | break; |
| 6918 | } |
| 6919 | if (defrag_key_entry.id < defrag_key_start_id) { |
| 6920 | continue; |
| 6921 | } |
| 6922 | /* We don't validate here but event must be GRN_WAL_EVENT_DEFRAG_KEY or |
| 6923 | * GRN_WAL_EVENT_DEFRAG_CURRENT_KEY. */ |
| 6924 | if (defrag_key_entry.event != GRN_WAL_EVENT_DEFRAG_KEY) { |
| 6925 | break; |
| 6926 | } |
| 6927 | grn_pat_wal_recover_defrag_key(ctx, pat, &defrag_key_entry, wal_error_tag); |
| 6928 | if (ctx->rc != GRN_SUCCESS) { |
| 6929 | return; |
| 6930 | } |
| 6931 | } |
| 6932 | grn_wal_reader_close(ctx, reader); |
| 6933 | pat_update_total_key_size(ctx, pat, entry->key_offset); |
| 6934 | grn_pat_defrag_clear_delinfos(ctx, pat, entry->record_id); |
| 6935 | } |
| 6936 | |
| 6937 | grn_rc |
| 6938 | grn_pat_wal_recover(grn_ctx *ctx, grn_pat *pat) |
| 6939 | { |
| 6940 | if (ctx->rc != GRN_SUCCESS) { |
| 6941 | return ctx->rc; |
| 6942 | } |
| 6943 | |
| 6944 | const char *wal_error_tag = "[pat]"; |
| 6945 | const char *tag = "[pat][recover]"; |
| 6946 | grn_wal_reader *reader = grn_wal_reader_open(ctx, (grn_obj *)pat, tag); |
| 6947 | if (ctx->rc != GRN_SUCCESS) { |
| 6948 | return ctx->rc; |
| 6949 | } |
| 6950 | |
| 6951 | bool need_flush = false; |
| 6952 | if (grn_io_is_locked(pat->io)) { |
| 6953 | grn_io_clear_lock(pat->io); |
| 6954 | need_flush = true; |
| 6955 | } |
| 6956 | |
| 6957 | if (reader) { |
| 6958 | uint64_t defrag_key_start_id = 0; |
| 6959 | while (true) { |
| 6960 | grn_wal_reader_entry entry = {0}; |
| 6961 | grn_rc rc = grn_wal_reader_read_entry(ctx, reader, &entry); |
| 6962 | if (rc != GRN_SUCCESS) { |
| 6963 | break; |
| 6964 | } |
| 6965 | switch (entry.event) { |
| 6966 | case GRN_WAL_EVENT_ADD_ENTRY: |
| 6967 | grn_pat_wal_recover_add_entry(ctx, pat, &entry, tag, wal_error_tag); |
| 6968 | break; |
| 6969 | case GRN_WAL_EVENT_ADD_SHARED_ENTRY: |
| 6970 | grn_pat_wal_recover_add_shared_entry(ctx, pat, &entry, wal_error_tag); |
| 6971 | break; |
| 6972 | case GRN_WAL_EVENT_REUSE_ENTRY: |
| 6973 | grn_pat_wal_recover_reuse_entry(ctx, pat, &entry, tag, wal_error_tag); |
| 6974 | break; |
| 6975 | case GRN_WAL_EVENT_REUSE_SHARED_ENTRY: |
| 6976 | grn_pat_wal_recover_reuse_shared_entry(ctx, pat, &entry, wal_error_tag); |
| 6977 | break; |
| 6978 | case GRN_WAL_EVENT_DELETE_INFO_PHASE1: |
| 6979 | grn_pat_wal_recover_delete_info_phase1(ctx, pat, &entry, wal_error_tag); |
| 6980 | break; |
| 6981 | case GRN_WAL_EVENT_DELETE_INFO_PHASE2: |
| 6982 | grn_pat_wal_recover_delete_info_phase2(ctx, pat, &entry, wal_error_tag); |
| 6983 | break; |
| 6984 | case GRN_WAL_EVENT_DELETE_INFO_PHASE3: |
| 6985 | grn_pat_wal_recover_delete_info_phase3(ctx, pat, &entry, wal_error_tag); |
| 6986 | break; |
| 6987 | case GRN_WAL_EVENT_DELETE_ENTRY: |
| 6988 | grn_pat_wal_recover_delete_entry(ctx, pat, &entry, wal_error_tag); |
| 6989 | break; |
| 6990 | case GRN_WAL_EVENT_DEFRAG_KEY: |
| 6991 | /* Defer until GRN_WAL_EVENT_DEFRAG_CURRENT_KEY is happen. */ |
| 6992 | if (defrag_key_start_id == 0) { |
| 6993 | defrag_key_start_id = entry.id; |
| 6994 | } |
| 6995 | break; |
| 6996 | case GRN_WAL_EVENT_DEFRAG_CURRENT_KEY: |
| 6997 | /* Other WAL entries must not be happen between |
| 6998 | * GRN_WAL_EVENT_DEFRAG_KEY{1..N} and GRN_WAL_EVENT_DEFRAG_CURRENT_KEY |
| 6999 | * but we don't validate it. WAL entries must be valid. If one or more |
| 7000 | * GRN_WAL_EVENT_DEFRAG_KEY exist but GRN_WAL_EVENT_DEFRAG_CURRENT_KEY |
| 7001 | * doesn't exist, the grn_pat_defrag() was crashed. In the case, the |
| 7002 | * defrag event is ignored. Because the defrag wasn't executed. */ |
| 7003 | grn_pat_wal_recover_defrag_current_key(ctx, |
| 7004 | pat, |
| 7005 | &entry, |
| 7006 | defrag_key_start_id, |
| 7007 | tag, |
| 7008 | wal_error_tag); |
| 7009 | defrag_key_start_id = 0; |
| 7010 | break; |
| 7011 | default: |
| 7012 | grn_wal_set_recover_error(ctx, |
| 7013 | GRN_FUNCTION_NOT_IMPLEMENTED, |
| 7014 | (grn_obj *)pat, |
| 7015 | &entry, |
| 7016 | wal_error_tag, |
| 7017 | "not implemented yet"); |
| 7018 | break; |
| 7019 | } |
| 7020 | if (ctx->rc != GRN_SUCCESS) { |
| 7021 | break; |
| 7022 | } |
| 7023 | if (entry.event == GRN_WAL_EVENT_DEFRAG_KEY) { |
| 7024 | /* Don't update wal_id and need_flush until |
| 7025 | * GRN_WAL_EVENT_DEFRAG_CURRENT_KEY is happen. If |
| 7026 | * GRN_WAL_EVENT_DEFRAG_CURRENT_KEY isn't happen, grn_pat_defrag() was |
| 7027 | * crashed between writing GRN_WAL_EVENT_DEFRAG_KEY and |
| 7028 | * GRN_WAL_EVENT_DEFRAG_CURRENT_KEY. In the case, we just ignore the |
| 7029 | * defrag event. Because the defrag wasn't executed. */ |
| 7030 | continue; |
| 7031 | } |
| 7032 | pat->header->wal_id = entry.id; |
| 7033 | need_flush = true; |
| 7034 | } |
| 7035 | grn_wal_reader_close(ctx, reader); |
| 7036 | } |
| 7037 | |
| 7038 | if (need_flush && ctx->rc == GRN_SUCCESS) { |
| 7039 | grn_obj_touch(ctx, (grn_obj *)pat, NULL); |
| 7040 | grn_obj_flush(ctx, (grn_obj *)pat); |
| 7041 | } |
| 7042 | |
| 7043 | return ctx->rc; |
| 7044 | } |
| 7045 | |
| 7046 | grn_rc |
| 7047 | grn_pat_warm(grn_ctx *ctx, grn_pat *pat) |
| 7048 | { |
| 7049 | return grn_io_warm(ctx, pat->io); |
| 7050 | } |