Coverage Report

Created: 2026-05-09 18:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/coding.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6
// Use of this source code is governed by a BSD-style license that can be
7
// found in the LICENSE file. See the AUTHORS file for names of contributors.
8
9
#pragma once
10
11
#include <bit>
12
13
#ifndef __APPLE__
14
#include <endian.h>
15
#endif
16
#include <stdint.h>
17
#include <string.h>
18
19
#include "exec/common/endian.h"
20
#include "storage/olap_common.h"
21
#include "util/slice.h"
22
23
namespace doris {
24
// TODO(zc): add encode big endian later when we need it
25
// use big endian when we have order requirement.
26
// little endian is more efficient when we use X86 CPU, so
27
// when we have no order needs, we prefer little endian encoding
28
1
inline void encode_fixed8(uint8_t* buf, uint8_t val) {
29
1
    *buf = val;
30
1
}
31
32
985
inline void encode_fixed16_le(uint8_t* buf, uint16_t val) {
33
985
    val = to_endian<std::endian::little>(val);
34
985
    memcpy(buf, &val, sizeof(val));
35
985
}
36
37
774k
inline void encode_fixed32_le(uint8_t* buf, uint32_t val) {
38
774k
    val = to_endian<std::endian::little>(val);
39
774k
    memcpy(buf, &val, sizeof(val));
40
774k
}
41
42
74.2k
inline void encode_fixed64_le(uint8_t* buf, uint64_t val) {
43
74.2k
    val = to_endian<std::endian::little>(val);
44
74.2k
    memcpy(buf, &val, sizeof(val));
45
74.2k
}
46
47
24.4k
inline void encode_fixed128_le(uint8_t* buf, uint128_t val) {
48
24.4k
    val = to_endian<std::endian::little>(val);
49
24.4k
    memcpy(buf, &val, sizeof(val));
50
24.4k
}
51
52
130k
inline uint8_t decode_fixed8(const uint8_t* buf) {
53
130k
    return *buf;
54
130k
}
55
56
47.5k
inline uint16_t decode_fixed16_le(const uint8_t* buf) {
57
47.5k
    uint16_t res;
58
47.5k
    memcpy(&res, buf, sizeof(res));
59
47.5k
    return to_endian<std::endian::little>(res);
60
47.5k
}
61
62
583k
inline uint32_t decode_fixed32_le(const uint8_t* buf) {
63
583k
    uint32_t res;
64
583k
    memcpy(&res, buf, sizeof(res));
65
583k
    return to_endian<std::endian::little>(res);
66
583k
}
67
68
49.5k
inline uint64_t decode_fixed64_le(const uint8_t* buf) {
69
49.5k
    uint64_t res;
70
49.5k
    memcpy(&res, buf, sizeof(res));
71
49.5k
    return to_endian<std::endian::little>(res);
72
49.5k
}
73
74
24.4k
inline uint128_t decode_fixed128_le(const uint8_t* buf) {
75
24.4k
    uint128_t res;
76
24.4k
    memcpy(&res, buf, sizeof(res));
77
24.4k
    return to_endian<std::endian::little>(res);
78
24.4k
}
79
80
template <typename T>
81
379k
void put_fixed32_le(T* dst, uint32_t val) {
82
379k
    uint8_t buf[sizeof(val)];
83
379k
    encode_fixed32_le(buf, val);
84
379k
    dst->append((char*)buf, sizeof(buf));
85
379k
}
_ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j
Line
Count
Source
81
36.9k
void put_fixed32_le(T* dst, uint32_t val) {
82
36.9k
    uint8_t buf[sizeof(val)];
83
36.9k
    encode_fixed32_le(buf, val);
84
36.9k
    dst->append((char*)buf, sizeof(buf));
85
36.9k
}
_ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j
Line
Count
Source
81
342k
void put_fixed32_le(T* dst, uint32_t val) {
82
342k
    uint8_t buf[sizeof(val)];
83
342k
    encode_fixed32_le(buf, val);
84
342k
    dst->append((char*)buf, sizeof(buf));
85
342k
}
86
87
template <typename T>
88
25.0k
void put_fixed64_le(T* dst, uint64_t val) {
89
25.0k
    uint8_t buf[sizeof(val)];
90
25.0k
    encode_fixed64_le(buf, val);
91
25.0k
    dst->append((char*)buf, sizeof(buf));
92
25.0k
}
_ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m
Line
Count
Source
88
615
void put_fixed64_le(T* dst, uint64_t val) {
89
615
    uint8_t buf[sizeof(val)];
90
615
    encode_fixed64_le(buf, val);
91
615
    dst->append((char*)buf, sizeof(buf));
92
615
}
_ZN5doris14put_fixed64_leINS_10faststringEEEvPT_m
Line
Count
Source
88
24.4k
void put_fixed64_le(T* dst, uint64_t val) {
89
24.4k
    uint8_t buf[sizeof(val)];
90
24.4k
    encode_fixed64_le(buf, val);
91
24.4k
    dst->append((char*)buf, sizeof(buf));
92
24.4k
}
93
94
// Returns the length of the varint32 or varint64 encoding of "v"
95
13
inline int varint_length(uint64_t v) {
96
13
    int len = 1;
97
13
    while (v >= 128) {
98
0
        v >>= 7;
99
0
        len++;
100
0
    }
101
13
    return len;
102
13
}
103
104
template <typename T>
105
24.4k
void put_fixed128_le(T* dst, uint128_t val) {
106
24.4k
    uint8_t buf[sizeof(val)];
107
24.4k
    encode_fixed128_le(buf, val);
108
24.4k
    dst->append((char*)buf, sizeof(buf));
109
24.4k
}
110
111
extern uint8_t* encode_varint32(uint8_t* dst, uint32_t value);
112
extern uint8_t* encode_varint64(uint8_t* dst, uint64_t value);
113
114
43.6k
inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) {
115
43.6k
    static const unsigned int B = 128;
116
133k
    while (v >= B) {
117
        // Fetch low seven bits from current v, and the eight bit is marked as compression mark.
118
        // v | B is optimised from (v & (B-1)) | B, because result is assigned to uint8_t and other bits
119
        // is cleared by implicit conversion.
120
89.6k
        *(dst++) = uint8_t(v | B);
121
89.6k
        v >>= 7;
122
89.6k
    }
123
43.6k
    *(dst++) = static_cast<unsigned char>(v);
124
43.6k
    return dst;
125
43.6k
}
126
127
extern const uint8_t* decode_varint32_ptr_fallback(const uint8_t* p, const uint8_t* limit,
128
                                                   uint32_t* value);
129
130
inline const uint8_t* decode_varint32_ptr(const uint8_t* ptr, const uint8_t* limit,
131
1.11M
                                          uint32_t* value) {
132
1.11M
    if (ptr < limit) {
133
1.11M
        uint32_t result = *ptr;
134
1.11M
        if ((result & 128) == 0) {
135
1.08M
            *value = result;
136
1.08M
            return ptr + 1;
137
1.08M
        }
138
1.11M
    }
139
28.9k
    return decode_varint32_ptr_fallback(ptr, limit, value);
140
1.11M
}
141
142
extern const uint8_t* decode_varint64_ptr(const uint8_t* p, const uint8_t* limit, uint64_t* value);
143
144
template <typename T>
145
330k
void put_varint32(T* dst, uint32_t v) {
146
330k
    uint8_t buf[16];
147
330k
    uint8_t* ptr = encode_varint32(buf, v);
148
330k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
149
330k
}
_ZN5doris12put_varint32INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j
Line
Count
Source
145
1
void put_varint32(T* dst, uint32_t v) {
146
1
    uint8_t buf[16];
147
1
    uint8_t* ptr = encode_varint32(buf, v);
148
1
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
149
1
}
_ZN5doris12put_varint32INS_10faststringEEEvPT_j
Line
Count
Source
145
330k
void put_varint32(T* dst, uint32_t v) {
146
330k
    uint8_t buf[16];
147
330k
    uint8_t* ptr = encode_varint32(buf, v);
148
330k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
149
330k
}
150
151
template <typename T>
152
1
void put_varint64(T* dst, uint64_t v) {
153
1
    uint8_t buf[16];
154
1
    uint8_t* ptr = encode_varint64(buf, v);
155
1
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
156
1
}
157
158
template <typename T>
159
43.6k
void put_length_prefixed_slice(T* dst, const Slice& value) {
160
43.6k
    put_varint32(dst, uint32_t(value.get_size()));
161
43.6k
    dst->append(value.get_data(), value.get_size());
162
43.6k
}
163
164
template <typename T>
165
43.6k
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
166
43.6k
    uint8_t buf[16];
167
43.6k
    uint8_t* ptr = encode_varint64(buf, v1);
168
43.6k
    ptr = encode_varint32(ptr, v2);
169
43.6k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
170
43.6k
}
_ZN5doris21put_varint64_varint32INS_10faststringEEEvPT_mj
Line
Count
Source
165
43.6k
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
166
43.6k
    uint8_t buf[16];
167
43.6k
    uint8_t* ptr = encode_varint64(buf, v1);
168
43.6k
    ptr = encode_varint32(ptr, v2);
169
43.6k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
170
43.6k
}
_ZN5doris21put_varint64_varint32INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_mj
Line
Count
Source
165
1
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
166
1
    uint8_t buf[16];
167
1
    uint8_t* ptr = encode_varint64(buf, v1);
168
1
    ptr = encode_varint32(ptr, v2);
169
1
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
170
1
}
171
172
// parse a varint32 from the start of `input` into `val`.
173
// on success, return true and advance `input` past the parsed value.
174
// on failure, return false and `input` is not modified.
175
62.6k
inline bool get_varint32(Slice* input, uint32_t* val) {
176
62.6k
    const auto* p = (const uint8_t*)input->data;
177
62.6k
    const uint8_t* limit = p + input->size;
178
62.6k
    const uint8_t* q = decode_varint32_ptr(p, limit, val);
179
62.6k
    if (q == nullptr) {
180
0
        return false;
181
62.6k
    } else {
182
62.6k
        *input = Slice(q, limit - q);
183
62.6k
        return true;
184
62.6k
    }
185
62.6k
}
186
187
// parse a varint64 from the start of `input` into `val`.
188
// on success, return true and advance `input` past the parsed value.
189
// on failure, return false and `input` is not modified.
190
20.4k
inline bool get_varint64(Slice* input, uint64_t* val) {
191
20.4k
    const auto* p = (const uint8_t*)input->data;
192
20.4k
    const uint8_t* limit = p + input->size;
193
20.4k
    const uint8_t* q = decode_varint64_ptr(p, limit, val);
194
20.4k
    if (q == nullptr) {
195
0
        return false;
196
20.4k
    } else {
197
20.4k
        *input = Slice(q, limit - q);
198
20.4k
        return true;
199
20.4k
    }
200
20.4k
}
201
202
// parse a length-prefixed-slice from the start of `input` into `val`.
203
// on success, return true and advance `input` past the parsed value.
204
// on failure, return false and `input` may or may not be modified.
205
20.4k
inline bool get_length_prefixed_slice(Slice* input, Slice* val) {
206
20.4k
    uint32_t len;
207
20.4k
    if (get_varint32(input, &len) && input->get_size() >= len) {
208
20.4k
        *val = Slice(input->get_data(), len);
209
20.4k
        input->remove_prefix(len);
210
20.4k
        return true;
211
20.4k
    } else {
212
0
        return false;
213
0
    }
214
20.4k
}
215
} // namespace doris