| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2026 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn_hash.h" |
| 21 | #include "grn_normalizer.h" |
| 22 | #include "grn_obj.h" |
| 23 | #include "grn_output.h" |
| 24 | #include "grn_posting.h" |
| 25 | #include "grn_selector.h" |
| 26 | #include "grn_store.h" |
| 27 | #include "grn_wal.h" |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <limits.h> |
| 31 | |
| 32 | /* grn_tiny_array */ |
| 33 | |
| 34 | /* Requirements: id != GRN_ID_NIL. */ |
| 35 | static inline int |
| 36 | grn_tiny_array_get_block_id(grn_id id) |
| 37 | { |
| 38 | grn_bit_scan_rev_result most_significant_one_bit_offset; |
| 39 | GRN_BIT_SCAN_REV(id, most_significant_one_bit_offset); |
| 40 | return most_significant_one_bit_offset >> GRN_TINY_ARRAY_FACTOR; |
| 41 | } |
| 42 | |
| 43 | /* Requirements: id != GRN_ID_NIL. */ |
| 44 | static inline void * |
| 45 | grn_tiny_array_get(grn_tiny_array *array, grn_id id) |
| 46 | { |
| 47 | const int block_id = grn_tiny_array_get_block_id(id); |
| 48 | uint8_t *const block = (uint8_t *)array->blocks[block_id]; |
| 49 | if (block) { |
| 50 | const size_t offset = GRN_TINY_ARRAY_GET_OFFSET(block_id); |
| 51 | return block + (id - offset) * array->element_size; |
| 52 | } |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | /* Requirements: id != GRN_ID_NIL. */ |
| 57 | static inline void * |
| 58 | grn_tiny_array_put(grn_tiny_array *array, grn_id id) |
| 59 | { |
| 60 | const int block_id = grn_tiny_array_get_block_id(id); |
| 61 | void **const block = &array->blocks[block_id]; |
| 62 | const size_t offset = GRN_TINY_ARRAY_GET_OFFSET(block_id); |
| 63 | if (!*block) { |
| 64 | grn_ctx *const ctx = array->ctx; |
| 65 | if (array->flags & GRN_TINY_ARRAY_THREADSAFE) { |
| 66 | CRITICAL_SECTION_ENTER(array->lock); |
| 67 | } |
| 68 | if (!*block) { |
| 69 | const size_t block_size = |
| 70 | GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id) * array->element_size; |
| 71 | if (array->flags & GRN_TINY_ARRAY_USE_MALLOC) { |
| 72 | if (array->flags & GRN_TINY_ARRAY_CLEAR) { |
| 73 | *block = GRN_CALLOC(block_size); |
| 74 | } else { |
| 75 | *block = GRN_MALLOC(block_size); |
| 76 | } |
| 77 | } else { |
| 78 | *block = GRN_CTX_ALLOC(ctx, block_size); |
| 79 | } |
| 80 | } |
| 81 | if (array->flags & GRN_TINY_ARRAY_THREADSAFE) { |
| 82 | CRITICAL_SECTION_LEAVE(array->lock); |
| 83 | } |
| 84 | if (!*block) { |
| 85 | return NULL; |
| 86 | } |
| 87 | } |
| 88 | if (id > array->max) { |
| 89 | array->max = id; |
| 90 | } |
| 91 | return (uint8_t *)*block + (id - offset) * array->element_size; |
| 92 | } |
| 93 | |
| 94 | static inline void * |
| 95 | grn_tiny_array_at_inline(grn_tiny_array *array, grn_id id) |
| 96 | { |
| 97 | return id ? grn_tiny_array_put(array, id) : NULL; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | grn_tiny_array_init(grn_ctx *ctx, |
| 102 | grn_tiny_array *array, |
| 103 | uint16_t element_size, |
| 104 | uint16_t flags) |
| 105 | { |
| 106 | array->ctx = ctx; |
| 107 | array->max = 0; |
| 108 | array->element_size = element_size; |
| 109 | array->flags = flags; |
| 110 | memset(array->blocks, 0, sizeof(array->blocks)); |
| 111 | if (flags & GRN_TINY_ARRAY_THREADSAFE) { |
| 112 | CRITICAL_SECTION_INIT(array->lock); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | grn_tiny_array_fin(grn_tiny_array *array) |
| 118 | { |
| 119 | int block_id; |
| 120 | grn_ctx *const ctx = array->ctx; |
| 121 | for (block_id = 0; block_id < GRN_TINY_ARRAY_NUM_BLOCKS; block_id++) { |
| 122 | if (array->blocks[block_id]) { |
| 123 | if (array->flags & GRN_TINY_ARRAY_USE_MALLOC) { |
| 124 | GRN_FREE(array->blocks[block_id]); |
| 125 | } else { |
| 126 | GRN_CTX_FREE(ctx, array->blocks[block_id]); |
| 127 | } |
| 128 | array->blocks[block_id] = NULL; |
| 129 | } |
| 130 | } |
| 131 | if (array->flags & GRN_TINY_ARRAY_THREADSAFE) { |
| 132 | CRITICAL_SECTION_FIN(array->lock); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void * |
| 137 | grn_tiny_array_at(grn_tiny_array *array, grn_id id) |
| 138 | { |
| 139 | return grn_tiny_array_at_inline(array, id); |
| 140 | } |
| 141 | |
| 142 | grn_id |
| 143 | grn_tiny_array_id(grn_tiny_array *array, const void *element_address) |
| 144 | { |
| 145 | const uint8_t *const ptr = (const uint8_t *)element_address; |
| 146 | uint32_t block_id; |
| 147 | grn_id offset = 1; |
| 148 | for (block_id = 0; block_id < GRN_TINY_ARRAY_NUM_BLOCKS; block_id++) { |
| 149 | const uint32_t block_size = GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id); |
| 150 | const uint8_t *const block = (const uint8_t *)array->blocks[block_id]; |
| 151 | if (block) { |
| 152 | if (block <= ptr && ptr < (block + block_size * array->element_size)) { |
| 153 | return offset + (grn_id)((ptr - block) / array->element_size); |
| 154 | } |
| 155 | } |
| 156 | offset += block_size; |
| 157 | } |
| 158 | return GRN_ID_NIL; |
| 159 | } |
| 160 | |
| 161 | /* grn_tiny_bitmap */ |
| 162 | |
| 163 | static void |
| 164 | grn_tiny_bitmap_init(grn_ctx *ctx, grn_tiny_bitmap *bitmap) |
| 165 | { |
| 166 | bitmap->ctx = ctx; |
| 167 | memset(bitmap->blocks, 0, sizeof(bitmap->blocks)); |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | grn_tiny_bitmap_fin(grn_tiny_bitmap *bitmap) |
| 172 | { |
| 173 | int block_id; |
| 174 | grn_ctx *const ctx = bitmap->ctx; |
| 175 | for (block_id = 0; block_id < GRN_TINY_ARRAY_NUM_BLOCKS; block_id++) { |
| 176 | if (bitmap->blocks[block_id]) { |
| 177 | GRN_CTX_FREE(ctx, bitmap->blocks[block_id]); |
| 178 | bitmap->blocks[block_id] = NULL; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* Requirements: bit_id != GRN_ID_NIL. */ |
| 184 | static inline uint8_t * |
| 185 | grn_tiny_bitmap_get_byte(grn_tiny_bitmap *bitmap, grn_id bit_id) |
| 186 | { |
| 187 | const uint32_t byte_id = (bit_id >> 3) + 1; |
| 188 | const int block_id = grn_tiny_array_get_block_id(byte_id); |
| 189 | uint8_t *const block = (uint8_t *)bitmap->blocks[block_id]; |
| 190 | if (block) { |
| 191 | const size_t offset = GRN_TINY_ARRAY_GET_OFFSET(block_id); |
| 192 | return block + byte_id - offset; |
| 193 | } |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | /* Requirements: bit_id != GRN_ID_NIL. */ |
| 198 | static inline uint8_t * |
| 199 | grn_tiny_bitmap_put_byte(grn_tiny_bitmap *bitmap, grn_id bit_id) |
| 200 | { |
| 201 | const uint32_t byte_id = (bit_id >> 3) + 1; |
| 202 | const int block_id = grn_tiny_array_get_block_id(byte_id); |
| 203 | void **const block = &bitmap->blocks[block_id]; |
| 204 | const size_t offset = GRN_TINY_ARRAY_GET_OFFSET(block_id); |
| 205 | if (!*block) { |
| 206 | grn_ctx *const ctx = bitmap->ctx; |
| 207 | *block = GRN_CTX_ALLOC(ctx, GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id)); |
| 208 | if (!*block) { |
| 209 | return NULL; |
| 210 | } |
| 211 | } |
| 212 | return (uint8_t *)*block + byte_id - offset; |
| 213 | } |
| 214 | |
| 215 | /* Requirements: bit_id != GRN_ID_NIL. */ |
| 216 | /* Return value: 1/0 on success, -1 on failure. */ |
| 217 | /* Note: A bitmap is extended if needed. */ |
| 218 | static inline int |
| 219 | grn_tiny_bitmap_put(grn_tiny_bitmap *bitmap, grn_id bit_id) |
| 220 | { |
| 221 | uint8_t *const ptr = grn_tiny_bitmap_put_byte(bitmap, bit_id); |
| 222 | return ptr ? ((*ptr >> (bit_id & 7)) & 1) : -1; |
| 223 | } |
| 224 | |
| 225 | /* Requirements: bit_id != GRN_ID_NIL. */ |
| 226 | static inline uint8_t * |
| 227 | grn_tiny_bitmap_get_and_set(grn_tiny_bitmap *bitmap, grn_id bit_id, bool bit) |
| 228 | { |
| 229 | uint8_t *const ptr = grn_tiny_bitmap_get_byte(bitmap, bit_id); |
| 230 | if (ptr) { |
| 231 | /* This branch will be removed because the given `bit' is constant. */ |
| 232 | if (bit) { |
| 233 | *ptr |= 1 << (bit_id & 7); |
| 234 | } else { |
| 235 | *ptr &= ~((uint8_t)(1 << (bit_id & 7))); |
| 236 | } |
| 237 | } |
| 238 | return ptr; |
| 239 | } |
| 240 | |
| 241 | /* Requirements: bit_id != GRN_ID_NIL. */ |
| 242 | /* Note: A bitmap is extended if needed. */ |
| 243 | static inline uint8_t * |
| 244 | grn_tiny_bitmap_put_and_set(grn_tiny_bitmap *bitmap, grn_id bit_id, bool bit) |
| 245 | { |
| 246 | uint8_t *const ptr = grn_tiny_bitmap_put_byte(bitmap, bit_id); |
| 247 | if (ptr) { |
| 248 | /* This branch will be removed because the given `bit' is constant. */ |
| 249 | if (bit) { |
| 250 | *ptr |= 1 << (bit_id & 7); |
| 251 | } else { |
| 252 | *ptr &= ~((uint8_t)(1 << (bit_id & 7))); |
| 253 | } |
| 254 | } |
| 255 | return ptr; |
| 256 | } |
| 257 | |
| 258 | /* grn_io_array */ |
| 259 | |
| 260 | #define GRN_ARRAY_MAX (GRN_ID_MAX - 8) |
| 261 | |
| 262 | inline static grn_rc |
| 263 | grn_io_array_wal_touch(grn_ctx *ctx, grn_array *array, const char *tag) |
| 264 | { |
| 265 | if (array->wal_touched) { |
| 266 | return GRN_SUCCESS; |
| 267 | } |
| 268 | |
| 269 | if (GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_NONE) { |
| 270 | return GRN_SUCCESS; |
| 271 | } |
| 272 | |
| 273 | grn_rc rc = grn_wal_touch(ctx, (grn_obj *)array, false, tag); |
| 274 | if (rc == GRN_SUCCESS) { |
| 275 | array->wal_touched = true; |
| 276 | } |
| 277 | return rc; |
| 278 | } |
| 279 | |
| 280 | static inline void * |
| 281 | grn_io_array_at_inline( |
| 282 | grn_ctx *ctx, grn_io *io, uint32_t segment_id, uint64_t offset, int flags) |
| 283 | { |
| 284 | return grn_io_array_at(ctx, io, segment_id, offset, &flags); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * grn_io_array_bit_at() returns 1/0 on success, -1 on failure. |
| 289 | */ |
| 290 | static inline int |
| 291 | grn_io_array_bit_at(grn_ctx *ctx, |
| 292 | grn_io *io, |
| 293 | uint32_t segment_id, |
| 294 | uint64_t offset) |
| 295 | { |
| 296 | uint8_t *const ptr = (uint8_t *) |
| 297 | grn_io_array_at_inline(ctx, io, segment_id, (offset >> 3) + 1, 0); |
| 298 | return ptr ? ((*ptr >> (offset & 7)) & 1) : -1; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * The following functions, grn_io_array_bit_*(), return a non-NULL pointer on |
| 303 | * success, a NULL pointer on failure. |
| 304 | */ |
| 305 | static inline void * |
| 306 | grn_io_array_bit_on(grn_ctx *ctx, |
| 307 | grn_io *io, |
| 308 | uint32_t segment_id, |
| 309 | uint64_t offset) |
| 310 | { |
| 311 | uint8_t *const ptr = (uint8_t *)grn_io_array_at_inline(ctx, |
| 312 | io, |
| 313 | segment_id, |
| 314 | (offset >> 3) + 1, |
| 315 | GRN_TABLE_ADD); |
| 316 | if (ptr) { |
| 317 | *ptr |= 1 << (offset & 7); |
| 318 | } |
| 319 | return ptr; |
| 320 | } |
| 321 | |
| 322 | static inline void * |
| 323 | grn_io_array_bit_off(grn_ctx *ctx, |
| 324 | grn_io *io, |
| 325 | uint32_t segment_id, |
| 326 | uint64_t offset) |
| 327 | { |
| 328 | uint8_t *const ptr = (uint8_t *)grn_io_array_at_inline(ctx, |
| 329 | io, |
| 330 | segment_id, |
| 331 | (offset >> 3) + 1, |
| 332 | GRN_TABLE_ADD); |
| 333 | if (ptr) { |
| 334 | *ptr &= ~((uint8_t)(1 << (offset & 7))); |
| 335 | } |
| 336 | return ptr; |
| 337 | } |
| 338 | |
| 339 | /* grn_array */ |
| 340 | |
| 341 | #define GRN_ARRAY_SEGMENT_SIZE 0x400000 |
| 342 | #define GRN_ARRAY_STATUS_TRUNCATED (0x01 << 0) |
| 343 | #define GRN_ARRAY_STATUS_GARBAGES_BUFFER_INITIALIZED (0x01 << 1) |
| 344 | #define GRN_ARRAY_GARBAGES_BUFFER_SIZE 5 |
| 345 | |
| 346 | /* Header of grn_io-based grn_array. */ |
| 347 | struct grn_array_header { |
| 348 | uint32_t flags; |
| 349 | uint32_t curr_rec; |
| 350 | uint32_t value_size; |
| 351 | uint32_t n_entries; |
| 352 | uint32_t n_garbages; |
| 353 | grn_id garbages; |
| 354 | uint32_t lock; |
| 355 | uint32_t status; |
| 356 | uint32_t n_garbages_in_buffer; |
| 357 | grn_id garbages_buffer[GRN_ARRAY_GARBAGES_BUFFER_SIZE]; |
| 358 | uint32_t reserved[1]; |
| 359 | }; |
| 360 | |
| 361 | /* |
| 362 | * A grn_io-based grn_array consists of the following 2 segments. |
| 363 | * GRN_ARRAY_VALUE_SEGMENT: stores values. |
| 364 | * GRN_ARRAY_BITMAP_SEGMENT: stores whether entries are valid or not. |
| 365 | */ |
| 366 | enum { |
| 367 | GRN_ARRAY_VALUE_SEGMENT = 0, |
| 368 | GRN_ARRAY_BITMAP_SEGMENT = 1 |
| 369 | }; |
| 370 | |
| 371 | static inline bool |
| 372 | grn_array_is_io_array(grn_array *array) |
| 373 | { |
| 374 | return array->io != NULL; |
| 375 | } |
| 376 | |
| 377 | static inline bool |
| 378 | grn_array_can_use_value_for_garbage(grn_array *array) |
| 379 | { |
| 380 | return array->value_size >= sizeof(grn_id); |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * array must be grn_array_is_io_array(array) && |
| 385 | * !grn_array_can_use_value_for_garbage(array). The |
| 386 | * grn_array_is_io_array(array) restriction may be removed when we |
| 387 | * need to implement this for tiny array. |
| 388 | */ |
| 389 | static inline void |
| 390 | grn_array_put_garbage_to_buffer(grn_ctx *ctx, |
| 391 | grn_array *array, |
| 392 | grn_id garbage_id) |
| 393 | { |
| 394 | struct grn_array_header *const header = array->header; |
| 395 | if (header->garbages != GRN_ID_NIL && |
| 396 | header->n_garbages_in_buffer < GRN_ARRAY_GARBAGES_BUFFER_SIZE) { |
| 397 | int i; |
| 398 | for (i = 0; i < GRN_ARRAY_GARBAGES_BUFFER_SIZE; i++) { |
| 399 | if (header->garbages_buffer[i] == GRN_ID_NIL) { |
| 400 | header->garbages_buffer[i] = header->garbages; |
| 401 | header->n_garbages_in_buffer++; |
| 402 | header->garbages = GRN_ID_NIL; |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | if (header->garbages == GRN_ID_NIL) { |
| 408 | header->garbages = garbage_id; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static inline void * |
| 413 | grn_array_io_entry_at(grn_ctx *ctx, grn_array *array, grn_id id, int flags) |
| 414 | { |
| 415 | return grn_io_array_at_inline(ctx, |
| 416 | array->io, |
| 417 | GRN_ARRAY_VALUE_SEGMENT, |
| 418 | id, |
| 419 | flags); |
| 420 | } |
| 421 | |
| 422 | static inline void * |
| 423 | grn_array_entry_at(grn_ctx *ctx, grn_array *array, grn_id id, int flags) |
| 424 | { |
| 425 | if (grn_array_is_io_array(array)) { |
| 426 | return grn_array_io_entry_at(ctx, array, id, flags); |
| 427 | } else { |
| 428 | return grn_tiny_array_at_inline(&array->array, id); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* grn_array_bitmap_at() returns 1/0 on success, -1 on failure. */ |
| 433 | static inline int |
| 434 | grn_array_bitmap_at(grn_ctx *ctx, grn_array *array, grn_id id) |
| 435 | { |
| 436 | if (grn_array_is_io_array(array)) { |
| 437 | int bit = grn_io_array_bit_at(ctx, array->io, GRN_ARRAY_BITMAP_SEGMENT, id); |
| 438 | if (bit == 0 && !grn_array_can_use_value_for_garbage(array)) { |
| 439 | grn_array_put_garbage_to_buffer(ctx, array, id); |
| 440 | } |
| 441 | return bit; |
| 442 | } else { |
| 443 | return grn_tiny_bitmap_put(&array->bitmap, id); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static grn_rc |
| 448 | grn_array_init_tiny_array(grn_ctx *ctx, |
| 449 | grn_array *array, |
| 450 | const char *path, |
| 451 | uint32_t value_size, |
| 452 | uint32_t flags) |
| 453 | { |
| 454 | if (path) { |
| 455 | ERR(GRN_INVALID_ARGUMENT, "failed to create tiny array"); |
| 456 | return ctx->rc; |
| 457 | } |
| 458 | array->obj.header.flags = (grn_obj_flags)flags; |
| 459 | array->ctx = ctx; |
| 460 | array->value_size = value_size; |
| 461 | array->n_keys = 0; |
| 462 | array->keys = NULL; |
| 463 | array->n_garbages = &array->n_garbages_buf; |
| 464 | array->n_entries = &array->n_entries_buf; |
| 465 | array->n_garbages_buf = 0; |
| 466 | array->n_entries_buf = 0; |
| 467 | array->io = NULL; |
| 468 | array->header = NULL; |
| 469 | array->garbages = GRN_ID_NIL; |
| 470 | grn_tiny_array_init(ctx, |
| 471 | &array->array, |
| 472 | (uint16_t)value_size, |
| 473 | GRN_TINY_ARRAY_CLEAR); |
| 474 | grn_tiny_bitmap_init(ctx, &array->bitmap); |
| 475 | return GRN_SUCCESS; |
| 476 | } |
| 477 | |
| 478 | static grn_io * |
| 479 | grn_array_create_io_array(grn_ctx *ctx, const char *path, uint32_t value_size) |
| 480 | { |
| 481 | uint32_t w_of_element = 0; |
| 482 | grn_io_array_spec array_spec[2]; |
| 483 | |
| 484 | while ((1U << w_of_element) < value_size) { |
| 485 | w_of_element++; |
| 486 | } |
| 487 | |
| 488 | array_spec[GRN_ARRAY_VALUE_SEGMENT].w_of_element = w_of_element; |
| 489 | array_spec[GRN_ARRAY_VALUE_SEGMENT].max_n_segments = |
| 490 | 1U << (30 - (22 - w_of_element)); |
| 491 | array_spec[GRN_ARRAY_BITMAP_SEGMENT].w_of_element = 0; |
| 492 | array_spec[GRN_ARRAY_BITMAP_SEGMENT].max_n_segments = 1U << (30 - (22 + 3)); |
| 493 | return grn_io_create_with_array(ctx, |
| 494 | path, |
| 495 | sizeof(struct grn_array_header), |
| 496 | GRN_ARRAY_SEGMENT_SIZE, |
| 497 | GRN_IO_AUTO, |
| 498 | 2, |
| 499 | array_spec); |
| 500 | } |
| 501 | |
| 502 | static grn_rc |
| 503 | grn_array_init_io_array(grn_ctx *ctx, |
| 504 | grn_array *array, |
| 505 | const char *path, |
| 506 | uint32_t value_size, |
| 507 | uint32_t flags) |
| 508 | { |
| 509 | grn_io *io; |
| 510 | struct grn_array_header *header; |
| 511 | |
| 512 | io = grn_array_create_io_array(ctx, path, value_size); |
| 513 | if (!io) { |
| 514 | return ctx->rc; |
| 515 | } |
| 516 | grn_io_set_type(io, GRN_TABLE_NO_KEY); |
| 517 | |
| 518 | header = grn_io_header(io); |
| 519 | header->flags = flags; |
| 520 | header->curr_rec = 0; |
| 521 | header->lock = 0; |
| 522 | header->value_size = value_size; |
| 523 | header->n_entries = 0; |
| 524 | header->n_garbages = 0; |
| 525 | header->garbages = GRN_ID_NIL; |
| 526 | header->status = 0; |
| 527 | header->n_garbages_in_buffer = 0; |
| 528 | { |
| 529 | int i; |
| 530 | for (i = 0; i < GRN_ARRAY_GARBAGES_BUFFER_SIZE; i++) { |
| 531 | header->garbages_buffer[i] = GRN_ID_NIL; |
| 532 | } |
| 533 | } |
| 534 | header->status |= GRN_ARRAY_STATUS_GARBAGES_BUFFER_INITIALIZED; |
| 535 | array->obj.header.flags = (grn_obj_flags)flags; |
| 536 | array->ctx = ctx; |
| 537 | array->value_size = value_size; |
| 538 | array->n_keys = 0; |
| 539 | array->keys = NULL; |
| 540 | array->n_garbages = &header->n_garbages; |
| 541 | array->n_entries = &header->n_entries; |
| 542 | array->io = io; |
| 543 | array->header = header; |
| 544 | array->lock = &header->lock; |
| 545 | array->wal_touched = false; |
| 546 | return GRN_SUCCESS; |
| 547 | } |
| 548 | |
| 549 | static grn_rc |
| 550 | grn_array_init(grn_ctx *ctx, |
| 551 | grn_array *array, |
| 552 | const char *path, |
| 553 | uint32_t value_size, |
| 554 | uint32_t flags) |
| 555 | { |
| 556 | if (flags & GRN_ARRAY_TINY) { |
| 557 | return grn_array_init_tiny_array(ctx, array, path, value_size, flags); |
| 558 | } else { |
| 559 | return grn_array_init_io_array(ctx, array, path, value_size, flags); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | grn_array * |
| 564 | grn_array_create(grn_ctx *ctx, |
| 565 | const char *path, |
| 566 | uint32_t value_size, |
| 567 | uint32_t flags) |
| 568 | { |
| 569 | if (ctx) { |
| 570 | grn_array *const array = (grn_array *)GRN_CALLOC(sizeof(grn_array)); |
| 571 | if (array) { |
| 572 | GRN_DB_OBJ_SET_TYPE(array, GRN_TABLE_NO_KEY); |
| 573 | if (!grn_array_init(ctx, array, path, value_size, flags)) { |
| 574 | return array; |
| 575 | } |
| 576 | GRN_FREE(array); |
| 577 | } |
| 578 | } |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | grn_array * |
| 583 | grn_array_open(grn_ctx *ctx, const char *path) |
| 584 | { |
| 585 | if (ctx) { |
| 586 | grn_io *const io = grn_io_open(ctx, path, GRN_IO_AUTO); |
| 587 | if (io) { |
| 588 | struct grn_array_header *const header = grn_io_header(io); |
| 589 | uint32_t io_type = grn_io_get_type(io); |
| 590 | if (io_type == GRN_TABLE_NO_KEY) { |
| 591 | grn_array *const array = (grn_array *)GRN_CALLOC(sizeof(grn_array)); |
| 592 | if (array) { |
| 593 | if (!(header->flags & GRN_ARRAY_TINY)) { |
| 594 | GRN_DB_OBJ_SET_TYPE(array, GRN_TABLE_NO_KEY); |
| 595 | array->obj.header.flags = (grn_obj_flags)(header->flags); |
| 596 | array->ctx = ctx; |
| 597 | array->value_size = header->value_size; |
| 598 | array->n_keys = 0; |
| 599 | array->keys = NULL; |
| 600 | array->n_garbages = &header->n_garbages; |
| 601 | array->n_entries = &header->n_entries; |
| 602 | array->io = io; |
| 603 | array->header = header; |
| 604 | array->lock = &header->lock; |
| 605 | array->wal_touched = false; |
| 606 | /* For old array created by Groonga that doesn't have |
| 607 | * garbages_buffer. */ |
| 608 | if (!grn_array_can_use_value_for_garbage(array) && |
| 609 | !(header->status & |
| 610 | GRN_ARRAY_STATUS_GARBAGES_BUFFER_INITIALIZED)) { |
| 611 | header->n_garbages_in_buffer = 0; |
| 612 | int i; |
| 613 | for (i = 0; i < GRN_ARRAY_GARBAGES_BUFFER_SIZE; i++) { |
| 614 | header->garbages_buffer[i] = GRN_ID_NIL; |
| 615 | } |
| 616 | header->status |= GRN_ARRAY_STATUS_GARBAGES_BUFFER_INITIALIZED; |
| 617 | } |
| 618 | return array; |
| 619 | } else { |
| 620 | GRN_LOG(ctx, |
| 621 | GRN_LOG_NOTICE, |
| 622 | "invalid array flags. (%x)", |
| 623 | header->flags); |
| 624 | } |
| 625 | GRN_FREE(array); |
| 626 | } |
| 627 | } else { |
| 628 | ERR(GRN_INVALID_FORMAT, |
| 629 | "[table][array] file type must be %#04x: <%#04x>", |
| 630 | GRN_TABLE_NO_KEY, |
| 631 | io_type); |
| 632 | } |
| 633 | grn_io_close(ctx, io); |
| 634 | } |
| 635 | } |
| 636 | return NULL; |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * grn_array_error_if_truncated() logs an error and returns its error code if |
| 641 | * an array is truncated by another process. |
| 642 | * Otherwise, this function returns GRN_SUCCESS. |
| 643 | * Note that `ctx` and `array` must be valid. |
| 644 | * |
| 645 | * FIXME: An array should be reopened if possible. |
| 646 | */ |
| 647 | static grn_rc |
| 648 | grn_array_error_if_truncated(grn_ctx *ctx, grn_array *array) |
| 649 | { |
| 650 | if (array->header && (array->header->status & GRN_ARRAY_STATUS_TRUNCATED)) { |
| 651 | ERR(GRN_FILE_CORRUPT, |
| 652 | "array is truncated, please unmap or reopen the database"); |
| 653 | return GRN_FILE_CORRUPT; |
| 654 | } |
| 655 | return GRN_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | grn_rc |
| 659 | grn_array_close(grn_ctx *ctx, grn_array *array) |
| 660 | { |
| 661 | grn_rc rc = GRN_SUCCESS; |
| 662 | if (!ctx || !array) { |
| 663 | return GRN_INVALID_ARGUMENT; |
| 664 | } |
| 665 | if (array->keys) { |
| 666 | GRN_FREE(array->keys); |
| 667 | } |
| 668 | if (grn_array_is_io_array(array)) { |
| 669 | if (array->io->path[0] != '\0' && |
| 670 | GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_PRIMARY) { |
| 671 | grn_obj_flush(ctx, (grn_obj *)array); |
| 672 | } |
| 673 | rc = grn_io_close(ctx, array->io); |
| 674 | } else { |
| 675 | GRN_ASSERT(ctx == array->ctx); |
| 676 | grn_tiny_array_fin(&array->array); |
| 677 | grn_tiny_bitmap_fin(&array->bitmap); |
| 678 | } |
| 679 | GRN_FREE(array); |
| 680 | return rc; |
| 681 | } |
| 682 | |
| 683 | grn_rc |
| 684 | grn_array_remove(grn_ctx *ctx, const char *path) |
| 685 | { |
| 686 | if (!ctx || !path) { |
| 687 | return GRN_INVALID_ARGUMENT; |
| 688 | } |
| 689 | grn_rc wal_rc = grn_wal_remove(ctx, path, "[array]"); |
| 690 | grn_rc io_rc = grn_io_remove(ctx, path); |
| 691 | grn_rc rc = io_rc; |
| 692 | if (rc == GRN_SUCCESS) { |
| 693 | rc = wal_rc; |
| 694 | } |
| 695 | return rc; |
| 696 | } |
| 697 | |
| 698 | uint32_t |
| 699 | grn_array_size(grn_ctx *ctx, grn_array *array) |
| 700 | { |
| 701 | if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) { |
| 702 | return 0; |
| 703 | } |
| 704 | return *array->n_entries; |
| 705 | } |
| 706 | |
| 707 | uint32_t |
| 708 | grn_array_get_flags(grn_ctx *ctx, grn_array *array) |
| 709 | { |
| 710 | return array->header->flags; |
| 711 | } |
| 712 | |
| 713 | grn_rc |
| 714 | grn_array_truncate(grn_ctx *ctx, grn_array *array) |
| 715 | { |
| 716 | grn_rc rc; |
| 717 | char *path = NULL; |
| 718 | uint32_t value_size, flags; |
| 719 | |
| 720 | if (!ctx || !array) { |
| 721 | return GRN_INVALID_ARGUMENT; |
| 722 | } |
| 723 | rc = grn_array_error_if_truncated(ctx, array); |
| 724 | if (rc != GRN_SUCCESS) { |
| 725 | return rc; |
| 726 | } |
| 727 | if (grn_array_is_io_array(array)) { |
| 728 | const char *const io_path = grn_io_path(array->io); |
| 729 | if (io_path && *io_path) { |
| 730 | path = GRN_STRDUP(io_path); |
| 731 | if (!path) { |
| 732 | ERR(GRN_NO_MEMORY_AVAILABLE, "cannot duplicate path: <%s>", io_path); |
| 733 | return GRN_NO_MEMORY_AVAILABLE; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | value_size = array->value_size; |
| 738 | flags = array->obj.header.flags; |
| 739 | |
| 740 | if (grn_array_is_io_array(array)) { |
| 741 | if (path) { |
| 742 | /* Only an I/O array with a valid path uses the `truncated` flag. */ |
| 743 | array->header->status |= GRN_ARRAY_STATUS_TRUNCATED; |
| 744 | } |
| 745 | rc = grn_io_close(ctx, array->io); |
| 746 | if (!rc) { |
| 747 | array->io = NULL; |
| 748 | if (path) { |
| 749 | rc = grn_array_remove(ctx, path); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | if (!rc) { |
| 754 | rc = grn_array_init(ctx, array, path, value_size, flags); |
| 755 | } |
| 756 | if (path) { |
| 757 | GRN_FREE(path); |
| 758 | } |
| 759 | return rc; |
| 760 | } |
| 761 | |
| 762 | static inline grn_id |
| 763 | grn_array_get_max_id(grn_array *array) |
| 764 | { |
| 765 | return grn_array_is_io_array(array) ? array->header->curr_rec |
| 766 | : array->array.max; |
| 767 | } |
| 768 | |
| 769 | static inline void * |
| 770 | grn_array_get_value_inline(grn_ctx *ctx, grn_array *array, grn_id id) |
| 771 | { |
| 772 | if (!ctx || !array) { |
| 773 | return NULL; |
| 774 | } |
| 775 | if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) { |
| 776 | return NULL; |
| 777 | } |
| 778 | if (*array->n_garbages) { |
| 779 | /* |
| 780 | * grn_array_bitmap_at() is a time-consuming function, so it is called only |
| 781 | * when there are garbages in the array. |
| 782 | */ |
| 783 | if (grn_array_bitmap_at(ctx, array, id) != 1) { |
| 784 | return NULL; |
| 785 | } |
| 786 | } else if (id == 0 || id > grn_array_get_max_id(array)) { |
| 787 | return NULL; |
| 788 | } |
| 789 | return grn_array_entry_at(ctx, array, id, 0); |
| 790 | } |
| 791 | |
| 792 | int |
| 793 | grn_array_get_value(grn_ctx *ctx, grn_array *array, grn_id id, void *valuebuf) |
| 794 | { |
| 795 | void *const value = grn_array_get_value_inline(ctx, array, id); |
| 796 | if (value) { |
| 797 | if (valuebuf) { |
| 798 | grn_memcpy(valuebuf, value, array->value_size); |
| 799 | } |
| 800 | return (int)(array->value_size); |
| 801 | } |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | void * |
| 806 | _grn_array_get_value(grn_ctx *ctx, grn_array *array, grn_id id) |
| 807 | { |
| 808 | return grn_array_get_value_inline(ctx, array, id); |
| 809 | } |
| 810 | |
| 811 | static inline grn_rc |
| 812 | grn_array_set_value_inline( |
| 813 | grn_ctx *ctx, grn_array *array, grn_id id, const void *value, int flags) |
| 814 | { |
| 815 | void *entry; |
| 816 | entry = grn_array_entry_at(ctx, array, id, 0); |
| 817 | if (!entry) { |
| 818 | return GRN_NO_MEMORY_AVAILABLE; |
| 819 | } |
| 820 | |
| 821 | if (grn_array_is_io_array(array)) { |
| 822 | grn_rc rc = grn_io_array_wal_touch(ctx, array, "[array][add]"); |
| 823 | if (rc != GRN_SUCCESS) { |
| 824 | return rc; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | switch ((flags & GRN_OBJ_SET_MASK)) { |
| 829 | case GRN_OBJ_SET: |
| 830 | grn_memcpy(entry, value, array->value_size); |
| 831 | return GRN_SUCCESS; |
| 832 | case GRN_OBJ_INCR: |
| 833 | switch (array->value_size) { |
| 834 | case sizeof(int32_t): |
| 835 | *((int32_t *)entry) += *((int32_t *)value); |
| 836 | return GRN_SUCCESS; |
| 837 | case sizeof(int64_t): |
| 838 | *((int64_t *)entry) += *((int64_t *)value); |
| 839 | return GRN_SUCCESS; |
| 840 | default: |
| 841 | return GRN_INVALID_ARGUMENT; |
| 842 | } |
| 843 | break; |
| 844 | case GRN_OBJ_DECR: |
| 845 | switch (array->value_size) { |
| 846 | case sizeof(int32_t): |
| 847 | *((int32_t *)entry) -= *((int32_t *)value); |
| 848 | return GRN_SUCCESS; |
| 849 | case sizeof(int64_t): |
| 850 | *((int64_t *)entry) -= *((int64_t *)value); |
| 851 | return GRN_SUCCESS; |
| 852 | default: |
| 853 | return GRN_INVALID_ARGUMENT; |
| 854 | } |
| 855 | break; |
| 856 | default: |
| 857 | /* todo : support other types. */ |
| 858 | return GRN_INVALID_ARGUMENT; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | grn_rc |
| 863 | grn_array_set_value( |
| 864 | grn_ctx *ctx, grn_array *array, grn_id id, const void *value, int flags) |
| 865 | { |
| 866 | grn_rc rc; |
| 867 | |
| 868 | if (!ctx || !array || !value) { |
| 869 | return GRN_INVALID_ARGUMENT; |
| 870 | } |
| 871 | |
| 872 | rc = grn_array_error_if_truncated(ctx, array); |
| 873 | if (rc != GRN_SUCCESS) { |
| 874 | return rc; |
| 875 | } |
| 876 | if (*array->n_garbages) { |
| 877 | /* |
| 878 | * grn_array_bitmap_at() is a time-consuming function, so it is called only |
| 879 | * when there are garbages in the array. |
| 880 | */ |
| 881 | if (grn_array_bitmap_at(ctx, array, id) != 1) { |
| 882 | return GRN_INVALID_ARGUMENT; |
| 883 | } |
| 884 | } else if (id == 0 || id > grn_array_get_max_id(array)) { |
| 885 | return GRN_INVALID_ARGUMENT; |
| 886 | } |
| 887 | return grn_array_set_value_inline(ctx, array, id, value, flags); |
| 888 | } |
| 889 | |
| 890 | grn_rc |
| 891 | grn_array_delete_by_id(grn_ctx *ctx, |
| 892 | grn_array *array, |
| 893 | grn_id id, |
| 894 | grn_table_delete_optarg *optarg) |
| 895 | { |
| 896 | grn_rc rc; |
| 897 | if (!ctx || !array) { |
| 898 | return GRN_INVALID_ARGUMENT; |
| 899 | } |
| 900 | rc = grn_array_error_if_truncated(ctx, array); |
| 901 | if (rc != GRN_SUCCESS) { |
| 902 | return rc; |
| 903 | } |
| 904 | if (grn_array_bitmap_at(ctx, array, id) != 1) { |
| 905 | return GRN_INVALID_ARGUMENT; |
| 906 | } |
| 907 | |
| 908 | { |
| 909 | rc = GRN_SUCCESS; |
| 910 | /* lock */ |
| 911 | if (grn_array_is_io_array(array)) { |
| 912 | rc = grn_io_array_wal_touch(ctx, array, "[array][delete]"); |
| 913 | if (rc != GRN_SUCCESS) { |
| 914 | return rc; |
| 915 | } |
| 916 | if (grn_array_can_use_value_for_garbage(array)) { |
| 917 | struct grn_array_header *const header = array->header; |
| 918 | void *const entry = grn_array_io_entry_at(ctx, array, id, 0); |
| 919 | if (!entry) { |
| 920 | rc = GRN_INVALID_ARGUMENT; |
| 921 | } else { |
| 922 | *((grn_id *)entry) = header->garbages; |
| 923 | header->garbages = id; |
| 924 | } |
| 925 | } else { |
| 926 | grn_array_put_garbage_to_buffer(ctx, array, id); |
| 927 | } |
| 928 | if (!rc) { |
| 929 | (*array->n_entries)--; |
| 930 | (*array->n_garbages)++; |
| 931 | /* |
| 932 | * The following grn_io_array_bit_off() fails iff a problem has |
| 933 | * occurred after the above grn_array_bitmap_at(). That is to say, |
| 934 | * an unexpected case. |
| 935 | */ |
| 936 | grn_io_array_bit_off(ctx, array->io, GRN_ARRAY_BITMAP_SEGMENT, id); |
| 937 | } |
| 938 | } else { |
| 939 | if (grn_array_can_use_value_for_garbage(array)) { |
| 940 | void *const entry = grn_tiny_array_get(&array->array, id); |
| 941 | if (!entry) { |
| 942 | rc = GRN_INVALID_ARGUMENT; |
| 943 | ERR(rc, |
| 944 | "[array][delete][tiny] failed to get garbage entry: %p: %u(%u)", |
| 945 | array, |
| 946 | id, |
| 947 | *(array->n_garbages)); |
| 948 | } else { |
| 949 | *((grn_id *)entry) = array->garbages; |
| 950 | array->garbages = id; |
| 951 | } |
| 952 | } |
| 953 | if (rc == GRN_SUCCESS) { |
| 954 | (*array->n_entries)--; |
| 955 | (*array->n_garbages)++; |
| 956 | /* |
| 957 | * The following grn_io_array_bit_off() fails iff a problem has |
| 958 | * occurred after the above grn_array_bitmap_at(). That is to say, |
| 959 | * an unexpected case. |
| 960 | */ |
| 961 | grn_tiny_bitmap_get_and_set(&array->bitmap, id, 0); |
| 962 | } |
| 963 | } |
| 964 | /* unlock */ |
| 965 | return rc; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | grn_id |
| 970 | grn_array_at(grn_ctx *ctx, grn_array *array, grn_id id) |
| 971 | { |
| 972 | if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) { |
| 973 | return GRN_ID_NIL; |
| 974 | } |
| 975 | if (*array->n_garbages) { |
| 976 | /* |
| 977 | * grn_array_bitmap_at() is a time-consuming function, so it is called only |
| 978 | * when there are garbages in the array. |
| 979 | */ |
| 980 | if (grn_array_bitmap_at(ctx, array, id) != 1) { |
| 981 | return GRN_ID_NIL; |
| 982 | } |
| 983 | } else if (id > grn_array_get_max_id(array)) { |
| 984 | return GRN_ID_NIL; |
| 985 | } |
| 986 | return id; |
| 987 | } |
| 988 | |
| 989 | grn_rc |
| 990 | grn_array_copy_sort_key(grn_ctx *ctx, |
| 991 | grn_array *array, |
| 992 | grn_table_sort_key *keys, |
| 993 | size_t n_keys) |
| 994 | { |
| 995 | array->keys = (grn_table_sort_key *)GRN_MALLOCN(grn_table_sort_key, n_keys); |
| 996 | if (!array->keys) { |
| 997 | return ctx->rc; |
| 998 | } |
| 999 | grn_memcpy(array->keys, keys, sizeof(grn_table_sort_key) * n_keys); |
| 1000 | array->n_keys = n_keys; |
| 1001 | return GRN_SUCCESS; |
| 1002 | } |
| 1003 | |
| 1004 | void |
| 1005 | grn_array_cursor_close(grn_ctx *ctx, grn_array_cursor *cursor) |
| 1006 | { |
| 1007 | GRN_ASSERT(cursor->ctx == ctx); |
| 1008 | GRN_FREE(cursor); |
| 1009 | } |
| 1010 | |
| 1011 | grn_array_cursor * |
| 1012 | grn_array_cursor_open(grn_ctx *ctx, |
| 1013 | grn_array *array, |
| 1014 | grn_id min, |
| 1015 | grn_id max, |
| 1016 | int offset, |
| 1017 | int limit, |
| 1018 | int flags) |
| 1019 | { |
| 1020 | grn_array_cursor *cursor; |
| 1021 | if (!array || !ctx) { |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) { |
| 1025 | return NULL; |
| 1026 | } |
| 1027 | |
| 1028 | cursor = (grn_array_cursor *)GRN_MALLOCN(grn_array_cursor, 1); |
| 1029 | if (!cursor) { |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
| 1033 | GRN_DB_OBJ_SET_TYPE(cursor, GRN_CURSOR_TABLE_NO_KEY); |
| 1034 | cursor->array = array; |
| 1035 | cursor->ctx = ctx; |
| 1036 | cursor->obj.header.flags = (grn_obj_flags)(flags); |
| 1037 | cursor->obj.header.domain = GRN_ID_NIL; |
| 1038 | |
| 1039 | if (flags & GRN_CURSOR_DESCENDING) { |
| 1040 | cursor->dir = -1; |
| 1041 | if (max) { |
| 1042 | cursor->curr_rec = max; |
| 1043 | if (!(flags & GRN_CURSOR_LT)) { |
| 1044 | cursor->curr_rec++; |
| 1045 | } |
| 1046 | } else { |
| 1047 | cursor->curr_rec = grn_array_get_max_id(array) + 1; |
| 1048 | } |
| 1049 | if (min) { |
| 1050 | cursor->tail = min; |
| 1051 | if ((flags & GRN_CURSOR_GT)) { |
| 1052 | cursor->tail++; |
| 1053 | } |
| 1054 | } else { |
| 1055 | cursor->tail = GRN_ID_NIL + 1; |
| 1056 | } |
| 1057 | if (cursor->curr_rec < cursor->tail) { |
| 1058 | cursor->tail = cursor->curr_rec; |
| 1059 | } |
| 1060 | } else { |
| 1061 | cursor->dir = 1; |
| 1062 | if (min) { |
| 1063 | cursor->curr_rec = min; |
| 1064 | if (!(flags & GRN_CURSOR_GT)) { |
| 1065 | cursor->curr_rec--; |
| 1066 | } |
| 1067 | } else { |
| 1068 | cursor->curr_rec = GRN_ID_NIL; |
| 1069 | } |
| 1070 | if (max) { |
| 1071 | cursor->tail = max; |
| 1072 | if ((flags & GRN_CURSOR_LT)) { |
| 1073 | cursor->tail--; |
| 1074 | } |
| 1075 | } else { |
| 1076 | cursor->tail = grn_array_get_max_id(array); |
| 1077 | } |
| 1078 | if (cursor->tail < cursor->curr_rec) { |
| 1079 | cursor->tail = cursor->curr_rec; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | if (*array->n_garbages) { |
| 1084 | while (offset && cursor->curr_rec != cursor->tail) { |
| 1085 | cursor->curr_rec = (grn_id)((int64_t)(cursor->curr_rec) + cursor->dir); |
| 1086 | if (grn_array_bitmap_at(ctx, cursor->array, cursor->curr_rec) == 1) { |
| 1087 | offset--; |
| 1088 | } |
| 1089 | } |
| 1090 | } else { |
| 1091 | cursor->curr_rec = |
| 1092 | (grn_id)((int64_t)(cursor->curr_rec) + (cursor->dir * offset)); |
| 1093 | } |
| 1094 | cursor->rest = (limit < 0) ? *(array->n_entries) : (unsigned int)limit; |
| 1095 | return cursor; |
| 1096 | } |
| 1097 | |
| 1098 | grn_id |
| 1099 | grn_array_cursor_next(grn_ctx *ctx, grn_array_cursor *cursor) |
| 1100 | { |
| 1101 | if (cursor && cursor->rest) { |
| 1102 | while (cursor->curr_rec != cursor->tail) { |
| 1103 | cursor->curr_rec = (grn_id)((int64_t)(cursor->curr_rec) + cursor->dir); |
| 1104 | if (*cursor->array->n_garbages) { |
| 1105 | if (grn_array_bitmap_at(ctx, cursor->array, cursor->curr_rec) != 1) { |
| 1106 | continue; |
| 1107 | } |
| 1108 | } |
| 1109 | cursor->rest--; |
| 1110 | return cursor->curr_rec; |
| 1111 | } |
| 1112 | } |
| 1113 | return GRN_ID_NIL; |
| 1114 | } |
| 1115 | |
| 1116 | grn_id |
| 1117 | grn_array_next(grn_ctx *ctx, grn_array *array, grn_id id) |
| 1118 | { |
| 1119 | grn_id max_id; |
| 1120 | if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) { |
| 1121 | return GRN_ID_NIL; |
| 1122 | } |
| 1123 | max_id = grn_array_get_max_id(array); |
| 1124 | while (++id <= max_id) { |
| 1125 | if (!*array->n_garbages || grn_array_bitmap_at(ctx, array, id) == 1) { |
| 1126 | return id; |
| 1127 | } |
| 1128 | } |
| 1129 | return GRN_ID_NIL; |
| 1130 | } |
| 1131 | |
| 1132 | int |
| 1133 | grn_array_cursor_get_value(grn_ctx *ctx, grn_array_cursor *cursor, void **value) |
| 1134 | { |
| 1135 | if (cursor && value) { |
| 1136 | void *const entry = |
| 1137 | grn_array_entry_at(ctx, cursor->array, cursor->curr_rec, 0); |
| 1138 | if (entry) { |
| 1139 | *value = entry; |
| 1140 | return (int)(cursor->array->value_size); |
| 1141 | } |
| 1142 | } |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | grn_rc |
| 1147 | grn_array_cursor_set_value(grn_ctx *ctx, |
| 1148 | grn_array_cursor *cursor, |
| 1149 | const void *value, |
| 1150 | int flags) |
| 1151 | { |
| 1152 | return grn_array_set_value_inline(ctx, |
| 1153 | cursor->array, |
| 1154 | cursor->curr_rec, |
| 1155 | value, |
| 1156 | flags); |
| 1157 | } |
| 1158 | |
| 1159 | grn_rc |
| 1160 | grn_array_cursor_delete(grn_ctx *ctx, |
| 1161 | grn_array_cursor *cursor, |
| 1162 | grn_table_delete_optarg *optarg) |
| 1163 | { |
| 1164 | return grn_array_delete_by_id(ctx, cursor->array, cursor->curr_rec, optarg); |
| 1165 | } |
| 1166 | |
| 1167 | static inline grn_id |
| 1168 | grn_array_add_to_tiny_array(grn_ctx *ctx, grn_array *array, void **value) |
| 1169 | { |
| 1170 | grn_id id = array->garbages; |
| 1171 | void *entry; |
| 1172 | if (id) { |
| 1173 | /* These operations fail iff the array is broken. */ |
| 1174 | entry = grn_tiny_array_get(&array->array, id); |
| 1175 | if (!entry) { |
| 1176 | ERR(GRN_UNKNOWN_ERROR, |
| 1177 | "[array][add][tiny] failed to get garbage entry: %p: %u(%u)", |
| 1178 | array, |
| 1179 | id, |
| 1180 | *(array->n_garbages)); |
| 1181 | return GRN_ID_NIL; |
| 1182 | } |
| 1183 | if (grn_tiny_bitmap_get_and_set(&array->bitmap, id, 1)) { |
| 1184 | array->garbages = *(grn_id *)entry; |
| 1185 | memset(entry, 0, array->value_size); |
| 1186 | (*array->n_garbages)--; |
| 1187 | } else { |
| 1188 | /* Actually, it is difficult to recover from this error. */ |
| 1189 | ERR(GRN_UNKNOWN_ERROR, |
| 1190 | "[array][add][tiny] failed to reuse garbage ID: %p: %u(%u)", |
| 1191 | array, |
| 1192 | id, |
| 1193 | *(array->n_garbages)); |
| 1194 | return GRN_ID_NIL; |
| 1195 | } |
| 1196 | } else { |
| 1197 | id = array->array.max + 1; |
| 1198 | if (!grn_tiny_bitmap_put_and_set(&array->bitmap, id, 1)) { |
| 1199 | ERR(GRN_UNKNOWN_ERROR, |
| 1200 | "[array][add][tiny] failed to initialize ID: %p: %u", |
| 1201 | array, |
| 1202 | id); |
| 1203 | return GRN_ID_NIL; |
| 1204 | } |
| 1205 | entry = grn_tiny_array_put(&array->array, id); |
| 1206 | if (!entry) { |
| 1207 | ERR(GRN_UNKNOWN_ERROR, |
| 1208 | "[array][add][tiny] failed to create an entry: %p: %u", |
| 1209 | array, |
| 1210 | id); |
| 1211 | if (!grn_tiny_bitmap_get_and_set(&array->bitmap, id, 0)) { |
| 1212 | ERR(GRN_UNKNOWN_ERROR, |
| 1213 | "[array][add][tiny] failed to deinitialize ID: %p: %u", |
| 1214 | array, |
| 1215 | id); |
| 1216 | } |
| 1217 | return GRN_ID_NIL; |
| 1218 | } |
| 1219 | array->array.max = id; |
| 1220 | } |
| 1221 | (*array->n_entries)++; |
| 1222 | if (value) { |
| 1223 | *value = entry; |
| 1224 | } |
| 1225 | return id; |
| 1226 | } |
| 1227 | |
| 1228 | static inline grn_id |
| 1229 | grn_array_add_to_io_array(grn_ctx *ctx, grn_array *array, void **value) |
| 1230 | { |
| 1231 | grn_id id; |
| 1232 | void *entry = NULL; |
| 1233 | struct grn_array_header *header; |
| 1234 | if (grn_array_error_if_truncated(ctx, array) != GRN_SUCCESS) { |
| 1235 | return GRN_ID_NIL; |
| 1236 | } |
| 1237 | if (grn_io_array_wal_touch(ctx, array, "[array][add]") != GRN_SUCCESS) { |
| 1238 | return GRN_ID_NIL; |
| 1239 | } |
| 1240 | header = array->header; |
| 1241 | id = header->garbages; |
| 1242 | if (id) { |
| 1243 | if (grn_array_can_use_value_for_garbage(array)) { |
| 1244 | /* These operations fail iff the array is broken. */ |
| 1245 | entry = grn_array_io_entry_at(ctx, array, id, GRN_TABLE_ADD); |
| 1246 | if (!entry) { |
| 1247 | return GRN_ID_NIL; |
| 1248 | } |
| 1249 | header->garbages = *(grn_id *)entry; |
| 1250 | memset(entry, 0, header->value_size); |
| 1251 | (*array->n_garbages)--; |
| 1252 | if (!grn_io_array_bit_on(ctx, array->io, GRN_ARRAY_BITMAP_SEGMENT, id)) { |
| 1253 | /* Actually, it is difficult to recover from this error. */ |
| 1254 | *(grn_id *)entry = header->garbages; |
| 1255 | header->garbages = id; |
| 1256 | (*array->n_garbages)++; |
| 1257 | return GRN_ID_NIL; |
| 1258 | } |
| 1259 | } else { |
| 1260 | entry = grn_array_io_entry_at(ctx, array, id, GRN_TABLE_ADD); |
| 1261 | if (!entry) { |
| 1262 | return GRN_ID_NIL; |
| 1263 | } |
| 1264 | header->garbages = GRN_ID_NIL; |
| 1265 | (*array->n_garbages)--; |
| 1266 | if (!grn_io_array_bit_on(ctx, array->io, GRN_ARRAY_BITMAP_SEGMENT, id)) { |
| 1267 | /* Actually, it is difficult to recover from this error. */ |
| 1268 | header->garbages = id; |
| 1269 | (*array->n_garbages)++; |
| 1270 | return GRN_ID_NIL; |
| 1271 | } |
| 1272 | if ((*array->n_garbages) > 0 && header->n_garbages_in_buffer > 0) { |
| 1273 | int i; |
| 1274 | for (i = 0; i < GRN_ARRAY_GARBAGES_BUFFER_SIZE; i++) { |
| 1275 | if (header->garbages_buffer[i] != GRN_ID_NIL) { |
| 1276 | header->garbages = header->garbages_buffer[i]; |
| 1277 | header->garbages_buffer[i] = GRN_ID_NIL; |
| 1278 | header->n_garbages_in_buffer--; |
| 1279 | break; |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | } else { |
| 1285 | if (header->curr_rec >= GRN_ARRAY_MAX) { |
| 1286 | return GRN_ID_NIL; |
| 1287 | } |
| 1288 | id = header->curr_rec + 1; |
| 1289 | if (!grn_io_array_bit_on(ctx, array->io, GRN_ARRAY_BITMAP_SEGMENT, id)) { |
| 1290 | return GRN_ID_NIL; |
| 1291 | } |
| 1292 | entry = grn_array_io_entry_at(ctx, array, id, GRN_TABLE_ADD); |
| 1293 | if (!entry) { |
| 1294 | grn_io_array_bit_off(ctx, array->io, GRN_ARRAY_BITMAP_SEGMENT, id); |
| 1295 | return GRN_ID_NIL; |
| 1296 | } |
| 1297 | header->curr_rec = id; |
| 1298 | } |
| 1299 | (*array->n_entries)++; |
| 1300 | if (value) { |
| 1301 | *value = entry; |
| 1302 | } |
| 1303 | return id; |
| 1304 | } |
| 1305 | |
| 1306 | grn_id |
| 1307 | grn_array_add(grn_ctx *ctx, grn_array *array, void **value) |
| 1308 | { |
| 1309 | if (ctx && array) { |
| 1310 | if (grn_array_is_io_array(array)) { |
| 1311 | return grn_array_add_to_io_array(ctx, array, value); |
| 1312 | } else { |
| 1313 | return grn_array_add_to_tiny_array(ctx, array, value); |
| 1314 | } |
| 1315 | } |
| 1316 | return GRN_ID_NIL; |
| 1317 | } |
| 1318 | |
| 1319 | grn_rc |
| 1320 | grn_array_wal_recover(grn_ctx *ctx, grn_array *array) |
| 1321 | { |
| 1322 | if (GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_NONE) { |
| 1323 | return GRN_SUCCESS; |
| 1324 | } |
| 1325 | |
| 1326 | if (!grn_wal_exist(ctx, (grn_obj *)array)) { |
| 1327 | return GRN_SUCCESS; |
| 1328 | } |
| 1329 | |
| 1330 | grn_obj_set_error(ctx, |
| 1331 | (grn_obj *)array, |
| 1332 | GRN_FUNCTION_NOT_IMPLEMENTED, |
| 1333 | GRN_ID_NIL, |
| 1334 | "[array][wal][recover]", |
| 1335 | "not implemented"); |
| 1336 | return ctx->rc; |
| 1337 | } |
| 1338 | |
| 1339 | grn_rc |
| 1340 | grn_array_warm(grn_ctx *ctx, grn_array *array) |
| 1341 | { |
| 1342 | if (grn_array_is_io_array(array)) { |
| 1343 | return grn_io_warm(ctx, array->io); |
| 1344 | } else { |
| 1345 | return ctx->rc; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | /* grn_hash : hash table */ |
| 1350 | |
| 1351 | #define GRN_HASH_MAX_SEGMENT 0x400 |
| 1352 | #define GRN_HASH_HEADER_SIZE_NORMAL 0x9000 |
| 1353 | #define GRN_HASH_HEADER_SIZE_LARGE \ |
| 1354 | (GRN_HASH_HEADER_SIZE_NORMAL + \ |
| 1355 | (sizeof(grn_id) * \ |
| 1356 | (GRN_HASH_MAX_KEY_SIZE_LARGE - GRN_HASH_MAX_KEY_SIZE_NORMAL))) |
| 1357 | #define GRN_HASH_SEGMENT_SIZE 0x400000 |
| 1358 | #define GRN_HASH_KEY_MAX_N_SEGMENTS_NORMAL 0x400 |
| 1359 | #define GRN_HASH_KEY_MAX_N_SEGMENTS_LARGE 0x40000 |
| 1360 | #define W_OF_KEY_IN_A_SEGMENT 22 |
| 1361 | #define GRN_HASH_KEY_MAX_TOTAL_SIZE_NORMAL \ |
| 1362 | (((uint64_t)(1) << W_OF_KEY_IN_A_SEGMENT) * \ |
| 1363 | GRN_HASH_KEY_MAX_N_SEGMENTS_NORMAL - \ |
| 1364 | 1) |
| 1365 | #define GRN_HASH_KEY_MAX_TOTAL_SIZE_LARGE \ |
| 1366 | (((uint64_t)(1) << W_OF_KEY_IN_A_SEGMENT) * \ |
| 1367 | GRN_HASH_KEY_MAX_N_SEGMENTS_LARGE - \ |
| 1368 | 1) |
| 1369 | #define IDX_MASK_IN_A_SEGMENT 0xfffff |
| 1370 | |
| 1371 | static uint32_t grn_hash_initial_max_offset = IDX_MASK_IN_A_SEGMENT + 1; |
| 1372 | |
| 1373 | #define MAX_INDEX_SIZE \ |
| 1374 | ((GRN_HASH_MAX_SEGMENT * (IDX_MASK_IN_A_SEGMENT + 1)) >> 1) |
| 1375 | static uint32_t grn_hash_max_index_size = MAX_INDEX_SIZE; |
| 1376 | |
| 1377 | void |
| 1378 | grn_hash_init_from_env(void) |
| 1379 | { |
| 1380 | { |
| 1381 | /* Just for test. */ |
| 1382 | char grn_hash_initial_max_offset_env[GRN_ENV_BUFFER_SIZE]; |
| 1383 | grn_getenv("GRN_HASH_INITIAL_MAX_OFFSET", |
| 1384 | grn_hash_initial_max_offset_env, |
| 1385 | GRN_ENV_BUFFER_SIZE); |
| 1386 | if (grn_hash_initial_max_offset_env[0]) { |
| 1387 | const char *end = grn_hash_initial_max_offset_env + |
| 1388 | strlen(grn_hash_initial_max_offset_env); |
| 1389 | const char *rest; |
| 1390 | uint32_t offset = grn_atoui(grn_hash_initial_max_offset_env, end, &rest); |
| 1391 | if (end == rest) { |
| 1392 | grn_hash_initial_max_offset = offset; |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | { |
| 1398 | /* Just for test. */ |
| 1399 | char grn_hash_max_index_size_env[GRN_ENV_BUFFER_SIZE]; |
| 1400 | grn_getenv("GRN_HASH_MAX_INDEX_SIZE", |
| 1401 | grn_hash_max_index_size_env, |
| 1402 | GRN_ENV_BUFFER_SIZE); |
| 1403 | if (grn_hash_max_index_size_env[0]) { |
| 1404 | const char *end = |
| 1405 | grn_hash_max_index_size_env + strlen(grn_hash_max_index_size_env); |
| 1406 | const char *rest; |
| 1407 | uint32_t size = grn_atoui(grn_hash_max_index_size_env, end, &rest); |
| 1408 | /* grn_hash_max_index_size must be positive and |
| 1409 | * GRN_HASH_MAX_SEGMENT (2^22) * N. */ |
| 1410 | if (end == rest && size > 0 && (size % GRN_HASH_MAX_SEGMENT) == 0) { |
| 1411 | grn_hash_max_index_size = size; |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | typedef struct { |
| 1418 | uint8_t key[4]; |
| 1419 | uint8_t value[1]; |
| 1420 | } grn_plain_hash_entry; |
| 1421 | |
| 1422 | typedef struct { |
| 1423 | uint32_t hash_value; |
| 1424 | uint8_t key_and_value[1]; |
| 1425 | } grn_rich_hash_entry; |
| 1426 | |
| 1427 | typedef struct { |
| 1428 | uint32_t hash_value; |
| 1429 | uint16_t flag; |
| 1430 | uint16_t key_size; |
| 1431 | union { |
| 1432 | uint8_t buf[sizeof(uint32_t)]; |
| 1433 | uint32_t offset; |
| 1434 | } key; |
| 1435 | uint8_t value[1]; |
| 1436 | } grn_io_hash_entry_normal; |
| 1437 | |
| 1438 | typedef struct { |
| 1439 | uint32_t hash_value; |
| 1440 | uint16_t flag; |
| 1441 | uint16_t key_size; |
| 1442 | union { |
| 1443 | uint8_t buf[sizeof(uint64_t)]; |
| 1444 | uint64_t offset; |
| 1445 | } key; |
| 1446 | uint8_t value[1]; |
| 1447 | } grn_io_hash_entry_large; |
| 1448 | |
| 1449 | typedef struct { |
| 1450 | uint32_t hash_value; |
| 1451 | uint16_t flag; |
| 1452 | uint16_t key_size; |
| 1453 | union { |
| 1454 | uint8_t buf[sizeof(void *)]; |
| 1455 | void *ptr; |
| 1456 | } key; |
| 1457 | uint8_t value[1]; |
| 1458 | } grn_tiny_hash_entry; |
| 1459 | |
| 1460 | /* |
| 1461 | * hash_value is valid even if the entry is grn_plain_hash_entry. In this case, |
| 1462 | * its hash_value equals its key. |
| 1463 | * flag, key_size and key.buf are valid if the entry has a variable length key. |
| 1464 | */ |
| 1465 | typedef struct { |
| 1466 | uint32_t hash_value; |
| 1467 | uint16_t flag; |
| 1468 | uint16_t key_size; |
| 1469 | } grn_hash_entry_header; |
| 1470 | |
| 1471 | typedef union { |
| 1472 | uint32_t hash_value; |
| 1473 | grn_hash_entry_header header; |
| 1474 | grn_plain_hash_entry plain_entry; |
| 1475 | grn_rich_hash_entry rich_entry; |
| 1476 | grn_io_hash_entry_normal io_entry_normal; |
| 1477 | grn_io_hash_entry_large io_entry_large; |
| 1478 | grn_tiny_hash_entry tiny_entry; |
| 1479 | } grn_hash_entry; |
| 1480 | |
| 1481 | typedef struct { |
| 1482 | uint32_t key; |
| 1483 | uint8_t dummy[1]; |
| 1484 | } entry; |
| 1485 | |
| 1486 | typedef struct { |
| 1487 | uint32_t key; |
| 1488 | uint16_t flag; |
| 1489 | uint16_t size; |
| 1490 | uint32_t str; |
| 1491 | uint8_t dummy[1]; |
| 1492 | } entry_str; |
| 1493 | |
| 1494 | enum { |
| 1495 | GRN_HASH_KEY_SEGMENT = 0, |
| 1496 | GRN_HASH_ENTRY_SEGMENT = 1, |
| 1497 | GRN_HASH_INDEX_SEGMENT = 2, |
| 1498 | GRN_HASH_BITMAP_SEGMENT = 3 |
| 1499 | }; |
| 1500 | #define GRN_HASH_N_SEGMENTS 4 |
| 1501 | |
| 1502 | inline static bool |
| 1503 | grn_hash_is_io_hash(grn_hash *hash) |
| 1504 | { |
| 1505 | return hash->io != NULL; |
| 1506 | } |
| 1507 | |
| 1508 | static inline void * |
| 1509 | grn_io_hash_entry_at(grn_ctx *ctx, grn_hash *hash, grn_id id, int flags) |
| 1510 | { |
| 1511 | return grn_io_array_at_inline(ctx, |
| 1512 | hash->io, |
| 1513 | GRN_HASH_ENTRY_SEGMENT, |
| 1514 | id, |
| 1515 | flags); |
| 1516 | } |
| 1517 | |
| 1518 | /* todo : error handling */ |
| 1519 | static inline void * |
| 1520 | grn_hash_entry_at(grn_ctx *ctx, grn_hash *hash, grn_id id, int flags) |
| 1521 | { |
| 1522 | if (grn_hash_is_io_hash(hash)) { |
| 1523 | return grn_io_hash_entry_at(ctx, hash, id, flags); |
| 1524 | } else { |
| 1525 | return grn_tiny_array_at_inline(&hash->a, id); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | static inline bool |
| 1530 | grn_hash_bitmap_at(grn_ctx *ctx, grn_hash *hash, grn_id id) |
| 1531 | { |
| 1532 | if (grn_hash_is_io_hash(hash)) { |
| 1533 | return grn_io_array_bit_at(ctx, hash->io, GRN_HASH_BITMAP_SEGMENT, id) == 1; |
| 1534 | } else { |
| 1535 | return grn_tiny_bitmap_put(&hash->bitmap, id) == 1; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | static inline grn_id * |
| 1540 | grn_io_hash_idx_at(grn_ctx *ctx, grn_hash *hash, grn_id id) |
| 1541 | { |
| 1542 | return grn_io_array_at_inline(ctx, |
| 1543 | hash->io, |
| 1544 | GRN_HASH_INDEX_SEGMENT, |
| 1545 | id, |
| 1546 | GRN_TABLE_ADD); |
| 1547 | } |
| 1548 | |
| 1549 | static inline grn_id * |
| 1550 | grn_hash_idx_at(grn_ctx *ctx, grn_hash *hash, grn_id id) |
| 1551 | { |
| 1552 | if (grn_hash_is_io_hash(hash)) { |
| 1553 | id = (id & *hash->max_offset) + hash->header.common->idx_offset; |
| 1554 | return grn_io_hash_idx_at(ctx, hash, id); |
| 1555 | } else { |
| 1556 | return hash->index + (id & *hash->max_offset); |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | static inline void * |
| 1561 | grn_io_hash_key_at(grn_ctx *ctx, grn_hash *hash, uint64_t pos) |
| 1562 | { |
| 1563 | return grn_io_array_at_inline(ctx, |
| 1564 | hash->io, |
| 1565 | GRN_HASH_KEY_SEGMENT, |
| 1566 | pos, |
| 1567 | GRN_TABLE_ADD); |
| 1568 | } |
| 1569 | |
| 1570 | #define HASH_IMMEDIATE 1 |
| 1571 | |
| 1572 | inline static uint16_t |
| 1573 | grn_hash_entry_get_key_size(grn_hash *hash, grn_hash_entry *entry) |
| 1574 | { |
| 1575 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 1576 | return entry->header.key_size; |
| 1577 | } else { |
| 1578 | return (uint16_t)(hash->key_size); |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | static inline char * |
| 1583 | grn_hash_entry_get_key(grn_ctx *ctx, grn_hash *hash, grn_hash_entry *entry) |
| 1584 | { |
| 1585 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 1586 | if (grn_hash_is_io_hash(hash)) { |
| 1587 | if (grn_hash_is_large_total_key_size(ctx, hash)) { |
| 1588 | if (entry->io_entry_large.flag & HASH_IMMEDIATE) { |
| 1589 | return (char *)entry->io_entry_large.key.buf; |
| 1590 | } else { |
| 1591 | return (char *)grn_io_hash_key_at(ctx, |
| 1592 | hash, |
| 1593 | entry->io_entry_large.key.offset); |
| 1594 | } |
| 1595 | } else { |
| 1596 | if (entry->io_entry_normal.flag & HASH_IMMEDIATE) { |
| 1597 | return (char *)entry->io_entry_normal.key.buf; |
| 1598 | } else { |
| 1599 | return (char *)grn_io_hash_key_at(ctx, |
| 1600 | hash, |
| 1601 | entry->io_entry_normal.key.offset); |
| 1602 | } |
| 1603 | } |
| 1604 | } else { |
| 1605 | if (entry->tiny_entry.flag & HASH_IMMEDIATE) { |
| 1606 | return (char *)entry->tiny_entry.key.buf; |
| 1607 | } else { |
| 1608 | return entry->tiny_entry.key.ptr; |
| 1609 | } |
| 1610 | } |
| 1611 | } else { |
| 1612 | if (hash->key_size == sizeof(uint32_t)) { |
| 1613 | return (char *)entry->plain_entry.key; |
| 1614 | } else { |
| 1615 | return (char *)entry->rich_entry.key_and_value; |
| 1616 | } |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | static inline void * |
| 1621 | grn_hash_entry_get_value(grn_ctx *ctx, grn_hash *hash, grn_hash_entry *entry) |
| 1622 | { |
| 1623 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 1624 | if (grn_hash_is_io_hash(hash)) { |
| 1625 | if (grn_hash_is_large_total_key_size(ctx, hash)) { |
| 1626 | return entry->io_entry_large.value; |
| 1627 | } else { |
| 1628 | return entry->io_entry_normal.value; |
| 1629 | } |
| 1630 | } else { |
| 1631 | return entry->tiny_entry.value; |
| 1632 | } |
| 1633 | } else { |
| 1634 | if (hash->key_size == sizeof(uint32_t)) { |
| 1635 | return entry->plain_entry.value; |
| 1636 | } else { |
| 1637 | return entry->rich_entry.key_and_value + hash->key_size; |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | static inline grn_rc |
| 1643 | grn_io_hash_entry_put_key(grn_ctx *ctx, |
| 1644 | grn_hash *hash, |
| 1645 | grn_hash_entry *entry, |
| 1646 | const void *key, |
| 1647 | unsigned int key_size) |
| 1648 | { |
| 1649 | bool key_exist; |
| 1650 | uint64_t key_offset; |
| 1651 | grn_io_hash_entry_normal *io_entry_normal = &(entry->io_entry_normal); |
| 1652 | grn_io_hash_entry_large *io_entry_large = &(entry->io_entry_large); |
| 1653 | |
| 1654 | bool is_large_mode = grn_hash_is_large_total_key_size(ctx, hash); |
| 1655 | |
| 1656 | if (is_large_mode) { |
| 1657 | key_exist = (io_entry_large->key_size > 0); |
| 1658 | } else { |
| 1659 | key_exist = (io_entry_normal->key_size > 0); |
| 1660 | } |
| 1661 | |
| 1662 | if (key_exist) { |
| 1663 | if (is_large_mode) { |
| 1664 | key_offset = io_entry_large->key.offset; |
| 1665 | } else { |
| 1666 | key_offset = io_entry_normal->key.offset; |
| 1667 | } |
| 1668 | } else { |
| 1669 | uint64_t segment_id; |
| 1670 | grn_hash_header_common *header; |
| 1671 | uint64_t curr_key; |
| 1672 | uint64_t max_total_size; |
| 1673 | |
| 1674 | header = hash->header.common; |
| 1675 | if (key_size >= GRN_HASH_SEGMENT_SIZE) { |
| 1676 | GRN_DEFINE_NAME(hash); |
| 1677 | ERR(GRN_INVALID_ARGUMENT, |
| 1678 | "[hash][key][put] too long key: <%.*s>: max=%u: key size=%u", |
| 1679 | name_size, |
| 1680 | name, |
| 1681 | GRN_HASH_SEGMENT_SIZE, |
| 1682 | key_size); |
| 1683 | return ctx->rc; |
| 1684 | } |
| 1685 | |
| 1686 | if (is_large_mode) { |
| 1687 | curr_key = header->curr_key_large; |
| 1688 | max_total_size = GRN_HASH_KEY_MAX_TOTAL_SIZE_LARGE; |
| 1689 | } else { |
| 1690 | curr_key = header->curr_key_normal; |
| 1691 | max_total_size = GRN_HASH_KEY_MAX_TOTAL_SIZE_NORMAL; |
| 1692 | } |
| 1693 | |
| 1694 | if (key_size > (max_total_size - curr_key)) { |
| 1695 | GRN_DEFINE_NAME(hash); |
| 1696 | ERR(GRN_NOT_ENOUGH_SPACE, |
| 1697 | "[hash][key][put] total key size is over: <%.*s>: " |
| 1698 | "max=%" GRN_FMT_INT64U ": " |
| 1699 | "current=%" GRN_FMT_INT64U ": " |
| 1700 | "new key size=%u", |
| 1701 | name_size, |
| 1702 | name, |
| 1703 | max_total_size, |
| 1704 | curr_key, |
| 1705 | key_size); |
| 1706 | return ctx->rc; |
| 1707 | } |
| 1708 | key_offset = curr_key; |
| 1709 | segment_id = (key_offset + key_size) >> W_OF_KEY_IN_A_SEGMENT; |
| 1710 | if ((key_offset >> W_OF_KEY_IN_A_SEGMENT) != segment_id) { |
| 1711 | key_offset = segment_id << W_OF_KEY_IN_A_SEGMENT; |
| 1712 | if (is_large_mode) { |
| 1713 | header->curr_key_large = key_offset; |
| 1714 | } else { |
| 1715 | header->curr_key_normal = (uint32_t)key_offset; |
| 1716 | } |
| 1717 | } |
| 1718 | if (is_large_mode) { |
| 1719 | header->curr_key_large += key_size; |
| 1720 | io_entry_large->key.offset = key_offset; |
| 1721 | } else { |
| 1722 | header->curr_key_normal += key_size; |
| 1723 | io_entry_normal->key.offset = (uint32_t)key_offset; |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | { |
| 1728 | void *const key_ptr = grn_io_hash_key_at(ctx, hash, key_offset); |
| 1729 | if (!key_ptr) { |
| 1730 | GRN_DEFINE_NAME(hash); |
| 1731 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 1732 | "[hash][key][put] failed to allocate for new key: <%.*s>: " |
| 1733 | "new offset:%" GRN_FMT_INT64U " " |
| 1734 | "key size:%u", |
| 1735 | name_size, |
| 1736 | name, |
| 1737 | key_offset, |
| 1738 | key_size); |
| 1739 | return ctx->rc; |
| 1740 | } |
| 1741 | grn_memcpy(key_ptr, key, key_size); |
| 1742 | } |
| 1743 | return GRN_SUCCESS; |
| 1744 | } |
| 1745 | |
| 1746 | static inline grn_rc |
| 1747 | grn_hash_entry_put_key(grn_ctx *ctx, |
| 1748 | grn_hash *hash, |
| 1749 | grn_hash_entry *entry, |
| 1750 | uint32_t hash_value, |
| 1751 | const void *key, |
| 1752 | unsigned int key_size) |
| 1753 | { |
| 1754 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 1755 | if (grn_hash_is_io_hash(hash)) { |
| 1756 | uint8_t *buffer; |
| 1757 | size_t buffer_size; |
| 1758 | uint16_t flag; |
| 1759 | |
| 1760 | bool is_large_mode = grn_hash_is_large_total_key_size(ctx, hash); |
| 1761 | if (is_large_mode) { |
| 1762 | buffer = entry->io_entry_large.key.buf; |
| 1763 | buffer_size = sizeof(entry->io_entry_large.key.buf); |
| 1764 | } else { |
| 1765 | buffer = entry->io_entry_normal.key.buf; |
| 1766 | buffer_size = sizeof(entry->io_entry_normal.key.buf); |
| 1767 | } |
| 1768 | |
| 1769 | if (key_size <= buffer_size) { |
| 1770 | grn_memcpy(buffer, key, key_size); |
| 1771 | flag = HASH_IMMEDIATE; |
| 1772 | } else { |
| 1773 | const grn_rc rc = |
| 1774 | grn_io_hash_entry_put_key(ctx, hash, entry, key, key_size); |
| 1775 | if (rc) { |
| 1776 | return rc; |
| 1777 | } |
| 1778 | flag = 0; |
| 1779 | } |
| 1780 | |
| 1781 | if (is_large_mode) { |
| 1782 | entry->io_entry_large.flag = flag; |
| 1783 | entry->io_entry_large.hash_value = hash_value; |
| 1784 | entry->io_entry_large.key_size = (uint16_t)key_size; |
| 1785 | } else { |
| 1786 | entry->io_entry_normal.flag = flag; |
| 1787 | entry->io_entry_normal.hash_value = hash_value; |
| 1788 | entry->io_entry_normal.key_size = (uint16_t)key_size; |
| 1789 | } |
| 1790 | } else { |
| 1791 | if (key_size <= sizeof(entry->tiny_entry.key.buf)) { |
| 1792 | grn_memcpy(entry->tiny_entry.key.buf, key, key_size); |
| 1793 | entry->tiny_entry.flag = HASH_IMMEDIATE; |
| 1794 | } else { |
| 1795 | grn_ctx *const ctx = hash->ctx; |
| 1796 | entry->tiny_entry.key.ptr = GRN_CTX_ALLOC(ctx, key_size); |
| 1797 | if (!entry->tiny_entry.key.ptr) { |
| 1798 | return GRN_NO_MEMORY_AVAILABLE; |
| 1799 | } |
| 1800 | grn_memcpy(entry->tiny_entry.key.ptr, key, key_size); |
| 1801 | entry->tiny_entry.flag = 0; |
| 1802 | } |
| 1803 | entry->tiny_entry.hash_value = hash_value; |
| 1804 | entry->tiny_entry.key_size = (uint16_t)key_size; |
| 1805 | } |
| 1806 | } else { |
| 1807 | if (hash->key_size == sizeof(uint32_t)) { |
| 1808 | *(uint32_t *)entry->plain_entry.key = hash_value; |
| 1809 | } else { |
| 1810 | entry->rich_entry.hash_value = hash_value; |
| 1811 | grn_memcpy(entry->rich_entry.key_and_value, key, key_size); |
| 1812 | } |
| 1813 | } |
| 1814 | return GRN_SUCCESS; |
| 1815 | } |
| 1816 | |
| 1817 | /* |
| 1818 | * grn_hash_entry_compare_key() returns true if the entry key equals the |
| 1819 | * specified key, or false otherwise. |
| 1820 | */ |
| 1821 | static inline bool |
| 1822 | grn_hash_entry_compare_key(grn_ctx *ctx, |
| 1823 | grn_hash *hash, |
| 1824 | grn_hash_entry *entry, |
| 1825 | uint32_t hash_value, |
| 1826 | const void *key, |
| 1827 | unsigned int key_size) |
| 1828 | { |
| 1829 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 1830 | if (entry->hash_value != hash_value || entry->header.key_size != key_size) { |
| 1831 | return false; |
| 1832 | } |
| 1833 | if (grn_hash_is_io_hash(hash)) { |
| 1834 | if (grn_hash_is_large_total_key_size(ctx, hash)) { |
| 1835 | if (entry->io_entry_large.flag & HASH_IMMEDIATE) { |
| 1836 | return !memcmp(key, entry->io_entry_large.key.buf, key_size); |
| 1837 | } else { |
| 1838 | const void *const entry_key_ptr = |
| 1839 | grn_io_hash_key_at(ctx, hash, entry->io_entry_large.key.offset); |
| 1840 | return !memcmp(key, entry_key_ptr, key_size); |
| 1841 | } |
| 1842 | } else { |
| 1843 | if (entry->io_entry_normal.flag & HASH_IMMEDIATE) { |
| 1844 | return !memcmp(key, entry->io_entry_normal.key.buf, key_size); |
| 1845 | } else { |
| 1846 | const void *const entry_key_ptr = |
| 1847 | grn_io_hash_key_at(ctx, hash, entry->io_entry_normal.key.offset); |
| 1848 | return !memcmp(key, entry_key_ptr, key_size); |
| 1849 | } |
| 1850 | } |
| 1851 | } else { |
| 1852 | if (entry->tiny_entry.flag & HASH_IMMEDIATE) { |
| 1853 | return !memcmp(key, entry->tiny_entry.key.buf, key_size); |
| 1854 | } else { |
| 1855 | return !memcmp(key, entry->tiny_entry.key.ptr, key_size); |
| 1856 | } |
| 1857 | } |
| 1858 | } else { |
| 1859 | if (entry->hash_value != hash_value) { |
| 1860 | return false; |
| 1861 | } |
| 1862 | if (key_size == sizeof(uint32_t)) { |
| 1863 | return true; |
| 1864 | } else { |
| 1865 | return !memcmp(key, entry->rich_entry.key_and_value, key_size); |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | static inline char * |
| 1871 | get_key(grn_ctx *ctx, grn_hash *hash, entry_str *n) |
| 1872 | { |
| 1873 | return grn_hash_entry_get_key(ctx, hash, (grn_hash_entry *)n); |
| 1874 | } |
| 1875 | |
| 1876 | static inline void * |
| 1877 | get_value(grn_ctx *ctx, grn_hash *hash, entry_str *n) |
| 1878 | { |
| 1879 | return grn_hash_entry_get_value(ctx, hash, (grn_hash_entry *)n); |
| 1880 | } |
| 1881 | |
| 1882 | #define GARBAGE (0xffffffff) |
| 1883 | |
| 1884 | inline static uint32_t |
| 1885 | grn_io_hash_calculate_entry_size(uint32_t key_size, |
| 1886 | uint32_t value_size, |
| 1887 | uint32_t flags) |
| 1888 | { |
| 1889 | if (flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 1890 | if (flags & GRN_OBJ_KEY_LARGE) { |
| 1891 | return offsetof(grn_io_hash_entry_large, value) + value_size; |
| 1892 | } else { |
| 1893 | return offsetof(grn_io_hash_entry_normal, value) + value_size; |
| 1894 | } |
| 1895 | } else { |
| 1896 | if (key_size == sizeof(uint32_t)) { |
| 1897 | return offsetof(grn_plain_hash_entry, value) + value_size; |
| 1898 | } else { |
| 1899 | return (uint32_t)(offsetof(grn_rich_hash_entry, key_and_value) + |
| 1900 | key_size + value_size); |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | static grn_io * |
| 1906 | grn_io_hash_create_io(grn_ctx *ctx, |
| 1907 | const char *path, |
| 1908 | uint32_t header_size, |
| 1909 | uint32_t entry_size, |
| 1910 | uint32_t flags) |
| 1911 | { |
| 1912 | uint32_t w_of_element = 0; |
| 1913 | grn_io_array_spec array_spec[GRN_HASH_N_SEGMENTS]; |
| 1914 | |
| 1915 | while ((1U << w_of_element) < entry_size) { |
| 1916 | w_of_element++; |
| 1917 | } |
| 1918 | |
| 1919 | array_spec[GRN_HASH_KEY_SEGMENT].w_of_element = 0; |
| 1920 | if (flags & GRN_OBJ_KEY_LARGE) { |
| 1921 | array_spec[GRN_HASH_KEY_SEGMENT].max_n_segments = |
| 1922 | GRN_HASH_KEY_MAX_N_SEGMENTS_LARGE; |
| 1923 | } else { |
| 1924 | array_spec[GRN_HASH_KEY_SEGMENT].max_n_segments = |
| 1925 | GRN_HASH_KEY_MAX_N_SEGMENTS_NORMAL; |
| 1926 | } |
| 1927 | array_spec[GRN_HASH_ENTRY_SEGMENT].w_of_element = w_of_element; |
| 1928 | array_spec[GRN_HASH_ENTRY_SEGMENT].max_n_segments = |
| 1929 | 1U << (30 - (22 - w_of_element)); |
| 1930 | const uint32_t index_w_of_element = 2; /* log2(sizeof(grn_id)) */ |
| 1931 | array_spec[GRN_HASH_INDEX_SEGMENT].w_of_element = index_w_of_element; |
| 1932 | array_spec[GRN_HASH_INDEX_SEGMENT].max_n_segments = |
| 1933 | 1U << (30 - (22 - index_w_of_element)); |
| 1934 | array_spec[GRN_HASH_BITMAP_SEGMENT].w_of_element = 0; |
| 1935 | array_spec[GRN_HASH_BITMAP_SEGMENT].max_n_segments = 1U << (30 - (22 + 3)); |
| 1936 | return grn_io_create_with_array(ctx, |
| 1937 | path, |
| 1938 | header_size, |
| 1939 | GRN_HASH_SEGMENT_SIZE, |
| 1940 | GRN_IO_AUTO, |
| 1941 | GRN_HASH_N_SEGMENTS, |
| 1942 | array_spec); |
| 1943 | } |
| 1944 | |
| 1945 | struct _grn_hash_wal_add_entry_data { |
| 1946 | grn_hash *hash; |
| 1947 | uint64_t wal_id; |
| 1948 | const char *tag; |
| 1949 | grn_wal_event event; |
| 1950 | grn_id record_id; |
| 1951 | const void *key; |
| 1952 | size_t key_size; |
| 1953 | uint32_t key_hash_value; |
| 1954 | uint64_t key_offset; |
| 1955 | uint32_t index_hash_value; |
| 1956 | const void *value; |
| 1957 | grn_id garbage_record_id; |
| 1958 | grn_id next_garbage_record_id; |
| 1959 | uint32_t n_entries; |
| 1960 | uint32_t n_garbages; |
| 1961 | bool found_garbage; |
| 1962 | uint32_t max_offset; |
| 1963 | uint32_t expected_n_entries; |
| 1964 | }; |
| 1965 | |
| 1966 | static grn_rc |
| 1967 | grn_io_hash_init(grn_ctx *ctx, |
| 1968 | grn_hash *hash, |
| 1969 | const char *path, |
| 1970 | uint32_t key_size, |
| 1971 | uint32_t value_size, |
| 1972 | uint32_t flags, |
| 1973 | grn_encoding encoding, |
| 1974 | uint32_t init_size) |
| 1975 | { |
| 1976 | grn_io *io; |
| 1977 | grn_hash_header_common *header; |
| 1978 | uint32_t header_size, entry_size, max_offset; |
| 1979 | |
| 1980 | if (key_size <= GRN_HASH_MAX_KEY_SIZE_NORMAL) { |
| 1981 | header_size = GRN_HASH_HEADER_SIZE_NORMAL; |
| 1982 | } else { |
| 1983 | header_size = GRN_HASH_HEADER_SIZE_LARGE; |
| 1984 | } |
| 1985 | entry_size = grn_io_hash_calculate_entry_size(key_size, value_size, flags); |
| 1986 | |
| 1987 | io = grn_io_hash_create_io(ctx, path, header_size, entry_size, flags); |
| 1988 | if (!io) { |
| 1989 | return GRN_NO_MEMORY_AVAILABLE; |
| 1990 | } |
| 1991 | grn_io_set_type(io, GRN_TABLE_HASH_KEY); |
| 1992 | |
| 1993 | max_offset = grn_hash_initial_max_offset; |
| 1994 | while (max_offset < init_size * 2) { |
| 1995 | max_offset *= 2; |
| 1996 | } |
| 1997 | max_offset--; |
| 1998 | |
| 1999 | if (encoding == GRN_ENC_DEFAULT) { |
| 2000 | encoding = ctx->encoding; |
| 2001 | } |
| 2002 | |
| 2003 | hash->key_size = key_size; |
| 2004 | |
| 2005 | header = grn_io_header(io); |
| 2006 | header->flags = flags; |
| 2007 | header->encoding = encoding; |
| 2008 | header->key_size = key_size; |
| 2009 | header->curr_rec = 0; |
| 2010 | header->curr_key_normal = 0; |
| 2011 | header->curr_key_large = 0; |
| 2012 | header->lock = 0; |
| 2013 | header->idx_offset = 0; |
| 2014 | header->value_size = value_size; |
| 2015 | header->entry_size = entry_size; |
| 2016 | header->max_offset = max_offset; |
| 2017 | header->n_entries = 0; |
| 2018 | header->n_garbages = 0; |
| 2019 | header->tokenizer = GRN_ID_NIL; |
| 2020 | grn_table_modules_init(ctx, &(hash->normalizers)); |
| 2021 | if (header->flags & GRN_OBJ_KEY_NORMALIZE) { |
| 2022 | header->flags &= ~((uint32_t)GRN_OBJ_KEY_NORMALIZE); |
| 2023 | header->normalizer = GRN_ID_NIL; |
| 2024 | grn_obj *normalizer = grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1); |
| 2025 | grn_table_modules_add(ctx, &(hash->normalizers), normalizer); |
| 2026 | } else { |
| 2027 | header->normalizer = GRN_ID_NIL; |
| 2028 | } |
| 2029 | header->truncated = false; |
| 2030 | grn_table_modules_init(ctx, &(hash->token_filters)); |
| 2031 | GRN_PTR_INIT(&(hash->token_filter_procs), GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 2032 | grn_table_modules_init(ctx, &(hash->extractors)); |
| 2033 | |
| 2034 | hash->obj.header.flags = (header->flags & GRN_OBJ_FLAGS_MASK); |
| 2035 | hash->ctx = ctx; |
| 2036 | hash->encoding = encoding; |
| 2037 | hash->value_size = value_size; |
| 2038 | hash->entry_size = entry_size; |
| 2039 | hash->n_garbages = &header->n_garbages; |
| 2040 | hash->n_entries = &header->n_entries; |
| 2041 | hash->max_offset = &header->max_offset; |
| 2042 | hash->io = io; |
| 2043 | hash->header.common = header; |
| 2044 | hash->lock = &header->lock; |
| 2045 | grn_table_module_init(ctx, &(hash->tokenizer), GRN_ID_NIL); |
| 2046 | return GRN_SUCCESS; |
| 2047 | } |
| 2048 | |
| 2049 | #define INITIAL_INDEX_SIZE 256U |
| 2050 | |
| 2051 | static uint32_t |
| 2052 | grn_tiny_hash_calculate_entry_size(uint32_t key_size, |
| 2053 | uint32_t value_size, |
| 2054 | uint32_t flags) |
| 2055 | { |
| 2056 | uint32_t entry_size; |
| 2057 | if (flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 2058 | entry_size = offsetof(grn_tiny_hash_entry, value) + value_size; |
| 2059 | } else { |
| 2060 | if (key_size == sizeof(uint32_t)) { |
| 2061 | entry_size = offsetof(grn_plain_hash_entry, value) + value_size; |
| 2062 | } else { |
| 2063 | entry_size = (uint32_t)(offsetof(grn_rich_hash_entry, key_and_value) + |
| 2064 | key_size + value_size); |
| 2065 | } |
| 2066 | } |
| 2067 | if (entry_size != sizeof(uint32_t)) { |
| 2068 | entry_size += sizeof(uintptr_t) - 1; |
| 2069 | entry_size &= ~((uint32_t)(sizeof(uintptr_t) - 1)); |
| 2070 | } |
| 2071 | return entry_size; |
| 2072 | } |
| 2073 | |
| 2074 | static grn_rc |
| 2075 | grn_tiny_hash_init(grn_ctx *ctx, |
| 2076 | grn_hash *hash, |
| 2077 | const char *path, |
| 2078 | uint32_t key_size, |
| 2079 | uint32_t value_size, |
| 2080 | uint32_t flags, |
| 2081 | grn_encoding encoding) |
| 2082 | { |
| 2083 | uint32_t entry_size; |
| 2084 | |
| 2085 | if (path) { |
| 2086 | return GRN_INVALID_ARGUMENT; |
| 2087 | } |
| 2088 | hash->index = GRN_CTX_ALLOC(ctx, INITIAL_INDEX_SIZE * sizeof(grn_id)); |
| 2089 | if (!hash->index) { |
| 2090 | return GRN_NO_MEMORY_AVAILABLE; |
| 2091 | } |
| 2092 | |
| 2093 | entry_size = grn_tiny_hash_calculate_entry_size(key_size, value_size, flags); |
| 2094 | hash->obj.header.flags = (grn_obj_flags)flags; |
| 2095 | hash->ctx = ctx; |
| 2096 | hash->key_size = key_size; |
| 2097 | hash->encoding = encoding; |
| 2098 | hash->value_size = value_size; |
| 2099 | hash->entry_size = entry_size; |
| 2100 | hash->n_garbages = &hash->n_garbages_; |
| 2101 | hash->n_entries = &hash->n_entries_; |
| 2102 | hash->max_offset = &hash->max_offset_; |
| 2103 | hash->max_offset_ = INITIAL_INDEX_SIZE - 1; |
| 2104 | hash->io = NULL; |
| 2105 | hash->header.common = NULL; |
| 2106 | hash->n_garbages_ = 0; |
| 2107 | hash->n_entries_ = 0; |
| 2108 | hash->garbages = GRN_ID_NIL; |
| 2109 | grn_table_module_init(ctx, &(hash->tokenizer), GRN_ID_NIL); |
| 2110 | grn_table_modules_init(ctx, &(hash->normalizers)); |
| 2111 | grn_table_modules_init(ctx, &(hash->token_filters)); |
| 2112 | GRN_PTR_INIT(&(hash->token_filter_procs), GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 2113 | grn_table_modules_init(ctx, &(hash->extractors)); |
| 2114 | grn_tiny_array_init(ctx, |
| 2115 | &hash->a, |
| 2116 | (uint16_t)entry_size, |
| 2117 | GRN_TINY_ARRAY_CLEAR); |
| 2118 | grn_tiny_bitmap_init(ctx, &hash->bitmap); |
| 2119 | return GRN_SUCCESS; |
| 2120 | } |
| 2121 | |
| 2122 | static grn_rc |
| 2123 | grn_hash_init_wal_data(grn_ctx *ctx, grn_hash *hash) |
| 2124 | { |
| 2125 | hash->wal_data = GRN_CALLOC(sizeof(grn_hash_wal_add_entry_data)); |
| 2126 | if (!hash->wal_data) { |
| 2127 | return GRN_NO_MEMORY_AVAILABLE; |
| 2128 | } |
| 2129 | hash->wal_data->hash = hash; |
| 2130 | return GRN_SUCCESS; |
| 2131 | } |
| 2132 | |
| 2133 | static grn_rc |
| 2134 | grn_hash_init(grn_ctx *ctx, |
| 2135 | grn_hash *hash, |
| 2136 | const char *path, |
| 2137 | uint32_t key_size, |
| 2138 | uint32_t value_size, |
| 2139 | uint32_t flags) |
| 2140 | { |
| 2141 | grn_rc rc; |
| 2142 | if (flags & GRN_HASH_TINY) { |
| 2143 | rc = grn_tiny_hash_init(ctx, |
| 2144 | hash, |
| 2145 | path, |
| 2146 | key_size, |
| 2147 | value_size, |
| 2148 | flags, |
| 2149 | ctx->encoding); |
| 2150 | } else { |
| 2151 | rc = grn_io_hash_init(ctx, |
| 2152 | hash, |
| 2153 | path, |
| 2154 | key_size, |
| 2155 | value_size, |
| 2156 | flags, |
| 2157 | ctx->encoding, |
| 2158 | 0); |
| 2159 | } |
| 2160 | if (rc == GRN_SUCCESS) { |
| 2161 | rc = grn_hash_init_wal_data(ctx, hash); |
| 2162 | } |
| 2163 | return rc; |
| 2164 | } |
| 2165 | |
| 2166 | grn_hash * |
| 2167 | grn_hash_create(grn_ctx *ctx, |
| 2168 | const char *path, |
| 2169 | uint32_t key_size, |
| 2170 | uint32_t value_size, |
| 2171 | uint32_t flags) |
| 2172 | { |
| 2173 | const char *tag = "[hash][create]"; |
| 2174 | grn_hash *hash; |
| 2175 | if (!ctx) { |
| 2176 | grn_ctx tmp_ctx; |
| 2177 | grn_ctx_init(&tmp_ctx, 0); |
| 2178 | ctx = &tmp_ctx; |
| 2179 | ERR(GRN_INVALID_ARGUMENT, "%s ctx must not NULL", tag); |
| 2180 | grn_ctx_fin(&tmp_ctx); |
| 2181 | return NULL; |
| 2182 | } |
| 2183 | if (key_size > GRN_HASH_MAX_KEY_SIZE_LARGE) { |
| 2184 | ERR(GRN_INVALID_ARGUMENT, |
| 2185 | "%s too large key size: %u > %u", |
| 2186 | tag, |
| 2187 | key_size, |
| 2188 | GRN_HASH_MAX_KEY_SIZE_LARGE); |
| 2189 | return NULL; |
| 2190 | } |
| 2191 | hash = (grn_hash *)GRN_CALLOC(sizeof(grn_hash)); |
| 2192 | if (!hash) { |
| 2193 | grn_rc rc = ctx->rc; |
| 2194 | if (rc == GRN_SUCCESS) { |
| 2195 | rc = GRN_NO_MEMORY_AVAILABLE; |
| 2196 | } |
| 2197 | ERR(rc, |
| 2198 | "%s failed to allocate grn_hash%s%s", |
| 2199 | tag, |
| 2200 | ctx->errbuf[0] == '\0' ? "" : ": ", |
| 2201 | ctx->errbuf[0] == '\0' ? "" : ctx->errbuf); |
| 2202 | return NULL; |
| 2203 | } |
| 2204 | GRN_DB_OBJ_SET_TYPE(hash, GRN_TABLE_HASH_KEY); |
| 2205 | if (grn_hash_init(ctx, hash, path, key_size, value_size, flags) != |
| 2206 | GRN_SUCCESS) { |
| 2207 | GRN_FREE(hash); |
| 2208 | return NULL; |
| 2209 | } |
| 2210 | return hash; |
| 2211 | } |
| 2212 | |
| 2213 | grn_hash * |
| 2214 | grn_hash_open(grn_ctx *ctx, const char *path) |
| 2215 | { |
| 2216 | if (ctx) { |
| 2217 | grn_io *const io = grn_io_open(ctx, path, GRN_IO_AUTO); |
| 2218 | if (io) { |
| 2219 | grn_hash_header_common *const header = grn_io_header(io); |
| 2220 | uint32_t io_type = grn_io_get_type(io); |
| 2221 | if (io_type == GRN_TABLE_HASH_KEY) { |
| 2222 | grn_hash *const hash = (grn_hash *)GRN_CALLOC(sizeof(grn_hash)); |
| 2223 | if (hash) { |
| 2224 | if (!(header->flags & GRN_HASH_TINY)) { |
| 2225 | if (grn_hash_init_wal_data(ctx, hash) != GRN_SUCCESS) { |
| 2226 | GRN_FREE(hash); |
| 2227 | return NULL; |
| 2228 | } |
| 2229 | GRN_DB_OBJ_SET_TYPE(hash, GRN_TABLE_HASH_KEY); |
| 2230 | hash->ctx = ctx; |
| 2231 | hash->key_size = header->key_size; |
| 2232 | hash->encoding = header->encoding; |
| 2233 | hash->value_size = header->value_size; |
| 2234 | hash->entry_size = header->entry_size; |
| 2235 | hash->n_garbages = &header->n_garbages; |
| 2236 | hash->n_entries = &header->n_entries; |
| 2237 | hash->max_offset = &header->max_offset; |
| 2238 | hash->io = io; |
| 2239 | hash->header.common = header; |
| 2240 | hash->lock = &header->lock; |
| 2241 | grn_table_module_init(ctx, &(hash->tokenizer), header->tokenizer); |
| 2242 | grn_table_modules_init(ctx, &(hash->normalizers)); |
| 2243 | if (header->flags & GRN_OBJ_KEY_NORMALIZE) { |
| 2244 | header->flags &= ~((uint32_t)GRN_OBJ_KEY_NORMALIZE); |
| 2245 | header->normalizer = GRN_ID_NIL; |
| 2246 | grn_obj *normalizer = |
| 2247 | grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1); |
| 2248 | grn_table_modules_add(ctx, &(hash->normalizers), normalizer); |
| 2249 | } else if (header->normalizer != GRN_ID_NIL) { |
| 2250 | grn_obj *normalizer = grn_ctx_at(ctx, header->normalizer); |
| 2251 | grn_table_modules_add(ctx, &(hash->normalizers), normalizer); |
| 2252 | } |
| 2253 | grn_table_modules_init(ctx, &(hash->token_filters)); |
| 2254 | GRN_PTR_INIT(&(hash->token_filter_procs), |
| 2255 | GRN_OBJ_VECTOR, |
| 2256 | GRN_ID_NIL); |
| 2257 | grn_table_modules_init(ctx, &(hash->extractors)); |
| 2258 | hash->obj.header.flags = (grn_obj_flags)(header->flags); |
| 2259 | return hash; |
| 2260 | } else { |
| 2261 | GRN_LOG(ctx, |
| 2262 | GRN_LOG_NOTICE, |
| 2263 | "invalid hash flag. (%x)", |
| 2264 | header->flags); |
| 2265 | } |
| 2266 | GRN_FREE(hash); |
| 2267 | } |
| 2268 | } else { |
| 2269 | ERR(GRN_INVALID_FORMAT, |
| 2270 | "[table][hash] file type must be %#04x: <%#04x>", |
| 2271 | GRN_TABLE_HASH_KEY, |
| 2272 | io_type); |
| 2273 | } |
| 2274 | grn_io_close(ctx, io); |
| 2275 | } |
| 2276 | } |
| 2277 | return NULL; |
| 2278 | } |
| 2279 | |
| 2280 | /* |
| 2281 | * grn_hash_error_if_truncated() logs an error and returns its error code if |
| 2282 | * a hash is truncated by another process. |
| 2283 | * Otherwise, this function returns GRN_SUCCESS. |
| 2284 | * Note that `ctx` and `hash` must be valid. |
| 2285 | * |
| 2286 | * FIXME: A hash should be reopened if possible. |
| 2287 | */ |
| 2288 | static grn_rc |
| 2289 | grn_hash_error_if_truncated(grn_ctx *ctx, grn_hash *hash) |
| 2290 | { |
| 2291 | if (hash->header.common && hash->header.common->truncated) { |
| 2292 | ERR(GRN_FILE_CORRUPT, |
| 2293 | "hash is truncated, please unmap or reopen the database"); |
| 2294 | return GRN_FILE_CORRUPT; |
| 2295 | } |
| 2296 | return GRN_SUCCESS; |
| 2297 | } |
| 2298 | |
| 2299 | static void |
| 2300 | grn_hash_close_token_filters(grn_ctx *ctx, grn_hash *hash) |
| 2301 | { |
| 2302 | grn_table_modules_fin(ctx, &(hash->token_filters)); |
| 2303 | GRN_OBJ_FIN(ctx, &(hash->token_filter_procs)); |
| 2304 | } |
| 2305 | |
| 2306 | static grn_rc |
| 2307 | grn_io_hash_fin(grn_ctx *ctx, grn_hash *hash) |
| 2308 | { |
| 2309 | grn_rc rc; |
| 2310 | |
| 2311 | if (hash->io->path[0] != '\0' && |
| 2312 | GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_PRIMARY) { |
| 2313 | grn_obj_flush(ctx, (grn_obj *)hash); |
| 2314 | } |
| 2315 | rc = grn_io_close(ctx, hash->io); |
| 2316 | GRN_FREE(hash->wal_data); |
| 2317 | grn_table_module_fin(ctx, &(hash->tokenizer)); |
| 2318 | grn_table_modules_fin(ctx, &(hash->normalizers)); |
| 2319 | grn_hash_close_token_filters(ctx, hash); |
| 2320 | grn_table_modules_fin(ctx, &(hash->extractors)); |
| 2321 | return rc; |
| 2322 | } |
| 2323 | |
| 2324 | static grn_rc |
| 2325 | grn_tiny_hash_fin(grn_ctx *ctx, grn_hash *hash) |
| 2326 | { |
| 2327 | if (!hash->index) { |
| 2328 | return GRN_INVALID_ARGUMENT; |
| 2329 | } |
| 2330 | |
| 2331 | GRN_FREE(hash->wal_data); |
| 2332 | grn_table_module_fin(ctx, &(hash->tokenizer)); |
| 2333 | grn_table_modules_fin(ctx, &(hash->normalizers)); |
| 2334 | grn_hash_close_token_filters(ctx, hash); |
| 2335 | grn_table_modules_fin(ctx, &(hash->extractors)); |
| 2336 | |
| 2337 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 2338 | uint32_t num_remaining_entries = *hash->n_entries; |
| 2339 | grn_id *hash_ptr; |
| 2340 | for (hash_ptr = hash->index; num_remaining_entries; hash_ptr++) { |
| 2341 | const grn_id id = *hash_ptr; |
| 2342 | if (id && id != GARBAGE) { |
| 2343 | grn_tiny_hash_entry *const entry = |
| 2344 | (grn_tiny_hash_entry *)grn_tiny_array_get(&hash->a, id); |
| 2345 | GRN_ASSERT(entry); |
| 2346 | num_remaining_entries--; |
| 2347 | if (entry && !(entry->flag & HASH_IMMEDIATE)) { |
| 2348 | GRN_CTX_FREE(ctx, entry->key.ptr); |
| 2349 | } |
| 2350 | } |
| 2351 | } |
| 2352 | } |
| 2353 | grn_tiny_array_fin(&hash->a); |
| 2354 | grn_tiny_bitmap_fin(&hash->bitmap); |
| 2355 | GRN_CTX_FREE(ctx, hash->index); |
| 2356 | return GRN_SUCCESS; |
| 2357 | } |
| 2358 | |
| 2359 | static grn_rc |
| 2360 | grn_hash_fin(grn_ctx *ctx, grn_hash *hash) |
| 2361 | { |
| 2362 | if (grn_hash_is_io_hash(hash)) { |
| 2363 | return grn_io_hash_fin(ctx, hash); |
| 2364 | } else { |
| 2365 | GRN_ASSERT(ctx == hash->ctx); |
| 2366 | return grn_tiny_hash_fin(ctx, hash); |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | grn_rc |
| 2371 | grn_hash_close(grn_ctx *ctx, grn_hash *hash) |
| 2372 | { |
| 2373 | grn_rc rc; |
| 2374 | if (!ctx || !hash) { |
| 2375 | return GRN_INVALID_ARGUMENT; |
| 2376 | } |
| 2377 | rc = grn_hash_fin(ctx, hash); |
| 2378 | GRN_FREE(hash); |
| 2379 | return rc; |
| 2380 | } |
| 2381 | |
| 2382 | grn_rc |
| 2383 | grn_hash_remove(grn_ctx *ctx, const char *path) |
| 2384 | { |
| 2385 | if (!ctx || !path) { |
| 2386 | return GRN_INVALID_ARGUMENT; |
| 2387 | } |
| 2388 | grn_rc wal_rc = grn_wal_remove(ctx, path, "[hash]"); |
| 2389 | grn_rc io_rc = grn_io_remove(ctx, path); |
| 2390 | grn_rc rc = wal_rc; |
| 2391 | if (rc == GRN_SUCCESS) { |
| 2392 | rc = io_rc; |
| 2393 | } |
| 2394 | return rc; |
| 2395 | } |
| 2396 | |
| 2397 | grn_rc |
| 2398 | grn_hash_truncate(grn_ctx *ctx, grn_hash *hash) |
| 2399 | { |
| 2400 | grn_rc rc; |
| 2401 | char *path = NULL; |
| 2402 | uint32_t key_size, value_size, flags; |
| 2403 | |
| 2404 | if (!ctx || !hash) { |
| 2405 | return GRN_INVALID_ARGUMENT; |
| 2406 | } |
| 2407 | rc = grn_hash_error_if_truncated(ctx, hash); |
| 2408 | if (rc != GRN_SUCCESS) { |
| 2409 | return rc; |
| 2410 | } |
| 2411 | |
| 2412 | if (grn_hash_is_io_hash(hash)) { |
| 2413 | const char *const io_path = grn_io_path(hash->io); |
| 2414 | if (io_path && *io_path) { |
| 2415 | path = GRN_STRDUP(io_path); |
| 2416 | if (!path) { |
| 2417 | ERR(GRN_NO_MEMORY_AVAILABLE, "cannot duplicate path: <%s>", io_path); |
| 2418 | return GRN_NO_MEMORY_AVAILABLE; |
| 2419 | } |
| 2420 | } |
| 2421 | } |
| 2422 | key_size = hash->key_size; |
| 2423 | value_size = hash->value_size; |
| 2424 | flags = hash->header.common->flags; |
| 2425 | |
| 2426 | if (grn_hash_is_io_hash(hash)) { |
| 2427 | if (path) { |
| 2428 | /* Only an I/O hash with a valid path uses the `truncated` flag. */ |
| 2429 | hash->header.common->truncated = true; |
| 2430 | } |
| 2431 | rc = grn_io_hash_fin(ctx, hash); |
| 2432 | if (!rc) { |
| 2433 | hash->io = NULL; |
| 2434 | if (path) { |
| 2435 | rc = grn_hash_remove(ctx, path); |
| 2436 | } |
| 2437 | } |
| 2438 | } else { |
| 2439 | rc = grn_tiny_hash_fin(ctx, hash); |
| 2440 | } |
| 2441 | if (rc == GRN_SUCCESS) { |
| 2442 | rc = grn_hash_init(ctx, hash, path, key_size, value_size, flags); |
| 2443 | } |
| 2444 | if (path) { |
| 2445 | GRN_FREE(path); |
| 2446 | } |
| 2447 | return rc; |
| 2448 | } |
| 2449 | |
| 2450 | typedef struct { |
| 2451 | bool record_id; |
| 2452 | bool key; |
| 2453 | bool key_size; |
| 2454 | bool key_hash_value; |
| 2455 | bool key_offset; |
| 2456 | bool index_hash_value; |
| 2457 | bool value; |
| 2458 | bool garbage_record_id; |
| 2459 | bool next_garbage_record_id; |
| 2460 | bool n_garbages; |
| 2461 | bool n_entries; |
| 2462 | bool found_garbage; |
| 2463 | bool max_offset; |
| 2464 | bool expected_n_entries; |
| 2465 | } grn_hash_wal_add_entry_used; |
| 2466 | |
| 2467 | static grn_rc |
| 2468 | grn_hash_wal_add_entry_add_entry(grn_ctx *ctx, |
| 2469 | grn_hash_wal_add_entry_data *data, |
| 2470 | grn_hash_wal_add_entry_used *used) |
| 2471 | { |
| 2472 | used->record_id = true; |
| 2473 | used->index_hash_value = true; |
| 2474 | used->n_entries = true; |
| 2475 | return grn_wal_add_entry(ctx, |
| 2476 | (grn_obj *)(data->hash), |
| 2477 | false, |
| 2478 | &(data->wal_id), |
| 2479 | data->tag, |
| 2480 | |
| 2481 | GRN_WAL_KEY_EVENT, |
| 2482 | GRN_WAL_VALUE_EVENT, |
| 2483 | data->event, |
| 2484 | |
| 2485 | GRN_WAL_KEY_RECORD_ID, |
| 2486 | GRN_WAL_VALUE_RECORD_ID, |
| 2487 | data->record_id, |
| 2488 | |
| 2489 | GRN_WAL_KEY_INDEX_HASH_VALUE, |
| 2490 | GRN_WAL_VALUE_UINT32, |
| 2491 | data->index_hash_value, |
| 2492 | |
| 2493 | GRN_WAL_KEY_N_ENTRIES, |
| 2494 | GRN_WAL_VALUE_UINT32, |
| 2495 | data->n_entries, |
| 2496 | |
| 2497 | GRN_WAL_KEY_END); |
| 2498 | } |
| 2499 | |
| 2500 | static grn_rc |
| 2501 | grn_hash_wal_add_entry_reuse_entry(grn_ctx *ctx, |
| 2502 | grn_hash_wal_add_entry_data *data, |
| 2503 | grn_hash_wal_add_entry_used *used) |
| 2504 | { |
| 2505 | used->record_id = true; |
| 2506 | used->key_size = true; |
| 2507 | used->index_hash_value = true; |
| 2508 | used->n_garbages = true; |
| 2509 | used->n_entries = true; |
| 2510 | used->found_garbage = true; |
| 2511 | int found_garbage = data->found_garbage; |
| 2512 | return grn_wal_add_entry(ctx, |
| 2513 | (grn_obj *)(data->hash), |
| 2514 | false, |
| 2515 | &(data->wal_id), |
| 2516 | data->tag, |
| 2517 | |
| 2518 | GRN_WAL_KEY_EVENT, |
| 2519 | GRN_WAL_VALUE_EVENT, |
| 2520 | data->event, |
| 2521 | |
| 2522 | GRN_WAL_KEY_RECORD_ID, |
| 2523 | GRN_WAL_VALUE_RECORD_ID, |
| 2524 | data->record_id, |
| 2525 | |
| 2526 | GRN_WAL_KEY_KEY_SIZE, |
| 2527 | GRN_WAL_VALUE_UINT32, |
| 2528 | data->key_size, |
| 2529 | |
| 2530 | GRN_WAL_KEY_INDEX_HASH_VALUE, |
| 2531 | GRN_WAL_VALUE_UINT32, |
| 2532 | data->index_hash_value, |
| 2533 | |
| 2534 | GRN_WAL_KEY_N_GARBAGES, |
| 2535 | GRN_WAL_VALUE_UINT32, |
| 2536 | data->n_garbages, |
| 2537 | |
| 2538 | GRN_WAL_KEY_N_ENTRIES, |
| 2539 | GRN_WAL_VALUE_UINT32, |
| 2540 | data->n_entries, |
| 2541 | |
| 2542 | GRN_WAL_KEY_FOUND_GARBAGE, |
| 2543 | GRN_WAL_VALUE_BOOLEAN, |
| 2544 | found_garbage, |
| 2545 | |
| 2546 | GRN_WAL_KEY_END); |
| 2547 | } |
| 2548 | |
| 2549 | static grn_rc |
| 2550 | grn_hash_wal_add_entry_reset_entry(grn_ctx *ctx, |
| 2551 | grn_hash_wal_add_entry_data *data, |
| 2552 | grn_hash_wal_add_entry_used *used) |
| 2553 | { |
| 2554 | used->record_id = true; |
| 2555 | used->key_size = true; |
| 2556 | used->next_garbage_record_id = true; |
| 2557 | return grn_wal_add_entry(ctx, |
| 2558 | (grn_obj *)(data->hash), |
| 2559 | false, |
| 2560 | &(data->wal_id), |
| 2561 | data->tag, |
| 2562 | |
| 2563 | GRN_WAL_KEY_EVENT, |
| 2564 | GRN_WAL_VALUE_EVENT, |
| 2565 | data->event, |
| 2566 | |
| 2567 | GRN_WAL_KEY_RECORD_ID, |
| 2568 | GRN_WAL_VALUE_RECORD_ID, |
| 2569 | data->record_id, |
| 2570 | |
| 2571 | GRN_WAL_KEY_KEY_SIZE, |
| 2572 | GRN_WAL_VALUE_UINT32, |
| 2573 | data->key_size, |
| 2574 | |
| 2575 | GRN_WAL_KEY_NEXT_GARBAGE_RECORD_ID, |
| 2576 | GRN_WAL_VALUE_RECORD_ID, |
| 2577 | data->next_garbage_record_id, |
| 2578 | |
| 2579 | GRN_WAL_KEY_END); |
| 2580 | } |
| 2581 | |
| 2582 | static grn_rc |
| 2583 | grn_hash_wal_add_entry_enable_entry(grn_ctx *ctx, |
| 2584 | grn_hash_wal_add_entry_data *data, |
| 2585 | grn_hash_wal_add_entry_used *used) |
| 2586 | { |
| 2587 | used->record_id = true; |
| 2588 | return grn_wal_add_entry(ctx, |
| 2589 | (grn_obj *)(data->hash), |
| 2590 | false, |
| 2591 | &(data->wal_id), |
| 2592 | data->tag, |
| 2593 | |
| 2594 | GRN_WAL_KEY_EVENT, |
| 2595 | GRN_WAL_VALUE_EVENT, |
| 2596 | data->event, |
| 2597 | |
| 2598 | GRN_WAL_KEY_RECORD_ID, |
| 2599 | GRN_WAL_VALUE_RECORD_ID, |
| 2600 | data->record_id, |
| 2601 | |
| 2602 | GRN_WAL_KEY_END); |
| 2603 | } |
| 2604 | |
| 2605 | static grn_rc |
| 2606 | grn_hash_wal_add_entry_set_entry_key(grn_ctx *ctx, |
| 2607 | grn_hash_wal_add_entry_data *data, |
| 2608 | grn_hash_wal_add_entry_used *used) |
| 2609 | { |
| 2610 | used->record_id = true; |
| 2611 | used->key = true; |
| 2612 | used->key_size = true; |
| 2613 | used->key_hash_value = true; |
| 2614 | used->key_offset = true; |
| 2615 | return grn_wal_add_entry(ctx, |
| 2616 | (grn_obj *)(data->hash), |
| 2617 | false, |
| 2618 | &(data->wal_id), |
| 2619 | data->tag, |
| 2620 | |
| 2621 | GRN_WAL_KEY_EVENT, |
| 2622 | GRN_WAL_VALUE_EVENT, |
| 2623 | data->event, |
| 2624 | |
| 2625 | GRN_WAL_KEY_RECORD_ID, |
| 2626 | GRN_WAL_VALUE_RECORD_ID, |
| 2627 | data->record_id, |
| 2628 | |
| 2629 | GRN_WAL_KEY_KEY, |
| 2630 | GRN_WAL_VALUE_BINARY, |
| 2631 | data->key, |
| 2632 | data->key_size, |
| 2633 | |
| 2634 | GRN_WAL_KEY_KEY_HASH_VALUE, |
| 2635 | GRN_WAL_VALUE_UINT32, |
| 2636 | data->key_hash_value, |
| 2637 | |
| 2638 | GRN_WAL_KEY_KEY_OFFSET, |
| 2639 | GRN_WAL_VALUE_UINT64, |
| 2640 | data->key_offset, |
| 2641 | |
| 2642 | GRN_WAL_KEY_END); |
| 2643 | } |
| 2644 | |
| 2645 | static grn_rc |
| 2646 | grn_hash_wal_add_entry_delete_entry(grn_ctx *ctx, |
| 2647 | grn_hash_wal_add_entry_data *data, |
| 2648 | grn_hash_wal_add_entry_used *used) |
| 2649 | { |
| 2650 | used->record_id = true; |
| 2651 | used->key_size = true; |
| 2652 | used->index_hash_value = true; |
| 2653 | used->garbage_record_id = true; |
| 2654 | used->n_garbages = true; |
| 2655 | used->n_entries = true; |
| 2656 | return grn_wal_add_entry(ctx, |
| 2657 | (grn_obj *)(data->hash), |
| 2658 | false, |
| 2659 | &(data->wal_id), |
| 2660 | data->tag, |
| 2661 | |
| 2662 | GRN_WAL_KEY_EVENT, |
| 2663 | GRN_WAL_VALUE_EVENT, |
| 2664 | data->event, |
| 2665 | |
| 2666 | GRN_WAL_KEY_RECORD_ID, |
| 2667 | GRN_WAL_VALUE_RECORD_ID, |
| 2668 | data->record_id, |
| 2669 | |
| 2670 | GRN_WAL_KEY_KEY_SIZE, |
| 2671 | GRN_WAL_VALUE_UINT32, |
| 2672 | data->key_size, |
| 2673 | |
| 2674 | GRN_WAL_KEY_INDEX_HASH_VALUE, |
| 2675 | GRN_WAL_VALUE_UINT32, |
| 2676 | data->index_hash_value, |
| 2677 | |
| 2678 | GRN_WAL_KEY_GARBAGE_RECORD_ID, |
| 2679 | GRN_WAL_VALUE_RECORD_ID, |
| 2680 | data->garbage_record_id, |
| 2681 | |
| 2682 | GRN_WAL_KEY_N_GARBAGES, |
| 2683 | GRN_WAL_VALUE_UINT32, |
| 2684 | data->n_garbages, |
| 2685 | |
| 2686 | GRN_WAL_KEY_N_ENTRIES, |
| 2687 | GRN_WAL_VALUE_UINT32, |
| 2688 | data->n_entries, |
| 2689 | |
| 2690 | GRN_WAL_KEY_END); |
| 2691 | } |
| 2692 | |
| 2693 | static grn_rc |
| 2694 | grn_hash_wal_add_entry_rehash(grn_ctx *ctx, |
| 2695 | grn_hash_wal_add_entry_data *data, |
| 2696 | grn_hash_wal_add_entry_used *used) |
| 2697 | { |
| 2698 | used->n_entries = true; |
| 2699 | used->max_offset = true; |
| 2700 | used->expected_n_entries = true; |
| 2701 | return grn_wal_add_entry(ctx, |
| 2702 | (grn_obj *)(data->hash), |
| 2703 | false, |
| 2704 | &(data->wal_id), |
| 2705 | data->tag, |
| 2706 | |
| 2707 | GRN_WAL_KEY_EVENT, |
| 2708 | GRN_WAL_VALUE_EVENT, |
| 2709 | data->event, |
| 2710 | |
| 2711 | GRN_WAL_KEY_N_ENTRIES, |
| 2712 | GRN_WAL_VALUE_UINT32, |
| 2713 | data->n_entries, |
| 2714 | |
| 2715 | GRN_WAL_KEY_MAX_OFFSET, |
| 2716 | GRN_WAL_VALUE_UINT32, |
| 2717 | data->max_offset, |
| 2718 | |
| 2719 | GRN_WAL_KEY_EXPECTED_N_ENTRIES, |
| 2720 | GRN_WAL_VALUE_UINT32, |
| 2721 | data->expected_n_entries, |
| 2722 | |
| 2723 | GRN_WAL_KEY_END); |
| 2724 | } |
| 2725 | |
| 2726 | static grn_rc |
| 2727 | grn_hash_wal_add_entry_set_value(grn_ctx *ctx, |
| 2728 | grn_hash_wal_add_entry_data *data, |
| 2729 | grn_hash_wal_add_entry_used *used) |
| 2730 | { |
| 2731 | used->record_id = true; |
| 2732 | used->value = true; |
| 2733 | size_t value_size = data->hash->value_size; |
| 2734 | return grn_wal_add_entry(ctx, |
| 2735 | (grn_obj *)(data->hash), |
| 2736 | false, |
| 2737 | &(data->wal_id), |
| 2738 | data->tag, |
| 2739 | |
| 2740 | GRN_WAL_KEY_EVENT, |
| 2741 | GRN_WAL_VALUE_EVENT, |
| 2742 | data->event, |
| 2743 | |
| 2744 | GRN_WAL_KEY_RECORD_ID, |
| 2745 | GRN_WAL_VALUE_RECORD_ID, |
| 2746 | data->record_id, |
| 2747 | |
| 2748 | GRN_WAL_KEY_VALUE, |
| 2749 | GRN_WAL_VALUE_BINARY, |
| 2750 | data->value, |
| 2751 | value_size, |
| 2752 | |
| 2753 | GRN_WAL_KEY_END); |
| 2754 | } |
| 2755 | |
| 2756 | static void |
| 2757 | grn_hash_wal_add_entry_format_details(grn_ctx *ctx, |
| 2758 | grn_hash_wal_add_entry_data *data, |
| 2759 | grn_hash_wal_add_entry_used *used, |
| 2760 | grn_obj *details) |
| 2761 | { |
| 2762 | grn_text_printf(ctx, |
| 2763 | details, |
| 2764 | "event:%u<%s> ", |
| 2765 | data->event, |
| 2766 | grn_wal_event_to_string(data->event)); |
| 2767 | if (used->record_id) { |
| 2768 | grn_text_printf(ctx, details, "record-id:%u ", data->record_id); |
| 2769 | } |
| 2770 | if (used->key_size) { |
| 2771 | grn_text_printf(ctx, |
| 2772 | details, |
| 2773 | "key-size:%" GRN_FMT_SIZE " ", |
| 2774 | data->key_size); |
| 2775 | } |
| 2776 | if (used->key_hash_value) { |
| 2777 | grn_text_printf(ctx, details, "key-hash-value:%u ", data->key_hash_value); |
| 2778 | } |
| 2779 | if (used->key_offset) { |
| 2780 | grn_text_printf(ctx, |
| 2781 | details, |
| 2782 | "key-offset:%" GRN_FMT_INT64U " ", |
| 2783 | data->key_offset); |
| 2784 | } |
| 2785 | if (used->index_hash_value) { |
| 2786 | grn_text_printf(ctx, |
| 2787 | details, |
| 2788 | "index-hash-value:%u ", |
| 2789 | data->index_hash_value); |
| 2790 | } |
| 2791 | if (used->value) { |
| 2792 | grn_text_printf(ctx, details, "value-size:%u ", data->hash->value_size); |
| 2793 | } |
| 2794 | if (used->garbage_record_id) { |
| 2795 | grn_text_printf(ctx, |
| 2796 | details, |
| 2797 | "garbage-record-id:%u ", |
| 2798 | data->garbage_record_id); |
| 2799 | } |
| 2800 | if (used->next_garbage_record_id) { |
| 2801 | grn_text_printf(ctx, |
| 2802 | details, |
| 2803 | "next-garbage-record-id:%u ", |
| 2804 | data->next_garbage_record_id); |
| 2805 | } |
| 2806 | if (used->n_garbages) { |
| 2807 | grn_text_printf(ctx, details, "n-garbages:%u ", data->n_garbages); |
| 2808 | } |
| 2809 | if (used->n_entries) { |
| 2810 | grn_text_printf(ctx, details, "n-entries:%u ", data->n_entries); |
| 2811 | } |
| 2812 | if (used->max_offset) { |
| 2813 | grn_text_printf(ctx, details, "max-offset:%u ", data->max_offset); |
| 2814 | } |
| 2815 | if (used->expected_n_entries) { |
| 2816 | grn_text_printf(ctx, |
| 2817 | details, |
| 2818 | "expected-n-entries:%u ", |
| 2819 | data->expected_n_entries); |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | static inline bool |
| 2824 | grn_hash_wal_need(grn_ctx *ctx, grn_hash *hash) |
| 2825 | { |
| 2826 | if (GRN_CTX_GET_WAL_ROLE(ctx) == GRN_WAL_ROLE_NONE) { |
| 2827 | return false; |
| 2828 | } |
| 2829 | if (!hash->io) { |
| 2830 | return false; |
| 2831 | } |
| 2832 | if (hash->io->path[0] == '\0') { |
| 2833 | return false; |
| 2834 | } |
| 2835 | return true; |
| 2836 | } |
| 2837 | |
| 2838 | static inline grn_rc |
| 2839 | grn_hash_wal_add_entry(grn_ctx *ctx, grn_hash_wal_add_entry_data *data) |
| 2840 | { |
| 2841 | if (!grn_hash_wal_need(ctx, data->hash)) { |
| 2842 | return GRN_SUCCESS; |
| 2843 | } |
| 2844 | |
| 2845 | grn_rc rc = GRN_SUCCESS; |
| 2846 | const char *usage = ""; |
| 2847 | grn_hash_wal_add_entry_used used = {0}; |
| 2848 | |
| 2849 | switch (data->event) { |
| 2850 | case GRN_WAL_EVENT_ADD_ENTRY: |
| 2851 | usage = "adding entry"; |
| 2852 | rc = grn_hash_wal_add_entry_add_entry(ctx, data, &used); |
| 2853 | break; |
| 2854 | case GRN_WAL_EVENT_REUSE_ENTRY: |
| 2855 | usage = "reusing entry"; |
| 2856 | rc = grn_hash_wal_add_entry_reuse_entry(ctx, data, &used); |
| 2857 | break; |
| 2858 | case GRN_WAL_EVENT_RESET_ENTRY: |
| 2859 | usage = "resetting entry"; |
| 2860 | rc = grn_hash_wal_add_entry_reset_entry(ctx, data, &used); |
| 2861 | break; |
| 2862 | case GRN_WAL_EVENT_ENABLE_ENTRY: |
| 2863 | usage = "enabling entry"; |
| 2864 | rc = grn_hash_wal_add_entry_enable_entry(ctx, data, &used); |
| 2865 | break; |
| 2866 | case GRN_WAL_EVENT_SET_ENTRY_KEY: |
| 2867 | usage = "setting entry key"; |
| 2868 | rc = grn_hash_wal_add_entry_set_entry_key(ctx, data, &used); |
| 2869 | break; |
| 2870 | case GRN_WAL_EVENT_DELETE_ENTRY: |
| 2871 | usage = "deleting entry"; |
| 2872 | rc = grn_hash_wal_add_entry_delete_entry(ctx, data, &used); |
| 2873 | break; |
| 2874 | case GRN_WAL_EVENT_REHASH: |
| 2875 | usage = "rehash"; |
| 2876 | rc = grn_hash_wal_add_entry_rehash(ctx, data, &used); |
| 2877 | break; |
| 2878 | case GRN_WAL_EVENT_SET_VALUE: |
| 2879 | usage = "setting value"; |
| 2880 | rc = grn_hash_wal_add_entry_set_value(ctx, data, &used); |
| 2881 | break; |
| 2882 | default: |
| 2883 | usage = "not implemented event"; |
| 2884 | rc = GRN_FUNCTION_NOT_IMPLEMENTED; |
| 2885 | break; |
| 2886 | } |
| 2887 | |
| 2888 | if (rc != GRN_SUCCESS) { |
| 2889 | grn_obj details; |
| 2890 | GRN_TEXT_INIT(&details, 0); |
| 2891 | grn_hash_wal_add_entry_format_details(ctx, data, &used, &details); |
| 2892 | grn_obj_set_error(ctx, |
| 2893 | (grn_obj *)(data->hash), |
| 2894 | rc, |
| 2895 | data->record_id, |
| 2896 | data->tag, |
| 2897 | "failed to add WAL entry for %s: %.*s", |
| 2898 | usage, |
| 2899 | (int)GRN_TEXT_LEN(&details), |
| 2900 | GRN_TEXT_VALUE(&details)); |
| 2901 | GRN_OBJ_FIN(ctx, &details); |
| 2902 | } |
| 2903 | |
| 2904 | return rc; |
| 2905 | } |
| 2906 | |
| 2907 | static inline void |
| 2908 | grn_hash_wal_set_wal_id(grn_ctx *ctx, grn_hash *hash, uint64_t wal_id) |
| 2909 | { |
| 2910 | if (wal_id == 0) { |
| 2911 | return; |
| 2912 | } |
| 2913 | if (!grn_hash_wal_need(ctx, hash)) { |
| 2914 | return; |
| 2915 | } |
| 2916 | hash->header.common->wal_id = wal_id; |
| 2917 | } |
| 2918 | |
| 2919 | static inline uint32_t |
| 2920 | grn_hash_calculate_hash_value(const void *ptr, uint32_t size) |
| 2921 | { |
| 2922 | uint32_t i; |
| 2923 | uint32_t hash_value = 0; |
| 2924 | for (i = 0; i < size; i++) { |
| 2925 | hash_value = (hash_value * 1021) + ((const uint8_t *)ptr)[i]; |
| 2926 | } |
| 2927 | return hash_value; |
| 2928 | } |
| 2929 | |
| 2930 | static inline uint32_t |
| 2931 | grn_hash_calculate_step(uint32_t hash_value) |
| 2932 | { |
| 2933 | return (hash_value >> 2) | 0x1010101; |
| 2934 | } |
| 2935 | |
| 2936 | static grn_rc |
| 2937 | grn_hash_rehash(grn_ctx *ctx, grn_hash *hash, uint32_t expected_n_entries) |
| 2938 | { |
| 2939 | grn_id *new_index = NULL; |
| 2940 | uint32_t new_index_size = INITIAL_INDEX_SIZE; |
| 2941 | grn_id *src_ptr = NULL, *dest_ptr = NULL; |
| 2942 | uint32_t src_offset = 0, dest_offset = 0; |
| 2943 | const uint32_t n_entries = *hash->n_entries; |
| 2944 | const uint32_t max_offset = *hash->max_offset; |
| 2945 | |
| 2946 | if (expected_n_entries == 0) { |
| 2947 | expected_n_entries = n_entries * 2; |
| 2948 | if (expected_n_entries >= grn_hash_max_index_size) { |
| 2949 | expected_n_entries = grn_hash_max_index_size - 1; |
| 2950 | } |
| 2951 | if (grn_id_maybe_table(ctx, hash->obj.header.domain)) { |
| 2952 | grn_obj *domain = grn_ctx_at(ctx, hash->obj.header.domain); |
| 2953 | if (grn_obj_is_table(ctx, domain)) { |
| 2954 | const unsigned int n_source_records = grn_table_size(ctx, domain); |
| 2955 | if (n_source_records + 1 > expected_n_entries) { |
| 2956 | expected_n_entries = n_source_records + 1; |
| 2957 | } |
| 2958 | } |
| 2959 | grn_obj_unref(ctx, domain); |
| 2960 | } |
| 2961 | } |
| 2962 | if (expected_n_entries >= grn_hash_max_index_size) { |
| 2963 | return GRN_NO_MEMORY_AVAILABLE; |
| 2964 | } |
| 2965 | while (new_index_size <= expected_n_entries) { |
| 2966 | new_index_size *= 2; |
| 2967 | } |
| 2968 | |
| 2969 | { |
| 2970 | grn_log_level log_level; |
| 2971 | if (grn_hash_is_io_hash(hash)) { |
| 2972 | log_level = GRN_LOG_INFO; |
| 2973 | } else { |
| 2974 | log_level = GRN_LOG_DEBUG; |
| 2975 | } |
| 2976 | if (grn_logger_pass(ctx, log_level)) { |
| 2977 | GRN_DEFINE_NAME(hash); |
| 2978 | if (grn_hash_is_io_hash(hash)) { |
| 2979 | GRN_LOG(ctx, |
| 2980 | log_level, |
| 2981 | "[hash][rehash][%.*s] <%u> -> <%u>: " |
| 2982 | "max-offset:<%u> " |
| 2983 | "n-garbages:<%u> " |
| 2984 | "idx-offset:<%u>", |
| 2985 | name_size, |
| 2986 | name, |
| 2987 | n_entries, |
| 2988 | new_index_size, |
| 2989 | max_offset, |
| 2990 | *hash->n_garbages, |
| 2991 | hash->header.common->idx_offset); |
| 2992 | } else { |
| 2993 | GRN_LOG(ctx, |
| 2994 | log_level, |
| 2995 | "[hash][rehash][%.*s] <%u> -> <%u>: " |
| 2996 | "max-offset:<%u> " |
| 2997 | "n-garbages:<%u>", |
| 2998 | name_size, |
| 2999 | name, |
| 3000 | n_entries, |
| 3001 | new_index_size, |
| 3002 | max_offset, |
| 3003 | *hash->n_garbages); |
| 3004 | } |
| 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | /* |
| 3009 | * Rehash: |
| 3010 | * 1. Prepare new large space |
| 3011 | * 2. Copy existing data to new space |
| 3012 | * |
| 3013 | * Here is an IO hash case. A non IO hash allocates a new space |
| 3014 | * instead of reusing two sub-spaces in a large space. An IO hash |
| 3015 | * uses idx_offset to determine whether the left space (2^(0..29) |
| 3016 | * space) is used or the right space (2^(29..30) space) is used. |
| 3017 | * |
| 3018 | * GRN_HASH_INDEX_SEGMENT space: |
| 3019 | * |
| 3020 | * 0 2^29 2^30 |
| 3021 | * |<------------|------------>| |
| 3022 | * |
| 3023 | * Start: |
| 3024 | * |
| 3025 | * 0 2^29 2^30 |
| 3026 | * |<------------|------------>| |
| 3027 | * |<-used-> |<-not used-> | |
| 3028 | * |
| 3029 | * Rehash (prepare): |
| 3030 | * |
| 3031 | * 0 2^29 2^30 |
| 3032 | * |<------------|------------>| |
| 3033 | * |<-used->| |<-0 clear-> | |
| 3034 | * |
| 3035 | * Rehash (coping): |
| 3036 | * |
| 3037 | * 0 2^29 2^30 |
| 3038 | * |<------------|------------>| |
| 3039 | * |<-used-> |<-new used-> | |
| 3040 | * +------copy--^ |
| 3041 | * |
| 3042 | * Rehash (copied): |
| 3043 | * |
| 3044 | * 0 2^29 2^30 |
| 3045 | * |<------------|------------>| |
| 3046 | * |<-not used-> |<-new used-> | |
| 3047 | * |
| 3048 | * Rehash again (prepare): |
| 3049 | * |
| 3050 | * 0 2^29 2^30 |
| 3051 | * |<------------|------------>| |
| 3052 | * |<-0 clear-> |<-used-> | |
| 3053 | * |
| 3054 | * ... |
| 3055 | */ |
| 3056 | if (grn_hash_is_io_hash(hash)) { |
| 3057 | /* 1. Prepare new large space: Clear an unused space. */ |
| 3058 | uint32_t i; |
| 3059 | src_offset = hash->header.common->idx_offset; |
| 3060 | dest_offset = grn_hash_max_index_size - src_offset; |
| 3061 | for (i = 0; i < new_index_size; i += (IDX_MASK_IN_A_SEGMENT + 1)) { |
| 3062 | /* |
| 3063 | * The following grn_io_hash_idx_at() allocates memory for a new segment |
| 3064 | * and returns a pointer to the new segment. It's actually bad manners |
| 3065 | * but faster than calling grn_io_hash_idx_at() for each element. |
| 3066 | */ |
| 3067 | dest_ptr = grn_io_hash_idx_at(ctx, hash, i + dest_offset); |
| 3068 | if (!dest_ptr) { |
| 3069 | return GRN_NO_MEMORY_AVAILABLE; |
| 3070 | } |
| 3071 | memset(dest_ptr, 0, GRN_HASH_SEGMENT_SIZE); |
| 3072 | } |
| 3073 | } else { |
| 3074 | /* 1. Prepare new large space: Allocate a new space. */ |
| 3075 | GRN_ASSERT(ctx == hash->ctx); |
| 3076 | new_index = GRN_CTX_ALLOC(ctx, new_index_size * sizeof(grn_id)); |
| 3077 | if (!new_index) { |
| 3078 | return GRN_NO_MEMORY_AVAILABLE; |
| 3079 | } |
| 3080 | src_ptr = hash->index; |
| 3081 | } |
| 3082 | |
| 3083 | { |
| 3084 | /* 2. Copy existing data to new space */ |
| 3085 | uint32_t src_pos, count; |
| 3086 | const uint32_t new_max_offset = new_index_size - 1; |
| 3087 | for (count = 0, src_pos = 0; count < n_entries && src_pos <= max_offset; |
| 3088 | src_pos++, src_ptr++) { |
| 3089 | uint32_t i, step; |
| 3090 | grn_id entry_id; |
| 3091 | grn_hash_entry *entry; |
| 3092 | if (grn_hash_is_io_hash(hash) && !(src_pos & IDX_MASK_IN_A_SEGMENT)) { |
| 3093 | src_ptr = grn_io_hash_idx_at(ctx, hash, src_pos + src_offset); |
| 3094 | if (!src_ptr) { |
| 3095 | return GRN_NO_MEMORY_AVAILABLE; |
| 3096 | } |
| 3097 | } |
| 3098 | entry_id = *src_ptr; |
| 3099 | if (!entry_id || (entry_id == GARBAGE)) { |
| 3100 | continue; |
| 3101 | } |
| 3102 | entry = grn_hash_entry_at(ctx, hash, entry_id, GRN_TABLE_ADD); |
| 3103 | if (!entry) { |
| 3104 | return GRN_NO_MEMORY_AVAILABLE; |
| 3105 | } |
| 3106 | step = grn_hash_calculate_step(entry->hash_value); |
| 3107 | for (i = entry->hash_value;; i += step) { |
| 3108 | i &= new_max_offset; |
| 3109 | if (grn_hash_is_io_hash(hash)) { |
| 3110 | dest_ptr = grn_io_hash_idx_at(ctx, hash, i + dest_offset); |
| 3111 | if (!dest_ptr) { |
| 3112 | return GRN_NO_MEMORY_AVAILABLE; |
| 3113 | } |
| 3114 | } else { |
| 3115 | dest_ptr = new_index + i; |
| 3116 | } |
| 3117 | if (!*dest_ptr) { |
| 3118 | break; |
| 3119 | } |
| 3120 | } |
| 3121 | *dest_ptr = entry_id; |
| 3122 | count++; |
| 3123 | } |
| 3124 | *hash->max_offset = new_max_offset; |
| 3125 | *hash->n_garbages = 0; |
| 3126 | /* |
| 3127 | * We must not reset garbages here: |
| 3128 | * |
| 3129 | * * hash->header.large->garbages |
| 3130 | * * hash->header.normal->garbages |
| 3131 | * * hash->garbages |
| 3132 | * |
| 3133 | * because they are for GRN_HASH_ENTRY_SEGMENT not |
| 3134 | * GRN_HASH_ENTRY_INDEX. |
| 3135 | */ |
| 3136 | } |
| 3137 | |
| 3138 | if (grn_hash_is_io_hash(hash)) { |
| 3139 | hash->header.common->idx_offset = dest_offset; |
| 3140 | } else { |
| 3141 | grn_id *const old_index = hash->index; |
| 3142 | hash->index = new_index; |
| 3143 | GRN_CTX_FREE(ctx, old_index); |
| 3144 | } |
| 3145 | |
| 3146 | return GRN_SUCCESS; |
| 3147 | } |
| 3148 | |
| 3149 | grn_rc |
| 3150 | grn_hash_lock(grn_ctx *ctx, grn_hash *hash, int timeout) |
| 3151 | { |
| 3152 | static int _ncalls = 0, _ncolls = 0; |
| 3153 | uint32_t count; |
| 3154 | _ncalls++; |
| 3155 | for (count = 0;; count++) { |
| 3156 | uint32_t lock; |
| 3157 | GRN_ATOMIC_ADD_EX(hash->lock, 1, lock); |
| 3158 | if (lock) { |
| 3159 | GRN_ATOMIC_ADD_EX(hash->lock, -1, lock); |
| 3160 | if (!timeout || (timeout > 0 && (uint32_t)timeout == count)) { |
| 3161 | break; |
| 3162 | } |
| 3163 | if (!(++_ncolls % 1000000) && (_ncolls > _ncalls)) { |
| 3164 | if (_ncolls < 0 || _ncalls < 0) { |
| 3165 | _ncolls = 0; |
| 3166 | _ncalls = 0; |
| 3167 | } else { |
| 3168 | GRN_LOG(ctx, |
| 3169 | GRN_LOG_NOTICE, |
| 3170 | "hash(%p) collisions(%d/%d)", |
| 3171 | hash, |
| 3172 | _ncolls, |
| 3173 | _ncalls); |
| 3174 | } |
| 3175 | } |
| 3176 | grn_nanosleep(GRN_LOCK_WAIT_TIME_NANOSECOND); |
| 3177 | continue; |
| 3178 | } |
| 3179 | return GRN_SUCCESS; |
| 3180 | } |
| 3181 | ERR(GRN_RESOURCE_DEADLOCK_AVOIDED, "grn_hash_lock"); |
| 3182 | return ctx->rc; |
| 3183 | } |
| 3184 | |
| 3185 | grn_rc |
| 3186 | grn_hash_unlock(grn_ctx *ctx, grn_hash *hash) |
| 3187 | { |
| 3188 | uint32_t lock; |
| 3189 | GRN_ATOMIC_ADD_EX(hash->lock, -1, lock); |
| 3190 | return GRN_SUCCESS; |
| 3191 | } |
| 3192 | |
| 3193 | grn_rc |
| 3194 | grn_hash_clear_lock(grn_ctx *ctx, grn_hash *hash) |
| 3195 | { |
| 3196 | *hash->lock = 0; |
| 3197 | return GRN_SUCCESS; |
| 3198 | } |
| 3199 | |
| 3200 | uint32_t |
| 3201 | grn_hash_size(grn_ctx *ctx, grn_hash *hash) |
| 3202 | { |
| 3203 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 3204 | return 0; |
| 3205 | } |
| 3206 | return *hash->n_entries; |
| 3207 | } |
| 3208 | |
| 3209 | static inline grn_hash_entry * |
| 3210 | grn_io_hash_reuse_entry(grn_ctx *ctx, |
| 3211 | grn_hash *hash, |
| 3212 | grn_id id, |
| 3213 | uint32_t key_size, |
| 3214 | grn_id *index, |
| 3215 | bool found_garbage, |
| 3216 | const char *tag) |
| 3217 | { |
| 3218 | grn_hash_entry *entry = grn_io_hash_entry_at(ctx, hash, id, GRN_TABLE_ADD); |
| 3219 | if (!entry) { |
| 3220 | grn_obj_set_error(ctx, |
| 3221 | (grn_obj *)hash, |
| 3222 | GRN_INVALID_ARGUMENT, |
| 3223 | id, |
| 3224 | tag, |
| 3225 | "failed to reuse entry"); |
| 3226 | return NULL; |
| 3227 | } |
| 3228 | *index = id; |
| 3229 | if (found_garbage) { |
| 3230 | (*(hash->n_garbages))--; |
| 3231 | } |
| 3232 | (*(hash->n_entries))++; |
| 3233 | return entry; |
| 3234 | } |
| 3235 | |
| 3236 | static inline void |
| 3237 | grn_io_hash_reset_entry(grn_ctx *ctx, |
| 3238 | grn_hash *hash, |
| 3239 | grn_hash_entry *entry, |
| 3240 | grn_id next_garbage_id, |
| 3241 | uint32_t key_size, |
| 3242 | const char *tag) |
| 3243 | { |
| 3244 | grn_hash_header_common *const header = hash->header.common; |
| 3245 | grn_id *garbages; |
| 3246 | if (GRN_HASH_IS_LARGE_KEY(hash)) { |
| 3247 | garbages = hash->header.large->garbages; |
| 3248 | } else { |
| 3249 | garbages = hash->header.normal->garbages; |
| 3250 | } |
| 3251 | uint32_t garbage_index = key_size - 1; |
| 3252 | garbages[garbage_index] = next_garbage_id; |
| 3253 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 3254 | /* keep entry->io_entry's hash_value, flag, key_size and key. */ |
| 3255 | if (grn_hash_is_large_total_key_size(ctx, hash)) { |
| 3256 | memset(entry->io_entry_large.value, 0, header->value_size); |
| 3257 | } else { |
| 3258 | memset(entry->io_entry_normal.value, 0, header->value_size); |
| 3259 | } |
| 3260 | } else { |
| 3261 | memset(entry, 0, header->entry_size); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | static inline grn_hash_entry * |
| 3266 | grn_io_hash_add_entry( |
| 3267 | grn_ctx *ctx, grn_hash *hash, grn_id id, grn_id *index, const char *tag) |
| 3268 | { |
| 3269 | grn_hash_entry *entry = grn_io_hash_entry_at(ctx, hash, id, GRN_TABLE_ADD); |
| 3270 | if (!entry) { |
| 3271 | grn_obj_set_error(ctx, |
| 3272 | (grn_obj *)hash, |
| 3273 | GRN_INVALID_ARGUMENT, |
| 3274 | id, |
| 3275 | tag, |
| 3276 | "failed to add entry"); |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | hash->header.common->curr_rec = id; |
| 3280 | *index = id; |
| 3281 | (*(hash->n_entries))++; |
| 3282 | return entry; |
| 3283 | } |
| 3284 | |
| 3285 | static inline bool |
| 3286 | grn_io_hash_enable_entry(grn_ctx *ctx, |
| 3287 | grn_hash *hash, |
| 3288 | grn_id id, |
| 3289 | const char *tag) |
| 3290 | { |
| 3291 | if (!grn_io_array_bit_on(ctx, hash->io, GRN_HASH_BITMAP_SEGMENT, id)) { |
| 3292 | grn_obj_set_error(ctx, |
| 3293 | (grn_obj *)hash, |
| 3294 | GRN_INVALID_ARGUMENT, |
| 3295 | id, |
| 3296 | tag, |
| 3297 | "failed to set a bit for the entry"); |
| 3298 | return false; |
| 3299 | } |
| 3300 | return true; |
| 3301 | } |
| 3302 | |
| 3303 | static inline grn_id |
| 3304 | grn_io_hash_add(grn_ctx *ctx, |
| 3305 | grn_hash *hash, |
| 3306 | uint32_t hash_value, |
| 3307 | const void *key, |
| 3308 | uint32_t key_size, |
| 3309 | void **value, |
| 3310 | grn_id *index, |
| 3311 | uint32_t index_hash_value, |
| 3312 | bool found_garbage) |
| 3313 | { |
| 3314 | grn_hash_header_common *const header = hash->header.common; |
| 3315 | grn_hash_wal_add_entry_data *wal_data = hash->wal_data; |
| 3316 | wal_data->tag = "[hash][io][add]"; |
| 3317 | wal_data->key = key; |
| 3318 | wal_data->key_size = key_size; |
| 3319 | const bool need_wal = grn_hash_wal_need(ctx, hash); |
| 3320 | if (need_wal) { |
| 3321 | wal_data->key_hash_value = hash_value; |
| 3322 | wal_data->index_hash_value = index_hash_value; |
| 3323 | } |
| 3324 | |
| 3325 | grn_id *garbages; |
| 3326 | if (GRN_HASH_IS_LARGE_KEY(hash)) { |
| 3327 | garbages = hash->header.large->garbages; |
| 3328 | } else { |
| 3329 | garbages = hash->header.normal->garbages; |
| 3330 | } |
| 3331 | |
| 3332 | grn_hash_entry *entry; |
| 3333 | uint32_t garbage_index = key_size - 1; |
| 3334 | grn_id garbage_id = garbages[garbage_index]; |
| 3335 | if (garbage_id != GRN_ID_NIL) { |
| 3336 | wal_data->record_id = garbage_id; |
| 3337 | if (need_wal) { |
| 3338 | wal_data->event = GRN_WAL_EVENT_REUSE_ENTRY; |
| 3339 | wal_data->n_garbages = *(hash->n_garbages); |
| 3340 | wal_data->n_entries = *(hash->n_entries); |
| 3341 | wal_data->found_garbage = found_garbage; |
| 3342 | if (grn_hash_wal_add_entry(ctx, wal_data) != GRN_SUCCESS) { |
| 3343 | return GRN_ID_NIL; |
| 3344 | } |
| 3345 | } |
| 3346 | entry = grn_io_hash_reuse_entry(ctx, |
| 3347 | hash, |
| 3348 | wal_data->record_id, |
| 3349 | (uint32_t)(wal_data->key_size), |
| 3350 | index, |
| 3351 | found_garbage, |
| 3352 | wal_data->tag); |
| 3353 | if (!entry) { |
| 3354 | return GRN_ID_NIL; |
| 3355 | } |
| 3356 | wal_data->next_garbage_record_id = *((grn_id *)entry); |
| 3357 | if (need_wal) { |
| 3358 | wal_data->event = GRN_WAL_EVENT_RESET_ENTRY; |
| 3359 | if (grn_hash_wal_add_entry(ctx, wal_data) != GRN_SUCCESS) { |
| 3360 | return GRN_ID_NIL; |
| 3361 | } |
| 3362 | } |
| 3363 | grn_io_hash_reset_entry(ctx, |
| 3364 | hash, |
| 3365 | entry, |
| 3366 | wal_data->next_garbage_record_id, |
| 3367 | (uint32_t)(wal_data->key_size), |
| 3368 | wal_data->tag); |
| 3369 | } else { |
| 3370 | wal_data->record_id = header->curr_rec + 1; |
| 3371 | if (need_wal) { |
| 3372 | wal_data->event = GRN_WAL_EVENT_ADD_ENTRY; |
| 3373 | wal_data->n_entries = *(hash->n_entries); |
| 3374 | if (grn_hash_wal_add_entry(ctx, wal_data) != GRN_SUCCESS) { |
| 3375 | return GRN_ID_NIL; |
| 3376 | } |
| 3377 | } |
| 3378 | entry = grn_io_hash_add_entry(ctx, |
| 3379 | hash, |
| 3380 | wal_data->record_id, |
| 3381 | index, |
| 3382 | wal_data->tag); |
| 3383 | if (!entry) { |
| 3384 | return GRN_ID_NIL; |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | if (need_wal) { |
| 3389 | wal_data->event = GRN_WAL_EVENT_ENABLE_ENTRY; |
| 3390 | if (grn_hash_wal_add_entry(ctx, wal_data) != GRN_SUCCESS) { |
| 3391 | return GRN_ID_NIL; |
| 3392 | } |
| 3393 | wal_data->event = GRN_WAL_EVENT_SET_ENTRY_KEY; |
| 3394 | if (grn_hash_is_large_total_key_size(ctx, hash)) { |
| 3395 | wal_data->key_offset = header->curr_key_large; |
| 3396 | } else { |
| 3397 | wal_data->key_offset = header->curr_key_normal; |
| 3398 | } |
| 3399 | if (grn_hash_wal_add_entry(ctx, wal_data) != GRN_SUCCESS) { |
| 3400 | return GRN_ID_NIL; |
| 3401 | } |
| 3402 | } |
| 3403 | if (!grn_io_hash_enable_entry(ctx, |
| 3404 | hash, |
| 3405 | wal_data->record_id, |
| 3406 | wal_data->tag)) { |
| 3407 | return GRN_ID_NIL; |
| 3408 | } |
| 3409 | grn_rc rc = |
| 3410 | grn_hash_entry_put_key(ctx, hash, entry, hash_value, key, key_size); |
| 3411 | if (rc != GRN_SUCCESS) { |
| 3412 | grn_obj inspected; |
| 3413 | GRN_TEXT_INIT(&inspected, 0); |
| 3414 | grn_inspect_key(ctx, &inspected, (grn_obj *)hash, key, key_size); |
| 3415 | grn_obj_set_error(ctx, |
| 3416 | (grn_obj *)hash, |
| 3417 | rc, |
| 3418 | wal_data->record_id, |
| 3419 | wal_data->tag, |
| 3420 | "failed to put key: <%.*s>", |
| 3421 | (int)GRN_TEXT_LEN(&inspected), |
| 3422 | GRN_TEXT_VALUE(&inspected)); |
| 3423 | GRN_OBJ_FIN(ctx, &inspected); |
| 3424 | goto exit; |
| 3425 | } |
| 3426 | |
| 3427 | if (value) { |
| 3428 | *value = grn_hash_entry_get_value(ctx, hash, entry); |
| 3429 | } |
| 3430 | |
| 3431 | if (need_wal) { |
| 3432 | grn_hash_wal_set_wal_id(ctx, hash, wal_data->wal_id); |
| 3433 | } |
| 3434 | |
| 3435 | exit: |
| 3436 | if (ctx->rc != GRN_SUCCESS && entry) { |
| 3437 | rc = grn_hash_delete_by_id(ctx, hash, wal_data->record_id, NULL); |
| 3438 | if (rc != GRN_SUCCESS) { |
| 3439 | grn_obj_set_error(ctx, |
| 3440 | (grn_obj *)hash, |
| 3441 | rc, |
| 3442 | wal_data->record_id, |
| 3443 | wal_data->tag, |
| 3444 | "failed to delete by id"); |
| 3445 | } |
| 3446 | return GRN_ID_NIL; |
| 3447 | } |
| 3448 | |
| 3449 | return wal_data->record_id; |
| 3450 | } |
| 3451 | |
| 3452 | static inline grn_id |
| 3453 | grn_tiny_hash_add(grn_ctx *ctx, |
| 3454 | grn_hash *hash, |
| 3455 | uint32_t hash_value, |
| 3456 | const void *key, |
| 3457 | unsigned int key_size, |
| 3458 | void **value, |
| 3459 | grn_id *index, |
| 3460 | bool found_garbage) |
| 3461 | { |
| 3462 | grn_id entry_id; |
| 3463 | grn_hash_entry *entry; |
| 3464 | if (hash->garbages) { |
| 3465 | entry_id = hash->garbages; |
| 3466 | entry = (grn_hash_entry *)grn_tiny_array_get(&hash->a, entry_id); |
| 3467 | hash->garbages = *(grn_id *)entry; |
| 3468 | memset(entry, 0, hash->entry_size); |
| 3469 | } else { |
| 3470 | entry_id = hash->a.max + 1; |
| 3471 | entry = (grn_hash_entry *)grn_tiny_array_put(&hash->a, entry_id); |
| 3472 | if (!entry) { |
| 3473 | return GRN_ID_NIL; |
| 3474 | } |
| 3475 | } |
| 3476 | if (found_garbage) { |
| 3477 | (*hash->n_garbages)--; |
| 3478 | } |
| 3479 | *index = entry_id; |
| 3480 | (*hash->n_entries)++; |
| 3481 | |
| 3482 | if (!grn_tiny_bitmap_put_and_set(&hash->bitmap, entry_id, 1)) { |
| 3483 | /* TODO: error handling. */ |
| 3484 | } |
| 3485 | |
| 3486 | if (grn_hash_entry_put_key(ctx, hash, entry, hash_value, key, key_size)) { |
| 3487 | /* TODO: error handling. */ |
| 3488 | } |
| 3489 | |
| 3490 | if (value) { |
| 3491 | *value = grn_hash_entry_get_value(ctx, hash, entry); |
| 3492 | } |
| 3493 | return entry_id; |
| 3494 | } |
| 3495 | |
| 3496 | static inline grn_rc |
| 3497 | grn_hash_ensure_rehash(grn_ctx *ctx, |
| 3498 | grn_hash *hash, |
| 3499 | uint32_t threshold, |
| 3500 | const char *tag) |
| 3501 | { |
| 3502 | if (threshold <= *hash->max_offset) { |
| 3503 | return GRN_SUCCESS; |
| 3504 | } |
| 3505 | /* We can't rehash more. */ |
| 3506 | if (*hash->max_offset == (grn_hash_max_index_size - 1)) { |
| 3507 | return GRN_SUCCESS; |
| 3508 | } |
| 3509 | |
| 3510 | grn_hash_wal_add_entry_data *wal_data = hash->wal_data; |
| 3511 | const bool need_wal = grn_hash_wal_need(ctx, hash); |
| 3512 | wal_data->tag = tag; |
| 3513 | wal_data->n_entries = *(hash->n_entries); |
| 3514 | wal_data->max_offset = *(hash->max_offset); |
| 3515 | wal_data->expected_n_entries = 0; |
| 3516 | if (need_wal) { |
| 3517 | wal_data->event = GRN_WAL_EVENT_REHASH; |
| 3518 | if (grn_hash_wal_add_entry(ctx, wal_data) != GRN_SUCCESS) { |
| 3519 | return ctx->rc; |
| 3520 | } |
| 3521 | } |
| 3522 | grn_rc rc = grn_hash_rehash(ctx, hash, wal_data->expected_n_entries); |
| 3523 | if (rc != GRN_SUCCESS) { |
| 3524 | grn_obj_set_error(ctx, |
| 3525 | (grn_obj *)hash, |
| 3526 | rc, |
| 3527 | GRN_ID_NIL, |
| 3528 | tag, |
| 3529 | "failed to rehash: " |
| 3530 | "n-entries:%u " |
| 3531 | "max-offset:%u", |
| 3532 | wal_data->n_entries, |
| 3533 | wal_data->max_offset); |
| 3534 | return rc; |
| 3535 | } |
| 3536 | if (need_wal) { |
| 3537 | grn_hash_wal_set_wal_id(ctx, hash, wal_data->wal_id); |
| 3538 | } |
| 3539 | |
| 3540 | return GRN_SUCCESS; |
| 3541 | } |
| 3542 | |
| 3543 | static inline grn_id |
| 3544 | grn_hash_add_entry(grn_ctx *ctx, |
| 3545 | grn_hash *hash, |
| 3546 | uint32_t hash_value, |
| 3547 | const void *key, |
| 3548 | uint32_t key_size, |
| 3549 | void **value, |
| 3550 | int *added, |
| 3551 | bool is_record, |
| 3552 | const char *tag) |
| 3553 | { |
| 3554 | uint32_t i; |
| 3555 | uint32_t i_tries; |
| 3556 | uint32_t max_tries = grn_hash_max_index_size; |
| 3557 | const uint32_t step = grn_hash_calculate_step(hash_value); |
| 3558 | grn_id id; |
| 3559 | grn_id *index = NULL; |
| 3560 | grn_id *garbage_index = NULL; |
| 3561 | uint32_t index_hash_value = 0; |
| 3562 | uint32_t garbage_index_hash_value = 0; |
| 3563 | for (i = hash_value, i_tries = 0; i_tries < max_tries; i += step, i_tries++) { |
| 3564 | index = grn_hash_idx_at(ctx, hash, i); |
| 3565 | if (!index) { |
| 3566 | GRN_DEFINE_NAME(hash); |
| 3567 | ERR(GRN_INVALID_ARGUMENT, |
| 3568 | "%s[%.*s] failed to detect index: <%u>", |
| 3569 | tag, |
| 3570 | name_size, |
| 3571 | name, |
| 3572 | i); |
| 3573 | return GRN_ID_NIL; |
| 3574 | } |
| 3575 | index_hash_value = i; |
| 3576 | id = *index; |
| 3577 | if (id == GRN_ID_NIL) { |
| 3578 | break; |
| 3579 | } |
| 3580 | if (id == GARBAGE) { |
| 3581 | if (!garbage_index) { |
| 3582 | garbage_index = index; |
| 3583 | garbage_index_hash_value = index_hash_value; |
| 3584 | } |
| 3585 | continue; |
| 3586 | } |
| 3587 | |
| 3588 | grn_hash_entry *entry = grn_hash_entry_at(ctx, hash, id, GRN_TABLE_ADD); |
| 3589 | if (!entry) { |
| 3590 | GRN_DEFINE_NAME(hash); |
| 3591 | ERR(GRN_INVALID_ARGUMENT, |
| 3592 | "%s[%*.s] failed to find an entry: <%u>", |
| 3593 | tag, |
| 3594 | name_size, |
| 3595 | name, |
| 3596 | id); |
| 3597 | return GRN_ID_NIL; |
| 3598 | } |
| 3599 | if (grn_hash_entry_compare_key(ctx, |
| 3600 | hash, |
| 3601 | entry, |
| 3602 | hash_value, |
| 3603 | key, |
| 3604 | key_size)) { |
| 3605 | if (value) { |
| 3606 | *value = grn_hash_entry_get_value(ctx, hash, entry); |
| 3607 | } |
| 3608 | if (added) { |
| 3609 | *added = 0; |
| 3610 | } |
| 3611 | return id; |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | grn_id *target_index; |
| 3616 | uint32_t target_index_hash_value; |
| 3617 | if (garbage_index) { |
| 3618 | target_index = garbage_index; |
| 3619 | target_index_hash_value = garbage_index_hash_value; |
| 3620 | } else { |
| 3621 | target_index = index; |
| 3622 | target_index_hash_value = index_hash_value; |
| 3623 | if (*(hash->n_entries) == grn_hash_max_index_size) { |
| 3624 | GRN_DEFINE_NAME(hash); |
| 3625 | ERR(GRN_TOO_LARGE_OFFSET, |
| 3626 | "%s[%.*s] hash table is full: %u", |
| 3627 | tag, |
| 3628 | name_size, |
| 3629 | name, |
| 3630 | grn_hash_max_index_size); |
| 3631 | return GRN_ID_NIL; |
| 3632 | } |
| 3633 | } |
| 3634 | if (grn_hash_is_io_hash(hash)) { |
| 3635 | id = grn_io_hash_add(ctx, |
| 3636 | hash, |
| 3637 | hash_value, |
| 3638 | key, |
| 3639 | key_size, |
| 3640 | value, |
| 3641 | target_index, |
| 3642 | target_index_hash_value, |
| 3643 | target_index == garbage_index); |
| 3644 | } else { |
| 3645 | id = grn_tiny_hash_add(ctx, |
| 3646 | hash, |
| 3647 | hash_value, |
| 3648 | key, |
| 3649 | key_size, |
| 3650 | value, |
| 3651 | target_index, |
| 3652 | target_index == garbage_index); |
| 3653 | } |
| 3654 | if (id == GRN_ID_NIL) { |
| 3655 | GRN_DEFINE_NAME(hash); |
| 3656 | if (is_record) { |
| 3657 | ERR(GRN_INVALID_ARGUMENT, |
| 3658 | "%s[%.*s] failed to add: <%u>", |
| 3659 | tag, |
| 3660 | name_size, |
| 3661 | name, |
| 3662 | *((grn_id *)key)); |
| 3663 | } else { |
| 3664 | ERR(GRN_INVALID_ARGUMENT, |
| 3665 | "%s[%.*s] failed to add: <%.*s>", |
| 3666 | tag, |
| 3667 | name_size, |
| 3668 | name, |
| 3669 | (int)key_size, |
| 3670 | (const char *)key); |
| 3671 | } |
| 3672 | return GRN_ID_NIL; |
| 3673 | } |
| 3674 | if (added) { |
| 3675 | *added = 1; |
| 3676 | } |
| 3677 | return id; |
| 3678 | } |
| 3679 | |
| 3680 | grn_id |
| 3681 | grn_hash_add(grn_ctx *ctx, |
| 3682 | grn_hash *hash, |
| 3683 | const void *key, |
| 3684 | uint32_t key_size, |
| 3685 | void **value, |
| 3686 | int *added) |
| 3687 | { |
| 3688 | const char *tag = "[hash][add]"; |
| 3689 | uint32_t hash_value; |
| 3690 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 3691 | return GRN_ID_NIL; |
| 3692 | } |
| 3693 | if (!key) { |
| 3694 | GRN_DEFINE_NAME(hash); |
| 3695 | ERR(GRN_INVALID_ARGUMENT, |
| 3696 | "%s[%.*s] key must not NULL", |
| 3697 | tag, |
| 3698 | name_size, |
| 3699 | name); |
| 3700 | return GRN_ID_NIL; |
| 3701 | } |
| 3702 | if (!key_size) { |
| 3703 | GRN_DEFINE_NAME(hash); |
| 3704 | ERR(GRN_INVALID_ARGUMENT, |
| 3705 | "%s[%.*s] key size must not zero", |
| 3706 | tag, |
| 3707 | name_size, |
| 3708 | name); |
| 3709 | return GRN_ID_NIL; |
| 3710 | } |
| 3711 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 3712 | if (key_size > hash->key_size) { |
| 3713 | GRN_DEFINE_NAME(hash); |
| 3714 | ERR(GRN_INVALID_ARGUMENT, "%s[%.*s] too long key", tag, name_size, name); |
| 3715 | return GRN_ID_NIL; |
| 3716 | } |
| 3717 | hash_value = grn_hash_calculate_hash_value(key, key_size); |
| 3718 | } else { |
| 3719 | if (key_size != hash->key_size) { |
| 3720 | GRN_DEFINE_NAME(hash); |
| 3721 | ERR(GRN_INVALID_ARGUMENT, |
| 3722 | "%s[%*.s] key size unmatch", |
| 3723 | tag, |
| 3724 | name_size, |
| 3725 | name); |
| 3726 | return GRN_ID_NIL; |
| 3727 | } |
| 3728 | if (key_size == sizeof(uint32_t)) { |
| 3729 | hash_value = *((uint32_t *)key); |
| 3730 | } else { |
| 3731 | hash_value = grn_hash_calculate_hash_value(key, key_size); |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | /* lock */ |
| 3736 | uint32_t reset_threshold = *hash->n_entries + *hash->n_garbages; |
| 3737 | if (!grn_id_maybe_table(ctx, hash->obj.header.domain)) { |
| 3738 | reset_threshold *= 2; |
| 3739 | } |
| 3740 | grn_rc rc = grn_hash_ensure_rehash(ctx, hash, reset_threshold, tag); |
| 3741 | if (rc != GRN_SUCCESS) { |
| 3742 | return GRN_ID_NIL; |
| 3743 | } |
| 3744 | |
| 3745 | grn_id id = grn_hash_add_entry(ctx, |
| 3746 | hash, |
| 3747 | hash_value, |
| 3748 | key, |
| 3749 | key_size, |
| 3750 | value, |
| 3751 | added, |
| 3752 | false, |
| 3753 | tag); |
| 3754 | /* unlock */ |
| 3755 | return id; |
| 3756 | } |
| 3757 | |
| 3758 | static inline grn_id |
| 3759 | grn_hash_get_internal(grn_ctx *ctx, |
| 3760 | grn_hash *hash, |
| 3761 | uint32_t hash_value, |
| 3762 | const void *key, |
| 3763 | unsigned int key_size, |
| 3764 | void **value) |
| 3765 | { |
| 3766 | uint32_t i; |
| 3767 | const uint32_t step = grn_hash_calculate_step(hash_value); |
| 3768 | for (i = hash_value;; i += step) { |
| 3769 | grn_id id; |
| 3770 | grn_id *const index = grn_hash_idx_at(ctx, hash, i); |
| 3771 | if (!index) { |
| 3772 | return GRN_ID_NIL; |
| 3773 | } |
| 3774 | id = *index; |
| 3775 | if (!id) { |
| 3776 | return GRN_ID_NIL; |
| 3777 | } |
| 3778 | if (id != GARBAGE) { |
| 3779 | grn_hash_entry *const entry = grn_hash_entry_at(ctx, hash, id, 0); |
| 3780 | if (entry) { |
| 3781 | if (grn_hash_entry_compare_key(ctx, |
| 3782 | hash, |
| 3783 | entry, |
| 3784 | hash_value, |
| 3785 | key, |
| 3786 | key_size)) { |
| 3787 | if (value) { |
| 3788 | *value = grn_hash_entry_get_value(ctx, hash, entry); |
| 3789 | } |
| 3790 | return id; |
| 3791 | } |
| 3792 | } |
| 3793 | } |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | grn_id |
| 3798 | grn_hash_get(grn_ctx *ctx, |
| 3799 | grn_hash *hash, |
| 3800 | const void *key, |
| 3801 | unsigned int key_size, |
| 3802 | void **value) |
| 3803 | { |
| 3804 | uint32_t hash_value; |
| 3805 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 3806 | return GRN_ID_NIL; |
| 3807 | } |
| 3808 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 3809 | if (key_size > hash->key_size) { |
| 3810 | return GRN_ID_NIL; |
| 3811 | } |
| 3812 | hash_value = grn_hash_calculate_hash_value(key, key_size); |
| 3813 | } else { |
| 3814 | if (key_size != hash->key_size) { |
| 3815 | return GRN_ID_NIL; |
| 3816 | } |
| 3817 | if (key_size == sizeof(uint32_t)) { |
| 3818 | hash_value = *((uint32_t *)key); |
| 3819 | } else { |
| 3820 | hash_value = grn_hash_calculate_hash_value(key, key_size); |
| 3821 | } |
| 3822 | } |
| 3823 | |
| 3824 | return grn_hash_get_internal(ctx, hash, hash_value, key, key_size, value); |
| 3825 | } |
| 3826 | |
| 3827 | static grn_rc |
| 3828 | grn_hash_add_records_validate(grn_ctx *ctx, |
| 3829 | grn_hash *hash, |
| 3830 | grn_operator op, |
| 3831 | const char *tag) |
| 3832 | { |
| 3833 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 3834 | return ctx->rc; |
| 3835 | } |
| 3836 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 3837 | GRN_DEFINE_NAME(hash); |
| 3838 | ERR(GRN_INVALID_ARGUMENT, |
| 3839 | "%s[%.*s] must not be variable key size", |
| 3840 | tag, |
| 3841 | name_size, |
| 3842 | name); |
| 3843 | return ctx->rc; |
| 3844 | } else { |
| 3845 | const uint32_t key_size = sizeof(grn_id); |
| 3846 | if (hash->key_size != key_size) { |
| 3847 | GRN_DEFINE_NAME(hash); |
| 3848 | ERR(GRN_INVALID_ARGUMENT, |
| 3849 | "%s[%.*s] key size unmatch: <%u> != <%u>", |
| 3850 | tag, |
| 3851 | name_size, |
| 3852 | name, |
| 3853 | hash->key_size, |
| 3854 | key_size); |
| 3855 | return ctx->rc; |
| 3856 | } |
| 3857 | } |
| 3858 | switch (op) { |
| 3859 | case GRN_OP_AND: |
| 3860 | case GRN_OP_OR: |
| 3861 | break; |
| 3862 | default: |
| 3863 | { |
| 3864 | GRN_DEFINE_NAME(hash); |
| 3865 | ERR(GRN_INVALID_ARGUMENT, |
| 3866 | "%s[%.*s] operator must be GRN_OP_AND or GRN_OP_OR: <%s>", |
| 3867 | tag, |
| 3868 | name_size, |
| 3869 | name, |
| 3870 | grn_operator_to_string(op)); |
| 3871 | return ctx->rc; |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | return GRN_SUCCESS; |
| 3876 | } |
| 3877 | |
| 3878 | static inline grn_rc |
| 3879 | grn_hash_add_record(grn_ctx *ctx, |
| 3880 | grn_hash *hash, |
| 3881 | grn_posting_internal *posting, |
| 3882 | grn_operator op, |
| 3883 | const char *tag) |
| 3884 | { |
| 3885 | const grn_id key = posting->rid; |
| 3886 | const uint32_t key_size = sizeof(grn_id); |
| 3887 | const uint32_t hash_value = key; |
| 3888 | void *value = NULL; |
| 3889 | grn_id id; |
| 3890 | if (op == GRN_OP_OR) { |
| 3891 | id = grn_hash_add_entry(ctx, |
| 3892 | hash, |
| 3893 | hash_value, |
| 3894 | &key, |
| 3895 | key_size, |
| 3896 | &value, |
| 3897 | NULL, |
| 3898 | true, |
| 3899 | tag); |
| 3900 | } else { |
| 3901 | id = grn_hash_get_internal(ctx, hash, hash_value, &key, key_size, &value); |
| 3902 | } |
| 3903 | if (id == GRN_ID_NIL) { |
| 3904 | return ctx->rc; |
| 3905 | } |
| 3906 | |
| 3907 | if (hash->obj.header.flags & GRN_OBJ_WITH_SUBREC) { |
| 3908 | grn_rset_recinfo *recinfo = value; |
| 3909 | if (op == GRN_OP_AND) { |
| 3910 | recinfo->n_subrecs |= GRN_RSET_UTIL_BIT; |
| 3911 | } |
| 3912 | grn_rset_posinfo posinfo = { |
| 3913 | posting->rid, |
| 3914 | posting->sid, |
| 3915 | posting->pos, |
| 3916 | }; |
| 3917 | float weight = posting->weight_float * posting->scale; |
| 3918 | grn_table_add_subrec(ctx, (grn_obj *)hash, recinfo, weight, &posinfo, 1); |
| 3919 | grn_selector_data_current_add_score(ctx, |
| 3920 | (grn_obj *)hash, |
| 3921 | id, |
| 3922 | posinfo.rid, |
| 3923 | weight); |
| 3924 | } |
| 3925 | return ctx->rc; |
| 3926 | } |
| 3927 | |
| 3928 | grn_rc |
| 3929 | grn_hash_add_table_cursor(grn_ctx *ctx, |
| 3930 | grn_hash *hash, |
| 3931 | grn_table_cursor *cursor, |
| 3932 | double score, |
| 3933 | grn_operator op) |
| 3934 | { |
| 3935 | const char *tag = "[hash][add-table-cursor]"; |
| 3936 | grn_rc rc = grn_hash_add_records_validate(ctx, hash, op, tag); |
| 3937 | if (rc != GRN_SUCCESS) { |
| 3938 | return rc; |
| 3939 | } |
| 3940 | |
| 3941 | /* lock */ |
| 3942 | if (op == GRN_OP_OR) { |
| 3943 | grn_obj *table = grn_table_cursor_table(ctx, cursor); |
| 3944 | const uint32_t reset_threshold = grn_table_size(ctx, table); |
| 3945 | rc = grn_hash_ensure_rehash(ctx, hash, reset_threshold, tag); |
| 3946 | if (rc != GRN_SUCCESS) { |
| 3947 | return rc; |
| 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | grn_posting_internal posting = {0}; |
| 3952 | posting.weight_float = (float)score; |
| 3953 | posting.scale = 1.0; |
| 3954 | while ((posting.rid = grn_table_cursor_next(ctx, cursor))) { |
| 3955 | rc = grn_hash_add_record(ctx, hash, &posting, op, tag); |
| 3956 | if (rc != GRN_SUCCESS) { |
| 3957 | return rc; |
| 3958 | } |
| 3959 | } |
| 3960 | /* unlock */ |
| 3961 | |
| 3962 | return ctx->rc; |
| 3963 | } |
| 3964 | |
| 3965 | grn_rc |
| 3966 | grn_hash_add_index_cursor(grn_ctx *ctx, |
| 3967 | grn_hash *hash, |
| 3968 | grn_obj *cursor, |
| 3969 | double additional_score, |
| 3970 | double weight, |
| 3971 | grn_operator op) |
| 3972 | { |
| 3973 | const char *tag = "[hash][add-index-cursor]"; |
| 3974 | grn_rc rc = grn_hash_add_records_validate(ctx, hash, op, tag); |
| 3975 | if (rc != GRN_SUCCESS) { |
| 3976 | return rc; |
| 3977 | } |
| 3978 | |
| 3979 | /* lock */ |
| 3980 | if (op == GRN_OP_OR) { |
| 3981 | grn_obj *index_column = grn_index_cursor_get_index_column(ctx, cursor); |
| 3982 | grn_obj *table = grn_ctx_at(ctx, DB_OBJ(index_column)->range); |
| 3983 | const uint32_t reset_threshold = grn_table_size(ctx, table); |
| 3984 | grn_obj_unref(ctx, table); |
| 3985 | rc = grn_hash_ensure_rehash(ctx, hash, reset_threshold, tag); |
| 3986 | if (rc != GRN_SUCCESS) { |
| 3987 | return rc; |
| 3988 | } |
| 3989 | } |
| 3990 | |
| 3991 | grn_id term_id; |
| 3992 | grn_posting *posting; |
| 3993 | while ((posting = grn_index_cursor_next(ctx, cursor, &term_id))) { |
| 3994 | grn_posting_internal posting_new = *((grn_posting_internal *)posting); |
| 3995 | posting_new.weight_float += (float)additional_score; |
| 3996 | posting_new.weight_float *= (float)weight; |
| 3997 | rc = grn_hash_add_record(ctx, hash, &posting_new, op, tag); |
| 3998 | if (rc != GRN_SUCCESS) { |
| 3999 | return rc; |
| 4000 | } |
| 4001 | } |
| 4002 | /* unlock */ |
| 4003 | |
| 4004 | return ctx->rc; |
| 4005 | } |
| 4006 | |
| 4007 | grn_rc |
| 4008 | grn_hash_add_ii_cursor(grn_ctx *ctx, |
| 4009 | grn_hash *hash, |
| 4010 | grn_ii_cursor *cursor, |
| 4011 | double additional_score, |
| 4012 | double weight, |
| 4013 | grn_operator op) |
| 4014 | { |
| 4015 | const char *tag = "[hash][add-ii-cursor]"; |
| 4016 | grn_rc rc = grn_hash_add_records_validate(ctx, hash, op, tag); |
| 4017 | if (rc != GRN_SUCCESS) { |
| 4018 | return rc; |
| 4019 | } |
| 4020 | |
| 4021 | /* lock */ |
| 4022 | if (op == GRN_OP_OR) { |
| 4023 | grn_ii *ii = grn_ii_cursor_get_ii(ctx, cursor); |
| 4024 | grn_obj *table = grn_ctx_at(ctx, DB_OBJ(ii)->range); |
| 4025 | const uint32_t reset_threshold = grn_table_size(ctx, table); |
| 4026 | grn_obj_unref(ctx, table); |
| 4027 | rc = grn_hash_ensure_rehash(ctx, hash, reset_threshold, tag); |
| 4028 | if (rc != GRN_SUCCESS) { |
| 4029 | return rc; |
| 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | grn_posting *posting; |
| 4034 | while ((posting = grn_ii_cursor_next(ctx, cursor))) { |
| 4035 | grn_posting_internal posting_new = *((grn_posting_internal *)posting); |
| 4036 | posting_new.weight_float += (float)additional_score; |
| 4037 | posting_new.weight_float *= (float)weight; |
| 4038 | rc = grn_hash_add_record(ctx, hash, &posting_new, op, tag); |
| 4039 | if (rc != GRN_SUCCESS) { |
| 4040 | return rc; |
| 4041 | } |
| 4042 | } |
| 4043 | /* unlock */ |
| 4044 | |
| 4045 | return ctx->rc; |
| 4046 | } |
| 4047 | |
| 4048 | grn_rc |
| 4049 | grn_hash_add_ii_select_cursor(grn_ctx *ctx, |
| 4050 | grn_hash *hash, |
| 4051 | grn_ii_select_cursor *cursor, |
| 4052 | grn_operator op) |
| 4053 | { |
| 4054 | const char *tag = "[hash][add-ii-select-cursor]"; |
| 4055 | grn_rc rc = grn_hash_add_records_validate(ctx, hash, op, tag); |
| 4056 | if (rc != GRN_SUCCESS) { |
| 4057 | return rc; |
| 4058 | } |
| 4059 | |
| 4060 | /* lock */ |
| 4061 | if (op == GRN_OP_OR) { |
| 4062 | grn_ii *ii = grn_ii_select_cursor_get_ii(ctx, cursor); |
| 4063 | grn_obj *table = grn_ctx_at(ctx, DB_OBJ(ii)->range); |
| 4064 | const uint32_t reset_threshold = grn_table_size(ctx, table); |
| 4065 | grn_obj_unref(ctx, table); |
| 4066 | rc = grn_hash_ensure_rehash(ctx, hash, reset_threshold, tag); |
| 4067 | if (rc != GRN_SUCCESS) { |
| 4068 | return rc; |
| 4069 | } |
| 4070 | } |
| 4071 | |
| 4072 | grn_ii_select_cursor_posting *posting; |
| 4073 | while ((posting = grn_ii_select_cursor_next(ctx, cursor))) { |
| 4074 | grn_posting_internal posting_new; |
| 4075 | posting_new.rid = posting->rid; |
| 4076 | posting_new.sid = posting->sid; |
| 4077 | posting_new.pos = posting->pos; |
| 4078 | posting_new.weight_float = (float)(posting->score); |
| 4079 | posting_new.scale = 1.0; |
| 4080 | rc = grn_hash_add_record(ctx, hash, &posting_new, op, tag); |
| 4081 | if (rc != GRN_SUCCESS) { |
| 4082 | return rc; |
| 4083 | } |
| 4084 | } |
| 4085 | /* unlock */ |
| 4086 | |
| 4087 | return ctx->rc; |
| 4088 | } |
| 4089 | |
| 4090 | static inline grn_hash_entry * |
| 4091 | grn_hash_get_entry(grn_ctx *ctx, grn_hash *hash, grn_id id) |
| 4092 | { |
| 4093 | if (!grn_hash_bitmap_at(ctx, hash, id)) { |
| 4094 | return NULL; |
| 4095 | } |
| 4096 | return grn_hash_entry_at(ctx, hash, id, 0); |
| 4097 | } |
| 4098 | |
| 4099 | const char * |
| 4100 | _grn_hash_key(grn_ctx *ctx, grn_hash *hash, grn_id id, uint32_t *key_size) |
| 4101 | { |
| 4102 | grn_hash_entry *const entry = grn_hash_get_entry(ctx, hash, id); |
| 4103 | if (!entry) { |
| 4104 | *key_size = 0; |
| 4105 | return NULL; |
| 4106 | } |
| 4107 | *key_size = grn_hash_entry_get_key_size(hash, entry); |
| 4108 | return grn_hash_entry_get_key(ctx, hash, entry); |
| 4109 | } |
| 4110 | |
| 4111 | int |
| 4112 | grn_hash_get_key( |
| 4113 | grn_ctx *ctx, grn_hash *hash, grn_id id, void *keybuf, int bufsize) |
| 4114 | { |
| 4115 | size_t key_size; |
| 4116 | grn_hash_entry *entry; |
| 4117 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 4118 | return 0; |
| 4119 | } |
| 4120 | entry = grn_hash_get_entry(ctx, hash, id); |
| 4121 | if (!entry) { |
| 4122 | return 0; |
| 4123 | } |
| 4124 | key_size = grn_hash_entry_get_key_size(hash, entry); |
| 4125 | if ((size_t)bufsize >= key_size) { |
| 4126 | grn_memcpy(keybuf, grn_hash_entry_get_key(ctx, hash, entry), key_size); |
| 4127 | } |
| 4128 | return (int)key_size; |
| 4129 | } |
| 4130 | |
| 4131 | int |
| 4132 | grn_hash_get_key2(grn_ctx *ctx, grn_hash *hash, grn_id id, grn_obj *bulk) |
| 4133 | { |
| 4134 | size_t key_size; |
| 4135 | char *key; |
| 4136 | grn_hash_entry *entry; |
| 4137 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 4138 | return 0; |
| 4139 | } |
| 4140 | entry = grn_hash_get_entry(ctx, hash, id); |
| 4141 | if (!entry) { |
| 4142 | return 0; |
| 4143 | } |
| 4144 | key_size = grn_hash_entry_get_key_size(hash, entry); |
| 4145 | key = grn_hash_entry_get_key(ctx, hash, entry); |
| 4146 | if (bulk->header.impl_flags & GRN_OBJ_REFER) { |
| 4147 | bulk->u.b.head = key; |
| 4148 | bulk->u.b.curr = key + key_size; |
| 4149 | } else { |
| 4150 | grn_bulk_write(ctx, bulk, key, key_size); |
| 4151 | } |
| 4152 | return (int)key_size; |
| 4153 | } |
| 4154 | |
| 4155 | int |
| 4156 | grn_hash_get_value(grn_ctx *ctx, grn_hash *hash, grn_id id, void *valuebuf) |
| 4157 | { |
| 4158 | void *value; |
| 4159 | grn_hash_entry *entry; |
| 4160 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 4161 | return 0; |
| 4162 | } |
| 4163 | entry = grn_hash_get_entry(ctx, hash, id); |
| 4164 | if (!entry) { |
| 4165 | return 0; |
| 4166 | } |
| 4167 | value = grn_hash_entry_get_value(ctx, hash, entry); |
| 4168 | if (!value) { |
| 4169 | return 0; |
| 4170 | } |
| 4171 | if (valuebuf) { |
| 4172 | grn_memcpy(valuebuf, value, hash->value_size); |
| 4173 | } |
| 4174 | return (int)(hash->value_size); |
| 4175 | } |
| 4176 | |
| 4177 | const char * |
| 4178 | grn_hash_get_value_(grn_ctx *ctx, grn_hash *hash, grn_id id, uint32_t *size) |
| 4179 | { |
| 4180 | const void *value; |
| 4181 | grn_hash_entry *entry; |
| 4182 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 4183 | return NULL; |
| 4184 | } |
| 4185 | entry = grn_hash_get_entry(ctx, hash, id); |
| 4186 | if (!entry) { |
| 4187 | return NULL; |
| 4188 | } |
| 4189 | value = grn_hash_entry_get_value(ctx, hash, entry); |
| 4190 | if (!value) { |
| 4191 | return NULL; |
| 4192 | } |
| 4193 | if (size) { |
| 4194 | *size = hash->value_size; |
| 4195 | } |
| 4196 | return (const char *)value; |
| 4197 | } |
| 4198 | |
| 4199 | int |
| 4200 | grn_hash_get_key_value(grn_ctx *ctx, |
| 4201 | grn_hash *hash, |
| 4202 | grn_id id, |
| 4203 | void *keybuf, |
| 4204 | int bufsize, |
| 4205 | void *valuebuf) |
| 4206 | { |
| 4207 | void *value; |
| 4208 | size_t key_size; |
| 4209 | grn_hash_entry *entry; |
| 4210 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 4211 | return 0; |
| 4212 | } |
| 4213 | entry = grn_hash_get_entry(ctx, hash, id); |
| 4214 | if (!entry) { |
| 4215 | return 0; |
| 4216 | } |
| 4217 | key_size = grn_hash_entry_get_key_size(hash, entry); |
| 4218 | if ((size_t)bufsize >= key_size) { |
| 4219 | grn_memcpy(keybuf, grn_hash_entry_get_key(ctx, hash, entry), key_size); |
| 4220 | } |
| 4221 | value = grn_hash_entry_get_value(ctx, hash, entry); |
| 4222 | if (!value) { |
| 4223 | return 0; |
| 4224 | } |
| 4225 | if (valuebuf) { |
| 4226 | grn_memcpy(valuebuf, value, hash->value_size); |
| 4227 | } |
| 4228 | return (int)key_size; |
| 4229 | } |
| 4230 | |
| 4231 | int |
| 4232 | _grn_hash_get_key_value( |
| 4233 | grn_ctx *ctx, grn_hash *hash, grn_id id, void **key, void **value) |
| 4234 | { |
| 4235 | int key_size; |
| 4236 | grn_hash_entry *entry; |
| 4237 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 4238 | return 0; |
| 4239 | } |
| 4240 | entry = grn_hash_get_entry(ctx, hash, id); |
| 4241 | if (!entry) { |
| 4242 | return 0; |
| 4243 | } |
| 4244 | key_size = grn_hash_entry_get_key_size(hash, entry); |
| 4245 | *key = grn_hash_entry_get_key(ctx, hash, entry); |
| 4246 | *value = grn_hash_entry_get_value(ctx, hash, entry); |
| 4247 | return *value ? key_size : 0; |
| 4248 | } |
| 4249 | |
| 4250 | static void |
| 4251 | grn_hash_set_value_raw(grn_ctx *ctx, |
| 4252 | grn_hash *hash, |
| 4253 | void *entry_value, |
| 4254 | const void *value) |
| 4255 | { |
| 4256 | grn_memcpy(entry_value, value, hash->value_size); |
| 4257 | } |
| 4258 | |
| 4259 | grn_rc |
| 4260 | grn_hash_set_value( |
| 4261 | grn_ctx *ctx, grn_hash *hash, grn_id id, const void *value, int flags) |
| 4262 | { |
| 4263 | void *entry_value; |
| 4264 | grn_hash_entry *entry; |
| 4265 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 4266 | return GRN_ID_NIL; |
| 4267 | } |
| 4268 | if (!value) { |
| 4269 | return GRN_INVALID_ARGUMENT; |
| 4270 | } |
| 4271 | entry = grn_hash_get_entry(ctx, hash, id); |
| 4272 | if (!entry) { |
| 4273 | return GRN_NO_MEMORY_AVAILABLE; |
| 4274 | } |
| 4275 | entry_value = grn_hash_entry_get_value(ctx, hash, entry); |
| 4276 | if (!entry_value) { |
| 4277 | return GRN_NO_MEMORY_AVAILABLE; |
| 4278 | } |
| 4279 | |
| 4280 | grn_hash_wal_add_entry_data *wal_data = hash->wal_data; |
| 4281 | wal_data->tag = "[hash][set-value]"; |
| 4282 | wal_data->event = GRN_WAL_EVENT_SET_VALUE; |
| 4283 | wal_data->record_id = id; |
| 4284 | int32_t int32_value; |
| 4285 | int64_t int64_value; |
| 4286 | |
| 4287 | switch (flags & GRN_OBJ_SET_MASK) { |
| 4288 | case GRN_OBJ_SET: |
| 4289 | wal_data->value = value; |
| 4290 | break; |
| 4291 | case GRN_OBJ_INCR: |
| 4292 | switch (hash->value_size) { |
| 4293 | case sizeof(int32_t): |
| 4294 | int32_value = *((int32_t *)entry_value) + *((int32_t *)value); |
| 4295 | wal_data->value = &int32_value; |
| 4296 | break; |
| 4297 | case sizeof(int64_t): |
| 4298 | int64_value = *((int64_t *)entry_value) + *((int64_t *)value); |
| 4299 | wal_data->value = &int64_value; |
| 4300 | break; |
| 4301 | default: |
| 4302 | return GRN_INVALID_ARGUMENT; |
| 4303 | } |
| 4304 | break; |
| 4305 | case GRN_OBJ_DECR: |
| 4306 | switch (hash->value_size) { |
| 4307 | case sizeof(int32_t): |
| 4308 | int32_value = *((int32_t *)entry_value) - *((int32_t *)value); |
| 4309 | wal_data->value = &int32_value; |
| 4310 | break; |
| 4311 | case sizeof(int64_t): |
| 4312 | int64_value = *((int64_t *)entry_value) - *((int64_t *)value); |
| 4313 | wal_data->value = &int64_value; |
| 4314 | break; |
| 4315 | default: |
| 4316 | return GRN_INVALID_ARGUMENT; |
| 4317 | } |
| 4318 | break; |
| 4319 | default: |
| 4320 | grn_obj_set_error(ctx, |
| 4321 | (grn_obj *)hash, |
| 4322 | GRN_INVALID_ARGUMENT, |
| 4323 | wal_data->record_id, |
| 4324 | wal_data->tag, |
| 4325 | "set flag must be " |
| 4326 | "GRN_OBJ_SET, GRN_OBJ_INCR or GRN_OBJ_DECR: " |
| 4327 | "%s<%d|%d>", |
| 4328 | grn_obj_set_flag_to_string(flags), |
| 4329 | flags & GRN_OBJ_SET_MASK, |
| 4330 | flags); |
| 4331 | return ctx->rc; |
| 4332 | } |
| 4333 | |
| 4334 | if (grn_hash_wal_add_entry(ctx, wal_data) != GRN_SUCCESS) { |
| 4335 | return ctx->rc; |
| 4336 | } |
| 4337 | grn_hash_set_value_raw(ctx, hash, entry_value, wal_data->value); |
| 4338 | if (ctx->rc == GRN_SUCCESS) { |
| 4339 | grn_hash_wal_set_wal_id(ctx, hash, wal_data->wal_id); |
| 4340 | } |
| 4341 | return ctx->rc; |
| 4342 | } |
| 4343 | |
| 4344 | typedef struct { |
| 4345 | grn_hash *hash; |
| 4346 | const char *tag; |
| 4347 | grn_id index_hash_value; |
| 4348 | grn_id *index; |
| 4349 | grn_id id; |
| 4350 | grn_hash_entry *entry; |
| 4351 | uint32_t key_size; |
| 4352 | } grn_hash_delete_data; |
| 4353 | |
| 4354 | static inline grn_rc |
| 4355 | grn_hash_delete_entry(grn_ctx *ctx, grn_hash_delete_data *data) |
| 4356 | { |
| 4357 | grn_hash *hash = data->hash; |
| 4358 | *(data->index) = GARBAGE; |
| 4359 | if (grn_hash_is_io_hash(hash)) { |
| 4360 | grn_id *garbages; |
| 4361 | if (GRN_HASH_IS_LARGE_KEY(hash)) { |
| 4362 | garbages = hash->header.large->garbages; |
| 4363 | } else { |
| 4364 | garbages = hash->header.normal->garbages; |
| 4365 | } |
| 4366 | uint32_t garbage_index = data->key_size - 1; |
| 4367 | *((grn_id *)(data->entry)) = garbages[garbage_index]; |
| 4368 | garbages[garbage_index] = data->id; |
| 4369 | grn_io_array_bit_off(ctx, hash->io, GRN_HASH_BITMAP_SEGMENT, data->id); |
| 4370 | } else { |
| 4371 | *((grn_id *)(data->entry)) = hash->garbages; |
| 4372 | hash->garbages = data->id; |
| 4373 | if ((hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) && |
| 4374 | !(data->entry->header.flag & HASH_IMMEDIATE)) { |
| 4375 | grn_ctx *ctx = hash->ctx; |
| 4376 | GRN_CTX_FREE(ctx, data->entry->tiny_entry.key.ptr); |
| 4377 | } |
| 4378 | grn_tiny_bitmap_get_and_set(&hash->bitmap, data->id, 0); |
| 4379 | } |
| 4380 | (*(hash->n_garbages))++; |
| 4381 | (*(hash->n_entries))--; |
| 4382 | return GRN_SUCCESS; |
| 4383 | } |
| 4384 | |
| 4385 | static inline grn_rc |
| 4386 | grn_hash_delete_internal(grn_ctx *ctx, grn_hash_delete_data *data) |
| 4387 | { |
| 4388 | grn_hash_wal_add_entry_data *wal_data = data->hash->wal_data; |
| 4389 | const bool need_wal = grn_hash_wal_need(ctx, data->hash); |
| 4390 | if (need_wal) { |
| 4391 | wal_data->tag = data->tag; |
| 4392 | wal_data->event = GRN_WAL_EVENT_DELETE_ENTRY; |
| 4393 | wal_data->record_id = data->id; |
| 4394 | wal_data->key_size = data->key_size; |
| 4395 | wal_data->index_hash_value = data->index_hash_value; |
| 4396 | grn_id *garbages; |
| 4397 | if (GRN_HASH_IS_LARGE_KEY(data->hash)) { |
| 4398 | garbages = data->hash->header.large->garbages; |
| 4399 | } else { |
| 4400 | garbages = data->hash->header.normal->garbages; |
| 4401 | } |
| 4402 | uint32_t garbage_index = (uint32_t)(wal_data->key_size - 1); |
| 4403 | wal_data->garbage_record_id = garbages[garbage_index]; |
| 4404 | wal_data->n_garbages = *(data->hash->n_garbages); |
| 4405 | wal_data->n_entries = *(data->hash->n_entries); |
| 4406 | if (grn_hash_wal_add_entry(ctx, wal_data) != GRN_SUCCESS) { |
| 4407 | return ctx->rc; |
| 4408 | } |
| 4409 | } |
| 4410 | grn_rc rc = grn_hash_delete_entry(ctx, data); |
| 4411 | if (rc == GRN_SUCCESS && need_wal) { |
| 4412 | grn_hash_wal_set_wal_id(ctx, data->hash, wal_data->wal_id); |
| 4413 | } |
| 4414 | return ctx->rc; |
| 4415 | } |
| 4416 | |
| 4417 | grn_rc |
| 4418 | grn_hash_delete_by_id(grn_ctx *ctx, |
| 4419 | grn_hash *hash, |
| 4420 | grn_id id, |
| 4421 | grn_table_delete_optarg *optarg) |
| 4422 | { |
| 4423 | grn_hash_delete_data data = {0}; |
| 4424 | data.hash = hash; |
| 4425 | data.tag = "[hash][delete][id]"; |
| 4426 | data.id = id; |
| 4427 | if (!hash || id == GRN_ID_NIL) { |
| 4428 | return GRN_INVALID_ARGUMENT; |
| 4429 | } |
| 4430 | grn_rc rc = grn_hash_error_if_truncated(ctx, hash); |
| 4431 | if (rc != GRN_SUCCESS) { |
| 4432 | return rc; |
| 4433 | } |
| 4434 | rc = GRN_INVALID_ARGUMENT; |
| 4435 | /* lock */ |
| 4436 | data.entry = grn_hash_entry_at(ctx, hash, data.id, 0); |
| 4437 | if (data.entry) { |
| 4438 | uint32_t hash_value = data.entry->hash_value; |
| 4439 | uint32_t step = grn_hash_calculate_step(hash_value); |
| 4440 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 4441 | data.key_size = data.entry->header.key_size; |
| 4442 | } else { |
| 4443 | data.key_size = hash->key_size; |
| 4444 | } |
| 4445 | for (data.index_hash_value = hash_value;; data.index_hash_value += step) { |
| 4446 | data.index = grn_hash_idx_at(ctx, hash, data.index_hash_value); |
| 4447 | if (!data.index) { |
| 4448 | return GRN_NO_MEMORY_AVAILABLE; |
| 4449 | } |
| 4450 | if (*(data.index) == GRN_ID_NIL) { |
| 4451 | break; |
| 4452 | } |
| 4453 | if (*(data.index) != id) { |
| 4454 | continue; |
| 4455 | } |
| 4456 | rc = grn_hash_delete_internal(ctx, &data); |
| 4457 | break; |
| 4458 | } |
| 4459 | } |
| 4460 | /* unlock */ |
| 4461 | return rc; |
| 4462 | } |
| 4463 | |
| 4464 | grn_rc |
| 4465 | grn_hash_delete(grn_ctx *ctx, |
| 4466 | grn_hash *hash, |
| 4467 | const void *key, |
| 4468 | uint32_t key_size, |
| 4469 | grn_table_delete_optarg *optarg) |
| 4470 | { |
| 4471 | grn_hash_delete_data data = {0}; |
| 4472 | data.hash = hash; |
| 4473 | data.tag = "[hash][delete][key]"; |
| 4474 | data.key_size = key_size; |
| 4475 | grn_rc rc = grn_hash_error_if_truncated(ctx, hash); |
| 4476 | if (rc != GRN_SUCCESS) { |
| 4477 | return rc; |
| 4478 | } |
| 4479 | rc = GRN_INVALID_ARGUMENT; |
| 4480 | uint32_t hash_value; |
| 4481 | if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) { |
| 4482 | if (key_size > hash->key_size) { |
| 4483 | return GRN_INVALID_ARGUMENT; |
| 4484 | } |
| 4485 | hash_value = grn_hash_calculate_hash_value(key, key_size); |
| 4486 | } else { |
| 4487 | if (key_size != hash->key_size) { |
| 4488 | return GRN_INVALID_ARGUMENT; |
| 4489 | } |
| 4490 | if (key_size == sizeof(uint32_t)) { |
| 4491 | hash_value = *((uint32_t *)key); |
| 4492 | } else { |
| 4493 | hash_value = grn_hash_calculate_hash_value(key, key_size); |
| 4494 | } |
| 4495 | } |
| 4496 | uint32_t step = grn_hash_calculate_step(hash_value); |
| 4497 | { |
| 4498 | /* lock */ |
| 4499 | for (data.index_hash_value = hash_value;; data.index_hash_value += step) { |
| 4500 | data.index = grn_hash_idx_at(ctx, hash, data.index_hash_value); |
| 4501 | if (!data.index) { |
| 4502 | return GRN_NO_MEMORY_AVAILABLE; |
| 4503 | } |
| 4504 | data.id = *(data.index); |
| 4505 | if (data.id == GRN_ID_NIL) { |
| 4506 | break; |
| 4507 | } |
| 4508 | if (data.id == GARBAGE) { |
| 4509 | continue; |
| 4510 | } |
| 4511 | data.entry = grn_hash_entry_at(ctx, hash, data.id, 0); |
| 4512 | if (!data.entry) { |
| 4513 | continue; |
| 4514 | } |
| 4515 | if (grn_hash_entry_compare_key(ctx, |
| 4516 | hash, |
| 4517 | data.entry, |
| 4518 | hash_value, |
| 4519 | key, |
| 4520 | key_size)) { |
| 4521 | rc = grn_hash_delete_internal(ctx, &data); |
| 4522 | break; |
| 4523 | } |
| 4524 | } |
| 4525 | /* unlock */ |
| 4526 | return rc; |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | void |
| 4531 | grn_hash_cursor_close(grn_ctx *ctx, grn_hash_cursor *c) |
| 4532 | { |
| 4533 | GRN_ASSERT(c->ctx == ctx); |
| 4534 | GRN_FREE(c); |
| 4535 | } |
| 4536 | |
| 4537 | #define HASH_CURR_MAX(hash) \ |
| 4538 | ((grn_hash_is_io_hash(hash)) ? (hash)->header.common->curr_rec \ |
| 4539 | : (hash)->a.max) |
| 4540 | |
| 4541 | grn_hash_cursor * |
| 4542 | grn_hash_cursor_open(grn_ctx *ctx, |
| 4543 | grn_hash *hash, |
| 4544 | const void *min, |
| 4545 | uint32_t min_size, |
| 4546 | const void *max, |
| 4547 | uint32_t max_size, |
| 4548 | int offset, |
| 4549 | int limit, |
| 4550 | int flags) |
| 4551 | { |
| 4552 | grn_hash_cursor *c; |
| 4553 | if (!hash || !ctx) { |
| 4554 | return NULL; |
| 4555 | } |
| 4556 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 4557 | return NULL; |
| 4558 | } |
| 4559 | if (!(c = GRN_CALLOC(sizeof(grn_hash_cursor)))) { |
| 4560 | return NULL; |
| 4561 | } |
| 4562 | GRN_DB_OBJ_SET_TYPE(c, GRN_CURSOR_TABLE_HASH_KEY); |
| 4563 | c->hash = hash; |
| 4564 | c->ctx = ctx; |
| 4565 | c->obj.header.flags = (grn_obj_flags)flags; |
| 4566 | c->obj.header.domain = GRN_ID_NIL; |
| 4567 | if (flags & GRN_CURSOR_DESCENDING) { |
| 4568 | c->dir = -1; |
| 4569 | if (max) { |
| 4570 | if (!(c->curr_rec = grn_hash_get(ctx, hash, max, max_size, NULL))) { |
| 4571 | c->tail = GRN_ID_NIL; |
| 4572 | goto exit; |
| 4573 | } |
| 4574 | if (!(flags & GRN_CURSOR_LT)) { |
| 4575 | c->curr_rec++; |
| 4576 | } |
| 4577 | } else { |
| 4578 | c->curr_rec = HASH_CURR_MAX(hash) + 1; |
| 4579 | } |
| 4580 | if (min) { |
| 4581 | if (!(c->tail = grn_hash_get(ctx, hash, min, min_size, NULL))) { |
| 4582 | c->curr_rec = GRN_ID_NIL; |
| 4583 | goto exit; |
| 4584 | } |
| 4585 | if ((flags & GRN_CURSOR_GT)) { |
| 4586 | c->tail++; |
| 4587 | } |
| 4588 | } else { |
| 4589 | c->tail = GRN_ID_NIL + 1; |
| 4590 | } |
| 4591 | if (c->curr_rec < c->tail) { |
| 4592 | c->tail = c->curr_rec; |
| 4593 | } |
| 4594 | } else { |
| 4595 | c->dir = 1; |
| 4596 | if (min) { |
| 4597 | if (!(c->curr_rec = grn_hash_get(ctx, hash, min, min_size, NULL))) { |
| 4598 | c->tail = GRN_ID_NIL; |
| 4599 | goto exit; |
| 4600 | } |
| 4601 | if (!(flags & GRN_CURSOR_GT)) { |
| 4602 | c->curr_rec--; |
| 4603 | } |
| 4604 | } else { |
| 4605 | c->curr_rec = GRN_ID_NIL; |
| 4606 | } |
| 4607 | if (max) { |
| 4608 | if (!(c->tail = grn_hash_get(ctx, hash, max, max_size, NULL))) { |
| 4609 | c->curr_rec = GRN_ID_NIL; |
| 4610 | goto exit; |
| 4611 | } |
| 4612 | if ((flags & GRN_CURSOR_LT)) { |
| 4613 | c->tail--; |
| 4614 | } |
| 4615 | } else { |
| 4616 | c->tail = HASH_CURR_MAX(hash); |
| 4617 | } |
| 4618 | if (c->tail < c->curr_rec) { |
| 4619 | c->tail = c->curr_rec; |
| 4620 | } |
| 4621 | } |
| 4622 | if (*hash->n_entries != HASH_CURR_MAX(hash)) { |
| 4623 | while (offset && c->curr_rec != c->tail) { |
| 4624 | c->curr_rec = (grn_id)((int64_t)(c->curr_rec) + c->dir); |
| 4625 | if (grn_hash_bitmap_at(ctx, c->hash, c->curr_rec)) { |
| 4626 | offset--; |
| 4627 | } |
| 4628 | } |
| 4629 | } else { |
| 4630 | c->curr_rec = (grn_id)((int64_t)(c->curr_rec) + (c->dir * offset)); |
| 4631 | } |
| 4632 | exit: |
| 4633 | c->rest = (limit < 0) ? GRN_ARRAY_MAX : (unsigned int)limit; |
| 4634 | return c; |
| 4635 | } |
| 4636 | |
| 4637 | grn_id |
| 4638 | grn_hash_cursor_next(grn_ctx *ctx, grn_hash_cursor *c) |
| 4639 | { |
| 4640 | if (c && c->rest) { |
| 4641 | while (c->curr_rec != c->tail) { |
| 4642 | c->curr_rec = (grn_id)((int64_t)(c->curr_rec) + c->dir); |
| 4643 | if (*c->hash->n_entries != HASH_CURR_MAX(c->hash)) { |
| 4644 | if (!grn_hash_bitmap_at(ctx, c->hash, c->curr_rec)) { |
| 4645 | continue; |
| 4646 | } |
| 4647 | } |
| 4648 | c->rest--; |
| 4649 | return c->curr_rec; |
| 4650 | } |
| 4651 | } |
| 4652 | return GRN_ID_NIL; |
| 4653 | } |
| 4654 | |
| 4655 | grn_id |
| 4656 | grn_hash_next(grn_ctx *ctx, grn_hash *hash, grn_id id) |
| 4657 | { |
| 4658 | grn_id max = HASH_CURR_MAX(hash); |
| 4659 | while (++id <= max) { |
| 4660 | if (grn_hash_bitmap_at(ctx, hash, id)) { |
| 4661 | return id; |
| 4662 | } |
| 4663 | } |
| 4664 | return GRN_ID_NIL; |
| 4665 | } |
| 4666 | |
| 4667 | grn_id |
| 4668 | grn_hash_at(grn_ctx *ctx, grn_hash *hash, grn_id id) |
| 4669 | { |
| 4670 | return grn_hash_bitmap_at(ctx, hash, id) ? id : GRN_ID_NIL; |
| 4671 | } |
| 4672 | |
| 4673 | int |
| 4674 | grn_hash_cursor_get_key(grn_ctx *ctx, grn_hash_cursor *c, void **key) |
| 4675 | { |
| 4676 | int key_size; |
| 4677 | entry_str *ee; |
| 4678 | if (!c) { |
| 4679 | return 0; |
| 4680 | } |
| 4681 | ee = grn_hash_entry_at(ctx, c->hash, c->curr_rec, 0); |
| 4682 | if (!ee) { |
| 4683 | return 0; |
| 4684 | } |
| 4685 | key_size = (c->hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) |
| 4686 | ? ee->size |
| 4687 | : (int)(c->hash->key_size); |
| 4688 | *key = get_key(ctx, c->hash, ee); |
| 4689 | return key_size; |
| 4690 | } |
| 4691 | |
| 4692 | int |
| 4693 | grn_hash_cursor_get_value(grn_ctx *ctx, grn_hash_cursor *c, void **value) |
| 4694 | { |
| 4695 | void *v; |
| 4696 | entry_str *ee; |
| 4697 | if (!c) { |
| 4698 | return 0; |
| 4699 | } |
| 4700 | ee = grn_hash_entry_at(ctx, c->hash, c->curr_rec, 0); |
| 4701 | if (ee && (v = get_value(ctx, c->hash, ee))) { |
| 4702 | *value = v; |
| 4703 | return (int)(c->hash->value_size); |
| 4704 | } |
| 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | int |
| 4709 | grn_hash_cursor_get_key_value(grn_ctx *ctx, |
| 4710 | grn_hash_cursor *c, |
| 4711 | void **key, |
| 4712 | uint32_t *key_size, |
| 4713 | void **value) |
| 4714 | { |
| 4715 | entry_str *ee; |
| 4716 | if (!c) { |
| 4717 | return GRN_INVALID_ARGUMENT; |
| 4718 | } |
| 4719 | ee = grn_hash_entry_at(ctx, c->hash, c->curr_rec, 0); |
| 4720 | if (!ee) { |
| 4721 | return GRN_INVALID_ARGUMENT; |
| 4722 | } |
| 4723 | if (key_size) { |
| 4724 | *key_size = (c->hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) |
| 4725 | ? ee->size |
| 4726 | : c->hash->key_size; |
| 4727 | } |
| 4728 | if (key) { |
| 4729 | *key = get_key(ctx, c->hash, ee); |
| 4730 | } |
| 4731 | if (value) { |
| 4732 | *value = get_value(ctx, c->hash, ee); |
| 4733 | } |
| 4734 | return (int)(c->hash->value_size); |
| 4735 | } |
| 4736 | |
| 4737 | grn_rc |
| 4738 | grn_hash_cursor_set_value(grn_ctx *ctx, |
| 4739 | grn_hash_cursor *c, |
| 4740 | const void *value, |
| 4741 | int flags) |
| 4742 | { |
| 4743 | if (!c) { |
| 4744 | return GRN_INVALID_ARGUMENT; |
| 4745 | } |
| 4746 | return grn_hash_set_value(ctx, c->hash, c->curr_rec, value, flags); |
| 4747 | } |
| 4748 | |
| 4749 | grn_rc |
| 4750 | grn_hash_cursor_delete(grn_ctx *ctx, |
| 4751 | grn_hash_cursor *c, |
| 4752 | grn_table_delete_optarg *optarg) |
| 4753 | { |
| 4754 | if (!c) { |
| 4755 | return GRN_INVALID_ARGUMENT; |
| 4756 | } |
| 4757 | return grn_hash_delete_by_id(ctx, c->hash, c->curr_rec, optarg); |
| 4758 | } |
| 4759 | |
| 4760 | /* sort */ |
| 4761 | |
| 4762 | #define PREPARE_VAL(e, ep, es) \ |
| 4763 | do { \ |
| 4764 | if ((arg->flags & GRN_TABLE_SORT_BY_VALUE)) { \ |
| 4765 | ep = ((const uint8_t *)(get_value(ctx, hash, (entry_str *)(e)))); \ |
| 4766 | es = hash->value_size; \ |
| 4767 | } else { \ |
| 4768 | ep = ((const uint8_t *)(get_key(ctx, hash, (entry_str *)(e)))); \ |
| 4769 | es = ((hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) \ |
| 4770 | ? ((entry_str *)(e))->size \ |
| 4771 | : hash->key_size); \ |
| 4772 | } \ |
| 4773 | ep += arg->offset; \ |
| 4774 | es = (uint32_t)((int)es - arg->offset); \ |
| 4775 | } while (0) |
| 4776 | |
| 4777 | #define COMPARE_VAL_(ap, as, bp, bs) \ |
| 4778 | (arg->compar ? arg->compar(ctx, \ |
| 4779 | (grn_obj *)hash, \ |
| 4780 | (void *)(ap), \ |
| 4781 | as, \ |
| 4782 | (grn_obj *)hash, \ |
| 4783 | (void *)(bp), \ |
| 4784 | bs, \ |
| 4785 | arg->compar_arg) \ |
| 4786 | : ((arg->flags & GRN_TABLE_SORT_AS_NUMBER) \ |
| 4787 | ? ((arg->flags & GRN_TABLE_SORT_AS_UNSIGNED) \ |
| 4788 | ? ((arg->flags & GRN_TABLE_SORT_AS_INT64) \ |
| 4789 | ? *((uint64_t *)(ap)) > *((uint64_t *)(bp)) \ |
| 4790 | : *((uint32_t *)(ap)) > *((uint32_t *)(bp))) \ |
| 4791 | : ((arg->flags & GRN_TABLE_SORT_AS_INT64) \ |
| 4792 | ? *((int64_t *)(ap)) > *((int64_t *)(bp)) \ |
| 4793 | : *((int32_t *)(ap)) > *((int32_t *)(bp)))) \ |
| 4794 | : grn_str_greater(ap, as, bp, bs))) |
| 4795 | |
| 4796 | #define COMPARE_VAL(ap, as, bp, bs) \ |
| 4797 | ((dir) ? COMPARE_VAL_((bp), (bs), (ap), (as)) \ |
| 4798 | : COMPARE_VAL_((ap), (as), (bp), (bs))) |
| 4799 | |
| 4800 | inline static entry ** |
| 4801 | pack(grn_ctx *ctx, |
| 4802 | grn_hash *hash, |
| 4803 | entry **res, |
| 4804 | grn_table_sort_optarg *arg, |
| 4805 | int dir) |
| 4806 | { |
| 4807 | uint32_t n; |
| 4808 | uint32_t cs, es; |
| 4809 | const uint8_t *cp, *ep; |
| 4810 | entry **head, **tail, *e, *c; |
| 4811 | grn_id id, m = HASH_CURR_MAX(hash); |
| 4812 | for (id = m >> 1;; id = (id == m) ? 1 : id + 1) { |
| 4813 | if (grn_hash_bitmap_at(ctx, hash, id)) { |
| 4814 | break; |
| 4815 | } |
| 4816 | } |
| 4817 | c = grn_hash_entry_at(ctx, hash, id, 0); |
| 4818 | if (!c) { |
| 4819 | return NULL; |
| 4820 | } |
| 4821 | PREPARE_VAL(c, cp, cs); |
| 4822 | head = res; |
| 4823 | n = *hash->n_entries - 1; |
| 4824 | tail = res + n; |
| 4825 | while (n--) { |
| 4826 | do { |
| 4827 | id = (id == m) ? 1 : id + 1; |
| 4828 | } while (!grn_hash_bitmap_at(ctx, hash, id)); |
| 4829 | e = grn_hash_entry_at(ctx, hash, id, 0); |
| 4830 | if (!e) { |
| 4831 | return NULL; |
| 4832 | } |
| 4833 | PREPARE_VAL(e, ep, es); |
| 4834 | if (COMPARE_VAL(cp, cs, ep, es)) { |
| 4835 | *head++ = e; |
| 4836 | } else { |
| 4837 | *tail-- = e; |
| 4838 | } |
| 4839 | } |
| 4840 | *head = c; |
| 4841 | return *hash->n_entries > 2 ? head : NULL; |
| 4842 | } |
| 4843 | |
| 4844 | static inline void |
| 4845 | swap(entry **a, entry **b) |
| 4846 | { |
| 4847 | entry *c_ = *a; |
| 4848 | *a = *b; |
| 4849 | *b = c_; |
| 4850 | } |
| 4851 | |
| 4852 | #define SWAP(a, ap, as, b, bp, bs) \ |
| 4853 | do { \ |
| 4854 | const uint8_t *cp_ = ap; \ |
| 4855 | uint32_t cs_ = as; \ |
| 4856 | ap = bp; \ |
| 4857 | bp = cp_; \ |
| 4858 | as = bs; \ |
| 4859 | bs = cs_; \ |
| 4860 | swap(a, b); \ |
| 4861 | } while (0) |
| 4862 | |
| 4863 | inline static entry ** |
| 4864 | part(grn_ctx *ctx, |
| 4865 | entry **b, |
| 4866 | entry **e, |
| 4867 | grn_table_sort_optarg *arg, |
| 4868 | grn_hash *hash, |
| 4869 | int dir) |
| 4870 | { |
| 4871 | entry **c; |
| 4872 | const uint8_t *bp, *cp, *ep; |
| 4873 | uint32_t bs, cs, es; |
| 4874 | intptr_t d = e - b; |
| 4875 | PREPARE_VAL(*b, bp, bs); |
| 4876 | PREPARE_VAL(*e, ep, es); |
| 4877 | if (COMPARE_VAL(bp, bs, ep, es)) { |
| 4878 | SWAP(b, bp, bs, e, ep, es); |
| 4879 | } |
| 4880 | if (d < 2) { |
| 4881 | return NULL; |
| 4882 | } |
| 4883 | c = b + (d >> 1); |
| 4884 | PREPARE_VAL(*c, cp, cs); |
| 4885 | if (COMPARE_VAL(bp, bs, cp, cs)) { |
| 4886 | SWAP(b, bp, bs, c, cp, cs); |
| 4887 | } else { |
| 4888 | if (COMPARE_VAL(cp, cs, ep, es)) { |
| 4889 | SWAP(c, cp, cs, e, ep, es); |
| 4890 | } |
| 4891 | } |
| 4892 | if (d < 3) { |
| 4893 | return NULL; |
| 4894 | } |
| 4895 | b++; |
| 4896 | swap(b, c); |
| 4897 | c = b; |
| 4898 | PREPARE_VAL(*c, cp, cs); |
| 4899 | for (;;) { |
| 4900 | do { |
| 4901 | b++; |
| 4902 | PREPARE_VAL(*b, bp, bs); |
| 4903 | } while (COMPARE_VAL(cp, cs, bp, bs)); |
| 4904 | do { |
| 4905 | e--; |
| 4906 | PREPARE_VAL(*e, ep, es); |
| 4907 | } while (COMPARE_VAL(ep, es, cp, cs)); |
| 4908 | if (b >= e) { |
| 4909 | break; |
| 4910 | } |
| 4911 | SWAP(b, bp, bs, e, ep, es); |
| 4912 | } |
| 4913 | SWAP(c, cp, cs, e, ep, es); |
| 4914 | return e; |
| 4915 | } |
| 4916 | |
| 4917 | static void |
| 4918 | _sort(grn_ctx *ctx, |
| 4919 | entry **head, |
| 4920 | entry **tail, |
| 4921 | int limit, |
| 4922 | grn_table_sort_optarg *arg, |
| 4923 | grn_hash *hash, |
| 4924 | int dir) |
| 4925 | { |
| 4926 | entry **c; |
| 4927 | if (head < tail && (c = part(ctx, head, tail, arg, hash, dir))) { |
| 4928 | intptr_t rest = limit - 1 - (c - head); |
| 4929 | _sort(ctx, head, c - 1, limit, arg, hash, dir); |
| 4930 | if (rest > 0) { |
| 4931 | _sort(ctx, c + 1, tail, (int)rest, arg, hash, dir); |
| 4932 | } |
| 4933 | } |
| 4934 | } |
| 4935 | |
| 4936 | static void |
| 4937 | sort(grn_ctx *ctx, |
| 4938 | grn_hash *hash, |
| 4939 | entry **res, |
| 4940 | int limit, |
| 4941 | grn_table_sort_optarg *arg, |
| 4942 | int dir) |
| 4943 | { |
| 4944 | entry **c = pack(ctx, hash, res, arg, dir); |
| 4945 | if (c) { |
| 4946 | intptr_t rest = limit - 1 - (c - res); |
| 4947 | _sort(ctx, res, c - 1, limit, arg, hash, dir); |
| 4948 | if (rest > 0) { |
| 4949 | _sort(ctx, c + 1, res + *hash->n_entries - 1, (int)rest, arg, hash, dir); |
| 4950 | } |
| 4951 | } |
| 4952 | } |
| 4953 | |
| 4954 | typedef struct { |
| 4955 | grn_id id; |
| 4956 | int32_t v; |
| 4957 | } val32; |
| 4958 | |
| 4959 | #define PREPARE_VAL32(id, e, ep) \ |
| 4960 | do { \ |
| 4961 | (ep)->id = id; \ |
| 4962 | (ep)->v = \ |
| 4963 | (arg->flags & GRN_TABLE_SORT_BY_ID) \ |
| 4964 | ? (int32_t)id \ |
| 4965 | : (*((int32_t *)((byte *)((arg->flags & GRN_TABLE_SORT_BY_VALUE) \ |
| 4966 | ? get_value(ctx, hash, (e)) \ |
| 4967 | : get_key(ctx, hash, (e))) + \ |
| 4968 | arg->offset))); \ |
| 4969 | } while (0) |
| 4970 | |
| 4971 | #define COMPARE_VAL32_(ap, bp) \ |
| 4972 | (arg->compar ? arg->compar(ctx, \ |
| 4973 | (grn_obj *)hash, \ |
| 4974 | (void *)&(ap)->v, \ |
| 4975 | sizeof(uint32_t), \ |
| 4976 | (grn_obj *)hash, \ |
| 4977 | (void *)&(bp)->v, \ |
| 4978 | sizeof(uint32_t), \ |
| 4979 | arg->compar_arg) \ |
| 4980 | : ((arg->flags & GRN_TABLE_SORT_AS_NUMBER) \ |
| 4981 | ? ((arg->flags & GRN_TABLE_SORT_AS_UNSIGNED) \ |
| 4982 | ? *((uint32_t *)&(ap)->v) > *((uint32_t *)&(bp)->v) \ |
| 4983 | : *((int32_t *)&(ap)->v) > *((int32_t *)&(bp)->v)) \ |
| 4984 | : memcmp(&(ap)->v, &(bp)->v, sizeof(uint32_t)) > 0)) |
| 4985 | |
| 4986 | #define COMPARE_VAL32(ap, bp) \ |
| 4987 | ((dir) ? COMPARE_VAL32_((bp), (ap)) : COMPARE_VAL32_((ap), (bp))) |
| 4988 | |
| 4989 | inline static val32 * |
| 4990 | pack_val32( |
| 4991 | grn_ctx *ctx, grn_hash *hash, val32 *res, grn_table_sort_optarg *arg, int dir) |
| 4992 | { |
| 4993 | uint32_t n; |
| 4994 | entry_str *e, *c; |
| 4995 | val32 *head, *tail, cr, er; |
| 4996 | grn_id id, m = HASH_CURR_MAX(hash); |
| 4997 | for (id = m >> 1;; id = (id == m) ? 1 : id + 1) { |
| 4998 | if (grn_hash_bitmap_at(ctx, hash, id)) { |
| 4999 | break; |
| 5000 | } |
| 5001 | } |
| 5002 | c = grn_hash_entry_at(ctx, hash, id, 0); |
| 5003 | if (!c) { |
| 5004 | return NULL; |
| 5005 | } |
| 5006 | PREPARE_VAL32(id, c, &cr); |
| 5007 | head = res; |
| 5008 | n = *hash->n_entries - 1; |
| 5009 | tail = res + n; |
| 5010 | while (n--) { |
| 5011 | do { |
| 5012 | id = (id == m) ? 1 : id + 1; |
| 5013 | } while (!grn_hash_bitmap_at(ctx, hash, id)); |
| 5014 | e = grn_hash_entry_at(ctx, hash, id, 0); |
| 5015 | if (!e) { |
| 5016 | return NULL; |
| 5017 | } |
| 5018 | PREPARE_VAL32(id, e, &er); |
| 5019 | if (COMPARE_VAL32(&cr, &er)) { |
| 5020 | *head++ = er; |
| 5021 | } else { |
| 5022 | *tail-- = er; |
| 5023 | } |
| 5024 | } |
| 5025 | *head = cr; |
| 5026 | return *hash->n_entries > 2 ? head : NULL; |
| 5027 | } |
| 5028 | |
| 5029 | #define SWAP_VAL32(ap, bp) \ |
| 5030 | do { \ |
| 5031 | val32 cr_ = *ap; \ |
| 5032 | *ap = *bp; \ |
| 5033 | *bp = cr_; \ |
| 5034 | } while (0) |
| 5035 | |
| 5036 | inline static val32 * |
| 5037 | part_val32(grn_ctx *ctx, |
| 5038 | val32 *b, |
| 5039 | val32 *e, |
| 5040 | grn_table_sort_optarg *arg, |
| 5041 | grn_hash *hash, |
| 5042 | int dir) |
| 5043 | { |
| 5044 | val32 *c; |
| 5045 | intptr_t d = e - b; |
| 5046 | if (COMPARE_VAL32(b, e)) { |
| 5047 | SWAP_VAL32(b, e); |
| 5048 | } |
| 5049 | if (d < 2) { |
| 5050 | return NULL; |
| 5051 | } |
| 5052 | c = b + (d >> 1); |
| 5053 | if (COMPARE_VAL32(b, c)) { |
| 5054 | SWAP_VAL32(b, c); |
| 5055 | } else { |
| 5056 | if (COMPARE_VAL32(c, e)) { |
| 5057 | SWAP_VAL32(c, e); |
| 5058 | } |
| 5059 | } |
| 5060 | if (d < 3) { |
| 5061 | return NULL; |
| 5062 | } |
| 5063 | b++; |
| 5064 | SWAP_VAL32(b, c); |
| 5065 | c = b; |
| 5066 | for (;;) { |
| 5067 | do { |
| 5068 | b++; |
| 5069 | } while (COMPARE_VAL32(c, b)); |
| 5070 | do { |
| 5071 | e--; |
| 5072 | } while (COMPARE_VAL32(e, c)); |
| 5073 | if (b >= e) { |
| 5074 | break; |
| 5075 | } |
| 5076 | SWAP_VAL32(b, e); |
| 5077 | } |
| 5078 | SWAP_VAL32(c, e); |
| 5079 | return e; |
| 5080 | } |
| 5081 | |
| 5082 | static void |
| 5083 | _sort_val32(grn_ctx *ctx, |
| 5084 | val32 *head, |
| 5085 | val32 *tail, |
| 5086 | int limit, |
| 5087 | grn_table_sort_optarg *arg, |
| 5088 | grn_hash *hash, |
| 5089 | int dir) |
| 5090 | { |
| 5091 | val32 *c; |
| 5092 | if (head < tail && (c = part_val32(ctx, head, tail, arg, hash, dir))) { |
| 5093 | intptr_t rest = limit - 1 - (c - head); |
| 5094 | _sort_val32(ctx, head, c - 1, limit, arg, hash, dir); |
| 5095 | if (rest > 0) { |
| 5096 | _sort_val32(ctx, c + 1, tail, (int)rest, arg, hash, dir); |
| 5097 | } |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | static void |
| 5102 | sort_val32(grn_ctx *ctx, |
| 5103 | grn_hash *hash, |
| 5104 | val32 *res, |
| 5105 | int limit, |
| 5106 | grn_table_sort_optarg *arg, |
| 5107 | int dir) |
| 5108 | { |
| 5109 | val32 *c = pack_val32(ctx, hash, res, arg, dir); |
| 5110 | if (c) { |
| 5111 | intptr_t rest = limit - 1 - (c - res); |
| 5112 | _sort_val32(ctx, res, c - 1, limit, arg, hash, dir); |
| 5113 | if (rest > 0) { |
| 5114 | _sort_val32(ctx, |
| 5115 | c + 1, |
| 5116 | res + *hash->n_entries - 1, |
| 5117 | (int)rest, |
| 5118 | arg, |
| 5119 | hash, |
| 5120 | dir); |
| 5121 | } |
| 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | static inline grn_id |
| 5126 | entry2id(grn_ctx *ctx, grn_hash *hash, entry *e) |
| 5127 | { |
| 5128 | entry *e2; |
| 5129 | grn_id id, *ep; |
| 5130 | uint32_t i, h = e->key, s = grn_hash_calculate_step(h); |
| 5131 | for (i = h;; i += s) { |
| 5132 | if (!(ep = grn_hash_idx_at(ctx, hash, i))) { |
| 5133 | return GRN_ID_NIL; |
| 5134 | } |
| 5135 | if (!(id = *ep)) { |
| 5136 | break; |
| 5137 | } |
| 5138 | if (id != GARBAGE) { |
| 5139 | e2 = grn_hash_entry_at(ctx, hash, id, 0); |
| 5140 | if (!e2) { |
| 5141 | return GRN_ID_NIL; |
| 5142 | } |
| 5143 | if (e2 == e) { |
| 5144 | break; |
| 5145 | } |
| 5146 | } |
| 5147 | } |
| 5148 | return id; |
| 5149 | } |
| 5150 | |
| 5151 | int |
| 5152 | grn_hash_sort(grn_ctx *ctx, |
| 5153 | grn_hash *hash, |
| 5154 | int limit, |
| 5155 | grn_array *result, |
| 5156 | grn_table_sort_optarg *optarg) |
| 5157 | { |
| 5158 | entry **res; |
| 5159 | if (!result || !*hash->n_entries) { |
| 5160 | return 0; |
| 5161 | } |
| 5162 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 5163 | return 0; |
| 5164 | } |
| 5165 | if (!(res = GRN_MALLOC(sizeof(entry *) * *hash->n_entries))) { |
| 5166 | GRN_LOG(ctx, |
| 5167 | GRN_LOG_ALERT, |
| 5168 | "allocation of entries failed on grn_hash_sort !"); |
| 5169 | return 0; |
| 5170 | } |
| 5171 | if (limit < 0) { |
| 5172 | limit = (int)(limit + (int64_t)(*hash->n_entries) + 1); |
| 5173 | if (limit < 0) { |
| 5174 | GRN_LOG(ctx, GRN_LOG_ALERT, "limit is too small in grn_hash_sort !"); |
| 5175 | return 0; |
| 5176 | } |
| 5177 | } |
| 5178 | if ((uint32_t)limit > *hash->n_entries) { |
| 5179 | limit = (int)(*hash->n_entries); |
| 5180 | } |
| 5181 | /* hash->limit = limit; */ |
| 5182 | if (optarg) { |
| 5183 | int dir = (optarg->flags & GRN_TABLE_SORT_DESC); |
| 5184 | if ((optarg->flags & GRN_TABLE_SORT_BY_ID) || |
| 5185 | (optarg->flags & GRN_TABLE_SORT_BY_VALUE) |
| 5186 | ? (((int)(hash->value_size) - optarg->offset) == sizeof(uint32_t)) |
| 5187 | : (!(hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) && |
| 5188 | hash->key_size == sizeof(uint32_t))) { |
| 5189 | if (sizeof(entry *) != sizeof(val32)) { |
| 5190 | GRN_FREE(res); |
| 5191 | if (!(res = GRN_MALLOC(sizeof(val32) * *hash->n_entries))) { |
| 5192 | GRN_LOG(ctx, |
| 5193 | GRN_LOG_ALERT, |
| 5194 | "allocation of entries failed on grn_hash_sort !"); |
| 5195 | return 0; |
| 5196 | } |
| 5197 | } |
| 5198 | sort_val32(ctx, hash, (val32 *)res, limit, optarg, dir); |
| 5199 | { |
| 5200 | int i; |
| 5201 | grn_id *v; |
| 5202 | val32 *rp = (val32 *)res; |
| 5203 | for (i = 0; i < limit; i++, rp++) { |
| 5204 | if (!grn_array_add(ctx, result, (void **)&v)) { |
| 5205 | break; |
| 5206 | } |
| 5207 | if (!(*v = rp->id)) { |
| 5208 | break; |
| 5209 | } |
| 5210 | } |
| 5211 | GRN_FREE(res); |
| 5212 | return i; |
| 5213 | } |
| 5214 | } else { |
| 5215 | sort(ctx, hash, res, limit, optarg, dir); |
| 5216 | } |
| 5217 | } else { |
| 5218 | grn_table_sort_optarg opt = {0, NULL, NULL, NULL, 0}; |
| 5219 | sort(ctx, hash, res, limit, &opt, 0); |
| 5220 | } |
| 5221 | { |
| 5222 | int i; |
| 5223 | grn_id *v; |
| 5224 | entry **rp = res; |
| 5225 | for (i = 0; i < limit; i++, rp++) { |
| 5226 | if (!grn_array_add(ctx, result, (void **)&v)) { |
| 5227 | break; |
| 5228 | } |
| 5229 | if (!(*v = entry2id(ctx, hash, *rp))) { |
| 5230 | break; |
| 5231 | } |
| 5232 | } |
| 5233 | GRN_FREE(res); |
| 5234 | return i; |
| 5235 | } |
| 5236 | } |
| 5237 | |
| 5238 | void |
| 5239 | grn_hash_check(grn_ctx *ctx, grn_hash *hash) |
| 5240 | { |
| 5241 | char buf[8]; |
| 5242 | grn_hash_header_common *h = hash->header.common; |
| 5243 | if (grn_hash_error_if_truncated(ctx, hash) != GRN_SUCCESS) { |
| 5244 | return; |
| 5245 | } |
| 5246 | GRN_OUTPUT_ARRAY_OPEN("RESULT", 1); |
| 5247 | GRN_OUTPUT_MAP_OPEN("SUMMARY", 26); |
| 5248 | GRN_OUTPUT_CSTR("flags"); |
| 5249 | grn_itoh(h->flags, buf, 8); |
| 5250 | GRN_OUTPUT_STR(buf, 8); |
| 5251 | GRN_OUTPUT_CSTR("key_size"); |
| 5252 | GRN_OUTPUT_INT64(hash->key_size); |
| 5253 | GRN_OUTPUT_CSTR("value_size"); |
| 5254 | GRN_OUTPUT_INT64(hash->value_size); |
| 5255 | GRN_OUTPUT_CSTR("tokenizer"); |
| 5256 | GRN_OUTPUT_INT64(h->tokenizer); |
| 5257 | GRN_OUTPUT_CSTR("normalizer"); |
| 5258 | GRN_OUTPUT_INT64(h->normalizer); |
| 5259 | GRN_OUTPUT_CSTR("curr_rec"); |
| 5260 | GRN_OUTPUT_INT64(h->curr_rec); |
| 5261 | GRN_OUTPUT_CSTR("curr_key_normal"); |
| 5262 | GRN_OUTPUT_UINT64(h->curr_key_normal); |
| 5263 | GRN_OUTPUT_CSTR("curr_key_large"); |
| 5264 | GRN_OUTPUT_UINT64(h->curr_key_large); |
| 5265 | GRN_OUTPUT_CSTR("idx_offset"); |
| 5266 | GRN_OUTPUT_INT64(h->idx_offset); |
| 5267 | GRN_OUTPUT_CSTR("entry_size"); |
| 5268 | GRN_OUTPUT_INT64(hash->entry_size); |
| 5269 | GRN_OUTPUT_CSTR("max_offset"); |
| 5270 | GRN_OUTPUT_INT64(*hash->max_offset); |
| 5271 | GRN_OUTPUT_CSTR("n_entries"); |
| 5272 | GRN_OUTPUT_INT64(*hash->n_entries); |
| 5273 | GRN_OUTPUT_CSTR("n_garbages"); |
| 5274 | GRN_OUTPUT_INT64(*hash->n_garbages); |
| 5275 | GRN_OUTPUT_CSTR("lock"); |
| 5276 | GRN_OUTPUT_INT64(h->lock); |
| 5277 | GRN_OUTPUT_MAP_CLOSE(); |
| 5278 | GRN_OUTPUT_ARRAY_CLOSE(); |
| 5279 | } |
| 5280 | |
| 5281 | /* rhash : grn_hash with subrecs */ |
| 5282 | |
| 5283 | #ifdef USE_GRN_INDEX2 |
| 5284 | |
| 5285 | static uint32_t default_flags = GRN_HASH_TINY; |
| 5286 | |
| 5287 | grn_rc |
| 5288 | grn_rhash_init(grn_ctx *ctx, |
| 5289 | grn_hash *hash, |
| 5290 | grn_rec_unit record_unit, |
| 5291 | int record_size, |
| 5292 | grn_rec_unit subrec_unit, |
| 5293 | int subrec_size, |
| 5294 | unsigned int max_n_subrecs) |
| 5295 | { |
| 5296 | grn_rc rc; |
| 5297 | record_size = grn_rec_unit_size(record_unit, record_size); |
| 5298 | subrec_size = grn_rec_unit_size(subrec_unit, subrec_size); |
| 5299 | if (record_unit != GRN_REC_USERDEF && subrec_unit != GRN_REC_USERDEF) { |
| 5300 | subrec_size -= record_size; |
| 5301 | } |
| 5302 | if (!hash) { |
| 5303 | return GRN_INVALID_ARGUMENT; |
| 5304 | } |
| 5305 | if (record_size < 0) { |
| 5306 | return GRN_INVALID_ARGUMENT; |
| 5307 | } |
| 5308 | if ((default_flags & GRN_HASH_TINY)) { |
| 5309 | rc = grn_tiny_hash_init(ctx, |
| 5310 | hash, |
| 5311 | NULL, |
| 5312 | record_size, |
| 5313 | max_n_subrecs * (GRN_RSET_SCORE_SIZE + subrec_size), |
| 5314 | default_flags, |
| 5315 | GRN_ENC_NONE); |
| 5316 | } else { |
| 5317 | rc = grn_io_hash_init(ctx, |
| 5318 | hash, |
| 5319 | NULL, |
| 5320 | record_size, |
| 5321 | max_n_subrecs * (GRN_RSET_SCORE_SIZE + subrec_size), |
| 5322 | default_flags, |
| 5323 | GRN_ENC_NONE, |
| 5324 | 0); |
| 5325 | } |
| 5326 | if (rc) { |
| 5327 | return rc; |
| 5328 | } |
| 5329 | hash->record_unit = record_unit; |
| 5330 | hash->subrec_unit = subrec_unit; |
| 5331 | hash->subrec_size = subrec_size; |
| 5332 | hash->max_n_subrecs = max_n_subrecs; |
| 5333 | return rc; |
| 5334 | } |
| 5335 | |
| 5336 | grn_rc |
| 5337 | grn_rhash_fin(grn_ctx *ctx, grn_hash *hash) |
| 5338 | { |
| 5339 | grn_rc rc; |
| 5340 | if (grn_hash_is_io_hash(hash)) { |
| 5341 | rc = grn_io_close(ctx, hash->io); |
| 5342 | } else { |
| 5343 | GRN_ASSERT(ctx == hash->ctx); |
| 5344 | rc = grn_tiny_hash_fin(ctx, hash); |
| 5345 | } |
| 5346 | return rc; |
| 5347 | } |
| 5348 | |
| 5349 | static inline void |
| 5350 | subrecs_push( |
| 5351 | byte *subrecs, int size, int n_subrecs, int score, void *body, int dir) |
| 5352 | { |
| 5353 | byte *v; |
| 5354 | int *c2; |
| 5355 | int n = n_subrecs - 1, n2; |
| 5356 | while (n) { |
| 5357 | n2 = (n - 1) >> 1; |
| 5358 | c2 = GRN_RSET_SUBRECS_NTH(subrecs, size, n2); |
| 5359 | if (GRN_RSET_SUBRECS_CMP(score, *c2, dir) > 0) { |
| 5360 | break; |
| 5361 | } |
| 5362 | GRN_RSET_SUBRECS_COPY(subrecs, size, n, c2); |
| 5363 | n = n2; |
| 5364 | } |
| 5365 | v = subrecs + n * (size + GRN_RSET_SCORE_SIZE); |
| 5366 | *((int *)v) = score; |
| 5367 | grn_memcpy(v + GRN_RSET_SCORE_SIZE, body, size); |
| 5368 | } |
| 5369 | |
| 5370 | static inline void |
| 5371 | subrecs_replace_min( |
| 5372 | byte *subrecs, int size, int n_subrecs, int score, void *body, int dir) |
| 5373 | { |
| 5374 | byte *v; |
| 5375 | int n = 0, n1, n2, *c1, *c2; |
| 5376 | for (;;) { |
| 5377 | n1 = n * 2 + 1; |
| 5378 | n2 = n1 + 1; |
| 5379 | c1 = n1 < n_subrecs ? GRN_RSET_SUBRECS_NTH(subrecs, size, n1) : NULL; |
| 5380 | c2 = n2 < n_subrecs ? GRN_RSET_SUBRECS_NTH(subrecs, size, n2) : NULL; |
| 5381 | if (c1 && GRN_RSET_SUBRECS_CMP(score, *c1, dir) > 0) { |
| 5382 | if (c2 && GRN_RSET_SUBRECS_CMP(score, *c2, dir) > 0 && |
| 5383 | GRN_RSET_SUBRECS_CMP(*c1, *c2, dir) > 0) { |
| 5384 | GRN_RSET_SUBRECS_COPY(subrecs, size, n, c2); |
| 5385 | n = n2; |
| 5386 | } else { |
| 5387 | GRN_RSET_SUBRECS_COPY(subrecs, size, n, c1); |
| 5388 | n = n1; |
| 5389 | } |
| 5390 | } else { |
| 5391 | if (c2 && GRN_RSET_SUBRECS_CMP(score, *c2, dir) > 0) { |
| 5392 | GRN_RSET_SUBRECS_COPY(subrecs, size, n, c2); |
| 5393 | n = n2; |
| 5394 | } else { |
| 5395 | break; |
| 5396 | } |
| 5397 | } |
| 5398 | } |
| 5399 | v = subrecs + n * (size + GRN_RSET_SCORE_SIZE); |
| 5400 | grn_memcpy(v, &score, GRN_RSET_SCORE_SIZE); |
| 5401 | grn_memcpy(v + GRN_RSET_SCORE_SIZE, body, size); |
| 5402 | } |
| 5403 | |
| 5404 | void |
| 5405 | grn_rhash_add_subrec( |
| 5406 | grn_hash *s, grn_rset_recinfo *ri, int score, void *body, int dir) |
| 5407 | { |
| 5408 | int limit = s->max_n_subrecs; |
| 5409 | ri->score += score; |
| 5410 | ri->n_subrecs += 1; |
| 5411 | if (limit) { |
| 5412 | int ssize = s->subrec_size; |
| 5413 | int n_subrecs = GRN_RSET_N_SUBRECS(ri); |
| 5414 | if (limit < n_subrecs) { |
| 5415 | if (GRN_RSET_SUBRECS_CMP(score, *ri->subrecs, dir) > 0) { |
| 5416 | subrecs_replace_min(ri->subrecs, ssize, limit, score, body, dir); |
| 5417 | } |
| 5418 | } else { |
| 5419 | subrecs_push(ri->subrecs, ssize, n_subrecs, score, body, dir); |
| 5420 | } |
| 5421 | } |
| 5422 | } |
| 5423 | |
| 5424 | grn_hash * |
| 5425 | grn_rhash_group(grn_hash *s, int limit, grn_group_optarg *optarg) |
| 5426 | { |
| 5427 | grn_ctx *ctx = s->ctx; |
| 5428 | grn_hash *g, h; |
| 5429 | grn_rset_recinfo *ri; |
| 5430 | grn_rec_unit unit; |
| 5431 | grn_hash_cursor *c; |
| 5432 | grn_id rh; |
| 5433 | byte *key, *ekey, *gkey = NULL; |
| 5434 | int funcp, dir; |
| 5435 | unsigned int rsize; |
| 5436 | if (!s || !s->index) { |
| 5437 | return NULL; |
| 5438 | } |
| 5439 | if (optarg) { |
| 5440 | unit = GRN_REC_USERDEF; |
| 5441 | rsize = optarg->key_size; |
| 5442 | funcp = optarg->func ? 1 : 0; |
| 5443 | dir = (optarg->mode == grn_sort_ascending) ? -1 : 1; |
| 5444 | } else { |
| 5445 | unit = GRN_REC_DOCUMENT; |
| 5446 | rsize = grn_rec_unit_size(unit, sizeof(grn_id)); |
| 5447 | funcp = 0; |
| 5448 | dir = 1; |
| 5449 | } |
| 5450 | if (funcp) { |
| 5451 | gkey = GRN_MALLOC(rsize ? rsize : 8192); |
| 5452 | if (!gkey) { |
| 5453 | GRN_LOG(ctx, GRN_LOG_ALERT, "allocation for gkey failed !"); |
| 5454 | return NULL; |
| 5455 | } |
| 5456 | } else { |
| 5457 | if (s->key_size <= rsize) { |
| 5458 | return NULL; |
| 5459 | } |
| 5460 | } |
| 5461 | if (!(c = grn_hash_cursor_open(s->ctx, s, NULL, 0, NULL, -1, 0))) { |
| 5462 | GRN_LOG(ctx, |
| 5463 | GRN_LOG_ALERT, |
| 5464 | "grn_hash_cursor_open on grn_hash_group failed !"); |
| 5465 | if (gkey) { |
| 5466 | GRN_FREE(gkey); |
| 5467 | } |
| 5468 | return NULL; |
| 5469 | } |
| 5470 | grn_memcpy(&h, s, sizeof(grn_hash)); |
| 5471 | g = s; |
| 5472 | s = &h; |
| 5473 | if (grn_rhash_init(ctx, g, unit, rsize, s->record_unit, s->key_size, limit)) { |
| 5474 | GRN_LOG(ctx, GRN_LOG_ALERT, "grn_rhash_init in grn_hash_group failed !"); |
| 5475 | grn_hash_cursor_close(s->ctx, c); |
| 5476 | if (gkey) { |
| 5477 | GRN_FREE(gkey); |
| 5478 | } |
| 5479 | return NULL; |
| 5480 | } |
| 5481 | while ((rh = grn_hash_cursor_next(ctx, c))) { |
| 5482 | grn_hash_cursor_get_key_value(ctx, c, (void **)&key, NULL, (void **)&ri); |
| 5483 | if (funcp) { |
| 5484 | if (optarg->func((grn_records *)s, |
| 5485 | (grn_recordh *)(intptr_t)rh, |
| 5486 | gkey, |
| 5487 | optarg->func_arg)) { |
| 5488 | continue; |
| 5489 | } |
| 5490 | ekey = key; |
| 5491 | } else { |
| 5492 | gkey = key; |
| 5493 | ekey = key + rsize; |
| 5494 | } |
| 5495 | { |
| 5496 | grn_rset_recinfo *gri; |
| 5497 | if (grn_hash_add(ctx, g, gkey, rsize, (void **)&gri, NULL)) { |
| 5498 | grn_rhash_add_subrec(g, gri, ri->score, ekey, dir); |
| 5499 | } |
| 5500 | } |
| 5501 | } |
| 5502 | grn_hash_cursor_close(s->ctx, c); |
| 5503 | grn_rhash_fin(s->ctx, s); |
| 5504 | if (funcp) { |
| 5505 | GRN_FREE(gkey); |
| 5506 | } |
| 5507 | return g; |
| 5508 | } |
| 5509 | |
| 5510 | grn_rc |
| 5511 | grn_rhash_subrec_info(grn_ctx *ctx, |
| 5512 | grn_hash *s, |
| 5513 | grn_id rh, |
| 5514 | int index, |
| 5515 | grn_id *rid, |
| 5516 | int *section, |
| 5517 | int *pos, |
| 5518 | int *score, |
| 5519 | void **subrec) |
| 5520 | { |
| 5521 | grn_rset_posinfo *pi; |
| 5522 | grn_rset_recinfo *ri; |
| 5523 | int *p, unit_size = GRN_RSET_SCORE_SIZE + s->subrec_size; |
| 5524 | if (!s || !rh || index < 0) { |
| 5525 | return GRN_INVALID_ARGUMENT; |
| 5526 | } |
| 5527 | if ((unsigned int)index >= s->max_n_subrecs) { |
| 5528 | return GRN_INVALID_ARGUMENT; |
| 5529 | } |
| 5530 | { |
| 5531 | entry_str *ee; |
| 5532 | if (!grn_hash_bitmap_at(ctx, s, rh)) { |
| 5533 | return GRN_INVALID_ARGUMENT; |
| 5534 | } |
| 5535 | ee = grn_hash_entry_at(ctx, s, rh, 0); |
| 5536 | if (!ee) { |
| 5537 | return GRN_INVALID_ARGUMENT; |
| 5538 | } |
| 5539 | pi = (grn_rset_posinfo *)get_key(ctx, s, ee); |
| 5540 | ri = get_value(ctx, s, ee); |
| 5541 | if (!pi || !ri) { |
| 5542 | return GRN_INVALID_ARGUMENT; |
| 5543 | } |
| 5544 | } |
| 5545 | if (index >= ri->n_subrecs) { |
| 5546 | return GRN_INVALID_ARGUMENT; |
| 5547 | } |
| 5548 | p = (int *)(ri->subrecs + index * unit_size); |
| 5549 | if (score) { |
| 5550 | *score = p[0]; |
| 5551 | } |
| 5552 | if (subrec) { |
| 5553 | *subrec = &p[1]; |
| 5554 | } |
| 5555 | switch (s->record_unit) { |
| 5556 | case GRN_REC_DOCUMENT: |
| 5557 | if (rid) { |
| 5558 | *rid = pi->rid; |
| 5559 | } |
| 5560 | if (section) { |
| 5561 | *section = (s->subrec_unit != GRN_REC_USERDEF) ? p[1] : 0; |
| 5562 | } |
| 5563 | if (pos) { |
| 5564 | *pos = (s->subrec_unit == GRN_REC_POSITION) ? p[2] : 0; |
| 5565 | } |
| 5566 | break; |
| 5567 | case GRN_REC_SECTION: |
| 5568 | if (rid) { |
| 5569 | *rid = pi->rid; |
| 5570 | } |
| 5571 | if (section) { |
| 5572 | *section = pi->sid; |
| 5573 | } |
| 5574 | if (pos) { |
| 5575 | *pos = (s->subrec_unit == GRN_REC_POSITION) ? p[1] : 0; |
| 5576 | } |
| 5577 | break; |
| 5578 | default: |
| 5579 | pi = (grn_rset_posinfo *)&p[1]; |
| 5580 | switch (s->subrec_unit) { |
| 5581 | case GRN_REC_DOCUMENT: |
| 5582 | if (rid) { |
| 5583 | *rid = pi->rid; |
| 5584 | } |
| 5585 | if (section) { |
| 5586 | *section = 0; |
| 5587 | } |
| 5588 | if (pos) { |
| 5589 | *pos = 0; |
| 5590 | } |
| 5591 | break; |
| 5592 | case GRN_REC_SECTION: |
| 5593 | if (rid) { |
| 5594 | *rid = pi->rid; |
| 5595 | } |
| 5596 | if (section) { |
| 5597 | *section = pi->sid; |
| 5598 | } |
| 5599 | if (pos) { |
| 5600 | *pos = 0; |
| 5601 | } |
| 5602 | break; |
| 5603 | case GRN_REC_POSITION: |
| 5604 | if (rid) { |
| 5605 | *rid = pi->rid; |
| 5606 | } |
| 5607 | if (section) { |
| 5608 | *section = pi->sid; |
| 5609 | } |
| 5610 | if (pos) { |
| 5611 | *pos = pi->pos; |
| 5612 | } |
| 5613 | break; |
| 5614 | default: |
| 5615 | if (rid) { |
| 5616 | *rid = 0; |
| 5617 | } |
| 5618 | if (section) { |
| 5619 | *section = 0; |
| 5620 | } |
| 5621 | if (pos) { |
| 5622 | *pos = 0; |
| 5623 | } |
| 5624 | break; |
| 5625 | } |
| 5626 | break; |
| 5627 | } |
| 5628 | return GRN_SUCCESS; |
| 5629 | } |
| 5630 | #endif /* USE_GRN_INDEX2 */ |
| 5631 | |
| 5632 | bool |
| 5633 | grn_hash_is_large_total_key_size(grn_ctx *ctx, grn_hash *hash) |
| 5634 | { |
| 5635 | return (hash->header.common->flags & GRN_OBJ_KEY_LARGE) == GRN_OBJ_KEY_LARGE; |
| 5636 | } |
| 5637 | |
| 5638 | uint64_t |
| 5639 | grn_hash_total_key_size(grn_ctx *ctx, grn_hash *hash) |
| 5640 | { |
| 5641 | if (grn_hash_is_large_total_key_size(ctx, hash)) { |
| 5642 | return hash->header.common->curr_key_large; |
| 5643 | } else { |
| 5644 | return hash->header.common->curr_key_normal; |
| 5645 | } |
| 5646 | } |
| 5647 | |
| 5648 | uint64_t |
| 5649 | grn_hash_max_total_key_size(grn_ctx *ctx, grn_hash *hash) |
| 5650 | { |
| 5651 | if (grn_hash_is_large_total_key_size(ctx, hash)) { |
| 5652 | return GRN_HASH_KEY_MAX_TOTAL_SIZE_LARGE; |
| 5653 | } else { |
| 5654 | return GRN_HASH_KEY_MAX_TOTAL_SIZE_NORMAL; |
| 5655 | } |
| 5656 | } |
| 5657 | |
| 5658 | grn_rc |
| 5659 | grn_hash_wal_recover(grn_ctx *ctx, grn_hash *hash) |
| 5660 | { |
| 5661 | if (ctx->rc != GRN_SUCCESS) { |
| 5662 | return ctx->rc; |
| 5663 | } |
| 5664 | |
| 5665 | const char *wal_error_tag = "[hash]"; |
| 5666 | const char *tag = "[hash][recover]"; |
| 5667 | grn_wal_reader *reader = grn_wal_reader_open(ctx, (grn_obj *)hash, tag); |
| 5668 | if (ctx->rc != GRN_SUCCESS) { |
| 5669 | return ctx->rc; |
| 5670 | } |
| 5671 | |
| 5672 | bool need_flush = false; |
| 5673 | if (grn_io_is_locked(hash->io)) { |
| 5674 | grn_io_clear_lock(hash->io); |
| 5675 | need_flush = true; |
| 5676 | } |
| 5677 | |
| 5678 | if (reader) { |
| 5679 | grn_id partial_record_id = GRN_ID_NIL; |
| 5680 | while (true) { |
| 5681 | grn_wal_reader_entry entry = {0}; |
| 5682 | grn_rc rc = grn_wal_reader_read_entry(ctx, reader, &entry); |
| 5683 | if (rc != GRN_SUCCESS) { |
| 5684 | break; |
| 5685 | } |
| 5686 | switch (entry.event) { |
| 5687 | case GRN_WAL_EVENT_ADD_ENTRY: |
| 5688 | { |
| 5689 | grn_id *index = grn_hash_idx_at(ctx, hash, entry.index_hash_value); |
| 5690 | if (!index) { |
| 5691 | grn_wal_set_recover_error(ctx, |
| 5692 | GRN_NO_MEMORY_AVAILABLE, |
| 5693 | (grn_obj *)hash, |
| 5694 | &entry, |
| 5695 | wal_error_tag, |
| 5696 | "failed to refer index"); |
| 5697 | break; |
| 5698 | } |
| 5699 | *(hash->n_entries) = entry.n_entries; |
| 5700 | if (grn_io_hash_add_entry(ctx, hash, entry.record_id, index, tag)) { |
| 5701 | partial_record_id = entry.record_id; |
| 5702 | } |
| 5703 | } |
| 5704 | break; |
| 5705 | case GRN_WAL_EVENT_REUSE_ENTRY: |
| 5706 | { |
| 5707 | grn_id *index = grn_hash_idx_at(ctx, hash, entry.index_hash_value); |
| 5708 | if (!index) { |
| 5709 | grn_wal_set_recover_error(ctx, |
| 5710 | GRN_NO_MEMORY_AVAILABLE, |
| 5711 | (grn_obj *)hash, |
| 5712 | &entry, |
| 5713 | wal_error_tag, |
| 5714 | "failed to refer index"); |
| 5715 | break; |
| 5716 | } |
| 5717 | *(hash->n_garbages) = entry.n_garbages; |
| 5718 | *(hash->n_entries) = entry.n_entries; |
| 5719 | if (grn_io_hash_reuse_entry(ctx, |
| 5720 | hash, |
| 5721 | entry.record_id, |
| 5722 | (uint32_t)(entry.key.content.uint64), |
| 5723 | index, |
| 5724 | entry.found_garbage, |
| 5725 | tag)) { |
| 5726 | partial_record_id = entry.record_id; |
| 5727 | } |
| 5728 | } |
| 5729 | break; |
| 5730 | case GRN_WAL_EVENT_RESET_ENTRY: |
| 5731 | { |
| 5732 | grn_hash_entry *hash_entry = |
| 5733 | grn_io_hash_entry_at(ctx, hash, entry.record_id, GRN_TABLE_ADD); |
| 5734 | if (!hash_entry) { |
| 5735 | grn_wal_set_recover_error(ctx, |
| 5736 | GRN_NO_MEMORY_AVAILABLE, |
| 5737 | (grn_obj *)hash, |
| 5738 | &entry, |
| 5739 | wal_error_tag, |
| 5740 | "failed to refer hash entry"); |
| 5741 | break; |
| 5742 | } |
| 5743 | grn_io_hash_reset_entry(ctx, |
| 5744 | hash, |
| 5745 | hash_entry, |
| 5746 | entry.next_garbage_record_id, |
| 5747 | (uint32_t)(entry.key.content.uint64), |
| 5748 | tag); |
| 5749 | } |
| 5750 | break; |
| 5751 | case GRN_WAL_EVENT_ENABLE_ENTRY: |
| 5752 | grn_io_hash_enable_entry(ctx, hash, entry.record_id, tag); |
| 5753 | break; |
| 5754 | case GRN_WAL_EVENT_SET_ENTRY_KEY: |
| 5755 | { |
| 5756 | grn_hash_entry *hash_entry = |
| 5757 | grn_io_hash_entry_at(ctx, hash, entry.record_id, GRN_TABLE_ADD); |
| 5758 | if (!hash_entry) { |
| 5759 | grn_wal_set_recover_error(ctx, |
| 5760 | GRN_NO_MEMORY_AVAILABLE, |
| 5761 | (grn_obj *)hash, |
| 5762 | &entry, |
| 5763 | wal_error_tag, |
| 5764 | "failed to refer hash entry"); |
| 5765 | break; |
| 5766 | } |
| 5767 | if (grn_hash_is_large_total_key_size(ctx, hash)) { |
| 5768 | hash->header.common->curr_key_large = entry.key_offset; |
| 5769 | } else { |
| 5770 | hash->header.common->curr_key_normal = (uint32_t)(entry.key_offset); |
| 5771 | } |
| 5772 | rc = grn_hash_entry_put_key( |
| 5773 | ctx, |
| 5774 | hash, |
| 5775 | hash_entry, |
| 5776 | entry.key_hash_value, |
| 5777 | entry.key.content.binary.data, |
| 5778 | (unsigned int)(entry.key.content.binary.size)); |
| 5779 | if (rc == GRN_SUCCESS) { |
| 5780 | partial_record_id = GRN_ID_NIL; |
| 5781 | } |
| 5782 | } |
| 5783 | break; |
| 5784 | case GRN_WAL_EVENT_DELETE_ENTRY: |
| 5785 | { |
| 5786 | grn_hash_delete_data data; |
| 5787 | data.hash = hash; |
| 5788 | data.tag = tag; |
| 5789 | data.index_hash_value = entry.index_hash_value; |
| 5790 | data.index = grn_hash_idx_at(ctx, hash, data.index_hash_value); |
| 5791 | if (!data.index) { |
| 5792 | grn_wal_set_recover_error(ctx, |
| 5793 | GRN_NO_MEMORY_AVAILABLE, |
| 5794 | (grn_obj *)hash, |
| 5795 | &entry, |
| 5796 | wal_error_tag, |
| 5797 | "failed to refer index"); |
| 5798 | break; |
| 5799 | } |
| 5800 | data.id = entry.record_id; |
| 5801 | data.entry = grn_hash_entry_at(ctx, hash, data.id, 0); |
| 5802 | if (!data.entry) { |
| 5803 | grn_wal_set_recover_error(ctx, |
| 5804 | GRN_NO_MEMORY_AVAILABLE, |
| 5805 | (grn_obj *)hash, |
| 5806 | &entry, |
| 5807 | wal_error_tag, |
| 5808 | "failed to refer hash entry"); |
| 5809 | break; |
| 5810 | } |
| 5811 | data.key_size = (uint32_t)(entry.key.content.uint64); |
| 5812 | grn_id *garbages; |
| 5813 | if (GRN_HASH_IS_LARGE_KEY(hash)) { |
| 5814 | garbages = hash->header.large->garbages; |
| 5815 | } else { |
| 5816 | garbages = hash->header.normal->garbages; |
| 5817 | } |
| 5818 | uint32_t garbage_index = data.key_size - 1; |
| 5819 | garbages[garbage_index] = entry.garbage_record_id; |
| 5820 | *(hash->n_garbages) = entry.n_garbages; |
| 5821 | *(hash->n_entries) = entry.n_entries; |
| 5822 | if (grn_hash_delete_entry(ctx, &data) == GRN_SUCCESS) { |
| 5823 | if (partial_record_id == data.id) { |
| 5824 | partial_record_id = GRN_ID_NIL; |
| 5825 | } |
| 5826 | } |
| 5827 | } |
| 5828 | break; |
| 5829 | case GRN_WAL_EVENT_REHASH: |
| 5830 | *(hash->n_entries) = entry.n_entries; |
| 5831 | *(hash->max_offset) = entry.max_offset; |
| 5832 | grn_hash_rehash(ctx, hash, entry.expected_n_entries); |
| 5833 | break; |
| 5834 | case GRN_WAL_EVENT_SET_VALUE: |
| 5835 | { |
| 5836 | grn_hash_entry *hash_entry = |
| 5837 | grn_io_hash_entry_at(ctx, hash, entry.record_id, GRN_TABLE_ADD); |
| 5838 | if (!hash_entry) { |
| 5839 | grn_wal_set_recover_error(ctx, |
| 5840 | GRN_NO_MEMORY_AVAILABLE, |
| 5841 | (grn_obj *)hash, |
| 5842 | &entry, |
| 5843 | wal_error_tag, |
| 5844 | "failed to refer hash entry"); |
| 5845 | break; |
| 5846 | } |
| 5847 | void *hash_entry_value = |
| 5848 | grn_hash_entry_get_value(ctx, hash, hash_entry); |
| 5849 | if (!hash_entry_value) { |
| 5850 | grn_wal_set_recover_error(ctx, |
| 5851 | GRN_NO_MEMORY_AVAILABLE, |
| 5852 | (grn_obj *)hash, |
| 5853 | &entry, |
| 5854 | wal_error_tag, |
| 5855 | "failed to refer hash entry value"); |
| 5856 | break; |
| 5857 | } |
| 5858 | grn_hash_set_value_raw(ctx, |
| 5859 | hash, |
| 5860 | hash_entry_value, |
| 5861 | entry.value.content.binary.data); |
| 5862 | } |
| 5863 | break; |
| 5864 | default: |
| 5865 | grn_wal_set_recover_error(ctx, |
| 5866 | GRN_FUNCTION_NOT_IMPLEMENTED, |
| 5867 | (grn_obj *)hash, |
| 5868 | &entry, |
| 5869 | wal_error_tag, |
| 5870 | "not implemented yet"); |
| 5871 | break; |
| 5872 | } |
| 5873 | if (ctx->rc != GRN_SUCCESS) { |
| 5874 | break; |
| 5875 | } |
| 5876 | grn_hash_wal_set_wal_id(ctx, hash, entry.id); |
| 5877 | need_flush = true; |
| 5878 | } |
| 5879 | grn_wal_reader_close(ctx, reader); |
| 5880 | |
| 5881 | if (partial_record_id != GRN_ID_NIL) { |
| 5882 | grn_obj_log(ctx, |
| 5883 | (grn_obj *)hash, |
| 5884 | GRN_LOG_WARNING, |
| 5885 | partial_record_id, |
| 5886 | tag, |
| 5887 | "there is a partially added record"); |
| 5888 | /* This adds a WAL entry but it will be removed by the following |
| 5889 | * grn_obj_flush(). */ |
| 5890 | grn_rc rc = grn_hash_delete_by_id(ctx, hash, partial_record_id, NULL); |
| 5891 | if (rc != GRN_SUCCESS) { |
| 5892 | grn_obj_set_error(ctx, |
| 5893 | (grn_obj *)hash, |
| 5894 | rc, |
| 5895 | partial_record_id, |
| 5896 | tag, |
| 5897 | "failed to delete a partially added record"); |
| 5898 | } |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | if (need_flush && ctx->rc == GRN_SUCCESS) { |
| 5903 | grn_obj_touch(ctx, (grn_obj *)hash, NULL); |
| 5904 | grn_obj_flush(ctx, (grn_obj *)hash); |
| 5905 | } |
| 5906 | |
| 5907 | return ctx->rc; |
| 5908 | } |
| 5909 | |
| 5910 | grn_rc |
| 5911 | grn_hash_warm(grn_ctx *ctx, grn_hash *hash) |
| 5912 | { |
| 5913 | if (grn_hash_is_io_hash(hash)) { |
| 5914 | return grn_io_warm(ctx, hash->io); |
| 5915 | } else { |
| 5916 | return GRN_SUCCESS; |
| 5917 | } |
| 5918 | } |