Coverage Report

Created: 2026-06-01 12:33

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
inline void encode_fixed8(uint8_t* buf, uint8_t val) {
29
    *buf = val;
30
}
31
32
208k
inline void encode_fixed16_le(uint8_t* buf, uint16_t val) {
33
208k
    val = to_endian<std::endian::little>(val);
34
208k
    memcpy(buf, &val, sizeof(val));
35
208k
}
36
37
4.33M
inline void encode_fixed32_le(uint8_t* buf, uint32_t val) {
38
4.33M
    val = to_endian<std::endian::little>(val);
39
4.33M
    memcpy(buf, &val, sizeof(val));
40
4.33M
}
41
42
128k
inline void encode_fixed64_le(uint8_t* buf, uint64_t val) {
43
128k
    val = to_endian<std::endian::little>(val);
44
128k
    memcpy(buf, &val, sizeof(val));
45
128k
}
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
530k
inline uint8_t decode_fixed8(const uint8_t* buf) {
53
530k
    return *buf;
54
530k
}
55
56
254k
inline uint16_t decode_fixed16_le(const uint8_t* buf) {
57
254k
    uint16_t res;
58
254k
    memcpy(&res, buf, sizeof(res));
59
254k
    return to_endian<std::endian::little>(res);
60
254k
}
61
62
61.9M
inline uint32_t decode_fixed32_le(const uint8_t* buf) {
63
61.9M
    uint32_t res;
64
61.9M
    memcpy(&res, buf, sizeof(res));
65
61.9M
    return to_endian<std::endian::little>(res);
66
61.9M
}
67
68
69.6k
inline uint64_t decode_fixed64_le(const uint8_t* buf) {
69
69.6k
    uint64_t res;
70
69.6k
    memcpy(&res, buf, sizeof(res));
71
69.6k
    return to_endian<std::endian::little>(res);
72
69.6k
}
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
2.74M
void put_fixed32_le(T* dst, uint32_t val) {
82
2.74M
    uint8_t buf[sizeof(val)];
83
2.74M
    encode_fixed32_le(buf, val);
84
2.74M
    dst->append((char*)buf, sizeof(buf));
85
2.74M
}
_ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j
Line
Count
Source
81
397k
void put_fixed32_le(T* dst, uint32_t val) {
82
397k
    uint8_t buf[sizeof(val)];
83
397k
    encode_fixed32_le(buf, val);
84
397k
    dst->append((char*)buf, sizeof(buf));
85
397k
}
_ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j
Line
Count
Source
81
2.34M
void put_fixed32_le(T* dst, uint32_t val) {
82
2.34M
    uint8_t buf[sizeof(val)];
83
2.34M
    encode_fixed32_le(buf, val);
84
2.34M
    dst->append((char*)buf, sizeof(buf));
85
2.34M
}
86
87
template <typename T>
88
59.7k
void put_fixed64_le(T* dst, uint64_t val) {
89
59.7k
    uint8_t buf[sizeof(val)];
90
59.7k
    encode_fixed64_le(buf, val);
91
59.7k
    dst->append((char*)buf, sizeof(buf));
92
59.7k
}
_ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m
Line
Count
Source
88
35.3k
void put_fixed64_le(T* dst, uint64_t val) {
89
35.3k
    uint8_t buf[sizeof(val)];
90
35.3k
    encode_fixed64_le(buf, val);
91
35.3k
    dst->append((char*)buf, sizeof(buf));
92
35.3k
}
_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
354k
inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) {
115
354k
    static const unsigned int B = 128;
116
739k
    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
385k
        *(dst++) = uint8_t(v | B);
121
385k
        v >>= 7;
122
385k
    }
123
354k
    *(dst++) = static_cast<unsigned char>(v);
124
354k
    return dst;
125
354k
}
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
11.7M
                                          uint32_t* value) {
132
11.7M
    if (ptr < limit) {
133
11.7M
        uint32_t result = *ptr;
134
11.7M
        if ((result & 128) == 0) {
135
11.7M
            *value = result;
136
11.7M
            return ptr + 1;
137
11.7M
        }
138
11.7M
    }
139
32.3k
    return decode_varint32_ptr_fallback(ptr, limit, value);
140
11.7M
}
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
974k
void put_varint32(T* dst, uint32_t v) {
146
974k
    uint8_t buf[16];
147
974k
    uint8_t* ptr = encode_varint32(buf, v);
148
974k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
149
974k
}
150
151
template <typename T>
152
void put_varint64(T* dst, uint64_t v) {
153
    uint8_t buf[16];
154
    uint8_t* ptr = encode_varint64(buf, v);
155
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
156
}
157
158
template <typename T>
159
354k
void put_length_prefixed_slice(T* dst, const Slice& value) {
160
354k
    put_varint32(dst, uint32_t(value.get_size()));
161
354k
    dst->append(value.get_data(), value.get_size());
162
354k
}
163
164
template <typename T>
165
354k
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
166
354k
    uint8_t buf[16];
167
354k
    uint8_t* ptr = encode_varint64(buf, v1);
168
354k
    ptr = encode_varint32(ptr, v2);
169
354k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
170
354k
}
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
100k
inline bool get_varint32(Slice* input, uint32_t* val) {
176
100k
    const auto* p = (const uint8_t*)input->data;
177
100k
    const uint8_t* limit = p + input->size;
178
100k
    const uint8_t* q = decode_varint32_ptr(p, limit, val);
179
100k
    if (q == nullptr) {
180
0
        return false;
181
100k
    } else {
182
100k
        *input = Slice(q, limit - q);
183
100k
        return true;
184
100k
    }
185
100k
}
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
21.8k
inline bool get_varint64(Slice* input, uint64_t* val) {
191
21.8k
    const auto* p = (const uint8_t*)input->data;
192
21.8k
    const uint8_t* limit = p + input->size;
193
21.8k
    const uint8_t* q = decode_varint64_ptr(p, limit, val);
194
21.8k
    if (q == nullptr) {
195
0
        return false;
196
21.8k
    } else {
197
21.8k
        *input = Slice(q, limit - q);
198
21.8k
        return true;
199
21.8k
    }
200
21.8k
}
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
21.8k
inline bool get_length_prefixed_slice(Slice* input, Slice* val) {
206
21.8k
    uint32_t len;
207
21.8k
    if (get_varint32(input, &len) && input->get_size() >= len) {
208
21.8k
        *val = Slice(input->get_data(), len);
209
21.8k
        input->remove_prefix(len);
210
21.8k
        return true;
211
21.8k
    } else {
212
0
        return false;
213
0
    }
214
21.8k
}
215
} // namespace doris