Debian Codegraph @

lib/grn_io.h

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2017 Brazil
3 Copyright (C) 2021-2022 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_ctx.h"
24 #include "grn_error.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #ifdef WIN32
31 # define GRN_IO_FILE_CREATE_MODE (GENERIC_READ | GENERIC_WRITE)
32 #else /* WIN32 */
33 # define GRN_IO_FILE_CREATE_MODE 0644
34 #endif /* WIN32 */
35
36 typedef enum {
37 GRN_IO_RDONLY,
38 GRN_IO_WRONLY,
39 GRN_IO_RDWR
40 } grn_io_rw_mode;
41
42 typedef enum {
43 GRN_IO_AUTO,
44 GRN_IO_MANUAL
45 } grn_io_mode;
46
47 /**** grn_io ****/
48
49 typedef struct _grn_io grn_io;
50
51 typedef struct {
52 grn_io *io;
53 grn_ctx *ctx;
54 uint8_t mode;
55 uint8_t tiny_p;
56 uint32_t pseg;
57 uint32_t segment;
58 uint32_t offset;
59 uint32_t size;
60 uint32_t nseg;
61 void *addr;
62 uint32_t diff;
63 int32_t cached;
64 #ifdef WIN32
65 HANDLE fmo;
66 #endif /* WIN32 */
67 void *uncompressed_value;
68 } grn_io_win;
69
70 typedef struct {
71 void *map;
72 uint32_t nref;
73 uint32_t count;
74 #ifdef WIN32
75 HANDLE fmo;
76 #endif /* WIN32 */
77 } grn_io_mapinfo;
78
79 typedef struct _grn_io_array_info grn_io_array_info;
80
81 struct _grn_io_header {
82 char idstr[16];
83 uint32_t type;
84 uint32_t version;
85 uint32_t flags;
86 uint32_t header_size;
87 uint32_t segment_size;
88 uint32_t max_segment;
89 uint32_t n_arrays;
90 uint32_t lock;
91 uint64_t curr_size;
92 uint32_t segment_tail;
93 uint32_t last_modified;
94 };
95
96 struct _grn_io {
97 char path[PATH_MAX];
98 struct _grn_io_header *header;
99 byte *user_header;
100 grn_io_mapinfo *maps;
101 uint32_t base;
102 uint32_t base_seg;
103 grn_io_mode mode;
104 struct _grn_io_fileinfo *fis;
105 grn_io_array_info *ainfo;
106 uint32_t max_map_seg;
107 uint32_t nmaps;
108 uint32_t nref;
109 uint32_t count;
110 uint32_t flags;
111 uint32_t *lock;
112 };
113
114 GRN_API grn_io *
115 grn_io_create(grn_ctx *ctx,
116 const char *path,
117 uint32_t header_size,
118 uint32_t segment_size,
119 uint32_t max_segment,
120 grn_io_mode mode,
121 unsigned int flags);
122 grn_io *
123 grn_io_open(grn_ctx *ctx, const char *path, grn_io_mode mode);
124 GRN_API grn_rc
125 grn_io_close(grn_ctx *ctx, grn_io *io);
126 grn_rc
127 grn_io_remove(grn_ctx *ctx, const char *path);
128 grn_rc
129 grn_io_remove_if_exist(grn_ctx *ctx, const char *path);
130 grn_rc
131 grn_io_size(grn_ctx *ctx, grn_io *io, uint64_t *size);
132 grn_rc
133 grn_io_rename(grn_ctx *ctx, const char *old_name, const char *new_name);
134 GRN_API void *
135 grn_io_header(grn_io *io);
136
137 void *
138 grn_io_win_map(grn_ctx *ctx,
139 grn_io *io,
140 grn_io_win *iw,
141 uint32_t segment,
142 uint32_t offset,
143 uint32_t size,
144 grn_io_rw_mode mode);
145 grn_rc
146 grn_io_win_unmap(grn_ctx *ctx, grn_io_win *iw);
147
148 typedef struct _grn_io_ja_einfo grn_io_ja_einfo;
149 typedef struct _grn_io_ja_ehead grn_io_ja_ehead;
150
151 struct _grn_io_ja_einfo {
152 uint32_t pos;
153 uint32_t size;
154 };
155
156 struct _grn_io_ja_ehead {
157 uint32_t size;
158 uint32_t key;
159 };
160
161 grn_rc
162 grn_io_read_ja(grn_io *io,
163 grn_ctx *ctx,
164 grn_io_ja_einfo *einfo,
165 uint32_t epos,
166 uint32_t key,
167 uint32_t segment,
168 uint32_t offset,
169 void **value,
170 uint32_t *value_len);
171 grn_rc
172 grn_io_write_ja(grn_io *io,
173 grn_ctx *ctx,
174 uint32_t key,
175 uint32_t segment,
176 uint32_t offset,
177 void *value,
178 uint32_t value_len);
179
180 grn_rc
181 grn_io_write_ja_ehead(grn_io *io,
182 grn_ctx *ctx,
183 uint32_t key,
184 uint32_t segment,
185 uint32_t offset,
186 uint32_t value_len);
187
188 #define GRN_TABLE_ADD (0x01 << 6)
189 #define GRN_TABLE_ADDED (0x01 << 7)
190
191 #define GRN_IO_MAX_RETRY (0x10000)
192 #define GRN_IO_MAX_REF (0x80000000)
193
194 #define GRN_IO_EXPIRE_GTICK (0x01)
195 #define GRN_IO_EXPIRE_SEGMENT (0x02)
196 #define GRN_IO_TEMPORARY (0x04)
197
198 void
199 grn_io_seg_map_(grn_ctx *ctx, grn_io *io, uint32_t segno, grn_io_mapinfo *info);
200
201 /*
202 * io mustn't be NULL;
203 */
204 static inline void *
205 grn_io_seg_ref(grn_ctx *ctx, grn_io *io, uint32_t segno)
206 {
207 const char *tag = "[io][seg][ref]";
208 grn_io_mapinfo *info = NULL;
209 if (segno >= io->header->max_segment) {
210 ERR(GRN_INVALID_ARGUMENT,
211 "%s too large segment ID: "
212 "id:%u, "
213 "max:%u, "
214 "path:<%s>",
215 tag,
216 segno,
217 io->header->max_segment,
218 io->path);
219 return NULL;
220 }
221 info = &(io->maps[segno]);
222 uint32_t nref, retry, *pnref = &info->nref;
223 if (io->flags & GRN_IO_EXPIRE_SEGMENT) {
224 if (io->flags & GRN_IO_EXPIRE_GTICK) {
225 for (retry = 0; !info->map || info->count != grn_gtick; retry++) {
226 GRN_ATOMIC_ADD_EX(pnref, 1, nref);
227 if (nref) {
228 GRN_ATOMIC_ADD_EX(pnref, -1, nref);
229 if (retry >= GRN_IO_MAX_RETRY) {
230 GRN_LOG(ctx,
231 GRN_LOG_CRIT,
232 "deadlock detected! in grn_io_seg_ref(%p, %u)",
233 io,
234 segno);
235 break;
236 }
237 GRN_FUTEX_WAIT(pnref);
238 } else {
239 info->count = grn_gtick;
240 if (!info->map) {
241 grn_io_seg_map_(ctx, io, segno, info);
242 if (!info->map) {
243 CRIT(GRN_NO_MEMORY_AVAILABLE,
244 "%s failed to mmap with expire-segment & expire-gtick: "
245 "id:%u, "
246 "max:%u, "
247 "path:<%s>, "
248 "message:%s",
249 tag,
250 segno,
251 io->header->max_segment,
252 io->path,
253 grn_error_get_current_system_message());
254 }
255 }
256 GRN_ATOMIC_ADD_EX(pnref, -1, nref);
257 GRN_FUTEX_WAKE(pnref);
258 break;
259 }
260 }
261 } else {
262 for (retry = 0;; retry++) {
263 GRN_ATOMIC_ADD_EX(pnref, 1, nref);
264 if (nref >= GRN_IO_MAX_REF) {
265 GRN_ATOMIC_ADD_EX(pnref, -1, nref);
266 if (retry >= GRN_IO_MAX_RETRY) {
267 GRN_LOG(ctx,
268 GRN_LOG_CRIT,
269 "deadlock detected!! in grn_io_seg_ref(%p, %u, %u)",
270 io,
271 segno,
272 nref);
273 *pnref = 0; /* force reset */
274 break;
275 }
276 GRN_FUTEX_WAIT(pnref);
277 continue;
278 }
279 if (nref >= 0x40000000) {
280 ALERT("strange nref value!! in grn_io_seg_ref(%p, %u, %u)",
281 io,
282 segno,
283 nref);
284 }
285 if (!info->map) {
286 if (nref) {
287 GRN_ATOMIC_ADD_EX(pnref, -1, nref);
288 if (retry >= GRN_IO_MAX_RETRY) {
289 GRN_LOG(ctx,
290 GRN_LOG_CRIT,
291 "deadlock detected!!! in grn_io_seg_ref(%p, %u, %u)",
292 io,
293 segno,
294 nref);
295 break;
296 }
297 GRN_FUTEX_WAIT(pnref);
298 continue;
299 } else {
300 grn_io_seg_map_(ctx, io, segno, info);
301 if (!info->map) {
302 GRN_ATOMIC_ADD_EX(pnref, -1, nref);
303 CRIT(GRN_NO_MEMORY_AVAILABLE,
304 "%s failed to mmap with expire-segment: "
305 "id:%u, "
306 "nref:%u, "
307 "max:%u, "
308 "path:<%s>, "
309 "message:%s",
310 tag,
311 segno,
312 nref,
313 io->header->max_segment,
314 io->path,
315 grn_error_get_current_system_message());
316 }
317 GRN_FUTEX_WAKE(pnref);
318 }
319 }
320 break;
321 }
322 info->count = grn_gtick;
323 }
324 } else {
325 for (retry = 0; !info->map; retry++) {
326 GRN_ATOMIC_ADD_EX(pnref, 1, nref);
327 if (nref) {
328 GRN_ATOMIC_ADD_EX(pnref, -1, nref);
329 if (retry >= GRN_IO_MAX_RETRY) {
330 GRN_LOG(ctx,
331 GRN_LOG_CRIT,
332 "deadlock detected!!!! in grn_io_seg_ref(%p, %u)",
333 io,
334 segno);
335 break;
336 }
337 GRN_FUTEX_WAIT(pnref);
338 } else {
339 if (!info->map) {
340 grn_io_seg_map_(ctx, io, segno, info);
341 if (!info->map) {
342 CRIT(GRN_NO_MEMORY_AVAILABLE,
343 "%s failed to mmap: "
344 "id:%u, "
345 "max:%u, "
346 "path:<%s>, "
347 "message:%s",
348 tag,
349 segno,
350 io->header->max_segment,
351 io->path,
352 grn_error_get_current_system_message());
353 }
354 }
355 GRN_ATOMIC_ADD_EX(pnref, -1, nref);
356 GRN_FUTEX_WAKE(pnref);
357 break;
358 }
359 }
360 info->count = grn_gtick;
361 }
362 return info->map;
363 }
364
365 static inline void
366 grn_io_seg_unref(grn_ctx *ctx, grn_io *io, uint32_t segno)
367 {
368 if (GRN_IO_EXPIRE_SEGMENT ==
369 (io->flags & (GRN_IO_EXPIRE_GTICK | GRN_IO_EXPIRE_SEGMENT))) {
370 uint32_t nref, *pnref = &(io)->maps[segno].nref;
371 GRN_ATOMIC_ADD_EX(pnref, -1, nref);
372 }
373 }
374
375 uint32_t
376 grn_io_base_seg(grn_io *io);
377 const char *
378 grn_io_path(grn_io *io);
379
380 typedef struct _grn_io_array_spec grn_io_array_spec;
381
382 struct _grn_io_array_spec {
383 uint32_t w_of_element;
384 uint32_t max_n_segments;
385 };
386
387 struct _grn_io_array_info {
388 uint32_t w_of_elm_in_a_segment;
389 uint32_t elm_mask_in_a_segment;
390 uint32_t max_n_segments;
391 uint32_t element_size;
392 uint32_t *segments;
393 void **addrs;
394 };
395
396 grn_io *
397 grn_io_create_with_array(grn_ctx *ctx,
398 const char *path,
399 uint32_t header_size,
400 uint32_t segment_size,
401 grn_io_mode mode,
402 uint32_t n_arrays,
403 grn_io_array_spec *array_specs);
404
405 void
406 grn_io_segment_alloc(grn_ctx *ctx,
407 grn_io *io,
408 grn_io_array_info *ai,
409 uint32_t lseg,
410 int *flags,
411 void **p);
412
413 GRN_API grn_rc
414 grn_io_lock(grn_ctx *ctx, grn_io *io, int timeout);
415 GRN_API void
416 grn_io_unlock(grn_ctx *ctx, grn_io *io);
417 void
418 grn_io_clear_lock(grn_io *io);
419 uint32_t
420 grn_io_is_locked(grn_io *io);
421 bool
422 grn_io_is_corrupt(grn_ctx *ctx, grn_io *io);
423 size_t
424 grn_io_get_disk_usage(grn_ctx *ctx, grn_io *io);
425
426 static inline void *
427 grn_io_array_at(
428 grn_ctx *ctx, grn_io *io, uint32_t array, uint64_t offset, int *flags)
429 {
430 grn_io_array_info *ainfo = &(io->ainfo[array]);
431 uint32_t lseg = (uint32_t)(offset >> ainfo->w_of_elm_in_a_segment);
432 if (lseg >= ainfo->max_n_segments) {
433 ERR(GRN_NO_MEMORY_AVAILABLE,
434 "[io][array][at] too large offset: %" GRN_FMT_INT64U
435 ": max=%" GRN_FMT_INT64D ": nth=%u"
436 ": path=<%s>",
437 offset,
438 (int64_t)((ainfo->max_n_segments << ainfo->w_of_elm_in_a_segment) - 1),
439 array,
440 io->path);
441 return NULL;
442 }
443 void **p_ = &ainfo->addrs[lseg];
444 if (!*p_) {
445 grn_io_segment_alloc(ctx, io, ainfo, lseg, flags, p_);
446 if (!*p_) {
447 return NULL;
448 }
449 }
450 return (((byte *)*p_) +
451 ((offset & ainfo->elm_mask_in_a_segment) * ainfo->element_size));
452 }
453
454 #define GRN_IO_ARRAY_BIT_AT(io, array, offset, res) \
455 do { \
456 int flags_ = 0; \
457 uint8_t *ptr_ = \
458 grn_io_array_at(ctx, (io), (array), ((offset) >> 3) + 1, &flags_, ); \
459 res = ptr_ ? ((*ptr_ >> ((offset) & 7)) & 1) : 0; \
460 } while (0)
461
462 #define GRN_IO_ARRAY_BIT_ON(io, array, offset) \
463 do { \
464 int flags_ = GRN_TABLE_ADD; \
465 uint8_t *ptr_ = \
466 grn_io_array_at(ctx, (io), (array), ((offset) >> 3) + 1, &flags_); \
467 if (ptr_) { \
468 *ptr_ |= (1 << ((offset) & 7)); \
469 } \
470 } while (0)
471
472 #define GRN_IO_ARRAY_BIT_OFF(io, array, offset) \
473 do { \
474 int flags_ = GRN_TABLE_ADD; \
475 uint8_t *ptr = \
476 grn_io_array_at(ctx, (io), (array), ((offset) >> 3) + 1, &flags_); \
477 if (ptr_) { \
478 *ptr_ &= ~(1 << ((offset) & 7)); \
479 } \
480 } while (0)
481
482 #define GRN_IO_ARRAY_BIT_FLIP(io, array, offset) \
483 do { \
484 int flags_ = GRN_TABLE_ADD; \
485 uint8_t *ptr_ = \
486 grn_io_array_at((io), (array), ((offset) >> 3) + 1, &flags_); \
487 if (ptr_) { \
488 *ptr_ ^= (1 << ((offset) & 7)); \
489 } \
490 } while (0)
491
492 void *
493 grn_io_anon_map(grn_ctx *ctx, grn_io_mapinfo *mi, size_t length);
494 void
495 grn_io_anon_unmap(grn_ctx *ctx, grn_io_mapinfo *mi, size_t length);
496 uint32_t
497 grn_io_detect_type(grn_ctx *ctx, const char *path);
498 grn_rc
499 grn_io_set_type(grn_io *io, uint32_t type);
500 uint32_t
501 grn_io_get_type(grn_io *io);
502
503 void
504 grn_io_init_from_env(void);
505
506 uint32_t
507 grn_io_expire(grn_ctx *ctx, grn_io *io, uint32_t count_thresh, uint32_t limit);
508
509 grn_rc
510 grn_io_flush(grn_ctx *ctx, grn_io *io);
511
512 bool
513 grn_io_warm_path(grn_ctx *ctx, grn_io *io, const char *path);
514 grn_rc
515 grn_io_warm(grn_ctx *ctx, grn_io *io);
516
517 /* encode/decode */
518
519 #define GRN_B_ENC_MAX_SIZE 5
520
521 /* B is for ???
522 *
523 * v: The uint32_t value to be encoded.
524 *
525 * v < 0x8f:
526 * +--------+
527 * |1000AAAA|
528 * +--------+
529 * A = v
530 *
531 * 0x8f <= v < 0x408f:
532 * +--------+--------+
533 * |11AAAAAA|BBBBBBBB|
534 * +--------+--------+
535 * A = ((v - 0x8f) >> 8)
536 * B = ((v - 0x8f) & 0xff)
537 *
538 * 0x408f <= v < 0x20408f:
539 * +--------+--------+--------+
540 * |101AAAAA|BBBBBBBB|CCCCCCCC|
541 * +--------+--------+--------+
542 * A = ((v - 0x408f) >> 16)
543 * B = ((v - 0x408f) >> 8) & 0xff
544 * C = ((v - 0x408f) & 0xff)
545 *
546 * 0x20408f <= v < 0x1020408f:
547 * +--------+--------+--------+--------+
548 * |1001AAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
549 * +--------+--------+--------+--------+
550 * A = ((v - 0x20408f) >> 24)
551 * B = ((v - 0x20408f) >> 16) & 0xff
552 * C = ((v - 0x20408f) >> 8) & 0xff
553 * D = ((v - 0x20408f) & 0xff)
554 *
555 * 0x1020408f <= v:
556 * +--------+--------+--------+--------+--------+
557 * |10001111|AAAAAAAA|BBBBBBBB|CCCCCCCC|DDDDDDDD|
558 * +--------+--------+--------+--------+--------+
559 * A = (v >> 24) & 0xff
560 * B = (v >> 16) & 0xff
561 * C = (v >> 8) & 0xff
562 * D = (v & 0xff)
563 * */
564 #define GRN_B_ENC(v, p) \
565 do { \
566 uint8_t *_p = (uint8_t *)p; \
567 uint32_t _v = v; \
568 if (_v < 0x8f) { \
569 *_p++ = (uint8_t)_v; \
570 } else if (_v < 0x408f) { \
571 _v -= 0x8f; \
572 *_p++ = (uint8_t)(0xc0 + (_v >> 8)); \
573 *_p++ = _v & 0xff; \
574 } else if (_v < 0x20408f) { \
575 _v -= 0x408f; \
576 *_p++ = (uint8_t)(0xa0 + (_v >> 16)); \
577 *_p++ = (_v >> 8) & 0xff; \
578 *_p++ = _v & 0xff; \
579 } else if (_v < 0x1020408f) { \
580 _v -= 0x20408f; \
581 *_p++ = (uint8_t)(0x90 + (_v >> 24)); \
582 *_p++ = (_v >> 16) & 0xff; \
583 *_p++ = (_v >> 8) & 0xff; \
584 *_p++ = _v & 0xff; \
585 } else { \
586 *_p++ = 0x8f; \
587 grn_memcpy(_p, &_v, sizeof(uint32_t)); \
588 _p += sizeof(uint32_t); \
589 } \
590 p = _p; \
591 } while (0)
592
593 #define GRN_B_ENC_SIZE(v) \
594 ((v) < 0x8f \
595 ? 1 \
596 : ((v) < 0x408f ? 2 : ((v) < 0x20408f ? 3 : ((v) < 0x1020408f ? 4 : 5))))
597
598 #define GRN_B_DEC(v, p) \
599 do { \
600 uint8_t *_p = (uint8_t *)p; \
601 uint32_t _v = *_p++; \
602 switch (_v >> 4) { \
603 case 0x08: \
604 if (_v == 0x8f) { \
605 grn_memcpy(&_v, _p, sizeof(uint32_t)); \
606 _p += sizeof(uint32_t); \
607 } \
608 break; \
609 case 0x09: \
610 _v = (_v - 0x90) * 0x100 + *_p++; \
611 _v = _v * 0x100 + *_p++; \
612 _v = _v * 0x100 + *_p++ + 0x20408f; \
613 break; \
614 case 0x0a: \
615 case 0x0b: \
616 _v = (_v - 0xa0) * 0x100 + *_p++; \
617 _v = _v * 0x100 + *_p++ + 0x408f; \
618 break; \
619 case 0x0c: \
620 case 0x0d: \
621 case 0x0e: \
622 case 0x0f: \
623 _v = (_v - 0xc0) * 0x100 + *_p++ + 0x8f; \
624 break; \
625 } \
626 v = _v; \
627 p = _p; \
628 } while (0)
629
630 #define GRN_B_SKIP(p) \
631 do { \
632 uint8_t *_p = (uint8_t *)p; \
633 uint32_t _v = *_p++; \
634 switch (_v >> 4) { \
635 case 0x08: \
636 if (_v == 0x8f) { \
637 _p += sizeof(uint32_t); \
638 } \
639 break; \
640 case 0x09: \
641 _p += 3; \
642 break; \
643 case 0x0a: \
644 case 0x0b: \
645 _p += 2; \
646 break; \
647 case 0x0c: \
648 case 0x0d: \
649 case 0x0e: \
650 case 0x0f: \
651 _p += 1; \
652 break; \
653 } \
654 p = _p; \
655 } while (0)
656
657 #define GRN_B_COPY(p2, p1) \
658 do { \
659 uint32_t size = 0, _v = *p1++; \
660 *p2++ = _v; \
661 switch (_v >> 4) { \
662 case 0x08: \
663 size = (_v == 0x8f) ? 4 : 0; \
664 break; \
665 case 0x09: \
666 size = 3; \
667 break; \
668 case 0x0a: \
669 case 0x0b: \
670 size = 2; \
671 break; \
672 case 0x0c: \
673 case 0x0d: \
674 case 0x0e: \
675 case 0x0f: \
676 size = 1; \
677 break; \
678 } \
679 while (size--) { \
680 *p2++ = *p1++; \
681 } \
682 } while (0)
683
684 #ifdef __cplusplus
685 }
686 #endif