Debian Codegraph @

lib/grn_hash.h

raw symbols SCIP
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_ii_select_cursor.h"
24 #include "grn_io.h"
25 #include "grn_table_module.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /**** grn_tiny_array ****/
32
33 /*
34 * grn_tiny_array_init() accepts a logical OR of the following flags.
35 * Note that other flags, such as (1 << 30), will be ignored.
36 *
37 * - GRN_TINY_ARRAY_CLEAR specifies to initialize a new block with zeros.
38 * It is valid only iff specified with GRN_TINY_ARRAY_USE_MALLOC.
39 * - GRN_TINY_ARRAY_THREADSAFE specifies to create a critical section when
40 * allocating memory.
41 * - GRN_TINY_ARRAY_USE_MALLOC specifies to use GRN_MALLOC/CALLOC/FREE instead
42 * of GRN_CTX_ALLOC/FREE.
43 */
44 #define GRN_TINY_ARRAY_CLEAR (1 << 0)
45 #define GRN_TINY_ARRAY_THREADSAFE (1 << 1)
46 #define GRN_TINY_ARRAY_USE_MALLOC (1 << 2)
47
48 /*
49 * - GRN_TINY_ARRAY_FACTOR is the global parameter of grn_tiny_array.
50 * - GRN_TINY_ARRAY_GET_OFFSET() returns the offset of a specified block.
51 * - GRN_TINY_ARRAY_BASE_BLOCK_SIZE is the number of elements in the first
52 * block.
53 * - GRN_TINY_ARRAY_GET_BLOCK_SIZE() returns the number of elements in a
54 * specified block.
55 * - GRN_TINY_ARRAY_NUM_BLOCKS is the maximum number of blocks.
56 */
57 #define GRN_TINY_ARRAY_FACTOR 0
58 #define GRN_TINY_ARRAY_GET_OFFSET(block_id) \
59 (1 << ((block_id) << GRN_TINY_ARRAY_FACTOR))
60 #define GRN_TINY_ARRAY_BASE_BLOCK_SIZE \
61 (GRN_TINY_ARRAY_GET_OFFSET(1) - GRN_TINY_ARRAY_GET_OFFSET(0))
62 #define GRN_TINY_ARRAY_GET_BLOCK_SIZE(block_id) \
63 (GRN_TINY_ARRAY_BASE_BLOCK_SIZE * GRN_TINY_ARRAY_GET_OFFSET(block_id))
64 #define GRN_TINY_ARRAY_NUM_BLOCKS (32 >> GRN_TINY_ARRAY_FACTOR)
65
66 /*
67 * grn_tiny_array uses several blocks to emulate an array.
68 * The k-th block, blocks[k - 1], consists of 2^(k-1) elements.
69 */
70 typedef struct _grn_tiny_array grn_tiny_array;
71
72 struct _grn_tiny_array {
73 grn_ctx *ctx;
74 grn_id max;
75 uint16_t element_size;
76 uint16_t flags;
77 void *blocks[GRN_TINY_ARRAY_NUM_BLOCKS];
78 grn_critical_section lock;
79 };
80
81 #define GRN_TINY_ARRAY_EACH(array, head, tail, key, value, block) \
82 do { \
83 int _block_id; \
84 const grn_id _head = (head); \
85 const grn_id _tail = (tail); \
86 for (_block_id = 0, (key) = (_head); \
87 _block_id < GRN_TINY_ARRAY_NUM_BLOCKS && (key) <= (_tail); \
88 _block_id++) { \
89 int _id = GRN_TINY_ARRAY_GET_BLOCK_SIZE(_block_id); \
90 (value) = (array)->blocks[_block_id]; \
91 if (value) { \
92 while (_id-- && (key) <= (_tail)) { \
93 {block}(key)++; \
94 (value) = (void *)((byte *)(value) + (array)->element_size); \
95 } \
96 } else { \
97 (key) += _id; \
98 } \
99 } \
100 } while (0)
101
102 #define GRN_TINY_ARRAY_EACH_BEGIN(array, head, tail, value) \
103 do { \
104 int _block_id; \
105 const grn_id _head = (head); \
106 const grn_id _tail = (tail); \
107 grn_id _current = _head; \
108 for (_block_id = 0; \
109 _block_id < GRN_TINY_ARRAY_NUM_BLOCKS && _current <= _tail; \
110 _block_id++) { \
111 int _n_elements = GRN_TINY_ARRAY_GET_BLOCK_SIZE(_block_id); \
112 uint8_t *_block = (array)->blocks[_block_id]; \
113 if (_block) { \
114 int _i; \
115 for (_i = 0; _i < _n_elements && _current <= _tail; \
116 _i++, _current++) { \
117 void *value = _block + (array)->element_size * _i;
118
119 #define GRN_TINY_ARRAY_EACH_END() \
120 } \
121 } \
122 else \
123 { \
124 _current += _n_elements; \
125 } \
126 } \
127 } \
128 while (0)
129
130 GRN_API void
131 grn_tiny_array_init(grn_ctx *ctx,
132 grn_tiny_array *array,
133 uint16_t element_size,
134 uint16_t flags);
135 GRN_API void
136 grn_tiny_array_fin(grn_tiny_array *array);
137 GRN_API void *
138 grn_tiny_array_at(grn_tiny_array *array, grn_id id);
139 GRN_API grn_id
140 grn_tiny_array_id(grn_tiny_array *array, const void *element_address);
141
142 /**** grn_tiny_bitmap ****/
143
144 typedef struct _grn_tiny_bitmap grn_tiny_bitmap;
145
146 struct _grn_tiny_bitmap {
147 grn_ctx *ctx;
148 void *blocks[GRN_TINY_ARRAY_NUM_BLOCKS];
149 };
150
151 /**** grn_array ****/
152
153 #define GRN_ARRAY_TINY (0x01 << 6)
154
155 /*
156 * grn_array uses grn_io or grn_tiny_array to represent an array.
157 *
158 * To create a grn_tiny_array-based grn_array, specify the GRN_ARRAY_TINY flag
159 * to grn_array_create(). Note that a grn_tiny_array-based grn_array is not
160 * backed by a file.
161 */
162 struct _grn_array {
163 grn_db_obj obj;
164 grn_ctx *ctx;
165 uint32_t value_size;
166 size_t n_keys;
167 grn_table_sort_key *keys;
168 uint32_t *n_garbages;
169 uint32_t *n_entries;
170
171 /* For grn_io_array. */
172 grn_io *io;
173 struct grn_array_header *header;
174 uint32_t *lock;
175 bool wal_touched;
176
177 /* For grn_tiny_array. */
178 uint32_t n_garbages_buf;
179 uint32_t n_entries_buf;
180 grn_id garbages;
181 grn_tiny_array array;
182 grn_tiny_bitmap bitmap;
183 };
184
185 struct _grn_array_cursor {
186 grn_db_obj obj;
187 grn_array *array;
188 grn_ctx *ctx;
189 grn_id curr_rec;
190 grn_id tail;
191 unsigned int rest;
192 int dir;
193 };
194
195 /*
196 * grn_array_size() returns the number of entries in an array.
197 * If the array was truncated by another process but `array` still refers to
198 * the old one, this function returns 0.
199 */
200 uint32_t
201 grn_array_size(grn_ctx *ctx, grn_array *array);
202
203 uint32_t
204 grn_array_get_flags(grn_ctx *ctx, grn_array *array);
205
206 grn_rc
207 grn_array_truncate(grn_ctx *ctx, grn_array *array);
208 grn_rc
209 grn_array_copy_sort_key(grn_ctx *ctx,
210 grn_array *array,
211 grn_table_sort_key *keys,
212 size_t n_keys);
213 grn_rc
214 grn_array_wal_recover(grn_ctx *ctx, grn_array *array);
215 grn_rc
216 grn_array_warm(grn_ctx *ctx, grn_array *array);
217
218 /**** grn_hash ****/
219
220 #define GRN_HASH_MAX_KEY_SIZE_NORMAL GRN_TABLE_MAX_KEY_SIZE
221 #define GRN_HASH_MAX_KEY_SIZE_LARGE (0xffff)
222
223 #define GRN_HASH_IS_LARGE_KEY(hash) \
224 ((hash)->key_size > GRN_HASH_MAX_KEY_SIZE_NORMAL)
225
226 typedef struct _grn_hash_header_common grn_hash_header_common;
227 typedef struct _grn_hash_header_normal grn_hash_header_normal;
228 typedef struct _grn_hash_header_large grn_hash_header_large;
229
230 typedef struct _grn_hash_wal_add_entry_data grn_hash_wal_add_entry_data;
231
232 struct _grn_hash {
233 grn_db_obj obj;
234 grn_ctx *ctx;
235 uint32_t key_size;
236 grn_encoding encoding;
237 uint32_t value_size;
238 uint32_t entry_size;
239 uint32_t *n_garbages;
240 uint32_t *n_entries;
241 uint32_t *max_offset;
242 grn_table_module tokenizer;
243 grn_obj normalizers;
244 grn_obj token_filters;
245 /* For backward compatibility */
246 grn_obj token_filter_procs;
247 grn_obj extractors;
248
249 /* For grn_io_hash. */
250 grn_io *io;
251 union {
252 grn_hash_header_common *common;
253 grn_hash_header_normal *normal;
254 grn_hash_header_large *large;
255 } header;
256 uint32_t *lock;
257 // uint32_t nref;
258 // unsigned int max_n_subrecs;
259 // unsigned int record_size;
260 // unsigned int subrec_size;
261 // grn_rec_unit record_unit;
262 // grn_rec_unit subrec_unit;
263 // uint8_t arrayp;
264 // grn_recordh *curr_rec;
265 // grn_set_cursor *cursor;
266 // int limit;
267 // void *userdata;
268 // grn_id subrec_id;
269
270 /* For grn_tiny_hash. */
271 uint32_t max_offset_;
272 uint32_t n_garbages_;
273 uint32_t n_entries_;
274 grn_id *index;
275 grn_id garbages;
276 grn_tiny_array a;
277 grn_tiny_bitmap bitmap;
278
279 grn_hash_wal_add_entry_data *wal_data;
280 };
281
282 #define GRN_HASH_HEADER_COMMON_FIELDS \
283 uint32_t flags; \
284 grn_encoding encoding; \
285 uint32_t key_size; \
286 uint32_t value_size; \
287 grn_id tokenizer; \
288 uint32_t curr_rec; \
289 uint32_t curr_key_normal; \
290 uint32_t idx_offset; \
291 uint32_t entry_size; \
292 uint32_t max_offset; \
293 uint32_t n_entries; \
294 uint32_t n_garbages; \
295 uint32_t lock; \
296 grn_id normalizer; \
297 uint32_t truncated; \
298 uint64_t curr_key_large; \
299 uint64_t wal_id; \
300 uint32_t reserved[10]
301
302 struct _grn_hash_header_common {
303 GRN_HASH_HEADER_COMMON_FIELDS;
304 };
305
306 struct _grn_hash_header_normal {
307 GRN_HASH_HEADER_COMMON_FIELDS;
308 grn_id garbages[GRN_HASH_MAX_KEY_SIZE_NORMAL];
309 };
310
311 struct _grn_hash_header_large {
312 GRN_HASH_HEADER_COMMON_FIELDS;
313 grn_id garbages[GRN_HASH_MAX_KEY_SIZE_LARGE];
314 };
315
316 struct _grn_hash_cursor {
317 grn_db_obj obj;
318 grn_hash *hash;
319 grn_ctx *ctx;
320 grn_id curr_rec;
321 grn_id tail;
322 unsigned int rest;
323 int dir;
324 };
325
326 /* deprecated */
327
328 #define GRN_TABLE_SORT_BY_KEY 0
329 #define GRN_TABLE_SORT_BY_ID (1L << 1)
330 #define GRN_TABLE_SORT_BY_VALUE (1L << 2)
331 #define GRN_TABLE_SORT_RES_ID 0
332 #define GRN_TABLE_SORT_RES_KEY (1L << 3)
333 #define GRN_TABLE_SORT_AS_BIN 0
334 #define GRN_TABLE_SORT_AS_NUMBER (1L << 4)
335 #define GRN_TABLE_SORT_AS_SIGNED 0
336 #define GRN_TABLE_SORT_AS_UNSIGNED (1L << 5)
337 #define GRN_TABLE_SORT_AS_INT32 0
338 #define GRN_TABLE_SORT_AS_INT64 (1L << 6)
339 #define GRN_TABLE_SORT_NO_PROC 0
340 #define GRN_TABLE_SORT_WITH_PROC (1L << 7)
341
342 typedef struct _grn_table_sort_optarg grn_table_sort_optarg;
343
344 struct _grn_table_sort_optarg {
345 grn_table_sort_flags flags;
346 int (*compar)(grn_ctx *ctx,
347 grn_obj *table1,
348 void *target1,
349 unsigned int target1_size,
350 grn_obj *table2,
351 void *target2,
352 unsigned int target2_size,
353 void *compare_arg);
354 void *compar_arg;
355 grn_obj *proc;
356 int offset;
357 };
358
359 void
360 grn_hash_init_from_env(void);
361
362 GRN_API int
363 grn_hash_sort(grn_ctx *ctx,
364 grn_hash *hash,
365 int limit,
366 grn_array *result,
367 grn_table_sort_optarg *optarg);
368
369 grn_rc
370 grn_hash_lock(grn_ctx *ctx, grn_hash *hash, int timeout);
371 grn_rc
372 grn_hash_unlock(grn_ctx *ctx, grn_hash *hash);
373 grn_rc
374 grn_hash_clear_lock(grn_ctx *ctx, grn_hash *hash);
375
376 #define GRN_HASH_SIZE(hash) (*((hash)->n_entries))
377
378 /* private */
379 typedef enum {
380 GRN_REC_DOCUMENT = 0,
381 GRN_REC_SECTION,
382 GRN_REC_POSITION,
383 GRN_REC_USERDEF,
384 GRN_REC_NONE
385 } grn_rec_unit;
386
387 GRN_API grn_rc
388 grn_hash_truncate(grn_ctx *ctx, grn_hash *hash);
389
390 int
391 grn_rec_unit_size(grn_rec_unit unit, int rec_size);
392
393 const char *
394 _grn_hash_key(grn_ctx *ctx, grn_hash *hash, grn_id id, uint32_t *key_size);
395
396 grn_rc
397 grn_hash_add_table_cursor(grn_ctx *ctx,
398 grn_hash *hash,
399 grn_table_cursor *cursor,
400 double score,
401 grn_operator op);
402 grn_rc
403 grn_hash_add_index_cursor(grn_ctx *ctx,
404 grn_hash *hash,
405 grn_obj *cursor,
406 double additional_score,
407 double weight,
408 grn_operator op);
409 grn_rc
410 grn_hash_add_ii_cursor(grn_ctx *ctx,
411 grn_hash *hash,
412 grn_ii_cursor *cursor,
413 double additional_score,
414 double weight,
415 grn_operator op);
416 grn_rc
417 grn_hash_add_ii_select_cursor(grn_ctx *ctx,
418 grn_hash *hash,
419 grn_ii_select_cursor *cursor,
420 grn_operator op);
421
422 int
423 grn_hash_get_key_value(grn_ctx *ctx,
424 grn_hash *hash,
425 grn_id id,
426 void *keybuf,
427 int bufsize,
428 void *valuebuf);
429
430 int
431 _grn_hash_get_key_value(
432 grn_ctx *ctx, grn_hash *hash, grn_id id, void **key, void **value);
433
434 grn_id
435 grn_hash_next(grn_ctx *ctx, grn_hash *hash, grn_id id);
436
437 /* only valid for hash tables, GRN_OBJ_KEY_VAR_SIZE && GRN_HASH_TINY */
438 const char *
439 _grn_hash_strkey_by_val(void *v, uint16_t *size);
440
441 const char *
442 grn_hash_get_value_(grn_ctx *ctx, grn_hash *hash, grn_id id, uint32_t *size);
443
444 grn_rc
445 grn_hash_remove(grn_ctx *ctx, const char *path);
446 grn_rc
447 grn_array_remove(grn_ctx *ctx, const char *path);
448
449 grn_id
450 grn_hash_at(grn_ctx *ctx, grn_hash *hash, grn_id id);
451 grn_id
452 grn_array_at(grn_ctx *ctx, grn_array *array, grn_id id);
453
454 void
455 grn_hash_check(grn_ctx *ctx, grn_hash *hash);
456
457 bool
458 grn_hash_is_large_total_key_size(grn_ctx *ctx, grn_hash *hash);
459
460 uint64_t
461 grn_hash_total_key_size(grn_ctx *ctx, grn_hash *hash);
462 uint64_t
463 grn_hash_max_total_key_size(grn_ctx *ctx, grn_hash *hash);
464
465 grn_rc
466 grn_hash_wal_recover(grn_ctx *ctx, grn_hash *hash);
467 grn_rc
468 grn_hash_warm(grn_ctx *ctx, grn_hash *hash);
469
470 #ifdef __cplusplus
471 }
472 #endif