| 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 | #pragma once |
| 21 | |
| 22 | #include "grn.h" |
| 23 | #include "grn_db.h" |
| 24 | #include "grn_hash.h" |
| 25 | #include "grn_table_module.h" |
| 26 | |
| 27 | #ifdef __cplusplus |
| 28 | extern "C" { |
| 29 | #endif |
| 30 | |
| 31 | #define GRN_PAT_MAX_KEY_SIZE GRN_TABLE_MAX_KEY_SIZE |
| 32 | |
| 33 | struct _grn_pat { |
| 34 | grn_db_obj obj; |
| 35 | grn_io *io; |
| 36 | struct grn_pat_header *header; |
| 37 | grn_encoding encoding; |
| 38 | uint32_t key_size; |
| 39 | uint32_t value_size; |
| 40 | grn_table_module tokenizer; |
| 41 | grn_obj normalizers; |
| 42 | grn_obj token_filters; |
| 43 | /* For backward compatibility */ |
| 44 | grn_obj token_filter_procs; |
| 45 | grn_obj extractors; |
| 46 | grn_id *cache; |
| 47 | uint32_t cache_size; |
| 48 | bool is_dirty; |
| 49 | grn_critical_section lock; |
| 50 | }; |
| 51 | |
| 52 | #define GRN_PAT_NDELINFOS 0x100 |
| 53 | |
| 54 | typedef struct { |
| 55 | grn_id d; /* The ID of a deleting node. */ |
| 56 | grn_id ld; /* The ID of the parent node of a deleting node. */ |
| 57 | /* delinfo->ld is set if required. */ |
| 58 | uint32_t stat; /* DL_EMPTY, DL_PHASE1, or DL_PHASE2. */ |
| 59 | uint32_t shared; /* This flag is used if GRN_OBJ_KEY_WITH_SIS is set. */ |
| 60 | } grn_pat_delinfo; |
| 61 | |
| 62 | struct grn_pat_header { |
| 63 | uint32_t flags; |
| 64 | grn_encoding encoding; |
| 65 | uint32_t key_size; |
| 66 | uint32_t value_size; |
| 67 | grn_id tokenizer; |
| 68 | uint32_t n_entries; |
| 69 | uint32_t curr_rec; |
| 70 | uint32_t curr_key; |
| 71 | uint32_t curr_del; |
| 72 | uint32_t curr_del2; |
| 73 | uint32_t curr_del3; |
| 74 | uint32_t n_garbages; |
| 75 | grn_id normalizer; |
| 76 | uint32_t truncated; |
| 77 | uint32_t n_dirty_opens; |
| 78 | uint64_t wal_id; |
| 79 | uint64_t curr_key_large; |
| 80 | uint32_t reserved[998]; |
| 81 | grn_pat_delinfo delinfos[GRN_PAT_NDELINFOS]; |
| 82 | grn_id garbages[GRN_PAT_MAX_KEY_SIZE + 1]; |
| 83 | }; |
| 84 | |
| 85 | struct _grn_pat_cursor_entry { |
| 86 | grn_id id; |
| 87 | uint16_t check; |
| 88 | }; |
| 89 | |
| 90 | typedef struct _grn_pat_cursor_entry grn_pat_cursor_entry; |
| 91 | |
| 92 | struct _grn_pat_cursor { |
| 93 | grn_db_obj obj; |
| 94 | grn_id curr_rec; /* ID of the latest record */ |
| 95 | grn_pat *pat; |
| 96 | grn_ctx *ctx; |
| 97 | uint32_t size; /* stack size (the maximum number of entries) */ |
| 98 | uint32_t sp; /* stack pointer (the number of entries) */ |
| 99 | grn_id tail; /* sentinel (the end of the traversal) */ |
| 100 | uint32_t rest; /* limit rest (the number of remaining records) */ |
| 101 | grn_pat_cursor_entry *ss; /* stack buffer (pointer to entries) */ |
| 102 | uint8_t curr_key[GRN_TABLE_MAX_KEY_SIZE]; |
| 103 | }; |
| 104 | |
| 105 | GRN_API grn_id |
| 106 | grn_pat_curr_id(grn_ctx *ctx, grn_pat *pat); |
| 107 | |
| 108 | /* private */ |
| 109 | GRN_API grn_rc |
| 110 | grn_pat_truncate(grn_ctx *ctx, grn_pat *pat); |
| 111 | const char * |
| 112 | _grn_pat_key(grn_ctx *ctx, grn_pat *pat, grn_id id, uint32_t *key_size); |
| 113 | grn_id |
| 114 | grn_pat_next(grn_ctx *ctx, grn_pat *pat, grn_id id); |
| 115 | const char * |
| 116 | grn_pat_get_value_(grn_ctx *ctx, grn_pat *pat, grn_id id, uint32_t *size); |
| 117 | GRN_API grn_id |
| 118 | grn_pat_at(grn_ctx *ctx, grn_pat *pat, grn_id id); |
| 119 | void |
| 120 | grn_pat_check(grn_ctx *ctx, grn_pat *pat); |
| 121 | void |
| 122 | grn_pat_inspect_nodes(grn_ctx *ctx, grn_pat *pat, grn_obj *buf); |
| 123 | void |
| 124 | grn_pat_cursor_inspect(grn_ctx *ctx, grn_pat_cursor *c, grn_obj *buf); |
| 125 | |
| 126 | grn_rc |
| 127 | grn_pat_cache_enable(grn_ctx *ctx, grn_pat *pat, uint32_t cache_size); |
| 128 | void |
| 129 | grn_pat_cache_disable(grn_ctx *ctx, grn_pat *pat); |
| 130 | |
| 131 | GRN_API grn_rc |
| 132 | grn_pat_fuzzy_search(grn_ctx *ctx, |
| 133 | grn_pat *pat, |
| 134 | const void *key, |
| 135 | unsigned int key_size, |
| 136 | grn_fuzzy_search_optarg *args, |
| 137 | grn_hash *h); |
| 138 | |
| 139 | uint64_t |
| 140 | grn_pat_total_key_size(grn_ctx *ctx, grn_pat *pat); |
| 141 | uint64_t |
| 142 | grn_pat_max_total_key_size(grn_ctx *ctx, grn_pat *pat); |
| 143 | |
| 144 | bool |
| 145 | grn_pat_is_key_encoded(grn_ctx *ctx, grn_pat *pat); |
| 146 | |
| 147 | grn_rc |
| 148 | grn_pat_dirty(grn_ctx *ctx, grn_pat *pat); |
| 149 | bool |
| 150 | grn_pat_is_dirty(grn_ctx *ctx, grn_pat *pat); |
| 151 | grn_rc |
| 152 | grn_pat_clean(grn_ctx *ctx, grn_pat *pat); |
| 153 | grn_rc |
| 154 | grn_pat_clear_dirty(grn_ctx *ctx, grn_pat *pat); |
| 155 | grn_rc |
| 156 | grn_pat_wal_recover(grn_ctx *ctx, grn_pat *pat); |
| 157 | grn_rc |
| 158 | grn_pat_warm(grn_ctx *ctx, grn_pat *pat); |
| 159 | uint64_t |
| 160 | grn_pat_defrag(grn_ctx *ctx, grn_pat *pat); |
| 161 | |
| 162 | #ifdef __cplusplus |
| 163 | } |
| 164 | #endif |