| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2019-2024 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 | /* "ii" is for inverted index */ |
| 23 | |
| 24 | #include "grn.h" |
| 25 | #include "grn_hash.h" |
| 26 | #include "grn_io.h" |
| 27 | #include "grn_store.h" |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | typedef struct _grn_ii_header_common grn_ii_header_common; |
| 34 | typedef struct _grn_ii_header_normal grn_ii_header_normal; |
| 35 | typedef struct _grn_ii_header_large grn_ii_header_large; |
| 36 | |
| 37 | struct _grn_ii { |
| 38 | grn_db_obj obj; |
| 39 | grn_io *seg; /* I/O for a variety of segments */ |
| 40 | grn_io *chunk; /* I/O for posting chunks */ |
| 41 | grn_obj *lexicon; /* Lexicon table */ |
| 42 | grn_table_flags lflags; |
| 43 | grn_encoding encoding; /* Character encoding */ |
| 44 | /* This member is used for matching */ |
| 45 | uint32_t n_elements; /* Number of elements in postings */ |
| 46 | /* rid, [sid], tf, [weight] and [pos] */ |
| 47 | union { |
| 48 | grn_ii_header_common *common; |
| 49 | grn_ii_header_normal *normal; |
| 50 | grn_ii_header_large *large; |
| 51 | } header; |
| 52 | |
| 53 | bool wal_touched; |
| 54 | }; |
| 55 | |
| 56 | /* BGQ is buffer garbage queue? */ |
| 57 | #define GRN_II_BGQSIZE 16 |
| 58 | /* L is for logical? */ |
| 59 | #define GRN_II_MAX_LSEG 0x10000 |
| 60 | #define GRN_II_W_LSEG 16 |
| 61 | #define GRN_II_W_LOFFSET (32 - GRN_II_W_LSEG) |
| 62 | #define GRN_II_MAX_LSEG_EXTEND 0x10000 |
| 63 | #define GRN_II_W_LSEG_LARGE 17 |
| 64 | #define GRN_II_W_LOFFSET_LARGE (32 - GRN_II_W_LSEG_LARGE) |
| 65 | #define GRN_II_W_TOTAL_CHUNK 40 |
| 66 | /* Each chunk has 2^GRN_II_W_CHUNK size: 4MiB */ |
| 67 | #define GRN_II_W_CHUNK 22 |
| 68 | /* The smallest subchunk size: 256B */ |
| 69 | #define GRN_II_W_LEAST_CHUNK (GRN_II_W_TOTAL_CHUNK - 32) |
| 70 | #define GRN_II_MAX_CHUNK (1 << (GRN_II_W_TOTAL_CHUNK - GRN_II_W_CHUNK)) |
| 71 | /* The number of subchunk size patterns: 256B, 512B, ..., 2MiB */ |
| 72 | #define GRN_II_N_CHUNK_VARIATION (GRN_II_W_CHUNK - GRN_II_W_LEAST_CHUNK) |
| 73 | |
| 74 | #define GRN_II_MAX_CHUNK_SMALL \ |
| 75 | (1 << (GRN_II_W_TOTAL_CHUNK - GRN_II_W_CHUNK - 8)) |
| 76 | /* GRN_II_MAX_CHUNK_MEDIUM has enough space for the following source: |
| 77 | * * Single source. |
| 78 | * * Source is a fixed size column or _key of a table. |
| 79 | * * Source column is a scalar column. |
| 80 | * * Lexicon doesn't have tokenizer. |
| 81 | */ |
| 82 | #define GRN_II_MAX_CHUNK_MEDIUM \ |
| 83 | (1 << (GRN_II_W_TOTAL_CHUNK - GRN_II_W_CHUNK - 4)) |
| 84 | |
| 85 | #define GRN_II_PSEG_NOT_ASSIGNED 0xffffffff |
| 86 | |
| 87 | #define GRN_II_MAX_TF 0x1ffff |
| 88 | |
| 89 | #define GRN_II_HEADER_COMMON_FIELDS \ |
| 90 | uint64_t total_chunk_size; \ |
| 91 | uint64_t bmax; \ |
| 92 | uint32_t flags; \ |
| 93 | uint32_t amax; \ |
| 94 | uint32_t smax; \ |
| 95 | uint32_t param1; /* unused */ \ |
| 96 | uint32_t param2; /* unused */ \ |
| 97 | uint32_t pnext; /* unused */ \ |
| 98 | uint32_t bgqhead; \ |
| 99 | uint32_t bgqtail; \ |
| 100 | uint32_t bgqbody[GRN_II_BGQSIZE]; \ |
| 101 | uint32_t reserved[288]; \ |
| 102 | uint32_t ainfo[GRN_II_MAX_LSEG]; /* array info */ \ |
| 103 | uint32_t binfo[GRN_II_MAX_LSEG]; /* buffer info */ \ |
| 104 | uint32_t free_chunks[GRN_II_N_CHUNK_VARIATION + 1]; \ |
| 105 | uint32_t garbages[GRN_II_N_CHUNK_VARIATION + 1]; \ |
| 106 | uint32_t ngarbages[GRN_II_N_CHUNK_VARIATION + 1]; \ |
| 107 | uint8_t chunks[GRN_II_MAX_CHUNK >> 3] |
| 108 | |
| 109 | struct _grn_ii_header_common { |
| 110 | GRN_II_HEADER_COMMON_FIELDS; |
| 111 | }; |
| 112 | |
| 113 | struct _grn_ii_header_normal { |
| 114 | GRN_II_HEADER_COMMON_FIELDS; |
| 115 | }; |
| 116 | |
| 117 | struct _grn_ii_header_large { |
| 118 | GRN_II_HEADER_COMMON_FIELDS; |
| 119 | uint32_t ainfo_extend[GRN_II_MAX_LSEG_EXTEND]; /* array info (extended) */ |
| 120 | uint32_t binfo_extend[GRN_II_MAX_LSEG_EXTEND]; /* buffer info (extended) */ |
| 121 | }; |
| 122 | |
| 123 | struct _grn_ii_pos { |
| 124 | struct _grn_ii_pos *next; |
| 125 | uint32_t pos; |
| 126 | }; |
| 127 | |
| 128 | struct _grn_ii_updspec { |
| 129 | uint32_t rid; |
| 130 | uint32_t sid; |
| 131 | int32_t weight; |
| 132 | int32_t tf; /* number of postings successfully stored to index */ |
| 133 | int32_t atf; /* actual number of postings */ |
| 134 | int32_t offset; |
| 135 | struct _grn_ii_pos *pos; |
| 136 | struct _grn_ii_pos *tail; |
| 137 | }; |
| 138 | |
| 139 | typedef struct _grn_ii_updspec grn_ii_updspec; |
| 140 | |
| 141 | void |
| 142 | grn_ii_init_from_env(void); |
| 143 | |
| 144 | GRN_API grn_ii * |
| 145 | grn_ii_create(grn_ctx *ctx, const char *path, grn_obj *lexicon, uint32_t flags); |
| 146 | GRN_API grn_ii * |
| 147 | grn_ii_open(grn_ctx *ctx, const char *path, grn_obj *lexicon); |
| 148 | GRN_API grn_rc |
| 149 | grn_ii_close(grn_ctx *ctx, grn_ii *ii); |
| 150 | GRN_API grn_rc |
| 151 | grn_ii_remove(grn_ctx *ctx, const char *path); |
| 152 | grn_rc |
| 153 | grn_ii_info(grn_ctx *ctx, grn_ii *ii, uint64_t *seg_size, uint64_t *chunk_size); |
| 154 | grn_rc |
| 155 | grn_ii_update_one( |
| 156 | grn_ctx *ctx, grn_ii *ii, uint32_t key, grn_ii_updspec *u, grn_hash *h); |
| 157 | grn_rc |
| 158 | grn_ii_delete_one( |
| 159 | grn_ctx *ctx, grn_ii *ii, uint32_t key, grn_ii_updspec *u, grn_hash *h); |
| 160 | grn_ii_updspec * |
| 161 | grn_ii_updspec_open(grn_ctx *ctx, uint32_t rid, uint32_t sid); |
| 162 | grn_rc |
| 163 | grn_ii_updspec_close(grn_ctx *ctx, grn_ii_updspec *u); |
| 164 | grn_rc |
| 165 | grn_ii_updspec_add(grn_ctx *ctx, grn_ii_updspec *u, int pos, int32_t weight); |
| 166 | int |
| 167 | grn_ii_updspec_cmp(grn_ii_updspec *a, grn_ii_updspec *b); |
| 168 | |
| 169 | void |
| 170 | grn_ii_expire(grn_ctx *ctx, grn_ii *ii); |
| 171 | grn_rc |
| 172 | grn_ii_flush(grn_ctx *ctx, grn_ii *ii); |
| 173 | size_t |
| 174 | grn_ii_get_disk_usage(grn_ctx *ctx, grn_ii *ii); |
| 175 | |
| 176 | void |
| 177 | grn_ii_set_visibility(grn_ctx *ctx, grn_ii *ii, bool is_visible); |
| 178 | |
| 179 | grn_ii_cursor * |
| 180 | grn_ii_cursor_openv1(grn_ii *ii, uint32_t key); |
| 181 | grn_rc |
| 182 | grn_ii_cursor_openv2(grn_ii_cursor **cursors, int ncursors); |
| 183 | |
| 184 | uint32_t |
| 185 | grn_ii_get_array_pseg(grn_ii *ii, uint32_t lseg); |
| 186 | uint32_t |
| 187 | grn_ii_get_buffer_pseg(grn_ii *ii, uint32_t pseg); |
| 188 | uint32_t |
| 189 | grn_ii_n_logical_segments(grn_ii *ii); |
| 190 | |
| 191 | uint32_t |
| 192 | grn_ii_max_section(grn_ii *ii); |
| 193 | |
| 194 | const char * |
| 195 | grn_ii_path(grn_ii *ii); |
| 196 | |
| 197 | #define GRN_II_DEFAULT_NEAR_MIN_INTERVAL (INT32_MIN) |
| 198 | |
| 199 | typedef struct _grn_select_optarg grn_select_optarg; |
| 200 | |
| 201 | struct _grn_select_optarg { |
| 202 | grn_operator mode; |
| 203 | int similarity_threshold; |
| 204 | int max_interval; |
| 205 | int *weight_vector; |
| 206 | float *weight_vector_float; |
| 207 | float weight_float; |
| 208 | int vector_size; |
| 209 | int (*func)(grn_ctx *, grn_hash *, const void *, int, void *); |
| 210 | void *func_arg; |
| 211 | int max_size; |
| 212 | grn_obj *scorer; |
| 213 | grn_obj *scorer_args_expr; |
| 214 | unsigned int scorer_args_expr_offset; |
| 215 | grn_fuzzy_search_optarg fuzzy; |
| 216 | grn_match_info *match_info; |
| 217 | int quorum_threshold; |
| 218 | int additional_last_interval; |
| 219 | grn_obj *query_options; |
| 220 | grn_obj *max_element_intervals; |
| 221 | int min_interval; |
| 222 | grn_id query_domain; |
| 223 | }; |
| 224 | |
| 225 | GRN_API grn_rc |
| 226 | grn_ii_column_update(grn_ctx *ctx, |
| 227 | grn_ii *ii, |
| 228 | grn_id id, |
| 229 | unsigned int section, |
| 230 | grn_obj *oldvalue, |
| 231 | grn_obj *newvalue, |
| 232 | grn_obj *posting); |
| 233 | grn_rc |
| 234 | grn_ii_term_extract(grn_ctx *ctx, |
| 235 | grn_ii *ii, |
| 236 | const char *string, |
| 237 | unsigned int string_len, |
| 238 | grn_hash *s, |
| 239 | grn_operator op, |
| 240 | grn_select_optarg *optarg); |
| 241 | grn_rc |
| 242 | grn_ii_similar_search(grn_ctx *ctx, |
| 243 | grn_ii *ii, |
| 244 | const char *string, |
| 245 | unsigned int string_len, |
| 246 | grn_hash *s, |
| 247 | grn_operator op, |
| 248 | grn_select_optarg *optarg); |
| 249 | GRN_API grn_rc |
| 250 | grn_ii_select(grn_ctx *ctx, |
| 251 | grn_ii *ii, |
| 252 | const char *string, |
| 253 | unsigned int string_len, |
| 254 | grn_hash *s, |
| 255 | grn_operator op, |
| 256 | grn_select_optarg *optarg); |
| 257 | grn_rc |
| 258 | grn_ii_sel(grn_ctx *ctx, |
| 259 | grn_ii *ii, |
| 260 | const char *string, |
| 261 | unsigned int string_len, |
| 262 | grn_hash *s, |
| 263 | grn_operator op, |
| 264 | grn_search_optarg *optarg); |
| 265 | |
| 266 | void |
| 267 | grn_ii_resolve_sel_and(grn_ctx *ctx, grn_hash *s, grn_operator op); |
| 268 | |
| 269 | grn_rc |
| 270 | grn_ii_at(grn_ctx *ctx, grn_ii *ii, grn_id id, grn_hash *s, grn_operator op); |
| 271 | |
| 272 | void |
| 273 | grn_ii_inspect_values(grn_ctx *ctx, grn_ii *ii, grn_obj *buf); |
| 274 | void |
| 275 | grn_ii_cursor_inspect(grn_ctx *ctx, grn_ii_cursor *c, grn_obj *buf); |
| 276 | |
| 277 | grn_rc |
| 278 | grn_ii_truncate(grn_ctx *ctx, grn_ii *ii); |
| 279 | |
| 280 | typedef struct grn_ii_builder_options grn_ii_builder_options; |
| 281 | |
| 282 | grn_rc |
| 283 | grn_ii_build(grn_ctx *ctx, grn_ii *ii, const grn_ii_builder_options *options); |
| 284 | |
| 285 | grn_rc |
| 286 | grn_ii_wal_recover(grn_ctx *ctx, grn_ii *ii); |
| 287 | grn_rc |
| 288 | grn_ii_warm(grn_ctx *ctx, grn_ii *ii); |
| 289 | |
| 290 | #ifdef __cplusplus |
| 291 | } |
| 292 | #endif |