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