Debian Codegraph @

storage/innobase/include/fts0vlc.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 2007, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/fts0vlc.ic
29 Full text variable length integer encoding/decoding.
30
31 Created 2007-03-27 Sunny Bains
32 *******************************************************/
33
34 #ifndef INNOBASE_FTS0VLC_IC
35 #define INNOBASE_FTS0VLC_IC
36
37 #include <cstddef>
38 #include <cstdint>
39 #include "fts0types.h"
40
41 inline unsigned int fts_get_encoded_len(uint64_t val) {
42 unsigned int length = 1;
43 for (;;) {
44 val >>= 7;
45 if (val != 0) {
46 ++length;
47 } else {
48 break;
49 }
50 }
51 return length;
52 }
53
54 inline unsigned int fts_encode_int(uint64_t val, byte *buf) {
55 constexpr unsigned int max_length = 10;
56
57 /* skip leading zeros */
58 unsigned int count = max_length - 1;
59 while (count > 0) {
60 /* We split the value into 7 bit batches); so val >= 2^63 need 10 bytes,
61 2^63 > val >= 2^56 needs 9 bytes, 2^56 > val >= 2^49 needs 8 bytes etc.
62 */
63 if (val >= uint64_t(1) << (count * 7)) {
64 break;
65 }
66 --count;
67 }
68
69 unsigned int length = count + 1;
70
71 byte *bufptr{buf};
72
73 for (;;) {
74 *bufptr = (byte)((val >> (7 * count)) & 0x7f);
75 if (count == 0) {
76 /* High-bit on means "last byte in the encoded integer". */
77 *bufptr |= 0x80;
78 break;
79 } else {
80 --count;
81 ++bufptr;
82 }
83 }
84
85 ut_ad(length <= max_length);
86 ut_a(bufptr - buf == std::ptrdiff_t(length) - 1);
87
88 return length;
89 }
90
91 inline uint64_t fts_decode_vlc(byte **ptr) {
92 uint64_t val = 0;
93
94 for (;;) {
95 byte b = **ptr;
96
97 ++*ptr;
98 val |= (b & 0x7F);
99
100 /* High-bit on means "last byte in the encoded integer". */
101 if (b & 0x80) {
102 break;
103 } else {
104 val <<= 7;
105 }
106 }
107
108 return val;
109 }
110
111 #endif